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