29865e0401e31c2aa8af8503b8d813bab340baee
[yazproxy-moved-to-github.git] / src / yaz-proxy-config.cpp
1 /* $Id: yaz-proxy-config.cpp,v 1.17 2005-02-21 14:27:32 adam Exp $
2    Copyright (c) 1998-2005, 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
24 #if HAVE_DLFCN_H
25 #include <dlfcn.h>
26 #endif
27
28 #include <yaz/log.h>
29 #include "proxyp.h"
30
31
32 class Yaz_ProxyModule {
33 private:
34     void *m_dl_handle;                /* dlopen/close handle */
35     Yaz_ProxyModule_entry *m_entry;
36     Yaz_ProxyModule *m_next; 
37     void *m_user_handle;              /* user handle */
38 public:
39     Yaz_ProxyModule(void *dl_handle, Yaz_ProxyModule_entry *ent,
40                     Yaz_ProxyModule *next);
41     ~Yaz_ProxyModule();
42     
43     Yaz_ProxyModule *get_next() { return m_next; };
44     int is_module(const char *name);
45     int authenticate(const char *target_name, void *element_ptr,
46                      const char *user, const char *group, const char *password);
47 };
48
49 int Yaz_ProxyModule::is_module(const char *type)
50 {
51     if (!type || !strcmp(m_entry->module_name, type))
52         return 1;
53     return 0;
54 }
55
56 Yaz_ProxyModule::Yaz_ProxyModule(void *dl_handle, Yaz_ProxyModule_entry *ent,
57                                  Yaz_ProxyModule *next)
58 {
59     m_dl_handle = dl_handle;
60     m_entry = ent;
61     m_next = next;
62     m_user_handle = 0;
63     if (m_entry->int_version == 0)
64     {
65         struct Yaz_ProxyModule_int0 *int0 =
66             reinterpret_cast<Yaz_ProxyModule_int0 *>(m_entry->fl);
67         if (int0->init)
68             m_user_handle = (*int0->init)();
69     }
70 }
71
72 Yaz_ProxyModule::~Yaz_ProxyModule()
73 {
74     if (m_entry->int_version == 0)
75     {
76         struct Yaz_ProxyModule_int0 *int0 =
77             reinterpret_cast<Yaz_ProxyModule_int0 *>(m_entry->fl);
78         if (int0->destroy)
79             (*int0->destroy)(m_user_handle);
80     }
81 #if HAVE_DLFCN_H
82     dlclose(m_dl_handle);
83 #endif
84 }
85
86 int Yaz_ProxyModule::authenticate(const char *name,
87                                   void *element_ptr,
88                                   const char *user, const char *group,
89                                   const char *password)
90 {
91     if (m_entry->int_version == 0)
92     {
93         struct Yaz_ProxyModule_int0 *int0 =
94             reinterpret_cast<Yaz_ProxyModule_int0 *>(m_entry->fl);
95         
96         if (!int0->authenticate)
97             return YAZPROXY_RET_NOT_ME;
98         return (*int0->authenticate)(m_user_handle, name, element_ptr,
99                                      user, group, password);
100     }
101     return YAZPROXY_RET_NOT_ME;
102 }
103
104 class Yaz_ProxyConfigP {
105     friend class Yaz_ProxyConfig;
106
107     Yaz_ProxyModule *m_modules;
108
109     int mycmp(const char *hay, const char *item, size_t len);
110     int match_list(int v, const char *m);
111     int atoi_l(const char **cp);
112 #if HAVE_XSLT
113     void load_modules(void);
114     void unload_modules(void);
115     int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
116                      const char *schema_identifier);
117     xmlDocPtr m_docPtr;
118     xmlNodePtr m_proxyPtr;
119     void return_target_info(xmlNodePtr ptr, const char **url,
120                             int *limit_bw, int *limit_pdu, int *limit_req,
121                             int *target_idletime, int *client_idletime,
122                             int *keepalive_limit_bw, int *keepalive_limit_pdu,
123                             int *pre_init, const char **cql2rpn,
124                             const char **negotiation_charset,
125                             const char **negotiation_lang);
126     void return_limit(xmlNodePtr ptr,
127                       int *limit_bw, int *limit_pdu, int *limit_req);
128     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
129                      char **addinfo);
130     xmlNodePtr find_target_node(const char *name, const char *db);
131     xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
132     const char *get_text(xmlNodePtr ptr);
133     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
134                                 Z_AttributeList *attrs,
135                                 char **addinfo);
136     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
137                                char **addinfo);
138     int get_explain_ptr(const char *host, const char *db,
139                         xmlNodePtr *ptr_target, xmlNodePtr *ptr_explain);
140 #endif
141     Yaz_ProxyConfigP();
142     ~Yaz_ProxyConfigP();
143 };
144
145 Yaz_ProxyConfigP::Yaz_ProxyConfigP()
146 {
147 #if HAVE_XSLT
148     m_docPtr = 0;
149     m_proxyPtr = 0;
150 #endif
151     m_modules = 0;
152 }
153
154 Yaz_ProxyConfigP::~Yaz_ProxyConfigP()
155 {
156 #if HAVE_XSLT
157     if (m_docPtr)
158         xmlFreeDoc(m_docPtr);
159 #endif
160 }
161
162 Yaz_ProxyConfig::Yaz_ProxyConfig()
163 {
164     m_cp = new Yaz_ProxyConfigP;
165 }
166
167 Yaz_ProxyConfig::~Yaz_ProxyConfig()
168 {
169     delete m_cp;
170 }
171
172 #if HAVE_XSLT
173 void Yaz_ProxyConfigP::unload_modules()
174 {
175     Yaz_ProxyModule *m = m_modules;
176     while (m)
177     {
178         Yaz_ProxyModule *m_next = m->get_next();
179         delete m;
180         m = m_next;
181     }
182     m_modules = 0;
183 }
184 #endif
185
186 #if HAVE_XSLT
187 void Yaz_ProxyConfigP::load_modules()
188 {
189     if (!m_proxyPtr)
190         return;
191     xmlNodePtr ptr;
192     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
193     {
194         const char *fname;
195         if (ptr->type == XML_ELEMENT_NODE 
196             && !strcmp((const char *) ptr->name, "module")
197             && (fname = get_text(ptr)))
198         {
199 #if HAVE_DLFCN_H
200             void *dl_handle = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
201             if (dl_handle)
202             {
203                 Yaz_ProxyModule_entry *fl_ptr = 0;
204                 fl_ptr = reinterpret_cast<Yaz_ProxyModule_entry *> 
205                     (dlsym(dl_handle, "yazproxy_module"));
206                 if (fl_ptr)
207                 {
208                     Yaz_ProxyModule *m = new Yaz_ProxyModule(dl_handle,
209                                                              fl_ptr,
210                                                              m_modules);
211                     m_modules = m;
212                     yaz_log(YLOG_LOG, "Loading %s OK", fname);
213                 }
214                 else
215                 {
216                     yaz_log(YLOG_WARN, "Loading %s FAIL: missing yazproxy_module symbol", fname);
217                     dlclose(dl_handle);
218                 }
219             }
220             else
221                 yaz_log(YLOG_WARN, "Loading %s FAIL: dlopen failed", fname);
222 #else
223             yaz_log(YLOG_WARN, "Loading &s FAIL: dl unsupported", fname);
224 #endif
225         }
226     }
227 }
228 #endif
229
230 int Yaz_ProxyConfig::read_xml(const char *fname)
231 {
232 #if HAVE_XSLT
233     xmlDocPtr ndoc = xmlParseFile(fname);
234
235     if (!ndoc)
236     {
237         yaz_log(YLOG_WARN, "Config file %s not found or parse error", fname);
238         return -1;  // no good
239     }
240     int noSubstitutions = xmlXIncludeProcess(ndoc);
241     if (noSubstitutions == -1)
242         yaz_log(YLOG_WARN, "XInclude processing failed on config %s", fname);
243
244     xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
245     if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
246         strcmp((const char *) proxyPtr->name, "proxy"))
247     {
248         yaz_log(YLOG_WARN, "No proxy element in %s", fname);
249         xmlFreeDoc(ndoc);
250         return -1;
251     }
252     m_cp->m_proxyPtr = proxyPtr;
253
254     // OK: release previous and make it the current one.
255     if (m_cp->m_docPtr)
256         xmlFreeDoc(m_cp->m_docPtr);
257     m_cp->m_docPtr = ndoc;
258
259     m_cp->unload_modules();
260     m_cp->load_modules();
261     return 0;
262 #else
263     return -2;
264 #endif
265 }
266
267 #if HAVE_XSLT
268 const char *Yaz_ProxyConfigP::get_text(xmlNodePtr ptr)
269 {
270     for(ptr = ptr->children; ptr; ptr = ptr->next)
271         if (ptr->type == XML_TEXT_NODE)
272         {
273             xmlChar *t = ptr->content;
274             if (t)
275             {
276                 while (*t == ' ')
277                     t++;
278                 return (const char *) t;
279             }
280         }
281     return 0;
282 }
283 #endif
284
285 #if HAVE_XSLT
286 void Yaz_ProxyConfigP::return_limit(xmlNodePtr ptr,
287                                    int *limit_bw,
288                                    int *limit_pdu,
289                                    int *limit_req)
290 {
291     for (ptr = ptr->children; ptr; ptr = ptr->next)
292     {
293         if (ptr->type == XML_ELEMENT_NODE 
294             && !strcmp((const char *) ptr->name, "bandwidth"))
295         {
296             const char *t = get_text(ptr);
297             if (t)
298                 *limit_bw = atoi(t);
299         }
300         if (ptr->type == XML_ELEMENT_NODE 
301             && !strcmp((const char *) ptr->name, "retrieve"))
302         {
303             const char *t = get_text(ptr);
304             if (t)
305                 *limit_req = atoi(t);
306         }
307         if (ptr->type == XML_ELEMENT_NODE 
308             && !strcmp((const char *) ptr->name, "pdu"))
309         {
310             const char *t = get_text(ptr);
311             if (t)
312                 *limit_pdu = atoi(t);
313         }
314     }
315 }
316 #endif
317
318 #if HAVE_XSLT
319 void Yaz_ProxyConfigP::return_target_info(xmlNodePtr ptr,
320                                           const char **url,
321                                           int *limit_bw,
322                                           int *limit_pdu,
323                                           int *limit_req,
324                                           int *target_idletime,
325                                           int *client_idletime,
326                                           int *keepalive_limit_bw,
327                                           int *keepalive_limit_pdu,
328                                           int *pre_init,
329                                           const char **cql2rpn,
330                                           const char **negotiation_charset,
331                                           const char **negotiation_lang)
332 {
333     *pre_init = 0;
334     int no_url = 0;
335     ptr = ptr->children;
336     for (; ptr; ptr = ptr->next)
337     {
338         if (ptr->type == XML_ELEMENT_NODE 
339             && !strcmp((const char *) ptr->name, "preinit"))
340         {
341             const char *v = get_text(ptr);
342             *pre_init = v ? atoi(v) : 1;
343         }
344         if (ptr->type == XML_ELEMENT_NODE 
345             && !strcmp((const char *) ptr->name, "url"))
346         {
347             const char *t = get_text(ptr);
348             if (t && no_url < MAX_ZURL_PLEX)
349             {
350                 url[no_url++] = t;
351                 url[no_url] = 0;
352             }
353         }
354         if (ptr->type == XML_ELEMENT_NODE 
355             && !strcmp((const char *) ptr->name, "keepalive"))
356         {
357             int dummy;
358             *keepalive_limit_bw = 500000;
359             *keepalive_limit_pdu = 1000;
360             return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
361                          &dummy);
362         }
363         if (ptr->type == XML_ELEMENT_NODE 
364             && !strcmp((const char *) ptr->name, "limit"))
365             return_limit(ptr, limit_bw, limit_pdu, limit_req);
366         if (ptr->type == XML_ELEMENT_NODE 
367             && !strcmp((const char *) ptr->name, "target-timeout"))
368         {
369             const char *t = get_text(ptr);
370             if (t)
371             {
372                 *target_idletime = atoi(t);
373                 if (*target_idletime < 0)
374                     *target_idletime = 0;
375             }
376         }
377         if (ptr->type == XML_ELEMENT_NODE 
378             && !strcmp((const char *) ptr->name, "client-timeout"))
379         {
380             const char *t = get_text(ptr);
381             if (t)
382             {
383                 *client_idletime = atoi(t);
384                 if (*client_idletime < 0)
385                     *client_idletime = 0;
386             }
387         }
388         if (ptr->type == XML_ELEMENT_NODE 
389             && !strcmp((const char *) ptr->name, "cql2rpn"))
390         {
391             const char *t = get_text(ptr);
392             if (t)
393                 *cql2rpn = t;
394         }
395         if (ptr->type == XML_ELEMENT_NODE 
396             && !strcmp((const char *) ptr->name, "negotiation-charset"))
397         {
398             const char *t = get_text(ptr);
399             if (t)
400                 *negotiation_charset = t;
401         }
402         if (ptr->type == XML_ELEMENT_NODE 
403             && !strcmp((const char *) ptr->name, "negotiation-lang"))
404         {
405             const char *t = get_text(ptr);
406             if (t)
407                 *negotiation_lang = t;
408         }
409     }
410 }
411 #endif
412
413 int Yaz_ProxyConfigP::atoi_l(const char **cp)
414 {
415     int v = 0;
416     while (**cp && isdigit(**cp))
417     {
418         v = v*10 + (**cp - '0');
419         (*cp)++;
420     }
421     return v;
422 }
423
424 int Yaz_ProxyConfigP::match_list(int v, const char *m)
425 {
426     while(m && *m)
427     {
428         while(*m && isspace(*m))
429             m++;
430         if (*m == '*')
431             return 1;
432         int l = atoi_l(&m);
433         int h = l;
434         if (*m == '-')
435         {
436             ++m;
437             h = atoi_l(&m);
438         }
439         if (v >= l && v <= h)
440           return 1;
441         if (*m == ',')
442             m++;
443     }
444     return 0;
445 }
446
447 #if HAVE_XSLT
448 int Yaz_ProxyConfigP::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
449                                               Z_AttributeList *attrs,
450                                               char **addinfo)
451 {
452     int i;
453     for (i = 0; i<attrs->num_attributes; i++)
454     {
455         Z_AttributeElement *el = attrs->attributes[i];
456         
457         if (!el->attributeType)
458             continue;
459         int type = *el->attributeType;
460         int *value = 0;
461         
462         if (el->which == Z_AttributeValue_numeric && el->value.numeric)
463             value = el->value.numeric;
464         
465         xmlNodePtr ptr;
466         for(ptr = ptrl->children; ptr; ptr = ptr->next)
467         {
468             if (ptr->type == XML_ELEMENT_NODE &&
469                 !strcmp((const char *) ptr->name, "attribute"))
470             {
471                 const char *match_type = 0;
472                 const char *match_value = 0;
473                 const char *match_error = 0;
474                 struct _xmlAttr *attr;
475                 for (attr = ptr->properties; attr; attr = attr->next)
476                 {
477                     if (!strcmp((const char *) attr->name, "type") &&
478                         attr->children && attr->children->type == XML_TEXT_NODE)
479                         match_type = (const char *) attr->children->content;
480                     if (!strcmp((const char *) attr->name, "value") &&
481                         attr->children && attr->children->type == XML_TEXT_NODE)
482                         match_value = (const char *) attr->children->content;
483                     if (!strcmp((const char *) attr->name, "error") &&
484                         attr->children && attr->children->type == XML_TEXT_NODE)
485                         match_error = (const char *) attr->children->content;
486                 }
487                 if (match_type && match_value)
488                 {
489                     char addinfo_str[20];
490                     if (!match_list(type, match_type))
491                         continue;
492                     
493                     *addinfo_str = '\0';
494                     if (!strcmp(match_type, "*"))
495                         sprintf (addinfo_str, "%d", type);
496                     else if (value)
497                     {
498                         if (!match_list(*value, match_value))
499                             continue;
500                         sprintf (addinfo_str, "%d", *value);
501                     }
502                     else
503                         continue;
504                     
505                     if (match_error)
506                     {
507                         if (*addinfo_str)
508                             *addinfo = odr_strdup(odr, addinfo_str);
509                         return atoi(match_error);
510                     }
511                     break;
512                 }
513             }
514         }
515     }
516     return 0;
517 }
518 #endif
519
520 #if HAVE_XSLT
521 int Yaz_ProxyConfigP::check_type_1_structure(ODR odr, xmlNodePtr ptr,
522                                             Z_RPNStructure *q,
523                                             char **addinfo)
524 {
525     if (q->which == Z_RPNStructure_complex)
526     {
527         int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
528         if (e)
529             return e;
530         e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
531         return e;
532     }
533     else if (q->which == Z_RPNStructure_simple)
534     {
535         if (q->u.simple->which == Z_Operand_APT)
536         {
537             return check_type_1_attributes(
538                 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
539                 addinfo);
540         }
541     }
542     return 0;
543 }
544 #endif
545
546 #if HAVE_XSLT
547 int Yaz_ProxyConfigP::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
548                                    char **addinfo)
549 {
550     // possibly check for Bib-1
551     return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
552 }
553 #endif
554
555 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
556                                  char **addinfo)
557 {
558 #if HAVE_XSLT
559     xmlNodePtr ptr;
560     
561     ptr = m_cp->find_target_node(name, 0);
562     if (ptr)
563     {
564         if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
565             return m_cp->check_type_1(odr, ptr, query->u.type_1, addinfo);
566     }
567 #endif
568     return 0;
569 }
570
571 #if HAVE_XSLT
572 int Yaz_ProxyConfigP::check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
573                                    const char *schema_identifier)
574 {
575     char *esn = 0;
576     int default_match = 1;
577     if (comp && comp->which == Z_RecordComp_simple &&
578         comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
579     {
580         esn = comp->u.simple->u.generic;
581     }
582     // if no ESN/schema was given accept..
583     if (!esn)
584         return 1;
585     // check if schema identifier match
586     if (schema_identifier && !strcmp(esn, schema_identifier))
587         return 1;
588     // Check each name element
589     for (; ptr; ptr = ptr->next)
590     {
591         if (ptr->type == XML_ELEMENT_NODE 
592             && !strcmp((const char *) ptr->name, "name"))
593         {
594             xmlNodePtr tptr = ptr->children;
595             default_match = 0;
596             for (; tptr; tptr = tptr->next)
597                 if (tptr->type == XML_TEXT_NODE && tptr->content)
598                 {
599                     xmlChar *t = tptr->content;
600                     while (*t && isspace(*t))
601                         t++;
602                     int i = 0;
603                     while (esn[i] && esn[i] == t[i])
604                         i++;
605                     if (!esn[i] && (!t[i] || isspace(t[i])))
606                         return 1;
607                 }
608         }
609     }
610     return default_match;
611 }
612 #endif
613
614 const char *Yaz_ProxyConfig::check_mime_type(const char *path)
615 {
616     struct {
617         const char *mask;
618         const char *type;
619     } types[] = {
620         {".xml", "text/xml"},
621         {".xsl", "text/xml"},
622         {".tkl", "text/xml"},
623         {".xsd", "text/xml"},
624         {".html", "text/html"},
625         {".jpg", "image/jpeg"},
626         {".png", "image/png"},
627         {".gif", "image/gif"},
628         {0, "text/plain"},
629         {0, 0},
630     };
631     int i;
632     size_t plen = strlen (path);
633     for (i = 0; types[i].type; i++)
634         if (types[i].mask == 0)
635             return types[i].type;
636         else
637         {
638             size_t mlen = strlen(types[i].mask);
639             if (plen > mlen && !memcmp(path+plen-mlen, types[i].mask, mlen))
640                 return types[i].type;
641         }
642     return "application/octet-stream";
643 }
644
645
646 void Yaz_ProxyConfig::target_authentication(const char *name,
647                                             ODR odr, Z_InitRequest *req)
648 {
649 #if HAVE_XSLT
650     xmlNodePtr ptr = m_cp->find_target_node(name, 0);
651     if (!ptr)
652         return ;
653     
654     for (ptr = ptr->children; ptr; ptr = ptr->next)
655         if (ptr->type == XML_ELEMENT_NODE &&
656             !strcmp((const char *) ptr->name, "target-authentication"))
657         {
658             struct _xmlAttr *attr;
659             const char *type = "open";
660             for (attr = ptr->properties; attr; attr = attr->next)
661             {
662                 if (!strcmp((const char *) attr->name, "type") &&
663                     attr->children && attr->children->type == XML_TEXT_NODE)
664                     type = (const char *) attr->children->content;
665             }
666             const char *t = m_cp->get_text(ptr);
667             if (!t || !strcmp(type, "none"))
668                 req->idAuthentication = 0;
669             else if (!strcmp(type, "anonymous"))
670             {
671                 req->idAuthentication =
672                     (Z_IdAuthentication *)
673                     odr_malloc (odr, sizeof(*req->idAuthentication));
674                 req->idAuthentication->which =
675                     Z_IdAuthentication_anonymous;
676                 req->idAuthentication->u.anonymous = odr_nullval();
677             }
678             else if (!strcmp(type, "open"))
679             {
680                 req->idAuthentication =
681                     (Z_IdAuthentication *)
682                     odr_malloc (odr, sizeof(*req->idAuthentication));
683                 req->idAuthentication->which =
684                     Z_IdAuthentication_open;
685                 req->idAuthentication->u.open = odr_strdup (odr, t);
686             }
687             else if (!strcmp(type, "idPass"))
688             {
689                 char user[64], group[64], password[64];
690                 *group = '\0';
691                 *password = '\0';
692                 *user = '\0';
693                 sscanf(t, "%63[^:]:%63[^:]:%63s", user, group, password);
694                 
695                 req->idAuthentication =
696                     (Z_IdAuthentication *)
697                     odr_malloc (odr, sizeof(*req->idAuthentication));
698                 req->idAuthentication->which =
699                     Z_IdAuthentication_idPass;
700                 req->idAuthentication->u.idPass =
701                     (Z_IdPass*) odr_malloc(odr, sizeof(Z_IdPass));
702                 req->idAuthentication->u.idPass->userId =
703                     *user ? odr_strdup(odr, user) : 0;
704                 req->idAuthentication->u.idPass->groupId =
705                     *group ? odr_strdup(odr, group) : 0;
706                 req->idAuthentication->u.idPass->password =
707                     *password ? odr_strdup(odr, password) : 0;
708             }
709         }
710 #endif
711 }
712
713 int Yaz_ProxyConfig::client_authentication(const char *name,
714                                            const char *user,
715                                            const char *group,
716                                            const char *password)
717 {
718     int ret = YAZPROXY_RET_NOT_ME;
719 #if HAVE_XSLT
720     xmlNodePtr ptr;
721     ptr = m_cp->find_target_node(name, 0);
722     if (!ptr)
723         return 1;
724     for (ptr = ptr->children; ptr; ptr = ptr->next)
725         if (ptr->type == XML_ELEMENT_NODE &&
726             !strcmp((const char *) ptr->name, "client-authentication"))
727         {
728             struct _xmlAttr *attr;
729             const char *module_name = 0;
730             for (attr = ptr->properties; attr; attr = attr->next)
731             {
732                 if (!strcmp((const char *) attr->name, "module") &&
733                     attr->children && attr->children->type == XML_TEXT_NODE)
734                     module_name = (const char *) attr->children->content;
735             }
736             Yaz_ProxyModule *m = m_cp->m_modules;
737             for (; m; m = m->get_next())
738             {
739                 if (m->is_module(module_name))
740                 {
741                     ret = m->authenticate(name, ptr, user, group, password);
742                     if (ret != YAZPROXY_RET_NOT_ME)
743                         break;
744                 }
745             }
746         }
747 #endif
748     if (ret == YAZPROXY_RET_PERM)
749         return 0;
750     return 1;
751 }
752
753 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
754                                   Odr_oid *syntax, Z_RecordComposition *comp,
755                                   char **addinfo,
756                                   char **stylesheet, char **schema,
757                                   char **backend_type,
758                                   char **backend_charset,
759                                   char **usemarcon_ini_stage1,
760                                   char **usemarcon_ini_stage2
761                                   )
762 {
763     if (stylesheet)
764     {
765         xfree (*stylesheet);
766         *stylesheet = 0;
767     }
768     if (schema)
769     {
770         xfree (*schema);
771         *schema = 0;
772     }
773     if (backend_type)
774     {
775         xfree (*backend_type);
776         *backend_type = 0;
777     }
778     if (backend_charset)
779     {
780         xfree (*backend_charset);
781         *backend_charset = 0;
782     }
783     if (usemarcon_ini_stage1)
784     {
785         xfree (*usemarcon_ini_stage1);
786         *usemarcon_ini_stage1 = 0;
787     }
788     if (usemarcon_ini_stage2)
789     {
790         xfree (*usemarcon_ini_stage2);
791         *usemarcon_ini_stage2 = 0;
792     }
793 #if HAVE_XSLT
794     int syntax_has_matched = 0;
795     xmlNodePtr ptr;
796     
797     ptr = m_cp->find_target_node(name, 0);
798     if (!ptr)
799         return 0;
800     for(ptr = ptr->children; ptr; ptr = ptr->next)
801     {
802         if (ptr->type == XML_ELEMENT_NODE &&
803             !strcmp((const char *) ptr->name, "syntax"))
804         {
805             int match = 0;  // if we match record syntax
806             const char *match_type = 0;
807             const char *match_error = 0;
808             const char *match_marcxml = 0;
809             const char *match_stylesheet = 0;
810             const char *match_identifier = 0;
811             const char *match_backend_type = 0;
812             const char *match_backend_charset = 0;
813             const char *match_usemarcon_ini_stage1 = 0;
814             const char *match_usemarcon_ini_stage2 = 0;
815             struct _xmlAttr *attr;
816             for (attr = ptr->properties; attr; attr = attr->next)
817             {
818                 if (!strcmp((const char *) attr->name, "type") &&
819                     attr->children && attr->children->type == XML_TEXT_NODE)
820                     match_type = (const char *) attr->children->content;
821                 if (!strcmp((const char *) attr->name, "error") &&
822                     attr->children && attr->children->type == XML_TEXT_NODE)
823                     match_error = (const char *) attr->children->content;
824                 if (!strcmp((const char *) attr->name, "marcxml") &&
825                     attr->children && attr->children->type == XML_TEXT_NODE)
826                     match_marcxml = (const char *) attr->children->content;
827                 if (!strcmp((const char *) attr->name, "stylesheet") &&
828                     attr->children && attr->children->type == XML_TEXT_NODE)
829                     match_stylesheet = (const char *) attr->children->content;
830                 if (!strcmp((const char *) attr->name, "identifier") &&
831                     attr->children && attr->children->type == XML_TEXT_NODE)
832                     match_identifier = (const char *) attr->children->content;
833                 if (!strcmp((const char *) attr->name, "backendtype") &&
834                     attr->children && attr->children->type == XML_TEXT_NODE)
835                     match_backend_type = (const char *)
836                         attr->children->content;
837                 if (!strcmp((const char *) attr->name, "backendcharset") &&
838                     attr->children && attr->children->type == XML_TEXT_NODE)
839                     match_backend_charset = (const char *)
840                         attr->children->content;
841                 if (!strcmp((const char *) attr->name, "usemarconstage1") &&
842                     attr->children && attr->children->type == XML_TEXT_NODE)
843                     match_usemarcon_ini_stage1 = (const char *)
844                         attr->children->content;
845                 if (!strcmp((const char *) attr->name, "usemarconstage2") &&
846                     attr->children && attr->children->type == XML_TEXT_NODE)
847                     match_usemarcon_ini_stage2 = (const char *)
848                         attr->children->content;
849             }
850             if (match_type)
851             {
852                 if (!strcmp(match_type, "*"))
853                     match = 1;
854                 else if (!strcmp(match_type, "none"))
855                 {
856                     if (syntax == 0)
857                         match = 1;
858                 }
859                 else if (syntax)
860                 {
861                     int match_oid[OID_SIZE];
862                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
863                     if (oid_oidcmp(match_oid, syntax) == 0)
864                         match = 1;
865                 }
866             }
867             if (match)
868             {
869                 if (!match_error)
870                     syntax_has_matched = 1;
871                 match = m_cp->check_schema(ptr->children, comp,
872                                            match_identifier);
873             }
874             if (match)
875             {
876                 if (stylesheet && match_stylesheet)
877                 {
878                     xfree(*stylesheet);
879                     *stylesheet = xstrdup(match_stylesheet);
880                 }
881                 if (schema && match_identifier)
882                 {
883                     xfree(*schema);
884                     *schema = xstrdup(match_identifier);
885                 }
886                 if (backend_type && match_backend_type)
887                 {
888                     xfree(*backend_type);
889                     *backend_type = xstrdup(match_backend_type);
890                 }
891                 if (backend_charset && match_backend_charset)
892                 {
893                     xfree(*backend_charset);
894                     *backend_charset = xstrdup(match_backend_charset);
895                 }
896                 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage1)
897                 {
898                     xfree(*usemarcon_ini_stage1);
899                     *usemarcon_ini_stage1 = xstrdup(match_usemarcon_ini_stage1);
900                 }
901                 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage2)
902                 {
903                     xfree(*usemarcon_ini_stage2);
904                     *usemarcon_ini_stage2 = xstrdup(match_usemarcon_ini_stage2);
905                 }
906                 if (match_marcxml)
907                 {
908                     return -1;
909                 }
910                 if (match_error)
911                 {
912                     if (syntax_has_matched)  // if syntax OK, bad schema/ESN
913                         return 25;
914                     if (syntax)
915                     {
916                         char dotoid_str[100];
917                         oid_to_dotstring(syntax, dotoid_str);
918                         *addinfo = odr_strdup(odr, dotoid_str);
919                     }
920                     return atoi(match_error);
921                 }
922                 return 0;
923             }
924         }
925     }
926 #endif
927     return 0;
928 }
929
930 #if HAVE_XSLT
931 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
932 {
933     xmlNodePtr dptr;
934     if (!db)
935         return ptr;
936     if (!ptr)
937         return 0;
938     for (dptr = ptr->children; dptr; dptr = dptr->next)
939         if (dptr->type == XML_ELEMENT_NODE &&
940             !strcmp((const char *) dptr->name, "database"))
941         {
942             struct _xmlAttr *attr;
943             for (attr = dptr->properties; attr; attr = attr->next)
944                 if (!strcmp((const char *) attr->name, "name"))
945                 {
946                     if (attr->children
947                         && attr->children->type==XML_TEXT_NODE
948                         && attr->children->content 
949                         && (!strcmp((const char *) attr->children->content, db)
950                             || !strcmp((const char *) attr->children->content,
951                                        "*")))
952                         return dptr;
953                 }
954         }
955     return ptr;
956 }
957     
958 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
959 {
960     xmlNodePtr ptr;
961     if (!m_proxyPtr)
962         return 0;
963     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
964     {
965         if (ptr->type == XML_ELEMENT_NODE &&
966             !strcmp((const char *) ptr->name, "target"))
967         {
968             // default one ? 
969             if (!name)
970             {
971                 // <target default="1"> ?
972                 struct _xmlAttr *attr;
973                 for (attr = ptr->properties; attr; attr = attr->next)
974                     if (!strcmp((const char *) attr->name, "default") &&
975                         attr->children && attr->children->type == XML_TEXT_NODE)
976                     {
977                         xmlChar *t = attr->children->content;
978                         if (!t || *t == '1')
979                         {
980                             return find_target_db(ptr, db);
981                         }
982                     }
983             }
984             else
985             {
986                 // <target name="name"> ?
987                 struct _xmlAttr *attr;
988                 for (attr = ptr->properties; attr; attr = attr->next)
989                     if (!strcmp((const char *) attr->name, "name"))
990                     {
991                         if (attr->children
992                             && attr->children->type==XML_TEXT_NODE
993                             && attr->children->content 
994                             && (!strcmp((const char *) attr->children->content,
995                                         name)
996                                 || !strcmp((const char *) attr->children->content,
997                                            "*")))
998                         {
999                             return find_target_db(ptr, db);
1000                         }
1001                     }
1002             }
1003         }
1004     }
1005     return 0;
1006 }
1007 #endif
1008
1009 int Yaz_ProxyConfig::get_target_no(int no,
1010                                    const char **name,
1011                                    const char **url,
1012                                    int *limit_bw,
1013                                    int *limit_pdu,
1014                                    int *limit_req,
1015                                    int *target_idletime,
1016                                    int *client_idletime,
1017                                    int *max_clients,
1018                                    int *keepalive_limit_bw,
1019                                    int *keepalive_limit_pdu,
1020                                    int *pre_init,
1021                                    const char **cql2rpn,
1022                                    const char **authentication,
1023                                    const char **negotiation_charset,
1024                                    const char **negotiation_lang)
1025 {
1026 #if HAVE_XSLT
1027     xmlNodePtr ptr;
1028     if (!m_cp->m_proxyPtr)
1029         return 0;
1030     int i = 0;
1031     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
1032         if (ptr->type == XML_ELEMENT_NODE &&
1033             !strcmp((const char *) ptr->name, "target"))
1034         {
1035             if (i == no)
1036             {
1037                 struct _xmlAttr *attr;
1038                 for (attr = ptr->properties; attr; attr = attr->next)
1039                     if (!strcmp((const char *) attr->name, "name"))
1040                     {
1041                         if (attr->children
1042                             && attr->children->type==XML_TEXT_NODE
1043                             && attr->children->content)
1044                             *name = (const char *) attr->children->content;
1045                     }
1046                 m_cp->return_target_info(
1047                     ptr, url,
1048                     limit_bw, limit_pdu, limit_req,
1049                     target_idletime, client_idletime,
1050                     keepalive_limit_bw, keepalive_limit_pdu,
1051                     pre_init, cql2rpn,
1052                     negotiation_charset, negotiation_lang);
1053                 return 1;
1054             }
1055             i++;
1056         }
1057 #endif
1058     return 0;
1059 }
1060
1061 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
1062 {
1063     if (len == strlen(item) && memcmp(hay, item, len) == 0)
1064         return 1;
1065     return 0;
1066 }
1067
1068 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
1069                                        int *max_clients)
1070 {
1071 #if HAVE_XSLT
1072     xmlNodePtr ptr;
1073     if (!m_cp->m_proxyPtr)
1074         return;
1075     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
1076     {
1077         if (ptr->type == XML_ELEMENT_NODE 
1078             && !strcmp((const char *) ptr->name, "log"))
1079         {
1080             const char *v = m_cp->get_text(ptr);
1081             *log_mask = 0;
1082             while (v && *v)
1083             {
1084                 const char *cp = v;
1085                 while (*cp && *cp != ',' && !isspace(*cp))
1086                     cp++;
1087                 size_t len = cp - v;
1088                 if (m_cp->mycmp(v, "client-apdu", len))
1089                     *log_mask |= PROXY_LOG_APDU_CLIENT;
1090                 if (m_cp->mycmp(v, "server-apdu", len))
1091                     *log_mask |= PROXY_LOG_APDU_SERVER;
1092                 if (m_cp->mycmp(v, "client-requests", len))
1093                     *log_mask |= PROXY_LOG_REQ_CLIENT;
1094                 if (m_cp->mycmp(v, "server-requests", len))
1095                     *log_mask |= PROXY_LOG_REQ_SERVER;
1096                 if (isdigit(*v))
1097                     *log_mask |= atoi(v);
1098                 if (*cp == ',')
1099                     cp++;
1100                 while (*cp && isspace(*cp))
1101                     cp++;
1102                 v = cp;
1103             }
1104         }
1105         if (ptr->type == XML_ELEMENT_NODE &&
1106             !strcmp((const char *) ptr->name, "max-clients"))
1107         {
1108             const char *t = m_cp->get_text(ptr);
1109             if (t)
1110             {
1111                 *max_clients = atoi(t);
1112                 if (*max_clients  < 1)
1113                     *max_clients = 1;
1114             }
1115         }
1116     }
1117 #endif
1118 }
1119
1120 #if HAVE_XSLT
1121 int Yaz_ProxyConfigP::get_explain_ptr(const char *host, const char *db,
1122                                       xmlNodePtr *ptr_target,
1123                                       xmlNodePtr *ptr_explain)
1124 {
1125     xmlNodePtr ptr;
1126     if (!m_proxyPtr)
1127         return 0;
1128     if (!db)
1129         return 0;
1130     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
1131     {
1132         if (ptr->type == XML_ELEMENT_NODE &&
1133             !strcmp((const char *) ptr->name, "target"))
1134         {
1135             *ptr_target = ptr;
1136             xmlNodePtr ptr = (*ptr_target)->children;
1137             for (; ptr; ptr = ptr->next)
1138             {
1139                 if (ptr->type == XML_ELEMENT_NODE &&
1140                     !strcmp((const char *) ptr->name, "explain"))
1141                 {
1142                     *ptr_explain = ptr;
1143                     xmlNodePtr ptr = (*ptr_explain)->children;
1144
1145                     for (; ptr; ptr = ptr->next)
1146                         if (ptr->type == XML_ELEMENT_NODE &&
1147                             !strcmp((const char *) ptr->name, "serverInfo"))
1148                             break;
1149                     if (!ptr)
1150                         continue;
1151                     for (ptr = ptr->children; ptr; ptr = ptr->next)
1152                         if (ptr->type == XML_ELEMENT_NODE &&
1153                             !strcmp((const char *) ptr->name, "database"))
1154                             break;
1155                     
1156                     if (!ptr)
1157                         continue;
1158                     for (ptr = ptr->children; ptr; ptr = ptr->next)
1159                         if (ptr->type == XML_TEXT_NODE &&
1160                             ptr->content &&
1161                             !strcmp((const char *) ptr->content, db))
1162                             break;
1163                     if (!ptr)
1164                         continue;
1165                     return 1;
1166                 }
1167             }
1168         }
1169     }
1170     return 0;
1171 }
1172 #endif
1173
1174 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
1175                                               const char **backend_db)
1176 {
1177 #if HAVE_XSLT
1178     xmlNodePtr ptr_target, ptr_explain;
1179     if (m_cp->get_explain_ptr(0, db, &ptr_target, &ptr_explain)
1180         && ptr_target)
1181     {
1182         struct _xmlAttr *attr;
1183         const char *name = 0;
1184         
1185         for (attr = ptr_target->properties; attr; attr = attr->next)
1186             if (!strcmp((const char *) attr->name, "name")
1187                 && attr->children
1188                 && attr->children->type==XML_TEXT_NODE
1189                 && attr->children->content 
1190                 && attr->children->content[0])
1191             {
1192                 name = (const char *)attr->children->content;
1193                 break;
1194             }
1195         if (name)
1196         {
1197             for (attr = ptr_target->properties; attr; attr = attr->next)
1198                 if (!strcmp((const char *) attr->name, "database"))
1199                 {
1200                     if (attr->children
1201                         && attr->children->type==XML_TEXT_NODE
1202                         && attr->children->content)
1203                         *backend_db = (const char *) attr->children->content;
1204                 }
1205             return name;
1206         }
1207     }
1208 #endif
1209     return 0;
1210 }
1211
1212 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
1213                                        const char *db, int *len)
1214 {
1215 #if HAVE_XSLT
1216     xmlNodePtr ptr_target, ptr_explain;
1217     if (m_cp->get_explain_ptr(0 /* host */, db, &ptr_target, &ptr_explain))
1218     {
1219         xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
1220         
1221         xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
1222         
1223         xmlDocSetRootElement(doc, ptr2);
1224         
1225         xmlChar *buf_out;
1226         xmlDocDumpMemory(doc, &buf_out, len);
1227         char *content = (char*) odr_malloc(odr, *len);
1228         memcpy(content, buf_out, *len);
1229         
1230         xmlFree(buf_out);
1231         xmlFreeDoc(doc);
1232         return content;
1233     }
1234 #endif
1235     return 0;
1236 }
1237
1238 void Yaz_ProxyConfig::get_target_info(const char *name,
1239                                       const char **url,
1240                                       int *limit_bw,
1241                                       int *limit_pdu,
1242                                       int *limit_req,
1243                                       int *target_idletime,
1244                                       int *client_idletime,
1245                                       int *max_clients,
1246                                       int *keepalive_limit_bw,
1247                                       int *keepalive_limit_pdu,
1248                                       int *pre_init,
1249                                       const char **cql2rpn,
1250                                       const char **negotiation_charset,
1251                                       const char **negotiation_lang)
1252 {
1253 #if HAVE_XSLT
1254     xmlNodePtr ptr;
1255     if (!m_cp->m_proxyPtr)
1256     {
1257         url[0] = name;
1258         url[1] = 0;
1259         return;
1260     }
1261     url[0] = 0;
1262     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
1263     {
1264         if (ptr->type == XML_ELEMENT_NODE &&
1265             !strcmp((const char *) ptr->name, "max-clients"))
1266         {
1267             const char *t = m_cp->get_text(ptr);
1268             if (t)
1269             {
1270                 *max_clients = atoi(t);
1271                 if (*max_clients  < 1)
1272                     *max_clients = 1;
1273             }
1274         }
1275     }
1276     ptr = m_cp->find_target_node(name, 0);
1277     if (ptr)
1278     {
1279         if (name)
1280         {
1281             url[0] = name;
1282             url[1] = 0;
1283         }
1284         m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
1285                                  target_idletime, client_idletime,
1286                                  keepalive_limit_bw, keepalive_limit_pdu,
1287                                  pre_init, cql2rpn,
1288                                  negotiation_charset, negotiation_lang);
1289     }
1290 #else
1291     *url = name;
1292     return;
1293 #endif
1294 }
1295
1296