1 /* $Id: yaz-proxy-config.cpp,v 1.12 2004-12-13 20:52:33 adam Exp $
2 Copyright (c) 1998-2004, Index Data.
4 This file is part of the yaz-proxy.
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 class Yaz_ProxyConfigP {
27 friend class Yaz_ProxyConfig;
31 int mycmp(const char *hay, const char *item, size_t len);
32 int match_list(int v, const char *m);
33 int atoi_l(const char **cp);
35 int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
36 const char *schema_identifier);
38 xmlNodePtr m_proxyPtr;
39 void return_target_info(xmlNodePtr ptr, const char **url,
40 int *limit_bw, int *limit_pdu, int *limit_req,
41 int *target_idletime, int *client_idletime,
42 int *keepalive_limit_bw, int *keepalive_limit_pdu,
43 int *pre_init, const char **cql2rpn,
44 const char **authentication);
45 void return_limit(xmlNodePtr ptr,
46 int *limit_bw, int *limit_pdu, int *limit_req);
47 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
49 xmlNodePtr find_target_node(const char *name, const char *db);
50 xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
51 const char *get_text(xmlNodePtr ptr);
52 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
53 Z_AttributeList *attrs,
55 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
57 int get_explain_ptr(const char *host, const char *db,
58 xmlNodePtr *ptr_target, xmlNodePtr *ptr_explain);
62 Yaz_ProxyConfig::Yaz_ProxyConfig()
64 m_cp = new Yaz_ProxyConfigP;
72 Yaz_ProxyConfig::~Yaz_ProxyConfig()
75 if (!m_cp->m_copy && m_cp->m_docPtr)
76 xmlFreeDoc(m_cp->m_docPtr);
81 int Yaz_ProxyConfig::read_xml(const char *fname)
84 xmlDocPtr ndoc = xmlParseFile(fname);
88 yaz_log(YLOG_WARN, "Config file %s not found or parse error", fname);
91 int noSubstitutions = xmlXIncludeProcess(ndoc);
92 if (noSubstitutions == -1)
93 yaz_log(YLOG_WARN, "XInclude processing failed on config %s", fname);
95 xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
96 if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
97 strcmp((const char *) proxyPtr->name, "proxy"))
99 yaz_log(YLOG_WARN, "No proxy element in %s", fname);
103 m_cp->m_proxyPtr = proxyPtr;
105 // OK: release previous and make it the current one.
107 xmlFreeDoc(m_cp->m_docPtr);
108 m_cp->m_docPtr = ndoc;
116 const char *Yaz_ProxyConfigP::get_text(xmlNodePtr ptr)
118 for(ptr = ptr->children; ptr; ptr = ptr->next)
119 if (ptr->type == XML_TEXT_NODE)
121 xmlChar *t = ptr->content;
126 return (const char *) t;
134 void Yaz_ProxyConfigP::return_limit(xmlNodePtr ptr,
139 for (ptr = ptr->children; ptr; ptr = ptr->next)
141 if (ptr->type == XML_ELEMENT_NODE
142 && !strcmp((const char *) ptr->name, "bandwidth"))
144 const char *t = get_text(ptr);
148 if (ptr->type == XML_ELEMENT_NODE
149 && !strcmp((const char *) ptr->name, "retrieve"))
151 const char *t = get_text(ptr);
153 *limit_req = atoi(t);
155 if (ptr->type == XML_ELEMENT_NODE
156 && !strcmp((const char *) ptr->name, "pdu"))
158 const char *t = get_text(ptr);
160 *limit_pdu = atoi(t);
167 void Yaz_ProxyConfigP::return_target_info(xmlNodePtr ptr,
172 int *target_idletime,
173 int *client_idletime,
174 int *keepalive_limit_bw,
175 int *keepalive_limit_pdu,
177 const char **cql2rpn,
178 const char **authentication)
183 for (; ptr; ptr = ptr->next)
185 if (ptr->type == XML_ELEMENT_NODE
186 && !strcmp((const char *) ptr->name, "preinit"))
188 const char *v = get_text(ptr);
189 *pre_init = v ? atoi(v) : 1;
191 if (ptr->type == XML_ELEMENT_NODE
192 && !strcmp((const char *) ptr->name, "url"))
194 const char *t = get_text(ptr);
195 if (t && no_url < MAX_ZURL_PLEX)
201 if (ptr->type == XML_ELEMENT_NODE
202 && !strcmp((const char *) ptr->name, "keepalive"))
205 *keepalive_limit_bw = 500000;
206 *keepalive_limit_pdu = 1000;
207 return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
210 if (ptr->type == XML_ELEMENT_NODE
211 && !strcmp((const char *) ptr->name, "limit"))
212 return_limit(ptr, limit_bw, limit_pdu, limit_req);
213 if (ptr->type == XML_ELEMENT_NODE
214 && !strcmp((const char *) ptr->name, "target-timeout"))
216 const char *t = get_text(ptr);
219 *target_idletime = atoi(t);
220 if (*target_idletime < 0)
221 *target_idletime = 0;
224 if (ptr->type == XML_ELEMENT_NODE
225 && !strcmp((const char *) ptr->name, "client-timeout"))
227 const char *t = get_text(ptr);
230 *client_idletime = atoi(t);
231 if (*client_idletime < 0)
232 *client_idletime = 0;
235 if (ptr->type == XML_ELEMENT_NODE
236 && !strcmp((const char *) ptr->name, "cql2rpn"))
238 const char *t = get_text(ptr);
242 if (ptr->type == XML_ELEMENT_NODE
243 && !strcmp((const char *) ptr->name, "authentication"))
245 const char *t = get_text(ptr);
253 int Yaz_ProxyConfigP::atoi_l(const char **cp)
256 while (**cp && isdigit(**cp))
258 v = v*10 + (**cp - '0');
264 int Yaz_ProxyConfigP::match_list(int v, const char *m)
268 while(*m && isspace(*m))
279 if (v >= l && v <= h)
288 int Yaz_ProxyConfigP::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
289 Z_AttributeList *attrs,
293 for (i = 0; i<attrs->num_attributes; i++)
295 Z_AttributeElement *el = attrs->attributes[i];
297 if (!el->attributeType)
299 int type = *el->attributeType;
302 if (el->which == Z_AttributeValue_numeric && el->value.numeric)
303 value = el->value.numeric;
306 for(ptr = ptrl->children; ptr; ptr = ptr->next)
308 if (ptr->type == XML_ELEMENT_NODE &&
309 !strcmp((const char *) ptr->name, "attribute"))
311 const char *match_type = 0;
312 const char *match_value = 0;
313 const char *match_error = 0;
314 struct _xmlAttr *attr;
315 for (attr = ptr->properties; attr; attr = attr->next)
317 if (!strcmp((const char *) attr->name, "type") &&
318 attr->children && attr->children->type == XML_TEXT_NODE)
319 match_type = (const char *) attr->children->content;
320 if (!strcmp((const char *) attr->name, "value") &&
321 attr->children && attr->children->type == XML_TEXT_NODE)
322 match_value = (const char *) attr->children->content;
323 if (!strcmp((const char *) attr->name, "error") &&
324 attr->children && attr->children->type == XML_TEXT_NODE)
325 match_error = (const char *) attr->children->content;
327 if (match_type && match_value)
329 char addinfo_str[20];
330 if (!match_list(type, match_type))
334 if (!strcmp(match_type, "*"))
335 sprintf (addinfo_str, "%d", type);
338 if (!match_list(*value, match_value))
340 sprintf (addinfo_str, "%d", *value);
348 *addinfo = odr_strdup(odr, addinfo_str);
349 return atoi(match_error);
361 int Yaz_ProxyConfigP::check_type_1_structure(ODR odr, xmlNodePtr ptr,
365 if (q->which == Z_RPNStructure_complex)
367 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
370 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
373 else if (q->which == Z_RPNStructure_simple)
375 if (q->u.simple->which == Z_Operand_APT)
377 return check_type_1_attributes(
378 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
387 int Yaz_ProxyConfigP::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
390 // possibly check for Bib-1
391 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
395 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
401 ptr = m_cp->find_target_node(name, 0);
404 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
405 return m_cp->check_type_1(odr, ptr, query->u.type_1, addinfo);
412 int Yaz_ProxyConfigP::check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
413 const char *schema_identifier)
416 int default_match = 1;
417 if (comp && comp->which == Z_RecordComp_simple &&
418 comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
420 esn = comp->u.simple->u.generic;
422 // if no ESN/schema was given accept..
425 // check if schema identifier match
426 if (schema_identifier && !strcmp(esn, schema_identifier))
428 // Check each name element
429 for (; ptr; ptr = ptr->next)
431 if (ptr->type == XML_ELEMENT_NODE
432 && !strcmp((const char *) ptr->name, "name"))
434 xmlNodePtr tptr = ptr->children;
436 for (; tptr; tptr = tptr->next)
437 if (tptr->type == XML_TEXT_NODE && tptr->content)
439 xmlChar *t = tptr->content;
440 while (*t && isspace(*t))
443 while (esn[i] && esn[i] == t[i])
445 if (!esn[i] && (!t[i] || isspace(t[i])))
450 return default_match;
454 const char *Yaz_ProxyConfig::check_mime_type(const char *path)
460 {".xml", "text/xml"},
461 {".xsl", "text/xml"},
462 {".tkl", "text/xml"},
463 {".xsd", "text/xml"},
468 size_t plen = strlen (path);
469 for (i = 0; types[i].type; i++)
470 if (types[i].mask == 0)
471 return types[i].type;
474 size_t mlen = strlen(types[i].mask);
475 if (plen > mlen && !memcmp(path+plen-mlen, types[i].mask, mlen))
476 return types[i].type;
478 return "application/octet-stream";
482 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
483 Odr_oid *syntax, Z_RecordComposition *comp,
485 char **stylesheet, char **schema,
487 char **backend_charset,
488 char **usemarcon_ini_stage1,
489 char **usemarcon_ini_stage2
504 xfree (*backend_type);
509 xfree (*backend_charset);
510 *backend_charset = 0;
512 if (usemarcon_ini_stage1)
514 xfree (*usemarcon_ini_stage1);
515 *usemarcon_ini_stage1 = 0;
517 if (usemarcon_ini_stage2)
519 xfree (*usemarcon_ini_stage2);
520 *usemarcon_ini_stage2 = 0;
523 int syntax_has_matched = 0;
526 ptr = m_cp->find_target_node(name, 0);
529 for(ptr = ptr->children; ptr; ptr = ptr->next)
531 if (ptr->type == XML_ELEMENT_NODE &&
532 !strcmp((const char *) ptr->name, "syntax"))
534 int match = 0; // if we match record syntax
535 const char *match_type = 0;
536 const char *match_error = 0;
537 const char *match_marcxml = 0;
538 const char *match_stylesheet = 0;
539 const char *match_identifier = 0;
540 const char *match_backend_type = 0;
541 const char *match_backend_charset = 0;
542 const char *match_usemarcon_ini_stage1 = 0;
543 const char *match_usemarcon_ini_stage2 = 0;
544 struct _xmlAttr *attr;
545 for (attr = ptr->properties; attr; attr = attr->next)
547 if (!strcmp((const char *) attr->name, "type") &&
548 attr->children && attr->children->type == XML_TEXT_NODE)
549 match_type = (const char *) attr->children->content;
550 if (!strcmp((const char *) attr->name, "error") &&
551 attr->children && attr->children->type == XML_TEXT_NODE)
552 match_error = (const char *) attr->children->content;
553 if (!strcmp((const char *) attr->name, "marcxml") &&
554 attr->children && attr->children->type == XML_TEXT_NODE)
555 match_marcxml = (const char *) attr->children->content;
556 if (!strcmp((const char *) attr->name, "stylesheet") &&
557 attr->children && attr->children->type == XML_TEXT_NODE)
558 match_stylesheet = (const char *) attr->children->content;
559 if (!strcmp((const char *) attr->name, "identifier") &&
560 attr->children && attr->children->type == XML_TEXT_NODE)
561 match_identifier = (const char *) attr->children->content;
562 if (!strcmp((const char *) attr->name, "backendtype") &&
563 attr->children && attr->children->type == XML_TEXT_NODE)
564 match_backend_type = (const char *)
565 attr->children->content;
566 if (!strcmp((const char *) attr->name, "backendcharset") &&
567 attr->children && attr->children->type == XML_TEXT_NODE)
568 match_backend_charset = (const char *)
569 attr->children->content;
570 if (!strcmp((const char *) attr->name, "usemarconstage1") &&
571 attr->children && attr->children->type == XML_TEXT_NODE)
572 match_usemarcon_ini_stage1 = (const char *)
573 attr->children->content;
574 if (!strcmp((const char *) attr->name, "usemarconstage2") &&
575 attr->children && attr->children->type == XML_TEXT_NODE)
576 match_usemarcon_ini_stage2 = (const char *)
577 attr->children->content;
581 if (!strcmp(match_type, "*"))
583 else if (!strcmp(match_type, "none"))
590 int match_oid[OID_SIZE];
591 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
592 if (oid_oidcmp(match_oid, syntax) == 0)
599 syntax_has_matched = 1;
600 match = m_cp->check_schema(ptr->children, comp,
605 if (stylesheet && match_stylesheet)
608 *stylesheet = xstrdup(match_stylesheet);
610 if (schema && match_identifier)
613 *schema = xstrdup(match_identifier);
615 if (backend_type && match_backend_type)
617 xfree(*backend_type);
618 *backend_type = xstrdup(match_backend_type);
620 if (backend_charset && match_backend_charset)
622 xfree(*backend_charset);
623 *backend_charset = xstrdup(match_backend_charset);
625 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage1)
627 xfree(*usemarcon_ini_stage1);
628 *usemarcon_ini_stage1 = xstrdup(match_usemarcon_ini_stage1);
630 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage2)
632 xfree(*usemarcon_ini_stage2);
633 *usemarcon_ini_stage2 = xstrdup(match_usemarcon_ini_stage2);
641 if (syntax_has_matched) // if syntax OK, bad schema/ESN
645 char dotoid_str[100];
646 oid_to_dotstring(syntax, dotoid_str);
647 *addinfo = odr_strdup(odr, dotoid_str);
649 return atoi(match_error);
660 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
667 for (dptr = ptr->children; dptr; dptr = dptr->next)
668 if (dptr->type == XML_ELEMENT_NODE &&
669 !strcmp((const char *) dptr->name, "database"))
671 struct _xmlAttr *attr;
672 for (attr = dptr->properties; attr; attr = attr->next)
673 if (!strcmp((const char *) attr->name, "name"))
676 && attr->children->type==XML_TEXT_NODE
677 && attr->children->content
678 && (!strcmp((const char *) attr->children->content, db)
679 || !strcmp((const char *) attr->children->content,
687 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
692 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
694 if (ptr->type == XML_ELEMENT_NODE &&
695 !strcmp((const char *) ptr->name, "target"))
700 // <target default="1"> ?
701 struct _xmlAttr *attr;
702 for (attr = ptr->properties; attr; attr = attr->next)
703 if (!strcmp((const char *) attr->name, "default") &&
704 attr->children && attr->children->type == XML_TEXT_NODE)
706 xmlChar *t = attr->children->content;
709 return find_target_db(ptr, db);
715 // <target name="name"> ?
716 struct _xmlAttr *attr;
717 for (attr = ptr->properties; attr; attr = attr->next)
718 if (!strcmp((const char *) attr->name, "name"))
721 && attr->children->type==XML_TEXT_NODE
722 && attr->children->content
723 && (!strcmp((const char *) attr->children->content,
725 || !strcmp((const char *) attr->children->content,
728 return find_target_db(ptr, db);
738 int Yaz_ProxyConfig::get_target_no(int no,
744 int *target_idletime,
745 int *client_idletime,
747 int *keepalive_limit_bw,
748 int *keepalive_limit_pdu,
750 const char **cql2rpn,
751 const char **authentication)
755 if (!m_cp->m_proxyPtr)
758 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
759 if (ptr->type == XML_ELEMENT_NODE &&
760 !strcmp((const char *) ptr->name, "target"))
764 struct _xmlAttr *attr;
765 for (attr = ptr->properties; attr; attr = attr->next)
766 if (!strcmp((const char *) attr->name, "name"))
769 && attr->children->type==XML_TEXT_NODE
770 && attr->children->content)
771 *name = (const char *) attr->children->content;
773 m_cp->return_target_info(
775 limit_bw, limit_pdu, limit_req,
776 target_idletime, client_idletime,
777 keepalive_limit_bw, keepalive_limit_pdu,
778 pre_init, cql2rpn, authentication);
787 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
789 if (len == strlen(item) && memcmp(hay, item, len) == 0)
794 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
799 if (!m_cp->m_proxyPtr)
801 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
803 if (ptr->type == XML_ELEMENT_NODE
804 && !strcmp((const char *) ptr->name, "log"))
806 const char *v = m_cp->get_text(ptr);
811 while (*cp && *cp != ',' && !isspace(*cp))
814 if (m_cp->mycmp(v, "client-apdu", len))
815 *log_mask |= PROXY_LOG_APDU_CLIENT;
816 if (m_cp->mycmp(v, "server-apdu", len))
817 *log_mask |= PROXY_LOG_APDU_SERVER;
818 if (m_cp->mycmp(v, "client-requests", len))
819 *log_mask |= PROXY_LOG_REQ_CLIENT;
820 if (m_cp->mycmp(v, "server-requests", len))
821 *log_mask |= PROXY_LOG_REQ_SERVER;
823 *log_mask |= atoi(v);
826 while (*cp && isspace(*cp))
831 if (ptr->type == XML_ELEMENT_NODE &&
832 !strcmp((const char *) ptr->name, "max-clients"))
834 const char *t = m_cp->get_text(ptr);
837 *max_clients = atoi(t);
838 if (*max_clients < 1)
847 int Yaz_ProxyConfigP::get_explain_ptr(const char *host, const char *db,
848 xmlNodePtr *ptr_target,
849 xmlNodePtr *ptr_explain)
856 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
858 if (ptr->type == XML_ELEMENT_NODE &&
859 !strcmp((const char *) ptr->name, "target"))
862 xmlNodePtr ptr = (*ptr_target)->children;
863 for (; ptr; ptr = ptr->next)
865 if (ptr->type == XML_ELEMENT_NODE &&
866 !strcmp((const char *) ptr->name, "explain"))
869 xmlNodePtr ptr = (*ptr_explain)->children;
871 for (; ptr; ptr = ptr->next)
872 if (ptr->type == XML_ELEMENT_NODE &&
873 !strcmp((const char *) ptr->name, "serverInfo"))
877 for (ptr = ptr->children; ptr; ptr = ptr->next)
878 if (ptr->type == XML_ELEMENT_NODE &&
879 !strcmp((const char *) ptr->name, "database"))
884 for (ptr = ptr->children; ptr; ptr = ptr->next)
885 if (ptr->type == XML_TEXT_NODE &&
887 !strcmp((const char *) ptr->content, db))
900 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
901 const char **backend_db)
904 xmlNodePtr ptr_target, ptr_explain;
905 if (m_cp->get_explain_ptr(0, db, &ptr_target, &ptr_explain)
908 struct _xmlAttr *attr;
909 const char *name = 0;
911 for (attr = ptr_target->properties; attr; attr = attr->next)
912 if (!strcmp((const char *) attr->name, "name")
914 && attr->children->type==XML_TEXT_NODE
915 && attr->children->content
916 && attr->children->content[0])
918 name = (const char *)attr->children->content;
923 for (attr = ptr_target->properties; attr; attr = attr->next)
924 if (!strcmp((const char *) attr->name, "database"))
927 && attr->children->type==XML_TEXT_NODE
928 && attr->children->content)
929 *backend_db = (const char *) attr->children->content;
938 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
939 const char *db, int *len)
942 xmlNodePtr ptr_target, ptr_explain;
943 if (m_cp->get_explain_ptr(0 /* host */, db, &ptr_target, &ptr_explain))
945 xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
947 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
949 xmlDocSetRootElement(doc, ptr2);
952 xmlDocDumpMemory(doc, &buf_out, len);
953 char *content = (char*) odr_malloc(odr, *len);
954 memcpy(content, buf_out, *len);
964 void Yaz_ProxyConfig::get_target_info(const char *name,
969 int *target_idletime,
970 int *client_idletime,
972 int *keepalive_limit_bw,
973 int *keepalive_limit_pdu,
975 const char **cql2rpn,
976 const char **authentication)
980 if (!m_cp->m_proxyPtr)
987 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
989 if (ptr->type == XML_ELEMENT_NODE &&
990 !strcmp((const char *) ptr->name, "max-clients"))
992 const char *t = m_cp->get_text(ptr);
995 *max_clients = atoi(t);
996 if (*max_clients < 1)
1001 ptr = m_cp->find_target_node(name, 0);
1009 m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
1010 target_idletime, client_idletime,
1011 keepalive_limit_bw, keepalive_limit_pdu,
1012 pre_init, cql2rpn, authentication);