1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
10 #include "lf_functions.h"
12 /* Codes for the different kinds of lsearch that are supported */
15 LSEARCH_PLAIN, /* Literal keys */
16 LSEARCH_WILD, /* Wild card keys, expanded */
17 LSEARCH_NWILD, /* Wild card keys, not expanded */
18 LSEARCH_IP /* IP addresses and networks */
23 /*************************************************
25 *************************************************/
27 /* See local README for interface description */
30 lsearch_open(const uschar * filename, uschar ** errmsg)
32 FILE *f = Ufopen(filename, "rb");
35 int save_errno = errno;
36 *errmsg = string_open_failed(errno, "%s for linear search", filename);
45 /*************************************************
47 *************************************************/
50 lsearch_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
51 gid_t *owngroups, uschar **errmsg)
53 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
54 owners, owngroups, "lsearch", errmsg) == 0;
59 /*************************************************
60 * Internal function for the various lsearches *
61 *************************************************/
63 /* See local README for interface description, plus:
67 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
70 There is some messy logic in here to cope with very long data lines that do not
71 fit into the fixed sized buffer. Most of the time this will never be exercised,
72 but people do occasionally do weird things. */
75 internal_lsearch_find(void * handle, const uschar * filename,
76 const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
79 FILE *f = (FILE *)handle;
80 BOOL last_was_eol = TRUE;
81 BOOL this_is_eol = TRUE;
82 int old_pool = store_pool;
83 rmark reset_point = NULL;
86 /* Wildcard searches may use up some store, because of expansions. We don't
87 want them to fill up our search store. What we do is set the pool to the main
88 pool and get a point to reset to later. Wildcard searches could also issue
89 lookups, but internal_search_find will take care of that, and the cache will be
90 safely stored in the search pool again. */
92 if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
94 store_pool = POOL_MAIN;
95 reset_point = store_mark();
98 filename = filename; /* Keep picky compilers happy */
102 for (last_was_eol = TRUE;
103 Ufgets(buffer, sizeof(buffer), f) != NULL;
104 last_was_eol = this_is_eol)
106 int p = Ustrlen(buffer);
108 BOOL this_is_comment;
112 /* Check whether this the final segment of a line. If it follows an
113 incomplete part-line, skip it. */
115 this_is_eol = p > 0 && buffer[p-1] == '\n';
116 if (!last_was_eol) continue;
118 /* We now have the start of a physical line. If this is a final line segment,
119 remove trailing white space. */
123 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
127 /* If the buffer is empty it might be (a) a complete empty line, or (b) the
128 start of a line that begins with so much white space that it doesn't all fit
129 in the buffer. In both cases we want to skip the entire physical line.
131 If the buffer begins with # it is a comment line; if it begins with white
132 space it is a logical continuation; again, we want to skip the entire
135 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
137 /* We assume that they key will fit in the buffer. If the key starts with ",
138 read it as a quoted string. We don't use string_dequote() because that uses
139 new store for the result, and we may be doing this many times in a long file.
140 We know that the dequoted string must be shorter than the original, because
141 we are removing the quotes, and also any escape sequences always turn two or
142 more characters into one character. Therefore, we can store the new string in
148 while (*s != 0 && *s != '\"')
150 if (*s == '\\') *t++ = string_interpret_escape(CUSS &s);
154 if (*s != 0) s++; /* Past terminating " */
155 linekeylength = t - buffer;
158 /* Otherwise it is terminated by a colon or white space */
162 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
163 linekeylength = s - buffer;
166 /* The matching test depends on which kind of lsearch we are doing */
170 /* A plain lsearch treats each key as a literal */
173 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
175 break; /* Key matched */
177 /* A wild lsearch treats each key as a possible wildcarded string; no
178 expansion is done for nwildlsearch. */
184 int save = buffer[linekeylength];
185 const uschar *list = buffer;
186 buffer[linekeylength] = 0;
187 rc = match_isinlist(keystring,
189 UCHAR_MAX+1, /* Single-item list */
190 NULL, /* No anchor */
191 NULL, /* No caching */
192 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
195 buffer[linekeylength] = save;
196 if (rc == FAIL) continue;
197 if (rc == DEFER) return DEFER;
200 /* The key has matched. If the search involved a regular expression, it
201 might have caused numerical variables to be set. However, their values will
202 be in the wrong storage pool for external use. Copying them to the standard
203 pool is not feasible because of the caching of lookup results - a repeated
204 lookup will not match the regular expression again. Therefore, we flatten
205 all numeric variables at this point. */
210 /* Compare an ip address against a list of network/ip addresses. We have to
211 allow for the "*" case specially. */
214 if (linekeylength == 1 && buffer[0] == '*')
216 if (length != 1 || keystring[0] != '*') continue;
218 else if (length == 1 && keystring[0] == '*') continue;
222 int save = buffer[linekeylength];
223 buffer[linekeylength] = 0;
224 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
225 !host_is_in_net(keystring, buffer, maskoffset)) continue;
226 buffer[linekeylength] = save;
228 break; /* Key matched */
231 /* The key has matched. Skip spaces after the key, and allow an optional
232 colon after the spaces. This is an odd specification, but it's for
235 while (isspace((uschar)*s)) s++;
239 while (isspace((uschar)*s)) s++;
242 /* Reset dynamic store, if we need to, and revert to the search pool */
246 reset_point = store_reset(reset_point);
247 store_pool = old_pool;
250 /* Now we want to build the result string to contain the data. There can be
251 two kinds of continuation: (a) the physical line may not all have fitted into
252 the buffer, and (b) there may be logical continuation lines, for which we
253 must convert all leading white space into a single blank.
255 Initialize, and copy the first segment of data. */
257 this_is_comment = FALSE;
258 yield = string_get(100);
260 yield = string_cat(yield, s);
262 /* Now handle continuations */
264 for (last_was_eol = this_is_eol;
265 Ufgets(buffer, sizeof(buffer), f) != NULL;
266 last_was_eol = this_is_eol)
270 this_is_eol = p > 0 && buffer[p-1] == '\n';
272 /* Remove trailing white space from a physical line end */
276 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
280 /* If this is not a physical line continuation, skip it entirely if it's
281 empty or starts with #. Otherwise, break the loop if it doesn't start with
282 white space. Otherwise, replace leading white space with a single blank. */
286 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
287 if (this_is_comment) continue;
288 if (!isspace((uschar)buffer[0])) break;
289 while (isspace((uschar)*s)) s++;
292 if (this_is_comment) continue;
294 /* Join a physical or logical line continuation onto the result string. */
296 yield = string_cat(yield, s);
299 gstring_release_unused(yield);
300 *result = string_from_gstring(yield);
304 /* Reset dynamic store, if we need to */
308 store_reset(reset_point);
309 store_pool = old_pool;
316 /*************************************************
317 * Find entry point for lsearch *
318 *************************************************/
320 /* See local README for interface description */
323 lsearch_find(void * handle, const uschar * filename, const uschar * keystring,
324 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
327 do_cache = do_cache; /* Keep picky compilers happy */
328 return internal_lsearch_find(handle, filename, keystring, length, result,
329 errmsg, LSEARCH_PLAIN);
334 /*************************************************
335 * Find entry point for wildlsearch *
336 *************************************************/
338 /* See local README for interface description */
341 wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
342 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
345 do_cache = do_cache; /* Keep picky compilers happy */
346 return internal_lsearch_find(handle, filename, keystring, length, result,
347 errmsg, LSEARCH_WILD);
352 /*************************************************
353 * Find entry point for nwildlsearch *
354 *************************************************/
356 /* See local README for interface description */
359 nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
360 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
363 do_cache = do_cache; /* Keep picky compilers happy */
364 return internal_lsearch_find(handle, filename, keystring, length, result,
365 errmsg, LSEARCH_NWILD);
371 /*************************************************
372 * Find entry point for iplsearch *
373 *************************************************/
375 /* See local README for interface description */
378 iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
379 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
382 do_cache = do_cache; /* Keep picky compilers happy */
384 if ((length == 1 && keystring[0] == '*') ||
385 string_is_ip_address(keystring, NULL) != 0)
386 return internal_lsearch_find(handle, filename, keystring, length, result,
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);
398 /*************************************************
399 * Close entry point *
400 *************************************************/
402 /* See local README for interface description */
405 lsearch_close(void *handle)
407 (void)fclose((FILE *)handle);
412 /*************************************************
413 * Version reporting entry point *
414 *************************************************/
416 /* See local README for interface description. */
418 #include "../version.h"
421 lsearch_version_report(FILE *f)
424 fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
429 static lookup_info iplsearch_lookup_info = {
430 .name = US"iplsearch", /* lookup name */
431 .type = lookup_absfile, /* uses absolute file name */
432 .open = lsearch_open, /* open function */
433 .check = lsearch_check, /* check function */
434 .find = iplsearch_find, /* find function */
435 .close = lsearch_close, /* close function */
436 .tidy = NULL, /* no tidy function */
437 .quote = NULL, /* no quoting function */
438 .version_report = NULL /* no version reporting (redundant) */
441 static lookup_info lsearch_lookup_info = {
442 .name = US"lsearch", /* lookup name */
443 .type = lookup_absfile, /* uses absolute file name */
444 .open = lsearch_open, /* open function */
445 .check = lsearch_check, /* check function */
446 .find = lsearch_find, /* find function */
447 .close = lsearch_close, /* close function */
448 .tidy = NULL, /* no tidy function */
449 .quote = NULL, /* no quoting function */
450 .version_report = lsearch_version_report /* version reporting */
453 static lookup_info nwildlsearch_lookup_info = {
454 .name = US"nwildlsearch", /* lookup name */
455 .type = lookup_absfile, /* uses absolute file name */
456 .open = lsearch_open, /* open function */
457 .check = lsearch_check, /* check function */
458 .find = nwildlsearch_find, /* find function */
459 .close = lsearch_close, /* close function */
460 .tidy = NULL, /* no tidy function */
461 .quote = NULL, /* no quoting function */
462 .version_report = NULL /* no version reporting (redundant) */
465 static lookup_info wildlsearch_lookup_info = {
466 .name = US"wildlsearch", /* lookup name */
467 .type = lookup_absfile, /* uses absolute file name */
468 .open = lsearch_open, /* open function */
469 .check = lsearch_check, /* check function */
470 .find = wildlsearch_find, /* find function */
471 .close = lsearch_close, /* close function */
472 .tidy = NULL, /* no tidy function */
473 .quote = NULL, /* no quoting function */
474 .version_report = NULL /* no version reporting (redundant) */
478 #define lsearch_lookup_module_info _lookup_module_info
481 static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
482 &lsearch_lookup_info,
483 &nwildlsearch_lookup_info,
484 &wildlsearch_lookup_info };
485 lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
487 /* End of lookups/lsearch.c */