Sqlite: new main option sqlite_dbfile
[exim.git] / src / src / lookups / lsearch.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
f9ba5e22 5/* Copyright (c) University of Cambridge 1995 - 2018 */
0756eb3c
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8#include "../exim.h"
9#include "lf_functions.h"
0756eb3c
PH
10
11/* Codes for the different kinds of lsearch that are supported */
12
13enum {
14 LSEARCH_PLAIN, /* Literal keys */
15 LSEARCH_WILD, /* Wild card keys, expanded */
16 LSEARCH_NWILD, /* Wild card keys, not expanded */
17 LSEARCH_IP /* IP addresses and networks */
18};
19
20
21
22/*************************************************
23* Open entry point *
24*************************************************/
25
26/* See local README for interface description */
27
e6d225ae 28static void *
d447dbd1 29lsearch_open(const uschar * filename, uschar ** errmsg)
0756eb3c
PH
30{
31FILE *f = Ufopen(filename, "rb");
32if (f == NULL)
33 {
34 int save_errno = errno;
35 *errmsg = string_open_failed(errno, "%s for linear search", filename);
36 errno = save_errno;
37 return NULL;
38 }
39return f;
40}
41
42
43
44/*************************************************
45* Check entry point *
46*************************************************/
47
e6d225ae 48static BOOL
d447dbd1 49lsearch_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
0756eb3c
PH
50 gid_t *owngroups, uschar **errmsg)
51{
52return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
53 owners, owngroups, "lsearch", errmsg) == 0;
54}
55
56
57
58/*************************************************
59* Internal function for the various lsearches *
60*************************************************/
61
62/* See local README for interface description, plus:
63
64Extra argument:
65
66 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
67 LSEARCH_IP
68
69There is some messy logic in here to cope with very long data lines that do not
70fit into the fixed sized buffer. Most of the time this will never be exercised,
71but people do occasionally do weird things. */
72
73static int
d447dbd1
JH
74internal_lsearch_find(void * handle, const uschar * filename,
75 const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
76 int type)
0756eb3c
PH
77{
78FILE *f = (FILE *)handle;
79BOOL last_was_eol = TRUE;
80BOOL this_is_eol = TRUE;
81int old_pool = store_pool;
f3ebb786 82rmark reset_point = NULL;
0756eb3c
PH
83uschar buffer[4096];
84
85/* Wildcard searches may use up some store, because of expansions. We don't
86want them to fill up our search store. What we do is set the pool to the main
87pool and get a point to reset to later. Wildcard searches could also issue
88lookups, but internal_search_find will take care of that, and the cache will be
89safely stored in the search pool again. */
90
91if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
92 {
93 store_pool = POOL_MAIN;
f3ebb786 94 reset_point = store_mark();
0756eb3c
PH
95 }
96
97filename = filename; /* Keep picky compilers happy */
98errmsg = errmsg;
99
100rewind(f);
101for (last_was_eol = TRUE;
102 Ufgets(buffer, sizeof(buffer), f) != NULL;
103 last_was_eol = this_is_eol)
104 {
0756eb3c
PH
105 int p = Ustrlen(buffer);
106 int linekeylength;
dbb0bf41 107 BOOL this_is_comment;
acec9514 108 gstring * yield;
0756eb3c
PH
109 uschar *s = buffer;
110
111 /* Check whether this the final segment of a line. If it follows an
112 incomplete part-line, skip it. */
113
114 this_is_eol = p > 0 && buffer[p-1] == '\n';
115 if (!last_was_eol) continue;
116
117 /* We now have the start of a physical line. If this is a final line segment,
118 remove trailing white space. */
119
120 if (this_is_eol)
121 {
122 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
123 buffer[p] = 0;
124 }
125
126 /* If the buffer is empty it might be (a) a complete empty line, or (b) the
127 start of a line that begins with so much white space that it doesn't all fit
128 in the buffer. In both cases we want to skip the entire physical line.
129
130 If the buffer begins with # it is a comment line; if it begins with white
131 space it is a logical continuation; again, we want to skip the entire
132 physical line. */
133
134 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
135
136 /* We assume that they key will fit in the buffer. If the key starts with ",
137 read it as a quoted string. We don't use string_dequote() because that uses
138 new store for the result, and we may be doing this many times in a long file.
139 We know that the dequoted string must be shorter than the original, because
140 we are removing the quotes, and also any escape sequences always turn two or
141 more characters into one character. Therefore, we can store the new string in
142 the same buffer. */
143
144 if (*s == '\"')
145 {
146 uschar *t = s++;
147 while (*s != 0 && *s != '\"')
148 {
55414b25 149 if (*s == '\\') *t++ = string_interpret_escape(CUSS &s);
0756eb3c
PH
150 else *t++ = *s;
151 s++;
152 }
153 if (*s != 0) s++; /* Past terminating " */
154 linekeylength = t - buffer;
155 }
156
157 /* Otherwise it is terminated by a colon or white space */
158
159 else
160 {
161 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
162 linekeylength = s - buffer;
163 }
164
165 /* The matching test depends on which kind of lsearch we are doing */
166
167 switch(type)
168 {
169 /* A plain lsearch treats each key as a literal */
170
171 case LSEARCH_PLAIN:
172 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
173 continue;
174 break; /* Key matched */
175
176 /* A wild lsearch treats each key as a possible wildcarded string; no
177 expansion is done for nwildlsearch. */
178
179 case LSEARCH_WILD:
180 case LSEARCH_NWILD:
181 {
182 int rc;
183 int save = buffer[linekeylength];
55414b25 184 const uschar *list = buffer;
0756eb3c
PH
185 buffer[linekeylength] = 0;
186 rc = match_isinlist(keystring,
187 &list,
188 UCHAR_MAX+1, /* Single-item list */
189 NULL, /* No anchor */
190 NULL, /* No caching */
191 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
192 TRUE, /* Caseless */
193 NULL);
194 buffer[linekeylength] = save;
195 if (rc == FAIL) continue;
196 if (rc == DEFER) return DEFER;
197 }
eba0c039
PH
198
199 /* The key has matched. If the search involved a regular expression, it
200 might have caused numerical variables to be set. However, their values will
201 be in the wrong storage pool for external use. Copying them to the standard
202 pool is not feasible because of the caching of lookup results - a repeated
203 lookup will not match the regular expression again. Therefore, we flatten
204 all numeric variables at this point. */
205
206 expand_nmax = -1;
207 break;
0756eb3c
PH
208
209 /* Compare an ip address against a list of network/ip addresses. We have to
210 allow for the "*" case specially. */
211
212 case LSEARCH_IP:
213 if (linekeylength == 1 && buffer[0] == '*')
214 {
215 if (length != 1 || keystring[0] != '*') continue;
216 }
217 else if (length == 1 && keystring[0] == '*') continue;
218 else
219 {
220 int maskoffset;
221 int save = buffer[linekeylength];
222 buffer[linekeylength] = 0;
a5a28604 223 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
0756eb3c
PH
224 !host_is_in_net(keystring, buffer, maskoffset)) continue;
225 buffer[linekeylength] = save;
226 }
227 break; /* Key matched */
228 }
229
230 /* The key has matched. Skip spaces after the key, and allow an optional
231 colon after the spaces. This is an odd specification, but it's for
232 compatibility. */
233
234 while (isspace((uschar)*s)) s++;
235 if (*s == ':')
236 {
237 s++;
238 while (isspace((uschar)*s)) s++;
239 }
240
241 /* Reset dynamic store, if we need to, and revert to the search pool */
242
acec9514 243 if (reset_point)
0756eb3c 244 {
f3ebb786 245 reset_point = store_reset(reset_point);
0756eb3c
PH
246 store_pool = old_pool;
247 }
248
249 /* Now we want to build the result string to contain the data. There can be
250 two kinds of continuation: (a) the physical line may not all have fitted into
251 the buffer, and (b) there may be logical continuation lines, for which we
252 must convert all leading white space into a single blank.
253
254 Initialize, and copy the first segment of data. */
255
dbb0bf41 256 this_is_comment = FALSE;
acec9514 257 yield = string_get(100);
0756eb3c 258 if (*s != 0)
acec9514 259 yield = string_cat(yield, s);
0756eb3c
PH
260
261 /* Now handle continuations */
262
263 for (last_was_eol = this_is_eol;
264 Ufgets(buffer, sizeof(buffer), f) != NULL;
265 last_was_eol = this_is_eol)
266 {
267 s = buffer;
268 p = Ustrlen(buffer);
269 this_is_eol = p > 0 && buffer[p-1] == '\n';
270
271 /* Remove trailing white space from a physical line end */
272
273 if (this_is_eol)
274 {
275 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
276 buffer[p] = 0;
277 }
278
279 /* If this is not a physical line continuation, skip it entirely if it's
280 empty or starts with #. Otherwise, break the loop if it doesn't start with
281 white space. Otherwise, replace leading white space with a single blank. */
282
283 if (last_was_eol)
284 {
31ffd7bf 285 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
dbb0bf41 286 if (this_is_comment) continue;
0756eb3c
PH
287 if (!isspace((uschar)buffer[0])) break;
288 while (isspace((uschar)*s)) s++;
289 *(--s) = ' ';
290 }
dbb0bf41 291 if (this_is_comment) continue;
0756eb3c
PH
292
293 /* Join a physical or logical line continuation onto the result string. */
294
acec9514 295 yield = string_cat(yield, s);
0756eb3c
PH
296 }
297
f3ebb786 298 gstring_release_unused(yield);
acec9514 299 *result = string_from_gstring(yield);
0756eb3c
PH
300 return OK;
301 }
302
303/* Reset dynamic store, if we need to */
304
acec9514 305if (reset_point)
0756eb3c
PH
306 {
307 store_reset(reset_point);
308 store_pool = old_pool;
309 }
310
311return FAIL;
312}
313
314
315/*************************************************
316* Find entry point for lsearch *
317*************************************************/
318
319/* See local README for interface description */
320
e6d225ae 321static int
d447dbd1
JH
322lsearch_find(void * handle, const uschar * filename, const uschar * keystring,
323 int length, uschar ** result, uschar ** errmsg, uint * do_cache)
0756eb3c
PH
324{
325do_cache = do_cache; /* Keep picky compilers happy */
326return internal_lsearch_find(handle, filename, keystring, length, result,
327 errmsg, LSEARCH_PLAIN);
328}
329
330
331
332/*************************************************
333* Find entry point for wildlsearch *
334*************************************************/
335
336/* See local README for interface description */
337
e6d225ae 338static int
d447dbd1
JH
339wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
340 int length, uschar ** result, uschar ** errmsg, uint * do_cache)
0756eb3c
PH
341{
342do_cache = do_cache; /* Keep picky compilers happy */
343return internal_lsearch_find(handle, filename, keystring, length, result,
344 errmsg, LSEARCH_WILD);
345}
346
347
348
349/*************************************************
350* Find entry point for nwildlsearch *
351*************************************************/
352
353/* See local README for interface description */
354
e6d225ae 355static int
d447dbd1
JH
356nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
357 int length, uschar ** result, uschar ** errmsg, uint * do_cache)
0756eb3c
PH
358{
359do_cache = do_cache; /* Keep picky compilers happy */
360return internal_lsearch_find(handle, filename, keystring, length, result,
361 errmsg, LSEARCH_NWILD);
362}
363
364
365
366
367/*************************************************
368* Find entry point for iplsearch *
369*************************************************/
370
371/* See local README for interface description */
372
e6d225ae 373static int
d447dbd1
JH
374iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
375 int length, uschar ** result, uschar ** errmsg, uint * do_cache)
0756eb3c
PH
376{
377do_cache = do_cache; /* Keep picky compilers happy */
d447dbd1 378
0756eb3c 379if ((length == 1 && keystring[0] == '*') ||
7e66e54d 380 string_is_ip_address(keystring, NULL) != 0)
0756eb3c
PH
381 return internal_lsearch_find(handle, filename, keystring, length, result,
382 errmsg, LSEARCH_IP);
d447dbd1
JH
383
384*errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
385"address, with optional CIDR mask, is wanted): "
386"in a host list, use net-iplsearch as the search type", keystring);
387return DEFER;
0756eb3c
PH
388}
389
390
391
392
393/*************************************************
394* Close entry point *
395*************************************************/
396
397/* See local README for interface description */
398
e6d225ae 399static void
0756eb3c
PH
400lsearch_close(void *handle)
401{
f1e894f3 402(void)fclose((FILE *)handle);
0756eb3c
PH
403}
404
6545de78
PP
405
406
407/*************************************************
408* Version reporting entry point *
409*************************************************/
410
411/* See local README for interface description. */
412
413#include "../version.h"
414
415void
416lsearch_version_report(FILE *f)
417{
418#ifdef DYNLOOKUP
419fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
420#endif
421}
422
423
e6d225ae
DW
424static lookup_info iplsearch_lookup_info = {
425 US"iplsearch", /* lookup name */
426 lookup_absfile, /* uses absolute file name */
427 lsearch_open, /* open function */
428 lsearch_check, /* check function */
429 iplsearch_find, /* find function */
430 lsearch_close, /* close function */
431 NULL, /* no tidy function */
6545de78
PP
432 NULL, /* no quoting function */
433 NULL /* no version reporting (redundant) */
e6d225ae
DW
434};
435
436static lookup_info lsearch_lookup_info = {
437 US"lsearch", /* lookup name */
438 lookup_absfile, /* uses absolute file name */
439 lsearch_open, /* open function */
440 lsearch_check, /* check function */
441 lsearch_find, /* find function */
442 lsearch_close, /* close function */
443 NULL, /* no tidy function */
6545de78
PP
444 NULL, /* no quoting function */
445 lsearch_version_report /* version reporting */
e6d225ae
DW
446};
447
448static lookup_info nwildlsearch_lookup_info = {
449 US"nwildlsearch", /* lookup name */
450 lookup_absfile, /* uses absolute file name */
451 lsearch_open, /* open function */
452 lsearch_check, /* check function */
453 nwildlsearch_find, /* find function */
454 lsearch_close, /* close function */
455 NULL, /* no tidy function */
6545de78
PP
456 NULL, /* no quoting function */
457 NULL /* no version reporting (redundant) */
e6d225ae
DW
458};
459
460static lookup_info wildlsearch_lookup_info = {
461 US"wildlsearch", /* lookup name */
462 lookup_absfile, /* uses absolute file name */
463 lsearch_open, /* open function */
464 lsearch_check, /* check function */
465 wildlsearch_find, /* find function */
466 lsearch_close, /* close function */
467 NULL, /* no tidy function */
6545de78
PP
468 NULL, /* no quoting function */
469 NULL /* no version reporting (redundant) */
e6d225ae
DW
470};
471
472#ifdef DYNLOOKUP
473#define lsearch_lookup_module_info _lookup_module_info
474#endif
475
476static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
477 &lsearch_lookup_info,
478 &nwildlsearch_lookup_info,
479 &wildlsearch_lookup_info };
480lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
481
0756eb3c 482/* End of lookups/lsearch.c */