Fixed potential buffer overrun in use of sprintf/strncpy. If the stylesheet
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.37 2007-06-19 10:15:44 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 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
9 version.
10
11 Pazpar2 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
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 /* $Id: config.c,v 1.37 2007-06-19 10:15:44 adam Exp $ */
23
24 #include <string.h>
25
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libxslt/xslt.h>
29 #include <libxslt/transform.h>
30 #include <libxslt/xsltutils.h>
31
32 #if HAVE_CONFIG_H
33 #include <cconfig.h>
34 #endif
35
36 #include <yaz/yaz-util.h>
37 #include <yaz/nmem.h>
38 #include <yaz/snprintf.h>
39
40 #define CONFIG_NOEXTERNS
41 #include "config.h"
42
43
44 static NMEM nmem = 0;
45 static char confdir[256] = ".";
46
47 struct conf_config *config = 0;
48
49
50 struct conf_metadata * conf_metadata_assign(NMEM nmem, 
51                                             struct conf_metadata * metadata,
52                                             const char *name,
53                                             enum conf_metadata_type type,
54                                             enum conf_metadata_merge merge,
55                                             int brief,
56                                             int termlist,
57                                             int rank,
58                                             int sortkey_offset)
59 {
60     if (!nmem || !metadata || !name)
61         return 0;
62     
63     metadata->name = nmem_strdup(nmem, name);
64
65     // enforcing that merge_range is always type_year 
66     if (merge == Metadata_merge_range)
67         metadata->type = Metadata_type_year;
68     else
69         metadata->type = type;
70
71     // enforcing that type_year is always range_merge
72     if (metadata->type == Metadata_type_year)
73         metadata->merge = Metadata_merge_range;
74     else
75         metadata->merge = merge;    
76
77     metadata->brief = brief;   
78     metadata->termlist = termlist;
79     metadata->rank = rank;    
80     metadata->sortkey_offset = sortkey_offset;
81     return metadata;
82 }
83
84
85 struct conf_sortkey * conf_sortkey_assign(NMEM nmem, 
86                                           struct conf_sortkey * sortkey,
87                                           const char *name,
88                                           enum conf_sortkey_type type)
89 {
90     if (!nmem || !sortkey || !name)
91         return 0;
92     
93     sortkey->name = nmem_strdup(nmem, name);
94     sortkey->type = type;
95
96     return sortkey;
97 }
98
99
100 struct conf_service * conf_service_create(NMEM nmem,
101                                           int num_metadata, int num_sortkeys)
102 {
103     struct conf_service * service = 0;
104
105     //assert(nmem);
106     
107     service = nmem_malloc(nmem, sizeof(struct conf_service));
108
109     service->num_metadata = num_metadata;
110     service->metadata = 0;
111     if (service->num_metadata)
112       service->metadata 
113           = nmem_malloc(nmem, 
114                         sizeof(struct conf_metadata) * service->num_metadata);
115     service->num_sortkeys = num_sortkeys;
116     service->sortkeys = 0;
117     if (service->num_sortkeys)
118         service->sortkeys 
119             = nmem_malloc(nmem, 
120                           sizeof(struct conf_sortkey) * service->num_sortkeys);
121
122     return service; 
123 }
124
125 struct conf_metadata* conf_service_add_metadata(NMEM nmem, 
126                                                 struct conf_service *service,
127                                                 int field_id,
128                                                 const char *name,
129                                                 enum conf_metadata_type type,
130                                                 enum conf_metadata_merge merge,
131                                                 int brief,
132                                                 int termlist,
133                                                 int rank,
134                                                 int sortkey_offset)
135 {
136     struct conf_metadata * md = 0;
137
138     if (!service || !service->metadata || !service->num_metadata
139         || field_id < 0  || !(field_id < service->num_metadata))
140         return 0;
141
142     //md = &((service->metadata)[field_id]);
143     md = service->metadata + field_id;
144     md = conf_metadata_assign(nmem, md, name, type, merge, 
145                              brief, termlist, rank, sortkey_offset);
146     return md;
147 }
148
149
150 struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
151                                                struct conf_service *service,
152                                                int field_id,
153                                                const char *name,
154                                                enum conf_sortkey_type type)
155 {
156     struct conf_sortkey * sk = 0;
157
158     if (!service || !service->sortkeys || !service->num_sortkeys
159         || field_id < 0  || !(field_id < service->num_sortkeys))
160         return 0;
161
162     //sk = &((service->sortkeys)[field_id]);
163     sk = service->sortkeys + field_id;
164     sk = conf_sortkey_assign(nmem, sk, name, type);
165
166     return sk;
167 }
168
169
170 int conf_service_metadata_field_id(struct conf_service *service,
171                                    const char * name)
172 {
173     int i = 0;
174
175     if (!service || !service->metadata || !service->num_metadata)
176         return -1;
177
178     for(i = 0; i < service->num_metadata; i++) {
179         if (!strcmp(name, (service->metadata[i]).name))
180             return i;
181     }
182    
183     return -1;
184 };
185
186
187 int conf_service_sortkey_field_id(struct conf_service *service,
188                                   const char * name)
189 {
190     int i = 0;
191
192     if (!service || !service->sortkeys || !service->num_sortkeys)
193         return -1;
194
195     for(i = 0; i < service->num_sortkeys; i++) {
196         if (!strcmp(name, (service->sortkeys[i]).name))
197             return i;
198     }
199    
200     return -1;
201 };
202
203
204
205 /* Code to parse configuration file */
206 /* ==================================================== */
207
208 static struct conf_service *parse_service(xmlNode *node)
209 {
210     xmlNode *n;
211     int md_node = 0;
212     int sk_node = 0;
213
214     struct conf_service *service = 0;
215     int num_metadata = 0;
216     int num_sortkeys = 0;
217     
218     // count num_metadata and num_sortkeys
219     for (n = node->children; n; n = n->next)
220         if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
221                                                    n->name, "metadata"))
222         {
223             xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
224             num_metadata++;
225             if (sortkey && strcmp((const char *) sortkey, "no"))
226                 num_sortkeys++;
227             xmlFree(sortkey);
228         }
229
230     service = conf_service_create(nmem, num_metadata, num_sortkeys);    
231
232     for (n = node->children; n; n = n->next)
233     {
234         if (n->type != XML_ELEMENT_NODE)
235             continue;
236         if (!strcmp((const char *) n->name, (const char *) "metadata"))
237         {
238             xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
239             xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
240             xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
241             xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
242             xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
243             xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
244             xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
245
246             enum conf_metadata_type type = Metadata_type_generic;
247             enum conf_metadata_merge merge = Metadata_merge_no;
248             int brief = 0;
249             int termlist = 0;
250             int rank = 0;
251             int sortkey_offset = 0;
252             enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
253             
254             // now do the parsing logic
255             if (!xml_name)
256             {
257                 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
258                 return 0;
259             }
260             if (xml_brief)
261             {
262                 if (!strcmp((const char *) xml_brief, "yes"))
263                     brief = 1;
264                  else if (strcmp((const char *) xml_brief, "no"))
265                 {
266                     yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
267                     return 0;
268                 }
269             }
270             else
271                 brief = 0;
272
273             if (xml_termlist)
274             {
275                 if (!strcmp((const char *) xml_termlist, "yes"))
276                     termlist = 1;
277                 else if (strcmp((const char *) xml_termlist, "no"))
278                 {
279                     yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
280                     return 0;
281                 }
282             }
283             else
284                 termlist = 0;
285
286             if (xml_rank)
287                 rank = atoi((const char *) xml_rank);
288             else
289                 rank = 0;
290
291             if (xml_type)
292             {
293                 if (!strcmp((const char *) xml_type, "generic"))
294                     type = Metadata_type_generic;
295                 else if (!strcmp((const char *) xml_type, "year"))
296                     type = Metadata_type_year;
297                 else
298                 {
299                     yaz_log(YLOG_FATAL, 
300                             "Unknown value for metadata/type: %s", xml_type);
301                     return 0;
302                 }
303             }
304             else
305                 type = Metadata_type_generic;
306
307             if (xml_merge)
308             {
309                 if (!strcmp((const char *) xml_merge, "no"))
310                     merge = Metadata_merge_no;
311                 else if (!strcmp((const char *) xml_merge, "unique"))
312                     merge = Metadata_merge_unique;
313                 else if (!strcmp((const char *) xml_merge, "longest"))
314                     merge = Metadata_merge_longest;
315                 else if (!strcmp((const char *) xml_merge, "range"))
316                     merge = Metadata_merge_range;
317                 else if (!strcmp((const char *) xml_merge, "all"))
318                     merge = Metadata_merge_all;
319                 else
320                 {
321                     yaz_log(YLOG_FATAL, 
322                             "Unknown value for metadata/merge: %s", xml_merge);
323                     return 0;
324                 }
325             }
326             else
327                 merge = Metadata_merge_no;
328
329             // add a sortkey if so specified
330             if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
331             {
332                 if (merge == Metadata_merge_no)
333                 {
334                     yaz_log(YLOG_FATAL, 
335                             "Can't specify sortkey on a non-merged field");
336                     return 0;
337                 }
338                 if (!strcmp((const char *) xml_sortkey, "numeric"))
339                     sk_type = Metadata_sortkey_numeric;
340                 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
341                     sk_type = Metadata_sortkey_skiparticle;
342                 else
343                 {
344                     yaz_log(YLOG_FATAL,
345                             "Unknown sortkey in metadata element: %s", 
346                             xml_sortkey);
347                     return 0;
348                 }
349                 sortkey_offset = sk_node;
350
351                 conf_service_add_sortkey(nmem, service, sk_node,
352                                          (const char *) xml_name, sk_type);
353                 
354                 sk_node++;
355             }
356             else
357                 sortkey_offset = -1;
358
359             // metadata known, assign values
360             conf_service_add_metadata(nmem, service, md_node,
361                                       (const char *) xml_name,
362                                       type, merge,
363                                       brief, termlist, rank, sortkey_offset);
364
365             xmlFree(xml_name);
366             xmlFree(xml_brief);
367             xmlFree(xml_sortkey);
368             xmlFree(xml_merge);
369             xmlFree(xml_type);
370             xmlFree(xml_termlist);
371             xmlFree(xml_rank);
372             md_node++;
373         }
374         else
375         {
376             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
377             return 0;
378         }
379     }
380     return service;
381 }
382
383 static char *parse_settings(xmlNode *node)
384 {
385     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
386     char *r;
387
388     if (src)
389         r = nmem_strdup(nmem, (const char *) src);
390     else
391     {
392         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
393         return 0;
394     }
395     xmlFree(src);
396     return r;
397 }
398
399 static struct conf_server *parse_server(xmlNode *node)
400 {
401     xmlNode *n;
402     struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
403
404     server->host = 0;
405     server->port = 0;
406     server->proxy_host = 0;
407     server->proxy_port = 0;
408     server->myurl = 0;
409     //server->zproxy_host = 0;
410     //server->zproxy_port = 0;
411     server->service = 0;
412     server->next = 0;
413     server->settings = 0;
414
415 #ifdef HAVE_ICU
416     server->icu_chn = 0;
417 #endif // HAVE_ICU
418
419
420     for (n = node->children; n; n = n->next)
421     {
422         if (n->type != XML_ELEMENT_NODE)
423             continue;
424         if (!strcmp((const char *) n->name, "listen"))
425         {
426             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
427             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
428             if (port)
429                 server->port = atoi((const char *) port);
430             if (host)
431                 server->host = nmem_strdup(nmem, (const char *) host);
432             xmlFree(port);
433             xmlFree(host);
434         }
435         else if (!strcmp((const char *) n->name, "proxy"))
436         {
437             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
438             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
439             xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
440             if (port)
441                 server->proxy_port = atoi((const char *) port);
442             if (host)
443                 server->proxy_host = nmem_strdup(nmem, (const char *) host);
444             if (myurl)
445                 server->myurl = nmem_strdup(nmem, (const char *) myurl);
446 #ifdef GAGA
447             else
448             {
449                 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
450                 return 0;
451             }
452 #endif
453             xmlFree(port);
454             xmlFree(host);
455             xmlFree(myurl);
456         }
457         else if (!strcmp((const char *) n->name, "settings"))
458         {
459             if (server->settings)
460             {
461                 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
462                 return 0;
463             }
464             if (!(server->settings = parse_settings(n)))
465                 return 0;
466         }
467         else if (!strcmp((const char *) n->name, "icu_chain"))
468         {
469 #ifdef HAVE_ICU
470             UErrorCode status = U_ZERO_ERROR;
471             struct icu_chain *chain = icu_chain_xml_config(n, &status);
472             if (!chain || U_FAILURE(status)){
473                 //xmlDocPtr icu_doc = 0;
474                 //xmlChar *xmlstr = 0;
475                 //int size = 0;
476                 //xmlDocDumpMemory(icu_doc, size);
477                 
478                 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
479                         "<%s>\n ... \n</%s>",
480                         n->name, n->name);
481                 return 0;
482             }
483             server->icu_chn = chain;
484 #else // HAVE_ICU
485             yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
486                     "<%s>\n ... \n</%s>",
487                     n->name, n->name);
488             yaz_log(YLOG_FATAL, 
489                     "But no ICU support compiled into pazpar2 server.");
490             yaz_log(YLOG_FATAL, 
491                     "Please install libicu36-dev and icu-doc or similar, "
492                     "re-configure and re-compile");            
493             return 0;
494 #endif // HAVE_ICU
495         }
496         else if (!strcmp((const char *) n->name, "service"))
497         {
498             struct conf_service *s = parse_service(n);
499             if (!s)
500                 return 0;
501             server->service = s;
502         }
503         else
504         {
505             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
506             return 0;
507         }
508     }
509     return server;
510 }
511
512 xsltStylesheet *conf_load_stylesheet(const char *fname)
513 {
514     char path[256];
515     if (*fname == '/')
516         yaz_snprintf(path, sizeof(path), fname);
517     else
518         yaz_snprintf(path, sizeof(path), "%s/%s", confdir, fname);
519     return xsltParseStylesheetFile((xmlChar *) path);
520 }
521
522 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
523 {
524     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
525     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
526     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
527
528     memset(r, 0, sizeof(*r));
529
530     if (type)
531     {
532         if (!strcmp((const char *) type, "local"))
533             r->type = Targetprofiles_local;
534         else
535         {
536             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
537             return 0;
538         }
539     }
540     else
541     {
542         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
543         return 0;
544     }
545
546     if (src)
547         r->src = nmem_strdup(nmem, (const char *) src);
548     else
549     {
550         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
551         return 0;
552     }
553     xmlFree(type);
554     xmlFree(src);
555     return r;
556 }
557
558 static struct conf_config *parse_config(xmlNode *root)
559 {
560     xmlNode *n;
561     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
562
563     r->servers = 0;
564     r->targetprofiles = 0;
565
566     for (n = root->children; n; n = n->next)
567     {
568         if (n->type != XML_ELEMENT_NODE)
569             continue;
570         if (!strcmp((const char *) n->name, "server"))
571         {
572             struct conf_server *tmp = parse_server(n);
573             if (!tmp)
574                 return 0;
575             tmp->next = r->servers;
576             r->servers = tmp;
577         }
578         else if (!strcmp((const char *) n->name, "targetprofiles"))
579         {
580             // It would be fun to be able to fix this sometime
581             if (r->targetprofiles)
582             {
583                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
584                 return 0;
585             }
586             if (!(r->targetprofiles = parse_targetprofiles(n)))
587                 return 0;
588         }
589         else
590         {
591             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
592             return 0;
593         }
594     }
595     return r;
596 }
597
598 int read_config(const char *fname)
599 {
600     xmlDoc *doc = xmlParseFile(fname);
601     const char *p;
602
603     if (!nmem)  // Initialize
604     {
605         nmem = nmem_create();
606         xmlSubstituteEntitiesDefault(1);
607         xmlLoadExtDtdDefaultValue = 1;
608     }
609     if (!doc)
610     {
611         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
612         exit(1);
613     }
614     if ((p = strrchr(fname, '/')))
615     {
616         int len = p - fname;
617         if (len >= sizeof(confdir))
618             len = sizeof(confdir)-1;
619         strncpy(confdir, fname, len);
620         confdir[len] = '\0';
621     }
622     config = parse_config(xmlDocGetRootElement(doc));
623     xmlFreeDoc(doc);
624
625     if (config)
626         return 1;
627     else
628         return 0;
629 }
630
631
632 /*
633  * Local variables:
634  * c-basic-offset: 4
635  * indent-tabs-mode: nil
636  * End:
637  * vim: shiftwidth=4 tabstop=8 expandtab
638  */