Added authentication check facility to zebra.
[idzebra-moved-to-github.git] / index / zserver.c
1 /*
2  * Copyright (C) 1995-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zserver.c,v $
7  * Revision 1.60  1998-06-22 11:36:49  adam
8  * Added authentication check facility to zebra.
9  *
10  * Revision 1.59  1998/06/12 12:22:13  adam
11  * Work on Zebra API.
12  *
13  * Revision 1.58  1998/05/27 16:57:46  adam
14  * Zebra returns surrogate diagnostic for single records when
15  * appropriate.
16  *
17  * Revision 1.57  1998/04/03 14:45:18  adam
18  * Fixed setting of last_in_set in bend_fetch.
19  *
20  * Revision 1.56  1998/03/05 08:45:13  adam
21  * New result set model and modular ranking system. Moved towards
22  * descent server API. System information stored as "SGML" records.
23  *
24  * Revision 1.55  1998/02/10 12:03:06  adam
25  * Implemented Sort.
26  *
27  * Revision 1.54  1998/01/29 13:39:13  adam
28  * Compress ISAM is default.
29  *
30  * Revision 1.53  1998/01/12 15:04:09  adam
31  * The test option (-s) only uses read-lock (and not write lock).
32  *
33  * Revision 1.52  1997/11/18 10:05:08  adam
34  * Changed character map facility so that admin can specify character
35  * mapping files for each register type, w, p, etc.
36  *
37  * Revision 1.51  1997/10/27 14:33:06  adam
38  * Moved towards generic character mapping depending on "structure"
39  * field in abstract syntax file. Fixed a few memory leaks. Fixed
40  * bug with negative integers when doing searches with relational
41  * operators.
42  *
43  * Revision 1.50  1997/09/29 09:08:36  adam
44  * Revised locking system to be thread safe for the server.
45  *
46  * Revision 1.49  1997/09/25 14:57:23  adam
47  * Windows NT port.
48  *
49  * Revision 1.48  1997/09/17 12:19:19  adam
50  * Zebra version corresponds to YAZ version 1.4.
51  * Changed Zebra server so that it doesn't depend on global common_resource.
52  *
53  * Revision 1.47  1997/09/04 13:58:36  adam
54  * New retrieve/extract method tellf (added).
55  * Added O_BINARY for open calls.
56  *
57  * Revision 1.46  1997/07/28 08:30:47  adam
58  * Server returns diagnostic 14 when record doesn't exist.
59  *
60  * Revision 1.45  1996/12/23 15:30:45  adam
61  * Work on truncation.
62  * Bug fix: result sets weren't deleted after server shut down.
63  *
64  * Revision 1.44  1996/12/11 12:08:01  adam
65  * Added better compression.
66  *
67  * Revision 1.43  1996/11/15 15:03:58  adam
68  * Logging of execution speed by using the times(2) call.
69  *
70  * Revision 1.42  1996/11/08  11:10:36  adam
71  * Buffers used during file match got bigger.
72  * Compressed ISAM support everywhere.
73  * Bug fixes regarding masking characters in queries.
74  * Redesigned Regexp-2 queries.
75  *
76  * Revision 1.41  1996/10/29 14:09:56  adam
77  * Use of cisam system - enabled if setting isamc is 1.
78  *
79  * Revision 1.40  1996/06/04 10:19:02  adam
80  * Minor changes - removed include of ctype.h.
81  *
82  * Revision 1.39  1996/05/31  09:07:05  quinn
83  * Work on character-set handling
84  *
85  * Revision 1.38  1996/05/14  11:34:01  adam
86  * Scan support in multiple registers/databases.
87  *
88  * Revision 1.37  1996/05/14  06:16:48  adam
89  * Compact use/set bytes used in search service.
90  *
91  * Revision 1.36  1996/05/01 13:46:37  adam
92  * First work on multiple records in one file.
93  * New option, -offset, to the "unread" command in the filter module.
94  *
95  * Revision 1.35  1996/03/26  16:01:14  adam
96  * New setting lockPath: directory of various lock files.
97  *
98  * Revision 1.34  1996/03/20  09:36:46  adam
99  * Function dict_lookup_grep got extra parameter, init_pos, which marks
100  * from which position in pattern approximate pattern matching should occur.
101  * Approximate pattern matching is used in relevance=re-2.
102  *
103  * Revision 1.33  1996/01/17  14:57:56  adam
104  * Prototype changed for reader functions in extract/retrieve. File
105  *  is identified by 'void *' instead of 'int.
106  *
107  * Revision 1.32  1995/12/11  09:12:58  adam
108  * The rec_get function returns NULL if record doesn't exist - will
109  * happen in the server if the result set records have been deleted since
110  * the creation of the set (i.e. the search).
111  * The server saves a result temporarily if it is 'volatile', i.e. the
112  * set is register dependent.
113  *
114  * Revision 1.31  1995/12/08  16:22:56  adam
115  * Work on update while servers are running. Three lock files introduced.
116  * The servers reload their registers when necessary, but they don't
117  * reestablish result sets yet.
118  *
119  * Revision 1.30  1995/12/07  17:38:48  adam
120  * Work locking mechanisms for concurrent updates/commit.
121  *
122  * Revision 1.29  1995/12/04  14:22:32  adam
123  * Extra arg to recType_byName.
124  * Started work on new regular expression parsed input to
125  * structured records.
126  *
127  * Revision 1.28  1995/11/28  09:09:48  adam
128  * Zebra config renamed.
129  * Use setting 'recordId' to identify record now.
130  * Bug fix in recindex.c: rec_release_blocks was invokeded even
131  * though the blocks were already released.
132  * File traversal properly deletes records when needed.
133  *
134  * Revision 1.27  1995/11/27  13:58:54  adam
135  * New option -t. storeStore data implemented in server.
136  *
137  * Revision 1.26  1995/11/25  10:24:07  adam
138  * More record fields - they are enumerated now.
139  * New options: flagStoreData flagStoreKey.
140  *
141  * Revision 1.25  1995/11/21  15:29:13  adam
142  * Config file 'base' read by default by both indexer and server.
143  *
144  * Revision 1.24  1995/11/20  16:59:47  adam
145  * New update method: the 'old' keys are saved for each records.
146  *
147  * Revision 1.23  1995/11/16  17:00:56  adam
148  * Better logging of rpn query.
149  *
150  * Revision 1.22  1995/11/16  15:34:55  adam
151  * Uses new record management system in both indexer and server.
152  *
153  * Revision 1.21  1995/11/01  16:25:52  quinn
154  * *** empty log message ***
155  *
156  * Revision 1.20  1995/10/27  14:00:12  adam
157  * Implemented detection of database availability.
158  *
159  * Revision 1.19  1995/10/17  18:02:11  adam
160  * New feature: databases. Implemented as prefix to words in dictionary.
161  *
162  * Revision 1.18  1995/10/16  14:03:09  quinn
163  * Changes to support element set names and espec1
164  *
165  * Revision 1.17  1995/10/16  09:32:40  adam
166  * More work on relational op.
167  *
168  * Revision 1.16  1995/10/13  12:26:44  adam
169  * Optimization of truncation.
170  *
171  * Revision 1.15  1995/10/12  12:40:55  adam
172  * Bug fixes in rpn_prox.
173  *
174  * Revision 1.14  1995/10/09  16:18:37  adam
175  * Function dict_lookup_grep got extra client data parameter.
176  *
177  * Revision 1.13  1995/10/06  14:38:00  adam
178  * New result set method: r_score.
179  * Local no (sysno) and score is transferred to retrieveCtrl.
180  *
181  * Revision 1.12  1995/10/06  13:52:06  adam
182  * Bug fixes. Handler may abort further scanning.
183  *
184  * Revision 1.11  1995/10/06  10:43:57  adam
185  * Scan added. 'occurrences' in scan entries not set yet.
186  *
187  * Revision 1.10  1995/10/02  16:43:32  quinn
188  * Set default resulting record type in fetch.
189  *
190  * Revision 1.9  1995/10/02  15:18:52  adam
191  * New member in recRetrieveCtrl: diagnostic.
192  *
193  * Revision 1.8  1995/09/28  09:19:47  adam
194  * xfree/xmalloc used everywhere.
195  * Extract/retrieve method seems to work for text records.
196  *
197  * Revision 1.7  1995/09/27  16:17:32  adam
198  * More work on retrieve.
199  *
200  * Revision 1.6  1995/09/08  08:53:22  adam
201  * Record buffer maintained in server_info.
202  *
203  * Revision 1.5  1995/09/06  16:11:18  adam
204  * Option: only one word key per file.
205  *
206  * Revision 1.4  1995/09/06  10:33:04  adam
207  * More work on present. Some log messages removed.
208  *
209  * Revision 1.3  1995/09/05  15:28:40  adam
210  * More work on search engine.
211  *
212  * Revision 1.2  1995/09/04  12:33:43  adam
213  * Various cleanup. YAZ util used instead.
214  *
215  * Revision 1.1  1995/09/04  09:10:41  adam
216  * More work on index add/del/update.
217  * Merge sort implemented.
218  * Initial work on z39 server.
219  *
220  */
221
222 #include <stdio.h>
223 #include <assert.h>
224 #include <fcntl.h>
225 #ifdef WINDOWS
226 #include <io.h>
227 #include <process.h>
228 #else
229 #include <unistd.h>
230 #endif
231
232 #include <data1.h>
233 #include <dmalloc.h>
234
235 #include "zserver.h"
236
237 static int bend_sort (void *handle, bend_sort_rr *rr);
238
239 bend_initresult *bend_init (bend_initrequest *q)
240 {
241     bend_initresult *r = odr_malloc (q->stream, sizeof(*r));
242     ZebraHandle zh;
243     struct statserv_options_block *sob;
244     char *user = NULL;
245     char *passwd = NULL;
246
247     r->errcode = 0;
248     r->errstring = 0;
249     q->bend_sort = bend_sort;
250
251     logf (LOG_DEBUG, "bend_init");
252
253     sob = statserv_getcontrol ();
254     if (!(zh = zebra_open (sob->configname)))
255     {
256         logf (LOG_FATAL, "Failed to open Zebra `%s'", sob->configname);
257         r->errcode = 1;
258         return r;
259     }
260     if (q->auth)
261     {
262         if (q->auth->which == Z_IdAuthentication_open)
263         {
264             char *openpass = xstrdup (q->auth->u.open);
265             char *cp = strchr (openpass, '/');
266             if (cp)
267             {
268                 *cp = '\0';
269                 user = nmem_strdup (odr_getmem (q->stream), openpass);
270                 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
271             }
272             xfree (openpass);
273         }
274     }
275     if (zebra_auth (zh, user, passwd))
276     {
277         r->errcode = 222;
278         r->errstring = user;
279         zebra_close (zh);
280         return r;
281     }
282     r->handle = zh;
283     return r;
284 }
285
286 bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
287 {
288     ZebraHandle zh = handle;
289     bend_searchresult *r = odr_malloc (q->stream, sizeof(*r));
290
291     r->hits = 0;
292     r->errcode = 0;
293     r->errstring = NULL;
294     
295     logf (LOG_LOG, "ResultSet '%s'", q->setname);
296     switch (q->query->which)
297     {
298     case Z_Query_type_1: case Z_Query_type_101:
299         zebra_search_rpn (zh, q->stream, q->query->u.type_1,
300                           q->num_bases, q->basenames, q->setname);
301         r->errcode = zh->errCode;
302         r->errstring = zh->errString;
303         r->hits = zh->hits;
304         break;
305     default:
306         r->errcode = 107;
307     }
308     return r;
309 }
310
311 bend_fetchresult *bend_fetch (void *handle, bend_fetchrequest *q, int *num)
312 {
313     ZebraHandle zh = handle;
314     bend_fetchresult *r = odr_malloc (q->stream, sizeof(*r));
315     ZebraRetrievalRecord retrievalRecord;
316
317     retrievalRecord.position = q->number;
318     
319     r->last_in_set = 0;
320     zebra_records_retrieve (zh, q->stream, q->setname, q->comp,
321                             q->format, 1, &retrievalRecord);
322     if (zh->errCode)                  /* non Surrogate Diagnostic */
323     {
324         r->errcode = zh->errCode;
325         r->errstring = zh->errString;
326     }
327     else if (retrievalRecord.errCode) /* Surrogate Diagnostic */
328     {
329         q->surrogate_flag = 1;
330         r->errcode = retrievalRecord.errCode;
331         r->errstring = retrievalRecord.errString;
332         r->basename = retrievalRecord.base;
333     }
334     else                              /* Database Record */
335     {
336         r->errcode = 0;
337         r->basename = retrievalRecord.base;
338         r->record = retrievalRecord.buf;
339         r->len = retrievalRecord.len;
340         r->format = retrievalRecord.format;
341     }
342     return r;
343 }
344
345 bend_deleteresult *bend_delete (void *handle, bend_deleterequest *q, int *num)
346 {
347     return 0;
348 }
349
350 bend_scanresult *bend_scan (void *handle, bend_scanrequest *q, int *num)
351 {
352     ZebraScanEntry *entries;
353     ZebraHandle zh = handle;
354     bend_scanresult *r = odr_malloc (q->stream, sizeof(*r));
355     int is_partial, i;
356     
357     r->term_position = q->term_position;
358     r->num_entries = q->num_entries;
359
360     r->entries = odr_malloc (q->stream, sizeof(*r->entries) * q->num_entries);
361     zebra_scan (zh, q->stream, q->term,
362                 q->attributeset,
363                 q->num_bases, q->basenames,
364                 &r->term_position,
365                 &r->num_entries, &entries, &is_partial);
366     if (is_partial)
367         r->status = BEND_SCAN_PARTIAL;
368     else
369         r->status = BEND_SCAN_SUCCESS;
370     for (i = 0; i < r->num_entries; i++)
371     {
372         r->entries[i].term = entries[i].term;
373         r->entries[i].occurrences = entries[i].occurrences;
374     }
375     r->errcode = zh->errCode;
376     r->errstring = zh->errString;
377     return r;
378 }
379
380 void bend_close (void *handle)
381 {
382     zebra_close ((ZebraHandle) handle);
383 }
384
385 int bend_sort (void *handle, bend_sort_rr *rr)
386 {
387     ZebraHandle zh = handle;
388
389     zebra_sort (zh, rr->stream, rr->num_input_setnames, rr->input_setnames,
390                 rr->output_setname, rr->sort_sequence, &rr->sort_status);
391     rr->errcode = zh->errCode;
392     rr->errstring = zh->errString;
393     return 0;
394 }
395
396 #ifndef WINDOWS
397 static void pre_init (struct statserv_options_block *sob)
398 {
399     char *pidfile = "zebrasrv.pid";
400     int fd = creat (pidfile, 0666);
401     
402     if (fd == -1)
403         logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
404     else
405     {
406         char pidstr[30];
407         
408         sprintf (pidstr, "%ld", (long) getpid ());
409         write (fd, pidstr, strlen(pidstr));
410         close (fd);
411     }
412 }
413 #endif
414
415 int main (int argc, char **argv)
416 {
417     struct statserv_options_block *sob;
418
419     sob = statserv_getcontrol ();
420     strcpy (sob->configname, FNAME_CONFIG);
421 #ifndef WINDOWS
422     sob->pre_init = pre_init;
423 #endif
424     statserv_setcontrol (sob);
425
426     return statserv_main (argc, argv);
427 }