1 /* $Id: yaz-proxy-config.cpp,v 1.9 2004-11-09 14:24: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
24 #include <yazproxy/proxy.h>
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29 #include <libxml/xinclude.h>
30 #include <libxslt/xsltutils.h>
31 #include <libxslt/transform.h>
34 class Yaz_ProxyConfigP {
35 friend class Yaz_ProxyConfig;
39 int mycmp(const char *hay, const char *item, size_t len);
40 int match_list(int v, const char *m);
41 int atoi_l(const char **cp);
43 int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
44 const char *schema_identifier);
46 xmlNodePtr m_proxyPtr;
47 void return_target_info(xmlNodePtr ptr, const char **url,
48 int *limit_bw, int *limit_pdu, int *limit_req,
49 int *target_idletime, int *client_idletime,
50 int *keepalive_limit_bw, int *keepalive_limit_pdu,
51 int *pre_init, const char **cql2rpn,
52 const char **authentication);
53 void return_limit(xmlNodePtr ptr,
54 int *limit_bw, int *limit_pdu, int *limit_req);
55 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
57 xmlNodePtr find_target_node(const char *name, const char *db);
58 xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
59 const char *get_text(xmlNodePtr ptr);
60 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
61 Z_AttributeList *attrs,
63 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
65 int get_explain_ptr(const char *host, const char *db,
66 xmlNodePtr *ptr_target, xmlNodePtr *ptr_explain);
70 Yaz_ProxyConfig::Yaz_ProxyConfig()
72 m_cp = new Yaz_ProxyConfigP;
80 Yaz_ProxyConfig::~Yaz_ProxyConfig()
83 if (!m_cp->m_copy && m_cp->m_docPtr)
84 xmlFreeDoc(m_cp->m_docPtr);
89 int Yaz_ProxyConfig::read_xml(const char *fname)
92 xmlDocPtr ndoc = xmlParseFile(fname);
96 yaz_log(LOG_WARN, "Config file %s not found or parse error", fname);
99 int noSubstitutions = xmlXIncludeProcess(ndoc);
100 if (noSubstitutions == -1)
101 yaz_log(LOG_WARN, "XInclude processing failed on config %s", fname);
103 xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
104 if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
105 strcmp((const char *) proxyPtr->name, "proxy"))
107 yaz_log(LOG_WARN, "No proxy element in %s", fname);
111 m_cp->m_proxyPtr = proxyPtr;
113 // OK: release previous and make it the current one.
115 xmlFreeDoc(m_cp->m_docPtr);
116 m_cp->m_docPtr = ndoc;
124 const char *Yaz_ProxyConfigP::get_text(xmlNodePtr ptr)
126 for(ptr = ptr->children; ptr; ptr = ptr->next)
127 if (ptr->type == XML_TEXT_NODE)
129 xmlChar *t = ptr->content;
134 return (const char *) t;
142 void Yaz_ProxyConfigP::return_limit(xmlNodePtr ptr,
147 for (ptr = ptr->children; ptr; ptr = ptr->next)
149 if (ptr->type == XML_ELEMENT_NODE
150 && !strcmp((const char *) ptr->name, "bandwidth"))
152 const char *t = get_text(ptr);
156 if (ptr->type == XML_ELEMENT_NODE
157 && !strcmp((const char *) ptr->name, "retrieve"))
159 const char *t = get_text(ptr);
161 *limit_req = atoi(t);
163 if (ptr->type == XML_ELEMENT_NODE
164 && !strcmp((const char *) ptr->name, "pdu"))
166 const char *t = get_text(ptr);
168 *limit_pdu = atoi(t);
175 void Yaz_ProxyConfigP::return_target_info(xmlNodePtr ptr,
180 int *target_idletime,
181 int *client_idletime,
182 int *keepalive_limit_bw,
183 int *keepalive_limit_pdu,
185 const char **cql2rpn,
186 const char **authentication)
191 for (; ptr; ptr = ptr->next)
193 if (ptr->type == XML_ELEMENT_NODE
194 && !strcmp((const char *) ptr->name, "preinit"))
196 const char *v = get_text(ptr);
197 *pre_init = v ? atoi(v) : 1;
199 if (ptr->type == XML_ELEMENT_NODE
200 && !strcmp((const char *) ptr->name, "url"))
202 const char *t = get_text(ptr);
203 if (t && no_url < MAX_ZURL_PLEX)
209 if (ptr->type == XML_ELEMENT_NODE
210 && !strcmp((const char *) ptr->name, "keepalive"))
213 *keepalive_limit_bw = 500000;
214 *keepalive_limit_pdu = 1000;
215 return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
218 if (ptr->type == XML_ELEMENT_NODE
219 && !strcmp((const char *) ptr->name, "limit"))
220 return_limit(ptr, limit_bw, limit_pdu, limit_req);
221 if (ptr->type == XML_ELEMENT_NODE
222 && !strcmp((const char *) ptr->name, "target-timeout"))
224 const char *t = get_text(ptr);
227 *target_idletime = atoi(t);
228 if (*target_idletime < 0)
229 *target_idletime = 0;
232 if (ptr->type == XML_ELEMENT_NODE
233 && !strcmp((const char *) ptr->name, "client-timeout"))
235 const char *t = get_text(ptr);
238 *client_idletime = atoi(t);
239 if (*client_idletime < 0)
240 *client_idletime = 0;
243 if (ptr->type == XML_ELEMENT_NODE
244 && !strcmp((const char *) ptr->name, "cql2rpn"))
246 const char *t = get_text(ptr);
250 if (ptr->type == XML_ELEMENT_NODE
251 && !strcmp((const char *) ptr->name, "authentication"))
253 const char *t = get_text(ptr);
261 int Yaz_ProxyConfigP::atoi_l(const char **cp)
264 while (**cp && isdigit(**cp))
266 v = v*10 + (**cp - '0');
272 int Yaz_ProxyConfigP::match_list(int v, const char *m)
276 while(*m && isspace(*m))
287 if (v >= l && v <= h)
296 int Yaz_ProxyConfigP::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
297 Z_AttributeList *attrs,
301 for (i = 0; i<attrs->num_attributes; i++)
303 Z_AttributeElement *el = attrs->attributes[i];
305 if (!el->attributeType)
307 int type = *el->attributeType;
310 if (el->which == Z_AttributeValue_numeric && el->value.numeric)
311 value = el->value.numeric;
314 for(ptr = ptrl->children; ptr; ptr = ptr->next)
316 if (ptr->type == XML_ELEMENT_NODE &&
317 !strcmp((const char *) ptr->name, "attribute"))
319 const char *match_type = 0;
320 const char *match_value = 0;
321 const char *match_error = 0;
322 struct _xmlAttr *attr;
323 for (attr = ptr->properties; attr; attr = attr->next)
325 if (!strcmp((const char *) attr->name, "type") &&
326 attr->children && attr->children->type == XML_TEXT_NODE)
327 match_type = (const char *) attr->children->content;
328 if (!strcmp((const char *) attr->name, "value") &&
329 attr->children && attr->children->type == XML_TEXT_NODE)
330 match_value = (const char *) attr->children->content;
331 if (!strcmp((const char *) attr->name, "error") &&
332 attr->children && attr->children->type == XML_TEXT_NODE)
333 match_error = (const char *) attr->children->content;
335 if (match_type && match_value)
337 char addinfo_str[20];
338 if (!match_list(type, match_type))
342 if (!strcmp(match_type, "*"))
343 sprintf (addinfo_str, "%d", type);
346 if (!match_list(*value, match_value))
348 sprintf (addinfo_str, "%d", *value);
356 *addinfo = odr_strdup(odr, addinfo_str);
357 return atoi(match_error);
369 int Yaz_ProxyConfigP::check_type_1_structure(ODR odr, xmlNodePtr ptr,
373 if (q->which == Z_RPNStructure_complex)
375 int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
378 e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
381 else if (q->which == Z_RPNStructure_simple)
383 if (q->u.simple->which == Z_Operand_APT)
385 return check_type_1_attributes(
386 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
395 int Yaz_ProxyConfigP::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
398 // possibly check for Bib-1
399 return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
403 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
409 ptr = m_cp->find_target_node(name, 0);
412 if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
413 return m_cp->check_type_1(odr, ptr, query->u.type_1, addinfo);
420 int Yaz_ProxyConfigP::check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
421 const char *schema_identifier)
424 int default_match = 1;
425 if (comp && comp->which == Z_RecordComp_simple &&
426 comp->u.simple && comp->u.simple->which == Z_ElementSetNames_generic)
428 esn = comp->u.simple->u.generic;
430 // if no ESN/schema was given accept..
433 // check if schema identifier match
434 if (schema_identifier && !strcmp(esn, schema_identifier))
436 // Check each name element
437 for (; ptr; ptr = ptr->next)
439 if (ptr->type == XML_ELEMENT_NODE
440 && !strcmp((const char *) ptr->name, "name"))
442 xmlNodePtr tptr = ptr->children;
444 for (; tptr; tptr = tptr->next)
445 if (tptr->type == XML_TEXT_NODE && tptr->content)
447 xmlChar *t = tptr->content;
448 while (*t && isspace(*t))
451 while (esn[i] && esn[i] == t[i])
453 if (!esn[i] && (!t[i] || isspace(t[i])))
458 return default_match;
462 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
463 Odr_oid *syntax, Z_RecordComposition *comp,
465 char **stylesheet, char **schema,
467 char **backend_charset)
481 xfree (*backend_type);
486 xfree (*backend_charset);
487 *backend_charset = 0;
490 int syntax_has_matched = 0;
493 ptr = m_cp->find_target_node(name, 0);
496 for(ptr = ptr->children; ptr; ptr = ptr->next)
498 if (ptr->type == XML_ELEMENT_NODE &&
499 !strcmp((const char *) ptr->name, "syntax"))
501 int match = 0; // if we match record syntax
502 const char *match_type = 0;
503 const char *match_error = 0;
504 const char *match_marcxml = 0;
505 const char *match_stylesheet = 0;
506 const char *match_identifier = 0;
507 const char *match_backend_type = 0;
508 const char *match_backend_charset = 0;
509 struct _xmlAttr *attr;
510 for (attr = ptr->properties; attr; attr = attr->next)
512 if (!strcmp((const char *) attr->name, "type") &&
513 attr->children && attr->children->type == XML_TEXT_NODE)
514 match_type = (const char *) attr->children->content;
515 if (!strcmp((const char *) attr->name, "error") &&
516 attr->children && attr->children->type == XML_TEXT_NODE)
517 match_error = (const char *) attr->children->content;
518 if (!strcmp((const char *) attr->name, "marcxml") &&
519 attr->children && attr->children->type == XML_TEXT_NODE)
520 match_marcxml = (const char *) attr->children->content;
521 if (!strcmp((const char *) attr->name, "stylesheet") &&
522 attr->children && attr->children->type == XML_TEXT_NODE)
523 match_stylesheet = (const char *) attr->children->content;
524 if (!strcmp((const char *) attr->name, "identifier") &&
525 attr->children && attr->children->type == XML_TEXT_NODE)
526 match_identifier = (const char *) attr->children->content;
527 if (!strcmp((const char *) attr->name, "backendtype") &&
528 attr->children && attr->children->type == XML_TEXT_NODE)
529 match_backend_type = (const char *)
530 attr->children->content;
531 if (!strcmp((const char *) attr->name, "backendcharset") &&
532 attr->children && attr->children->type == XML_TEXT_NODE)
533 match_backend_charset = (const char *)
534 attr->children->content;
538 if (!strcmp(match_type, "*"))
540 else if (!strcmp(match_type, "none"))
547 int match_oid[OID_SIZE];
548 oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
549 if (oid_oidcmp(match_oid, syntax) == 0)
556 syntax_has_matched = 1;
557 match = m_cp->check_schema(ptr->children, comp,
562 if (stylesheet && match_stylesheet)
565 *stylesheet = xstrdup(match_stylesheet);
567 if (schema && match_identifier)
570 *schema = xstrdup(match_identifier);
572 if (backend_type && match_backend_type)
574 xfree(*backend_type);
575 *backend_type = xstrdup(match_backend_type);
577 if (backend_charset && match_backend_charset)
579 xfree(*backend_charset);
580 *backend_charset = xstrdup(match_backend_charset);
588 if (syntax_has_matched) // if syntax OK, bad schema/ESN
592 char dotoid_str[100];
593 oid_to_dotstring(syntax, dotoid_str);
594 *addinfo = odr_strdup(odr, dotoid_str);
596 return atoi(match_error);
607 xmlNodePtr Yaz_ProxyConfigP::find_target_db(xmlNodePtr ptr, const char *db)
614 for (dptr = ptr->children; dptr; dptr = dptr->next)
615 if (dptr->type == XML_ELEMENT_NODE &&
616 !strcmp((const char *) dptr->name, "database"))
618 struct _xmlAttr *attr;
619 for (attr = dptr->properties; attr; attr = attr->next)
620 if (!strcmp((const char *) attr->name, "name"))
623 && attr->children->type==XML_TEXT_NODE
624 && attr->children->content
625 && (!strcmp((const char *) attr->children->content, db)
626 || !strcmp((const char *) attr->children->content,
634 xmlNodePtr Yaz_ProxyConfigP::find_target_node(const char *name, const char *db)
639 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
641 if (ptr->type == XML_ELEMENT_NODE &&
642 !strcmp((const char *) ptr->name, "target"))
647 // <target default="1"> ?
648 struct _xmlAttr *attr;
649 for (attr = ptr->properties; attr; attr = attr->next)
650 if (!strcmp((const char *) attr->name, "default") &&
651 attr->children && attr->children->type == XML_TEXT_NODE)
653 xmlChar *t = attr->children->content;
656 return find_target_db(ptr, db);
662 // <target name="name"> ?
663 struct _xmlAttr *attr;
664 for (attr = ptr->properties; attr; attr = attr->next)
665 if (!strcmp((const char *) attr->name, "name"))
668 && attr->children->type==XML_TEXT_NODE
669 && attr->children->content
670 && (!strcmp((const char *) attr->children->content,
672 || !strcmp((const char *) attr->children->content,
675 return find_target_db(ptr, db);
685 int Yaz_ProxyConfig::get_target_no(int no,
691 int *target_idletime,
692 int *client_idletime,
694 int *keepalive_limit_bw,
695 int *keepalive_limit_pdu,
697 const char **cql2rpn,
698 const char **authentication)
702 if (!m_cp->m_proxyPtr)
705 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
706 if (ptr->type == XML_ELEMENT_NODE &&
707 !strcmp((const char *) ptr->name, "target"))
711 struct _xmlAttr *attr;
712 for (attr = ptr->properties; attr; attr = attr->next)
713 if (!strcmp((const char *) attr->name, "name"))
716 && attr->children->type==XML_TEXT_NODE
717 && attr->children->content)
718 *name = (const char *) attr->children->content;
720 m_cp->return_target_info(
722 limit_bw, limit_pdu, limit_req,
723 target_idletime, client_idletime,
724 keepalive_limit_bw, keepalive_limit_pdu,
725 pre_init, cql2rpn, authentication);
734 int Yaz_ProxyConfigP::mycmp(const char *hay, const char *item, size_t len)
736 if (len == strlen(item) && memcmp(hay, item, len) == 0)
741 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
746 if (!m_cp->m_proxyPtr)
748 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
750 if (ptr->type == XML_ELEMENT_NODE
751 && !strcmp((const char *) ptr->name, "log"))
753 const char *v = m_cp->get_text(ptr);
758 while (*cp && *cp != ',' && !isspace(*cp))
761 if (m_cp->mycmp(v, "client-apdu", len))
762 *log_mask |= PROXY_LOG_APDU_CLIENT;
763 if (m_cp->mycmp(v, "server-apdu", len))
764 *log_mask |= PROXY_LOG_APDU_SERVER;
765 if (m_cp->mycmp(v, "client-requests", len))
766 *log_mask |= PROXY_LOG_REQ_CLIENT;
767 if (m_cp->mycmp(v, "server-requests", len))
768 *log_mask |= PROXY_LOG_REQ_SERVER;
770 *log_mask |= atoi(v);
773 while (*cp && isspace(*cp))
778 if (ptr->type == XML_ELEMENT_NODE &&
779 !strcmp((const char *) ptr->name, "max-clients"))
781 const char *t = m_cp->get_text(ptr);
784 *max_clients = atoi(t);
785 if (*max_clients < 1)
794 int Yaz_ProxyConfigP::get_explain_ptr(const char *host, const char *db,
795 xmlNodePtr *ptr_target,
796 xmlNodePtr *ptr_explain)
803 for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
805 if (ptr->type == XML_ELEMENT_NODE &&
806 !strcmp((const char *) ptr->name, "target"))
809 xmlNodePtr ptr = (*ptr_target)->children;
810 for (; ptr; ptr = ptr->next)
812 if (ptr->type == XML_ELEMENT_NODE &&
813 !strcmp((const char *) ptr->name, "explain"))
816 xmlNodePtr ptr = (*ptr_explain)->children;
818 for (; ptr; ptr = ptr->next)
819 if (ptr->type == XML_ELEMENT_NODE &&
820 !strcmp((const char *) ptr->name, "serverInfo"))
824 for (ptr = ptr->children; ptr; ptr = ptr->next)
825 if (ptr->type == XML_ELEMENT_NODE &&
826 !strcmp((const char *) ptr->name, "database"))
831 for (ptr = ptr->children; ptr; ptr = ptr->next)
832 if (ptr->type == XML_TEXT_NODE &&
834 !strcmp((const char *) ptr->content, db))
847 const char *Yaz_ProxyConfig::get_explain_name(const char *db,
848 const char **backend_db)
851 xmlNodePtr ptr_target, ptr_explain;
852 if (m_cp->get_explain_ptr(0, db, &ptr_target, &ptr_explain)
855 struct _xmlAttr *attr;
856 const char *name = 0;
858 for (attr = ptr_target->properties; attr; attr = attr->next)
859 if (!strcmp((const char *) attr->name, "name")
861 && attr->children->type==XML_TEXT_NODE
862 && attr->children->content
863 && attr->children->content[0])
865 name = (const char *)attr->children->content;
870 for (attr = ptr_target->properties; attr; attr = attr->next)
871 if (!strcmp((const char *) attr->name, "database"))
874 && attr->children->type==XML_TEXT_NODE
875 && attr->children->content)
876 *backend_db = (const char *) attr->children->content;
885 char *Yaz_ProxyConfig::get_explain_doc(ODR odr, const char *name,
886 const char *db, int *len)
889 xmlNodePtr ptr_target, ptr_explain;
890 if (m_cp->get_explain_ptr(0 /* host */, db, &ptr_target, &ptr_explain))
892 xmlNodePtr ptr2 = xmlCopyNode(ptr_explain, 1);
894 xmlDocPtr doc = xmlNewDoc((const xmlChar *) "1.0");
896 xmlDocSetRootElement(doc, ptr2);
899 xmlDocDumpMemory(doc, &buf_out, len);
900 char *content = (char*) odr_malloc(odr, *len);
901 memcpy(content, buf_out, *len);
911 void Yaz_ProxyConfig::get_target_info(const char *name,
916 int *target_idletime,
917 int *client_idletime,
919 int *keepalive_limit_bw,
920 int *keepalive_limit_pdu,
922 const char **cql2rpn,
923 const char **authentication)
927 if (!m_cp->m_proxyPtr)
934 for (ptr = m_cp->m_proxyPtr->children; ptr; ptr = ptr->next)
936 if (ptr->type == XML_ELEMENT_NODE &&
937 !strcmp((const char *) ptr->name, "max-clients"))
939 const char *t = m_cp->get_text(ptr);
942 *max_clients = atoi(t);
943 if (*max_clients < 1)
948 ptr = m_cp->find_target_node(name, 0);
956 m_cp->return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
957 target_idletime, client_idletime,
958 keepalive_limit_bw, keepalive_limit_pdu,
959 pre_init, cql2rpn, authentication);