Added setting sru_version which allows SRU version to be set
[pazpar2-moved-to-github.git] / src / settings.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 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 // This module implements a generic system of settings
21 // (attribute-value) that can be associated with search targets. The
22 // system supports both default values, per-target overrides, and
23 // per-user settings.
24
25 #if HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29
30 #include <string.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include "direntz.h"
34 #include <stdlib.h>
35 #include <sys/stat.h>
36
37 #include <libxml/parser.h>
38 #include <libxml/tree.h>
39
40 #include <yaz/nmem.h>
41 #include <yaz/log.h>
42
43 #include "pazpar2.h"
44 #include "database.h"
45 #include "settings.h"
46
47 static NMEM nmem = 0;
48
49 // Used for initializing setting_dictionary with pazpar2-specific settings
50 static char *hard_settings[] = {
51     "pz:piggyback",
52     "pz:elements",
53     "pz:requestsyntax",
54     "pz:cclmap:",
55     "pz:xslt",
56     "pz:nativesyntax",
57     "pz:authentication",
58     "pz:allow",
59     "pz:maxrecs",
60     "pz:id",
61     "pz:name",
62     "pz:queryencoding",
63     "pz:ip",
64     "pz:zproxy",
65     "pz:apdulog",
66     "pz:sru",
67     "pz:sru_version",
68     0
69 };
70
71 struct setting_dictionary
72 {
73     char **dict;
74     int size;
75     int num;
76 };
77
78 static struct setting_dictionary *dictionary = 0;
79
80 // This establishes the precedence of wildcard expressions
81 #define SETTING_WILDCARD_NO     0 // No wildcard
82 #define SETTING_WILDCARD_DB     1 // Database wildcard 'host:port/*'
83 #define SETTING_WILDCARD_YES    2 // Complete wildcard '*'
84
85 // Returns size of settings directory
86 int settings_num(void)
87 {
88     return dictionary->num;
89 }
90
91 int settings_offset(const char *name)
92 {
93     int i;
94
95     if (!name)
96         name = "";
97     for (i = 0; i < dictionary->num; i++)
98         if (!strcmp(name, dictionary->dict[i]))
99             return i;
100     return -1;
101 }
102
103 // Ignores everything after second colon, if present
104 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
105 int settings_offset_cprefix(const char *name)
106 {
107     const char *p;
108     int maxlen = 100;
109     int i;
110
111     if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
112         maxlen = (p - name) + 1;
113     for (i = 0; i < dictionary->num; i++)
114         if (!strncmp(name, dictionary->dict[i], maxlen))
115             return i;
116     return -1;
117 }
118
119 char *settings_name(int offset)
120 {
121     return dictionary->dict[offset];
122 }
123
124 static int isdir(const char *path)
125 {
126     struct stat st;
127
128     if (stat(path, &st) < 0)
129     {
130         yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
131         exit(1);
132     }
133     return st.st_mode & S_IFDIR;
134 }
135
136 // Read settings from an XML file, calling handler function for each setting
137 static void read_settings_file(const char *path,
138         void (*fun)(struct setting *set))
139 {
140     xmlDoc *doc = xmlParseFile(path);
141     xmlNode *n;
142     xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
143
144     if (!doc)
145     {
146         yaz_log(YLOG_FATAL, "Failed to parse %s", path);
147         exit(1);
148     }
149     n = xmlDocGetRootElement(doc);
150     namea = xmlGetProp(n, (xmlChar *) "name");
151     targeta = xmlGetProp(n, (xmlChar *) "target");
152     valuea = xmlGetProp(n, (xmlChar *) "value");
153     usera = xmlGetProp(n, (xmlChar *) "user");
154     precedencea = xmlGetProp(n, (xmlChar *) "precedence");
155     for (n = n->children; n; n = n->next)
156     {
157         if (n->type != XML_ELEMENT_NODE)
158             continue;
159         if (!strcmp((const char *) n->name, "set"))
160         {
161             char *name, *target, *value, *user, *precedence;
162
163             name = (char *) xmlGetProp(n, (xmlChar *) "name");
164             target = (char *) xmlGetProp(n, (xmlChar *) "target");
165             value = (char *) xmlGetProp(n, (xmlChar *) "value");
166             user = (char *) xmlGetProp(n, (xmlChar *) "user");
167             precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
168
169             if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
170             {
171                 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
172                 exit(1);
173             }
174             else
175             {
176                 struct setting set;
177                 char nameb[1024];
178                 char targetb[1024];
179                 char valueb[1024];
180
181                 // Copy everything into a temporary buffer -- we decide
182                 // later if we are keeping it.
183                 if (precedence)
184                     set.precedence = atoi((char *) precedence);
185                 else if (precedencea)
186                     set.precedence = atoi((char *) precedencea);
187                 else
188                     set.precedence = 0;
189                 if (target)
190                     strcpy(targetb, target);
191                 else
192                     strcpy(targetb, (const char *) targeta);
193                 set.target = targetb;
194                 if (name)
195                     strcpy(nameb, name);
196                 else
197                     strcpy(nameb, (const char *) namea);
198                 set.name = nameb;
199                 if (value)
200                     strcpy(valueb, value);
201                 else
202                     strcpy(valueb, (const char *) valuea);
203                 set.value = valueb;
204                 set.next = 0;
205                 (*fun)(&set);
206             }
207             xmlFree(name);
208             xmlFree(precedence);
209             xmlFree(value);
210             xmlFree(user);
211             xmlFree(target);
212         }
213         else
214         {
215             yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
216             exit(1);
217         }
218     }
219     xmlFree(namea);
220     xmlFree(precedencea);
221     xmlFree(valuea);
222     xmlFree(usera);
223     xmlFree(targeta);
224
225     xmlFreeDoc(doc);
226 }
227  
228 // Recursively read files or directories, invoking a 
229 // callback for each one
230 static void read_settings(const char *path,
231                 void (*fun)(struct setting *set))
232 {
233     DIR *d;
234     struct dirent *de;
235     char *dot;
236
237     if (isdir(path))
238     {
239         if (!(d = opendir(path)))
240         {
241             yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
242             exit(1);
243         }
244         while ((de = readdir(d)))
245         {
246             char tmp[1024];
247             if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
248                 continue;
249             sprintf(tmp, "%s/%s", path, de->d_name);
250             read_settings(tmp, fun);
251         }
252         closedir(d);
253     }
254     else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
255         read_settings_file(path, fun);
256 }
257
258 // Determines if a ZURL is a wildcard, and what kind
259 static int zurl_wildcard(const char *zurl)
260 {
261     if (!zurl)
262         return SETTING_WILDCARD_NO;
263     if (*zurl == '*')
264         return SETTING_WILDCARD_YES;
265     else if (*(zurl + strlen(zurl) - 1) == '*')
266         return SETTING_WILDCARD_DB;
267     else
268         return SETTING_WILDCARD_NO;
269 }
270
271 // Callback. Adds a new entry to the dictionary if necessary
272 // This is used in pass 1 to determine layout of dictionary
273 // and to load any databases mentioned
274 static void prepare_dictionary(struct setting *set)
275 {
276     int i;
277     char *p;
278
279     // If target address is not wildcard, add the database
280     if (*set->target && !zurl_wildcard(set->target))
281         find_database(set->target, 0);
282
283     // Determine if we already have a dictionary entry
284     if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
285         *(p + 1) = '\0';
286     for (i = 0; i < dictionary->num; i++)
287         if (!strcmp(dictionary->dict[i], set->name))
288             return;
289
290     if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
291         {
292             yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
293             exit(1);
294         }
295
296     // Create a new dictionary entry
297     // Grow dictionary if necessary
298     if (!dictionary->size)
299         dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
300     else if (dictionary->num + 1 > dictionary->size)
301     {
302         char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
303         memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
304         dictionary->dict = tmp;
305         dictionary->size *= 2;
306     }
307     dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
308 }
309
310 // This is called from grep_databases -- adds/overrides setting for a target
311 // This is also where the rules for precedence of settings are implemented
312 static void update_database(void *context, struct database *db)
313 {
314     struct setting *set = (struct setting *) context;
315     struct setting *s, **sp;
316     int offset;
317
318     // Is this the right database?
319     if (!match_zurl(db->url, set->target))
320         return;
321
322     if ((offset = settings_offset_cprefix(set->name)) < 0)
323         abort(); // Should never get here
324
325     // First we determine if this setting is overriding  any existing settings
326     // with the same name.
327     for (s = db->settings[offset], sp = &db->settings[offset]; s;
328             sp = &s->next, s = s->next)
329         if (!strcmp(s->name, set->name))
330         {
331             if (s->precedence < set->precedence)
332                 // We discard the value (nmem keeps track of the space)
333                 *sp = (*sp)->next; // unlink value from existing setting
334             else if (s->precedence > set->precedence)
335                 // Db contains a higher-priority setting. Abort search
336                 break;
337             if (zurl_wildcard(s->target) > zurl_wildcard(set->target))
338                 // target-specific value trumps wildcard. Delete.
339                 *sp = (*sp)->next; // unlink.....
340             else if (!zurl_wildcard(s->target))
341                 // Db already contains higher-priority setting. Abort search
342                 break;
343         }
344     if (!s) // s will be null when there are no higher-priority settings -- we add one
345     {
346         struct setting *new = nmem_malloc(nmem, sizeof(*new));
347
348         memset(new, 0, sizeof(*new));
349         new->precedence = set->precedence;
350         new->target = nmem_strdup(nmem, set->target);
351         new->name = nmem_strdup(nmem, set->name);
352         new->value = nmem_strdup(nmem, set->value);
353         new->next = db->settings[offset];
354         db->settings[offset] = new;
355     }
356 }
357
358 // Callback -- updates database records with dictionary entries as appropriate
359 // This is used in pass 2 to assign name/value pairs to databases
360 static void update_databases(struct setting *set)
361 {
362     predef_grep_databases(set, 0, update_database);
363 }
364
365 // This simply copies the 'hard' (application-specific) settings
366 // to the settings dictionary.
367 static void initialize_hard_settings(struct setting_dictionary *dict)
368 {
369     dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
370     dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
371     memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
372     dict->num = dict->size;
373 }
374
375 // Read any settings names introduced in service definition (config) and add to dictionary
376 // This is done now to avoid errors if user settings are declared in session overrides
377 static void initialize_soft_settings()
378 {
379     struct conf_service *service = config->servers->service;
380     int i;
381
382     for (i = 0; i < service->num_metadata; i++)
383     {
384         struct setting set;
385         struct conf_metadata *md = &service->metadata[i];
386
387         if (md->setting == Metadata_setting_no)
388             continue;
389
390         set.precedence = 0;
391         set.target = "";
392         set.name = md->name;
393         set.value = "";
394         set.next = 0;
395         prepare_dictionary(&set);
396     }
397 }
398
399 // If we ever decide we need to be able to specify multiple settings directories,
400 // the two calls to read_settings must be split -- so the dictionary is prepared
401 // for the contents of every directory before the databases are updated.
402 void settings_read(const char *path)
403 {
404     read_settings(path, prepare_dictionary);
405     read_settings(path, update_databases);
406 }
407
408 void init_settings(void)
409 {
410     struct setting_dictionary *new;
411     if (!nmem)
412         nmem = nmem_create();
413     else
414         nmem_reset(nmem);
415     new = nmem_malloc(nmem, sizeof(*new));
416     memset(new, 0, sizeof(*new));
417     initialize_hard_settings(new);
418     dictionary = new;
419     initialize_soft_settings();
420 }
421
422 /*
423  * Local variables:
424  * c-basic-offset: 4
425  * indent-tabs-mode: nil
426  * End:
427  * vim: shiftwidth=4 tabstop=8 expandtab
428  */