Protect struct host
[pazpar2-moved-to-github.git] / src / database.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 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 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <assert.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <yaz/log.h>
30
31 #include "pazpar2.h"
32 #include "host.h"
33 #include "settings.h"
34 #include "http.h"
35 #include "zeerex.h"
36 #include "database.h"
37
38 #include <sys/types.h>
39 #if HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #if HAVE_NETDB_H
43 #include <netdb.h>
44 #endif
45 #if HAVE_NETINET_IN_H
46 #include <netinet/in.h>
47 #endif
48
49 enum pazpar2_database_criterion_type {
50     PAZPAR2_STRING_MATCH,
51     PAZPAR2_SUBSTRING_MATCH
52 };
53
54 struct database_criterion_value {
55     char *value;
56     struct database_criterion_value *next;
57 };
58
59 struct database_criterion {
60     char *name;
61     enum pazpar2_database_criterion_type type;
62     struct database_criterion_value *values;
63     struct database_criterion *next;
64 };
65
66 static struct host *hosts = 0;  /* thread pr */
67
68 static xmlDoc *get_explain_xml(struct conf_targetprofiles *targetprofiles,
69                                const char *id)
70 {
71     struct stat st;
72     char *dir;
73     char path[256];
74     char ide[256];
75     if (targetprofiles->type != Targetprofiles_local)
76     {
77         yaz_log(YLOG_FATAL, "Only supports local type");
78         return 0;
79     }
80     dir = targetprofiles->src;
81     urlencode(id, ide);
82     sprintf(path, "%s/%s", dir, ide);
83     if (!stat(path, &st))
84         return xmlParseFile(path);
85     else
86         return 0;
87 }
88
89 // Create a new host structure for hostport
90 static struct host *create_host(const char *hostport, iochan_man_t iochan_man)
91 {
92     struct host *host;
93
94     host = xmalloc(sizeof(struct host));
95     host->hostport = xstrdup(hostport);
96     host->connections = 0;
97     host->ipport = 0;
98     host->mutex = 0;
99
100     if (host_getaddrinfo(host, iochan_man))
101     {
102         xfree(host->hostport);
103         xfree(host);
104         return 0;
105     }
106     yaz_mutex_create(&host->mutex);
107
108     host->next = hosts;
109     hosts = host;
110     return host;
111 }
112
113 static struct host *find_host(const char *hostport, iochan_man_t iochan_man)
114 {
115     struct host *p;
116     for (p = hosts; p; p = p->next)
117         if (!strcmp(p->hostport, hostport))
118             return p;
119     return create_host(hostport, iochan_man);
120 }
121
122 int resolve_database(struct conf_service *service, struct database *db)
123 {
124     if (db->host == 0)
125     {
126         struct host *host;
127         char *p;
128         char hostport[256];
129         strcpy(hostport, db->url);
130         if ((p = strchr(hostport, '/')))
131             *p = '\0';
132         if (!(host = find_host(hostport, service->server->iochan_man)))
133             return -1;
134         db->host = host;
135     }
136     return 0;
137 }
138
139 void resolve_databases(struct conf_service *service)
140 {
141     struct database *db = service->databases;
142     for (; db; db = db->next)
143         resolve_database(service, db);
144 }
145
146 struct database *new_database(const char *id, NMEM nmem)
147 {
148     struct database *db;
149     char hostport[256];
150     char *dbname;
151     struct setting *idset;
152
153     if (strlen(id) > 255)
154         return 0;
155     strcpy(hostport, id);
156     if ((dbname = strchr(hostport, '/')))
157         *(dbname++) = '\0';
158     else
159         dbname = "";
160     db = nmem_malloc(nmem, sizeof(*db));
161     memset(db, 0, sizeof(*db));
162     db->host = 0;
163     db->url = nmem_strdup(nmem, id);
164     db->databases = nmem_malloc(nmem, 2 * sizeof(char *));
165     db->databases[0] = nmem_strdup(nmem, dbname);
166     db->databases[1] = 0;
167     db->errors = 0;
168     db->explain = 0;
169
170     db->num_settings = PZ_NEGOTIATION_CHARSET+1;
171     db->settings = nmem_malloc(nmem, sizeof(struct settings*) * 
172                                db->num_settings);
173     memset(db->settings, 0, sizeof(struct settings*) * db->num_settings);
174     idset = nmem_malloc(nmem, sizeof(*idset));
175     idset->precedence = 0;
176     idset->name = "pz:id";
177     idset->target = idset->value = db->url;
178     idset->next = 0;
179     db->settings[PZ_ID] = idset;
180     db->next = 0;
181
182     return db;
183 }
184
185 static struct database *load_database(const char *id,
186                                       struct conf_service *service)
187 {
188     struct database *db;
189     struct zr_explain *explain = 0;
190     xmlDoc *doc = 0;
191
192     if (service->targetprofiles 
193         && (doc = get_explain_xml(service->targetprofiles, id)))
194     {
195         explain = zr_read_xml(service->nmem, xmlDocGetRootElement(doc));
196         if (!explain)
197             return 0;
198     }
199     db = new_database(id, service->nmem);
200     db->explain = explain;
201     db->next = service->databases;
202     service->databases = db;
203
204     return db;
205 }
206
207 // Return a database structure by ID. Load and add to list if necessary
208 // new==1 just means we know it's not in the list
209 struct database *find_database(const char *id, struct conf_service *service)
210 {
211     struct database *p;
212     for (p = service->databases; p; p = p->next)
213         if (!strcmp(p->url, id))
214             return p;
215     return load_database(id, service);
216 }
217
218 // This whole session_grep database thing should be moved elsewhere
219
220 int match_zurl(const char *zurl, const char *pattern)
221 {
222     int len;
223
224     if (!strcmp(pattern, "*"))
225         return 1;
226     else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
227     {
228         char *db = strchr(zurl, '/');
229         if (!db)
230             return 0;
231         if (!strcmp(pattern + 2, db))
232             return 1;
233         else
234             return 0;
235     }
236     else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
237     {
238         if (!strncmp(pattern, zurl, len))
239             return 1;
240         else
241             return 2;
242     }
243     else if (!strcmp(pattern, zurl))
244         return 1;
245     else
246         return 0;
247 }
248
249 // This will be generalized at some point
250 static int match_criterion(struct setting **settings,
251                            struct conf_service *service, 
252                            struct database_criterion *c)
253 {
254     int offset = settings_lookup_offset(service, c->name);
255     struct database_criterion_value *v;
256
257     if (offset < 0)
258     {
259         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
260         return 0;
261     }
262     if (!settings[offset])
263         return 0;
264     for (v = c->values; v; v = v->next)
265     {
266         if (c->type == PAZPAR2_STRING_MATCH)
267         {
268             if (offset == PZ_ID)
269             {
270                 if (match_zurl(settings[offset]->value, v->value))
271                     break;
272             }
273             else 
274             {
275                 if (!strcmp(settings[offset]->value, v->value))
276                     break;
277             }
278         }            
279         else if (c->type == PAZPAR2_SUBSTRING_MATCH)
280         {
281             if (strstr(settings[offset]->value, v->value))
282                 break;
283         }
284     }
285     if (v)
286         return 1;
287     else
288         return 0;
289 }
290
291 // parses crit1=val1,crit2=val2|val3,...
292 static struct database_criterion *create_database_criterion(NMEM m,
293                                                             const char *buf)
294 {
295     struct database_criterion *res = 0;
296     char **values;
297     int num;
298     int i;
299
300     if (!buf || !*buf)
301         return 0;
302     nmem_strsplit(m, ",", buf,  &values, &num);
303     for (i = 0; i < num; i++)
304     {
305         char **subvalues;
306         int subnum;
307         int subi;
308         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
309         char *eq;
310         if ((eq = strchr(values[i], '=')))
311             new->type = PAZPAR2_STRING_MATCH;
312         else if ((eq = strchr(values[i], '~')))
313             new->type = PAZPAR2_SUBSTRING_MATCH;
314         else
315         {
316             yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
317             return 0;
318         }
319         *(eq++) = '\0';
320         new->name = values[i];
321         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
322         new->values = 0;
323         for (subi = 0; subi < subnum; subi++)
324         {
325             struct database_criterion_value *newv
326                 = nmem_malloc(m, sizeof(*newv));
327             newv->value = subvalues[subi];
328             newv->next = new->values;
329             new->values = newv;
330         }
331         new->next = res;
332         res = new;
333     }
334     return res;
335 }
336
337 static int database_match_criteria(struct setting **settings,
338                                    struct conf_service *service,
339                                    struct database_criterion *cl)
340 {
341     for (; cl; cl = cl->next)
342         if (!match_criterion(settings, service, cl))
343             break;
344     if (cl) // one of the criteria failed to match -- skip this db
345         return 0;
346     else
347         return 1;
348 }
349
350 // Cycles through databases, calling a handler function on the ones for
351 // which all criteria matched.
352 int session_grep_databases(struct session *se, const char *filter,
353                            void (*fun)(void *context, struct session_database *db))
354 {
355     struct session_database *p;
356     NMEM nmem = nmem_create();
357     int i = 0;
358     struct database_criterion *cl = create_database_criterion(nmem, filter);
359
360     for (p = se->databases; p; p = p->next)
361     {
362         if (p->settings && p->settings[PZ_ALLOW] && *p->settings[PZ_ALLOW]->value == '0')
363             continue;
364         if (!p->settings[PZ_NAME])
365             continue;
366         if (database_match_criteria(p->settings, se->service, cl))
367         {
368             (*fun)(se, p);
369             i++;
370         }
371     }
372     nmem_destroy(nmem);
373     return i;
374 }
375
376 int predef_grep_databases(void *context, struct conf_service *service,
377                           void (*fun)(void *context, struct database *db))
378 {
379     struct database *p;
380     int i = 0;
381
382     for (p = service->databases; p; p = p->next)
383         if (database_match_criteria(p->settings, service, 0))
384         {
385             (*fun)(context, p);
386             i++;
387         }
388     return i;
389 }
390
391 /*
392  * Local variables:
393  * c-basic-offset: 4
394  * c-file-style: "Stroustrup"
395  * indent-tabs-mode: nil
396  * End:
397  * vim: shiftwidth=4 tabstop=8 expandtab
398  */
399