Logging options may be configured in config file
[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.11 2003-10-16 16:10:43 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 {
122     *pre_init = 0;
123     int no_url = 0;
124     ptr = ptr->children;
125     for (; ptr; ptr = ptr->next)
126     {
127         if (ptr->type == XML_ELEMENT_NODE 
128             && !strcmp((const char *) ptr->name, "preinit"))
129         {
130             const char *v = get_text(ptr);
131             *pre_init = v ? atoi(v) : 1;
132         }
133         if (ptr->type == XML_ELEMENT_NODE 
134             && !strcmp((const char *) ptr->name, "url"))
135         {
136             const char *t = get_text(ptr);
137             if (t && no_url < MAX_ZURL_PLEX)
138             {
139                 url[no_url++] = t;
140                 url[no_url] = 0;
141             }
142         }
143         if (ptr->type == XML_ELEMENT_NODE 
144             && !strcmp((const char *) ptr->name, "keepalive"))
145         {
146             int dummy;
147             *keepalive_limit_bw = 500000;
148             *keepalive_limit_pdu = 1000;
149             return_limit(ptr, keepalive_limit_bw, keepalive_limit_pdu,
150                          &dummy);
151         }
152         if (ptr->type == XML_ELEMENT_NODE 
153             && !strcmp((const char *) ptr->name, "limit"))
154             return_limit(ptr, limit_bw, limit_pdu, limit_req);
155         if (ptr->type == XML_ELEMENT_NODE 
156             && !strcmp((const char *) ptr->name, "target-timeout"))
157         {
158             const char *t = get_text(ptr);
159             if (t)
160             {
161                 *target_idletime = atoi(t);
162                 if (*target_idletime < 0)
163                     *target_idletime = 0;
164             }
165         }
166         if (ptr->type == XML_ELEMENT_NODE 
167             && !strcmp((const char *) ptr->name, "client-timeout"))
168         {
169             const char *t = get_text(ptr);
170             if (t)
171             {
172                 *client_idletime = atoi(t);
173                 if (*client_idletime < 0)
174                     *client_idletime = 0;
175             }
176         }
177     }
178 }
179 #endif
180
181 int Yaz_ProxyConfig::atoi_l(const char **cp)
182 {
183     int v = 0;
184     while (**cp && isdigit(**cp))
185     {
186         v = v*10 + (**cp - '0');
187         (*cp)++;
188     }
189     return v;
190 }
191
192 int Yaz_ProxyConfig::match_list(int v, const char *m)
193 {
194   while(m && *m)
195   {
196       while(*m && isspace(*m))
197           m++;
198       if (*m == '*')
199           return 1;
200       int l = atoi_l(&m);
201       int h = l;
202       if (*m == '-')
203       {
204           ++m;
205           h = atoi_l(&m);
206       }
207       if (v >= l && v <= h)
208           return 1;
209       if (*m == ',')
210           m++;
211   }
212   return 0;
213 }
214
215 #if HAVE_XML2
216 int Yaz_ProxyConfig::check_type_1_attributes(ODR odr, xmlNodePtr ptr,
217                                              Z_AttributeList *attrs,
218                                              char **addinfo)
219 {
220     for(ptr = ptr->children; ptr; ptr = ptr->next)
221     {
222         if (ptr->type == XML_ELEMENT_NODE &&
223             !strcmp((const char *) ptr->name, "attribute"))
224         {
225             const char *match_type = 0;
226             const char *match_value = 0;
227             const char *match_error = 0;
228             struct _xmlAttr *attr;
229             for (attr = ptr->properties; attr; attr = attr->next)
230             {
231                 if (!strcmp((const char *) attr->name, "type") &&
232                     attr->children && attr->children->type == XML_TEXT_NODE)
233                     match_type = (const char *) attr->children->content;
234                 if (!strcmp((const char *) attr->name, "value") &&
235                     attr->children && attr->children->type == XML_TEXT_NODE)
236                     match_value = (const char *) attr->children->content;
237                 if (!strcmp((const char *) attr->name, "error") &&
238                     attr->children && attr->children->type == XML_TEXT_NODE)
239                     match_error = (const char *) attr->children->content;
240             }
241             int i;
242
243             if (match_type && match_value)
244             {
245                 for (i = 0; i<attrs->num_attributes; i++)
246                 {
247                     Z_AttributeElement *el = attrs->attributes[i];
248                     char value_str[20];
249                     
250                     value_str[0] = '\0';
251                     if (!el->attributeType)
252                         continue;
253                     int type = *el->attributeType;
254
255                     if (!match_list(type, match_type))
256                         continue;
257                     if (el->which == Z_AttributeValue_numeric && 
258                         el->value.numeric)
259                     {
260                         if (!match_list(*el->value.numeric, match_value))
261                             continue;
262                         sprintf (value_str, "%d", *el->value.numeric);
263                     }
264                     else
265                         continue;
266                     if (match_error)
267                     {
268                         if (*value_str)
269                             *addinfo = odr_strdup(odr, value_str);
270                         return atoi(match_error);
271                     }
272                     return 0;
273                 }
274             }
275         }
276     }
277     return 0;
278 }
279 #endif
280
281 #if HAVE_XML2
282 int Yaz_ProxyConfig::check_type_1_structure(ODR odr, xmlNodePtr ptr,
283                                             Z_RPNStructure *q,
284                                             char **addinfo)
285 {
286     int c;
287     if (q->which == Z_RPNStructure_complex)
288     {
289         int e = check_type_1_structure(odr, ptr, q->u.complex->s1, addinfo);
290         if (e)
291             return e;
292         e = check_type_1_structure(odr, ptr, q->u.complex->s2, addinfo);
293         return e;
294     }
295     else if (q->which == Z_RPNStructure_simple)
296     {
297         if (q->u.simple->which == Z_Operand_APT)
298         {
299             return check_type_1_attributes(
300                 odr, ptr, q->u.simple->u.attributesPlusTerm->attributes,
301                 addinfo);
302         }
303     }
304     return 0;
305 }
306 #endif
307
308 #if HAVE_XML2
309 int Yaz_ProxyConfig::check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
310                                   char **addinfo)
311 {
312     // possibly check for Bib-1
313     return check_type_1_structure(odr, ptr, query->RPNStructure, addinfo);
314 }
315 #endif
316
317 int Yaz_ProxyConfig::check_query(ODR odr, const char *name, Z_Query *query,
318                                  char **addinfo)
319 {
320 #if HAVE_XML2
321     xmlNodePtr ptr;
322     
323     ptr = find_target_node(name);
324     if (ptr)
325     {
326         if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
327             return check_type_1(odr, ptr, query->u.type_1, addinfo);
328     }
329 #endif
330     return 0;
331 }
332
333 int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name,
334                                   Odr_oid *syntax, char **addinfo)
335 {
336 #if HAVE_XML2
337     xmlNodePtr ptr;
338     
339     ptr = find_target_node(name);
340     if (!ptr)
341         return 0;
342     for(ptr = ptr->children; ptr; ptr = ptr->next)
343     {
344         if (ptr->type == XML_ELEMENT_NODE &&
345             !strcmp((const char *) ptr->name, "syntax"))
346         {
347             int match = 0;  // if we match record syntax
348             const char *match_type = 0;
349             const char *match_error = 0;
350             const char *match_marcxml = 0;
351             struct _xmlAttr *attr;
352             for (attr = ptr->properties; attr; attr = attr->next)
353             {
354                 if (!strcmp((const char *) attr->name, "type") &&
355                     attr->children && attr->children->type == XML_TEXT_NODE)
356                     match_type = (const char *) attr->children->content;
357                 if (!strcmp((const char *) attr->name, "error") &&
358                     attr->children && attr->children->type == XML_TEXT_NODE)
359                     match_error = (const char *) attr->children->content;
360                 if (!strcmp((const char *) attr->name, "marcxml") &&
361                     attr->children && attr->children->type == XML_TEXT_NODE)
362                     match_marcxml = (const char *) attr->children->content;
363             }
364             if (match_type)
365             {
366                 if (!strcmp(match_type, "*"))
367                     match = 1;
368                 else if (!strcmp(match_type, "none"))
369                 {
370                     if (syntax == 0)
371                         match = 1;
372                 }
373                 else if (syntax)
374                 {
375                     int match_oid[OID_SIZE];
376                     oid_name_to_oid(CLASS_RECSYN, match_type, match_oid);
377                     if (oid_oidcmp(match_oid, syntax) == 0)
378                         match = 1;
379                 }
380             }
381             if (match)
382             {
383                 if (match_marcxml)
384                 {
385                     return -1;
386                 }
387                 if (match_error)
388                 {
389                     if (syntax)
390                     {
391                         char dotoid_str[100];
392                         oid_to_dotstring(syntax, dotoid_str);
393                         *addinfo = odr_strdup(odr, dotoid_str);
394                     }
395                     return atoi(match_error);
396                 }
397                 return 0;
398             }
399         }
400     }
401 #endif
402     return 0;
403 }
404
405 #if HAVE_XML2
406 xmlNodePtr Yaz_ProxyConfig::find_target_node(const char *name)
407 {
408     xmlNodePtr ptr;
409     if (!m_proxyPtr)
410         return 0;
411     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
412     {
413         if (ptr->type == XML_ELEMENT_NODE &&
414             !strcmp((const char *) ptr->name, "target"))
415         {
416             // default one ? 
417             if (!name)
418             {
419                 // <target default="1"> ?
420                 struct _xmlAttr *attr;
421                 for (attr = ptr->properties; attr; attr = attr->next)
422                     if (!strcmp((const char *) attr->name, "default") &&
423                         attr->children && attr->children->type == XML_TEXT_NODE)
424                     {
425                         xmlChar *t = attr->children->content;
426                         if (!t || *t == '1')
427                             return ptr;
428                     }
429             }
430             else
431             {
432                 // <target name="name"> ?
433                 struct _xmlAttr *attr;
434                 for (attr = ptr->properties; attr; attr = attr->next)
435                     if (!strcmp((const char *) attr->name, "name"))
436                     {
437                         if (attr->children
438                             && attr->children->type==XML_TEXT_NODE
439                             && attr->children->content 
440                             && (!strcmp((const char *) attr->children->content,
441                                         name)
442                                 || !strcmp((const char *) attr->children->content,
443                                            "*")))
444                         {
445                             return ptr;
446                         }
447                     }
448             }
449         }
450     }
451     return 0;
452 }
453 #endif
454
455 int Yaz_ProxyConfig::get_target_no(int no,
456                                    const char **name,
457                                    const char **url,
458                                    int *limit_bw,
459                                    int *limit_pdu,
460                                    int *limit_req,
461                                    int *target_idletime,
462                                    int *client_idletime,
463                                    int *max_clients,
464                                    int *keepalive_limit_bw,
465                                    int *keepalive_limit_pdu,
466                                    int *pre_init)
467 {
468 #if HAVE_XML2
469     xmlNodePtr ptr;
470     if (!m_proxyPtr)
471         return 0;
472     int i = 0;
473     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
474         if (ptr->type == XML_ELEMENT_NODE &&
475             !strcmp((const char *) ptr->name, "target"))
476         {
477             if (i == no)
478             {
479                 struct _xmlAttr *attr;
480                 for (attr = ptr->properties; attr; attr = attr->next)
481                     if (!strcmp((const char *) attr->name, "name"))
482                     {
483                         if (attr->children
484                             && attr->children->type==XML_TEXT_NODE
485                             && attr->children->content)
486                             *name = (const char *) attr->children->content;
487                     }
488                 return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
489                                    target_idletime, client_idletime,
490                                    keepalive_limit_bw, keepalive_limit_pdu,
491                                    pre_init);
492                 return 1;
493             }
494             i++;
495         }
496 #endif
497     return 0;
498 }
499
500 int Yaz_ProxyConfig::mycmp(const char *hay, const char *item, int len)
501 {
502     if (len == strlen(item) && memcmp(hay, item, len) == 0)
503         return 1;
504     return 0;
505 }
506
507 void Yaz_ProxyConfig::get_generic_info(int *log_mask,
508                                        int *max_clients)
509 {
510 #if HAVE_XML2
511     xmlNodePtr ptr;
512     if (!m_proxyPtr)
513         return;
514     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
515     {
516         if (ptr->type == XML_ELEMENT_NODE 
517             && !strcmp((const char *) ptr->name, "log"))
518         {
519             const char *v = get_text(ptr);
520             *log_mask = 0;
521             while (v && *v)
522             {
523                 const char *cp = v;
524                 while (*cp && *cp != ',' && !isspace(*cp))
525                     cp++;
526                 size_t len = cp - v;
527                 if (mycmp(v, "client-apdu", len))
528                     *log_mask |= PROXY_LOG_APDU_CLIENT;
529                 if (mycmp(v, "server-apdu", len))
530                     *log_mask |= PROXY_LOG_APDU_SERVER;
531                 if (mycmp(v, "client-requests", len))
532                     *log_mask |= PROXY_LOG_REQ_CLIENT;
533                 if (mycmp(v, "server-requests", len))
534                     *log_mask |= PROXY_LOG_REQ_SERVER;
535                 if (isdigit(*v))
536                     *log_mask |= atoi(v);
537                 if (*cp == ',')
538                     cp++;
539                 while (*cp && isspace(*cp))
540                     cp++;
541                 v = cp;
542             }
543         }
544         if (ptr->type == XML_ELEMENT_NODE &&
545             !strcmp((const char *) ptr->name, "max-clients"))
546         {
547             const char *t = get_text(ptr);
548             if (t)
549             {
550                 *max_clients = atoi(t);
551                 if (*max_clients  < 1)
552                     *max_clients = 1;
553             }
554         }
555     }
556 #endif
557 }
558
559 void Yaz_ProxyConfig::get_target_info(const char *name,
560                                       const char **url,
561                                       int *limit_bw,
562                                       int *limit_pdu,
563                                       int *limit_req,
564                                       int *target_idletime,
565                                       int *client_idletime,
566                                       int *max_clients,
567                                       int *keepalive_limit_bw,
568                                       int *keepalive_limit_pdu,
569                                       int *pre_init)
570 {
571 #if HAVE_XML2
572     xmlNodePtr ptr;
573     if (!m_proxyPtr)
574     {
575         url[0] = name;
576         url[1] = 0;
577         return;
578     }
579     url[0] = 0;
580     for (ptr = m_proxyPtr->children; ptr; ptr = ptr->next)
581     {
582         if (ptr->type == XML_ELEMENT_NODE &&
583             !strcmp((const char *) ptr->name, "max-clients"))
584         {
585             const char *t = get_text(ptr);
586             if (t)
587             {
588                 *max_clients = atoi(t);
589                 if (*max_clients  < 1)
590                     *max_clients = 1;
591             }
592         }
593     }
594     ptr = find_target_node(name);
595     if (ptr)
596     {
597         if (name)
598         {
599             url[0] = name;
600             url[1] = 0;
601         }
602         return_target_info(ptr, url, limit_bw, limit_pdu, limit_req,
603                            target_idletime, client_idletime,
604                            keepalive_limit_bw, keepalive_limit_pdu,
605                            pre_init);
606     }
607 #else
608     *url = name;
609     return;
610 #endif
611 }
612
613