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