Fix broken ~ match if filter contains = PAZ-941
[pazpar2-moved-to-github.git] / src / database.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 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 <assert.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <yaz/log.h>
28 #include <yaz/nmem.h>
29
30 #include "ppmutex.h"
31 #include "session.h"
32 #include "host.h"
33 #include "pazpar2_config.h"
34 #include "settings.h"
35 #include "http.h"
36 #include "database.h"
37
38 #include <sys/types.h>
39
40 enum pazpar2_database_criterion_type {
41     PAZPAR2_STRING_MATCH,
42     PAZPAR2_SUBSTRING_MATCH
43 };
44
45 struct database_criterion_value {
46     char *value;
47     struct database_criterion_value *next;
48 };
49
50 struct database_criterion {
51     char *name;
52     enum pazpar2_database_criterion_type type;
53     struct database_criterion_value *values;
54     struct database_criterion *next;
55 };
56
57 struct database *new_database(const char *id, NMEM nmem)
58 {
59     return new_database_inherit_settings(id, nmem, 0);
60 }
61 struct database *new_database_inherit_settings(const char *id, NMEM nmem, struct settings_array *service_settings)
62 {
63     struct database *db;
64     struct setting *idset;
65
66     db = nmem_malloc(nmem, sizeof(*db));
67     db->id = nmem_strdup(nmem, id);
68     db->next = 0;
69
70     if (service_settings && service_settings->num_settings > 0) {
71         yaz_log(YLOG_DEBUG, "copying settings from service to database %s settings", db->id);
72         db->num_settings = service_settings->num_settings;
73         db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings);
74         // Initialize database settings with service settings
75         memcpy(db->settings, service_settings->settings,  sizeof(*db->settings) * db->num_settings);
76     }
77     else {
78         yaz_log(YLOG_DEBUG, "No service settings to database %s ", db->id);
79         db->num_settings = PZ_MAX_EOF;
80         db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings);
81         memset(db->settings, 0, sizeof(*db->settings) * db->num_settings);
82     }
83     idset = nmem_malloc(nmem, sizeof(*idset));
84     idset->precedence = 0;
85     idset->name = "pz:id";
86     idset->target = idset->value = db->id;
87     idset->next = db->settings[PZ_ID];
88     db->settings[PZ_ID] = idset;
89
90     return db;
91 }
92
93 // Return a database structure by ID. Load and add to list if necessary
94 // new==1 just means we know it's not in the list
95 struct database *create_database_for_service(const char *id,
96                                              struct conf_service *service)
97 {
98     struct database *p;
99     for (p = service->databases; p; p = p->next)
100         if (!strcmp(p->id, id))
101             return p;
102
103     yaz_log(YLOG_DEBUG, "new database %s under service %s", id,
104        service->id ? service->id : "null");
105     p = new_database_inherit_settings(id, service->nmem, service->settings);
106     p->next = service->databases;
107     service->databases = p;
108
109     return p;
110 }
111
112 // This whole session_grep database thing should be moved elsewhere
113
114 int match_zurl(const char *zurl, const char *pattern)
115 {
116     int len;
117
118     if (!strcmp(pattern, "*"))
119         return 1;
120     else if (!strncmp(pattern, "*/", 2))   // host wildcard.. what the heck is that for?
121     {
122         char *db = strchr(zurl, '/');
123         if (!db)
124             return 0;
125         if (!strcmp(pattern + 2, db))
126             return 1;
127         else
128             return 0;
129     }
130     else if (*(pattern + (len = strlen(pattern) - 1)) == '*')  // db wildcard
131     {
132         if (!strncmp(pattern, zurl, len))
133             return 1;
134         else
135             return 0;
136     }
137     else if (!strcmp(pattern, zurl))
138         return 1;
139     else
140         return 0;
141 }
142
143 // This will be generalized at some point
144 static int match_criterion(struct setting **settings,
145                            struct conf_service *service,
146                            struct database_criterion *c)
147 {
148     int offset = settings_lookup_offset(service, c->name);
149     struct database_criterion_value *v;
150
151     if (offset < 0)
152     {
153         yaz_log(YLOG_WARN, "Criterion not found: %s", c->name);
154         return 0;
155     }
156     if (!settings[offset])
157         return 0;
158     for (v = c->values; v; v = v->next)
159     {
160         if (c->type == PAZPAR2_STRING_MATCH)
161         {
162             if (offset == PZ_ID)
163             {
164                 if (match_zurl(settings[offset]->value, v->value))
165                     break;
166             }
167             else
168             {
169                 if (!strcmp(settings[offset]->value, v->value))
170                     break;
171             }
172         }
173         else if (c->type == PAZPAR2_SUBSTRING_MATCH)
174         {
175             if (strstr(settings[offset]->value, v->value))
176                 break;
177         }
178     }
179     if (v)
180         return 1;
181     else
182         return 0;
183 }
184
185 // parses crit1=val1,crit2=val2|val3,...
186 static struct database_criterion *create_database_criterion(NMEM m,
187                                                             const char *buf)
188 {
189     struct database_criterion *res = 0;
190     char **values;
191     int num;
192     int i;
193
194     if (!buf || !*buf)
195         return 0;
196     nmem_strsplit(m, ",", buf,  &values, &num);
197     for (i = 0; i < num; i++)
198     {
199         char **subvalues;
200         int subnum;
201         int subi;
202         struct database_criterion *new = nmem_malloc(m, sizeof(*new));
203         char *eq;
204         for (eq = values[i]; *eq; eq++)
205             if (*eq == '=')
206             {
207                 new->type = PAZPAR2_STRING_MATCH;
208                 break;
209             }
210             else if (*eq == '~')
211             {
212                 new->type = PAZPAR2_SUBSTRING_MATCH;
213                 break;
214             }
215         if (!*eq)
216         {
217             yaz_log(YLOG_WARN, "Missing equal-sign/tilde in filter");
218             return 0;
219         }
220         *(eq++) = '\0';
221         new->name = values[i];
222         nmem_strsplit(m, "|", eq, &subvalues, &subnum);
223         new->values = 0;
224         for (subi = 0; subi < subnum; subi++)
225         {
226             struct database_criterion_value *newv
227                 = nmem_malloc(m, sizeof(*newv));
228             newv->value = subvalues[subi];
229             newv->next = new->values;
230             new->values = newv;
231         }
232         new->next = res;
233         res = new;
234     }
235     return res;
236 }
237
238 static int database_match_criteria(struct setting **settings,
239                                    struct conf_service *service,
240                                    struct database_criterion *cl)
241 {
242     for (; cl; cl = cl->next)
243         if (!match_criterion(settings, service, cl))
244             break;
245     if (cl) // one of the criteria failed to match -- skip this db
246         return 0;
247     else
248         return 1;
249 }
250
251 // Cycles through databases, calling a handler function on the ones for
252 // which all criteria matched.
253 int session_grep_databases(struct session *se, const char *filter,
254                            void (*fun)(struct session *se, struct session_database *db))
255 {
256     struct session_database *p;
257     NMEM nmem = nmem_create();
258     int i = 0;
259     struct database_criterion *cl = create_database_criterion(nmem, filter);
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 (!p->settings[PZ_NAME])
266             continue;
267         if (database_match_criteria(p->settings, se->service, cl))
268         {
269             (*fun)(se, p);
270             i++;
271         }
272     }
273     nmem_destroy(nmem);
274     return i;
275 }
276
277 int predef_grep_databases(void *context, struct conf_service *service,
278                           void (*fun)(void *context, struct database *db))
279 {
280     struct database *p;
281     int i = 0;
282
283     for (p = service->databases; p; p = p->next)
284         if (database_match_criteria(p->settings, service, 0))
285         {
286             (*fun)(context, p);
287             i++;
288         }
289     return i;
290 }
291
292 /*
293  * Local variables:
294  * c-basic-offset: 4
295  * c-file-style: "Stroustrup"
296  * indent-tabs-mode: nil
297  * End:
298  * vim: shiftwidth=4 tabstop=8 expandtab
299  */
300