Fixed bug #189: Bad authenticated session transfer.
[yazproxy-moved-to-github.git] / src / yaz-proxy-config.cpp
1 /* $Id: yaz-proxy-config.cpp,v 1.8 2004-10-23 23:12:24 adam Exp $
2    Copyright (c) 1998-2004, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include <ctype.h>
23 #include <yaz/log.h>
24 #include <yazproxy/proxy.h>
25
26 #if HAVE_XSLT
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29 #include <libxslt/xsltutils.h>
30 #include <libxslt/transform.h>
31 #endif
32
33 class Yaz_ProxyConfigP {
34     friend class Yaz_ProxyConfig;
35
36     int m_copy;
37
38     int mycmp(const char *hay, const char *item, size_t len);
39     int match_list(int v, const char *m);
40     int atoi_l(const char **cp);
41 #if HAVE_XSLT
42     int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
43                      const char *schema_identifier);
44     xmlDocPtr m_docPtr;
45     xmlNodePtr m_proxyPtr;
46     void return_target_info(xmlNodePtr ptr, const char **url,
47                             int *limit_bw, int *limit_pdu, int *limit_req,
48                             int *target_idletime, int *client_idletime,
49                             int *keepalive_limit_bw, int *keepalive_limit_pdu,
50                             int *pre_init, const char **cql2rpn,
51                             const char **authentication);
52     void return_limit(xmlNodePtr ptr,
53                       int *limit_bw, int *limit_pdu, int *limit_req);
54     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
55                      char **addinfo);
56     xmlNodePtr find_target_node(const char *name, const char *db);
57     xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
58     const char *get_text(xmlNodePtr ptr);
59     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
60                                 Z_AttributeList *attrs,
61                                 char **addinfo);
62     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
63                                char **addinfo);
64     int get_explain_ptr(const char *host, const char *db,
65                         xmlNodePtr *ptr_target, xmlNodePtr *ptr_explain);
66 #endif
67 };
68
69 Yaz_ProxyConfig::Yaz_ProxyConfig()
70 {
71     m_cp = new Yaz_ProxyConfigP;
72     m_cp->m_copy = 0;
73 #if HAVE_XSLT
74     m_cp->m_docPtr = 0;
75     m_cp->m_proxyPtr = 0;
76 #endif
77 }
78
79 Yaz_ProxyConfig::~Yaz_ProxyConfig()
80 {
81 #if HAVE_XSLT
82     if (!m_cp->m_copy && m_cp->m_docPtr)
83         xmlFreeDoc(m_cp->m_docPtr);
84 #endif
85     delete m_cp;
86 }
87
88 int Yaz_ProxyConfig::read_xml(const char *fname)
89 {
90 #if HAVE_XSLT
91     xmlDocPtr ndoc = xmlParseFile(fname);
92
93     if (!ndoc)
94     {
95         yaz_log(LOG_WARN, "Config file %s not found or parse error", fname);
96         return -1;  // no good
97     }
98     xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
99     if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
100         strcmp((const char *) proxyPtr->name, "proxy"))
101     {
102         yaz_log(LOG_WARN, "No proxy element in %s", fname);
103         xmlFreeDoc(ndoc);
104         return -1;
105     }
106     m_cp->m_proxyPtr = proxyPtr;
107
108     // OK: release previous and make it the current one.
109     if (m_cp->m_docPtr)
110         xmlFreeDoc(m_cp->m_docPtr);
111     m_cp->m_docPtr = ndoc;
112     return 0;
113 #else
114     return -2;
115 #endif
116 }
117
118 #if HAVE_XSLT
119 const char *Yaz_ProxyConfigP::get_text(xmlNodePtr ptr)
120 {
121     for(ptr = ptr->children; ptr; ptr = ptr->next)
122         if (ptr->type == XML_TEXT_NODE)
123         {
124             xmlChar *t = ptr->content;
125             if (t)
126             {
127                 while (*t == ' ')
128                     t++;
129                 return (const char *) t;
130             }
131         }
132     return 0;
133 }
134 #endif
135
136 #if HAVE_XSLT
137 void Yaz_ProxyConfigP::return_limit(xmlNodePtr ptr,
138                                    int *limit_bw,
139                                    int *limit_pdu,
140                                    int *limit_req)
141 {
142     for (ptr = ptr->children; ptr; ptr = ptr->next)
143     {
144         if (ptr->type == XML_ELEMENT_NODE 
145             && !strcmp((const char *) ptr->name, "bandwidth"))
146         {
147             const char *t = get_text(ptr);
148             if (t)
149                 *limit_bw = atoi(t);
150         }
151         if (ptr->type == XML_ELEMENT_NODE 
152             && !strcmp((const char *) ptr->name, "retrieve"))
153         {
154             const char *t = get_text(ptr);
155             if (t)
156                 *limit_req = atoi(t);
157         }
158         if (ptr->type == XML_ELEMENT_NODE 
159             && !strcmp((const char *) ptr->name, "pdu"))
160         {
161             const char *t = get_text(ptr);
162             if (t)
163                 *limit_pdu = atoi(t);
164         }
165     }
166 }
167 #endif
168
169 #if HAVE_XSLT
170 void Yaz_ProxyConfigP::return_target_info(xmlNodePtr ptr,
171                                           const char **url,
172                                           int *limit_bw,
173                                           int *limit_pdu,
174                                           int *limit_req,
175                                           int *target_idletime,
176                                           int *client_idletime,
177                                           int *keepalive_limit_bw,
178                                           int *keepalive_limit_pdu,
179                                           int *pre_init,
180                                           const char **cql2rpn,
181                                           const char **authentication)
182 {
183     *pre_init = 0;
184     int no_url = 0;
185     ptr = ptr->children;
186     for (; ptr; ptr = ptr->next)
187     {
188         if (ptr->type == XML_ELEMENT_NODE 
189             && !strcmp((const char *) ptr->name, "preinit"))
190         {
191             const char *v = get_text(ptr);
192             *pre_init = v ? atoi(v) : 1;
193         }
194         if (ptr->type == XML_ELEMENT_NODE 
195             && !strcmp((const char *) ptr->name, "url"))
196         {
197             const char *t = get_text(ptr);
198             if (t && no_url < MAX_ZURL_PLEX)
199             {
200                 url[no_url++] = t;
201                 url[no_url] = 0;
202             }
203         }
204         if (ptr->type == XML_ELEMENT_NODE 
205             && !strcmp((const char *) ptr->name, "keepalive"))
206         {
207             int dummy;
208             *keepalive_limit_bw = 500000;
209             *keepalive_limit_pdu = 1000;
210             return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
211                          &dummy);
212         }
213         if (ptr->type == XML_ELEMENT_NODE 
214             && !strcmp((const char *) ptr->name, "limit"))
215             return_limit(ptr, limit_bw, limit_pdu, limit_req);
216         if (ptr->type == XML_ELEMENT_NODE 
217             && !strcmp((const char *) ptr->name, "target-timeout"))
218         {
219             const char *t = get_text(ptr);
220             if (t)
221             {
222                 *target_idletime = atoi(t);
223                 if (*target_idletime < 0)
224                     *target_idletime = 0;
225             }
226         }
227         if (ptr->type == XML_ELEMENT_NODE 
228             && !strcmp((const char *) ptr->name, "client-timeout"))
229         {
230             const char *t = get_text(ptr);
231             if (t)
232             {
233                 *client_idletime = atoi(t);
234                 if (*client_idletime < 0)
235                     *client_idletime = 0;
236             }
237         }
238         if (ptr->type == XML_ELEMENT_NODE 
239             && !strcmp((const char *) ptr->name, "cql2rpn"))
240         {
241             const char *t = get_text(ptr);
242             if (t)
243                 *cql2rpn = t;
244         }
245         if (ptr->type == XML_ELEMENT_NODE 
246             && !strcmp((const char *) ptr->name, "authentication"))
247         {
248             const char *t = get_text(ptr);
249             if (t)
250                 *authentication = t;
251         }
252     }
253 }
254 #endif
255
256 int Yaz_ProxyConfigP::atoi_l(const char **cp)
257 {
258     int v = 0;
259     while (**cp && isdigit(**cp))
260     {
261         v = v*10 + (**cp - '0');
262         (*cp)++;
263     }
264     return v;
265 }
266
267 int Yaz_ProxyConfigP::match_list(int v, const char *m)
268 {
269     while(m && *m)
270     {
271         while(*m && isspace(*m))
272             m++;
273         if (*m == '*')
274             return 1;
275         int l = atoi_l(&m);
276         int h = l;
277         if (*m == '-')
278         {
279             ++m;
280             h = atoi_l(&m);
281         }
282         if (v >= l && v <= h)
283           return 1;
284         if (*m == ',')
285             m++;
286     }
287     return 0;
288 }
289
290 #if HAVE_XSLT
291 int Yaz_ProxyConfigP::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
292                                               Z_AttributeList *attrs,
293                                               char **addinfo)
294 {
295     int i;
296     for (i = 0; i<attrs->num_attributes; i++)
297     {
298         Z_AttributeElement *el = attrs->attributes[i];
299         
300         if (!el->attributeType)
301             continue;
302         int type = *el->attributeType;
303         int *value = 0;
304         
305         if (el->which == Z_AttributeValue_numeric && el->value.numeric)
306             value = el->value.numeric;
307         
308         xmlNodePtr ptr;
309         for(ptr = ptrl->children; ptr; ptr = ptr->next)
310         {
311             if (ptr->type == XML_ELEMENT_NODE &&
312                 !strcmp((const char *) ptr->name, "attribute"))
313             {
314                 const char *match_type = 0;
315                 const char *match_value = 0;
316                 const char *match_error = 0;
317                 struct _xmlAttr *attr;
318                 for (attr = ptr->properties; attr; attr = attr->next)
319                 {
320                     if (!strcmp((const char *) attr->name, "type") &&
321                         attr->children && attr->children->type == XML_TEXT_NODE)
322                         match_type = (const char *) attr->children->content;
323                     if (!strcmp((const char *) attr->name, "value") &&
324                         attr->children && attr->children->type == XML_TEXT_NODE)
325                         match_value = (const char *) attr->children->content;
326                     if (!strcmp((const char *) attr->name, "error") &&
327                         attr->children && attr->children->type == XML_TEXT_NODE)
328                         match_error = (const char *) attr->children->content;
329                 }
330                 if (match_type && match_value)
331                 {
332                     char addinfo_str[20];
333                     if (!match_list(type, match_type))
334                         continue;
335                     
336                     *addinfo_str = '\0';
337                     if (!strcmp(match_type, "*"))
338                         sprintf (addinfo_str, "%d", type);
339                     else if (value)
340                     {
341                         if (!match_list(*value, match_value))
342                             continue;
343                         sprintf (addinfo_str, "%d", *value);
344                     }
345                     else
346                         continue;
347                     
348                     if (match_error)
349                     {
350                         if (*addinfo_str)
351                             *addinfo = odr_strdup(odr, addinfo_str);
352                         return atoi(match_error);
353                     }
354                     break;
355                 }
356             }
357         }
358     }
359     return 0;
360 }
361 #endif
362
363 #if HAVE_XSLT
364 int Yaz_ProxyConfigP::check_type_1_structure(ODR odr, xmlNodePtr ptr,
365                                             Z_RPNStructure *q,
366                                             char **addinfo)
367 {
368     if (q->which == Z_RPNStructure_complex)
369     {
370         int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
371         if (e)
372             return e;
373         e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
374         return e;
375     }
376     else if (q->which == Z_RPNStructure_simple)
377     {
378         if (q->u.simple->which == Z_Operand_APT)
379         {
380             return check_type_1_attributes(
381                 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
382                 addinfo);
383         }
384     }
385     return 0;
386 }
387 #endif
388
389 #if HAVE_XSLT
390 int Yaz_ProxyConfigP::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
391                                    char **addinfo)
392 {
393     // possibly check for Bib-1
394     return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
395 }
396 #endif
397
398 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
399                                  char **addinfo)
400 {
401 #if HAVE_XSLT
402     xmlNodePtr ptr;
403     
404     ptr = m_cp->find_target_node(name, 0);
405     if (ptr)
406     {
407         if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
408             return m_cp->check_type_1(odr, ptr, query->u.type_1, addinfo);
409     }
410 #endif
411     return 0;
412 }
413
414 #if HAVE_XSLT
415 int Yaz_ProxyConfigP::check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
416                                    const char *schema_identifier)
417 {
418     char *esn = 0;
419     int default_match = 1;
420     if (comp && comp->which == Z_RecordComp_simple &&
421         comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
422     {
423         esn = comp->u.simple->u.generic;
424     }
425     // if no ESN/schema was given accept..
426     if (!esn)
427         return 1;
428     // check if schema identifier match
429     if (schema_identifier && !strcmp(esn, schema_identifier))
430         return 1;
431     // Check each name element
432     for (; ptr; ptr = ptr->next)
433     {
434         if (ptr->type == XML_ELEMENT_NODE 
435             && !strcmp((const char *) ptr->name, "name"))
436         {
437             xmlNodePtr tptr = ptr->children;
438             default_match = 0;
439             for (; tptr; tptr = tptr->next)
440                 if (tptr->type == XML_TEXT_NODE && tptr->content)
441                 {
442                     xmlChar *t = tptr->content;
443                     while (*t && isspace(*t))
444                         t++;
445                     int i = 0;
446                     while (esn[i] && esn[i] == t[i])
447                         i++;
448                     if (!esn[i] && (!t[i] || isspace(t[i])))
449                         return 1;
450                 }
451         }
452     }
453     return default_match;
454 }
455 #endif
456
457 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
458                                   Odr_oid *syntax, Z_RecordComposition *comp,
459                                   char **addinfo,
460                                   char **stylesheet, char **schema,
461                                   char **backend_type,
462                                   char **backend_charset)
463 {
464     if (stylesheet)
465     {
466         xfree (*stylesheet);
467         *stylesheet = 0;
468     }
469     if (schema)
470     {
471         xfree (*schema);
472         *schema = 0;
473     }
474     if (backend_type)
475     {
476         xfree (*backend_type);
477         *backend_type = 0;
478     }
479     if (backend_charset)
480     {
481         xfree (*backend_charset);
482         *backend_charset = 0;
483     }
484 #if HAVE_XSLT
485     int syntax_has_matched = 0;
486     xmlNodePtr ptr;
487     
488     ptr = m_cp->find_target_node(name, 0);
489     if (!ptr)
490         return 0;
491     for(ptr = ptr->children; ptr; ptr = ptr->next)
492     {
493         if (ptr->type == XML_ELEMENT_NODE &&
494             !strcmp((const char *) ptr->name, "syntax"))
495         {
496             int match = 0;  // if we match record syntax
497             const char *match_type = 0;
498             const char *match_error = 0;
499             const char *match_marcxml = 0;
500             const char *match_stylesheet = 0;
501             const char *match_identifier = 0;
502             const char *match_backend_type = 0;
503             const char *match_backend_charset = 0;
504             struct _xmlAttr *attr;
505             for (attr = ptr->properties; attr; attr = attr->next)
506             {
507                 if (!strcmp((const char *) attr->name, "type") &&
508                     attr->children && attr->children->type == XML_TEXT_NODE)
509                     match_type = (const char *) attr->children->content;
510                 if (!strcmp((const char *) attr->name, "error") &&
511                     attr->children && attr->children->type == XML_TEXT_NODE)
512                     match_error = (const char *) attr->children->content;
513                 if (!strcmp((const char *) attr->name, "marcxml") &&
514                     attr->children && attr->children->type == XML_TEXT_NODE)
515                     match_marcxml = (const char *) attr->children->content;
516                 if (!strcmp((const char *) attr->name, "stylesheet") &&
517                     attr->children && attr->children->type == XML_TEXT_NODE)
518                     match_stylesheet = (const char *) attr->children->content;
519                 if (!strcmp((const char *) attr->name, "identifier") &&
520                     attr->children && attr->children->type == XML_TEXT_NODE)
521                     match_identifier = (const char *) attr->children->content;
522                 if (!strcmp((const char *) attr->name, "backendtype") &&
523                     attr->children && attr->children->type == XML_TEXT_NODE)
524                     match_backend_type = (const char *)
525                         attr->children->content;
526                 if (!strcmp((const char *) attr->name, "backendcharset") &&
527                     attr->children && attr->children->type == XML_TEXT_NODE)
528                     match_backend_charset = (const char *)
529                         attr->children->content;
530             }
531             if (match_type)
532             {
533                 if (!strcmp(match_type, "*"))
534                     match = 1;
535                 else if (!strcmp(match_type, "none"))
536                 {
537                     if (syntax == 0)
538                         match = 1;
539                 }
540                 else if (syntax)
541                 {
542                     int match_oid[OID_SIZE];
543                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
544                     if (oid_oidcmp(match_oid, syntax) == 0)
545                         match = 1;
546                 }
547             }
548             if (match)
549             {
550                 if (!match_error)
551                     syntax_has_matched = 1;
552                 match = m_cp->check_schema(ptr->children, comp,
553                                            match_identifier);
554             }
555             if (match)
556             {
557                 if (stylesheet && match_stylesheet)
558                 {
559                     xfree(*stylesheet);
560                     *stylesheet = xstrdup(match_stylesheet);
561                 }
562                 if (schema && match_identifier)
563                 {
564                     xfree(*schema);
565                     *schema = xstrdup(match_identifier);
566                 }
567                 if (backend_type && match_backend_type)
568                 {
569                     xfree(*backend_type);
570                     *backend_type = xstrdup(match_backend_type);
571                 }
572                 if (backend_charset && match_backend_charset)
573                 {
574                     xfree(*backend_charset);
575                     *backend_charset = xstrdup(match_backend_charset);
576                 }
577                 if (match_marcxml)
578                 {
579                     return -1;
580                 }
581                 if (match_error)
582                 {
583                     if (syntax_has_matched)  // if syntax OK, bad schema/ESN
584                         return 25;
585                     if (syntax)
586                     {
587                         char dotoid_str[100];
588                         oid_to_dotstring(syntax, dotoid_str);
589                         *addinfo = odr_strdup(odr, dotoid_str);
590                     }
591                     return atoi(match_error);
592                 }
593                 return 0;
594             }
595         }
596     }
597 #endif
598     return 0;
599 }
600
601 #if HAVE_XSLT
602 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
603 {
604     xmlNodePtr dptr;
605     if (!db)
606         return ptr;
607     if (!ptr)
608         return 0;
609     for (dptr = ptr->children; dptr; dptr = dptr->next)
610         if (dptr->type == XML_ELEMENT_NODE &&
611             !strcmp((const char *) dptr->name, "database"))
612         {
613             struct _xmlAttr *attr;
614             for (attr = dptr->properties; attr; attr = attr->next)
615                 if (!strcmp((const char *) attr->name, "name"))
616                 {
617                     if (attr->children
618                         && attr->children->type==XML_TEXT_NODE
619                         && attr->children->content 
620                         && (!strcmp((const char *) attr->children->content, db)
621                             || !strcmp((const char *) attr->children->content,
622                                        "*")))
623                         return dptr;
624                 }
625         }
626     return ptr;
627 }
628     
629 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
630 {
631     xmlNodePtr ptr;
632     if (!m_proxyPtr)
633         return 0;
634     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
635     {
636         if (ptr->type == XML_ELEMENT_NODE &&
637             !strcmp((const char *) ptr->name, "target"))
638         {
639             // default one ? 
640             if (!name)
641             {
642                 // <target default="1"> ?
643                 struct _xmlAttr *attr;
644                 for (attr = ptr->properties; attr; attr = attr->next)
645                     if (!strcmp((const char *) attr->name, "default") &&
646                         attr->children && attr->children->type == XML_TEXT_NODE)
647                     {
648                         xmlChar *t = attr->children->content;
649                         if (!t || *t == '1')
650                         {
651                             return find_target_db(ptr, db);
652                         }
653                     }
654             }
655             else
656             {
657                 // <target name="name"> ?
658                 struct _xmlAttr *attr;
659                 for (attr = ptr->properties; attr; attr = attr->next)
660                     if (!strcmp((const char *) attr->name, "name"))
661                     {
662                         if (attr->children
663                             && attr->children->type==XML_TEXT_NODE
664                             && attr->children->content 
665                             && (!strcmp((const char *) attr->children->content,
666                                         name)
667                                 || !strcmp((const char *) attr->children->content,
668                                            "*")))
669                         {
670                             return find_target_db(ptr, db);
671                         }
672                     }
673             }
674         }
675     }
676     return 0;
677 }
678 #endif
679
680 int Yaz_ProxyConfig::get_target_no(int no,
681                                    const char **name,
682                                    const char **url,
683                                    int *limit_bw,
684                                    int *limit_pdu,
685                                    int *limit_req,
686                                    int *target_idletime,
687                                    int *client_idletime,
688                                    int *max_clients,
689                                    int *keepalive_limit_bw,
690                                    int *keepalive_limit_pdu,
691                                    int *pre_init,
692                                    const char **cql2rpn,
693                                    const char **authentication)
694 {
695 #if HAVE_XSLT
696     xmlNodePtr ptr;
697     if (!m_cp->m_proxyPtr)
698         return 0;
699     int i = 0;
700     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
701         if (ptr->type == XML_ELEMENT_NODE &&
702             !strcmp((const char *) ptr->name, "target"))
703         {
704             if (i == no)
705             {
706                 struct _xmlAttr *attr;
707                 for (attr = ptr->properties; attr; attr = attr->next)
708                     if (!strcmp((const char *) attr->name, "name"))
709                     {
710                         if (attr->children
711                             && attr->children->type==XML_TEXT_NODE
712                             && attr->children->content)
713                             *name = (const char *) attr->children->content;
714                     }
715                 m_cp->return_target_info(
716                     ptr, url,
717                     limit_bw, limit_pdu, limit_req,
718                     target_idletime, client_idletime,
719                     keepalive_limit_bw, keepalive_limit_pdu,
720                     pre_init, cql2rpn, authentication);
721                 return 1;
722             }
723             i++;
724         }
725 #endif
726     return 0;
727 }
728
729 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
730 {
731     if (len == strlen(item) && memcmp(hay, item, len) == 0)
732         return 1;
733     return 0;
734 }
735
736 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
737                                        int *max_clients)
738 {
739 #if HAVE_XSLT
740     xmlNodePtr ptr;
741     if (!m_cp->m_proxyPtr)
742         return;
743     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
744     {
745         if (ptr->type == XML_ELEMENT_NODE 
746             && !strcmp((const char *) ptr->name, "log"))
747         {
748             const char *v = m_cp->get_text(ptr);
749             *log_mask = 0;
750             while (v && *v)
751             {
752                 const char *cp = v;
753                 while (*cp && *cp != ',' && !isspace(*cp))
754                     cp++;
755                 size_t len = cp - v;
756                 if (m_cp->mycmp(v, "client-apdu", len))
757                     *log_mask |= PROXY_LOG_APDU_CLIENT;
758                 if (m_cp->mycmp(v, "server-apdu", len))
759                     *log_mask |= PROXY_LOG_APDU_SERVER;
760                 if (m_cp->mycmp(v, "client-requests", len))
761                     *log_mask |= PROXY_LOG_REQ_CLIENT;
762                 if (m_cp->mycmp(v, "server-requests", len))
763                     *log_mask |= PROXY_LOG_REQ_SERVER;
764                 if (isdigit(*v))
765                     *log_mask |= atoi(v);
766                 if (*cp == ',')
767                     cp++;
768                 while (*cp && isspace(*cp))
769                     cp++;
770                 v = cp;
771             }
772         }
773         if (ptr->type == XML_ELEMENT_NODE &&
774             !strcmp((const char *) ptr->name, "max-clients"))
775         {
776             const char *t = m_cp->get_text(ptr);
777             if (t)
778             {
779                 *max_clients = atoi(t);
780                 if (*max_clients  < 1)
781                     *max_clients = 1;
782             }
783         }
784     }
785 #endif
786 }
787
788 #if HAVE_XSLT
789 int Yaz_ProxyConfigP::get_explain_ptr(const char *host, const char *db,
790                                       xmlNodePtr *ptr_target,
791                                       xmlNodePtr *ptr_explain)
792 {
793     xmlNodePtr ptr;
794     if (!m_proxyPtr)
795         return 0;
796     if (!db)
797         return 0;
798     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
799     {
800         if (ptr->type == XML_ELEMENT_NODE &&
801             !strcmp((const char *) ptr->name, "target"))
802         {
803             *ptr_target = ptr;
804             xmlNodePtr ptr = (*ptr_target)->children;
805             for (; ptr; ptr = ptr->next)
806             {
807                 if (ptr->type == XML_ELEMENT_NODE &&
808                     !strcmp((const char *) ptr->name, "explain"))
809                 {
810                     *ptr_explain = ptr;
811                     xmlNodePtr ptr = (*ptr_explain)->children;
812
813                     for (; ptr; ptr = ptr->next)
814                         if (ptr->type == XML_ELEMENT_NODE &&
815                             !strcmp((const char *) ptr->name, "serverInfo"))
816                             break;
817                     if (!ptr)
818                         continue;
819                     for (ptr = ptr->children; ptr; ptr = ptr->next)
820                         if (ptr->type == XML_ELEMENT_NODE &&
821                             !strcmp((const char *) ptr->name, "database"))
822                             break;
823                     
824                     if (!ptr)
825                         continue;
826                     for (ptr = ptr->children; ptr; ptr = ptr->next)
827                         if (ptr->type == XML_TEXT_NODE &&
828                             ptr->content &&
829                             !strcmp((const char *) ptr->content, db))
830                             break;
831                     if (!ptr)
832                         continue;
833                     return 1;
834                 }
835             }
836         }
837     }
838     return 0;
839 }
840 #endif
841
842 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
843                                               const char **backend_db)
844 {
845 #if HAVE_XSLT
846     xmlNodePtr ptr_target, ptr_explain;
847     if (m_cp->get_explain_ptr(0, db, &ptr_target, &ptr_explain)
848         && ptr_target)
849     {
850         struct _xmlAttr *attr;
851         const char *name = 0;
852         
853         for (attr = ptr_target->properties; attr; attr = attr->next)
854             if (!strcmp((const char *) attr->name, "name")
855                 && attr->children
856                 && attr->children->type==XML_TEXT_NODE
857                 && attr->children->content 
858                 && attr->children->content[0])
859             {
860                 name = (const char *)attr->children->content;
861                 break;
862             }
863         if (name)
864         {
865             for (attr = ptr_target->properties; attr; attr = attr->next)
866                 if (!strcmp((const char *) attr->name, "database"))
867                 {
868                     if (attr->children
869                         && attr->children->type==XML_TEXT_NODE
870                         && attr->children->content)
871                         *backend_db = (const char *) attr->children->content;
872                 }
873             return name;
874         }
875     }
876 #endif
877     return 0;
878 }
879
880 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
881                                        const char *db, int *len)
882 {
883 #if HAVE_XSLT
884     xmlNodePtr ptr_target, ptr_explain;
885     if (m_cp->get_explain_ptr(0 /* host */, db, &ptr_target, &ptr_explain))
886     {
887         xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
888         
889         xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
890         
891         xmlDocSetRootElement(doc, ptr2);
892         
893         xmlChar *buf_out;
894         xmlDocDumpMemory(doc, &buf_out, len);
895         char *content = (char*) odr_malloc(odr, *len);
896         memcpy(content, buf_out, *len);
897         
898         xmlFree(buf_out);
899         xmlFreeDoc(doc);
900         return content;
901     }
902 #endif
903     return 0;
904 }
905
906 void Yaz_ProxyConfig::get_target_info(const char *name,
907                                       const char **url,
908                                       int *limit_bw,
909                                       int *limit_pdu,
910                                       int *limit_req,
911                                       int *target_idletime,
912                                       int *client_idletime,
913                                       int *max_clients,
914                                       int *keepalive_limit_bw,
915                                       int *keepalive_limit_pdu,
916                                       int *pre_init,
917                                       const char **cql2rpn,
918                                       const char **authentication)
919 {
920 #if HAVE_XSLT
921     xmlNodePtr ptr;
922     if (!m_cp->m_proxyPtr)
923     {
924         url[0] = name;
925         url[1] = 0;
926         return;
927     }
928     url[0] = 0;
929     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
930     {
931         if (ptr->type == XML_ELEMENT_NODE &&
932             !strcmp((const char *) ptr->name, "max-clients"))
933         {
934             const char *t = m_cp->get_text(ptr);
935             if (t)
936             {
937                 *max_clients = atoi(t);
938                 if (*max_clients  < 1)
939                     *max_clients = 1;
940             }
941         }
942     }
943     ptr = m_cp->find_target_node(name, 0);
944     if (ptr)
945     {
946         if (name)
947         {
948             url[0] = name;
949             url[1] = 0;
950         }
951         m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
952                                  target_idletime, client_idletime,
953                                  keepalive_limit_bw, keepalive_limit_pdu,
954                                  pre_init, cql2rpn, authentication);
955     }
956 #else
957     *url = name;
958     return;
959 #endif
960 }
961
962