Service may be POSTed for command=init.
[pazpar2-moved-to-github.git] / src / pazpar2_config.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25 #include <assert.h>
26
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29 #include <libxslt/xslt.h>
30 #include <libxslt/transform.h>
31 #include <libxslt/xsltutils.h>
32
33 #include <yaz/yaz-util.h>
34 #include <yaz/nmem.h>
35 #include <yaz/snprintf.h>
36 #include <yaz/tpath.h>
37
38 #if HAVE_GLOB_H
39 #define USE_POSIX_GLOB 1
40 #else
41 #define USE_POSIX_GLOB 0
42 #endif
43
44
45 #if USE_POSIX_GLOB
46 #include <glob.h>
47 #endif
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #if HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53 #include "pazpar2_config.h"
54 #include "settings.h"
55 #include "eventl.h"
56 #include "http.h"
57
58 struct conf_config
59 {
60     NMEM nmem; /* for conf_config and servers memory */
61     struct conf_server *servers;
62     WRBUF confdir;
63 };
64
65
66 static char *parse_settings(struct conf_config *config,
67                             NMEM nmem, xmlNode *node);
68
69 static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
70                                                         xmlNode *node);
71
72 static void conf_metadata_assign(NMEM nmem, 
73                                  struct conf_metadata * metadata,
74                                  const char *name,
75                                  enum conf_metadata_type type,
76                                  enum conf_metadata_merge merge,
77                                  enum conf_setting_type setting,
78                                  int brief,
79                                  int termlist,
80                                  int rank,
81                                  int sortkey_offset,
82                                  enum conf_metadata_mergekey mt)
83 {
84     assert(nmem && metadata && name);
85     
86     metadata->name = nmem_strdup(nmem, name);
87
88     metadata->type = type;
89
90     // enforcing that type_year is always range_merge
91     if (metadata->type == Metadata_type_year)
92         metadata->merge = Metadata_merge_range;
93     else
94         metadata->merge = merge;    
95
96     metadata->setting = setting;
97     metadata->brief = brief;   
98     metadata->termlist = termlist;
99     metadata->rank = rank;    
100     metadata->sortkey_offset = sortkey_offset;
101     metadata->mergekey = mt;
102 }
103
104
105 static void conf_sortkey_assign(NMEM nmem, 
106                                 struct conf_sortkey * sortkey,
107                                 const char *name,
108                                 enum conf_sortkey_type type)
109 {
110     assert(nmem && sortkey && name);
111     
112     sortkey->name = nmem_strdup(nmem, name);
113     sortkey->type = type;
114 }
115
116
117 static struct conf_service *service_init(struct conf_server *server,
118                                          int num_metadata, int num_sortkeys,
119                                          const char *service_id)
120 {
121     struct conf_service * service = 0;
122     NMEM nmem = nmem_create();
123
124     service = nmem_malloc(nmem, sizeof(struct conf_service));
125     service->ref_count = 1;
126     service->nmem = nmem;
127     service->next = 0;
128     service->settings = 0;
129     service->databases = 0;
130     service->targetprofiles = 0;
131     service->server = server;
132     service->session_timeout = 60; /* default session timeout */
133     service->z3950_session_timeout = 180;
134     service->z3950_connect_timeout = 15;
135
136     service->relevance_pct = 0;
137     service->sort_pct = 0;
138     service->mergekey_pct = 0;
139
140     service->id = service_id ? nmem_strdup(nmem, service_id) : 0;
141     service->num_metadata = num_metadata;
142     service->metadata = 0;
143     if (service->num_metadata)
144       service->metadata 
145           = nmem_malloc(nmem, 
146                         sizeof(struct conf_metadata) * service->num_metadata);
147     service->num_sortkeys = num_sortkeys;
148     service->sortkeys = 0;
149     if (service->num_sortkeys)
150         service->sortkeys 
151             = nmem_malloc(nmem, 
152                           sizeof(struct conf_sortkey) * service->num_sortkeys);
153     service->dictionary = 0;
154     return service; 
155 }
156
157 static struct conf_metadata* conf_service_add_metadata(
158     struct conf_service *service,
159     int field_id,
160     const char *name,
161     enum conf_metadata_type type,
162     enum conf_metadata_merge merge,
163     enum conf_setting_type setting,
164     int brief,
165     int termlist,
166     int rank,
167     int sortkey_offset,
168     enum conf_metadata_mergekey mt)
169 {
170     struct conf_metadata * md = 0;
171
172     if (!service || !service->metadata || !service->num_metadata
173         || field_id < 0  || !(field_id < service->num_metadata))
174         return 0;
175
176     md = service->metadata + field_id;
177     conf_metadata_assign(service->nmem, md, name, type, merge, setting,
178                          brief, termlist, rank, sortkey_offset,
179                          mt);
180     return md;
181 }
182
183
184 static struct conf_sortkey * conf_service_add_sortkey(
185     struct conf_service *service,
186     int field_id,
187     const char *name,
188     enum conf_sortkey_type type)
189 {
190     struct conf_sortkey * sk = 0;
191
192     if (!service || !service->sortkeys || !service->num_sortkeys
193         || field_id < 0  || !(field_id < service->num_sortkeys))
194         return 0;
195
196     //sk = &((service->sortkeys)[field_id]);
197     sk = service->sortkeys + field_id;
198     conf_sortkey_assign(service->nmem, sk, name, type);
199
200     return sk;
201 }
202
203
204 int conf_service_metadata_field_id(struct conf_service *service,
205                                    const char * name)
206 {
207     int i = 0;
208
209     if (!service || !service->metadata || !service->num_metadata)
210         return -1;
211
212     for(i = 0; i < service->num_metadata; i++) {
213         if (!strcmp(name, (service->metadata[i]).name))
214             return i;
215     }
216    
217     return -1;
218 }
219
220
221 int conf_service_sortkey_field_id(struct conf_service *service,
222                                   const char * name)
223 {
224     int i = 0;
225
226     if (!service || !service->sortkeys || !service->num_sortkeys)
227         return -1;
228
229     for(i = 0; i < service->num_sortkeys; i++) {
230         if (!strcmp(name, (service->sortkeys[i]).name))
231             return i;
232     }
233    
234     return -1;
235 }
236
237 static void conf_dir_path(struct conf_config *config, WRBUF w, const char *src)
238 {
239     if (config->confdir && wrbuf_len(config->confdir) > 0 &&
240         !yaz_is_abspath(src))
241     {
242         wrbuf_printf(w, "%s/%s", wrbuf_cstr(config->confdir), src);
243     }
244     else
245         wrbuf_puts(w, src);
246 }
247
248 void service_destroy(struct conf_service *service)
249 {
250     if (service)
251     {
252         assert(service->ref_count > 0);
253         service->ref_count--;
254         if (service->ref_count == 0)
255         {
256             pp2_charset_destroy(service->relevance_pct);
257             pp2_charset_destroy(service->sort_pct);
258             pp2_charset_destroy(service->mergekey_pct);
259             nmem_destroy(service->nmem);
260         }
261     }
262 }
263
264 void service_incref(struct conf_service *service)
265 {
266     service->ref_count++;
267 }
268
269 static int parse_metadata(struct conf_service *service, xmlNode *n,
270                           int *md_node, int *sk_node)
271 {
272     xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
273     xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
274     xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
275     xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
276     xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
277     xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
278     xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
279     xmlChar *xml_setting = xmlGetProp(n, (xmlChar *) "setting");
280     xmlChar *xml_mergekey = xmlGetProp(n, (xmlChar *) "mergekey");
281     
282     enum conf_metadata_type type = Metadata_type_generic;
283     enum conf_metadata_merge merge = Metadata_merge_no;
284     enum conf_setting_type setting = Metadata_setting_no;
285     enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
286     enum conf_metadata_mergekey mergekey_type = Metadata_mergekey_no;
287     int brief = 0;
288     int termlist = 0;
289     int rank = 0;
290     int sortkey_offset = 0;
291     
292     // now do the parsing logic
293     if (!xml_name)
294     {
295         yaz_log(YLOG_FATAL, "Must specify name in metadata element");
296         return -1;
297     }
298     if (xml_brief)
299     {
300         if (!strcmp((const char *) xml_brief, "yes"))
301             brief = 1;
302         else if (strcmp((const char *) xml_brief, "no"))
303         {
304             yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
305             return -1;
306         }
307     }
308     else
309         brief = 0;
310     
311     if (xml_termlist)
312     {
313         if (!strcmp((const char *) xml_termlist, "yes"))
314             termlist = 1;
315         else if (strcmp((const char *) xml_termlist, "no"))
316         {
317             yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
318             return -1;
319         }
320     }
321     else
322         termlist = 0;
323     
324     if (xml_rank)
325         rank = atoi((const char *) xml_rank);
326     else
327         rank = 0;
328     
329     if (xml_type)
330     {
331         if (!strcmp((const char *) xml_type, "generic"))
332             type = Metadata_type_generic;
333         else if (!strcmp((const char *) xml_type, "year"))
334             type = Metadata_type_year;
335         else if (!strcmp((const char *) xml_type, "date"))
336             type = Metadata_type_date;
337         else
338         {
339             yaz_log(YLOG_FATAL, 
340                     "Unknown value for metadata/type: %s", xml_type);
341             return -1;
342         }
343     }
344     else
345         type = Metadata_type_generic;
346     
347     if (xml_merge)
348     {
349         if (!strcmp((const char *) xml_merge, "no"))
350             merge = Metadata_merge_no;
351         else if (!strcmp((const char *) xml_merge, "unique"))
352             merge = Metadata_merge_unique;
353         else if (!strcmp((const char *) xml_merge, "longest"))
354             merge = Metadata_merge_longest;
355         else if (!strcmp((const char *) xml_merge, "range"))
356             merge = Metadata_merge_range;
357         else if (!strcmp((const char *) xml_merge, "all"))
358             merge = Metadata_merge_all;
359         else
360         {
361             yaz_log(YLOG_FATAL, 
362                     "Unknown value for metadata/merge: %s", xml_merge);
363             return -1;
364         }
365     }
366     else
367         merge = Metadata_merge_no;
368     
369     if (xml_setting)
370     {
371         if (!strcmp((const char *) xml_setting, "no"))
372             setting = Metadata_setting_no;
373         else if (!strcmp((const char *) xml_setting, "postproc"))
374             setting = Metadata_setting_postproc;
375         else if (!strcmp((const char *) xml_setting, "parameter"))
376             setting = Metadata_setting_parameter;
377         else
378         {
379             yaz_log(YLOG_FATAL,
380                     "Unknown value for medadata/setting: %s", xml_setting);
381             return -1;
382         }
383     }
384     
385     // add a sortkey if so specified
386     if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
387     {
388         if (merge == Metadata_merge_no)
389         {
390             yaz_log(YLOG_FATAL, 
391                     "Can't specify sortkey on a non-merged field");
392             return -1;
393         }
394         if (!strcmp((const char *) xml_sortkey, "numeric"))
395             sk_type = Metadata_sortkey_numeric;
396         else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
397             sk_type = Metadata_sortkey_skiparticle;
398         else
399         {
400             yaz_log(YLOG_FATAL,
401                     "Unknown sortkey in metadata element: %s", 
402                     xml_sortkey);
403             return -1;
404         }
405         sortkey_offset = *sk_node;
406         
407         conf_service_add_sortkey(service, *sk_node,
408                                  (const char *) xml_name, sk_type);
409         
410         (*sk_node)++;
411     }
412     else
413         sortkey_offset = -1;
414     
415     if (xml_mergekey && strcmp((const char *) xml_mergekey, "no"))
416     {
417         mergekey_type = Metadata_mergekey_yes;
418     }
419     
420     
421     // metadata known, assign values
422     conf_service_add_metadata(service, *md_node,
423                               (const char *) xml_name,
424                               type, merge, setting,
425                               brief, termlist, rank, sortkey_offset,
426                               mergekey_type);
427     
428     xmlFree(xml_name);
429     xmlFree(xml_brief);
430     xmlFree(xml_sortkey);
431     xmlFree(xml_merge);
432     xmlFree(xml_type);
433     xmlFree(xml_termlist);
434     xmlFree(xml_rank);
435     xmlFree(xml_setting);
436     (*md_node)++;
437     return 0;
438 }
439
440 static struct conf_service *service_create_static(struct conf_server *server,
441                                                   xmlNode *node,
442                                                   const char *service_id)
443 {
444     xmlNode *n;
445     int md_node = 0;
446     int sk_node = 0;
447
448     struct conf_service *service = 0;
449     int num_metadata = 0;
450     int num_sortkeys = 0;
451     int got_settings = 0;
452     
453     // count num_metadata and num_sortkeys
454     for (n = node->children; n; n = n->next)
455         if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
456                                                    n->name, "metadata"))
457         {
458             xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
459             num_metadata++;
460             if (sortkey && strcmp((const char *) sortkey, "no"))
461                 num_sortkeys++;
462             xmlFree(sortkey);
463         }
464
465     service = service_init(server, num_metadata, num_sortkeys, service_id);
466
467     for (n = node->children; n; n = n->next)
468     {
469         if (n->type != XML_ELEMENT_NODE)
470             continue;
471         if (!strcmp((const char *) n->name, "timeout"))
472         {
473             xmlChar *src = xmlGetProp(n, (xmlChar *) "session");
474             if (src)
475             {
476                 service->session_timeout = atoi((const char *) src);
477                 xmlFree(src);
478                 if (service->session_timeout < 9)
479                 {
480                     yaz_log(YLOG_FATAL, "session timeout out of range");
481                     return 0;
482                 }
483             }
484             src = xmlGetProp(n, (xmlChar *) "z3950_connect");
485             if (src)
486             {
487                 service->z3950_connect_timeout = atoi((const char *) src);
488                 xmlFree(src);
489                 if (service->z3950_session_timeout < 9)
490                 {
491                     yaz_log(YLOG_FATAL, "Z39.50 connect timeout out of range");
492                     return 0;
493                 }
494             }
495             src = xmlGetProp(n, (xmlChar *) "z3950_session");
496             if (src)
497             {
498                 service->z3950_session_timeout = atoi((const char *) src);
499                 xmlFree(src);
500                 if (service->z3950_session_timeout < 9)
501                 {
502                     yaz_log(YLOG_FATAL, "Z39.50 session timeout out of range");
503                     return 0;
504                 }
505             }
506         }
507         else if (!strcmp((const char *) n->name, "settings"))
508             got_settings++;
509         else if (!strcmp((const char *) n->name, (const char *) "targetprofiles"))
510         {
511             if (service->targetprofiles)
512             {
513                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
514                 return 0;
515             }
516             if (!(service->targetprofiles = 
517                   parse_targetprofiles(service->nmem, n)))
518                 return 0;
519         }
520         else if (!strcmp((const char *) n->name, "relevance"))
521         {
522             if (service->relevance_pct)
523             {
524                 yaz_log(YLOG_LOG, "relevance may not repeat in service");
525                 return 0;
526             }
527             else
528             {
529                 service->relevance_pct = pp2_charset_create_xml(n);
530                 if (!service->relevance_pct)
531                     return 0;
532             }
533         }
534         else if (!strcmp((const char *) n->name, "sort"))
535         {
536             if (service->sort_pct)
537             {
538                 yaz_log(YLOG_LOG, "sort may not repeat in service");
539                 return 0;
540             }
541             else
542             {
543                 service->sort_pct = pp2_charset_create_xml(n);
544                 if (!service->sort_pct)
545                     return 0;
546             }
547         }
548         else if (!strcmp((const char *) n->name, "mergekey"))
549         {
550             if (service->mergekey_pct)
551             {
552                 yaz_log(YLOG_LOG, "mergekey may not repeat in service");
553                 return 0;
554             }
555             else
556             {
557                 service->mergekey_pct = pp2_charset_create_xml(n);
558                 if (!service->mergekey_pct)
559                     return 0;
560             }
561         }
562         else if (!strcmp((const char *) n->name, (const char *) "metadata"))
563         {
564             if (parse_metadata(service, n, &md_node, &sk_node))
565                 return 0;
566         }
567         else
568         {
569             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
570             return 0;
571         }
572     }
573     if (got_settings)
574     {
575         int pass;
576         /* metadata has been read.. Consider now settings */
577         init_settings(service);
578         for (pass = 1; pass <= 2; pass++)
579         {
580             for (n = node->children; n; n = n->next)
581             {
582                 if (n->type != XML_ELEMENT_NODE)
583                     continue;
584                 if (!strcmp((const char *) n->name, "settings"))
585                 {
586                     xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
587                     if (src)
588                     {
589                         WRBUF w = wrbuf_alloc();
590                         conf_dir_path(server->config, w, (const char *) src);
591                         settings_read_file(service, wrbuf_cstr(w), pass);
592                         wrbuf_destroy(w);
593                         xmlFree(src);
594                     }
595                     else
596                     {
597                         settings_read_node(service, n, pass);
598                     }
599                 }
600             }
601         }
602     }
603     return service;
604 }
605
606 static char *parse_settings(struct conf_config *config,
607                             NMEM nmem, xmlNode *node)
608 {
609     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
610     char *r;
611
612     if (src)
613     {
614         WRBUF w = wrbuf_alloc();
615         conf_dir_path(config, w, (const char *) src);
616         r = nmem_strdup(nmem, wrbuf_cstr(w));
617         wrbuf_destroy(w);
618     }
619     else
620     {
621         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
622         return 0;
623     }
624     xmlFree(src);
625     return r;
626 }
627
628 static void inherit_server_settings(struct conf_service *s)
629 {
630     struct conf_server *server = s->server;
631     if (!s->dictionary) /* service has no config settings ? */
632     {
633         if (server->server_settings)
634         {
635             /* inherit settings from server */
636             init_settings(s);
637             settings_read_file(s, server->server_settings, 1);
638             settings_read_file(s, server->server_settings, 2);
639         }
640         else
641         {
642             yaz_log(YLOG_WARN, "service '%s' has no settings",
643                     s->id ? s->id : "unnamed");
644             init_settings(s);
645         }
646     }
647     
648     /* use relevance/sort/mergekey from server if not defined
649        for this service.. */
650     if (!s->relevance_pct)
651     {
652         if (server->relevance_pct)
653         {
654             s->relevance_pct = server->relevance_pct;
655             pp2_charset_incref(s->relevance_pct);
656         }
657         else
658             s->relevance_pct = pp2_charset_create(0);
659     }
660     
661     if (!s->sort_pct)
662     {
663         if (server->sort_pct)
664         {
665             s->sort_pct = server->sort_pct;
666             pp2_charset_incref(s->sort_pct);
667         }
668         else
669             s->sort_pct = pp2_charset_create(0);
670     }
671     
672     if (!s->mergekey_pct)
673     {
674         if (server->mergekey_pct)
675         {
676             s->mergekey_pct = server->mergekey_pct;
677             pp2_charset_incref(s->mergekey_pct);
678         }
679         else
680             s->mergekey_pct = pp2_charset_create(0);
681     }
682 }
683
684 struct conf_service *service_create(struct conf_server *server,
685                                     xmlNode *node)
686 {
687     struct conf_service *service = service_create_static(server,
688                                                          node, 0);
689     if (service)
690     {
691         inherit_server_settings(service);
692         resolve_databases(service);
693     }
694     return service;
695 }
696
697 static struct conf_server *server_create(struct conf_config *config,
698                                          NMEM nmem, xmlNode *node)
699 {
700     xmlNode *n;
701     struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
702
703     server->host = 0;
704     server->port = 0;
705     server->proxy_host = 0;
706     server->proxy_port = 0;
707     server->myurl = 0;
708     server->proxy_addr = 0;
709     server->service = 0;
710     server->config = config;
711     server->next = 0;
712     server->relevance_pct = 0;
713     server->sort_pct = 0;
714     server->mergekey_pct = 0;
715     server->server_settings = 0;
716
717     for (n = node->children; n; n = n->next)
718     {
719         if (n->type != XML_ELEMENT_NODE)
720             continue;
721         if (!strcmp((const char *) n->name, "listen"))
722         {
723             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
724             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
725             if (port)
726                 server->port = atoi((const char *) port);
727             if (host)
728                 server->host = nmem_strdup(nmem, (const char *) host);
729             xmlFree(port);
730             xmlFree(host);
731         }
732         else if (!strcmp((const char *) n->name, "proxy"))
733         {
734             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
735             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
736             xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
737             if (port)
738                 server->proxy_port = atoi((const char *) port);
739             if (host)
740                 server->proxy_host = nmem_strdup(nmem, (const char *) host);
741             if (myurl)
742                 server->myurl = nmem_strdup(nmem, (const char *) myurl);
743             xmlFree(port);
744             xmlFree(host);
745             xmlFree(myurl);
746         }
747         else if (!strcmp((const char *) n->name, "settings"))
748         {
749             if (server->server_settings)
750             {
751                 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
752                 return 0;
753             }
754             if (!(server->server_settings = parse_settings(config, nmem, n)))
755                 return 0;
756         }
757         else if (!strcmp((const char *) n->name, "relevance"))
758         {
759             server->relevance_pct = pp2_charset_create_xml(n);
760             if (!server->relevance_pct)
761                 return 0;
762         }
763         else if (!strcmp((const char *) n->name, "sort"))
764         {
765             server->sort_pct = pp2_charset_create_xml(n);
766             if (!server->sort_pct)
767                 return 0;
768         }
769         else if (!strcmp((const char *) n->name, "mergekey"))
770         {
771             server->mergekey_pct = pp2_charset_create_xml(n);
772             if (!server->mergekey_pct)
773                 return 0;
774         }
775         else if (!strcmp((const char *) n->name, "service"))
776         {
777             char *service_id = (char *)
778                 xmlGetProp(n, (xmlChar *) "id");
779
780             struct conf_service **sp = &server->service;
781             for (; *sp; sp = &(*sp)->next)
782                 if ((*sp)->id && service_id &&
783                     0 == strcmp((*sp)->id, service_id))
784                 {
785                     yaz_log(YLOG_FATAL, "Duplicate service: %s", service_id);
786                     break;
787                 }
788                 else if (!(*sp)->id && !service_id)
789                 {
790                     yaz_log(YLOG_FATAL, "Duplicate unnamed service '%s'",
791                         service_id);
792                     break;
793                 }
794
795             if (*sp)  /* service already exist */
796             {
797                 xmlFree(service_id);
798                 return 0;
799             }
800             else
801             {
802                 struct conf_service *s = service_create_static(server, n,
803                                                                service_id);
804                 xmlFree(service_id);
805                 if (!s)
806                     return 0;
807                 *sp = s;
808             }
809         }
810         else
811         {
812             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
813             return 0;
814         }
815     }
816     if (server->service)
817     {
818         struct conf_service *s;
819         for (s = server->service; s; s = s->next)
820             inherit_server_settings(s);
821     }
822     return server;
823 }
824
825 xsltStylesheet *conf_load_stylesheet(struct conf_service *service,
826                                      const char *fname)
827 {
828     struct conf_config *config = service->server->config;
829     WRBUF w = wrbuf_alloc();
830     xsltStylesheet *s;
831
832     conf_dir_path(config, w, fname);
833     s = xsltParseStylesheetFile((xmlChar *) wrbuf_cstr(w));
834     wrbuf_destroy(w);
835     return s;
836 }
837
838 static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
839                                                         xmlNode *node)
840 {
841     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
842     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
843     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
844
845     memset(r, 0, sizeof(*r));
846
847     if (type)
848     {
849         if (!strcmp((const char *) type, "local"))
850             r->type = Targetprofiles_local;
851         else
852         {
853             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
854             return 0;
855         }
856     }
857     else
858     {
859         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
860         return 0;
861     }
862
863     if (src)
864         r->src = nmem_strdup(nmem, (const char *) src);
865     else
866     {
867         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
868         return 0;
869     }
870     xmlFree(type);
871     xmlFree(src);
872     return r;
873 }
874
875 struct conf_service *locate_service(struct conf_server *server,
876                                     const char *service_id)
877 {
878     struct conf_service *s = server->service;
879     for (; s; s = s->next)
880         if (s->id && service_id && 0 == strcmp(s->id, service_id))
881             return s;
882         else if (!s->id && !service_id)
883             return s;
884     return 0;
885 }
886
887
888 static int parse_config(struct conf_config *config, xmlNode *root)
889 {
890     xmlNode *n;
891
892     for (n = root->children; n; n = n->next)
893     {
894         if (n->type != XML_ELEMENT_NODE)
895             continue;
896         if (!strcmp((const char *) n->name, "server"))
897         {
898             struct conf_server *tmp = server_create(config, config->nmem, n);
899             if (!tmp)
900                 return -1;
901             tmp->next = config->servers;
902             config->servers = tmp;
903         }
904         else if (!strcmp((const char *) n->name, "targetprofiles"))
905         {
906             yaz_log(YLOG_FATAL, "targetprofiles unsupported here. Must be part of service");
907             return -1;
908
909         }
910         else
911         {
912             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
913             return -1;
914         }
915     }
916     return 0;
917 }
918
919 static int process_config_includes(struct conf_config *config, xmlNode *n);
920
921 static int config_include_one(struct conf_config *config, xmlNode **sib,
922     const char *path)
923 {
924     struct stat st;
925     if (stat(path, &st) < 0)
926     {
927         yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
928         return -1;
929     }
930     else
931     {
932         if ((st.st_mode & S_IFMT) == S_IFREG)
933         {
934             xmlDoc *doc = xmlParseFile(path);
935             if (doc)
936             {
937                 xmlNodePtr t = xmlDocGetRootElement(doc);
938                 int ret = process_config_includes(config, t);
939                 *sib = xmlAddNextSibling(*sib, xmlCopyNode(t, 1));
940                 xmlFreeDoc(doc);
941                 if (ret)
942                     return -1;
943             }
944             else
945             {
946                 yaz_log(YLOG_FATAL, "Could not parse %s", path);
947                 return -1;
948             }
949         }
950     }
951     return 0;
952 }
953
954 static int config_include_src(struct conf_config *config, xmlNode **np,
955                               const char *src)
956 {
957     int ret = 0; /* return code. OK so far */
958     WRBUF w = wrbuf_alloc();
959     xmlNodePtr sib; /* our sibling that we append */
960     xmlNodePtr c; /* tmp node */
961
962     wrbuf_printf(w, " begin include src=\"%s\" ", src);
963
964     /* replace include element with a 'begin' comment */
965     sib = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
966     xmlReplaceNode(*np, sib);
967
968     xmlFreeNode(*np);
969
970     wrbuf_rewind(w);
971     conf_dir_path(config, w, src);
972 #if USE_POSIX_GLOB
973     {
974         size_t i;
975         glob_t glob_res;
976         glob(wrbuf_cstr(w), 0 /* flags */, 0 /* errfunc */, &glob_res);
977         
978         for (i = 0; ret == 0 && i < glob_res.gl_pathc; i++)
979         {
980             const char *path = glob_res.gl_pathv[i];
981             ret = config_include_one(config, &sib, path);
982         }
983         globfree(&glob_res);
984     }
985 #else
986     ret = config_include_one(config, &sib, wrbuf_cstr(w));
987 #endif
988     wrbuf_rewind(w);
989     wrbuf_printf(w, " end include src=\"%s\" ", src);
990     c = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
991     sib = xmlAddNextSibling(sib, c);
992     
993     *np = sib;
994     wrbuf_destroy(w);
995     return ret;
996 }
997
998 static int process_config_includes(struct conf_config *config, xmlNode *n)
999 {
1000     for (; n; n = n->next)
1001     {
1002         if (n->type == XML_ELEMENT_NODE)
1003         {
1004             if (!strcmp((const char *) n->name, "include"))
1005             {
1006                 xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
1007                 if (src)
1008                 {
1009                     int ret = config_include_src(config, &n,
1010                                                  (const char *) src);
1011                     xmlFree(src);
1012                     if (ret)
1013                         return ret;
1014                         
1015                 }
1016             }
1017             else
1018             {
1019                 if (process_config_includes(config, n->children))
1020                     return -1;
1021             }
1022         }
1023     }
1024     return 0;
1025 }
1026
1027 struct conf_config *config_create(const char *fname, int verbose)
1028 {
1029     xmlDoc *doc = xmlParseFile(fname);
1030     xmlNode *n;
1031     const char *p;
1032     int r;
1033     NMEM nmem = nmem_create();
1034     struct conf_config *config = nmem_malloc(nmem, sizeof(struct conf_config));
1035
1036     xmlSubstituteEntitiesDefault(1);
1037     xmlLoadExtDtdDefaultValue = 1;
1038     if (!doc)
1039     {
1040         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
1041         nmem_destroy(nmem);
1042         return 0;
1043     }
1044
1045     config->nmem = nmem;
1046     config->servers = 0;
1047
1048     config->confdir = wrbuf_alloc();
1049     if ((p = strrchr(fname, 
1050 #ifdef WIN32
1051                      '\\'
1052 #else
1053                      '/'
1054 #endif
1055              )))
1056     {
1057         int len = p - fname;
1058         wrbuf_write(config->confdir, fname, len);
1059     }
1060     wrbuf_puts(config->confdir, "");
1061     
1062     n = xmlDocGetRootElement(doc);
1063     r = process_config_includes(config, n);
1064     if (r == 0) /* OK */
1065     {
1066         if (verbose)
1067         {
1068             yaz_log(YLOG_LOG, "Configuration %s after include processing",
1069                     fname);
1070             xmlDocFormatDump(yaz_log_file(), doc, 0);
1071         }
1072         r = parse_config(config, n);
1073     }
1074     xmlFreeDoc(doc);
1075
1076     if (r)
1077     {
1078         config_destroy(config);
1079         return 0;
1080     }
1081     return config;
1082 }
1083
1084 void server_destroy(struct conf_server *server)
1085 {
1086     struct conf_service *s = server->service;
1087     while (s)
1088     {
1089         struct conf_service *s_next = s->next;
1090         service_destroy(s);
1091         s = s_next;
1092     }
1093     pp2_charset_destroy(server->relevance_pct);
1094     pp2_charset_destroy(server->sort_pct);
1095     pp2_charset_destroy(server->mergekey_pct);
1096 }
1097
1098 void config_destroy(struct conf_config *config)
1099 {
1100     if (config)
1101     {
1102         struct conf_server *server = config->servers;
1103         while (server)
1104         {
1105             struct conf_server *s_next = server->next;
1106             server_destroy(server);
1107             server = s_next;
1108         }
1109         wrbuf_destroy(config->confdir);
1110         nmem_destroy(config->nmem);
1111     }
1112 }
1113
1114 void config_stop_listeners(struct conf_config *conf)
1115 {
1116     struct conf_server *ser;
1117     for (ser = conf->servers; ser; ser = ser->next)
1118         http_close_server(ser);
1119 }
1120
1121 void config_start_databases(struct conf_config *conf)
1122 {
1123     struct conf_server *ser;
1124     for (ser = conf->servers; ser; ser = ser->next)
1125     {
1126         struct conf_service *s = ser->service;
1127         for (;s ; s = s->next)
1128             resolve_databases(s);
1129     }
1130 }
1131
1132 int config_start_listeners(struct conf_config *conf,
1133                            const char *listener_override)
1134 {
1135     struct conf_server *ser;
1136     for (ser = conf->servers; ser; ser = ser->next)
1137     {
1138         WRBUF w = wrbuf_alloc();
1139         int r;
1140         if (listener_override)
1141         {
1142             wrbuf_puts(w, listener_override);
1143             listener_override = 0; /* only first server is overriden */
1144         }
1145         else
1146         {
1147             if (ser->host)
1148                 wrbuf_puts(w, ser->host);
1149             if (ser->port)
1150             {
1151                 if (wrbuf_len(w))
1152                     wrbuf_puts(w, ":");
1153                 wrbuf_printf(w, "%d", ser->port);
1154             }
1155         }
1156         r = http_init(wrbuf_cstr(w), ser);
1157         wrbuf_destroy(w);
1158         if (r)
1159             return -1;
1160
1161         w = wrbuf_alloc();
1162         if (ser->proxy_host || ser->proxy_port)
1163         {
1164             if (ser->proxy_host)
1165                 wrbuf_puts(w, ser->proxy_host);
1166             if (ser->proxy_port)
1167             {
1168                 if (wrbuf_len(w))
1169                     wrbuf_puts(w, ":");
1170                 wrbuf_printf(w, "%d", ser->proxy_port);
1171             }
1172         }
1173         if (wrbuf_len(w))
1174             http_set_proxyaddr(wrbuf_cstr(w), ser);
1175         wrbuf_destroy(w);
1176     }
1177     return 0;
1178 }
1179
1180 /*
1181  * Local variables:
1182  * c-basic-offset: 4
1183  * c-file-style: "Stroustrup"
1184  * indent-tabs-mode: nil
1185  * End:
1186  * vim: shiftwidth=4 tabstop=8 expandtab
1187  */
1188