6c3ffa6cd50c225218f3fa6178f13b6aeadc86d5
[yazpp-moved-to-github.git] / src / yaz-proxy-config.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy-config.cpp,v 1.15 2003-12-16 14:17:01 adam Exp $
6  */
7
8 #include <ctype.h>
9 #include <yaz/log.h>
10 #include <yaz++/proxy.h>
11
12 Yaz_ProxyConfig::Yaz_ProxyConfig()
13 {
14     m_copy = 0;
15 #if HAVE_XML2
16     m_docPtr = 0;
17     m_proxyPtr = 0;
18 #endif
19 }
20
21 Yaz_ProxyConfig::~Yaz_ProxyConfig()
22 {
23 #if HAVE_XML2
24     if (!m_copy && m_docPtr)
25         xmlFreeDoc(m_docPtr);
26 #endif
27 }
28
29 int Yaz_ProxyConfig::read_xml(const char *fname)
30 {
31 #if HAVE_XML2
32     xmlDocPtr ndoc = xmlParseFile(fname);
33
34     if (!ndoc)
35     {
36         yaz_log(LOG_WARN, "Config file %s not found or parse error", fname);
37         return -1;  // no good
38     }
39     xmlNodePtr proxyPtr = xmlDocGetRootElement(ndoc);
40     if (!proxyPtr || proxyPtr->type != XML_ELEMENT_NODE ||
41         strcmp((const char *) proxyPtr->name, "proxy"))
42     {
43         yaz_log(LOG_WARN, "No proxy element in %s", fname);
44         xmlFreeDoc(ndoc);
45         return -1;
46     }
47     m_proxyPtr = proxyPtr;
48
49     // OK: release previous and make it the current one.
50     if (m_docPtr)
51         xmlFreeDoc(m_docPtr);
52     m_docPtr = ndoc;
53     return 0;
54 #else
55     return -2;
56 #endif
57 }
58
59 #if HAVE_XML2
60 const char *Yaz_ProxyConfig::get_text(xmlNodePtr ptr)
61 {
62     for(ptr = ptr->children; ptr; ptr = ptr->next)
63         if (ptr->type == XML_TEXT_NODE)
64         {
65             xmlChar *t = ptr->content;
66             if (t)
67             {
68                 while (*t == ' ')
69                     t++;
70                 return (const char *) t;
71             }
72         }
73     return 0;
74 }
75 #endif
76
77 #if HAVE_XML2
78 void Yaz_ProxyConfig::return_limit(xmlNodePtr ptr,
79                                    int *limit_bw,
80                                    int *limit_pdu,
81                                    int *limit_req)
82 {
83     for (ptr = ptr->children; ptr; ptr = ptr->next)
84     {
85         if (ptr->type == XML_ELEMENT_NODE 
86             && !strcmp((const char *) ptr->name, "bandwidth"))
87         {
88             const char *t = get_text(ptr);
89             if (t)
90                 *limit_bw = atoi(t);
91         }
92         if (ptr->type == XML_ELEMENT_NODE 
93             && !strcmp((const char *) ptr->name, "retrieve"))
94         {
95             const char *t = get_text(ptr);
96             if (t)
97                 *limit_req = atoi(t);
98         }
99         if (ptr->type == XML_ELEMENT_NODE 
100             && !strcmp((const char *) ptr->name, "pdu"))
101         {
102             const char *t = get_text(ptr);
103             if (t)
104                 *limit_pdu = atoi(t);
105         }
106     }
107 }
108 #endif
109
110 #if HAVE_XML2
111 void Yaz_ProxyConfig::return_target_info(xmlNodePtr ptr,
112                                          const char **url,
113                                          int *limit_bw,
114                                          int *limit_pdu,
115                                          int *limit_req,
116                                          int *target_idletime,
117                                          int *client_idletime,
118                                          int *keepalive_limit_bw,
119                                          int *keepalive_limit_pdu,
120                                          int *pre_init,
121                                          const char **cql2rpn)
122 {
123     *pre_init = 0;
124     int no_url = 0;
125     ptr = ptr->children;
126     for (; ptr; ptr = ptr->next)
127     {
128         if (ptr->type == XML_ELEMENT_NODE 
129             && !strcmp((const char *) ptr->name, "preinit"))
130         {
131             const char *v = get_text(ptr);
132             *pre_init = v ? atoi(v) : 1;
133         }
134         if (ptr->type == XML_ELEMENT_NODE 
135             && !strcmp((const char *) ptr->name, "url"))
136         {
137             const char *t = get_text(ptr);
138             if (t && no_url < MAX_ZURL_PLEX)
139             {
140                 url[no_url++] = t;
141                 url[no_url] = 0;
142             }
143         }
144         if (ptr->type == XML_ELEMENT_NODE 
145             && !strcmp((const char *) ptr->name, "keepalive"))
146         {
147             int dummy;
148             *keepalive_limit_bw = 500000;
149             *keepalive_limit_pdu = 1000;
150             return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
151                          &dummy);
152         }
153         if (ptr->type == XML_ELEMENT_NODE 
154             && !strcmp((const char *) ptr->name, "limit"))
155             return_limit(ptr, limit_bw, limit_pdu, limit_req);
156         if (ptr->type == XML_ELEMENT_NODE 
157             && !strcmp((const char *) ptr->name, "target-timeout"))
158         {
159             const char *t = get_text(ptr);
160             if (t)
161             {
162                 *target_idletime = atoi(t);
163                 if (*target_idletime < 0)
164                     *target_idletime = 0;
165             }
166         }
167         if (ptr->type == XML_ELEMENT_NODE 
168             && !strcmp((const char *) ptr->name, "client-timeout"))
169         {
170             const char *t = get_text(ptr);
171             if (t)
172             {
173                 *client_idletime = atoi(t);
174                 if (*client_idletime < 0)
175                     *client_idletime = 0;
176             }
177         }
178         if (ptr->type == XML_ELEMENT_NODE 
179             && !strcmp((const char *) ptr->name, "cql2rpn"))
180         {
181             const char *t = get_text(ptr);
182             if (t)
183                 *cql2rpn = t;
184         }
185     }
186 }
187 #endif
188
189 int Yaz_ProxyConfig::atoi_l(const char **cp)
190 {
191     int v = 0;
192     while (**cp && isdigit(**cp))
193     {
194         v = v*10 + (**cp - '0');
195         (*cp)++;
196     }
197     return v;
198 }
199
200 int Yaz_ProxyConfig::match_list(int v, const char *m)
201 {
202   while(m && *m)
203   {
204       while(*m && isspace(*m))
205           m++;
206       if (*m == '*')
207           return 1;
208       int l = atoi_l(&m);
209       int h = l;
210       if (*m == '-')
211       {
212           ++m;
213           h = atoi_l(&m);
214       }
215       if (v >= l && v <= h)
216           return 1;
217       if (*m == ',')
218           m++;
219   }
220   return 0;
221 }
222
223 #if HAVE_XML2
224 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptrl,
225                                              Z_AttributeList *attrs,
226                                              char **addinfo)
227 {
228     int i;
229     for (i = 0; i<attrs->num_attributes; i++)
230     {
231         Z_AttributeElement *el = attrs->attributes[i];
232         
233         if (!el->attributeType)
234             continue;
235         int type = *el->attributeType;
236         int *value = 0;
237         
238         if (el->which == Z_AttributeValue_numeric && el->value.numeric)
239             value = el->value.numeric;
240         
241         xmlNodePtr ptr;
242         for(ptr = ptrl->children; ptr; ptr = ptr->next)
243         {
244             if (ptr->type == XML_ELEMENT_NODE &&
245                 !strcmp((const char *) ptr->name, "attribute"))
246             {
247                 const char *match_type = 0;
248                 const char *match_value = 0;
249                 const char *match_error = 0;
250                 struct _xmlAttr *attr;
251                 for (attr = ptr->properties; attr; attr = attr->next)
252                 {
253                     if (!strcmp((const char *) attr->name, "type") &&
254                         attr->children && attr->children->type == XML_TEXT_NODE)
255                         match_type = (const char *) attr->children->content;
256                     if (!strcmp((const char *) attr->name, "value") &&
257                         attr->children && attr->children->type == XML_TEXT_NODE)
258                         match_value = (const char *) attr->children->content;
259                     if (!strcmp((const char *) attr->name, "error") &&
260                         attr->children && attr->children->type == XML_TEXT_NODE)
261                         match_error = (const char *) attr->children->content;
262                 }
263                 if (match_type && match_value)
264                 {
265                     char addinfo_str[20];
266                     if (!match_list(type, match_type))
267                         continue;
268                     
269                     *addinfo_str = '\0';
270                     if (!strcmp(match_type, "*"))
271                         sprintf (addinfo_str, "%d", type);
272                     else if (value)
273                     {
274                         if (!match_list(*value, match_value))
275                             continue;
276                         sprintf (addinfo_str, "%d", *value);
277                     }
278                     else
279                         continue;
280                     
281                     if (match_error)
282                     {
283                         if (*addinfo_str)
284                             *addinfo = odr_strdup(odr, addinfo_str);
285                         return atoi(match_error);
286                     }
287                     break;
288                 }
289             }
290         }
291     }
292     return 0;
293 }
294 #endif
295
296 #if HAVE_XML2
297 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
298                                             Z_RPNStructure *q,
299                                             char **addinfo)
300 {
301     if (q->which == Z_RPNStructure_complex)
302     {
303         int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
304         if (e)
305             return e;
306         e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
307         return e;
308     }
309     else if (q->which == Z_RPNStructure_simple)
310     {
311         if (q->u.simple->which == Z_Operand_APT)
312         {
313             return check_type_1_attributes(
314                 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
315                 addinfo);
316         }
317     }
318     return 0;
319 }
320 #endif
321
322 #if HAVE_XML2
323 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
324                                   char **addinfo)
325 {
326     // possibly check for Bib-1
327     return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
328 }
329 #endif
330
331 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
332                                  char **addinfo)
333 {
334 #if HAVE_XML2
335     xmlNodePtr ptr;
336     
337     ptr = find_target_node(name);
338     if (ptr)
339     {
340         if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
341             return check_type_1(odr, ptr, query->u.type_1, addinfo);
342     }
343 #endif
344     return 0;
345 }
346
347 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
348                                   Odr_oid *syntax, char **addinfo)
349 {
350 #if HAVE_XML2
351     xmlNodePtr ptr;
352     
353     ptr = find_target_node(name);
354     if (!ptr)
355         return 0;
356     for(ptr = ptr->children; ptr; ptr = ptr->next)
357     {
358         if (ptr->type == XML_ELEMENT_NODE &&
359             !strcmp((const char *) ptr->name, "syntax"))
360         {
361             int match = 0;  // if we match record syntax
362             const char *match_type = 0;
363             const char *match_error = 0;
364             const char *match_marcxml = 0;
365             struct _xmlAttr *attr;
366             for (attr = ptr->properties; attr; attr = attr->next)
367             {
368                 if (!strcmp((const char *) attr->name, "type") &&
369                     attr->children && attr->children->type == XML_TEXT_NODE)
370                     match_type = (const char *) attr->children->content;
371                 if (!strcmp((const char *) attr->name, "error") &&
372                     attr->children && attr->children->type == XML_TEXT_NODE)
373                     match_error = (const char *) attr->children->content;
374                 if (!strcmp((const char *) attr->name, "marcxml") &&
375                     attr->children && attr->children->type == XML_TEXT_NODE)
376                     match_marcxml = (const char *) attr->children->content;
377             }
378             if (match_type)
379             {
380                 if (!strcmp(match_type, "*"))
381                     match = 1;
382                 else if (!strcmp(match_type, "none"))
383                 {
384                     if (syntax == 0)
385                         match = 1;
386                 }
387                 else if (syntax)
388                 {
389                     int match_oid[OID_SIZE];
390                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
391                     if (oid_oidcmp(match_oid, syntax) == 0)
392                         match = 1;
393                 }
394             }
395             if (match)
396             {
397                 if (match_marcxml)
398                 {
399                     return -1;
400                 }
401                 if (match_error)
402                 {
403                     if (syntax)
404                     {
405                         char dotoid_str[100];
406                         oid_to_dotstring(syntax, dotoid_str);
407                         *addinfo = odr_strdup(odr, dotoid_str);
408                     }
409                     return atoi(match_error);
410                 }
411                 return 0;
412             }
413         }
414     }
415 #endif
416     return 0;
417 }
418
419 #if HAVE_XML2
420 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
421 {
422     xmlNodePtr ptr;
423     if (!m_proxyPtr)
424         return 0;
425     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
426     {
427         if (ptr->type == XML_ELEMENT_NODE &&
428             !strcmp((const char *) ptr->name, "target"))
429         {
430             // default one ? 
431             if (!name)
432             {
433                 // <target default="1"> ?
434                 struct _xmlAttr *attr;
435                 for (attr = ptr->properties; attr; attr = attr->next)
436                     if (!strcmp((const char *) attr->name, "default") &&
437                         attr->children && attr->children->type == XML_TEXT_NODE)
438                     {
439                         xmlChar *t = attr->children->content;
440                         if (!t || *t == '1')
441                             return ptr;
442                     }
443             }
444             else
445             {
446                 // <target name="name"> ?
447                 struct _xmlAttr *attr;
448                 for (attr = ptr->properties; attr; attr = attr->next)
449                     if (!strcmp((const char *) attr->name, "name"))
450                     {
451                         if (attr->children
452                             && attr->children->type==XML_TEXT_NODE
453                             && attr->children->content 
454                             && (!strcmp((const char *) attr->children->content,
455                                         name)
456                                 || !strcmp((const char *) attr->children->content,
457                                            "*")))
458                         {
459                             return ptr;
460                         }
461                     }
462             }
463         }
464     }
465     return 0;
466 }
467 #endif
468
469 int Yaz_ProxyConfig::get_target_no(int no,
470                                    const char **name,
471                                    const char **url,
472                                    int *limit_bw,
473                                    int *limit_pdu,
474                                    int *limit_req,
475                                    int *target_idletime,
476                                    int *client_idletime,
477                                    int *max_clients,
478                                    int *keepalive_limit_bw,
479                                    int *keepalive_limit_pdu,
480                                    int *pre_init,
481                                    const char **cql2rpn)
482 {
483 #if HAVE_XML2
484     xmlNodePtr ptr;
485     if (!m_proxyPtr)
486         return 0;
487     int i = 0;
488     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
489         if (ptr->type == XML_ELEMENT_NODE &&
490             !strcmp((const char *) ptr->name, "target"))
491         {
492             if (i == no)
493             {
494                 struct _xmlAttr *attr;
495                 for (attr = ptr->properties; attr; attr = attr->next)
496                     if (!strcmp((const char *) attr->name, "name"))
497                     {
498                         if (attr->children
499                             && attr->children->type==XML_TEXT_NODE
500                             && attr->children->content)
501                             *name = (const char *) attr->children->content;
502                     }
503                 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
504                                    target_idletime, client_idletime,
505                                    keepalive_limit_bw, keepalive_limit_pdu,
506                                    pre_init, cql2rpn);
507                 return 1;
508             }
509             i++;
510         }
511 #endif
512     return 0;
513 }
514
515 int Yaz_ProxyConfig::mycmp(const char *hay, const char *item, size_t len)
516 {
517     if (len == strlen(item) && memcmp(hay, item, len) == 0)
518         return 1;
519     return 0;
520 }
521
522 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
523                                        int *max_clients)
524 {
525 #if HAVE_XML2
526     xmlNodePtr ptr;
527     if (!m_proxyPtr)
528         return;
529     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
530     {
531         if (ptr->type == XML_ELEMENT_NODE 
532             && !strcmp((const char *) ptr->name, "log"))
533         {
534             const char *v = get_text(ptr);
535             *log_mask = 0;
536             while (v && *v)
537             {
538                 const char *cp = v;
539                 while (*cp && *cp != ',' && !isspace(*cp))
540                     cp++;
541                 size_t len = cp - v;
542                 if (mycmp(v, "client-apdu", len))
543                     *log_mask |= PROXY_LOG_APDU_CLIENT;
544                 if (mycmp(v, "server-apdu", len))
545                     *log_mask |= PROXY_LOG_APDU_SERVER;
546                 if (mycmp(v, "client-requests", len))
547                     *log_mask |= PROXY_LOG_REQ_CLIENT;
548                 if (mycmp(v, "server-requests", len))
549                     *log_mask |= PROXY_LOG_REQ_SERVER;
550                 if (isdigit(*v))
551                     *log_mask |= atoi(v);
552                 if (*cp == ',')
553                     cp++;
554                 while (*cp && isspace(*cp))
555                     cp++;
556                 v = cp;
557             }
558         }
559         if (ptr->type == XML_ELEMENT_NODE &&
560             !strcmp((const char *) ptr->name, "max-clients"))
561         {
562             const char *t = get_text(ptr);
563             if (t)
564             {
565                 *max_clients = atoi(t);
566                 if (*max_clients  < 1)
567                     *max_clients = 1;
568             }
569         }
570     }
571 #endif
572 }
573
574 void Yaz_ProxyConfig::get_target_info(const char *name,
575                                       const char **url,
576                                       int *limit_bw,
577                                       int *limit_pdu,
578                                       int *limit_req,
579                                       int *target_idletime,
580                                       int *client_idletime,
581                                       int *max_clients,
582                                       int *keepalive_limit_bw,
583                                       int *keepalive_limit_pdu,
584                                       int *pre_init,
585                                       const char **cql2rpn)
586 {
587 #if HAVE_XML2
588     xmlNodePtr ptr;
589     if (!m_proxyPtr)
590     {
591         url[0] = name;
592         url[1] = 0;
593         return;
594     }
595     url[0] = 0;
596     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
597     {
598         if (ptr->type == XML_ELEMENT_NODE &&
599             !strcmp((const char *) ptr->name, "max-clients"))
600         {
601             const char *t = get_text(ptr);
602             if (t)
603             {
604                 *max_clients = atoi(t);
605                 if (*max_clients  < 1)
606                     *max_clients = 1;
607             }
608         }
609     }
610     ptr = find_target_node(name);
611     if (ptr)
612     {
613         if (name)
614         {
615             url[0] = name;
616             url[1] = 0;
617         }
618         return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
619                            target_idletime, client_idletime,
620                            keepalive_limit_bw, keepalive_limit_pdu,
621                            pre_init, cql2rpn);
622     }
623 #else
624     *url = name;
625     return;
626 #endif
627 }
628
629