Version 1.3.9
[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                 else 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                 else 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                 else 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                 else 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                 else 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                 else 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                 else 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                 else 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                 else 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                 else
829                 {
830                     yaz_log(YLOG_WARN, "0 Unknown attribute %s in <syntax>",
831                             (const char *) attr->name);
832                 }
833             }
834             if (match_type)
835             {
836                 if (!strcmp(match_type, "*"))
837                     match = 1;
838                 else if (!strcmp(match_type, "none"))
839                 {
840                     if (syntax == 0)
841                         match = 1;
842                 }
843                 else if (syntax)
844                 {
845                     Odr_oid *match_oid
846                         = yaz_string_to_oid_odr(yaz_oid_std(),
847                                                 CLASS_RECSYN, match_type,
848                                                 odr);
849                     if (oid_oidcmp(match_oid, syntax) == 0)
850                         match = 1;
851                 }
852             }
853             if (match)
854             {
855                 if (!match_error)
856                     syntax_has_matched = 1;
857                 match = m_cp->check_schema(ptr->children, comp,
858                                            match_identifier);
859             }
860             if (match)
861             {
862                 if (stylesheet && match_stylesheet)
863                 {
864                     xfree(*stylesheet);
865                     *stylesheet = xstrdup(match_stylesheet);
866                 }
867                 if (schema && match_identifier)
868                 {
869                     xfree(*schema);
870                     *schema = xstrdup(match_identifier);
871                 }
872                 if (backend_type && match_backend_type)
873                 {
874                     xfree(*backend_type);
875                     *backend_type = xstrdup(match_backend_type);
876                 }
877                 if (backend_charset && match_backend_charset)
878                 {
879                     xfree(*backend_charset);
880                     *backend_charset = xstrdup(match_backend_charset);
881                 }
882                 if (match_elementset)
883                 {
884                     xfree(*backend_elementset);
885                     *backend_elementset = xstrdup(match_elementset);
886                 }
887                 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage1)
888                 {
889                     xfree(*usemarcon_ini_stage1);
890                     *usemarcon_ini_stage1 = xstrdup(match_usemarcon_ini_stage1);
891                 }
892                 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage2)
893                 {
894                     xfree(*usemarcon_ini_stage2);
895                     *usemarcon_ini_stage2 = xstrdup(match_usemarcon_ini_stage2);
896                 }
897                 if (match_marcxml)
898                 {
899                     return -1;
900                 }
901                 if (match_error)
902                 {
903                     if (syntax_has_matched)  // if syntax OK, bad schema/ESN
904                         return 25;
905                     if (syntax)
906                     {
907                         char dotoid_str[OID_STR_MAX];
908                         oid_oid_to_dotstring(syntax, dotoid_str);
909                         *addinfo = odr_strdup(odr, dotoid_str);
910                     }
911                     return atoi(match_error);
912                 }
913                 return 0;
914             }
915         }
916     }
917 #endif
918     return 0;
919 }
920
921
922 #if YAZ_HAVE_XSLT
923
924 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name)
925 {
926     /* db seems always to be passed as NULL */
927     xmlNodePtr ptr;
928     if (!m_proxyPtr)
929         return 0;
930     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
931     {
932         if (ptr->type == XML_ELEMENT_NODE &&
933             !strcmp((const char *) ptr->name, "target"))
934         {
935             // default one ?
936             if (!name)
937             {
938                 // <target default="1"> ?
939                 struct _xmlAttr *attr;
940                 for (attr = ptr->properties; attr; attr = attr->next)
941                     if (!strcmp((const char *) attr->name, "default") &&
942                         attr->children && attr->children->type == XML_TEXT_NODE)
943                     {
944                         xmlChar *t = attr->children->content;
945                         if (!t || *t == '1')
946                             return ptr;
947                     }
948             }
949             else
950             {
951                 // <target name="name"> ?
952                 struct _xmlAttr *attr;
953                 for (attr = ptr->properties; attr; attr = attr->next)
954                     if (!strcmp((const char *) attr->name, "name"))
955                     {
956                         if (attr->children
957                             && attr->children->type==XML_TEXT_NODE
958                             && attr->children->content
959                             && (!strcmp((const char *) attr->children->content,
960                                         name)
961                                 || !strcmp((const char *) attr->children->content,
962                                            "*")))
963                         {
964                             return ptr;
965                         }
966                     }
967             }
968         }
969     }
970     return 0;
971 }
972 #endif
973
974 int Yaz_ProxyConfig::get_target_no(int no,
975                                    const char **name,
976                                    const char **url,
977                                    int *limit_bw,
978                                    int *limit_pdu,
979                                    int *limit_req,
980                                    int *limit_search,
981                                    int *target_idletime,
982                                    int *client_idletime,
983                                    int *max_sockets,
984                                    int *max_clients,
985                                    int *keepalive_limit_bw,
986                                    int *keepalive_limit_pdu,
987                                    int *pre_init,
988                                    const char **cql2rpn,
989                                    const char **authentication,
990                                    const char **negotiation_charset,
991                                    const char **negotiation_lang,
992                                    const char **target_charset,
993                                    const char **default_client_query_charset)
994 {
995 #if YAZ_HAVE_XSLT
996     xmlNodePtr ptr;
997     if (!m_cp->m_proxyPtr)
998         return 0;
999     int i = 0;
1000     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
1001         if (ptr->type == XML_ELEMENT_NODE &&
1002             !strcmp((const char *) ptr->name, "target"))
1003         {
1004             if (i == no)
1005             {
1006                 struct _xmlAttr *attr;
1007                 for (attr = ptr->properties; attr; attr = attr->next)
1008                     if (!strcmp((const char *) attr->name, "name"))
1009                     {
1010                         if (attr->children
1011                             && attr->children->type==XML_TEXT_NODE
1012                             && attr->children->content)
1013                             *name = (const char *) attr->children->content;
1014                     }
1015                 m_cp->return_target_info(
1016                     ptr, url,
1017                     limit_bw, limit_pdu, limit_req,
1018                     limit_search,
1019                     target_idletime, client_idletime,
1020                     max_sockets,
1021                     keepalive_limit_bw, keepalive_limit_pdu,
1022                     pre_init, cql2rpn,
1023                     negotiation_charset, negotiation_lang, target_charset,
1024                     default_client_query_charset);
1025                 return 1;
1026             }
1027             i++;
1028         }
1029 #endif
1030     return 0;
1031 }
1032
1033 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
1034 {
1035     if (len == strlen(item) && memcmp(hay, item, len) == 0)
1036         return 1;
1037     return 0;
1038 }
1039
1040 int Yaz_ProxyConfig::get_file_access_info(const char *path)
1041 {
1042 #if YAZ_HAVE_XSLT
1043     xmlNodePtr ptr;
1044     if (!m_cp->m_proxyPtr)
1045         return 0;
1046     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
1047     {
1048         if (ptr->type == XML_ELEMENT_NODE
1049             && !strcmp((const char *) ptr->name, "docpath"))
1050         {
1051             const char *docpath = m_cp->get_text(ptr);
1052             size_t docpath_len = strlen(docpath);
1053             if (docpath_len < strlen(path) && path[docpath_len] == '/'
1054                 && !memcmp(docpath, path, docpath_len))
1055                 return 1;
1056         }
1057     }
1058 #endif
1059     return 0;
1060 }
1061
1062 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
1063                                        int *max_clients,
1064                                        int *max_connect,
1065                                        int *limit_connect,
1066                                        int *period_connect,
1067                                        int *num_msg_threads)
1068 {
1069     *max_connect = 0;
1070     *limit_connect = 0;
1071     *num_msg_threads = 0;
1072 #if YAZ_HAVE_XSLT
1073     xmlNodePtr ptr;
1074     if (!m_cp->m_proxyPtr)
1075         return;
1076     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
1077     {
1078         if (ptr->type == XML_ELEMENT_NODE
1079             && !strcmp((const char *) ptr->name, "log"))
1080         {
1081             const char *v = m_cp->get_text(ptr);
1082             *log_mask = 0;
1083             while (v && *v)
1084             {
1085                 const char *cp = v;
1086                 while (*cp && *cp != ',' && !isspace(*cp))
1087                     cp++;
1088                 size_t len = cp - v;
1089                 if (m_cp->mycmp(v, "client-apdu", len))
1090                     *log_mask |= PROXY_LOG_APDU_CLIENT;
1091                 if (m_cp->mycmp(v, "server-apdu", len))
1092                     *log_mask |= PROXY_LOG_APDU_SERVER;
1093                 if (m_cp->mycmp(v, "client-requests", len))
1094                     *log_mask |= PROXY_LOG_REQ_CLIENT;
1095                 if (m_cp->mycmp(v, "server-requests", len))
1096                     *log_mask |= PROXY_LOG_REQ_SERVER;
1097                 if (m_cp->mycmp(v, "client-ip", len))
1098                     *log_mask |= PROXY_LOG_IP_CLIENT;
1099                 if (isdigit(*v))
1100                     *log_mask |= atoi(v);
1101                 if (*cp == ',')
1102                     cp++;
1103                 while (*cp && isspace(*cp))
1104                     cp++;
1105                 v = cp;
1106             }
1107         }
1108         else if (ptr->type == XML_ELEMENT_NODE &&
1109             !strcmp((const char *) ptr->name, "max-clients"))
1110         {
1111             const char *t = m_cp->get_text(ptr);
1112             if (t)
1113             {
1114                 *max_clients = atoi(t);
1115                 if (*max_clients  < 1)
1116                     *max_clients = 1;
1117             }
1118         }
1119         else if (ptr->type == XML_ELEMENT_NODE &&
1120             !strcmp((const char *) ptr->name, "period-connect"))
1121         {
1122             const char *t = m_cp->get_text(ptr);
1123             if (t)
1124                 *period_connect = atoi(t);
1125         }
1126         else if (ptr->type == XML_ELEMENT_NODE &&
1127             !strcmp((const char *) ptr->name, "max-connect"))
1128         {
1129             const char *t = m_cp->get_text(ptr);
1130             if (t)
1131             {
1132                 *max_connect = atoi(t);
1133             }
1134         }
1135         else if (ptr->type == XML_ELEMENT_NODE &&
1136             !strcmp((const char *) ptr->name, "limit-connect"))
1137         {
1138             const char *t = m_cp->get_text(ptr);
1139             if (t)
1140             {
1141                 *limit_connect = atoi(t);
1142             }
1143         }
1144         else if (ptr->type == XML_ELEMENT_NODE &&
1145             !strcmp((const char *) ptr->name, "target"))
1146             ;
1147         else if (ptr->type == XML_ELEMENT_NODE &&
1148             !strcmp((const char *) ptr->name, "docpath"))
1149             ;
1150         else if (ptr->type == XML_ELEMENT_NODE &&
1151             !strcmp((const char *) ptr->name, "module"))
1152             ;
1153         else if (ptr->type == XML_ELEMENT_NODE &&
1154             !strcmp((const char *) ptr->name, "client-authentication"))
1155             ;
1156         else if (ptr->type == XML_ELEMENT_NODE &&
1157             !strcmp((const char *) ptr->name, "threads"))
1158         {
1159             const char *t = m_cp->get_text(ptr);
1160             if (t)
1161             {
1162                 *num_msg_threads = atoi(t);
1163             }
1164         }
1165         else if (ptr->type == XML_ELEMENT_NODE)
1166         {
1167             yaz_log(YLOG_WARN, "0 Unknown element %s in yazproxy config",
1168                     ptr->name);
1169         }
1170     }
1171 #endif
1172 }
1173
1174 #if YAZ_HAVE_XSLT
1175 int Yaz_ProxyConfigP::get_explain_ptr(const char *db,
1176                                       xmlNodePtr *ptr_target,
1177                                       xmlNodePtr *ptr_explain)
1178 {
1179     xmlNodePtr ptr;
1180     if (!m_proxyPtr)
1181         return 0;
1182     if (!db)
1183         return 0;
1184     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
1185     {
1186         if (ptr->type == XML_ELEMENT_NODE &&
1187             !strcmp((const char *) ptr->name, "target"))
1188         {
1189             int db_match_on_name = 0;
1190             struct _xmlAttr *attr;
1191
1192             for (attr = ptr->properties; attr; attr = attr->next)
1193                 if (!strcmp((const char *) attr->name, "name"))
1194                 {
1195                     if (attr->children
1196                         && attr->children->type==XML_TEXT_NODE
1197                         && attr->children->content
1198                         && (!strcmp((const char *) attr->children->content,
1199                                     db)))
1200                         db_match_on_name = 1;
1201                 }
1202             *ptr_target = ptr;
1203             xmlNodePtr ptr = (*ptr_target)->children;
1204             for (; ptr; ptr = ptr->next)
1205             {
1206                 if (ptr->type == XML_ELEMENT_NODE &&
1207                     !strcmp((const char *) ptr->name, "explain"))
1208                 {
1209                     *ptr_explain = ptr;
1210                     xmlNodePtr ptr = (*ptr_explain)->children;
1211
1212                     for (; ptr; ptr = ptr->next)
1213                         if (ptr->type == XML_ELEMENT_NODE &&
1214                             !strcmp((const char *) ptr->name, "serverInfo"))
1215                             break;
1216                     if (!ptr)
1217                         continue;
1218                     for (ptr = ptr->children; ptr; ptr = ptr->next)
1219                         if (ptr->type == XML_ELEMENT_NODE &&
1220                             !strcmp((const char *) ptr->name, "database"))
1221                             break;
1222
1223                     if (!ptr)
1224                         continue;
1225                     for (ptr = ptr->children; ptr; ptr = ptr->next)
1226                         if (ptr->type == XML_TEXT_NODE &&
1227                             ptr->content &&
1228                             !strcmp((const char *) ptr->content, db))
1229                             break;
1230                     if (!ptr)
1231                         continue;
1232                     return 1;
1233                 }
1234             }
1235             if (db_match_on_name)
1236                 return 1;
1237         }
1238     }
1239     return 0;
1240 }
1241 #endif
1242
1243 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
1244                                               const char **backend_db)
1245 {
1246 #if YAZ_HAVE_XSLT
1247     xmlNodePtr ptr_target, ptr_explain;
1248     if (m_cp->get_explain_ptr(db, &ptr_target, &ptr_explain)
1249         && ptr_target)
1250     {
1251         struct _xmlAttr *attr;
1252         const char *name = 0;
1253
1254         for (attr = ptr_target->properties; attr; attr = attr->next)
1255             if (!strcmp((const char *) attr->name, "name")
1256                 && attr->children
1257                 && attr->children->type==XML_TEXT_NODE
1258                 && attr->children->content
1259                 && attr->children->content[0])
1260             {
1261                 name = (const char *)attr->children->content;
1262                 break;
1263             }
1264         if (name)
1265         {
1266             for (attr = ptr_target->properties; attr; attr = attr->next)
1267                 if (!strcmp((const char *) attr->name, "database"))
1268                 {
1269                     if (attr->children
1270                         && attr->children->type==XML_TEXT_NODE
1271                         && attr->children->content)
1272                         *backend_db = (const char *) attr->children->content;
1273                 }
1274             return name;
1275         }
1276     }
1277 #endif
1278     return 0;
1279 }
1280
1281 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
1282                                        const char *db, int *len,
1283                                        int *http_status)
1284 {
1285 #if YAZ_HAVE_XSLT
1286     xmlNodePtr ptr_target, ptr_explain;
1287     if (m_cp->get_explain_ptr(db, &ptr_target, &ptr_explain))
1288     {
1289         if (!ptr_explain)
1290         {
1291             *http_status = 500;
1292             return 0;
1293         }
1294         else
1295         {
1296             xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
1297
1298             xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
1299
1300             xmlDocSetRootElement(doc, ptr2);
1301
1302             xmlChar *buf_out;
1303             xmlDocDumpMemory(doc, &buf_out, len);
1304             char *content = (char*) odr_malloc(odr, *len);
1305             memcpy(content, buf_out, *len);
1306
1307             xmlFree(buf_out);
1308             xmlFreeDoc(doc);
1309             return content;
1310         }
1311     }
1312 #endif
1313     *http_status = 404;
1314     return 0;
1315 }
1316
1317 void Yaz_ProxyConfig::get_target_info(const char *name,
1318                                       const char **url,
1319                                       int *limit_bw,
1320                                       int *limit_pdu,
1321                                       int *limit_req,
1322                                       int *limit_search,
1323                                       int *target_idletime,
1324                                       int *client_idletime,
1325                                       int *max_sockets,
1326                                       int *max_clients,
1327                                       int *keepalive_limit_bw,
1328                                       int *keepalive_limit_pdu,
1329                                       int *pre_init,
1330                                       const char **cql2rpn,
1331                                       const char **negotiation_charset,
1332                                       const char **negotiation_lang,
1333                                       const char **target_charset,
1334                                       const char **default_client_query_charset)
1335 {
1336 #if YAZ_HAVE_XSLT
1337     xmlNodePtr ptr;
1338     if (!m_cp->m_proxyPtr)
1339     {
1340         url[0] = name;
1341         url[1] = 0;
1342         return;
1343     }
1344     url[0] = 0;
1345     for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
1346     {
1347         if (ptr->type == XML_ELEMENT_NODE &&
1348             !strcmp((const char *) ptr->name, "max-clients"))
1349         {
1350             const char *t = m_cp->get_text(ptr);
1351             if (t)
1352             {
1353                 *max_clients = atoi(t);
1354                 if (*max_clients  < 1)
1355                     *max_clients = 1;
1356             }
1357         }
1358     }
1359     ptr = m_cp->find_target_node(name);
1360     if (ptr)
1361     {
1362         if (name)
1363         {
1364             url[0] = name;
1365             url[1] = 0;
1366         }
1367         m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
1368                                  limit_search,
1369                                  target_idletime, client_idletime,
1370                                  max_sockets,
1371                                  keepalive_limit_bw, keepalive_limit_pdu,
1372                                  pre_init, cql2rpn,
1373                                  negotiation_charset, negotiation_lang,
1374                                  target_charset,
1375                                  default_client_query_charset);
1376     }
1377 #else
1378     *url = name;
1379     return;
1380 #endif
1381 }
1382
1383
1384 /*
1385  * Local variables:
1386  * c-basic-offset: 4
1387  * c-file-style: "Stroustrup"
1388  * indent-tabs-mode: nil
1389  * End:
1390  * vim: shiftwidth=4 tabstop=8 expandtab
1391  */
1392