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