1 /* $Id: yaz-proxy-config.cpp,v 1.13 2005-01-18 10:49:22 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"},
464 {".html", "text/html"},
465 {".jpg", "image/jpeg"},
466 {".png", "image/png"},
467 {".gif", "image/gif"},
472 size_t plen = strlen (path);
473 for (i = 0; types[i].type; i++)
474 if (types[i].mask == 0)
475 return types[i].type;
478 size_t mlen = strlen(types[i].mask);
479 if (plen > mlen && !memcmp(path+plen-mlen, types[i].mask, mlen))
480 return types[i].type;
482 return "application/octet-stream";
486 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
487 Odr_oid *syntax, Z_RecordComposition *comp,
489 char **stylesheet, char **schema,
491 char **backend_charset,
492 char **usemarcon_ini_stage1,
493 char **usemarcon_ini_stage2
508 xfree (*backend_type);
513 xfree (*backend_charset);
514 *backend_charset = 0;
516 if (usemarcon_ini_stage1)
518 xfree (*usemarcon_ini_stage1);
519 *usemarcon_ini_stage1 = 0;
521 if (usemarcon_ini_stage2)
523 xfree (*usemarcon_ini_stage2);
524 *usemarcon_ini_stage2 = 0;
527 int syntax_has_matched = 0;
530 ptr = m_cp->find_target_node(name, 0);
533 for(ptr = ptr->children; ptr; ptr = ptr->next)
535 if (ptr->type == XML_ELEMENT_NODE &&
536 !strcmp((const char *) ptr->name, "syntax"))
538 int match = 0; // if we match record syntax
539 const char *match_type = 0;
540 const char *match_error = 0;
541 const char *match_marcxml = 0;
542 const char *match_stylesheet = 0;
543 const char *match_identifier = 0;
544 const char *match_backend_type = 0;
545 const char *match_backend_charset = 0;
546 const char *match_usemarcon_ini_stage1 = 0;
547 const char *match_usemarcon_ini_stage2 = 0;
548 struct _xmlAttr *attr;
549 for (attr = ptr->properties; attr; attr = attr->next)
551 if (!strcmp((const char *) attr->name, "type") &&
552 attr->children && attr->children->type == XML_TEXT_NODE)
553 match_type = (const char *) attr->children->content;
554 if (!strcmp((const char *) attr->name, "error") &&
555 attr->children && attr->children->type == XML_TEXT_NODE)
556 match_error = (const char *) attr->children->content;
557 if (!strcmp((const char *) attr->name, "marcxml") &&
558 attr->children && attr->children->type == XML_TEXT_NODE)
559 match_marcxml = (const char *) attr->children->content;
560 if (!strcmp((const char *) attr->name, "stylesheet") &&
561 attr->children && attr->children->type == XML_TEXT_NODE)
562 match_stylesheet = (const char *) attr->children->content;
563 if (!strcmp((const char *) attr->name, "identifier") &&
564 attr->children && attr->children->type == XML_TEXT_NODE)
565 match_identifier = (const char *) attr->children->content;
566 if (!strcmp((const char *) attr->name, "backendtype") &&
567 attr->children && attr->children->type == XML_TEXT_NODE)
568 match_backend_type = (const char *)
569 attr->children->content;
570 if (!strcmp((const char *) attr->name, "backendcharset") &&
571 attr->children && attr->children->type == XML_TEXT_NODE)
572 match_backend_charset = (const char *)
573 attr->children->content;
574 if (!strcmp((const char *) attr->name, "usemarconstage1") &&
575 attr->children && attr->children->type == XML_TEXT_NODE)
576 match_usemarcon_ini_stage1 = (const char *)
577 attr->children->content;
578 if (!strcmp((const char *) attr->name, "usemarconstage2") &&
579 attr->children && attr->children->type == XML_TEXT_NODE)
580 match_usemarcon_ini_stage2 = (const char *)
581 attr->children->content;
585 if (!strcmp(match_type, "*"))
587 else if (!strcmp(match_type, "none"))
594 int match_oid[OID_SIZE];
595 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
596 if (oid_oidcmp(match_oid, syntax) == 0)
603 syntax_has_matched = 1;
604 match = m_cp->check_schema(ptr->children, comp,
609 if (stylesheet && match_stylesheet)
612 *stylesheet = xstrdup(match_stylesheet);
614 if (schema && match_identifier)
617 *schema = xstrdup(match_identifier);
619 if (backend_type && match_backend_type)
621 xfree(*backend_type);
622 *backend_type = xstrdup(match_backend_type);
624 if (backend_charset && match_backend_charset)
626 xfree(*backend_charset);
627 *backend_charset = xstrdup(match_backend_charset);
629 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage1)
631 xfree(*usemarcon_ini_stage1);
632 *usemarcon_ini_stage1 = xstrdup(match_usemarcon_ini_stage1);
634 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage2)
636 xfree(*usemarcon_ini_stage2);
637 *usemarcon_ini_stage2 = xstrdup(match_usemarcon_ini_stage2);
645 if (syntax_has_matched) // if syntax OK, bad schema/ESN
649 char dotoid_str[100];
650 oid_to_dotstring(syntax, dotoid_str);
651 *addinfo = odr_strdup(odr, dotoid_str);
653 return atoi(match_error);
664 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
671 for (dptr = ptr->children; dptr; dptr = dptr->next)
672 if (dptr->type == XML_ELEMENT_NODE &&
673 !strcmp((const char *) dptr->name, "database"))
675 struct _xmlAttr *attr;
676 for (attr = dptr->properties; attr; attr = attr->next)
677 if (!strcmp((const char *) attr->name, "name"))
680 && attr->children->type==XML_TEXT_NODE
681 && attr->children->content
682 && (!strcmp((const char *) attr->children->content, db)
683 || !strcmp((const char *) attr->children->content,
691 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
696 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
698 if (ptr->type == XML_ELEMENT_NODE &&
699 !strcmp((const char *) ptr->name, "target"))
704 // <target default="1"> ?
705 struct _xmlAttr *attr;
706 for (attr = ptr->properties; attr; attr = attr->next)
707 if (!strcmp((const char *) attr->name, "default") &&
708 attr->children && attr->children->type == XML_TEXT_NODE)
710 xmlChar *t = attr->children->content;
713 return find_target_db(ptr, db);
719 // <target name="name"> ?
720 struct _xmlAttr *attr;
721 for (attr = ptr->properties; attr; attr = attr->next)
722 if (!strcmp((const char *) attr->name, "name"))
725 && attr->children->type==XML_TEXT_NODE
726 && attr->children->content
727 && (!strcmp((const char *) attr->children->content,
729 || !strcmp((const char *) attr->children->content,
732 return find_target_db(ptr, db);
742 int Yaz_ProxyConfig::get_target_no(int no,
748 int *target_idletime,
749 int *client_idletime,
751 int *keepalive_limit_bw,
752 int *keepalive_limit_pdu,
754 const char **cql2rpn,
755 const char **authentication)
759 if (!m_cp->m_proxyPtr)
762 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
763 if (ptr->type == XML_ELEMENT_NODE &&
764 !strcmp((const char *) ptr->name, "target"))
768 struct _xmlAttr *attr;
769 for (attr = ptr->properties; attr; attr = attr->next)
770 if (!strcmp((const char *) attr->name, "name"))
773 && attr->children->type==XML_TEXT_NODE
774 && attr->children->content)
775 *name = (const char *) attr->children->content;
777 m_cp->return_target_info(
779 limit_bw, limit_pdu, limit_req,
780 target_idletime, client_idletime,
781 keepalive_limit_bw, keepalive_limit_pdu,
782 pre_init, cql2rpn, authentication);
791 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
793 if (len == strlen(item) && memcmp(hay, item, len) == 0)
798 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
803 if (!m_cp->m_proxyPtr)
805 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
807 if (ptr->type == XML_ELEMENT_NODE
808 && !strcmp((const char *) ptr->name, "log"))
810 const char *v = m_cp->get_text(ptr);
815 while (*cp && *cp != ',' && !isspace(*cp))
818 if (m_cp->mycmp(v, "client-apdu", len))
819 *log_mask |= PROXY_LOG_APDU_CLIENT;
820 if (m_cp->mycmp(v, "server-apdu", len))
821 *log_mask |= PROXY_LOG_APDU_SERVER;
822 if (m_cp->mycmp(v, "client-requests", len))
823 *log_mask |= PROXY_LOG_REQ_CLIENT;
824 if (m_cp->mycmp(v, "server-requests", len))
825 *log_mask |= PROXY_LOG_REQ_SERVER;
827 *log_mask |= atoi(v);
830 while (*cp && isspace(*cp))
835 if (ptr->type == XML_ELEMENT_NODE &&
836 !strcmp((const char *) ptr->name, "max-clients"))
838 const char *t = m_cp->get_text(ptr);
841 *max_clients = atoi(t);
842 if (*max_clients < 1)
851 int Yaz_ProxyConfigP::get_explain_ptr(const char *host, const char *db,
852 xmlNodePtr *ptr_target,
853 xmlNodePtr *ptr_explain)
860 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
862 if (ptr->type == XML_ELEMENT_NODE &&
863 !strcmp((const char *) ptr->name, "target"))
866 xmlNodePtr ptr = (*ptr_target)->children;
867 for (; ptr; ptr = ptr->next)
869 if (ptr->type == XML_ELEMENT_NODE &&
870 !strcmp((const char *) ptr->name, "explain"))
873 xmlNodePtr ptr = (*ptr_explain)->children;
875 for (; ptr; ptr = ptr->next)
876 if (ptr->type == XML_ELEMENT_NODE &&
877 !strcmp((const char *) ptr->name, "serverInfo"))
881 for (ptr = ptr->children; ptr; ptr = ptr->next)
882 if (ptr->type == XML_ELEMENT_NODE &&
883 !strcmp((const char *) ptr->name, "database"))
888 for (ptr = ptr->children; ptr; ptr = ptr->next)
889 if (ptr->type == XML_TEXT_NODE &&
891 !strcmp((const char *) ptr->content, db))
904 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
905 const char **backend_db)
908 xmlNodePtr ptr_target, ptr_explain;
909 if (m_cp->get_explain_ptr(0, db, &ptr_target, &ptr_explain)
912 struct _xmlAttr *attr;
913 const char *name = 0;
915 for (attr = ptr_target->properties; attr; attr = attr->next)
916 if (!strcmp((const char *) attr->name, "name")
918 && attr->children->type==XML_TEXT_NODE
919 && attr->children->content
920 && attr->children->content[0])
922 name = (const char *)attr->children->content;
927 for (attr = ptr_target->properties; attr; attr = attr->next)
928 if (!strcmp((const char *) attr->name, "database"))
931 && attr->children->type==XML_TEXT_NODE
932 && attr->children->content)
933 *backend_db = (const char *) attr->children->content;
942 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
943 const char *db, int *len)
946 xmlNodePtr ptr_target, ptr_explain;
947 if (m_cp->get_explain_ptr(0 /* host */, db, &ptr_target, &ptr_explain))
949 xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
951 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
953 xmlDocSetRootElement(doc, ptr2);
956 xmlDocDumpMemory(doc, &buf_out, len);
957 char *content = (char*) odr_malloc(odr, *len);
958 memcpy(content, buf_out, *len);
968 void Yaz_ProxyConfig::get_target_info(const char *name,
973 int *target_idletime,
974 int *client_idletime,
976 int *keepalive_limit_bw,
977 int *keepalive_limit_pdu,
979 const char **cql2rpn,
980 const char **authentication)
984 if (!m_cp->m_proxyPtr)
991 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
993 if (ptr->type == XML_ELEMENT_NODE &&
994 !strcmp((const char *) ptr->name, "max-clients"))
996 const char *t = m_cp->get_text(ptr);
999 *max_clients = atoi(t);
1000 if (*max_clients < 1)
1005 ptr = m_cp->find_target_node(name, 0);
1013 m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
1014 target_idletime, client_idletime,
1015 keepalive_limit_bw, keepalive_limit_pdu,
1016 pre_init, cql2rpn, authentication);