fbcc88d6b3a091c938556e32d10cae1cfe478dd1
[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.18 2004-01-05 09:31:09 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     xmlNodePtr ptr;
400     
401     ptr = find_target_node(name, 0);
402     if (!ptr)
403         return 0;
404     for(ptr = ptr->children; ptr; ptr = ptr->next)
405     {
406         if (ptr->type == XML_ELEMENT_NODE &&
407             !strcmp((const char *) ptr->name, "syntax"))
408         {
409             int match = 0;  // if we match record syntax
410             const char *match_type = 0;
411             const char *match_error = 0;
412             const char *match_marcxml = 0;
413             const char *match_stylesheet = 0;
414             struct _xmlAttr *attr;
415             for (attr = ptr->properties; attr; attr = attr->next)
416             {
417                 if (!strcmp((const char *) attr->name, "type") &&
418                     attr->children && attr->children->type == XML_TEXT_NODE)
419                     match_type = (const char *) attr->children->content;
420                 if (!strcmp((const char *) attr->name, "error") &&
421                     attr->children && attr->children->type == XML_TEXT_NODE)
422                     match_error = (const char *) attr->children->content;
423                 if (!strcmp((const char *) attr->name, "marcxml") &&
424                     attr->children && attr->children->type == XML_TEXT_NODE)
425                     match_marcxml = (const char *) attr->children->content;
426                 if (!strcmp((const char *) attr->name, "stylesheet") &&
427                     attr->children && attr->children->type == XML_TEXT_NODE)
428                     match_stylesheet = (const char *) attr->children->content;
429             }
430             if (match_type)
431             {
432                 if (!strcmp(match_type, "*"))
433                     match = 1;
434                 else if (!strcmp(match_type, "none"))
435                 {
436                     if (syntax == 0)
437                         match = 1;
438                 }
439                 else if (syntax)
440                 {
441                     int match_oid[OID_SIZE];
442                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
443                     if (oid_oidcmp(match_oid, syntax) == 0)
444                         match = 1;
445                 }
446             }
447             if (match)
448                 match = check_esn(ptr->children, comp);
449
450             if (match)
451             {
452                 if (stylesheet && match_stylesheet)
453                 {
454                     xfree(*stylesheet);
455                     *stylesheet = xstrdup(match_stylesheet);
456                 }
457                 if (match_marcxml)
458                 {
459                     return -1;
460                 }
461                 if (match_error)
462                 {
463                     if (syntax)
464                     {
465                         char dotoid_str[100];
466                         oid_to_dotstring(syntax, dotoid_str);
467                         *addinfo = odr_strdup(odr, dotoid_str);
468                     }
469                     return atoi(match_error);
470                 }
471                 return 0;
472             }
473         }
474     }
475 #endif
476     return 0;
477 }
478
479 #if HAVE_XSLT
480 xmlNodePtr Yaz_ProxyConfig::find_target_db(xmlNodePtr ptr, const char *db)
481 {
482     xmlNodePtr dptr;
483     if (!db)
484         return ptr;
485     if (!ptr)
486         return 0;
487     for (dptr = ptr->children; dptr; dptr = dptr->next)
488         if (dptr->type == XML_ELEMENT_NODE &&
489             !strcmp((const char *) dptr->name, "database"))
490         {
491             struct _xmlAttr *attr;
492             for (attr = dptr->properties; attr; attr = attr->next)
493                 if (!strcmp((const char *) attr->name, "name"))
494                 {
495                     if (attr->children
496                         && attr->children->type==XML_TEXT_NODE
497                         && attr->children->content 
498                         && (!strcmp((const char *) attr->children->content, db)
499                             || !strcmp((const char *) attr->children->content,
500                                        "*")))
501                         return dptr;
502                 }
503         }
504     return ptr;
505 }
506     
507 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name, const char *db)
508 {
509     xmlNodePtr ptr;
510     if (!m_proxyPtr)
511         return 0;
512     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
513     {
514         if (ptr->type == XML_ELEMENT_NODE &&
515             !strcmp((const char *) ptr->name, "target"))
516         {
517             // default one ? 
518             if (!name)
519             {
520                 // <target default="1"> ?
521                 struct _xmlAttr *attr;
522                 for (attr = ptr->properties; attr; attr = attr->next)
523                     if (!strcmp((const char *) attr->name, "default") &&
524                         attr->children && attr->children->type == XML_TEXT_NODE)
525                     {
526                         xmlChar *t = attr->children->content;
527                         if (!t || *t == '1')
528                         {
529                             return find_target_db(ptr, db);
530                         }
531                     }
532             }
533             else
534             {
535                 // <target name="name"> ?
536                 struct _xmlAttr *attr;
537                 for (attr = ptr->properties; attr; attr = attr->next)
538                     if (!strcmp((const char *) attr->name, "name"))
539                     {
540                         if (attr->children
541                             && attr->children->type==XML_TEXT_NODE
542                             && attr->children->content 
543                             && (!strcmp((const char *) attr->children->content,
544                                         name)
545                                 || !strcmp((const char *) attr->children->content,
546                                            "*")))
547                         {
548                             return find_target_db(ptr, db);
549                         }
550                     }
551             }
552         }
553     }
554     return 0;
555 }
556 #endif
557
558 int Yaz_ProxyConfig::get_target_no(int no,
559                                    const char **name,
560                                    const char **url,
561                                    int *limit_bw,
562                                    int *limit_pdu,
563                                    int *limit_req,
564                                    int *target_idletime,
565                                    int *client_idletime,
566                                    int *max_clients,
567                                    int *keepalive_limit_bw,
568                                    int *keepalive_limit_pdu,
569                                    int *pre_init,
570                                    const char **cql2rpn)
571 {
572 #if HAVE_XSLT
573     xmlNodePtr ptr;
574     if (!m_proxyPtr)
575         return 0;
576     int i = 0;
577     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
578         if (ptr->type == XML_ELEMENT_NODE &&
579             !strcmp((const char *) ptr->name, "target"))
580         {
581             if (i == no)
582             {
583                 struct _xmlAttr *attr;
584                 for (attr = ptr->properties; attr; attr = attr->next)
585                     if (!strcmp((const char *) attr->name, "name"))
586                     {
587                         if (attr->children
588                             && attr->children->type==XML_TEXT_NODE
589                             && attr->children->content)
590                             *name = (const char *) attr->children->content;
591                     }
592                 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
593                                    target_idletime, client_idletime,
594                                    keepalive_limit_bw, keepalive_limit_pdu,
595                                    pre_init, cql2rpn);
596                 return 1;
597             }
598             i++;
599         }
600 #endif
601     return 0;
602 }
603
604 int Yaz_ProxyConfig::mycmp(const char *hay, const char *item, size_t len)
605 {
606     if (len == strlen(item) && memcmp(hay, item, len) == 0)
607         return 1;
608     return 0;
609 }
610
611 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
612                                        int *max_clients)
613 {
614 #if HAVE_XSLT
615     xmlNodePtr ptr;
616     if (!m_proxyPtr)
617         return;
618     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
619     {
620         if (ptr->type == XML_ELEMENT_NODE 
621             && !strcmp((const char *) ptr->name, "log"))
622         {
623             const char *v = get_text(ptr);
624             *log_mask = 0;
625             while (v && *v)
626             {
627                 const char *cp = v;
628                 while (*cp && *cp != ',' && !isspace(*cp))
629                     cp++;
630                 size_t len = cp - v;
631                 if (mycmp(v, "client-apdu", len))
632                     *log_mask |= PROXY_LOG_APDU_CLIENT;
633                 if (mycmp(v, "server-apdu", len))
634                     *log_mask |= PROXY_LOG_APDU_SERVER;
635                 if (mycmp(v, "client-requests", len))
636                     *log_mask |= PROXY_LOG_REQ_CLIENT;
637                 if (mycmp(v, "server-requests", len))
638                     *log_mask |= PROXY_LOG_REQ_SERVER;
639                 if (isdigit(*v))
640                     *log_mask |= atoi(v);
641                 if (*cp == ',')
642                     cp++;
643                 while (*cp && isspace(*cp))
644                     cp++;
645                 v = cp;
646             }
647         }
648         if (ptr->type == XML_ELEMENT_NODE &&
649             !strcmp((const char *) ptr->name, "max-clients"))
650         {
651             const char *t = get_text(ptr);
652             if (t)
653             {
654                 *max_clients = atoi(t);
655                 if (*max_clients  < 1)
656                     *max_clients = 1;
657             }
658         }
659     }
660 #endif
661 }
662
663 char *Yaz_ProxyConfig::get_explain(ODR odr, const char *name, const char *db,
664                                    int *len)
665 {
666 #if HAVE_XSLT
667     xmlNodePtr ptr = find_target_node(name, db);
668     if (ptr)
669     {
670         ptr = ptr->children;
671         for (; ptr; ptr = ptr->next)
672             if (ptr->type == XML_ELEMENT_NODE &&
673                 !strcmp((const char *) ptr->name, "explain"))
674             {
675                 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
676
677                 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
678                 
679                 xmlDocSetRootElement(doc, ptr2);
680                 
681                 xmlChar *buf_out;
682                 int len_out;
683                 xmlDocDumpMemory(doc, &buf_out, len);
684                 char *content = (char*) odr_malloc(odr, *len);
685                 memcpy(content, buf_out, *len);
686                 
687                 xmlFree(buf_out);
688                 xmlFreeDoc(doc);
689                 return content;
690             }
691     }
692     else
693         yaz_log(LOG_WARN, "No explain node 1");
694
695 #endif
696     yaz_log(LOG_WARN, "No explain node");
697     return 0;
698 }
699
700 void Yaz_ProxyConfig::get_target_info(const char *name,
701                                       const char **url,
702                                       int *limit_bw,
703                                       int *limit_pdu,
704                                       int *limit_req,
705                                       int *target_idletime,
706                                       int *client_idletime,
707                                       int *max_clients,
708                                       int *keepalive_limit_bw,
709                                       int *keepalive_limit_pdu,
710                                       int *pre_init,
711                                       const char **cql2rpn)
712 {
713 #if HAVE_XSLT
714     xmlNodePtr ptr;
715     if (!m_proxyPtr)
716     {
717         url[0] = name;
718         url[1] = 0;
719         return;
720     }
721     url[0] = 0;
722     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
723     {
724         if (ptr->type == XML_ELEMENT_NODE &&
725             !strcmp((const char *) ptr->name, "max-clients"))
726         {
727             const char *t = get_text(ptr);
728             if (t)
729             {
730                 *max_clients = atoi(t);
731                 if (*max_clients  < 1)
732                     *max_clients = 1;
733             }
734         }
735     }
736     ptr = find_target_node(name, 0);
737     if (ptr)
738     {
739         if (name)
740         {
741             url[0] = name;
742             url[1] = 0;
743         }
744         return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
745                            target_idletime, client_idletime,
746                            keepalive_limit_bw, keepalive_limit_pdu,
747                            pre_init, cql2rpn);
748     }
749 #else
750     *url = name;
751     return;
752 #endif
753 }
754
755