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