Handle CQL diagnostics better. Use present always for SRW-to-Z.
[yazpp-moved-to-github.git] / src / yaz-proxy-config.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy-config.cpp,v 1.19 2004-01-06 21:17:42 adam Exp $
6  */
7
8 #include <ctype.h>
9 #include <yaz/log.h>
10 #include <yaz++/proxy.h>
11
12 Yaz_ProxyConfig::Yaz_ProxyConfig()
13 {
14     m_copy = 0;
15 #if HAVE_XSLT
16     m_docPtr = 0;
17     m_proxyPtr = 0;
18 #endif
19 }
20
21 Yaz_ProxyConfig::~Yaz_ProxyConfig()
22 {
23 #if HAVE_XSLT
24     if (!m_copy && m_docPtr)
25         xmlFreeDoc(m_docPtr);
26 #endif
27 }
28
29 int Yaz_ProxyConfig::read_xml(const char *fname)
30 {
31 #if HAVE_XSLT
32     xmlDocPtr ndoc = xmlParseFile(fname);
33
34     if (!ndoc)
35     {
36         yaz_log(LOG_WARN, "Config file %s not found or parse error", fname);
37         return -1;  // no good
38     }
39     xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
40     if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
41         strcmp((const char *) proxyPtr->name, "proxy"))
42     {
43         yaz_log(LOG_WARN, "No proxy element in %s", fname);
44         xmlFreeDoc(ndoc);
45         return -1;
46     }
47     m_proxyPtr = proxyPtr;
48
49     // OK: release previous and make it the current one.
50     if (m_docPtr)
51         xmlFreeDoc(m_docPtr);
52     m_docPtr = ndoc;
53     return 0;
54 #else
55     return -2;
56 #endif
57 }
58
59 #if HAVE_XSLT
60 const char *Yaz_ProxyConfig::get_text(xmlNodePtr ptr)
61 {
62     for(ptr = ptr->children; ptr; ptr = ptr->next)
63         if (ptr->type == XML_TEXT_NODE)
64         {
65             xmlChar *t = ptr->content;
66             if (t)
67             {
68                 while (*t == ' ')
69                     t++;
70                 return (const char *) t;
71             }
72         }
73     return 0;
74 }
75 #endif
76
77 #if HAVE_XSLT
78 void Yaz_ProxyConfig::return_limit(xmlNodePtr ptr,
79                                    int *limit_bw,
80                                    int *limit_pdu,
81                                    int *limit_req)
82 {
83     for (ptr = ptr->children; ptr; ptr = ptr->next)
84     {
85         if (ptr->type == XML_ELEMENT_NODE 
86             && !strcmp((const char *) ptr->name, "bandwidth"))
87         {
88             const char *t = get_text(ptr);
89             if (t)
90                 *limit_bw = atoi(t);
91         }
92         if (ptr->type == XML_ELEMENT_NODE 
93             && !strcmp((const char *) ptr->name, "retrieve"))
94         {
95             const char *t = get_text(ptr);
96             if (t)
97                 *limit_req = atoi(t);
98         }
99         if (ptr->type == XML_ELEMENT_NODE 
100             && !strcmp((const char *) ptr->name, "pdu"))
101         {
102             const char *t = get_text(ptr);
103             if (t)
104                 *limit_pdu = atoi(t);
105         }
106     }
107 }
108 #endif
109
110 #if HAVE_XSLT
111 void Yaz_ProxyConfig::return_target_info(xmlNodePtr ptr,
112                                          const char **url,
113                                          int *limit_bw,
114                                          int *limit_pdu,
115                                          int *limit_req,
116                                          int *target_idletime,
117                                          int *client_idletime,
118                                          int *keepalive_limit_bw,
119                                          int *keepalive_limit_pdu,
120                                          int *pre_init,
121                                          const char **cql2rpn)
122 {
123     *pre_init = 0;
124     int no_url = 0;
125     ptr = ptr->children;
126     for (; ptr; ptr = ptr->next)
127     {
128         if (ptr->type == XML_ELEMENT_NODE 
129             && !strcmp((const char *) ptr->name, "preinit"))
130         {
131             const char *v = get_text(ptr);
132             *pre_init = v ? atoi(v) : 1;
133         }
134         if (ptr->type == XML_ELEMENT_NODE 
135             && !strcmp((const char *) ptr->name, "url"))
136         {
137             const char *t = get_text(ptr);
138             if (t && no_url < MAX_ZURL_PLEX)
139             {
140                 url[no_url++] = t;
141                 url[no_url] = 0;
142             }
143         }
144         if (ptr->type == XML_ELEMENT_NODE 
145             && !strcmp((const char *) ptr->name, "keepalive"))
146         {
147             int dummy;
148             *keepalive_limit_bw = 500000;
149             *keepalive_limit_pdu = 1000;
150             return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
151                          &dummy);
152         }
153         if (ptr->type == XML_ELEMENT_NODE 
154             && !strcmp((const char *) ptr->name, "limit"))
155             return_limit(ptr, limit_bw, limit_pdu, limit_req);
156         if (ptr->type == XML_ELEMENT_NODE 
157             && !strcmp((const char *) ptr->name, "target-timeout"))
158         {
159             const char *t = get_text(ptr);
160             if (t)
161             {
162                 *target_idletime = atoi(t);
163                 if (*target_idletime < 0)
164                     *target_idletime = 0;
165             }
166         }
167         if (ptr->type == XML_ELEMENT_NODE 
168             && !strcmp((const char *) ptr->name, "client-timeout"))
169         {
170             const char *t = get_text(ptr);
171             if (t)
172             {
173                 *client_idletime = atoi(t);
174                 if (*client_idletime < 0)
175                     *client_idletime = 0;
176             }
177         }
178         if (ptr->type == XML_ELEMENT_NODE 
179             && !strcmp((const char *) ptr->name, "cql2rpn"))
180         {
181             const char *t = get_text(ptr);
182             if (t)
183                 *cql2rpn = t;
184         }
185     }
186 }
187 #endif
188
189 int Yaz_ProxyConfig::atoi_l(const char **cp)
190 {
191     int v = 0;
192     while (**cp && isdigit(**cp))
193     {
194         v = v*10 + (**cp - '0');
195         (*cp)++;
196     }
197     return v;
198 }
199
200 int Yaz_ProxyConfig::match_list(int v, const char *m)
201 {
202   while(m && *m)
203   {
204       while(*m && isspace(*m))
205           m++;
206       if (*m == '*')
207           return 1;
208       int l = atoi_l(&m);
209       int h = l;
210       if (*m == '-')
211       {
212           ++m;
213           h = atoi_l(&m);
214       }
215       if (v >= l && v <= h)
216           return 1;
217       if (*m == ',')
218           m++;
219   }
220   return 0;
221 }
222
223 #if HAVE_XSLT
224 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
225                                              Z_AttributeList *attrs,
226                                              char **addinfo)
227 {
228     int i;
229     for (i = 0; i<attrs->num_attributes; i++)
230     {
231         Z_AttributeElement *el = attrs->attributes[i];
232         
233         if (!el->attributeType)
234             continue;
235         int type = *el->attributeType;
236         int *value = 0;
237         
238         if (el->which == Z_AttributeValue_numeric && el->value.numeric)
239             value = el->value.numeric;
240         
241         xmlNodePtr ptr;
242         for(ptr = ptrl->children; ptr; ptr = ptr->next)
243         {
244             if (ptr->type == XML_ELEMENT_NODE &&
245                 !strcmp((const char *) ptr->name, "attribute"))
246             {
247                 const char *match_type = 0;
248                 const char *match_value = 0;
249                 const char *match_error = 0;
250                 struct _xmlAttr *attr;
251                 for (attr = ptr->properties; attr; attr = attr->next)
252                 {
253                     if (!strcmp((const char *) attr->name, "type") &&
254                         attr->children && attr->children->type == XML_TEXT_NODE)
255                         match_type = (const char *) attr->children->content;
256                     if (!strcmp((const char *) attr->name, "value") &&
257                         attr->children && attr->children->type == XML_TEXT_NODE)
258                         match_value = (const char *) attr->children->content;
259                     if (!strcmp((const char *) attr->name, "error") &&
260                         attr->children && attr->children->type == XML_TEXT_NODE)
261                         match_error = (const char *) attr->children->content;
262                 }
263                 if (match_type && match_value)
264                 {
265                     char addinfo_str[20];
266                     if (!match_list(type, match_type))
267                         continue;
268                     
269                     *addinfo_str = '\0';
270                     if (!strcmp(match_type, "*"))
271                         sprintf (addinfo_str, "%d", type);
272                     else if (value)
273                     {
274                         if (!match_list(*value, match_value))
275                             continue;
276                         sprintf (addinfo_str, "%d", *value);
277                     }
278                     else
279                         continue;
280                     
281                     if (match_error)
282                     {
283                         if (*addinfo_str)
284                             *addinfo = odr_strdup(odr, addinfo_str);
285                         return atoi(match_error);
286                     }
287                     break;
288                 }
289             }
290         }
291     }
292     return 0;
293 }
294 #endif
295
296 #if HAVE_XSLT
297 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
298                                             Z_RPNStructure *q,
299                                             char **addinfo)
300 {
301     if (q->which == Z_RPNStructure_complex)
302     {
303         int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
304         if (e)
305             return e;
306         e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
307         return e;
308     }
309     else if (q->which == Z_RPNStructure_simple)
310     {
311         if (q->u.simple->which == Z_Operand_APT)
312         {
313             return check_type_1_attributes(
314                 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
315                 addinfo);
316         }
317     }
318     return 0;
319 }
320 #endif
321
322 #if HAVE_XSLT
323 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
324                                   char **addinfo)
325 {
326     // possibly check for Bib-1
327     return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
328 }
329 #endif
330
331 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
332                                  char **addinfo)
333 {
334 #if HAVE_XSLT
335     xmlNodePtr ptr;
336     
337     ptr = find_target_node(name, 0);
338     if (ptr)
339     {
340         if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
341             return check_type_1(odr, ptr, query->u.type_1, addinfo);
342     }
343 #endif
344     return 0;
345 }
346
347 #if HAVE_XSLT
348 int Yaz_ProxyConfig::check_esn(xmlNodePtr ptr, Z_RecordComposition *comp)
349 {
350     char *esn = 0;
351     int default_match = 1;
352     if (comp && comp->which == Z_RecordComp_simple &&
353         comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
354     {
355         esn = comp->u.simple->u.generic;
356     }
357     if (!esn)
358         return 1;
359     for (; ptr; ptr = ptr->next)
360     {
361         if (ptr->type == XML_TEXT_NODE)
362         {
363             default_match = 0;
364             xmlChar *t = ptr->content;
365             while (*t)
366             {
367                 while (*t && isspace(*t))
368                     t++;
369                 xmlChar *s = t;
370                 int i = 0;
371                 while (esn[i] && esn[i] == *s)
372                 {
373                     i++;
374                     s++;
375                 }
376                 if (!esn[i] &&  (!*s || isspace(*s)))
377                     return 1;
378                 while (*s && !isspace(*s))
379                     s++;
380                 t = s;
381             }
382         }
383     }
384     return default_match;
385 }
386 #endif
387
388 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
389                                   Odr_oid *syntax, Z_RecordComposition *comp,
390                                   char **addinfo,
391                                   char **stylesheet)
392 {
393     if (stylesheet)
394     {
395         xfree (*stylesheet);
396         *stylesheet = 0;
397     }
398 #if HAVE_XSLT
399     int syntax_has_matched = 0;
400     xmlNodePtr ptr;
401     
402     ptr = find_target_node(name, 0);
403     if (!ptr)
404         return 0;
405     for(ptr = ptr->children; ptr; ptr = ptr->next)
406     {
407         if (ptr->type == XML_ELEMENT_NODE &&
408             !strcmp((const char *) ptr->name, "syntax"))
409         {
410             int match = 0;  // if we match record syntax
411             const char *match_type = 0;
412             const char *match_error = 0;
413             const char *match_marcxml = 0;
414             const char *match_stylesheet = 0;
415             struct _xmlAttr *attr;
416             for (attr = ptr->properties; attr; attr = attr->next)
417             {
418                 if (!strcmp((const char *) attr->name, "type") &&
419                     attr->children && attr->children->type == XML_TEXT_NODE)
420                     match_type = (const char *) attr->children->content;
421                 if (!strcmp((const char *) attr->name, "error") &&
422                     attr->children && attr->children->type == XML_TEXT_NODE)
423                     match_error = (const char *) attr->children->content;
424                 if (!strcmp((const char *) attr->name, "marcxml") &&
425                     attr->children && attr->children->type == XML_TEXT_NODE)
426                     match_marcxml = (const char *) attr->children->content;
427                 if (!strcmp((const char *) attr->name, "stylesheet") &&
428                     attr->children && attr->children->type == XML_TEXT_NODE)
429                     match_stylesheet = (const char *) attr->children->content;
430             }
431             if (match_type)
432             {
433                 if (!strcmp(match_type, "*"))
434                     match = 1;
435                 else if (!strcmp(match_type, "none"))
436                 {
437                     if (syntax == 0)
438                         match = 1;
439                 }
440                 else if (syntax)
441                 {
442                     int match_oid[OID_SIZE];
443                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
444                     if (oid_oidcmp(match_oid, syntax) == 0)
445                         match = 1;
446                 }
447             }
448             if (match)
449             {
450                 syntax_has_matched = 1;
451                 match = check_esn(ptr->children, comp);
452             }
453             if (match)
454             {
455                 if (stylesheet && match_stylesheet)
456                 {
457                     xfree(*stylesheet);
458                     *stylesheet = xstrdup(match_stylesheet);
459                 }
460                 if (match_marcxml)
461                 {
462                     return -1;
463                 }
464                 if (match_error)
465                 {
466                     if (syntax_has_matched)  // if syntax did match, schema/ESN was bad
467                         return 25;
468                     if (syntax)
469                     {
470                         char dotoid_str[100];
471                         oid_to_dotstring(syntax, dotoid_str);
472                         *addinfo = odr_strdup(odr, dotoid_str);
473                     }
474                     return atoi(match_error);
475                 }
476                 return 0;
477             }
478         }
479     }
480 #endif
481     return 0;
482 }
483
484 #if HAVE_XSLT
485 xmlNodePtr Yaz_ProxyConfig::find_target_db(xmlNodePtr ptr, const char *db)
486 {
487     xmlNodePtr dptr;
488     if (!db)
489         return ptr;
490     if (!ptr)
491         return 0;
492     for (dptr = ptr->children; dptr; dptr = dptr->next)
493         if (dptr->type == XML_ELEMENT_NODE &&
494             !strcmp((const char *) dptr->name, "database"))
495         {
496             struct _xmlAttr *attr;
497             for (attr = dptr->properties; attr; attr = attr->next)
498                 if (!strcmp((const char *) attr->name, "name"))
499                 {
500                     if (attr->children
501                         && attr->children->type==XML_TEXT_NODE
502                         && attr->children->content 
503                         && (!strcmp((const char *) attr->children->content, db)
504                             || !strcmp((const char *) attr->children->content,
505                                        "*")))
506                         return dptr;
507                 }
508         }
509     return ptr;
510 }
511     
512 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name, const char *db)
513 {
514     xmlNodePtr ptr;
515     if (!m_proxyPtr)
516         return 0;
517     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
518     {
519         if (ptr->type == XML_ELEMENT_NODE &&
520             !strcmp((const char *) ptr->name, "target"))
521         {
522             // default one ? 
523             if (!name)
524             {
525                 // <target default="1"> ?
526                 struct _xmlAttr *attr;
527                 for (attr = ptr->properties; attr; attr = attr->next)
528                     if (!strcmp((const char *) attr->name, "default") &&
529                         attr->children && attr->children->type == XML_TEXT_NODE)
530                     {
531                         xmlChar *t = attr->children->content;
532                         if (!t || *t == '1')
533                         {
534                             return find_target_db(ptr, db);
535                         }
536                     }
537             }
538             else
539             {
540                 // <target name="name"> ?
541                 struct _xmlAttr *attr;
542                 for (attr = ptr->properties; attr; attr = attr->next)
543                     if (!strcmp((const char *) attr->name, "name"))
544                     {
545                         if (attr->children
546                             && attr->children->type==XML_TEXT_NODE
547                             && attr->children->content 
548                             && (!strcmp((const char *) attr->children->content,
549                                         name)
550                                 || !strcmp((const char *) attr->children->content,
551                                            "*")))
552                         {
553                             return find_target_db(ptr, db);
554                         }
555                     }
556             }
557         }
558     }
559     return 0;
560 }
561 #endif
562
563 int Yaz_ProxyConfig::get_target_no(int no,
564                                    const char **name,
565                                    const char **url,
566                                    int *limit_bw,
567                                    int *limit_pdu,
568                                    int *limit_req,
569                                    int *target_idletime,
570                                    int *client_idletime,
571                                    int *max_clients,
572                                    int *keepalive_limit_bw,
573                                    int *keepalive_limit_pdu,
574                                    int *pre_init,
575                                    const char **cql2rpn)
576 {
577 #if HAVE_XSLT
578     xmlNodePtr ptr;
579     if (!m_proxyPtr)
580         return 0;
581     int i = 0;
582     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
583         if (ptr->type == XML_ELEMENT_NODE &&
584             !strcmp((const char *) ptr->name, "target"))
585         {
586             if (i == no)
587             {
588                 struct _xmlAttr *attr;
589                 for (attr = ptr->properties; attr; attr = attr->next)
590                     if (!strcmp((const char *) attr->name, "name"))
591                     {
592                         if (attr->children
593                             && attr->children->type==XML_TEXT_NODE
594                             && attr->children->content)
595                             *name = (const char *) attr->children->content;
596                     }
597                 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
598                                    target_idletime, client_idletime,
599                                    keepalive_limit_bw, keepalive_limit_pdu,
600                                    pre_init, cql2rpn);
601                 return 1;
602             }
603             i++;
604         }
605 #endif
606     return 0;
607 }
608
609 int Yaz_ProxyConfig::mycmp(const char *hay, const char *item, size_t len)
610 {
611     if (len == strlen(item) && memcmp(hay, item, len) == 0)
612         return 1;
613     return 0;
614 }
615
616 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
617                                        int *max_clients)
618 {
619 #if HAVE_XSLT
620     xmlNodePtr ptr;
621     if (!m_proxyPtr)
622         return;
623     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
624     {
625         if (ptr->type == XML_ELEMENT_NODE 
626             && !strcmp((const char *) ptr->name, "log"))
627         {
628             const char *v = get_text(ptr);
629             *log_mask = 0;
630             while (v && *v)
631             {
632                 const char *cp = v;
633                 while (*cp && *cp != ',' && !isspace(*cp))
634                     cp++;
635                 size_t len = cp - v;
636                 if (mycmp(v, "client-apdu", len))
637                     *log_mask |= PROXY_LOG_APDU_CLIENT;
638                 if (mycmp(v, "server-apdu", len))
639                     *log_mask |= PROXY_LOG_APDU_SERVER;
640                 if (mycmp(v, "client-requests", len))
641                     *log_mask |= PROXY_LOG_REQ_CLIENT;
642                 if (mycmp(v, "server-requests", len))
643                     *log_mask |= PROXY_LOG_REQ_SERVER;
644                 if (isdigit(*v))
645                     *log_mask |= atoi(v);
646                 if (*cp == ',')
647                     cp++;
648                 while (*cp && isspace(*cp))
649                     cp++;
650                 v = cp;
651             }
652         }
653         if (ptr->type == XML_ELEMENT_NODE &&
654             !strcmp((const char *) ptr->name, "max-clients"))
655         {
656             const char *t = get_text(ptr);
657             if (t)
658             {
659                 *max_clients = atoi(t);
660                 if (*max_clients  < 1)
661                     *max_clients = 1;
662             }
663         }
664     }
665 #endif
666 }
667
668 char *Yaz_ProxyConfig::get_explain(ODR odr, const char *name, const char *db,
669                                    int *len)
670 {
671 #if HAVE_XSLT
672     xmlNodePtr ptr = find_target_node(name, db);
673     if (ptr)
674     {
675         ptr = ptr->children;
676         for (; ptr; ptr = ptr->next)
677             if (ptr->type == XML_ELEMENT_NODE &&
678                 !strcmp((const char *) ptr->name, "explain"))
679             {
680                 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
681
682                 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
683                 
684                 xmlDocSetRootElement(doc, ptr2);
685                 
686                 xmlChar *buf_out;
687                 int len_out;
688                 xmlDocDumpMemory(doc, &buf_out, len);
689                 char *content = (char*) odr_malloc(odr, *len);
690                 memcpy(content, buf_out, *len);
691                 
692                 xmlFree(buf_out);
693                 xmlFreeDoc(doc);
694                 return content;
695             }
696     }
697     else
698         yaz_log(LOG_WARN, "No explain node 1");
699
700 #endif
701     yaz_log(LOG_WARN, "No explain node");
702     return 0;
703 }
704
705 void Yaz_ProxyConfig::get_target_info(const char *name,
706                                       const char **url,
707                                       int *limit_bw,
708                                       int *limit_pdu,
709                                       int *limit_req,
710                                       int *target_idletime,
711                                       int *client_idletime,
712                                       int *max_clients,
713                                       int *keepalive_limit_bw,
714                                       int *keepalive_limit_pdu,
715                                       int *pre_init,
716                                       const char **cql2rpn)
717 {
718 #if HAVE_XSLT
719     xmlNodePtr ptr;
720     if (!m_proxyPtr)
721     {
722         url[0] = name;
723         url[1] = 0;
724         return;
725     }
726     url[0] = 0;
727     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
728     {
729         if (ptr->type == XML_ELEMENT_NODE &&
730             !strcmp((const char *) ptr->name, "max-clients"))
731         {
732             const char *t = get_text(ptr);
733             if (t)
734             {
735                 *max_clients = atoi(t);
736                 if (*max_clients  < 1)
737                     *max_clients = 1;
738             }
739         }
740     }
741     ptr = find_target_node(name, 0);
742     if (ptr)
743     {
744         if (name)
745         {
746             url[0] = name;
747             url[1] = 0;
748         }
749         return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
750                            target_idletime, client_idletime,
751                            keepalive_limit_bw, keepalive_limit_pdu,
752                            pre_init, cql2rpn);
753     }
754 #else
755     *url = name;
756     return;
757 #endif
758 }
759
760