Add include of sys/time.h
[idzebra-moved-to-github.git] / index / zserver.c
1 /*
2  * Copyright (C) 1995-2002, Index Data 
3  * All rights reserved.
4  *
5  * $Id: zserver.c,v 1.88 2002-05-07 11:05:19 adam Exp $
6  */
7
8 #include <stdio.h>
9 #include <assert.h>
10 #include <fcntl.h>
11 #ifdef WIN32
12 #include <io.h>
13 #include <process.h>
14 #else
15 #include <unistd.h>
16 #endif
17
18 #include <yaz/data1.h>
19 #ifdef ASN_COMPILED
20 #include <yaz/ill.h>
21 #endif
22
23 #include "zserver.h"
24
25 static int bend_sort (void *handle, bend_sort_rr *rr);
26 static int bend_delete (void *handle, bend_delete_rr *rr);
27 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
28 static int bend_segment (void *handle, bend_segment_rr *rr);
29 static int bend_search (void *handle, bend_search_rr *r);
30 static int bend_fetch (void *handle, bend_fetch_rr *r);
31 static int bend_scan (void *handle, bend_scan_rr *r);
32
33 bend_initresult *bend_init (bend_initrequest *q)
34 {
35     bend_initresult *r = (bend_initresult *)
36         odr_malloc (q->stream, sizeof(*r));
37     ZebraHandle zh;
38     struct statserv_options_block *sob;
39     char *user = NULL;
40     char *passwd = NULL;
41
42     r->errcode = 0;
43     r->errstring = 0;
44     q->bend_sort = bend_sort;
45     q->bend_delete = bend_delete;
46     q->bend_esrequest = bend_esrequest;
47     q->bend_segment = bend_segment;
48     q->bend_search = bend_search;
49     q->bend_fetch = bend_fetch;
50     q->bend_scan = bend_scan;
51
52 #if ZMBOL
53     q->implementation_name = "Z'mbol Information Server";
54     q->implementation_version = "Z'mbol " ZEBRAVER;
55 #else
56     q->implementation_name = "Zebra Information Server";
57     q->implementation_version = "Zebra " ZEBRAVER;
58 #endif
59
60     logf (LOG_DEBUG, "bend_init");
61
62     sob = statserv_getcontrol ();
63     if (!(zh = zebra_open (sob->handle)))
64     {
65         yaz_log (LOG_FATAL, "Failed to open config `%s'", sob->configname);
66         r->errcode = 1;
67         return r;
68     }
69     if (q->auth)
70     {
71         if (q->auth->which == Z_IdAuthentication_open)
72         {
73             char *openpass = xstrdup (q->auth->u.open);
74             char *cp = strchr (openpass, '/');
75             if (cp)
76             {
77                 *cp = '\0';
78                 user = nmem_strdup (odr_getmem (q->stream), openpass);
79                 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
80             }
81             xfree (openpass);
82         }
83     }
84     if (zebra_auth (zh, user, passwd))
85     {
86         r->errcode = 222;
87         r->errstring = user;
88         zebra_close (zh);
89         return r;
90     }
91     r->handle = zh;
92     return r;
93 }
94
95 static void search_terms (ZebraHandle zh, bend_search_rr *r)
96 {
97     int count;
98     int no_terms;
99     int i;
100     struct Z_External *ext;
101     Z_SearchInfoReport *sr;
102
103     /* get no of terms for result set */
104     zebra_resultSetTerms (zh, r->setname, -1, &count, &no_terms);
105     if (!no_terms)
106         return;
107
108     r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
109
110     r->search_info->num_elements = 1;
111     r->search_info->list =
112         odr_malloc (r->stream, sizeof(*r->search_info->list));
113     r->search_info->list[0] =
114         odr_malloc (r->stream, sizeof(**r->search_info->list));
115     r->search_info->list[0]->category = 0;
116     r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
117     ext = odr_malloc (r->stream, sizeof(*ext));
118     r->search_info->list[0]->information.externallyDefinedInfo = ext;
119     ext->direct_reference =
120         yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
121     ext->indirect_reference = 0;
122     ext->descriptor = 0;
123     ext->which = Z_External_searchResult1;
124     sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
125     ext->u.searchResult1 = sr;
126     sr->num = no_terms;
127     sr->elements = odr_malloc (r->stream, sr->num *
128                                sizeof(*sr->elements));
129     for (i = 0; i<no_terms; i++)
130     {
131         Z_Term *term;
132         const char *termz = zebra_resultSetTerms (zh, r->setname, i,
133                                                   &count, &no_terms);
134         
135         sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
136         sr->elements[i]->subqueryId = 0;
137         sr->elements[i]->fullQuery = odr_malloc (r->stream, 
138                                                  sizeof(bool_t));
139         *sr->elements[i]->fullQuery = 0;
140         sr->elements[i]->subqueryExpression = 
141             odr_malloc (r->stream, sizeof(Z_QueryExpression));
142         sr->elements[i]->subqueryExpression->which = 
143             Z_QueryExpression_term;
144         sr->elements[i]->subqueryExpression->u.term =
145             odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
146         term = odr_malloc (r->stream, sizeof(Z_Term));
147         sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
148
149         term->which = Z_Term_general;
150         term->u.general = odr_malloc (r->stream, sizeof(Odr_oct));
151         term->u.general->buf = odr_strdup (r->stream, termz);
152
153         term->u.general->len = strlen (termz);
154         term->u.general->size = strlen (termz);
155         
156         sr->elements[i]->subqueryExpression->u.term->termComment = 0;
157         sr->elements[i]->subqueryInterpretation = 0;
158         sr->elements[i]->subqueryRecommendation = 0;
159         sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
160         sr->elements[i]->subqueryWeight = 0;
161         sr->elements[i]->resultsByDB = 0;
162     }
163 }
164
165 int bend_search (void *handle, bend_search_rr *r)
166 {
167     ZebraHandle zh = (ZebraHandle) handle;
168
169     r->hits = 0;
170     r->errcode = 0;
171     r->errstring = NULL;
172     
173     if (zebra_select_databases (zh, r->num_bases,
174                                 (const char **) r->basenames))
175     {
176         zebra_result (zh, &r->errcode, &r->errstring);
177         return 0;
178     }
179     logf (LOG_LOG, "ResultSet '%s'", r->setname);
180     switch (r->query->which)
181     {
182     case Z_Query_type_1: case Z_Query_type_101:
183         zebra_search_rpn (zh, r->decode, r->stream, r->query->u.type_1,
184                           r->setname, &r->hits);
185         zebra_result (zh, &r->errcode, &r->errstring);
186         if (!r->errcode)
187             search_terms (zh, r);
188         break;
189     case Z_Query_type_2:
190         r->errcode = 107;
191         r->errstring = "type-2";
192         break;
193     default:
194         r->errcode = 107;
195     }
196     return 0;
197 }
198
199
200 int bend_fetch (void *handle, bend_fetch_rr *r)
201 {
202     ZebraHandle zh = (ZebraHandle) handle;
203     ZebraRetrievalRecord retrievalRecord;
204
205     retrievalRecord.position = r->number;
206     
207     r->last_in_set = 0;
208     zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
209                             r->request_format, 1, &retrievalRecord);
210     zebra_result (zh, &r->errcode, &r->errstring);
211     /*  non Surrogate Diagnostic OR Surrogate Diagnostic */
212     if (r->errcode == 0 && retrievalRecord.errCode)
213     {
214         r->surrogate_flag = 1;
215         r->errcode = retrievalRecord.errCode;
216         r->errstring = retrievalRecord.errString;
217         r->basename = retrievalRecord.base;
218     }
219     else if (r->errcode == 0)        /* Database Record */
220     {
221         r->errcode = 0;
222         r->basename = retrievalRecord.base;
223         r->record = retrievalRecord.buf;
224         r->len = retrievalRecord.len;
225         r->output_format = retrievalRecord.format;
226     }
227     return 0;
228 }
229
230 static int bend_scan (void *handle, bend_scan_rr *r)
231 {
232     ZebraScanEntry *entries;
233     ZebraHandle zh = (ZebraHandle) handle;
234     int is_partial, i;
235
236     if (zebra_select_databases (zh, r->num_bases, 
237                                 (const char **) r->basenames))
238     {
239         zebra_result (zh, &r->errcode, &r->errstring);
240         return 0;
241     }
242     r->entries = (struct scan_entry *)
243         odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
244     zebra_scan (zh, r->stream, r->term,
245                 r->attributeset,
246                 &r->term_position,
247                 &r->num_entries, &entries, &is_partial);
248     if (is_partial)
249         r->status = BEND_SCAN_PARTIAL;
250     else
251         r->status = BEND_SCAN_SUCCESS;
252     for (i = 0; i < r->num_entries; i++)
253     {
254         r->entries[i].term = entries[i].term;
255         r->entries[i].occurrences = entries[i].occurrences;
256     }
257     zebra_result (zh, &r->errcode, &r->errstring);
258     return 0;
259 }
260
261 void bend_close (void *handle)
262 {
263     zebra_close ((ZebraHandle) handle);
264     xmalloc_trav("bend_close");
265     nmem_print_list();
266 }
267
268 int bend_sort (void *handle, bend_sort_rr *rr)
269 {
270     ZebraHandle zh = (ZebraHandle) handle;
271
272     zebra_sort (zh, rr->stream,
273                 rr->num_input_setnames, (const char **) rr->input_setnames,
274                 rr->output_setname, rr->sort_sequence, &rr->sort_status);
275     zebra_result (zh, &rr->errcode, &rr->errstring);
276     return 0;
277 }
278
279 int bend_delete (void *handle, bend_delete_rr *rr)
280 {
281     ZebraHandle zh = (ZebraHandle) handle;
282
283     rr->delete_status = zebra_deleleResultSet(zh, rr->function,
284                                               rr->num_setnames, rr->setnames,
285                                               rr->statuses);
286     return 0;
287 }
288
289 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
290 {
291     switch (r->toKeep->which)
292     {
293     case Z_ESAdminOriginPartToKeep_reIndex:
294         yaz_log(LOG_LOG, "adm-reindex");
295         break;
296     case Z_ESAdminOriginPartToKeep_truncate:
297         yaz_log(LOG_LOG, "adm-truncate");
298         break;
299     case Z_ESAdminOriginPartToKeep_drop:
300         yaz_log(LOG_LOG, "adm-drop");
301         break;
302     case Z_ESAdminOriginPartToKeep_create:
303         yaz_log(LOG_LOG, "adm-create");
304         zebra_admin_create (zh, r->toKeep->databaseName);
305         break;
306     case Z_ESAdminOriginPartToKeep_import:
307         yaz_log(LOG_LOG, "adm-import");
308         zebra_admin_import_begin (zh, r->toKeep->databaseName);
309         break;
310     case Z_ESAdminOriginPartToKeep_refresh:
311         yaz_log(LOG_LOG, "adm-refresh");
312         break;
313     case Z_ESAdminOriginPartToKeep_commit:
314         yaz_log(LOG_LOG, "adm-commit");
315         break;
316     case Z_ESAdminOriginPartToKeep_shutdown:
317         yaz_log(LOG_LOG, "shutdown");
318         zebra_admin_shutdown(zh);
319         break;
320     case Z_ESAdminOriginPartToKeep_start:
321         yaz_log(LOG_LOG, "start");
322         zebra_admin_start(zh);
323         break;
324     default:
325         yaz_log(LOG_LOG, "unknown admin");
326     }
327     if (r->toKeep->databaseName)
328     {
329         yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
330     }
331     return 0;
332 }
333
334 static int es_admin (ZebraHandle zh, Z_Admin *r)
335 {
336     switch (r->which)
337     {
338     case Z_Admin_esRequest:
339         es_admin_request (zh, r->u.esRequest);
340         break;
341     case Z_Admin_taskPackage:
342         yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
343         break;
344     default:
345         break;
346     }
347
348     return 0;
349 }
350
351 int bend_segment (void *handle, bend_segment_rr *rr)
352 {
353     ZebraHandle zh = (ZebraHandle) handle;
354     Z_Segment *segment = rr->segment;
355
356     if (segment->num_segmentRecords)
357         zebra_admin_import_segment (zh, rr->segment);
358     else
359         zebra_admin_import_end (zh);
360     return 0;
361 }
362
363 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
364 {
365     ZebraHandle zh = (ZebraHandle) handle;
366     
367     yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
368     if (rr->esr->packageName)
369         yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
370     yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
371
372     if (!rr->esr->taskSpecificParameters)
373     {
374         yaz_log (LOG_WARN, "No task specific parameters");
375     }
376     else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
377     {
378         es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
379
380         zebra_result (zh, &rr->errcode, &rr->errstring);
381     }
382     else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
383     {
384         Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
385         yaz_log (LOG_LOG, "Received ItemOrder");
386         switch (it->which)
387         {
388 #ifdef ASN_COMPILED
389         case Z_IOItemOrder_esRequest:
390 #else
391         case Z_ItemOrder_esRequest:
392 #endif
393         {
394             Z_IORequest *ir = it->u.esRequest;
395             Z_IOOriginPartToKeep *k = ir->toKeep;
396             Z_IOOriginPartNotToKeep *n = ir->notToKeep;
397             
398             if (k && k->contact)
399             {
400                 if (k->contact->name)
401                     yaz_log(LOG_LOG, "contact name %s", k->contact->name);
402                 if (k->contact->phone)
403                     yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
404                 if (k->contact->email)
405                     yaz_log(LOG_LOG, "contact email %s", k->contact->email);
406             }
407             if (k->addlBilling)
408             {
409                 yaz_log(LOG_LOG, "Billing info (not shown)");
410             }
411             
412             if (n->resultSetItem)
413             {
414                 yaz_log(LOG_LOG, "resultsetItem");
415                 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
416                 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
417             }
418 #ifdef ASN_COMPILED
419             if (n->itemRequest)
420             {
421                 Z_External *r = (Z_External*) n->itemRequest;
422                 ILL_ItemRequest *item_req = 0;
423                 ILL_Request *ill_req = 0;
424                 if (r->direct_reference)
425                 {
426                     oident *ent = oid_getentbyoid(r->direct_reference);
427                     if (ent)
428                         yaz_log(LOG_LOG, "OID %s", ent->desc);
429                     if (ent && ent->value == VAL_ISO_ILL_1)
430                     {
431                         yaz_log (LOG_LOG, "ItemRequest");
432                         if (r->which == ODR_EXTERNAL_single)
433                         {
434                             odr_setbuf(rr->decode,
435                                        r->u.single_ASN1_type->buf,
436                                        r->u.single_ASN1_type->len, 0);
437                             
438                             if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
439                             {
440                                 yaz_log (LOG_LOG,
441                                     "Couldn't decode ItemRequest %s near %d",
442                                        odr_errmsg(odr_geterror(rr->decode)),
443                                        odr_offset(rr->decode));
444                                 yaz_log(LOG_LOG, "PDU dump:");
445                                 odr_dumpBER(yaz_log_file(),
446                                      r->u.single_ASN1_type->buf,
447                                      r->u.single_ASN1_type->len);
448                             }
449                             if (rr->print)
450                             {
451                                 ill_ItemRequest (rr->print, &item_req, 0,
452                                     "ItemRequest");
453                                 odr_reset (rr->print);
454                             }
455                         }
456                         if (!item_req && r->which == ODR_EXTERNAL_single)
457                         {
458                             yaz_log (LOG_LOG, "ILLRequest");
459                             odr_setbuf(rr->decode,
460                                        r->u.single_ASN1_type->buf,
461                                        r->u.single_ASN1_type->len, 0);
462                             
463                             if (!ill_Request (rr->decode, &ill_req, 0, 0))
464                             {
465                                 yaz_log (LOG_LOG,
466                                     "Couldn't decode ILLRequest %s near %d",
467                                        odr_errmsg(odr_geterror(rr->decode)),
468                                        odr_offset(rr->decode));
469                                 yaz_log(LOG_LOG, "PDU dump:");
470                                 odr_dumpBER(yaz_log_file(),
471                                      r->u.single_ASN1_type->buf,
472                                      r->u.single_ASN1_type->len);
473                             }
474                             if (rr->print)
475                             {
476                                 ill_Request (rr->print, &ill_req, 0,
477                                     "ILLRequest");
478                                 odr_reset (rr->print);
479                             }
480                         }
481                     }
482                 }
483                 if (item_req)
484                 {
485                     yaz_log (LOG_LOG, "ILL protocol version = %d",
486                              *item_req->protocol_version_num);
487                 }
488             }
489 #endif
490         }
491         break;
492         }
493     }
494     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
495     {
496         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
497         yaz_log (LOG_LOG, "Received DB Update");
498         if (up->which == Z_IUUpdate_esRequest)
499         {
500             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
501             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
502             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
503             
504             yaz_log (LOG_LOG, "action");
505             if (toKeep->action)
506             {
507                 switch (*toKeep->action)
508                 {
509                 case Z_IUOriginPartToKeep_recordInsert:
510                     yaz_log (LOG_LOG, "recordInsert");
511                     break;
512                 case Z_IUOriginPartToKeep_recordReplace:
513                     yaz_log (LOG_LOG, "recordUpdate");
514                     break;
515                 case Z_IUOriginPartToKeep_recordDelete:
516                     yaz_log (LOG_LOG, "recordDelete");
517                     break;
518                 case Z_IUOriginPartToKeep_elementUpdate:
519                     yaz_log (LOG_LOG, "elementUpdate");
520                     break;
521                 case Z_IUOriginPartToKeep_specialUpdate:
522                     yaz_log (LOG_LOG, "specialUpdate");
523                     break;
524                 case Z_ESAdminOriginPartToKeep_shutdown:
525                     yaz_log (LOG_LOG, "shutDown");
526                     break;
527                 case Z_ESAdminOriginPartToKeep_start:
528                     yaz_log (LOG_LOG, "start");
529                     break;
530                 default:
531                     yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
532                 }
533             }
534             if (toKeep->databaseName)
535             {
536                 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
537                 if (!strcmp(toKeep->databaseName, "fault"))
538                 {
539                     rr->errcode = 109;
540                     rr->errstring = toKeep->databaseName;
541                 }
542                 if (!strcmp(toKeep->databaseName, "accept"))
543                     rr->errcode = -1;
544             }
545             if (notToKeep)
546             {
547                 int i;
548                 for (i = 0; i < notToKeep->num; i++)
549                 {
550                     Z_External *rec = notToKeep->elements[i]->record;
551
552                     if (rec->direct_reference)
553                     {
554                         struct oident *oident;
555                         oident = oid_getentbyoid(rec->direct_reference);
556                         if (oident)
557                             yaz_log (LOG_LOG, "record %d type %s", i,
558                                      oident->desc);
559                     }
560                     switch (rec->which)
561                     {
562                     case Z_External_sutrs:
563                         if (rec->u.octet_aligned->len > 170)
564                             yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
565                                      rec->u.sutrs->len,
566                                      rec->u.sutrs->buf);
567                         else
568                             yaz_log (LOG_LOG, "%d bytes:\n%s",
569                                      rec->u.sutrs->len,
570                                      rec->u.sutrs->buf);
571                         break;
572                     case Z_External_octet:
573                         if (rec->u.octet_aligned->len > 170)
574                             yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
575                                      rec->u.octet_aligned->len,
576                                      rec->u.octet_aligned->buf);
577                         else
578                             yaz_log (LOG_LOG, "%d bytes\n%s",
579                                      rec->u.octet_aligned->len,
580                                      rec->u.octet_aligned->buf);
581                     }
582                 }
583             }
584         }
585     }
586     else
587     {
588         yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
589                  rr->esr->taskSpecificParameters->which);
590         
591     }
592     return 0;
593 }
594
595 static void bend_start (struct statserv_options_block *sob)
596 {
597 #ifdef WIN32
598     
599 #else
600     if (!sob->inetd) 
601     {
602         char *pidfile = "zebrasrv.pid";
603         int fd = creat (pidfile, 0666);
604         if (fd == -1)
605             logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
606         else
607         {
608             char pidstr[30];
609         
610             sprintf (pidstr, "%ld", (long) getpid ());
611             write (fd, pidstr, strlen(pidstr));
612             close (fd);
613         }
614     }
615 #endif
616     if (sob->handle)
617         zebra_stop((ZebraService) sob->handle);
618     sob->handle = zebra_start(sob->configname);
619 }
620
621 static void bend_stop(struct statserv_options_block *sob)
622 {
623 #ifdef WIN32
624
625 #else
626     if (!sob->inetd) 
627         unlink ("zebrasrv.pid");
628 #endif
629     if (sob->handle)
630     {
631         ZebraService service = sob->handle;
632         zebra_stop(service);
633     }
634 }
635
636 int main (int argc, char **argv)
637 {
638     struct statserv_options_block *sob;
639
640     sob = statserv_getcontrol ();
641     strcpy (sob->configname, "zebra.cfg");
642     sob->bend_start = bend_start;
643     sob->bend_stop = bend_stop;
644
645     statserv_setcontrol (sob);
646
647     return statserv_main (argc, argv, bend_init, bend_close);
648 }