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