*** empty log message ***
[pazpar2-moved-to-github.git] / src / database.c
1 /* $Id: database.c,v 1.21 2007-04-20 15:36:48 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 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 #include <libxslt/xslt.h>
25 #include <libxslt/transform.h>
26 #include <libxslt/xsltutils.h>
27 #include <assert.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 #include "pazpar2.h"
32 #include "config.h"
33 #include "settings.h"
34 #include "http.h"
35 #include "zeerex.h"
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netdb.h>
40 #include <netinet/in.h>
41
42 static struct host *hosts = 0;  // The hosts we know about 
43 static struct database *databases = 0; // The databases we know about
44 static NMEM nmem = 0;
45
46 static xmlDoc *get_explain_xml(const char *id)
47 {
48     struct stat st;
49     char *dir;
50     char path[256];
51     char ide[256];
52     if (!config || !config->targetprofiles)
53     {
54         yaz_log(YLOG_WARN, "Config must be loaded and specify targetprofiles");
55         return 0;
56     }
57     if (config->targetprofiles->type != Targetprofiles_local)
58     {
59         yaz_log(YLOG_FATAL, "Only supports local type");
60         return 0;
61     }
62     dir = config->targetprofiles->src;
63     urlencode(id, ide);
64     sprintf(path, "%s/%s", dir, ide);
65     if (!stat(path, &st))
66         return xmlParseFile(path);
67     else
68         return 0;
69 }
70
71 // Create a new host structure for hostport
72 static struct host *create_host(const char *hostport)
73 {
74     struct addrinfo *addrinfo, hints;
75     struct host *host;
76     char *port;
77     char ipport[128];
78     unsigned char addrbuf[4];
79     int res;
80
81     host = xmalloc(sizeof(struct host));
82     host->hostport = xstrdup(hostport);
83     host->connections = 0;
84
85     if ((port = strchr(hostport, ':')))
86         *(port++) = '\0';
87     else
88         port = "210";
89
90     hints.ai_flags = 0;
91     hints.ai_family = PF_INET;
92     hints.ai_socktype = SOCK_STREAM;
93     hints.ai_protocol = IPPROTO_TCP;
94     hints.ai_addrlen = 0;
95     hints.ai_addr = 0;
96     hints.ai_canonname = 0;
97     hints.ai_next = 0;
98     // This is not robust code. It assumes that getaddrinfo always
99     // returns AF_INET address.
100     if ((res = getaddrinfo(hostport, port, &hints, &addrinfo)))
101     {
102         yaz_log(YLOG_WARN, "Failed to resolve %s: %s", hostport, gai_strerror(res));
103         xfree(host->hostport);
104         xfree(host);
105         return 0;
106     }
107     assert(addrinfo->ai_family == PF_INET);
108     memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
109     sprintf(ipport, "%u.%u.%u.%u:%s",
110             addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
111     host->ipport = xstrdup(ipport);
112     freeaddrinfo(addrinfo);
113     host->next = hosts;
114     hosts = host;
115     return host;
116 }
117
118 static struct host *find_host(const char *hostport)
119 {
120     struct host *p;
121     for (p = hosts; p; p = p->next)
122         if (!strcmp(p->hostport, hostport))
123             return p;
124     return create_host(hostport);
125 }
126
127 static struct database *load_database(const char *id)
128 {
129     xmlDoc *doc = 0;
130     struct zr_explain *explain = 0;
131     struct database *db;
132     struct host *host;
133     char hostport[256];
134     char *dbname;
135
136     yaz_log(YLOG_LOG, "New database: %s", id);
137     if (!nmem)
138         nmem = nmem_create();
139
140     if (config && config->targetprofiles 
141         && (doc = get_explain_xml(id)))
142     {
143         explain = zr_read_xml(nmem, xmlDocGetRootElement(doc));
144         if (!explain)
145             return 0;
146     }
147
148     if (strlen(id) > 255)
149         return 0;
150     strcpy(hostport, id);
151     if ((dbname = strchr(hostport, '/')))
152         *(dbname++) = '\0';
153     else
154         dbname = "Default";
155     if (!(host = find_host(hostport)))
156         return 0;
157     db = nmem_malloc(nmem, sizeof(*db));
158     memset(db, 0, sizeof(*db));
159     db->host = host;
160     db->url = nmem_strdup(nmem, id);
161     db->databases = xmalloc(2 * sizeof(char *));
162     db->databases[0] = nmem_strdup(nmem, dbname);
163     db->databases[1] = 0;
164     db->errors = 0;
165     db->explain = explain;
166     db->settings = 0;
167     db->next = databases;
168     db->map = 0;
169     databases = db;
170
171     return db;
172 }
173
174 // Return a database structure by ID. Load and add to list if necessary
175 // new==1 just means we know it's not in the list
176 struct database *find_database(const char *id, int new)
177 {
178     struct database *p;
179     if (!new)
180     {
181         for (p = databases; p; p = p->next)
182             if (!strcmp(p->url, id))
183                 return p;
184     }
185     return load_database(id);
186 }
187
188 // This whole session_grep database thing should be moved to pazpar2.c
189
190 int match_zurl(const char *zurl, const char *pattern)
191 {
192     if (!strcmp(pattern, "*"))
193         return 1;
194     else if (!strncmp(pattern, "*/", 2))
195     {
196         char *db = strchr(zurl, '/');
197         if (!db)
198             return 0;
199         if (!strcmp(pattern + 2, db))
200             return 1;
201         else
202             return 0;
203     }
204     else if (!strcmp(pattern, zurl))
205         return 1;
206     else
207         return 0;
208 }
209
210 // This will be generalized at some point
211 static int match_criterion(struct setting **settings, struct database_criterion *c)
212 {
213     int offset = settings_offset(c->name);
214     struct database_criterion_value *v;
215
216     if (offset < 0)
217     {
218         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
219         return 0;
220     }
221     if (!settings[offset])
222         return 0;
223     for (v = c->values; v; v = v->next)
224     {
225         if (offset == PZ_ID)
226         {
227             if (match_zurl(settings[offset]->value, v->value))
228                 break;
229         }
230         else 
231         {
232             if (!strcmp(settings[offset]->value, v->value))
233                 break;
234         }
235     }
236     if (v)
237         return 1;
238     else
239         return 0;
240 }
241
242 int database_match_criteria(struct setting **settings, struct database_criterion *cl)
243 {
244     for (; cl; cl = cl->next)
245         if (!match_criterion(settings, cl))
246             break;
247     if (cl) // one of the criteria failed to match -- skip this db
248         return 0;
249     else
250         return 1;
251 }
252
253 // Cycles through databases, calling a handler function on the ones for
254 // which all criteria matched.
255 int session_grep_databases(struct session *se, struct database_criterion *cl,
256         void (*fun)(void *context, struct session_database *db))
257 {
258     struct session_database *p;
259     int i = 0;
260
261     for (p = se->databases; p; p = p->next)
262     {
263         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
264             continue;
265         if (database_match_criteria(p->settings, cl))
266         {
267             (*fun)(se, p);
268             i++;
269         }
270     }
271     return i;
272 }
273
274 int grep_databases(void *context, struct database_criterion *cl,
275         void (*fun)(void *context, struct database *db))
276 {
277     struct database *p;
278     int i = 0;
279
280     for (p = databases; p; p = p->next)
281         if (database_match_criteria(p->settings, cl))
282         {
283             (*fun)(context, p);
284             i++;
285         }
286     return i;
287 }
288
289 // Prepare XSLT stylesheets for record normalization
290 static void prepare_map(void *ignore, struct database *db)
291 {
292     struct setting *s;
293
294     if (!db->settings)
295         return;
296     for (s = db->settings[PZ_XSLT]; s; s = s->next)
297     {
298         char **stylesheets;
299         struct database_retrievalmap **m = &db->map;
300         int num, i;
301
302         nmem_strsplit(nmem, ",", s->value, &stylesheets, &num);
303         for (i = 0; i < num; i++)
304         {
305             (*m) = nmem_malloc(nmem, sizeof(**m));
306             (*m)->next = 0;
307             if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
308             {
309                 yaz_log(YLOG_FATAL, "Unable to load stylesheet: %s",
310                         stylesheets[i]);
311                 exit(1);
312             }
313             m = &(*m)->next;
314         }
315     }
316     if (!db->map)
317         yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s", db->url);
318 }
319
320 // Read settings for each database, and prepare support data structures
321 void prepare_databases(void)
322 {
323     grep_databases(0, 0, prepare_map);
324 }
325
326 // This function will most likely vanish when a proper target profile mechanism is
327 // introduced.
328 void load_simpletargets(const char *fn)
329 {
330     FILE *f = fopen(fn, "r");
331     char line[256];
332
333     if (!f)
334     {
335         yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
336         exit(1);
337     }
338
339     while (fgets(line, 255, f))
340     {
341         char *url;
342         char *name;
343         struct database *db;
344
345         if (strncmp(line, "target ", 7))
346             continue;
347         line[strlen(line) - 1] = '\0';
348
349         if ((name = strchr(line, ';')))
350             *(name++) = '\0';
351
352         url = line + 7;
353
354         if (!(db = find_database(url, 0)))
355             yaz_log(YLOG_WARN, "Unable to load database %s", url);
356     }
357     fclose(f);
358 }
359
360
361 /*
362  * Local variables:
363  * c-basic-offset: 4
364  * indent-tabs-mode: nil
365  * End:
366  * vim: shiftwidth=4 tabstop=8 expandtab
367  */