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