Added body-of-text to BIB-1 ANY and the WAIS profile
[yaz-moved-to-github.git] / server / seshigh.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: seshigh.c,v $
7  * Revision 1.60  1996-05-30 11:03:10  quinn
8  * Fixed NextresultSetPosition bug fixed.
9  *
10  * Revision 1.59  1996/05/14  09:26:46  quinn
11  * Added attribute set to scan backend
12  *
13  * Revision 1.58  1996/02/20  12:53:04  quinn
14  * Chanes to SCAN
15  *
16  * Revision 1.57  1996/01/02  08:57:47  quinn
17  * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
18  *
19  * Revision 1.56  1995/12/14  11:09:57  quinn
20  * Work on Explain
21  *
22  * Revision 1.55  1995/11/08  17:41:37  quinn
23  * Smallish.
24  *
25  * Revision 1.54  1995/11/08  15:11:29  quinn
26  * Log of close transmit.
27  *
28  * Revision 1.53  1995/11/01  13:54:58  quinn
29  * Minor adjustments
30  *
31  * Revision 1.52  1995/11/01  12:19:13  quinn
32  * Second attempt to fix same bug.
33  *
34  * Revision 1.50  1995/10/25  16:58:32  quinn
35  * Simple.
36  *
37  * Revision 1.49  1995/10/16  13:51:53  quinn
38  * Changes to provide Especs to the backend.
39  *
40  * Revision 1.48  1995/10/06  08:51:20  quinn
41  * Added Write-buffer.
42  *
43  * Revision 1.47  1995/08/29  14:24:16  quinn
44  * Added second half of close-handshake
45  *
46  * Revision 1.46  1995/08/29  11:17:58  quinn
47  * Added code to receive close
48  *
49  * Revision 1.45  1995/08/21  09:11:00  quinn
50  * Smallish fixes to suppport new formats.
51  *
52  * Revision 1.44  1995/08/17  12:45:25  quinn
53  * Fixed minor problems with GRS-1. Added support in c&s.
54  *
55  * Revision 1.43  1995/08/15  12:00:31  quinn
56  * Updated External
57  *
58  * Revision 1.42  1995/08/15  11:16:50  quinn
59  *
60  * Revision 1.41  1995/08/02  10:23:06  quinn
61  * Smallish
62  *
63  * Revision 1.40  1995/07/31  14:34:26  quinn
64  * Fixed bug in process_searchResponse (numberOfRecordsReturned).
65  *
66  * Revision 1.39  1995/06/27  13:21:00  quinn
67  * SUTRS support
68  *
69  * Revision 1.38  1995/06/19  12:39:11  quinn
70  * Fixed bug in timeout code. Added BER dumper.
71  *
72  * Revision 1.37  1995/06/16  13:16:14  quinn
73  * Fixed Defaultdiagformat.
74  *
75  * Revision 1.36  1995/06/16  10:31:36  quinn
76  * Added session timeout.
77  *
78  * Revision 1.35  1995/06/15  07:45:14  quinn
79  * Moving to v3.
80  *
81  * Revision 1.34  1995/06/14  15:26:46  quinn
82  * *** empty log message ***
83  *
84  * Revision 1.33  1995/06/06  14:57:05  quinn
85  * Better diagnostics.
86  *
87  * Revision 1.32  1995/06/06  08:41:44  quinn
88  * Better diagnostics.
89  *
90  * Revision 1.31  1995/06/06  08:15:37  quinn
91  * Cosmetic.
92  *
93  * Revision 1.30  1995/06/05  10:53:32  quinn
94  * Added a better SCAN.
95  *
96  * Revision 1.29  1995/06/01  11:25:03  quinn
97  * Smallish.
98  *
99  * Revision 1.28  1995/06/01  11:21:01  quinn
100  * Attempting to fix a bug in pack-records. replaced break with continue
101  * for large records, according to standard.
102  *
103  * Revision 1.27  1995/05/29  08:12:06  quinn
104  * Moved oid to util
105  *
106  * Revision 1.26  1995/05/18  13:02:12  quinn
107  * Smallish.
108  *
109  * Revision 1.25  1995/05/17  08:42:26  quinn
110  * Transfer auth info to backend. Allow backend to reject init gracefully.
111  *
112  * Revision 1.24  1995/05/16  08:51:04  quinn
113  * License, documentation, and memory fixes
114  *
115  * Revision 1.23  1995/05/15  13:25:10  quinn
116  * Fixed memory bug.
117  *
118  * Revision 1.22  1995/05/15  11:56:39  quinn
119  * Asynchronous facilities. Restructuring of seshigh code.
120  *
121  * Revision 1.21  1995/05/02  08:53:19  quinn
122  * Trying in vain to fix comm with ISODE
123  *
124  * Revision 1.20  1995/04/20  15:13:00  quinn
125  * Cosmetic
126  *
127  * Revision 1.19  1995/04/18  08:15:34  quinn
128  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
129  * neater. We'll make the same change for decoding one day.
130  *
131  * Revision 1.18  1995/04/17  11:28:25  quinn
132  * Smallish
133  *
134  * Revision 1.17  1995/04/10  10:23:36  quinn
135  * Some work to add scan and other things.
136  *
137  * Revision 1.16  1995/03/31  09:18:55  quinn
138  * Added logging.
139  *
140  * Revision 1.15  1995/03/30  14:03:23  quinn
141  * Added RFC1006 as separate library
142  *
143  * Revision 1.14  1995/03/30  12:18:17  quinn
144  * Fixed bug.
145  *
146  * Revision 1.13  1995/03/30  09:09:24  quinn
147  * Added state-handle and some support for asynchronous activities.
148  *
149  * Revision 1.12  1995/03/29  15:40:16  quinn
150  * Ongoing work. Statserv is now dynamic by default
151  *
152  * Revision 1.11  1995/03/28  09:16:21  quinn
153  * Added record packing to the search request
154  *
155  * Revision 1.10  1995/03/27  08:34:24  quinn
156  * Added dynamic server functionality.
157  * Released bindings to session.c (is now redundant)
158  *
159  * Revision 1.9  1995/03/22  15:01:26  quinn
160  * Adjusting record packing.
161  *
162  * Revision 1.8  1995/03/22  10:13:21  quinn
163  * Working on record packer
164  *
165  * Revision 1.7  1995/03/21  15:53:31  quinn
166  * Little changes.
167  *
168  * Revision 1.6  1995/03/21  12:30:09  quinn
169  * Beginning to add support for record packing.
170  *
171  * Revision 1.5  1995/03/17  10:44:13  quinn
172  * Added catch of null-string in makediagrec
173  *
174  * Revision 1.4  1995/03/17  10:18:08  quinn
175  * Added memory management.
176  *
177  * Revision 1.3  1995/03/16  17:42:39  quinn
178  * Little changes
179  *
180  * Revision 1.2  1995/03/16  13:29:01  quinn
181  * Partitioned server.
182  *
183  * Revision 1.1  1995/03/15  16:02:10  quinn
184  * Modded session.c to seshigh.c
185  *
186  */
187
188 /*
189  * Frontend server logic.
190  *
191  * This code receives incoming APDUs, and handles client requests by means
192  * of the backend API.
193  *
194  * Some of the code is getting quite involved, compared to simpler servers -
195  * primarily because it is asynchronous both in the communication with
196  * the user and the backend. We think the complexity will pay off in
197  * the form of greater flexibility when more asynchronous facilities
198  * are implemented.
199  *
200  * Memory management has become somewhat involved. In the simple case, where
201  * only one PDU is pending at a time, it will simply reuse the same memory,
202  * once it has found its working size. When we enable multiple concurrent
203  * operations, perhaps even with multiple parallel calls to the backend, it
204  * will maintain a pool of buffers for encoding and decoding, trying to
205  * minimize memory allocation/deallocation during normal operation.
206  *
207  * TODOs include (and will be done in order of public interest):
208  * 
209  * Support for EXPLAIN - provide simple meta-database system.
210  * Support for access control.
211  * Support for resource control.
212  * Support for extended services - primarily Item Order.
213  * Rest of Z39.50-1994
214  *
215  */
216
217 #include <stdlib.h>
218 #include <stdio.h>
219 #include <unistd.h>
220 #include <assert.h>
221 #include <sys/time.h>
222
223 #include <xmalloc.h>
224 #include <comstack.h>
225 #include <eventl.h>
226 #include <session.h>
227 #include <proto.h>
228 #include <oid.h>
229 #include <log.h>
230 #include <statserv.h>
231
232 #include <backend.h>
233
234 static int process_request(association *assoc);
235 void backend_response(IOCHAN i, int event);
236 static int process_response(association *assoc, request *req, Z_APDU *res);
237 static Z_APDU *process_initRequest(association *assoc, request *reqb);
238 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
239     int *fd);
240 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
241     bend_searchresult *bsrt, int *fd);
242 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
243     int *fd);
244 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
245 static void process_close(association *assoc, request *reqb);
246
247 static FILE *apduf = 0; /* for use in static mode */
248 static statserv_options_block *control_block = 0;
249
250 /*
251  * Create and initialize a new association-handle.
252  *  channel  : iochannel for the current line.
253  *  link     : communications channel.
254  * Returns: 0 or a new association handle.
255  */
256 association *create_association(IOCHAN channel, COMSTACK link)
257 {
258     association *new;
259
260     if (!control_block)
261         control_block = statserv_getcontrol();
262     if (!(new = xmalloc(sizeof(*new))))
263         return 0;
264     new->client_chan = channel;
265     new->client_link = link;
266     if (!(new->decode = odr_createmem(ODR_DECODE)) ||
267         !(new->encode = odr_createmem(ODR_ENCODE)))
268         return 0;
269     if (*control_block->apdufile)
270     {
271         char filename[256];
272         FILE *f;
273
274         strcpy(filename, control_block->apdufile);
275         if (!(new->print = odr_createmem(ODR_PRINT)))
276             return 0;
277         if (*control_block->apdufile != '-')
278         {
279             strcpy(filename, control_block->apdufile);
280             if (!control_block->dynamic)
281             {
282                 if (!apduf)
283                 {
284                     if (!(apduf = fopen(filename, "w")))
285                     {
286                         logf(LOG_WARN|LOG_ERRNO, "%s", filename);
287                         return 0;
288                     }
289                     setvbuf(apduf, 0, _IONBF, 0);
290                 }
291                 f = apduf;
292             }
293             else 
294             {
295                 sprintf(filename + strlen(filename), ".%d", getpid());
296                 if (!(f = fopen(filename, "w")))
297                 {
298                     logf(LOG_WARN|LOG_ERRNO, "%s", filename);
299                     return 0;
300                 }
301                 setvbuf(f, 0, _IONBF, 0);
302             }
303             odr_setprint(new->print, f);
304         }
305     }
306     else
307         new->print = 0;
308     new->input_buffer = 0;
309     new->input_buffer_len = 0;
310     new->backend = 0;
311     new->state = ASSOC_NEW;
312     request_initq(&new->incoming);
313     request_initq(&new->outgoing);
314     new->proto = cs_getproto(link);
315     return new;
316 }
317
318 /*
319  * Free association and release resources.
320  */
321 void destroy_association(association *h)
322 {
323     odr_destroy(h->decode);
324     odr_destroy(h->encode);
325     if (h->print)
326         odr_destroy(h->print);
327     if (h->input_buffer)
328     xfree(h->input_buffer);
329     if (h->backend)
330         bend_close(h->backend);
331     while (request_deq(&h->incoming));
332     while (request_deq(&h->outgoing));
333    xfree(h);
334 }
335
336 static void do_close(association *a, int reason, char *message)
337 {
338     Z_APDU apdu;
339     Z_Close *cls = zget_Close(a->encode);
340     request *req = request_get();
341
342     /* Purge request queue */
343     while (request_deq(&a->incoming));
344     while (request_deq(&a->outgoing));
345     if (a->version >= 3)
346     {
347         logf(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
348             reason, message ? message : "none");
349         apdu.which = Z_APDU_close;
350         apdu.u.close = cls;
351         *cls->closeReason = reason;
352         cls->diagnosticInformation = message;
353         process_response(a, req, &apdu);
354         iochan_settimeout(a->client_chan, 60);
355     }
356     else
357     {
358         logf(LOG_DEBUG, "v2 client. No Close PDU");
359         iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
360     }
361     a->state = ASSOC_DEAD;
362 }
363
364 /*
365  * This is where PDUs from the client are read and the further
366  * processing is initiated. Flow of control moves down through the
367  * various process_* functions below, until the encoded result comes back up
368  * to the output handler in here.
369  * 
370  *  h     : the I/O channel that has an outstanding event.
371  *  event : the current outstanding event.
372  */
373 void ir_session(IOCHAN h, int event)
374 {
375     int res;
376     association *assoc = iochan_getdata(h);
377     COMSTACK conn = assoc->client_link;
378     request *req;
379
380     assert(h && conn && assoc);
381     if (event == EVENT_TIMEOUT)
382     {
383         if (assoc->state != ASSOC_UP)
384         {
385             logf(LOG_LOG, "Final timeout - closing connection.");
386             cs_close(conn);
387             destroy_association(assoc);
388             iochan_destroy(h);
389         }
390         else
391         {
392             logf(LOG_LOG, "Session idle too long. Sending close.");
393             do_close(assoc, Z_Close_lackOfActivity, 0);
394         }
395         return;
396     }
397     if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
398     {
399         if (event & EVENT_INPUT)
400         {
401             logf(LOG_DEBUG, "ir_session (input)");
402             assert(assoc && conn);
403             /* We aren't speaking to this fellow */
404             if (assoc->state == ASSOC_DEAD)
405             {
406                 logf(LOG_LOG, "Closed connection after reject");
407                 cs_close(conn);
408                 destroy_association(assoc);
409                 iochan_destroy(h);
410                 return;
411             }
412             if ((res = cs_get(conn, &assoc->input_buffer,
413                 &assoc->input_buffer_len)) <= 0)
414             {
415                 logf(LOG_LOG, "Connection closed by client");
416                 cs_close(conn);
417                 destroy_association(assoc);
418                 iochan_destroy(h);
419                 return;
420             }
421             else if (res == 1) /* incomplete read - wait for more  */
422                 return;
423             if (cs_more(conn)) /* more stuff - call us again later, please */
424                 iochan_setevent(h, EVENT_INPUT);
425                 
426             /* we got a complete PDU. Let's decode it */
427             logf(LOG_DEBUG, "Got PDU, %d bytes", res);
428             req = request_get(); /* get a new request structure */
429             odr_reset(assoc->decode);
430             odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
431             if (!z_APDU(assoc->decode, &req->request, 0))
432             {
433                 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
434                     odr_errlist[odr_geterror(assoc->decode)],
435                     odr_offset(assoc->decode));
436                 logf(LOG_LOG, "PDU dump:");
437                 odr_dumpBER(log_file(), assoc->input_buffer, res);
438                 do_close(assoc, Z_Close_protocolError, "Malformed package");
439                 return;
440             }
441             req->request_mem = odr_extract_mem(assoc->decode);
442             if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
443             {
444                 logf(LOG_WARN, "ODR print error: %s", 
445                     odr_errlist[odr_geterror(assoc->print)]);
446                 odr_reset(assoc->print);
447             }
448             request_enq(&assoc->incoming, req);
449         }
450
451         /* can we do something yet? */
452         req = request_head(&assoc->incoming);
453         if (req->state == REQUEST_IDLE)
454             if (process_request(assoc) < 0)
455                 do_close(assoc, Z_Close_systemProblem, "Unknown error");
456     }
457     if (event & EVENT_OUTPUT)
458     {
459         request *req = request_head(&assoc->outgoing);
460
461         logf(LOG_DEBUG, "ir_session (output)");
462         req->state = REQUEST_PENDING;
463         switch (res = cs_put(conn, req->response, req->len_response))
464         {
465             case -1:
466                 logf(LOG_LOG, "Connection closed by client");
467                 cs_close(conn);
468                 destroy_association(assoc);
469                 iochan_destroy(h);
470                 break;
471             case 0: /* all sent - release the request structure */
472                 logf(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
473                 nmem_destroy(req->request_mem);
474                 request_deq(&assoc->outgoing);
475                 request_release(req);
476                 if (!request_head(&assoc->outgoing))
477                     iochan_clearflag(h, EVENT_OUTPUT);
478                 break;
479             /* value of 1 -- partial send -- is simply ignored */
480         }
481     }
482     if (event & EVENT_EXCEPT)
483     {
484         logf(LOG_DEBUG, "ir_session (exception)");
485         cs_close(conn);
486         destroy_association(assoc);
487         iochan_destroy(h);
488     }
489 }
490
491 /*
492  * Initiate request processing.
493  */
494 static int process_request(association *assoc)
495 {
496     request *req = request_head(&assoc->incoming);
497     int fd = -1;
498     Z_APDU *res;
499
500     logf(LOG_DEBUG, "process_request");
501     assert(req && req->state == REQUEST_IDLE);
502     switch (req->request->which)
503     {
504         case Z_APDU_initRequest:
505             res = process_initRequest(assoc, req); break;
506         case Z_APDU_searchRequest:
507             res = process_searchRequest(assoc, req, &fd); break;
508         case Z_APDU_presentRequest:
509             res = process_presentRequest(assoc, req, &fd); break;
510         case Z_APDU_scanRequest:
511             res = process_scanRequest(assoc, req, &fd); break;
512         case Z_APDU_close:
513             process_close(assoc, req); return 0;
514         default:
515             logf(LOG_WARN, "Bad APDU received");
516             return -1;
517     }
518     if (res)
519     {
520         logf(LOG_DEBUG, "  result immediately available");
521         return process_response(assoc, req, res);
522     }
523     else if (fd < 0)
524     {
525         logf(LOG_WARN, "   bad result");
526         return -1;
527     }
528     else /* no result yet - one will be provided later */
529     {
530         IOCHAN chan;
531
532         /* Set up an I/O handler for the fd supplied by the backend */
533
534         logf(LOG_DEBUG, "   establishing handler for result");
535         req->state = REQUEST_PENDING;
536         if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
537             abort();
538         iochan_setdata(chan, assoc);
539         return 0;
540     }
541 }
542
543 /*
544  * Handle message from the backend.
545  */
546 void backend_response(IOCHAN i, int event)
547 {
548     association *assoc = iochan_getdata(i);
549     request *req = request_head(&assoc->incoming);
550     Z_APDU *res;
551     int fd;
552
553     logf(LOG_DEBUG, "backend_response");
554     assert(assoc && req && req->state != REQUEST_IDLE);
555     /* determine what it is we're waiting for */
556     switch (req->request->which)
557     {
558         case Z_APDU_searchRequest:
559             res = response_searchRequest(assoc, req, 0, &fd); break;
560 #if 0
561         case Z_APDU_presentRequest:
562             res = response_presentRequest(assoc, req, 0, &fd); break;
563         case Z_APDU_scanRequest:
564             res = response_scanRequest(assoc, req, 0, &fd); break;
565 #endif
566         default:
567             logf(LOG_WARN, "Serious programmer's lapse or bug");
568             abort();
569     }
570     if ((res && process_response(assoc, req, res) < 0) || fd < 0)
571     {
572         logf(LOG_LOG, "Fatal error when talking to backend");
573         do_close(assoc, Z_Close_systemProblem, 0);
574         iochan_destroy(i);
575         return;
576     }
577     else if (!res) /* no result yet - try again later */
578     {
579         logf(LOG_DEBUG, "   no result yet");
580         iochan_setfd(i, fd); /* in case fd has changed */
581     }
582 }
583
584 /*
585  * Encode response, and transfer the request structure to the outgoing queue.
586  */
587 static int process_response(association *assoc, request *req, Z_APDU *res)
588 {
589     odr_setbuf(assoc->encode, req->response, req->size_response, 1);
590     if (!z_APDU(assoc->encode, &res, 0))
591     {
592         logf(LOG_WARN, "ODR error when encoding response: %s",
593             odr_errlist[odr_geterror(assoc->decode)]);
594         odr_reset(assoc->encode);
595         return -1;
596     }
597     req->response = odr_getbuf(assoc->encode, &req->len_response,
598         &req->size_response);
599     odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
600     odr_reset(assoc->encode);
601     if (assoc->print && !z_APDU(assoc->print, &res, 0))
602     {
603         logf(LOG_WARN, "ODR print error: %s", 
604             odr_errlist[odr_geterror(assoc->print)]);
605         odr_reset(assoc->print);
606     }
607     /* change this when we make the backend reentrant */
608     if (req == request_head(&assoc->incoming))
609     {
610         req->state = REQUEST_IDLE;
611         request_deq(&assoc->incoming);
612     }
613     request_enq(&assoc->outgoing, req);
614     /* turn the work over to the ir_session handler */
615     iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
616     /* Is there more work to be done? give that to the input handler too */
617     if (request_head(&assoc->incoming))
618         iochan_setevent(assoc->client_chan, EVENT_WORK);
619     return 0;
620 }
621
622 /*
623  * Handle init request.
624  * At the moment, we don't check the options
625  * anywhere else in the code - we just try not to do anything that would
626  * break a naive client. We'll toss 'em into the association block when
627  * we need them there.
628  */
629 static Z_APDU *process_initRequest(association *assoc, request *reqb)
630 {
631     Z_InitRequest *req = reqb->request->u.initRequest;
632     Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
633     Z_InitResponse *resp = apdu->u.initResponse;
634     bend_initrequest binitreq;
635     bend_initresult *binitres;
636     char options[100];
637
638     logf(LOG_LOG, "Got initRequest");
639     if (req->implementationId)
640         logf(LOG_LOG, "Id:        %s", req->implementationId);
641     if (req->implementationName)
642         logf(LOG_LOG, "Name:      %s", req->implementationName);
643     if (req->implementationVersion)
644         logf(LOG_LOG, "Version:   %s", req->implementationVersion);
645
646     binitreq.configname = "default-config";
647     binitreq.auth = req->idAuthentication;
648     if (!(binitres = bend_init(&binitreq)))
649     {
650         logf(LOG_WARN, "Bad response from backend.");
651         return 0;
652     }
653
654     assoc->backend = binitres->handle;
655     resp->referenceId = req->referenceId;
656     *options = '\0';
657     /* let's tell the client what we can do */
658     if (ODR_MASK_GET(req->options, Z_Options_search))
659     {
660         ODR_MASK_SET(resp->options, Z_Options_search);
661         strcat(options, "srch");
662     }
663     if (ODR_MASK_GET(req->options, Z_Options_present))
664     {
665         ODR_MASK_SET(resp->options, Z_Options_present);
666         strcat(options, " prst");
667     }
668 #if 0
669     if (ODR_MASK_GET(req->options, Z_Options_delSet))
670     {
671         ODR_MASK_SET(&options, Z_Options_delSet);
672         strcat(options, " del");
673     }
674 #endif
675     if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
676     {
677         ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
678         strcat(options, " namedresults");
679     }
680     if (ODR_MASK_GET(req->options, Z_Options_scan))
681     {
682         ODR_MASK_SET(resp->options, Z_Options_scan);
683         strcat(options, " scan");
684     }
685     if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
686     {
687         ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
688         strcat(options, " concurop");
689     }
690
691     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
692     {
693         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
694         assoc->version = 2; /* 1 & 2 are equivalent */
695     }
696     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
697     {
698         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
699         assoc->version = 2;
700     }
701     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
702     {
703         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
704         assoc->version = 3;
705     }
706     logf(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
707     assoc->maximumRecordSize = *req->maximumRecordSize;
708     if (assoc->maximumRecordSize > control_block->maxrecordsize)
709         assoc->maximumRecordSize = control_block->maxrecordsize;
710     assoc->preferredMessageSize = *req->preferredMessageSize;
711     if (assoc->preferredMessageSize > assoc->maximumRecordSize)
712         assoc->preferredMessageSize = assoc->maximumRecordSize;
713     resp->preferredMessageSize = &assoc->preferredMessageSize;
714     resp->maximumRecordSize = &assoc->maximumRecordSize;
715     resp->implementationName = "Index Data/YAZ Generic Frontend Server";
716     if (binitres->errcode)
717     {
718         logf(LOG_LOG, "Connection rejected by backend.");
719         *resp->result = 0;
720         assoc->state = ASSOC_DEAD;
721     }
722     else
723         assoc->state = ASSOC_UP;
724     return apdu;
725 }
726
727 /*
728  * These functions should be merged.
729  */
730
731 /*
732  * nonsurrogate diagnostic record.
733  */
734 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
735 {
736     static Z_Records rec;
737     oident bib1;
738     static int err;
739 #ifdef Z_95
740     static Z_DiagRec drec;
741     static Z_DefaultDiagFormat dr;
742 #else
743     static Z_DiagRec dr;
744 #endif
745
746     bib1.proto = proto;
747     bib1.oclass = CLASS_DIAGSET;
748     bib1.value = VAL_BIB1;
749
750     logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
751         "NULL");
752     err = error;
753     rec.which = Z_Records_NSD;
754 #ifdef Z_95
755     rec.u.nonSurrogateDiagnostic = &drec;
756     drec.which = Z_DiagRec_defaultFormat;
757     drec.u.defaultFormat = &dr;
758 #else
759     rec.u.nonSurrogateDiagnostic = &dr;
760 #endif
761     dr.diagnosticSetId = oid_getoidbyent(&bib1);
762     dr.condition = &err;
763     dr.which = Z_DiagForm_v2AddInfo;
764     dr.addinfo = addinfo ? addinfo : "";
765     return &rec;
766 }
767
768 /*
769  * surrogate diagnostic.
770  */
771 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
772                                             int error, char *addinfo)
773 {
774     static Z_NamePlusRecord rec;
775     static int err;
776     oident bib1;
777 #ifdef Z_95
778     static Z_DiagRec drec;
779     static Z_DefaultDiagFormat dr;
780 #else
781     static Z_DiagRec dr;
782 #endif
783
784     bib1.proto = proto;
785     bib1.oclass = CLASS_DIAGSET;
786     bib1.value = VAL_BIB1;
787
788     logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
789     err = error;
790     rec.databaseName = dbname;
791     rec.which = Z_NamePlusRecord_surrogateDiagnostic;
792 #ifdef Z_95
793     rec.u.surrogateDiagnostic = &drec;
794     drec.which = Z_DiagRec_defaultFormat;
795     drec.u.defaultFormat = &dr;
796 #else
797     rec.u.surrogateDiagnostic = &dr;
798 #endif
799     dr.diagnosticSetId = oid_getoidbyent(&bib1);
800     dr.condition = &err;
801     dr.which = Z_DiagForm_v2AddInfo;
802     dr.addinfo = addinfo ? addinfo : "";
803     return &rec;
804 }
805
806 /*
807  * multiple nonsurrogate diagnostics.
808  */
809 static Z_DiagRecs *diagrecs(oid_proto proto, int error, char *addinfo)
810 {
811     static Z_DiagRecs recs;
812     static int err;
813     oident bib1;
814 #ifdef Z_95
815     static Z_DiagRec *recp[1], drec;
816     static Z_DefaultDiagFormat rec;
817 #else
818     static Z_DiagRec *recp[1], rec;
819 #endif
820
821     logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
822     bib1.proto = proto;
823     bib1.oclass = CLASS_DIAGSET;
824     bib1.value = VAL_BIB1;
825
826     err = error;
827     recs.num_diagRecs = 1;
828     recs.diagRecs = recp;
829 #ifdef Z_95
830     recp[0] = &drec;
831     drec.which = Z_DiagRec_defaultFormat;
832     drec.u.defaultFormat = &rec;
833 #else
834     recp[0] = &rec;
835 #endif
836     rec.diagnosticSetId = oid_getoidbyent(&bib1);
837     rec.condition = &err;
838     rec.which = Z_DiagForm_v2AddInfo;
839     rec.addinfo = addinfo ? addinfo : "";
840     return &recs;
841 }
842
843 #define MAX_RECORDS 256
844
845 static Z_Records *pack_records(association *a, char *setname, int start,
846                                 int *num, Z_RecordComposition *comp,
847                                 int *next, int *pres, oid_value format)
848 {
849     int recno, total_length = 0, toget = *num;
850     static Z_Records records;
851     static Z_NamePlusRecordList reclist;
852     static Z_NamePlusRecord *list[MAX_RECORDS];
853     oident recform;
854
855     records.which = Z_Records_DBOSD;
856     records.u.databaseOrSurDiagnostics = &reclist;
857     reclist.num_records = 0;
858     reclist.records = list;
859     *pres = Z_PRES_SUCCESS;
860     *num = 0;
861     *next = 0;
862
863     logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
864     logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
865         a->maximumRecordSize);
866     for (recno = start; reclist.num_records < toget; recno++)
867     {
868         bend_fetchrequest freq;
869         bend_fetchresult *fres;
870         Z_NamePlusRecord *thisrec;
871         Z_DatabaseRecord *thisext;
872         int this_length;
873
874         /*
875          * we get the number of bytes allocated on the stream before any
876          * allocation done by the backend - this should give us a reasonable
877          * idea of the total size of the data so far.
878          */
879         total_length = odr_total(a->encode);
880         if (reclist.num_records == MAX_RECORDS - 1)
881         {
882             *pres = Z_PRES_PARTIAL_2;
883             break;
884         }
885         freq.setname = setname;
886         freq.number = recno;
887         freq.comp = comp;
888         freq.format = format;
889         freq.stream = a->encode;
890         if (!(fres = bend_fetch(a->backend, &freq, 0)))
891         {
892             *pres = Z_PRES_FAILURE;
893             return diagrec(a->proto, 2, "Backend interface problem");
894         }
895         /* backend should be able to signal whether error is system-wide
896            or only pertaining to current record */
897         if (fres->errcode)
898         {
899             *pres = Z_PRES_FAILURE;
900             return diagrec(a->proto, fres->errcode, fres->errstring);
901         }
902         if (fres->len >= 0)
903             this_length = fres->len;
904         else
905             this_length = odr_total(a->encode) - total_length;
906         logf(LOG_DEBUG, "  fetched record, len=%d, total=%d",
907             this_length, total_length);
908         if (this_length + total_length > a->preferredMessageSize)
909         {
910             /* record is small enough, really */
911             if (this_length <= a->preferredMessageSize)
912             {
913                 logf(LOG_DEBUG, "  Dropped last normal-sized record");
914                 *pres = Z_PRES_PARTIAL_2;
915                 break;
916             }
917             /* record can only be fetched by itself */
918             if (this_length < a->maximumRecordSize)
919             {
920                 logf(LOG_DEBUG, "  Record > prefmsgsz");
921                 if (toget > 1)
922                 {
923                     logf(LOG_DEBUG, "  Dropped it");
924                     reclist.records[reclist.num_records] =
925                          surrogatediagrec(a->proto, fres->basename, 16, 0);
926                     reclist.num_records++;
927                     *next = fres->last_in_set ? 0 : recno + 1;
928                     continue;
929                 }
930             }
931             else /* too big entirely */
932             {
933                 logf(LOG_DEBUG, "Record > maxrcdsz");
934                 reclist.records[reclist.num_records] =
935                     surrogatediagrec(a->proto, fres->basename, 17, 0);
936                 reclist.num_records++;
937                 *next = fres->last_in_set ? 0 : recno + 1;
938                 continue;
939             }
940         }
941         if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
942             return 0;
943         if (!(thisrec->databaseName = odr_malloc(a->encode,
944             strlen(fres->basename) + 1)))
945             return 0;
946         strcpy(thisrec->databaseName, fres->basename);
947         thisrec->which = Z_NamePlusRecord_databaseRecord;
948         if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
949             sizeof(Z_DatabaseRecord))))
950             return 0;
951         recform.proto = a->proto;
952         recform.oclass = CLASS_RECSYN;
953         recform.value = fres->format;
954         thisext->direct_reference = odr_oiddup(a->encode,
955             oid_getoidbyent(&recform));
956         thisext->indirect_reference = 0;
957         thisext->descriptor = 0;
958         if (fres->len < 0) /* Structured data */
959         {
960             switch (fres->format)
961             {
962                 case VAL_SUTRS: thisext->which = Z_External_sutrs; break;
963                 case VAL_GRS1: thisext->which = Z_External_grs1; break;
964                 case VAL_EXPLAIN: thisext->which = Z_External_explainRecord;
965                     break;
966
967                 default:
968                     logf(LOG_FATAL, "Unknown structured format from backend.");
969                     return 0;
970             }
971
972             /*
973              * We cheat on the pointers here. Obviously, the record field
974              * of the backend-fetch structure should have been a union for
975              * correctness, but we're stuck with this for backwards
976              * compatibility.
977              */
978             thisext->u.grs1 = (Z_GenericRecord*) fres->record;
979         }
980         else if (fres->format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
981         {
982             Odr_oct *sutrs = odr_malloc(a->encode, sizeof(*sutrs));
983
984             thisext->which = Z_External_sutrs;
985             thisext->u.sutrs = sutrs;
986             sutrs->buf = odr_malloc(a->encode, fres->len);
987             sutrs->len = sutrs->size = fres->len;
988             memcpy(sutrs->buf, fres->record, fres->len);
989         }
990         else /* octet-aligned record. */
991         {
992             thisext->which = Z_External_octet;
993             if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
994                 sizeof(Odr_oct))))
995                 return 0;
996             if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode,
997                 fres->len)))
998                 return 0;
999             memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
1000             thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
1001                 fres->len;
1002         }
1003         reclist.records[reclist.num_records] = thisrec;
1004         reclist.num_records++;
1005         *next = fres->last_in_set ? 0 : recno + 1;
1006     }
1007     *num = reclist.num_records;
1008     return &records;
1009 }
1010
1011 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1012     int *fd)
1013 {
1014     Z_SearchRequest *req = reqb->request->u.searchRequest;
1015     bend_searchrequest bsrq;
1016     bend_searchresult *bsrt;
1017
1018     logf(LOG_LOG, "Got SearchRequest.");
1019
1020     bsrq.setname = req->resultSetName;
1021     bsrq.replace_set = *req->replaceIndicator;
1022     bsrq.num_bases = req->num_databaseNames;
1023     bsrq.basenames = req->databaseNames;
1024     bsrq.query = req->query;
1025
1026     if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
1027         return 0;
1028     return response_searchRequest(assoc, reqb, bsrt, fd);
1029 }
1030
1031 bend_searchresult *bend_searchresponse(void *handle) {return 0;}
1032
1033 /*
1034  * Prepare a searchresponse based on the backend results. We probably want
1035  * to look at making the fetching of records nonblocking as well, but
1036  * so far, we'll keep things simple.
1037  * If bsrt is null, that means we're called in response to a communications
1038  * event, and we'll have to get the response for ourselves.
1039  */
1040 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1041     bend_searchresult *bsrt, int *fd)
1042 {
1043     Z_SearchRequest *req = reqb->request->u.searchRequest;
1044     static Z_APDU apdu;
1045     static Z_SearchResponse resp;
1046     static int nulint = 0;
1047     static bool_t sr = 1;
1048     static int next = 0;
1049     static int none = Z_RES_NONE;
1050
1051     apdu.which = Z_APDU_searchResponse;
1052     apdu.u.searchResponse = &resp;
1053     resp.referenceId = req->referenceId;
1054 #ifdef Z_95
1055     resp.additionalSearchInfo = 0;
1056     resp.otherInfo = 0;
1057 #endif
1058     *fd = -1;
1059     if (!bsrt && !(bsrt = bend_searchresponse(assoc->backend)))
1060     {
1061         logf(LOG_FATAL, "Bad result from backend");
1062         return 0;
1063     }
1064     else if (bsrt->errcode)
1065     {
1066         resp.records = diagrec(assoc->proto, bsrt->errcode,
1067             bsrt->errstring);
1068         resp.resultCount = &nulint;
1069         resp.numberOfRecordsReturned = &nulint;
1070         resp.nextResultSetPosition = &nulint;
1071         resp.searchStatus = &nulint;
1072         resp.resultSetStatus = &none;
1073         resp.presentStatus = 0;
1074     }
1075     else
1076     {
1077         static int toget;
1078         Z_RecordComposition comp, *compp = 0;
1079         static int presst = 0;
1080
1081         resp.records = 0;
1082         resp.resultCount = &bsrt->hits;
1083
1084         comp.which = Z_RecordComp_simple;
1085         /* how many records does the user agent want, then? */
1086         if (bsrt->hits <= *req->smallSetUpperBound)
1087         {
1088             toget = bsrt->hits;
1089             if ((comp.u.simple = req->smallSetElementSetNames))
1090                 compp = &comp;
1091         }
1092         else if (bsrt->hits < *req->largeSetLowerBound)
1093         {
1094             toget = *req->mediumSetPresentNumber;
1095             if (toget > bsrt->hits)
1096                 toget = bsrt->hits;
1097             if ((comp.u.simple = req->mediumSetElementSetNames))
1098                 compp = &comp;
1099         }
1100         else
1101             toget = 0;
1102
1103         if (toget && !resp.records)
1104         {
1105             oident *prefformat;
1106             oid_value form;
1107
1108             if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1109                 prefformat->oclass != CLASS_RECSYN)
1110                 form = VAL_NONE;
1111             else
1112                 form = prefformat->value;
1113             resp.records = pack_records(assoc, req->resultSetName, 1,
1114                 &toget, compp, &next, &presst, form);
1115             if (!resp.records)
1116                 return 0;
1117             resp.numberOfRecordsReturned = &toget;
1118             resp.nextResultSetPosition = &next;
1119             resp.searchStatus = &sr;
1120             resp.resultSetStatus = 0;
1121             resp.presentStatus = &presst;
1122         }
1123         else
1124         {
1125             if (*resp.resultCount)
1126                 next = 1;
1127             resp.numberOfRecordsReturned = &nulint;
1128             resp.nextResultSetPosition = &next;
1129             resp.searchStatus = &sr;
1130             resp.resultSetStatus = 0;
1131             resp.presentStatus = 0;
1132         }
1133     }
1134     return &apdu;
1135 }
1136
1137 /*
1138  * Maybe we got a little over-friendly when we designed bend_fetch to
1139  * get only one record at a time. Some backends can optimise multiple-record
1140  * fetches, and at any rate, there is some overhead involved in
1141  * all that selecting and hopping around. Problem is, of course, that the
1142  * frontend can't know ahead of time how many records it'll need to
1143  * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1144  * is downright lousy as a bulk data transfer protocol.
1145  *
1146  * To start with, we'll do the fetching of records from the backend
1147  * in one operation: To save some trips in and out of the event-handler,
1148  * and to simplify the interface to pack_records. At any rate, asynch
1149  * operation is more fun in operations that have an unpredictable execution
1150  * speed - which is normally more true for search than for present.
1151  */
1152 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1153     int *fd)
1154 {
1155     Z_PresentRequest *req = reqb->request->u.presentRequest;
1156     static Z_APDU apdu;
1157     static Z_PresentResponse resp;
1158     static int presst, next, num;
1159     oident *prefformat;
1160     oid_value form;
1161
1162
1163     logf(LOG_LOG, "Got PresentRequest.");
1164     apdu.which = Z_APDU_presentResponse;
1165     apdu.u.presentResponse = &resp;
1166     resp.referenceId = req->referenceId;
1167 #ifdef Z_95
1168     resp.otherInfo = 0;
1169 #endif
1170
1171     if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1172         prefformat->oclass != CLASS_RECSYN)
1173         form = VAL_NONE;
1174     else
1175         form = prefformat->value;
1176     num = *req->numberOfRecordsRequested;
1177     resp.records = pack_records(assoc, req->resultSetId,
1178         *req->resultSetStartPoint, &num, req->recordComposition, &next,
1179         &presst, form);
1180     if (!resp.records)
1181         return 0;
1182     resp.numberOfRecordsReturned = &num;
1183     resp.presentStatus = &presst;
1184     resp.nextResultSetPosition = &next;
1185
1186     return &apdu;
1187 }
1188
1189 /*
1190  * Scan was implemented rather in a hurry, and with support for only the basic
1191  * elements of the service in the backend API. Suggestions are welcome.
1192  */
1193 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1194 {
1195     Z_ScanRequest *req = reqb->request->u.scanRequest;
1196     static Z_APDU apdu;
1197     static Z_ScanResponse res;
1198     static int scanStatus = Z_Scan_failure;
1199     static int numberOfEntriesReturned = 0;
1200     oident *attent;
1201     static Z_ListEntries ents;
1202 #define SCAN_MAX_ENTRIES 200
1203     static Z_Entry *tab[SCAN_MAX_ENTRIES];
1204     bend_scanrequest srq;
1205     bend_scanresult *srs;
1206     oident *attset;
1207
1208     logf(LOG_LOG, "Got scanrequest");
1209     apdu.which = Z_APDU_scanResponse;
1210     apdu.u.scanResponse = &res;
1211     res.referenceId = req->referenceId;
1212     res.stepSize = 0;
1213     res.scanStatus = &scanStatus;
1214     res.numberOfEntriesReturned = &numberOfEntriesReturned;
1215     res.positionOfTerm = 0;
1216     res.entries = &ents;
1217     ents.which = Z_ListEntries_nonSurrogateDiagnostics;
1218     res.attributeSet = 0;
1219 #ifdef Z_95
1220     res.otherInfo = 0;
1221 #endif
1222
1223     if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1224         attent->oclass != CLASS_ATTSET || attent->value != VAL_BIB1))
1225         ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 121, 0);
1226     else if (req->stepSize && *req->stepSize > 0)
1227         ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 205, 0);
1228     else
1229     {
1230         if (req->termListAndStartPoint->term->which == Z_Term_general)
1231             logf(LOG_DEBUG, " term: '%.*s'",
1232                 req->termListAndStartPoint->term->u.general->len,
1233                 req->termListAndStartPoint->term->u.general->buf);
1234         srq.num_bases = req->num_databaseNames;
1235         srq.basenames = req->databaseNames;
1236         srq.num_entries = *req->numberOfTermsRequested;
1237         srq.term = req->termListAndStartPoint;
1238         if (!(attset = oid_getentbyoid(req->attributeSet)) ||
1239             attset->oclass != CLASS_RECSYN)
1240             srq.attributeset = VAL_NONE;
1241         else
1242             srq.attributeset = attset->value;
1243         srq.term_position = req->preferredPositionInResponse ?
1244             *req->preferredPositionInResponse : 1;
1245         if (!(srs = bend_scan(assoc->backend, &srq, 0)))
1246             ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 2, 0);
1247         else if (srs->errcode)
1248             ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto,
1249                 srs->errcode, srs->errstring);
1250         else
1251         {
1252             int i;
1253             static Z_Entries list;
1254
1255             if (srs->status == BEND_SCAN_PARTIAL)
1256                 scanStatus = Z_Scan_partial_5;
1257             else
1258                 scanStatus = Z_Scan_success;
1259             ents.which = Z_ListEntries_entries;
1260             ents.u.entries = &list;
1261             list.entries = tab;
1262             for (i = 0; i < srs->num_entries; i++)
1263             {
1264                 Z_Entry *e;
1265                 Z_TermInfo *t;
1266                 Odr_oct *o;
1267
1268                 if (i >= SCAN_MAX_ENTRIES)
1269                 {
1270                     scanStatus = Z_Scan_partial_4;
1271                     break;
1272                 }
1273                 list.entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
1274                 e->which = Z_Entry_termInfo;
1275                 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
1276                 t->suggestedAttributes = 0;
1277                 t->displayTerm = 0;
1278                 t->alternativeTerm = 0;
1279                 t->byAttributes = 0;
1280                 t->otherTermInfo = 0;
1281                 t->globalOccurrences = &srs->entries[i].occurrences;
1282                 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
1283                 t->term->which = Z_Term_general;
1284                 t->term->u.general = o = odr_malloc(assoc->encode,
1285                     sizeof(Odr_oct));
1286                 o->buf = odr_malloc(assoc->encode, o->len = o->size =
1287                     strlen(srs->entries[i].term));
1288                 memcpy(o->buf, srs->entries[i].term, o->len);
1289                 logf(LOG_DEBUG, "  term #%d: '%s' (%d)", i,
1290                     srs->entries[i].term, srs->entries[i].occurrences);
1291             }
1292             list.num_entries = i;
1293             res.numberOfEntriesReturned = &list.num_entries;
1294             res.positionOfTerm = &srs->term_position;
1295         }
1296     }
1297
1298     return &apdu;
1299 }
1300
1301 static void process_close(association *assoc, request *reqb)
1302 {
1303     Z_Close *req = reqb->request->u.close;
1304     static char *reasons[] =
1305     {
1306         "finished",
1307         "shutdown",
1308         "systemProblem",
1309         "costLimit",
1310         "resources",
1311         "securityViolation",
1312         "protocolError",
1313         "lackOfActivity",
1314         "peerAbort",
1315         "unspecified"
1316     };
1317
1318     logf(LOG_LOG, "Got close, reason %s, message %s",
1319         reasons[*req->closeReason], req->diagnosticInformation ?
1320         req->diagnosticInformation : "NULL");
1321     if (assoc->version < 3) /* to make do_force respond with close */
1322         assoc->version = 3;
1323     do_close(assoc, Z_Close_finished, "Association terminated by client");
1324 }