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();
99 for (last_was_eol = TRUE;
100 Ufgets(buffer, sizeof(buffer), f) != NULL;
101 last_was_eol = this_is_eol)
103 int p = Ustrlen(buffer);
105 BOOL this_is_comment;
109 /* Check whether this the final segment of a line. If it follows an
110 incomplete part-line, skip it. */
112 this_is_eol = p > 0 && buffer[p-1] == '\n';
113 if (!last_was_eol) continue;
115 /* We now have the start of a physical line. If this is a final line segment,
116 remove trailing white space. */
120 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
124 /* If the buffer is empty it might be (a) a complete empty line, or (b) the
125 start of a line that begins with so much white space that it doesn't all fit
126 in the buffer. In both cases we want to skip the entire physical line.
128 If the buffer begins with # it is a comment line; if it begins with white
129 space it is a logical continuation; again, we want to skip the entire
132 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
134 /* We assume that they key will fit in the buffer. If the key starts with ",
135 read it as a quoted string. We don't use string_dequote() because that uses
136 new store for the result, and we may be doing this many times in a long file.
137 We know that the dequoted string must be shorter than the original, because
138 we are removing the quotes, and also any escape sequences always turn two or
139 more characters into one character. Therefore, we can store the new string in
145 while (*s != 0 && *s != '\"')
147 if (*s == '\\') *t++ = string_interpret_escape(CUSS &s);
151 if (*s != 0) s++; /* Past terminating " */
152 linekeylength = t - buffer;
155 /* Otherwise it is terminated by a colon or white space */
159 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
160 linekeylength = s - buffer;
163 /* The matching test depends on which kind of lsearch we are doing */
167 /* A plain lsearch treats each key as a literal */
170 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
172 break; /* Key matched */
174 /* A wild lsearch treats each key as a possible wildcarded string; no
175 expansion is done for nwildlsearch. */
181 int save = buffer[linekeylength];
182 const uschar *list = buffer;
183 buffer[linekeylength] = 0;
184 rc = match_isinlist(keystring,
186 UCHAR_MAX+1, /* Single-item list */
187 NULL, /* No anchor */
188 NULL, /* No caching */
189 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
192 buffer[linekeylength] = save;
193 if (rc == FAIL) continue;
194 if (rc == DEFER) return DEFER;
197 /* The key has matched. If the search involved a regular expression, it
198 might have caused numerical variables to be set. However, their values will
199 be in the wrong storage pool for external use. Copying them to the standard
200 pool is not feasible because of the caching of lookup results - a repeated
201 lookup will not match the regular expression again. Therefore, we flatten
202 all numeric variables at this point. */
207 /* Compare an ip address against a list of network/ip addresses. We have to
208 allow for the "*" case specially. */
211 if (linekeylength == 1 && buffer[0] == '*')
213 if (length != 1 || keystring[0] != '*') continue;
215 else if (length == 1 && keystring[0] == '*') continue;
219 int save = buffer[linekeylength];
220 buffer[linekeylength] = 0;
221 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
222 !host_is_in_net(keystring, buffer, maskoffset)) continue;
223 buffer[linekeylength] = save;
225 break; /* Key matched */
228 /* The key has matched. Skip spaces after the key, and allow an optional
229 colon after the spaces. This is an odd specification, but it's for
232 while (isspace((uschar)*s)) s++;
236 while (isspace((uschar)*s)) s++;
239 /* Reset dynamic store, if we need to, and revert to the search pool */
243 reset_point = store_reset(reset_point);
244 store_pool = old_pool;
247 /* Now we want to build the result string to contain the data. There can be
248 two kinds of continuation: (a) the physical line may not all have fitted into
249 the buffer, and (b) there may be logical continuation lines, for which we
250 must convert all leading white space into a single blank.
252 Initialize, and copy the first segment of data. */
254 this_is_comment = FALSE;
255 yield = string_get(100);
257 yield = string_cat(yield, s);
259 /* Now handle continuations */
261 for (last_was_eol = this_is_eol;
262 Ufgets(buffer, sizeof(buffer), f) != NULL;
263 last_was_eol = this_is_eol)
267 this_is_eol = p > 0 && buffer[p-1] == '\n';
269 /* Remove trailing white space from a physical line end */
273 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
277 /* If this is not a physical line continuation, skip it entirely if it's
278 empty or starts with #. Otherwise, break the loop if it doesn't start with
279 white space. Otherwise, replace leading white space with a single blank. */
283 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
284 if (this_is_comment) continue;
285 if (!isspace((uschar)buffer[0])) break;
286 while (isspace((uschar)*s)) s++;
289 if (this_is_comment) continue;
291 /* Join a physical or logical line continuation onto the result string. */
293 yield = string_cat(yield, s);
296 gstring_release_unused(yield);
297 *result = string_from_gstring(yield);
301 /* Reset dynamic store, if we need to */
305 store_reset(reset_point);
306 store_pool = old_pool;
313 /*************************************************
314 * Find entry point for lsearch *
315 *************************************************/
317 /* See local README for interface description */
320 lsearch_find(void * handle, const uschar * filename, const uschar * keystring,
321 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
324 return internal_lsearch_find(handle, filename, keystring, length, result,
325 errmsg, LSEARCH_PLAIN);
330 /*************************************************
331 * Find entry point for wildlsearch *
332 *************************************************/
334 /* See local README for interface description */
337 wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
338 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
341 return internal_lsearch_find(handle, filename, keystring, length, result,
342 errmsg, LSEARCH_WILD);
347 /*************************************************
348 * Find entry point for nwildlsearch *
349 *************************************************/
351 /* See local README for interface description */
354 nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
355 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
358 return internal_lsearch_find(handle, filename, keystring, length, result,
359 errmsg, LSEARCH_NWILD);
365 /*************************************************
366 * Find entry point for iplsearch *
367 *************************************************/
369 /* See local README for interface description */
372 iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
373 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
376 if ((length == 1 && keystring[0] == '*') ||
377 string_is_ip_address(keystring, NULL) != 0)
378 return internal_lsearch_find(handle, filename, keystring, length, result,
381 *errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
382 "address, with optional CIDR mask, is wanted): "
383 "in a host list, use net-iplsearch as the search type", keystring);
390 /*************************************************
391 * Close entry point *
392 *************************************************/
394 /* See local README for interface description */
397 lsearch_close(void *handle)
399 (void)fclose((FILE *)handle);
404 /*************************************************
405 * Version reporting entry point *
406 *************************************************/
408 /* See local README for interface description. */
410 #include "../version.h"
413 lsearch_version_report(FILE *f)
416 fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
421 static lookup_info iplsearch_lookup_info = {
422 .name = US"iplsearch", /* lookup name */
423 .type = lookup_absfile, /* uses absolute file name */
424 .open = lsearch_open, /* open function */
425 .check = lsearch_check, /* check function */
426 .find = iplsearch_find, /* find function */
427 .close = lsearch_close, /* close function */
428 .tidy = NULL, /* no tidy function */
429 .quote = NULL, /* no quoting function */
430 .version_report = NULL /* no version reporting (redundant) */
433 static lookup_info lsearch_lookup_info = {
434 .name = US"lsearch", /* lookup name */
435 .type = lookup_absfile, /* uses absolute file name */
436 .open = lsearch_open, /* open function */
437 .check = lsearch_check, /* check function */
438 .find = lsearch_find, /* find function */
439 .close = lsearch_close, /* close function */
440 .tidy = NULL, /* no tidy function */
441 .quote = NULL, /* no quoting function */
442 .version_report = lsearch_version_report /* version reporting */
445 static lookup_info nwildlsearch_lookup_info = {
446 .name = US"nwildlsearch", /* lookup name */
447 .type = lookup_absfile, /* uses absolute file name */
448 .open = lsearch_open, /* open function */
449 .check = lsearch_check, /* check function */
450 .find = nwildlsearch_find, /* find function */
451 .close = lsearch_close, /* close function */
452 .tidy = NULL, /* no tidy function */
453 .quote = NULL, /* no quoting function */
454 .version_report = NULL /* no version reporting (redundant) */
457 static lookup_info wildlsearch_lookup_info = {
458 .name = US"wildlsearch", /* lookup name */
459 .type = lookup_absfile, /* uses absolute file name */
460 .open = lsearch_open, /* open function */
461 .check = lsearch_check, /* check function */
462 .find = wildlsearch_find, /* find function */
463 .close = lsearch_close, /* close function */
464 .tidy = NULL, /* no tidy function */
465 .quote = NULL, /* no quoting function */
466 .version_report = NULL /* no version reporting (redundant) */
470 #define lsearch_lookup_module_info _lookup_module_info
473 static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
474 &lsearch_lookup_info,
475 &nwildlsearch_lookup_info,
476 &wildlsearch_lookup_info };
477 lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
479 /* End of lookups/lsearch.c */