1 /* $Id: yaz-proxy-config.cpp,v 1.11 2004-12-03 14:28:18 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 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
455 Odr_oid *syntax, Z_RecordComposition *comp,
457 char **stylesheet, char **schema,
459 char **backend_charset,
460 char **usemarcon_ini_stage1,
461 char **usemarcon_ini_stage2
476 xfree (*backend_type);
481 xfree (*backend_charset);
482 *backend_charset = 0;
484 if (usemarcon_ini_stage1)
486 xfree (*usemarcon_ini_stage1);
487 *usemarcon_ini_stage1 = 0;
489 if (usemarcon_ini_stage2)
491 xfree (*usemarcon_ini_stage2);
492 *usemarcon_ini_stage2 = 0;
495 int syntax_has_matched = 0;
498 ptr = m_cp->find_target_node(name, 0);
501 for(ptr = ptr->children; ptr; ptr = ptr->next)
503 if (ptr->type == XML_ELEMENT_NODE &&
504 !strcmp((const char *) ptr->name, "syntax"))
506 int match = 0; // if we match record syntax
507 const char *match_type = 0;
508 const char *match_error = 0;
509 const char *match_marcxml = 0;
510 const char *match_stylesheet = 0;
511 const char *match_identifier = 0;
512 const char *match_backend_type = 0;
513 const char *match_backend_charset = 0;
514 const char *match_usemarcon_ini_stage1 = 0;
515 const char *match_usemarcon_ini_stage2 = 0;
516 struct _xmlAttr *attr;
517 for (attr = ptr->properties; attr; attr = attr->next)
519 if (!strcmp((const char *) attr->name, "type") &&
520 attr->children && attr->children->type == XML_TEXT_NODE)
521 match_type = (const char *) attr->children->content;
522 if (!strcmp((const char *) attr->name, "error") &&
523 attr->children && attr->children->type == XML_TEXT_NODE)
524 match_error = (const char *) attr->children->content;
525 if (!strcmp((const char *) attr->name, "marcxml") &&
526 attr->children && attr->children->type == XML_TEXT_NODE)
527 match_marcxml = (const char *) attr->children->content;
528 if (!strcmp((const char *) attr->name, "stylesheet") &&
529 attr->children && attr->children->type == XML_TEXT_NODE)
530 match_stylesheet = (const char *) attr->children->content;
531 if (!strcmp((const char *) attr->name, "identifier") &&
532 attr->children && attr->children->type == XML_TEXT_NODE)
533 match_identifier = (const char *) attr->children->content;
534 if (!strcmp((const char *) attr->name, "backendtype") &&
535 attr->children && attr->children->type == XML_TEXT_NODE)
536 match_backend_type = (const char *)
537 attr->children->content;
538 if (!strcmp((const char *) attr->name, "backendcharset") &&
539 attr->children && attr->children->type == XML_TEXT_NODE)
540 match_backend_charset = (const char *)
541 attr->children->content;
542 if (!strcmp((const char *) attr->name, "usemarconstage1") &&
543 attr->children && attr->children->type == XML_TEXT_NODE)
544 match_usemarcon_ini_stage1 = (const char *)
545 attr->children->content;
546 if (!strcmp((const char *) attr->name, "usemarconstage2") &&
547 attr->children && attr->children->type == XML_TEXT_NODE)
548 match_usemarcon_ini_stage2 = (const char *)
549 attr->children->content;
553 if (!strcmp(match_type, "*"))
555 else if (!strcmp(match_type, "none"))
562 int match_oid[OID_SIZE];
563 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
564 if (oid_oidcmp(match_oid, syntax) == 0)
571 syntax_has_matched = 1;
572 match = m_cp->check_schema(ptr->children, comp,
577 if (stylesheet && match_stylesheet)
580 *stylesheet = xstrdup(match_stylesheet);
582 if (schema && match_identifier)
585 *schema = xstrdup(match_identifier);
587 if (backend_type && match_backend_type)
589 xfree(*backend_type);
590 *backend_type = xstrdup(match_backend_type);
592 if (backend_charset && match_backend_charset)
594 xfree(*backend_charset);
595 *backend_charset = xstrdup(match_backend_charset);
597 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage1)
599 xfree(*usemarcon_ini_stage1);
600 *usemarcon_ini_stage1 = xstrdup(match_usemarcon_ini_stage1);
602 if (usemarcon_ini_stage1 && match_usemarcon_ini_stage2)
604 xfree(*usemarcon_ini_stage2);
605 *usemarcon_ini_stage2 = xstrdup(match_usemarcon_ini_stage2);
613 if (syntax_has_matched) // if syntax OK, bad schema/ESN
617 char dotoid_str[100];
618 oid_to_dotstring(syntax, dotoid_str);
619 *addinfo = odr_strdup(odr, dotoid_str);
621 return atoi(match_error);
632 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
639 for (dptr = ptr->children; dptr; dptr = dptr->next)
640 if (dptr->type == XML_ELEMENT_NODE &&
641 !strcmp((const char *) dptr->name, "database"))
643 struct _xmlAttr *attr;
644 for (attr = dptr->properties; attr; attr = attr->next)
645 if (!strcmp((const char *) attr->name, "name"))
648 && attr->children->type==XML_TEXT_NODE
649 && attr->children->content
650 && (!strcmp((const char *) attr->children->content, db)
651 || !strcmp((const char *) attr->children->content,
659 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
664 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
666 if (ptr->type == XML_ELEMENT_NODE &&
667 !strcmp((const char *) ptr->name, "target"))
672 // <target default="1"> ?
673 struct _xmlAttr *attr;
674 for (attr = ptr->properties; attr; attr = attr->next)
675 if (!strcmp((const char *) attr->name, "default") &&
676 attr->children && attr->children->type == XML_TEXT_NODE)
678 xmlChar *t = attr->children->content;
681 return find_target_db(ptr, db);
687 // <target name="name"> ?
688 struct _xmlAttr *attr;
689 for (attr = ptr->properties; attr; attr = attr->next)
690 if (!strcmp((const char *) attr->name, "name"))
693 && attr->children->type==XML_TEXT_NODE
694 && attr->children->content
695 && (!strcmp((const char *) attr->children->content,
697 || !strcmp((const char *) attr->children->content,
700 return find_target_db(ptr, db);
710 int Yaz_ProxyConfig::get_target_no(int no,
716 int *target_idletime,
717 int *client_idletime,
719 int *keepalive_limit_bw,
720 int *keepalive_limit_pdu,
722 const char **cql2rpn,
723 const char **authentication)
727 if (!m_cp->m_proxyPtr)
730 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
731 if (ptr->type == XML_ELEMENT_NODE &&
732 !strcmp((const char *) ptr->name, "target"))
736 struct _xmlAttr *attr;
737 for (attr = ptr->properties; attr; attr = attr->next)
738 if (!strcmp((const char *) attr->name, "name"))
741 && attr->children->type==XML_TEXT_NODE
742 && attr->children->content)
743 *name = (const char *) attr->children->content;
745 m_cp->return_target_info(
747 limit_bw, limit_pdu, limit_req,
748 target_idletime, client_idletime,
749 keepalive_limit_bw, keepalive_limit_pdu,
750 pre_init, cql2rpn, authentication);
759 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
761 if (len == strlen(item) && memcmp(hay, item, len) == 0)
766 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
771 if (!m_cp->m_proxyPtr)
773 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
775 if (ptr->type == XML_ELEMENT_NODE
776 && !strcmp((const char *) ptr->name, "log"))
778 const char *v = m_cp->get_text(ptr);
783 while (*cp && *cp != ',' && !isspace(*cp))
786 if (m_cp->mycmp(v, "client-apdu", len))
787 *log_mask |= PROXY_LOG_APDU_CLIENT;
788 if (m_cp->mycmp(v, "server-apdu", len))
789 *log_mask |= PROXY_LOG_APDU_SERVER;
790 if (m_cp->mycmp(v, "client-requests", len))
791 *log_mask |= PROXY_LOG_REQ_CLIENT;
792 if (m_cp->mycmp(v, "server-requests", len))
793 *log_mask |= PROXY_LOG_REQ_SERVER;
795 *log_mask |= atoi(v);
798 while (*cp && isspace(*cp))
803 if (ptr->type == XML_ELEMENT_NODE &&
804 !strcmp((const char *) ptr->name, "max-clients"))
806 const char *t = m_cp->get_text(ptr);
809 *max_clients = atoi(t);
810 if (*max_clients < 1)
819 int Yaz_ProxyConfigP::get_explain_ptr(const char *host, const char *db,
820 xmlNodePtr *ptr_target,
821 xmlNodePtr *ptr_explain)
828 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
830 if (ptr->type == XML_ELEMENT_NODE &&
831 !strcmp((const char *) ptr->name, "target"))
834 xmlNodePtr ptr = (*ptr_target)->children;
835 for (; ptr; ptr = ptr->next)
837 if (ptr->type == XML_ELEMENT_NODE &&
838 !strcmp((const char *) ptr->name, "explain"))
841 xmlNodePtr ptr = (*ptr_explain)->children;
843 for (; ptr; ptr = ptr->next)
844 if (ptr->type == XML_ELEMENT_NODE &&
845 !strcmp((const char *) ptr->name, "serverInfo"))
849 for (ptr = ptr->children; ptr; ptr = ptr->next)
850 if (ptr->type == XML_ELEMENT_NODE &&
851 !strcmp((const char *) ptr->name, "database"))
856 for (ptr = ptr->children; ptr; ptr = ptr->next)
857 if (ptr->type == XML_TEXT_NODE &&
859 !strcmp((const char *) ptr->content, db))
872 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
873 const char **backend_db)
876 xmlNodePtr ptr_target, ptr_explain;
877 if (m_cp->get_explain_ptr(0, db, &ptr_target, &ptr_explain)
880 struct _xmlAttr *attr;
881 const char *name = 0;
883 for (attr = ptr_target->properties; attr; attr = attr->next)
884 if (!strcmp((const char *) attr->name, "name")
886 && attr->children->type==XML_TEXT_NODE
887 && attr->children->content
888 && attr->children->content[0])
890 name = (const char *)attr->children->content;
895 for (attr = ptr_target->properties; attr; attr = attr->next)
896 if (!strcmp((const char *) attr->name, "database"))
899 && attr->children->type==XML_TEXT_NODE
900 && attr->children->content)
901 *backend_db = (const char *) attr->children->content;
910 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
911 const char *db, int *len)
914 xmlNodePtr ptr_target, ptr_explain;
915 if (m_cp->get_explain_ptr(0 /* host */, db, &ptr_target, &ptr_explain))
917 xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
919 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
921 xmlDocSetRootElement(doc, ptr2);
924 xmlDocDumpMemory(doc, &buf_out, len);
925 char *content = (char*) odr_malloc(odr, *len);
926 memcpy(content, buf_out, *len);
936 void Yaz_ProxyConfig::get_target_info(const char *name,
941 int *target_idletime,
942 int *client_idletime,
944 int *keepalive_limit_bw,
945 int *keepalive_limit_pdu,
947 const char **cql2rpn,
948 const char **authentication)
952 if (!m_cp->m_proxyPtr)
959 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
961 if (ptr->type == XML_ELEMENT_NODE &&
962 !strcmp((const char *) ptr->name, "max-clients"))
964 const char *t = m_cp->get_text(ptr);
967 *max_clients = atoi(t);
968 if (*max_clients < 1)
973 ptr = m_cp->find_target_node(name, 0);
981 m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
982 target_idletime, client_idletime,
983 keepalive_limit_bw, keepalive_limit_pdu,
984 pre_init, cql2rpn, authentication);