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