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