1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2024 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
11 #include "lf_functions.h"
13 /* Codes for the different kinds of lsearch that are supported */
16 LSEARCH_PLAIN, /* Literal keys */
17 LSEARCH_WILD, /* Wild card keys, expanded */
18 LSEARCH_NWILD, /* Wild card keys, not expanded */
19 LSEARCH_IP /* IP addresses and networks */
24 /*************************************************
26 *************************************************/
28 /* See local README for interface description */
31 lsearch_open(const uschar * filename, uschar ** errmsg)
33 FILE * f = Ufopen(filename, "rb");
35 *errmsg = string_open_failed("%s for linear search", filename);
41 /*************************************************
43 *************************************************/
46 lsearch_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
47 gid_t *owngroups, uschar **errmsg)
49 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
50 owners, owngroups, "lsearch", errmsg) == 0;
55 /*************************************************
56 * Internal function for the various lsearches *
57 *************************************************/
59 /* See local README for interface description, plus:
63 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
66 There is some messy logic in here to cope with very long data lines that do not
67 fit into the fixed sized buffer. Most of the time this will never be exercised,
68 but people do occasionally do weird things. */
71 internal_lsearch_find(void * handle, const uschar * filename,
72 const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
73 int type, const uschar * opts)
76 BOOL ret_full = FALSE;
77 int old_pool = store_pool;
78 rmark reset_point = NULL;
86 while ((ele = string_nextinlist(&opts, &sep, NULL, 0)))
87 if (Ustrcmp(ele, "ret=full") == 0)
88 { ret_full = TRUE; break; }
91 /* Wildcard searches may use up some store, because of expansions. We don't
92 want them to fill up our search store. What we do is set the pool to the main
93 pool and get a point to reset to later. Wildcard searches could also issue
94 lookups, but internal_search_find will take care of that, and the cache will be
95 safely stored in the search pool again. */
97 if (type == LSEARCH_WILD || type == LSEARCH_NWILD)
99 store_pool = POOL_MAIN;
100 reset_point = store_mark();
104 for (BOOL this_is_eol, last_was_eol = TRUE;
105 Ufgets(buffer, sizeof(buffer), f) != NULL;
106 last_was_eol = this_is_eol)
108 int p = Ustrlen(buffer);
110 BOOL this_is_comment;
114 /* Check whether this the final segment of a line. If it follows an
115 incomplete part-line, skip it. */
117 this_is_eol = p > 0 && buffer[p-1] == '\n';
118 if (!last_was_eol) continue;
120 /* We now have the start of a physical line. If this is a final line segment,
121 remove trailing white space. */
125 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
129 /* If the buffer is empty it might be (a) a complete empty line, or (b) the
130 start of a line that begins with so much white space that it doesn't all fit
131 in the buffer. In both cases we want to skip the entire physical line.
133 If the buffer begins with # it is a comment line; if it begins with white
134 space it is a logical continuation; again, we want to skip the entire
137 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
139 /* We assume that they key will fit in the buffer. If the key starts with ",
140 read it as a quoted string. We don't use string_dequote() because that uses
141 new store for the result, and we may be doing this many times in a long file.
142 We know that the dequoted string must be shorter than the original, because
143 we are removing the quotes, and also any escape sequences always turn two or
144 more characters into one character. Therefore, we can store the new string in
150 while (*s && *s != '\"')
152 *t++ = *s == '\\' ? string_interpret_escape(CUSS &s) : *s;
155 linekeylength = t - buffer;
156 if (*s) s++; /* Past terminating " */
158 memmove(t, s, Ustrlen(s)+1); /* copy the rest of line also */
161 /* Otherwise it is terminated by a colon or white space */
165 while (*s && *s != ':' && !isspace(*s)) s++;
166 linekeylength = s - buffer;
169 /* The matching test depends on which kind of lsearch we are doing */
173 /* A plain lsearch treats each key as a literal */
176 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
178 break; /* Key matched */
180 /* A wild lsearch treats each key as a possible wildcarded string; no
181 expansion is done for nwildlsearch. */
187 int save = buffer[linekeylength];
188 const uschar *list = buffer;
189 buffer[linekeylength] = 0;
190 rc = match_isinlist(keystring,
192 UCHAR_MAX+1, /* Single-item list */
193 NULL, /* No anchor */
194 NULL, /* No caching */
195 MCL_STRING + (type == LSEARCH_WILD ? 0 : MCL_NOEXPAND),
198 buffer[linekeylength] = save;
199 if (rc == FAIL) continue;
200 if (rc == DEFER) return DEFER;
203 /* The key has matched. If the search involved a regular expression, it
204 might have caused numerical variables to be set. However, their values will
205 be in the wrong storage pool for external use. Copying them to the standard
206 pool is not feasible because of the caching of lookup results - a repeated
207 lookup will not match the regular expression again. Therefore, we drop
208 all numeric variables at this point. */
213 /* Compare an ip address against a list of network/ip addresses. We have to
214 allow for the "*" case specially. */
217 if (linekeylength == 1 && buffer[0] == '*')
219 if (length != 1 || keystring[0] != '*') continue;
221 else if (length == 1 && keystring[0] == '*') continue;
225 int save = buffer[linekeylength];
226 buffer[linekeylength] = 0;
227 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
228 !host_is_in_net(keystring, buffer, maskoffset)) continue;
229 buffer[linekeylength] = save;
231 break; /* Key matched */
234 /* The key has matched. Skip spaces after the key, and allow an optional
235 colon after the spaces. This is an odd specification, but it's for
239 if (Uskip_whitespace(&s) == ':')
242 Uskip_whitespace(&s);
245 /* Reset dynamic store, if we need to, and revert to the search pool */
249 reset_point = store_reset(reset_point);
250 store_pool = old_pool;
253 /* Now we want to build the result string to contain the data. There can be
254 two kinds of continuation: (a) the physical line may not all have fitted into
255 the buffer, and (b) there may be logical continuation lines, for which we
256 must convert all leading white space into a single blank.
258 Initialize, and copy the first segment of data. */
260 this_is_comment = FALSE;
261 yield = string_get(100);
263 yield = string_cat(yield, buffer);
265 yield = string_cat(yield, s);
267 /* Now handle continuations */
269 for (last_was_eol = this_is_eol;
270 Ufgets(buffer, sizeof(buffer), f) != NULL;
271 last_was_eol = this_is_eol)
275 this_is_eol = p > 0 && buffer[p-1] == '\n';
277 /* Remove trailing white space from a physical line end */
281 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
285 /* If this is not a physical line continuation, skip it entirely if it's
286 empty or starts with #. Otherwise, break the loop if it doesn't start with
287 white space. Otherwise, replace leading white space with a single blank. */
291 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
292 if (this_is_comment) continue;
293 if (!isspace((uschar)buffer[0])) break;
294 Uskip_whitespace(&s);
297 if (this_is_comment) continue;
299 /* Join a physical or logical line continuation onto the result string. */
301 yield = string_cat(yield, s);
304 gstring_release_unused(yield);
305 *result = string_from_gstring(yield);
309 /* Reset dynamic store, if we need to */
313 store_reset(reset_point);
314 store_pool = old_pool;
321 /*************************************************
322 * Find entry point for lsearch *
323 *************************************************/
325 /* See local README for interface description */
328 lsearch_find(void * handle, const uschar * filename, const uschar * keystring,
329 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
332 return internal_lsearch_find(handle, filename, keystring, length, result,
333 errmsg, LSEARCH_PLAIN, opts);
338 /*************************************************
339 * Find entry point for wildlsearch *
340 *************************************************/
342 /* See local README for interface description */
345 wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
346 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
349 return internal_lsearch_find(handle, filename, keystring, length, result,
350 errmsg, LSEARCH_WILD, opts);
355 /*************************************************
356 * Find entry point for nwildlsearch *
357 *************************************************/
359 /* See local README for interface description */
362 nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
363 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
366 return internal_lsearch_find(handle, filename, keystring, length, result,
367 errmsg, LSEARCH_NWILD, opts);
373 /*************************************************
374 * Find entry point for iplsearch *
375 *************************************************/
377 /* See local README for interface description */
380 iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
381 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
384 if ((length == 1 && keystring[0] == '*') ||
385 string_is_ip_address(keystring, NULL) != 0)
386 return internal_lsearch_find(handle, filename, keystring, length, result,
387 errmsg, LSEARCH_IP, opts);
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(gstring * g)
424 g = string_fmt_append(g, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
430 static lookup_info iplsearch_lookup_info = {
431 .name = US"iplsearch", /* lookup name */
432 .type = lookup_absfile, /* uses absolute file name */
433 .open = lsearch_open, /* open function */
434 .check = lsearch_check, /* check function */
435 .find = iplsearch_find, /* find function */
436 .close = lsearch_close, /* close function */
437 .tidy = NULL, /* no tidy function */
438 .quote = NULL, /* no quoting function */
439 .version_report = NULL /* no version reporting (redundant) */
442 static lookup_info lsearch_lookup_info = {
443 .name = US"lsearch", /* lookup name */
444 .type = lookup_absfile, /* uses absolute file name */
445 .open = lsearch_open, /* open function */
446 .check = lsearch_check, /* check function */
447 .find = lsearch_find, /* find function */
448 .close = lsearch_close, /* close function */
449 .tidy = NULL, /* no tidy function */
450 .quote = NULL, /* no quoting function */
451 .version_report = lsearch_version_report /* version reporting */
454 static lookup_info nwildlsearch_lookup_info = {
455 .name = US"nwildlsearch", /* lookup name */
456 .type = lookup_absfile, /* uses absolute file name */
457 .open = lsearch_open, /* open function */
458 .check = lsearch_check, /* check function */
459 .find = nwildlsearch_find, /* find function */
460 .close = lsearch_close, /* close function */
461 .tidy = NULL, /* no tidy function */
462 .quote = NULL, /* no quoting function */
463 .version_report = NULL /* no version reporting (redundant) */
466 static lookup_info wildlsearch_lookup_info = {
467 .name = US"wildlsearch", /* lookup name */
468 .type = lookup_absfile, /* uses absolute file name */
469 .open = lsearch_open, /* open function */
470 .check = lsearch_check, /* check function */
471 .find = wildlsearch_find, /* find function */
472 .close = lsearch_close, /* close function */
473 .tidy = NULL, /* no tidy function */
474 .quote = NULL, /* no quoting function */
475 .version_report = NULL /* no version reporting (redundant) */
479 #define lsearch_lookup_module_info _lookup_module_info
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 };
488 /* End of lookups/lsearch.c */