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