Edited ZDist zlayer to use new API.
[egate.git] / zlayer-zdist / zaccess.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /*
45  * Europagate, 1995
46  *
47  * Z39.50 API for the Email gateway
48  *
49  * $Log: zaccess.c,v $
50  * Revision 1.2  1995/07/05 10:20:44  adam
51  * Edited ZDist zlayer to use new API.
52  *
53  * Revision 1.1  1995/07/03  08:21:43  adam
54  * Zdist layer moved to new sub directory.
55  *
56  * Revision 1.17  1995/05/16  09:41:47  adam
57  * LICENSE. Uses new versions of odr_{set,get}buf.
58  *
59  * Revision 1.16  1995/04/20  15:25:34  quinn
60  * Asynch. API
61  *
62  * Revision 1.15  1995/04/17  11:26:55  quinn
63  * Added YAZ version of zaccess
64  *
65  * Revision 1.14  1995/02/23  08:32:26  adam
66  * Changed header.
67  *
68  * Revision 1.12  1995/02/20  20:35:37  quinn
69  * Pull present status from presresp.
70  *
71  * Revision 1.11  1995/02/20  18:58:05  quinn
72  * Added hack for record in ANY
73  *
74  * Revision 1.10  1995/02/20  18:19:30  quinn
75  * More relaxed about record types.
76  *
77  * Revision 1.9  1995/02/17  15:17:51  quinn
78  * Bug fix
79  *
80  * Revision 1.8  1995/02/17  14:48:41  quinn
81  * 'nother bug in present
82  *
83  * Revision 1.7  1995/02/17  14:42:21  quinn
84  * Trivial bug in fetch-loop.
85  *
86  * Revision 1.6  1995/02/17  14:41:22  quinn
87  * Debugging.
88  *
89  * Revision 1.5  1995/02/17  13:58:01  quinn
90  * First kick at present handling
91  *
92  * Revision 1.4  1995/02/16  15:33:45  quinn
93  * Fixed bug in KWAQS generator
94  *
95  * Revision 1.3  1995/02/16  15:20:45  quinn
96  * Added initialization of response from search
97  *
98  * Revision 1.2  1995/02/16  15:14:53  quinn
99  * Fixed KWAQS-generator
100  *
101  * Revision 1.1  1995/02/16  14:47:55  quinn
102  * First kick.
103  *
104  */
105
106 /*
107  * Interface to the Z39.50 toolkit. Primary function is to hide Zdist, or
108  * whatever lower-layer we decide to use later. The decision to add a
109  * layer atop the toolkit was twofold: It vastly simplifies the
110  * implementation of the protocol persistence, and it hides Zdist. The
111  * latter is useful after Zdist has gone and changed their fine API after
112  * we went through all the trouble of documenting it in our Design. Don't
113  * want that to happen again.
114  *
115  * For the time being at least, we'll have these routines hang (or err) if
116  * they get a WOULDBLOCK on a write. That seems safe since, under normal
117  * circumstances, the network buffers should always be able to absorb
118  * the small request packages.
119  */
120
121 #include <stdlib.h>
122 #include <assert.h>
123
124 #include <z3950.h>
125 #include <z3950sup.h>
126 #include <zutil.h>
127
128 #include <gw-log.h>
129
130 #include <ccl.h>
131 #include <zaccess.h>
132
133 struct zass    /* Z-assoc */
134 {
135     NETBOXPROFILE *ass;              /* ZDIST association handle */
136     int fd;                         /* low-level socket (for select only) */
137     int maxrecordsize;
138     int preferredmessagesize;
139     char *buf;                      /* intermediary buffer */
140 };
141
142 int rpn2kwaqs(struct ccl_rpn_node *q, char **p)
143 {
144     struct ccl_rpn_attr *i;
145     static char *ops[] = {"and", "or", "not"};
146
147     assert(!CCL_RPN_AND);
148     switch (q->kind)
149     {
150         case CCL_RPN_TERM:
151             strcpy(*p, q->u.t.term);
152             (*p) += strlen(q->u.t.term);
153             if (q->u.t.attr_list)
154             {
155                 strcat(*p, "[");
156                 (*p)++;
157                 for (i = q->u.t.attr_list; i; i = i->next)
158                 {
159                     sprintf(*p, "%d,%d%s", i->type, i->value, i->next ?
160                         "," : "");
161                     *p += strlen(*p);
162                 }
163                 strcat(*p, "]");
164                 (*p)++;
165             }
166             return 0;
167         case CCL_RPN_SET:
168             gw_log(GW_LOG_FATAL, ZASS_TYPE, "KWAQS Doesn't support set refs");
169             return -1;
170         case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT:
171             strcpy(*p, ops[q->kind]);
172             *p += strlen(ops[q->kind]);
173             strcat(*p, "(");
174             (*p)++;
175             if (rpn2kwaqs(q->u.p[0], p) < 0)
176                 return -1;
177             strcat(*p, ",");
178             (*p)++;
179             if (rpn2kwaqs(q->u.p[1], p) < 0)
180                 return -1;
181             strcat(*p, ")");
182             (*p)++;
183             return 0;
184         default:
185             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Unknown RPN node");
186             return -1;
187     }
188 }
189
190 int zass_openresult(ZASS p, int *complete)
191 {
192     int len;
193     PINITRESPONSE ires;
194
195     if ((len = zutil_GetBERFromNet(p->ass, (unsigned char*)p->buf,
196         p->maxrecordsize)) <= 0)
197     {
198         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive initresponse");
199         return 0;
200     }
201     ires = (PINITRESPONSE) zutil_CreateFromData((unsigned char*)p->buf, len);
202     if (InitResponse_GetTag(ires) != INITRESPONSE_TAG)
203     {
204         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected initresponse from target");
205         return 0;
206     }
207     gw_log(ZASS_DEBUG, ZASS_TYPE, "Got initresponse");
208     if (!InitResponse_GetResult(ires))
209     {
210         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Access to target denied.");
211         return 0;
212     }
213     gw_log(ZASS_DEBUG, ZASS_TYPE, "Connected OK");
214     p->preferredmessagesize = InitResponse_GetPreferredMessageSize(ires);
215     p->maxrecordsize = InitResponse_GetExceptionalRecordSize(ires);
216     InitResponse_Destroy(ires);
217     *complete = 1;
218     return 0;
219 }
220
221 ZASS zass_open(char *host, int port, int *complete, char *auth)
222 {
223     struct zass *p;
224     PINITREQUEST ireq;
225     PINITRESPONSE ires;
226     int len;
227     char name[512];
228
229     if (!(p = malloc(sizeof(*p))))
230     {
231         gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
232         return 0;
233     }
234     p->maxrecordsize = ZASS_MAXRECORDSIZE;
235     p->preferredmessagesize = ZASS_PREFERREDMESSAGESIZE;
236     if (!(p->buf = malloc(ZASS_MAXRECORDSIZE + 100)))
237     {
238         gw_log(GW_LOG_FATAL, ZASS_TYPE, "alloc zass-buffer");
239         return 0;
240     }
241     if (!(p->ass = NEWSTRUCT(NETBOXPROFILE)))
242     {
243         gw_log(GW_LOG_FATAL, ZASS_TYPE, "memory alloc failed");
244         return 0;
245     }
246     p->ass->TimeOutSec = 120;
247     p->ass->TimeOutUSec = 0;
248     strcpy(p->ass->HostName, host);
249     p->ass->Port = port;
250
251     if (netbox_Open(p->ass) != 1)
252     {
253         gw_log(GW_LOG_WARN, ZASS_TYPE, "netbox_Open failed");
254         return 0;
255     }
256     gw_log(ZASS_DEBUG, ZASS_TYPE, "Opened connection to %s:%d",
257          p->ass->HostName, p->ass->Port);
258     sprintf(name, "%s (ZDIST protocol layer)", ZASS_NAME);
259     ireq = InitRequest_CreateInitAllASCII(0, "yy", "yy", p->maxrecordsize,
260         p->preferredmessagesize, ZASS_ID, name, ZASS_VERSION, 0);
261     if (!ireq)
262     {
263         gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create initrequest");
264         return 0;
265     }
266     zutil_GetBEREncodedBuffer(ireq, (unsigned char*)p->buf, &len,
267         p->maxrecordsize);
268     if (len <= 0)
269     {
270         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
271         return 0;
272     }
273     InitRequest_Destroy(ireq);
274     if (netbox_SendBuffer(p->ass, p->buf, len) != len)
275     {
276         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
277         return 0;
278     }
279     gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent initrequest.");
280
281     if (zass_openresult(p, complete) < 0 && (!complete || *complete))
282         return 0;
283     else
284         return p;
285
286 }
287
288 const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
289     char *resname, char *databases, int *complete)
290 {
291     static struct zass_searchent r;
292     char kwaqs[512], *p;
293     DATA_DIR *pdu, *record;
294     int len;
295
296     p = kwaqs;
297     if (rpn2kwaqs(query, &p) < 0)
298     {
299         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode query");
300         return 0;
301     }
302     gw_log(ZASS_DEBUG, ZASS_TYPE, "Query: KWAQS: '%s'", kwaqs);
303     pdu = SearchRequest_CreateInitAllASCII(0, 0, 2, 0, 1, resname, databases,
304         0, 0, 0, kwaqs, BIB1_OID);
305     if (!pdu)
306     {
307         gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create searchrequest");
308         return 0;
309     }
310     zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
311         a->maxrecordsize);
312     if (len <= 0)
313     {
314         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode initrequest");
315         return 0;
316     }
317     SearchRequest_Destroy(pdu);
318     if (netbox_SendBuffer(a->ass, a->buf, len) != len)
319     {
320         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send initrequest");
321         return 0;
322     }
323     gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent searchrequest.");
324     if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
325         a->maxrecordsize)) <= 0)
326     {
327         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive searchresponse");
328         return 0;
329     }
330     pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
331     if (zutil_GetTag(pdu) != SEARCHRESPONSE_TAG)
332     {
333         gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected serchresponse from target");
334         return 0;
335     }
336     gw_log(ZASS_DEBUG, ZASS_TYPE, "Got searchresponse");
337     r.status = SearchResponse_GetSearchStatus(pdu);
338     r.num = SearchResponse_GetResultCount(pdu);
339     r.status = SearchResponse_GetResultSetStatus(pdu);
340     r.errcode = -1;
341     *r.errstring = '\0';
342     if ((record = SearchResponse_GetRecords(pdu)))
343     {
344         if (zutil_GetTag(record) == NONSURROGATEDIAGNOSTIC_TAG)
345         {
346             DATA_DIR *ad;
347
348             r.errcode = zutil_GetTaggedInt(record, ASN1_INTEGER);
349             if ((ad = zutil_GetTaggedObject(record, ASN1_VISIBLESTRING)))
350             {
351                 char *s;
352
353                 if ((s = OctetString_GetASCIIString(ad)))
354                 {
355                     strcpy(r.errstring, s);
356                     FREE(s);
357                 }
358             }
359         }
360         else
361             gw_log(GW_LOG_WARN, ZASS_TYPE, "Got real record in SRCHRESP");
362     }
363     SearchResponse_Destroy(pdu);
364
365     return &r;
366 }
367
368 /*
369  * Triple indirection - that's kinda heavy. We'll fix it later.
370  * There are worse things around, though. Like ZDist.
371  */
372 void get_diagrec(zass_record ***p, DATA_DIR *rec)
373 {
374     DATA_DIR *ad;
375
376     **p = malloc(sizeof(***p));
377     (**p)->next = 0;
378     (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
379     if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
380     {
381         char *s;
382
383         if ((s = OctetString_GetASCIIString(ad)))
384         {
385             strcpy((**p)->errstring, s);
386             FREE(s);
387         }
388     }
389     (**p)->which = ZASS_REC_DIAG;
390     *p = &(**p)->next;
391 }
392
393 void get_responserecords(zass_record ***p, DATA_DIR *rec)
394 {
395     int num, recsyntaxlen, i;
396     DATA_DIR *record, *retrec, *align;
397     PEXTERNAL ext;
398     POBJECTIDENTIFIER oid;
399     char recsyntax[256];
400
401     num = ResponseRecords_GetCount(rec);
402     for (i = 1; i <= num; i++)
403     {
404         record = ResponseRecords_GetRecord(rec, i);
405         if (!record)
406         {
407             gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
408             return;
409         }
410         retrec = NamePlusRecord_GetRetrievalRecord(record);
411         if (!retrec)
412         {
413             /* check if it's a diagrec */
414             if (record->ptr.child->fldid == 2)
415                 get_diagrec(p, record->ptr.child);
416             else
417             {
418                 gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
419                 return;
420             }
421         }
422         ext = RetrievalRecord_GetExternal(retrec);
423         if (!ext)
424         {
425             gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
426             return;
427         }
428         oid = External_GetDirectReference(ext);
429         if (!oid)
430         {
431             gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
432             return;
433         }
434         recsyntaxlen = DirectReference_GetLength(oid);
435         memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
436         recsyntax[recsyntaxlen] = '\0';
437         **p = malloc(sizeof(***p));
438         (**p)->next = 0;
439         if (!strcmp(recsyntax, USMARC_OID))
440             (**p)->which = ZASS_REC_USMARC;
441         else
442         {
443             gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
444             gw_log(GW_LOG_WARN, ZASS_TYPE, "Type was '%d'", (**p)->which);
445         }
446         align = External_GetEncodingAligned(ext);
447         if (!align)
448         {
449             gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't octet-aligned");
450             align = External_GetEncodingSingle(ext);
451             if (!align)
452             {
453                 gw_log(GW_LOG_WARN, ZASS_TYPE, "Record wasn't ANY");
454                 return;
455             }
456             align = align->ptr.child;
457         }
458         if (!((**p)->record = malloc(align->count + 1)))
459         {
460             gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
461             return;
462         }
463         memcpy((**p)->record, align->ptr.data, align->count);
464         (**p)->record[align->count] = '\0';
465         gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
466             align->count);
467
468         (*p) = &(**p)->next;
469     }
470 }
471
472 static void zass_records_free(zass_record *p)
473 {
474 }
475
476 /*
477  * Note that 1== first record.
478  */
479 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
480     int num, int *complete)
481 {
482     static struct zass_presentent r = {0, 0, 0, 0};
483     zass_record **rec = &r.records;
484     DATA_DIR *pdu;
485     int len;
486
487     r.num = 0;
488     if (r.records)
489     {
490         zass_records_free(r.records);
491         r.records = 0;
492     }
493     do
494     {
495         gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num - r.num,
496             start);
497         pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num - r.num, "F",
498             USMARC_OID);
499         if (!pdu)
500         {
501             gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
502             return 0;
503         }
504         zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
505             a->maxrecordsize);
506         if (len <= 0)
507         {
508             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
509             return 0;
510         }
511         PresentRequest_Destroy(pdu);
512         if (netbox_SendBuffer(a->ass, a->buf, len) != len)
513         {
514             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
515             return 0;
516         }
517         gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
518         if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
519             a->maxrecordsize)) <= 0)
520         {
521             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
522             return 0;
523         }
524         pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
525         if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
526         {
527             gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
528             return 0;
529         }
530         gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
531         r.num += PresentResponse_GetNumberOfRecordsReturned(pdu);
532         r.presentstatus = PresentResponse_GetPresentStatus(pdu);
533         if (r.num == 0)
534         {
535             gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
536             return 0;
537         }
538         r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
539         start = r.nextpos;
540         switch(PresentResponse_GetRecordType(pdu))
541         {
542             case RESPONSERECORDS_TAG:
543                 get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
544                 break;
545             case NONSURROGATEDIAGNOSTIC_TAG:
546                 get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
547                 break;
548             default:
549                 gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
550         }
551         PresentResponse_Destroy(pdu);
552     }
553     while (num - r.num && start);
554     *rec = 0;
555         
556     return &r;
557 }