1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
9 #include "lf_functions.h"
11 /* Codes for the different kinds of lsearch that are supported */
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 */
22 /*************************************************
24 *************************************************/
26 /* See local README for interface description */
29 lsearch_open(const uschar * filename, uschar ** errmsg)
31 FILE *f = Ufopen(filename, "rb");
34 int save_errno = errno;
35 *errmsg = string_open_failed(errno, "%s for linear search", filename);
44 /*************************************************
46 *************************************************/
49 lsearch_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
50 gid_t *owngroups, uschar **errmsg)
52 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
53 owners, owngroups, "lsearch", errmsg) == 0;
58 /*************************************************
59 * Internal function for the various lsearches *
60 *************************************************/
62 /* See local README for interface description, plus:
66 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
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. */
74 internal_lsearch_find(void * handle, const uschar * filename,
75 const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
78 FILE *f = (FILE *)handle;
79 BOOL last_was_eol = TRUE;
80 BOOL this_is_eol = TRUE;
81 int old_pool = store_pool;
82 rmark reset_point = NULL;
85 /* Wildcard searches may use up some store, because of expansions. We don't
86 want them to fill up our search store. What we do is set the pool to the main
87 pool and get a point to reset to later. Wildcard searches could also issue
88 lookups, but internal_search_find will take care of that, and the cache will be
89 safely stored in the search pool again. */
91 if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
93 store_pool = POOL_MAIN;
94 reset_point = store_mark();
97 filename = filename; /* Keep picky compilers happy */
101 for (last_was_eol = TRUE;
102 Ufgets(buffer, sizeof(buffer), f) != NULL;
103 last_was_eol = this_is_eol)
105 int p = Ustrlen(buffer);
107 BOOL this_is_comment;
111 /* Check whether this the final segment of a line. If it follows an
112 incomplete part-line, skip it. */
114 this_is_eol = p > 0 && buffer[p-1] == '\n';
115 if (!last_was_eol) continue;
117 /* We now have the start of a physical line. If this is a final line segment,
118 remove trailing white space. */
122 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
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.
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
134 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
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
147 while (*s != 0 && *s != '\"')
149 if (*s == '\\') *t++ = string_interpret_escape(CUSS &s);
153 if (*s != 0) s++; /* Past terminating " */
154 linekeylength = t - buffer;
157 /* Otherwise it is terminated by a colon or white space */
161 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
162 linekeylength = s - buffer;
165 /* The matching test depends on which kind of lsearch we are doing */
169 /* A plain lsearch treats each key as a literal */
172 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
174 break; /* Key matched */
176 /* A wild lsearch treats each key as a possible wildcarded string; no
177 expansion is done for nwildlsearch. */
183 int save = buffer[linekeylength];
184 const uschar *list = buffer;
185 buffer[linekeylength] = 0;
186 rc = match_isinlist(keystring,
188 UCHAR_MAX+1, /* Single-item list */
189 NULL, /* No anchor */
190 NULL, /* No caching */
191 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
194 buffer[linekeylength] = save;
195 if (rc == FAIL) continue;
196 if (rc == DEFER) return DEFER;
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. */
209 /* Compare an ip address against a list of network/ip addresses. We have to
210 allow for the "*" case specially. */
213 if (linekeylength == 1 && buffer[0] == '*')
215 if (length != 1 || keystring[0] != '*') continue;
217 else if (length == 1 && keystring[0] == '*') continue;
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;
227 break; /* Key matched */
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
234 while (isspace((uschar)*s)) s++;
238 while (isspace((uschar)*s)) s++;
241 /* Reset dynamic store, if we need to, and revert to the search pool */
245 reset_point = store_reset(reset_point);
246 store_pool = old_pool;
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.
254 Initialize, and copy the first segment of data. */
256 this_is_comment = FALSE;
257 yield = string_get(100);
259 yield = string_cat(yield, s);
261 /* Now handle continuations */
263 for (last_was_eol = this_is_eol;
264 Ufgets(buffer, sizeof(buffer), f) != NULL;
265 last_was_eol = this_is_eol)
269 this_is_eol = p > 0 && buffer[p-1] == '\n';
271 /* Remove trailing white space from a physical line end */
275 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
279 /* If this is not a physical line continuation, skip it entirely if it's
280 empty or starts with #. Otherwise, break the loop if it doesn't start with
281 white space. Otherwise, replace leading white space with a single blank. */
285 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
286 if (this_is_comment) continue;
287 if (!isspace((uschar)buffer[0])) break;
288 while (isspace((uschar)*s)) s++;
291 if (this_is_comment) continue;
293 /* Join a physical or logical line continuation onto the result string. */
295 yield = string_cat(yield, s);
298 gstring_release_unused(yield);
299 *result = string_from_gstring(yield);
303 /* Reset dynamic store, if we need to */
307 store_reset(reset_point);
308 store_pool = old_pool;
315 /*************************************************
316 * Find entry point for lsearch *
317 *************************************************/
319 /* See local README for interface description */
322 lsearch_find(void * handle, const uschar * filename, const uschar * keystring,
323 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
326 do_cache = do_cache; /* Keep picky compilers happy */
327 return internal_lsearch_find(handle, filename, keystring, length, result,
328 errmsg, LSEARCH_PLAIN);
333 /*************************************************
334 * Find entry point for wildlsearch *
335 *************************************************/
337 /* See local README for interface description */
340 wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
341 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
344 do_cache = do_cache; /* Keep picky compilers happy */
345 return internal_lsearch_find(handle, filename, keystring, length, result,
346 errmsg, LSEARCH_WILD);
351 /*************************************************
352 * Find entry point for nwildlsearch *
353 *************************************************/
355 /* See local README for interface description */
358 nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
359 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
362 do_cache = do_cache; /* Keep picky compilers happy */
363 return internal_lsearch_find(handle, filename, keystring, length, result,
364 errmsg, LSEARCH_NWILD);
370 /*************************************************
371 * Find entry point for iplsearch *
372 *************************************************/
374 /* See local README for interface description */
377 iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
378 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
381 do_cache = do_cache; /* Keep picky compilers happy */
383 if ((length == 1 && keystring[0] == '*') ||
384 string_is_ip_address(keystring, NULL) != 0)
385 return internal_lsearch_find(handle, filename, keystring, length, result,
388 *errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
389 "address, with optional CIDR mask, is wanted): "
390 "in a host list, use net-iplsearch as the search type", keystring);
397 /*************************************************
398 * Close entry point *
399 *************************************************/
401 /* See local README for interface description */
404 lsearch_close(void *handle)
406 (void)fclose((FILE *)handle);
411 /*************************************************
412 * Version reporting entry point *
413 *************************************************/
415 /* See local README for interface description. */
417 #include "../version.h"
420 lsearch_version_report(FILE *f)
423 fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
428 static lookup_info iplsearch_lookup_info = {
429 .name = US"iplsearch", /* lookup name */
430 .type = lookup_absfile, /* uses absolute file name */
431 .open = lsearch_open, /* open function */
432 .check = lsearch_check, /* check function */
433 .find = iplsearch_find, /* find function */
434 .close = lsearch_close, /* close function */
435 .tidy = NULL, /* no tidy function */
436 .quote = NULL, /* no quoting function */
437 .version_report = NULL /* no version reporting (redundant) */
440 static lookup_info lsearch_lookup_info = {
441 .name = US"lsearch", /* lookup name */
442 .type = lookup_absfile, /* uses absolute file name */
443 .open = lsearch_open, /* open function */
444 .check = lsearch_check, /* check function */
445 .find = lsearch_find, /* find function */
446 .close = lsearch_close, /* close function */
447 .tidy = NULL, /* no tidy function */
448 .quote = NULL, /* no quoting function */
449 .version_report = lsearch_version_report /* version reporting */
452 static lookup_info nwildlsearch_lookup_info = {
453 .name = US"nwildlsearch", /* lookup name */
454 .type = lookup_absfile, /* uses absolute file name */
455 .open = lsearch_open, /* open function */
456 .check = lsearch_check, /* check function */
457 .find = nwildlsearch_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) */
464 static lookup_info wildlsearch_lookup_info = {
465 .name = US"wildlsearch", /* lookup name */
466 .type = lookup_absfile, /* uses absolute file name */
467 .open = lsearch_open, /* open function */
468 .check = lsearch_check, /* check function */
469 .find = wildlsearch_find, /* find function */
470 .close = lsearch_close, /* close function */
471 .tidy = NULL, /* no tidy function */
472 .quote = NULL, /* no quoting function */
473 .version_report = NULL /* no version reporting (redundant) */
477 #define lsearch_lookup_module_info _lookup_module_info
480 static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
481 &lsearch_lookup_info,
482 &nwildlsearch_lookup_info,
483 &wildlsearch_lookup_info };
484 lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
486 /* End of lookups/lsearch.c */