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