POST of target settings.
[pazpar2-moved-to-github.git] / src / settings.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 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 <assert.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include "direntz.h"
35 #include <stdlib.h>
36 #include <sys/stat.h>
37
38 #include <libxml/parser.h>
39 #include <libxml/tree.h>
40
41 #include <yaz/nmem.h>
42 #include <yaz/log.h>
43
44 #include "pazpar2.h"
45 #include "database.h"
46 #include "settings.h"
47
48 // Used for initializing setting_dictionary with pazpar2-specific settings
49 static char *hard_settings[] = {
50     "pz:piggyback",
51     "pz:elements",
52     "pz:requestsyntax",
53     "pz:cclmap:",
54     "pz:xslt",
55     "pz:nativesyntax",
56     "pz:authentication",
57     "pz:allow",
58     "pz:maxrecs",
59     "pz:id",
60     "pz:name",
61     "pz:queryencoding",
62     "pz:ip",
63     "pz:zproxy",
64     "pz:apdulog",
65     "pz:sru",
66     "pz:sru_version",
67     "pz:pqf_prefix",
68     "pz:sort",
69     0
70 };
71
72 struct setting_dictionary
73 {
74     char **dict;
75     int size;
76     int num;
77 };
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(struct conf_service *service)
86 {
87     return service->dictionary->num;
88 }
89
90 int settings_offset(struct conf_service *service, const char *name)
91 {
92     int i;
93
94     if (!name)
95         name = "";
96     for (i = 0; i < service->dictionary->num; i++)
97         if (!strcmp(name, service->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(struct conf_service *service, 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 < service->dictionary->num; i++)
113         if (!strncmp(name, service->dictionary->dict[i], maxlen))
114             return i;
115     return -1;
116 }
117
118 char *settings_name(struct conf_service *service, int offset)
119 {
120     return service->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, "stat %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 void settings_read_node_x(xmlNode *n,
137                           void *client_data,
138                           void (*fun)(void *client_data,
139                                       struct setting *set))
140 {
141     xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
142
143     namea = xmlGetProp(n, (xmlChar *) "name");
144     targeta = xmlGetProp(n, (xmlChar *) "target");
145     valuea = xmlGetProp(n, (xmlChar *) "value");
146     usera = xmlGetProp(n, (xmlChar *) "user");
147     precedencea = xmlGetProp(n, (xmlChar *) "precedence");
148     for (n = n->children; n; n = n->next)
149     {
150         if (n->type != XML_ELEMENT_NODE)
151             continue;
152         if (!strcmp((const char *) n->name, "set"))
153         {
154             char *name, *target, *value, *user, *precedence;
155
156             name = (char *) xmlGetProp(n, (xmlChar *) "name");
157             target = (char *) xmlGetProp(n, (xmlChar *) "target");
158             value = (char *) xmlGetProp(n, (xmlChar *) "value");
159             user = (char *) xmlGetProp(n, (xmlChar *) "user");
160             precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
161
162             if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
163             {
164                 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
165                 exit(1);
166             }
167             else
168             {
169                 struct setting set;
170                 char nameb[1024];
171                 char targetb[1024];
172                 char valueb[1024];
173
174                 // Copy everything into a temporary buffer -- we decide
175                 // later if we are keeping it.
176                 if (precedence)
177                     set.precedence = atoi((char *) precedence);
178                 else if (precedencea)
179                     set.precedence = atoi((char *) precedencea);
180                 else
181                     set.precedence = 0;
182                 if (target)
183                     strcpy(targetb, target);
184                 else
185                     strcpy(targetb, (const char *) targeta);
186                 set.target = targetb;
187                 if (name)
188                     strcpy(nameb, name);
189                 else
190                     strcpy(nameb, (const char *) namea);
191                 set.name = nameb;
192                 if (value)
193                     strcpy(valueb, value);
194                 else
195                     strcpy(valueb, (const char *) valuea);
196                 set.value = valueb;
197                 set.next = 0;
198                 (*fun)(client_data, &set);
199             }
200             xmlFree(name);
201             xmlFree(precedence);
202             xmlFree(value);
203             xmlFree(user);
204             xmlFree(target);
205         }
206         else
207         {
208             yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
209             exit(1);
210         }
211     }
212     xmlFree(namea);
213     xmlFree(precedencea);
214     xmlFree(valuea);
215     xmlFree(usera);
216     xmlFree(targeta);
217 }
218  
219 static void read_settings_file(const char *path,
220                                void *client_data,
221                                void (*fun)(void *client_data,
222                                            struct setting *set))
223 {
224     xmlDoc *doc = xmlParseFile(path);
225     xmlNode *n;
226
227     if (!doc)
228     {
229         yaz_log(YLOG_FATAL, "Failed to parse %s", path);
230         exit(1);
231     }
232     n = xmlDocGetRootElement(doc);
233     settings_read_node_x(n, client_data, fun);
234
235     xmlFreeDoc(doc);
236 }
237
238
239 // Recursively read files or directories, invoking a 
240 // callback for each one
241 static void read_settings(const char *path,
242                           void *client_data,
243                           void (*fun)(void *client_data,
244                                       struct setting *set))
245 {
246     DIR *d;
247     struct dirent *de;
248     char *dot;
249
250     if (isdir(path))
251     {
252         if (!(d = opendir(path)))
253         {
254             yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
255             exit(1);
256         }
257         while ((de = readdir(d)))
258         {
259             char tmp[1024];
260             if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
261                 continue;
262             sprintf(tmp, "%s/%s", path, de->d_name);
263             read_settings(tmp, client_data, fun);
264         }
265         closedir(d);
266     }
267     else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
268         read_settings_file(path, client_data, fun);
269 }
270
271 // Determines if a ZURL is a wildcard, and what kind
272 static int zurl_wildcard(const char *zurl)
273 {
274     if (!zurl)
275         return SETTING_WILDCARD_NO;
276     if (*zurl == '*')
277         return SETTING_WILDCARD_YES;
278     else if (*(zurl + strlen(zurl) - 1) == '*')
279         return SETTING_WILDCARD_DB;
280     else
281         return SETTING_WILDCARD_NO;
282 }
283
284 // Callback. Adds a new entry to the dictionary if necessary
285 // This is used in pass 1 to determine layout of dictionary
286 // and to load any databases mentioned
287 static void prepare_dictionary(struct conf_service *service,
288                                struct setting *set)
289 {
290     struct setting_dictionary *dictionary = service->dictionary;
291
292     int i;
293     char *p;
294
295     // Determine if we already have a dictionary entry
296     if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
297         *(p + 1) = '\0';
298     for (i = 0; i < dictionary->num; i++)
299         if (!strcmp(dictionary->dict[i], set->name))
300             return;
301
302     if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
303         {
304             yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
305             exit(1);
306         }
307
308     // Create a new dictionary entry
309     // Grow dictionary if necessary
310     if (!dictionary->size)
311         dictionary->dict =
312             nmem_malloc(service->nmem, (dictionary->size = 50) * sizeof(char*));
313     else if (dictionary->num + 1 > dictionary->size)
314     {
315         char **tmp =
316             nmem_malloc(service->nmem, dictionary->size * 2 * sizeof(char*));
317         memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
318         dictionary->dict = tmp;
319         dictionary->size *= 2;
320     }
321     dictionary->dict[dictionary->num++] = nmem_strdup(service->nmem, set->name);
322 }
323
324
325 struct update_database_context {
326     struct setting *set;
327     struct conf_service *service;
328 };
329
330 // This is called from grep_databases -- adds/overrides setting for a target
331 // This is also where the rules for precedence of settings are implemented
332 static void update_database(void *context, struct database *db)
333 {
334     struct setting *set = ((struct update_database_context *)
335                            context)->set;
336     struct conf_service *service = ((struct update_database_context *) 
337                                     context)->service;
338     struct setting *s, **sp;
339     int offset;
340
341     // Is this the right database?
342     if (!match_zurl(db->url, set->target))
343         return;
344
345     if ((offset = settings_offset_cprefix(service, set->name)) < 0)
346         return ;
347
348     // First we determine if this setting is overriding  any existing settings
349     // with the same name.
350     for (s = db->settings[offset], sp = &db->settings[offset]; s;
351             sp = &s->next, s = s->next)
352         if (!strcmp(s->name, set->name))
353         {
354             if (s->precedence < set->precedence)
355                 // We discard the value (nmem keeps track of the space)
356                 *sp = (*sp)->next; // unlink value from existing setting
357             else if (s->precedence > set->precedence)
358                 // Db contains a higher-priority setting. Abort search
359                 break;
360             if (zurl_wildcard(s->target) > zurl_wildcard(set->target))
361                 // target-specific value trumps wildcard. Delete.
362                 *sp = (*sp)->next; // unlink.....
363             else if (!zurl_wildcard(s->target))
364                 // Db already contains higher-priority setting. Abort search
365                 break;
366         }
367     if (!s) // s will be null when there are no higher-priority settings -- we add one
368     {
369         struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
370
371         memset(new, 0, sizeof(*new));
372         new->precedence = set->precedence;
373         new->target = nmem_strdup(service->nmem, set->target);
374         new->name = nmem_strdup(service->nmem, set->name);
375         new->value = nmem_strdup(service->nmem, set->value);
376         new->next = db->settings[offset];
377         db->settings[offset] = new;
378     }
379 }
380
381 // Callback -- updates database records with dictionary entries as appropriate
382 // This is used in pass 2 to assign name/value pairs to databases
383 static void update_databases(void *client_data, struct setting *set)
384 {
385     struct conf_service *service = (struct conf_service *) client_data;
386     struct update_database_context context;
387     context.set = set;
388     context.service = service;
389     predef_grep_databases(&context, service, 0, update_database);
390 }
391
392 // This simply copies the 'hard' (application-specific) settings
393 // to the settings dictionary.
394 static void initialize_hard_settings(struct conf_service *service)
395 {
396     struct setting_dictionary *dict = service->dictionary;
397     dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
398     dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
399     memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
400     dict->num = dict->size;
401 }
402
403 // Read any settings names introduced in service definition (config) and add to dictionary
404 // This is done now to avoid errors if user settings are declared in session overrides
405 static void initialize_soft_settings(struct conf_service *service)
406 {
407     int i;
408
409     for (i = 0; i < service->num_metadata; i++)
410     {
411         struct setting set;
412         struct conf_metadata *md = &service->metadata[i];
413
414         if (md->setting == Metadata_setting_no)
415             continue;
416
417         set.precedence = 0;
418         set.target = "";
419         set.name = md->name;
420         set.value = "";
421         set.next = 0;
422         prepare_dictionary(service, &set);
423     }
424 }
425
426 static void prepare_target_dictionary(void *client_data, struct setting *set)
427 {
428     struct conf_service *service = (struct conf_service *) client_data;
429     struct setting_dictionary *dictionary = service->dictionary;
430
431     int i;
432     char *p;
433
434     // If target address is not wildcard, add the database
435     if (*set->target && !zurl_wildcard(set->target))
436         find_database(set->target, 0, service);
437
438     // Determine if we already have a dictionary entry
439     if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
440         *(p + 1) = '\0';
441     for (i = 0; i < dictionary->num; i++)
442         if (!strcmp(dictionary->dict[i], set->name))
443             return;
444     yaz_log(YLOG_WARN, "Setting '%s' not configured as metadata", set->name);
445 }
446
447 void init_settings(struct conf_service *service)
448 {
449     struct setting_dictionary *new;
450     
451     assert(service->nmem);
452     
453     new = nmem_malloc(service->nmem, sizeof(*new));
454     memset(new, 0, sizeof(*new));
455     service->dictionary = new;
456     initialize_hard_settings(service);
457     initialize_soft_settings(service);
458 }
459
460 void settings_read_file(struct conf_service *service, const char *path,
461                         int pass)
462 {
463     if (pass == 1)
464         read_settings(path, service, prepare_target_dictionary);
465     else
466         read_settings(path, service, update_databases);
467 }
468
469 void settings_read_node(struct conf_service *service, xmlNode *n,
470                         int pass)
471 {
472     if (pass == 1)
473         settings_read_node_x(n, service, prepare_target_dictionary);
474     else
475         settings_read_node_x(n, service, update_databases);
476 }
477
478 /*
479  * Local variables:
480  * c-basic-offset: 4
481  * c-file-style: "Stroustrup"
482  * indent-tabs-mode: nil
483  * End:
484  * vim: shiftwidth=4 tabstop=8 expandtab
485  */
486