DNS: time-limit cached returns, using TTL. Bug 1395
[exim.git] / src / src / lookups / lsearch.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 #include "../exim.h"
9 #include "lf_functions.h"
10
11 /* Codes for the different kinds of lsearch that are supported */
12
13 enum {
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
28 static void *
29 lsearch_open(uschar *filename, uschar **errmsg)
30 {
31 FILE *f = Ufopen(filename, "rb");
32 if (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   }
39 return f;
40 }
41
42
43
44 /*************************************************
45 *             Check entry point                  *
46 *************************************************/
47
48 static BOOL
49 lsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
50   gid_t *owngroups, uschar **errmsg)
51 {
52 return 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
64 Extra argument:
65
66   type     one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
67            LSEARCH_IP
68
69 There is some messy logic in here to cope with very long data lines that do not
70 fit into the fixed sized buffer. Most of the time this will never be exercised,
71 but people do occasionally do weird things. */
72
73 static int
74 internal_lsearch_find(void *handle, uschar *filename, const uschar *keystring,
75   int length, uschar **result, uschar **errmsg, int type)
76 {
77 FILE *f = (FILE *)handle;
78 BOOL last_was_eol = TRUE;
79 BOOL this_is_eol = TRUE;
80 int old_pool = store_pool;
81 void *reset_point = NULL;
82 uschar buffer[4096];
83
84 /* Wildcard searches may use up some store, because of expansions. We don't
85 want them to fill up our search store. What we do is set the pool to the main
86 pool and get a point to reset to later. Wildcard searches could also issue
87 lookups, but internal_search_find will take care of that, and the cache will be
88 safely stored in the search pool again. */
89
90 if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
91   {
92   store_pool = POOL_MAIN;
93   reset_point = store_get(0);
94   }
95
96 filename = filename;  /* Keep picky compilers happy */
97 errmsg = errmsg;
98
99 rewind(f);
100 for (last_was_eol = TRUE;
101      Ufgets(buffer, sizeof(buffer), f) != NULL;
102      last_was_eol = this_is_eol)
103   {
104   int ptr, size;
105   int p = Ustrlen(buffer);
106   int linekeylength;
107   BOOL this_is_comment;
108   uschar *yield;
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       {
149       if (*s == '\\') *t++ = string_interpret_escape(CUSS &s);
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];
184       const uschar *list = buffer;
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       }
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;
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;
223       if (string_is_ip_address(buffer, &maskoffset) == 0 ||
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
243   if (reset_point != NULL)
244     {
245     store_reset(reset_point);
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
256   this_is_comment = FALSE;
257   size = 100;
258   ptr = 0;
259   yield = store_get(size);
260   if (*s != 0)
261     yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
262
263   /* Now handle continuations */
264
265   for (last_was_eol = this_is_eol;
266        Ufgets(buffer, sizeof(buffer), f) != NULL;
267        last_was_eol = this_is_eol)
268     {
269     s = buffer;
270     p = Ustrlen(buffer);
271     this_is_eol = p > 0 && buffer[p-1] == '\n';
272
273     /* Remove trailing white space from a physical line end */
274
275     if (this_is_eol)
276       {
277       while (p > 0 && isspace((uschar)buffer[p-1])) p--;
278       buffer[p] = 0;
279       }
280
281     /* If this is not a physical line continuation, skip it entirely if it's
282     empty or starts with #. Otherwise, break the loop if it doesn't start with
283     white space. Otherwise, replace leading white space with a single blank. */
284
285     if (last_was_eol)
286       {
287       this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
288       if (this_is_comment) continue;
289       if (!isspace((uschar)buffer[0])) break;
290       while (isspace((uschar)*s)) s++;
291       *(--s) = ' ';
292       }
293     if (this_is_comment) continue;
294
295     /* Join a physical or logical line continuation onto the result string. */
296
297     yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
298     }
299
300   yield[ptr] = 0;
301   store_reset(yield + ptr + 1);
302   *result = yield;
303   return OK;
304   }
305
306 /* Reset dynamic store, if we need to */
307
308 if (reset_point != NULL)
309   {
310   store_reset(reset_point);
311   store_pool = old_pool;
312   }
313
314 return FAIL;
315 }
316
317
318 /*************************************************
319 *         Find entry point for lsearch           *
320 *************************************************/
321
322 /* See local README for interface description */
323
324 static int
325 lsearch_find(void *handle, uschar *filename, const uschar *keystring, int length,
326   uschar **result, uschar **errmsg, uint *do_cache)
327 {
328 do_cache = do_cache;  /* Keep picky compilers happy */
329 return internal_lsearch_find(handle, filename, keystring, length, result,
330   errmsg, LSEARCH_PLAIN);
331 }
332
333
334
335 /*************************************************
336 *      Find entry point for wildlsearch          *
337 *************************************************/
338
339 /* See local README for interface description */
340
341 static int
342 wildlsearch_find(void *handle, uschar *filename, const uschar *keystring, int length,
343   uschar **result, uschar **errmsg, uint *do_cache)
344 {
345 do_cache = do_cache;  /* Keep picky compilers happy */
346 return internal_lsearch_find(handle, filename, keystring, length, result,
347   errmsg, LSEARCH_WILD);
348 }
349
350
351
352 /*************************************************
353 *      Find entry point for nwildlsearch         *
354 *************************************************/
355
356 /* See local README for interface description */
357
358 static int
359 nwildlsearch_find(void *handle, uschar *filename, const uschar *keystring, int length,
360   uschar **result, uschar **errmsg, uint *do_cache)
361 {
362 do_cache = do_cache;  /* Keep picky compilers happy */
363 return internal_lsearch_find(handle, filename, keystring, length, result,
364   errmsg, LSEARCH_NWILD);
365 }
366
367
368
369
370 /*************************************************
371 *      Find entry point for iplsearch            *
372 *************************************************/
373
374 /* See local README for interface description */
375
376 static int
377 iplsearch_find(void *handle, uschar *filename, const uschar *keystring, int length,
378   uschar **result, uschar **errmsg, uint *do_cache)
379 {
380 do_cache = do_cache;  /* Keep picky compilers happy */
381 if ((length == 1 && keystring[0] == '*') ||
382     string_is_ip_address(keystring, NULL) != 0)
383   {
384   return internal_lsearch_find(handle, filename, keystring, length, result,
385     errmsg, LSEARCH_IP);
386   }
387 else
388   {
389   *errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
390     "address, with optional CIDR mask, is wanted): "
391     "in a host list, use net-iplsearch as the search type", keystring);
392   return DEFER;
393   }
394 }
395
396
397
398
399 /*************************************************
400 *              Close entry point                 *
401 *************************************************/
402
403 /* See local README for interface description */
404
405 static void
406 lsearch_close(void *handle)
407 {
408 (void)fclose((FILE *)handle);
409 }
410
411
412
413 /*************************************************
414 *         Version reporting entry point          *
415 *************************************************/
416
417 /* See local README for interface description. */
418
419 #include "../version.h"
420
421 void
422 lsearch_version_report(FILE *f)
423 {
424 #ifdef DYNLOOKUP
425 fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
426 #endif
427 }
428
429
430 static lookup_info iplsearch_lookup_info = {
431   US"iplsearch",                 /* lookup name */
432   lookup_absfile,                /* uses absolute file name */
433   lsearch_open,                  /* open function */
434   lsearch_check,                 /* check function */
435   iplsearch_find,                /* find function */
436   lsearch_close,                 /* close function */
437   NULL,                          /* no tidy function */
438   NULL,                          /* no quoting function */
439   NULL                           /* no version reporting (redundant) */
440 };
441
442 static lookup_info lsearch_lookup_info = {
443   US"lsearch",                   /* lookup name */
444   lookup_absfile,                /* uses absolute file name */
445   lsearch_open,                  /* open function */
446   lsearch_check,                 /* check function */
447   lsearch_find,                  /* find function */
448   lsearch_close,                 /* close function */
449   NULL,                          /* no tidy function */
450   NULL,                          /* no quoting function */
451   lsearch_version_report         /* version reporting */
452 };
453
454 static lookup_info nwildlsearch_lookup_info = {
455   US"nwildlsearch",              /* lookup name */
456   lookup_absfile,                /* uses absolute file name */
457   lsearch_open,                  /* open function */
458   lsearch_check,                 /* check function */
459   nwildlsearch_find,             /* find function */
460   lsearch_close,                 /* close function */
461   NULL,                          /* no tidy function */
462   NULL,                          /* no quoting function */
463   NULL                           /* no version reporting (redundant) */
464 };
465
466 static lookup_info wildlsearch_lookup_info = {
467   US"wildlsearch",               /* lookup name */
468   lookup_absfile,                /* uses absolute file name */
469   lsearch_open,                  /* open function */
470   lsearch_check,                 /* check function */
471   wildlsearch_find,              /* find function */
472   lsearch_close,                 /* close function */
473   NULL,                          /* no tidy function */
474   NULL,                          /* no quoting function */
475   NULL                           /* no version reporting (redundant) */
476 };
477
478 #ifdef DYNLOOKUP
479 #define lsearch_lookup_module_info _lookup_module_info
480 #endif
481
482 static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
483                                        &lsearch_lookup_info,
484                                        &nwildlsearch_lookup_info,
485                                        &wildlsearch_lookup_info };
486 lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
487
488 /* End of lookups/lsearch.c */