1 /* $Id: zserver.c,v 1.119 2004-08-06 13:36:23 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
29 #include <sys/locking.h>
37 #include <yaz/yaz-util.h>
39 #include <sys/types.h>
43 static int bend_sort (void *handle, bend_sort_rr *rr);
44 static int bend_delete (void *handle, bend_delete_rr *rr);
45 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
46 static int bend_segment (void *handle, bend_segment_rr *rr);
47 static int bend_search (void *handle, bend_search_rr *r);
48 static int bend_fetch (void *handle, bend_fetch_rr *r);
49 static int bend_scan (void *handle, bend_scan_rr *r);
51 bend_initresult *bend_init (bend_initrequest *q)
53 bend_initresult *r = (bend_initresult *)
54 odr_malloc (q->stream, sizeof(*r));
56 struct statserv_options_block *sob;
62 q->bend_sort = bend_sort;
63 q->bend_delete = bend_delete;
64 q->bend_esrequest = bend_esrequest;
65 q->bend_segment = bend_segment;
66 q->bend_search = bend_search;
67 q->bend_fetch = bend_fetch;
68 q->bend_scan = bend_scan;
70 q->implementation_name = "Zebra Information Server";
71 q->implementation_version = "Zebra " ZEBRAVER;
73 yaz_log (LOG_DEBUG, "bend_init");
75 sob = statserv_getcontrol ();
76 if (!(zh = zebra_open (sob->handle)))
78 yaz_log (LOG_WARN, "Failed to read config `%s'", sob->configname);
85 if (q->auth->which == Z_IdAuthentication_open)
87 char *openpass = xstrdup (q->auth->u.open);
88 char *cp = strchr (openpass, '/');
92 user = nmem_strdup (odr_getmem (q->stream), openpass);
93 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
97 else if (q->auth->which == Z_IdAuthentication_idPass)
99 Z_IdPass *idPass = q->auth->u.idPass;
101 user = idPass->userId;
102 passwd = idPass->password;
105 if (zebra_auth (zh, user, passwd))
111 if (q->charneg_request) /* characater set and langauge negotiation? */
119 NMEM nmem = nmem_create();
121 yaz_log (LOG_LOG, "character set and language negotiation");
123 yaz_get_proposal_charneg (nmem, q->charneg_request,
124 &charsets, &num_charsets,
125 &langs, &num_langs, &selected);
127 for (i = 0; i < num_charsets; i++)
129 const char *right_name = "";
131 * FIXME! It is like rudiment :-))
132 * We have to support this short names of character sets,
133 * because a lot servers in Russia to use own in during
134 * character set and language negotiation still.
137 if (!yaz_matchstr(charsets[i], "win")) {
138 right_name = "WINDOWS-1251";
139 } else if (!yaz_matchstr(charsets[i], "koi")) {
140 right_name = "KOI8-R";
141 } else if (!yaz_matchstr(charsets[i], "iso")) {
142 right_name = "ISO-8859-5";
143 } else if (!yaz_matchstr(charsets[i], "dos")) {
144 right_name = "CP866";
145 } else if (!yaz_matchstr(charsets[i], "uni")) {
146 right_name = "UTF-8";
148 right_name = charsets[i];
150 if (odr_set_charset (q->decode, "UTF-8", right_name) == 0)
152 yaz_log (LOG_LOG, "charset %d %s (proper name %s): OK", i,
153 charsets[i], right_name);
154 odr_set_charset (q->stream, right_name, "UTF-8");
156 zebra_record_encoding (zh, right_name);
157 q->charneg_response =
158 yaz_set_response_charneg (q->stream, charsets[i],
162 yaz_log (LOG_LOG, "charset %d %s (proper name %s): unsupported", i,
163 charsets[i], right_name);
171 static void search_terms (ZebraHandle zh, bend_search_rr *r)
177 struct Z_External *ext;
178 Z_SearchInfoReport *sr;
180 /* get no of terms for result set */
181 no_terms = zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0);
185 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
187 r->search_info->num_elements = 1;
188 r->search_info->list =
189 odr_malloc (r->stream, sizeof(*r->search_info->list));
190 r->search_info->list[0] =
191 odr_malloc (r->stream, sizeof(**r->search_info->list));
192 r->search_info->list[0]->category = 0;
193 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
194 ext = odr_malloc (r->stream, sizeof(*ext));
195 r->search_info->list[0]->information.externallyDefinedInfo = ext;
196 ext->direct_reference =
197 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
198 ext->indirect_reference = 0;
200 ext->which = Z_External_searchResult1;
201 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
202 ext->u.searchResult1 = sr;
204 sr->elements = odr_malloc (r->stream, sr->num *
205 sizeof(*sr->elements));
206 for (i = 0; i<no_terms; i++)
210 size_t len = sizeof(outbuf);
211 zebra_resultSetTerms (zh, r->setname, i,
212 &count, &type, outbuf, &len);
214 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
215 sr->elements[i]->subqueryId = 0;
216 sr->elements[i]->fullQuery = odr_malloc (r->stream,
218 *sr->elements[i]->fullQuery = 0;
219 sr->elements[i]->subqueryExpression =
220 odr_malloc (r->stream, sizeof(Z_QueryExpression));
221 sr->elements[i]->subqueryExpression->which =
222 Z_QueryExpression_term;
223 sr->elements[i]->subqueryExpression->u.term =
224 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
225 term = odr_malloc (r->stream, sizeof(Z_Term));
226 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
229 case Z_Term_characterString:
230 yaz_log (LOG_DEBUG, "term as characterString");
231 term->which = Z_Term_characterString;
232 term->u.characterString = odr_strdup (r->stream, outbuf);
235 yaz_log (LOG_DEBUG, "term as general");
236 term->which = Z_Term_general;
237 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
238 term->u.general->size = term->u.general->len = len;
239 term->u.general->buf = odr_malloc (r->stream, len);
240 memcpy (term->u.general->buf, outbuf, len);
243 term->which = Z_Term_general;
244 term->u.null = odr_nullval();
246 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
247 sr->elements[i]->subqueryInterpretation = 0;
248 sr->elements[i]->subqueryRecommendation = 0;
249 if (count > 2000000000)
251 sr->elements[i]->subqueryCount = odr_intdup (r->stream, (int) count);
252 sr->elements[i]->subqueryWeight = 0;
253 sr->elements[i]->resultsByDB = 0;
257 int bend_search (void *handle, bend_search_rr *r)
259 ZebraHandle zh = (ZebraHandle) handle;
265 if (zebra_select_databases (zh, r->num_bases,
266 (const char **) r->basenames))
268 zebra_result (zh, &r->errcode, &r->errstring);
271 yaz_log (LOG_LOG, "ResultSet '%s'", r->setname);
272 switch (r->query->which)
274 case Z_Query_type_1: case Z_Query_type_101:
275 zebra_search_RPN (zh, r->stream, r->query->u.type_1,
276 r->setname, &r->hits);
277 zebra_result (zh, &r->errcode, &r->errstring);
279 search_terms (zh, r);
283 r->errstring = "type-2";
292 int bend_fetch (void *handle, bend_fetch_rr *r)
294 ZebraHandle zh = (ZebraHandle) handle;
295 ZebraRetrievalRecord retrievalRecord;
297 retrievalRecord.position = r->number;
300 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
301 r->request_format, 1, &retrievalRecord);
302 zebra_result (zh, &r->errcode, &r->errstring);
303 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
304 if (r->errcode == 0 && retrievalRecord.errCode)
306 r->surrogate_flag = 1;
307 r->errcode = retrievalRecord.errCode;
308 r->errstring = retrievalRecord.errString;
309 r->basename = retrievalRecord.base;
311 else if (r->errcode == 0) /* Database Record */
314 r->basename = retrievalRecord.base;
315 r->record = retrievalRecord.buf;
316 r->len = retrievalRecord.len;
317 r->output_format = retrievalRecord.format;
322 static int bend_scan (void *handle, bend_scan_rr *r)
324 ZebraScanEntry *entries;
325 ZebraHandle zh = (ZebraHandle) handle;
328 if (zebra_select_databases (zh, r->num_bases,
329 (const char **) r->basenames))
331 zebra_result (zh, &r->errcode, &r->errstring);
334 if (r->step_size != 0 && *r->step_size != 0) {
335 r->errcode = 205; /* "Only zero step size supported for Scan" */
339 r->entries = (struct scan_entry *)
340 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
341 zebra_scan (zh, r->stream, r->term,
344 &r->num_entries, &entries, &is_partial);
346 r->status = BEND_SCAN_PARTIAL;
348 r->status = BEND_SCAN_SUCCESS;
349 for (i = 0; i < r->num_entries; i++)
351 r->entries[i].term = entries[i].term;
352 r->entries[i].occurrences = entries[i].occurrences;
354 zebra_result (zh, &r->errcode, &r->errstring);
358 void bend_close (void *handle)
360 zebra_close ((ZebraHandle) handle);
361 xmalloc_trav("bend_close");
365 int bend_sort (void *handle, bend_sort_rr *rr)
367 ZebraHandle zh = (ZebraHandle) handle;
369 zebra_sort (zh, rr->stream,
370 rr->num_input_setnames, (const char **) rr->input_setnames,
371 rr->output_setname, rr->sort_sequence, &rr->sort_status);
372 zebra_result (zh, &rr->errcode, &rr->errstring);
376 int bend_delete (void *handle, bend_delete_rr *rr)
378 ZebraHandle zh = (ZebraHandle) handle;
380 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
381 rr->num_setnames, rr->setnames,
386 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
388 if (r->toKeep->databaseName)
390 yaz_log(LOG_LOG, "adm request database %s", r->toKeep->databaseName);
392 switch (r->toKeep->which)
394 case Z_ESAdminOriginPartToKeep_reIndex:
395 yaz_log(LOG_LOG, "adm-reindex");
397 case Z_ESAdminOriginPartToKeep_truncate:
398 yaz_log(LOG_LOG, "adm-truncate");
400 case Z_ESAdminOriginPartToKeep_drop:
401 yaz_log(LOG_LOG, "adm-drop");
402 zebra_drop_database (zh, r->toKeep->databaseName);
404 case Z_ESAdminOriginPartToKeep_create:
405 yaz_log(LOG_LOG, "adm-create %s", r->toKeep->databaseName);
406 zebra_create_database (zh, r->toKeep->databaseName);
408 case Z_ESAdminOriginPartToKeep_import:
409 yaz_log(LOG_LOG, "adm-import");
410 zebra_admin_import_begin (zh, r->toKeep->databaseName,
411 r->toKeep->u.import->recordType);
413 case Z_ESAdminOriginPartToKeep_refresh:
414 yaz_log(LOG_LOG, "adm-refresh");
416 case Z_ESAdminOriginPartToKeep_commit:
417 yaz_log(LOG_LOG, "adm-commit");
418 if (r->toKeep->databaseName)
419 zebra_select_database(zh, r->toKeep->databaseName);
422 case Z_ESAdminOriginPartToKeep_shutdown:
423 yaz_log(LOG_LOG, "shutdown");
424 zebra_admin_shutdown(zh);
426 case Z_ESAdminOriginPartToKeep_start:
427 yaz_log(LOG_LOG, "start");
428 zebra_admin_start(zh);
431 yaz_log(LOG_LOG, "unknown admin");
436 static int es_admin (ZebraHandle zh, Z_Admin *r)
440 case Z_Admin_esRequest:
441 es_admin_request (zh, r->u.esRequest);
443 case Z_Admin_taskPackage:
444 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
453 int bend_segment (void *handle, bend_segment_rr *rr)
455 ZebraHandle zh = (ZebraHandle) handle;
456 Z_Segment *segment = rr->segment;
458 if (segment->num_segmentRecords)
459 zebra_admin_import_segment (zh, rr->segment);
461 zebra_admin_import_end (zh);
465 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
467 ZebraHandle zh = (ZebraHandle) handle;
469 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
470 if (rr->esr->packageName)
471 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
472 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
474 if (!rr->esr->taskSpecificParameters)
476 yaz_log (LOG_WARN, "No task specific parameters");
478 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
480 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
482 zebra_result (zh, &rr->errcode, &rr->errstring);
484 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
486 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
487 yaz_log (LOG_LOG, "Received DB Update");
488 if (up->which == Z_IUUpdate_esRequest)
490 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
491 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
492 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
494 yaz_log (LOG_LOG, "action");
497 switch (*toKeep->action)
499 case Z_IUOriginPartToKeep_recordInsert:
500 yaz_log (LOG_LOG, "recordInsert");
502 case Z_IUOriginPartToKeep_recordReplace:
503 yaz_log (LOG_LOG, "recordUpdate");
505 case Z_IUOriginPartToKeep_recordDelete:
506 yaz_log (LOG_LOG, "recordDelete");
508 case Z_IUOriginPartToKeep_elementUpdate:
509 yaz_log (LOG_LOG, "elementUpdate");
511 case Z_IUOriginPartToKeep_specialUpdate:
512 yaz_log (LOG_LOG, "specialUpdate");
514 case Z_ESAdminOriginPartToKeep_shutdown:
515 yaz_log (LOG_LOG, "shutDown");
517 case Z_ESAdminOriginPartToKeep_start:
518 yaz_log (LOG_LOG, "start");
521 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
524 if (toKeep->databaseName)
526 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
528 if (zebra_select_database(zh, toKeep->databaseName))
533 yaz_log (LOG_WARN, "no database supplied for ES Update");
535 rr->errstring = "database";
541 zebra_begin_trans (zh, 1);
542 for (i = 0; i < notToKeep->num; i++)
544 Z_External *rec = notToKeep->elements[i]->record;
545 struct oident *oident = 0;
546 Odr_oct *opaque_recid = 0;
549 if (notToKeep->elements[i]->u.opaque)
551 switch(notToKeep->elements[i]->which)
553 case Z_IUSuppliedRecords_elem_opaque:
554 opaque_recid = notToKeep->elements[i]->u.opaque;
555 break; /* OK, recid already set */
556 case Z_IUSuppliedRecords_elem_number:
557 sysno = *notToKeep->elements[i]->u.number;
561 if (rec->direct_reference)
563 oident = oid_getentbyoid(rec->direct_reference);
565 yaz_log (LOG_LOG, "record %d type %s", i,
570 case Z_External_sutrs:
571 if (rec->u.octet_aligned->len > 170)
572 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
576 yaz_log (LOG_LOG, "%d bytes:\n%s",
580 case Z_External_octet:
581 if (rec->u.octet_aligned->len > 170)
582 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
583 rec->u.octet_aligned->len,
584 rec->u.octet_aligned->buf);
586 yaz_log (LOG_LOG, "%d bytes\n%s",
587 rec->u.octet_aligned->len,
588 rec->u.octet_aligned->buf);
590 if (oident && oident->value != VAL_TEXT_XML)
593 rr->errstring = "only XML update supported";
596 if (rec->which == Z_External_octet)
600 if (*toKeep->action ==
601 Z_IUOriginPartToKeep_recordInsert)
603 if (*toKeep->action ==
604 Z_IUOriginPartToKeep_recordReplace)
606 if (*toKeep->action ==
607 Z_IUOriginPartToKeep_recordDelete)
609 if (*toKeep->action ==
610 Z_IUOriginPartToKeep_specialUpdate)
616 rr->errstring = "unsupported ES Update action";
619 else if (opaque_recid)
621 int r = zebra_admin_exchange_record (
623 rec->u.octet_aligned->buf,
624 rec->u.octet_aligned->len,
625 opaque_recid->buf, opaque_recid->len,
630 rr->errstring = "record exchange failed";
639 r = zebra_insert_record(
645 rec->u.octet_aligned->buf,
646 rec->u.octet_aligned->len,
651 rr->errstring = "insert_record failed";
656 r = zebra_update_record(
662 rec->u.octet_aligned->buf,
663 rec->u.octet_aligned->len,
668 rr->errstring = "update_record failed";
672 r = zebra_delete_record(
678 rec->u.octet_aligned->buf,
679 rec->u.octet_aligned->len,
684 rr->errstring = "delete_record failed";
691 zebra_end_trans (zh);
697 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
698 rr->esr->taskSpecificParameters->which);
705 static void bend_start (struct statserv_options_block *sob)
708 zebra_stop((ZebraService) sob->handle);
709 sob->handle = zebra_start(sob->configname);
712 yaz_log (LOG_FATAL, "Failed to read config `%s'", sob->configname);
724 zebra_pidfname(sob->handle, pidfname);
726 fd = open (pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
731 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
734 fd = open(pidfname, O_RDWR, 0666);
737 yaz_log(LOG_FATAL|LOG_ERRNO, "lock file %s", pidfname);
741 area.l_type = F_WRLCK;
742 area.l_whence = SEEK_SET;
743 area.l_len = area.l_start = 0L;
744 if (fcntl (fd, F_SETLK, &area) == -1)
746 yaz_log(LOG_ERRNO|LOG_FATAL, "Zebra server already running");
753 sprintf (pidstr, "%ld", (long) getpid ());
754 write (fd, pidstr, strlen(pidstr));
760 static void bend_stop(struct statserv_options_block *sob)
765 if (!sob->inetd && sob->handle)
768 zebra_pidfname(sob->handle, pidfname);
774 ZebraService service = sob->handle;
779 int main (int argc, char **argv)
781 struct statserv_options_block *sob;
783 sob = statserv_getcontrol ();
784 strcpy (sob->configname, "zebra.cfg");
785 sob->bend_start = bend_start;
786 sob->bend_stop = bend_stop;
788 strcpy (sob->service_display_name, "Zebra Server");
790 statserv_setcontrol (sob);
792 return statserv_main (argc, argv, bend_init, bend_close);