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