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");
34 *errmsg = string_open_failed("%s for linear search", filename);
40 /*************************************************
42 *************************************************/
45 lsearch_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
46 gid_t *owngroups, uschar **errmsg)
48 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
49 owners, owngroups, "lsearch", errmsg) == 0;
54 /*************************************************
55 * Internal function for the various lsearches *
56 *************************************************/
58 /* See local README for interface description, plus:
62 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
65 There is some messy logic in here to cope with very long data lines that do not
66 fit into the fixed sized buffer. Most of the time this will never be exercised,
67 but people do occasionally do weird things. */
70 internal_lsearch_find(void * handle, const uschar * filename,
71 const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
74 FILE *f = (FILE *)handle;
75 BOOL last_was_eol = TRUE;
76 BOOL this_is_eol = TRUE;
77 int old_pool = store_pool;
78 rmark reset_point = NULL;
81 /* Wildcard searches may use up some store, because of expansions. We don't
82 want them to fill up our search store. What we do is set the pool to the main
83 pool and get a point to reset to later. Wildcard searches could also issue
84 lookups, but internal_search_find will take care of that, and the cache will be
85 safely stored in the search pool again. */
87 if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
89 store_pool = POOL_MAIN;
90 reset_point = store_mark();
94 for (last_was_eol = TRUE;
95 Ufgets(buffer, sizeof(buffer), f) != NULL;
96 last_was_eol = this_is_eol)
98 int p = Ustrlen(buffer);
100 BOOL this_is_comment;
104 /* Check whether this the final segment of a line. If it follows an
105 incomplete part-line, skip it. */
107 this_is_eol = p > 0 && buffer[p-1] == '\n';
108 if (!last_was_eol) continue;
110 /* We now have the start of a physical line. If this is a final line segment,
111 remove trailing white space. */
115 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
119 /* If the buffer is empty it might be (a) a complete empty line, or (b) the
120 start of a line that begins with so much white space that it doesn't all fit
121 in the buffer. In both cases we want to skip the entire physical line.
123 If the buffer begins with # it is a comment line; if it begins with white
124 space it is a logical continuation; again, we want to skip the entire
127 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
129 /* We assume that they key will fit in the buffer. If the key starts with ",
130 read it as a quoted string. We don't use string_dequote() because that uses
131 new store for the result, and we may be doing this many times in a long file.
132 We know that the dequoted string must be shorter than the original, because
133 we are removing the quotes, and also any escape sequences always turn two or
134 more characters into one character. Therefore, we can store the new string in
140 while (*s != 0 && *s != '\"')
142 if (*s == '\\') *t++ = string_interpret_escape(CUSS &s);
146 if (*s != 0) s++; /* Past terminating " */
147 linekeylength = t - buffer;
150 /* Otherwise it is terminated by a colon or white space */
154 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
155 linekeylength = s - buffer;
158 /* The matching test depends on which kind of lsearch we are doing */
162 /* A plain lsearch treats each key as a literal */
165 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
167 break; /* Key matched */
169 /* A wild lsearch treats each key as a possible wildcarded string; no
170 expansion is done for nwildlsearch. */
176 int save = buffer[linekeylength];
177 const uschar *list = buffer;
178 buffer[linekeylength] = 0;
179 rc = match_isinlist(keystring,
181 UCHAR_MAX+1, /* Single-item list */
182 NULL, /* No anchor */
183 NULL, /* No caching */
184 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
187 buffer[linekeylength] = save;
188 if (rc == FAIL) continue;
189 if (rc == DEFER) return DEFER;
192 /* The key has matched. If the search involved a regular expression, it
193 might have caused numerical variables to be set. However, their values will
194 be in the wrong storage pool for external use. Copying them to the standard
195 pool is not feasible because of the caching of lookup results - a repeated
196 lookup will not match the regular expression again. Therefore, we flatten
197 all numeric variables at this point. */
202 /* Compare an ip address against a list of network/ip addresses. We have to
203 allow for the "*" case specially. */
206 if (linekeylength == 1 && buffer[0] == '*')
208 if (length != 1 || keystring[0] != '*') continue;
210 else if (length == 1 && keystring[0] == '*') continue;
214 int save = buffer[linekeylength];
215 buffer[linekeylength] = 0;
216 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
217 !host_is_in_net(keystring, buffer, maskoffset)) continue;
218 buffer[linekeylength] = save;
220 break; /* Key matched */
223 /* The key has matched. Skip spaces after the key, and allow an optional
224 colon after the spaces. This is an odd specification, but it's for
227 while (isspace((uschar)*s)) s++;
231 while (isspace((uschar)*s)) s++;
234 /* Reset dynamic store, if we need to, and revert to the search pool */
238 reset_point = store_reset(reset_point);
239 store_pool = old_pool;
242 /* Now we want to build the result string to contain the data. There can be
243 two kinds of continuation: (a) the physical line may not all have fitted into
244 the buffer, and (b) there may be logical continuation lines, for which we
245 must convert all leading white space into a single blank.
247 Initialize, and copy the first segment of data. */
249 this_is_comment = FALSE;
250 yield = string_get(100);
252 yield = string_cat(yield, s);
254 /* Now handle continuations */
256 for (last_was_eol = this_is_eol;
257 Ufgets(buffer, sizeof(buffer), f) != NULL;
258 last_was_eol = this_is_eol)
262 this_is_eol = p > 0 && buffer[p-1] == '\n';
264 /* Remove trailing white space from a physical line end */
268 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
272 /* If this is not a physical line continuation, skip it entirely if it's
273 empty or starts with #. Otherwise, break the loop if it doesn't start with
274 white space. Otherwise, replace leading white space with a single blank. */
278 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
279 if (this_is_comment) continue;
280 if (!isspace((uschar)buffer[0])) break;
281 while (isspace((uschar)*s)) s++;
284 if (this_is_comment) continue;
286 /* Join a physical or logical line continuation onto the result string. */
288 yield = string_cat(yield, s);
291 gstring_release_unused(yield);
292 *result = string_from_gstring(yield);
296 /* Reset dynamic store, if we need to */
300 store_reset(reset_point);
301 store_pool = old_pool;
308 /*************************************************
309 * Find entry point for lsearch *
310 *************************************************/
312 /* See local README for interface description */
315 lsearch_find(void * handle, const uschar * filename, const uschar * keystring,
316 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
319 return internal_lsearch_find(handle, filename, keystring, length, result,
320 errmsg, LSEARCH_PLAIN);
325 /*************************************************
326 * Find entry point for wildlsearch *
327 *************************************************/
329 /* See local README for interface description */
332 wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
333 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
336 return internal_lsearch_find(handle, filename, keystring, length, result,
337 errmsg, LSEARCH_WILD);
342 /*************************************************
343 * Find entry point for nwildlsearch *
344 *************************************************/
346 /* See local README for interface description */
349 nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
350 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
353 return internal_lsearch_find(handle, filename, keystring, length, result,
354 errmsg, LSEARCH_NWILD);
360 /*************************************************
361 * Find entry point for iplsearch *
362 *************************************************/
364 /* See local README for interface description */
367 iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
368 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
371 if ((length == 1 && keystring[0] == '*') ||
372 string_is_ip_address(keystring, NULL) != 0)
373 return internal_lsearch_find(handle, filename, keystring, length, result,
376 *errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
377 "address, with optional CIDR mask, is wanted): "
378 "in a host list, use net-iplsearch as the search type", keystring);
385 /*************************************************
386 * Close entry point *
387 *************************************************/
389 /* See local README for interface description */
392 lsearch_close(void *handle)
394 (void)fclose((FILE *)handle);
399 /*************************************************
400 * Version reporting entry point *
401 *************************************************/
403 /* See local README for interface description. */
405 #include "../version.h"
408 lsearch_version_report(FILE *f)
411 fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
416 static lookup_info iplsearch_lookup_info = {
417 .name = US"iplsearch", /* lookup name */
418 .type = lookup_absfile, /* uses absolute file name */
419 .open = lsearch_open, /* open function */
420 .check = lsearch_check, /* check function */
421 .find = iplsearch_find, /* find function */
422 .close = lsearch_close, /* close function */
423 .tidy = NULL, /* no tidy function */
424 .quote = NULL, /* no quoting function */
425 .version_report = NULL /* no version reporting (redundant) */
428 static lookup_info lsearch_lookup_info = {
429 .name = US"lsearch", /* lookup name */
430 .type = lookup_absfile, /* uses absolute file name */
431 .open = lsearch_open, /* open function */
432 .check = lsearch_check, /* check function */
433 .find = lsearch_find, /* find function */
434 .close = lsearch_close, /* close function */
435 .tidy = NULL, /* no tidy function */
436 .quote = NULL, /* no quoting function */
437 .version_report = lsearch_version_report /* version reporting */
440 static lookup_info nwildlsearch_lookup_info = {
441 .name = US"nwildlsearch", /* lookup name */
442 .type = lookup_absfile, /* uses absolute file name */
443 .open = lsearch_open, /* open function */
444 .check = lsearch_check, /* check function */
445 .find = nwildlsearch_find, /* find function */
446 .close = lsearch_close, /* close function */
447 .tidy = NULL, /* no tidy function */
448 .quote = NULL, /* no quoting function */
449 .version_report = NULL /* no version reporting (redundant) */
452 static lookup_info wildlsearch_lookup_info = {
453 .name = US"wildlsearch", /* lookup name */
454 .type = lookup_absfile, /* uses absolute file name */
455 .open = lsearch_open, /* open function */
456 .check = lsearch_check, /* check function */
457 .find = wildlsearch_find, /* find function */
458 .close = lsearch_close, /* close function */
459 .tidy = NULL, /* no tidy function */
460 .quote = NULL, /* no quoting function */
461 .version_report = NULL /* no version reporting (redundant) */
465 #define lsearch_lookup_module_info _lookup_module_info
468 static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
469 &lsearch_lookup_info,
470 &nwildlsearch_lookup_info,
471 &wildlsearch_lookup_info };
472 lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
474 /* End of lookups/lsearch.c */