1 /* $Id: zserver.c,v 1.102 2003-01-13 15:06:23 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
40 static int bend_sort (void *handle, bend_sort_rr *rr);
41 static int bend_delete (void *handle, bend_delete_rr *rr);
42 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
43 static int bend_segment (void *handle, bend_segment_rr *rr);
44 static int bend_search (void *handle, bend_search_rr *r);
45 static int bend_fetch (void *handle, bend_fetch_rr *r);
46 static int bend_scan (void *handle, bend_scan_rr *r);
48 bend_initresult *bend_init (bend_initrequest *q)
50 bend_initresult *r = (bend_initresult *)
51 odr_malloc (q->stream, sizeof(*r));
53 struct statserv_options_block *sob;
59 q->bend_sort = bend_sort;
60 q->bend_delete = bend_delete;
61 q->bend_esrequest = bend_esrequest;
62 q->bend_segment = bend_segment;
63 q->bend_search = bend_search;
64 q->bend_fetch = bend_fetch;
65 q->bend_scan = bend_scan;
67 q->implementation_name = "Zebra Information Server";
68 q->implementation_version = "Zebra " ZEBRAVER;
70 yaz_log (LOG_DEBUG, "bend_init");
72 sob = statserv_getcontrol ();
73 if (!(zh = zebra_open (sob->handle)))
75 yaz_log (LOG_WARN, "Failed to read config `%s'", sob->configname);
81 if (q->auth->which == Z_IdAuthentication_open)
83 char *openpass = xstrdup (q->auth->u.open);
84 char *cp = strchr (openpass, '/');
88 user = nmem_strdup (odr_getmem (q->stream), openpass);
89 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
94 if (zebra_auth (zh, user, passwd))
102 if (q->charneg_request) /* characater set and langauge negotiation? */
110 NMEM nmem = nmem_create();
112 yaz_log (LOG_LOG, "character set and language negotiation");
114 yaz_get_proposal_charneg (nmem, q->charneg_request,
115 &charsets, &num_charsets,
116 &langs, &num_langs, &selected);
118 for (i = 0; i < num_charsets; i++)
120 const char *right_name = "";
122 * FIXME! It is like rudiment :-))
123 * We have to support this short names of character sets,
124 * because a lot servers in Russia to use own in during
125 * character set and language negotiation still.
128 if (!yaz_matchstr(charsets[i], "win")) {
129 right_name = "WINDOWS-1251";
130 } else if (!yaz_matchstr(charsets[i], "koi")) {
131 right_name = "KOI8-R";
132 } else if (!yaz_matchstr(charsets[i], "iso")) {
133 right_name = "ISO-8859-5";
134 } else if (!yaz_matchstr(charsets[i], "dos")) {
135 right_name = "CP866";
136 } else if (!yaz_matchstr(charsets[i], "uni")) {
137 right_name = "UTF-8";
139 right_name = charsets[i];
141 if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
143 yaz_log (LOG_LOG, "charset %d %s (proper name %s): OK", i,
144 charsets[i], right_name);
145 odr_set_charset (q->stream, right_name, "UTF-8");
147 zebra_record_encoding (zh, right_name);
148 q->charneg_response =
149 yaz_set_response_charneg (q->stream, right_name,
153 yaz_log (LOG_LOG, "charset %d %s (proper name %s): unsupported", i,
154 charsets[i], right_name);
162 static void search_terms (ZebraHandle zh, bend_search_rr *r)
168 struct Z_External *ext;
169 Z_SearchInfoReport *sr;
171 /* get no of terms for result set */
172 no_terms = zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0);
176 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
178 r->search_info->num_elements = 1;
179 r->search_info->list =
180 odr_malloc (r->stream, sizeof(*r->search_info->list));
181 r->search_info->list[0] =
182 odr_malloc (r->stream, sizeof(**r->search_info->list));
183 r->search_info->list[0]->category = 0;
184 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
185 ext = odr_malloc (r->stream, sizeof(*ext));
186 r->search_info->list[0]->information.externallyDefinedInfo = ext;
187 ext->direct_reference =
188 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
189 ext->indirect_reference = 0;
191 ext->which = Z_External_searchResult1;
192 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
193 ext->u.searchResult1 = sr;
195 sr->elements = odr_malloc (r->stream, sr->num *
196 sizeof(*sr->elements));
197 for (i = 0; i<no_terms; i++)
201 size_t len = sizeof(outbuf);
202 zebra_resultSetTerms (zh, r->setname, i,
203 &count, &type, outbuf, &len);
205 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
206 sr->elements[i]->subqueryId = 0;
207 sr->elements[i]->fullQuery = odr_malloc (r->stream,
209 *sr->elements[i]->fullQuery = 0;
210 sr->elements[i]->subqueryExpression =
211 odr_malloc (r->stream, sizeof(Z_QueryExpression));
212 sr->elements[i]->subqueryExpression->which =
213 Z_QueryExpression_term;
214 sr->elements[i]->subqueryExpression->u.term =
215 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
216 term = odr_malloc (r->stream, sizeof(Z_Term));
217 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
220 case Z_Term_characterString:
221 yaz_log (LOG_DEBUG, "term as characterString");
222 term->which = Z_Term_characterString;
223 term->u.characterString = odr_strdup (r->stream, outbuf);
226 yaz_log (LOG_DEBUG, "term as general");
227 term->which = Z_Term_general;
228 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
229 term->u.general->size = term->u.general->len = len;
230 term->u.general->buf = odr_malloc (r->stream, len);
231 memcpy (term->u.general->buf, outbuf, len);
234 term->which = Z_Term_general;
235 term->u.null = odr_nullval();
237 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
238 sr->elements[i]->subqueryInterpretation = 0;
239 sr->elements[i]->subqueryRecommendation = 0;
240 sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
241 sr->elements[i]->subqueryWeight = 0;
242 sr->elements[i]->resultsByDB = 0;
246 int bend_search (void *handle, bend_search_rr *r)
248 ZebraHandle zh = (ZebraHandle) handle;
254 if (zebra_select_databases (zh, r->num_bases,
255 (const char **) r->basenames))
257 zebra_result (zh, &r->errcode, &r->errstring);
260 yaz_log (LOG_LOG, "ResultSet '%s'", r->setname);
261 switch (r->query->which)
263 case Z_Query_type_1: case Z_Query_type_101:
264 zebra_search_rpn (zh, r->decode, r->stream, r->query->u.type_1,
265 r->setname, &r->hits);
266 zebra_result (zh, &r->errcode, &r->errstring);
268 search_terms (zh, r);
272 r->errstring = "type-2";
281 int bend_fetch (void *handle, bend_fetch_rr *r)
283 ZebraHandle zh = (ZebraHandle) handle;
284 ZebraRetrievalRecord retrievalRecord;
286 retrievalRecord.position = r->number;
289 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
290 r->request_format, 1, &retrievalRecord);
291 zebra_result (zh, &r->errcode, &r->errstring);
292 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
293 if (r->errcode == 0 && retrievalRecord.errCode)
295 r->surrogate_flag = 1;
296 r->errcode = retrievalRecord.errCode;
297 r->errstring = retrievalRecord.errString;
298 r->basename = retrievalRecord.base;
300 else if (r->errcode == 0) /* Database Record */
303 r->basename = retrievalRecord.base;
304 r->record = retrievalRecord.buf;
305 r->len = retrievalRecord.len;
306 r->output_format = retrievalRecord.format;
311 static int bend_scan (void *handle, bend_scan_rr *r)
313 ZebraScanEntry *entries;
314 ZebraHandle zh = (ZebraHandle) handle;
317 if (zebra_select_databases (zh, r->num_bases,
318 (const char **) r->basenames))
320 zebra_result (zh, &r->errcode, &r->errstring);
323 r->entries = (struct scan_entry *)
324 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
325 zebra_scan (zh, r->stream, r->term,
328 &r->num_entries, &entries, &is_partial);
330 r->status = BEND_SCAN_PARTIAL;
332 r->status = BEND_SCAN_SUCCESS;
333 for (i = 0; i < r->num_entries; i++)
335 r->entries[i].term = entries[i].term;
336 r->entries[i].occurrences = entries[i].occurrences;
338 zebra_result (zh, &r->errcode, &r->errstring);
342 void bend_close (void *handle)
344 zebra_close ((ZebraHandle) handle);
345 xmalloc_trav("bend_close");
349 int bend_sort (void *handle, bend_sort_rr *rr)
351 ZebraHandle zh = (ZebraHandle) handle;
353 zebra_sort (zh, rr->stream,
354 rr->num_input_setnames, (const char **) rr->input_setnames,
355 rr->output_setname, rr->sort_sequence, &rr->sort_status);
356 zebra_result (zh, &rr->errcode, &rr->errstring);
360 int bend_delete (void *handle, bend_delete_rr *rr)
362 ZebraHandle zh = (ZebraHandle) handle;
364 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
365 rr->num_setnames, rr->setnames,
370 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
372 switch (r->toKeep->which)
374 case Z_ESAdminOriginPartToKeep_reIndex:
375 yaz_log(LOG_LOG, "adm-reindex");
377 case Z_ESAdminOriginPartToKeep_truncate:
378 yaz_log(LOG_LOG, "adm-truncate");
380 case Z_ESAdminOriginPartToKeep_drop:
381 yaz_log(LOG_LOG, "adm-drop");
383 case Z_ESAdminOriginPartToKeep_create:
384 yaz_log(LOG_LOG, "adm-create %s", r->toKeep->databaseName);
385 zebra_admin_create (zh, r->toKeep->databaseName);
387 case Z_ESAdminOriginPartToKeep_import:
388 yaz_log(LOG_LOG, "adm-import");
389 zebra_admin_import_begin (zh, r->toKeep->databaseName,
390 r->toKeep->u.import->recordType);
392 case Z_ESAdminOriginPartToKeep_refresh:
393 yaz_log(LOG_LOG, "adm-refresh");
395 case Z_ESAdminOriginPartToKeep_commit:
396 yaz_log(LOG_LOG, "adm-commit");
398 case Z_ESAdminOriginPartToKeep_shutdown:
399 yaz_log(LOG_LOG, "shutdown");
400 zebra_admin_shutdown(zh);
402 case Z_ESAdminOriginPartToKeep_start:
403 yaz_log(LOG_LOG, "start");
404 zebra_admin_start(zh);
407 yaz_log(LOG_LOG, "unknown admin");
409 if (r->toKeep->databaseName)
411 yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
416 static int es_admin (ZebraHandle zh, Z_Admin *r)
420 case Z_Admin_esRequest:
421 es_admin_request (zh, r->u.esRequest);
423 case Z_Admin_taskPackage:
424 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
433 int bend_segment (void *handle, bend_segment_rr *rr)
435 ZebraHandle zh = (ZebraHandle) handle;
436 Z_Segment *segment = rr->segment;
438 if (segment->num_segmentRecords)
439 zebra_admin_import_segment (zh, rr->segment);
441 zebra_admin_import_end (zh);
445 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
447 ZebraHandle zh = (ZebraHandle) handle;
449 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
450 if (rr->esr->packageName)
451 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
452 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
454 if (!rr->esr->taskSpecificParameters)
456 yaz_log (LOG_WARN, "No task specific parameters");
458 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
460 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
462 zebra_result (zh, &rr->errcode, &rr->errstring);
464 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
466 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
467 yaz_log (LOG_LOG, "Received DB Update");
468 if (up->which == Z_IUUpdate_esRequest)
470 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
471 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
472 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
474 yaz_log (LOG_LOG, "action");
477 switch (*toKeep->action)
479 case Z_IUOriginPartToKeep_recordInsert:
480 yaz_log (LOG_LOG, "recordInsert");
482 case Z_IUOriginPartToKeep_recordReplace:
483 yaz_log (LOG_LOG, "recordUpdate");
485 case Z_IUOriginPartToKeep_recordDelete:
486 yaz_log (LOG_LOG, "recordDelete");
488 case Z_IUOriginPartToKeep_elementUpdate:
489 yaz_log (LOG_LOG, "elementUpdate");
491 case Z_IUOriginPartToKeep_specialUpdate:
492 yaz_log (LOG_LOG, "specialUpdate");
494 case Z_ESAdminOriginPartToKeep_shutdown:
495 yaz_log (LOG_LOG, "shutDown");
497 case Z_ESAdminOriginPartToKeep_start:
498 yaz_log (LOG_LOG, "start");
501 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
504 if (toKeep->databaseName)
506 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
508 if (zebra_select_database(zh, toKeep->databaseName))
513 yaz_log (LOG_WARN, "no database supplied for ES Update");
515 rr->errstring = "database";
521 zebra_begin_trans (zh);
522 for (i = 0; i < notToKeep->num; i++)
524 Z_External *rec = notToKeep->elements[i]->record;
525 struct oident *oident = 0;
526 Odr_oct *recid = notToKeep->elements[i]->u.opaque;
531 rr->errstring = "record Id not supplied";
534 if (notToKeep->elements[i]->which !=
535 Z_IUSuppliedRecords_elem_opaque)
538 rr->errstring = "only opaque record ID supported";
542 if (rec->direct_reference)
544 oident = oid_getentbyoid(rec->direct_reference);
546 yaz_log (LOG_LOG, "record %d type %s", i,
551 case Z_External_sutrs:
552 if (rec->u.octet_aligned->len > 170)
553 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
557 yaz_log (LOG_LOG, "%d bytes:\n%s",
561 case Z_External_octet:
562 if (rec->u.octet_aligned->len > 170)
563 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
564 rec->u.octet_aligned->len,
565 rec->u.octet_aligned->buf);
567 yaz_log (LOG_LOG, "%d bytes\n%s",
568 rec->u.octet_aligned->len,
569 rec->u.octet_aligned->buf);
571 if (oident && oident->value != VAL_TEXT_XML)
574 rr->errstring = "only XML update supported";
577 if (rec->which == Z_External_octet)
581 if (*toKeep->action ==
582 Z_IUOriginPartToKeep_recordInsert)
584 if (*toKeep->action ==
585 Z_IUOriginPartToKeep_recordReplace)
587 if (*toKeep->action ==
588 Z_IUOriginPartToKeep_recordDelete)
590 if (*toKeep->action ==
591 Z_IUOriginPartToKeep_specialUpdate)
597 rr->errstring = "unsupported ES Update action";
602 int r = zebra_admin_exchange_record (
603 zh, toKeep->databaseName,
604 rec->u.octet_aligned->buf,
605 rec->u.octet_aligned->len,
606 recid->buf, recid->len,
608 if (r && *toKeep->action ==
609 Z_IUOriginPartToKeep_specialUpdate)
611 r = zebra_admin_exchange_record (
612 zh, toKeep->databaseName,
613 rec->u.octet_aligned->buf,
614 rec->u.octet_aligned->len,
615 recid->buf, recid->len,
621 rr->errstring = "record exchange failed";
627 zebra_end_trans (zh);
633 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
634 rr->esr->taskSpecificParameters->which);
641 static void bend_start (struct statserv_options_block *sob)
648 char *pidfile = "zebrasrv.pid";
649 int fd = creat (pidfile, 0666);
651 yaz_log (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
656 sprintf (pidstr, "%ld", (long) getpid ());
657 write (fd, pidstr, strlen(pidstr));
663 zebra_stop((ZebraService) sob->handle);
664 sob->handle = zebra_start(sob->configname);
667 yaz_log (LOG_FATAL, "Failed to read config `%s'", sob->configname);
672 static void bend_stop(struct statserv_options_block *sob)
678 unlink ("zebrasrv.pid");
682 ZebraService service = sob->handle;
687 int main (int argc, char **argv)
689 struct statserv_options_block *sob;
691 sob = statserv_getcontrol ();
692 strcpy (sob->configname, "zebra.cfg");
693 sob->bend_start = bend_start;
694 sob->bend_stop = bend_stop;
696 strcpy (sob->service_display_name, "Zebra Server");
698 statserv_setcontrol (sob);
700 return statserv_main (argc, argv, bend_init, bend_close);