1 /* $Cambridge: exim/src/src/lookups/lsearch.c,v 1.11 2009/11/16 19:50:38 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
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(uschar *filename, uschar **errmsg)
33 FILE *f = Ufopen(filename, "rb");
36 int save_errno = errno;
37 *errmsg = string_open_failed(errno, "%s for linear search", filename);
46 /*************************************************
48 *************************************************/
51 lsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
52 gid_t *owngroups, uschar **errmsg)
54 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
55 owners, owngroups, "lsearch", errmsg) == 0;
60 /*************************************************
61 * Internal function for the various lsearches *
62 *************************************************/
64 /* See local README for interface description, plus:
68 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
71 There is some messy logic in here to cope with very long data lines that do not
72 fit into the fixed sized buffer. Most of the time this will never be exercised,
73 but people do occasionally do weird things. */
76 internal_lsearch_find(void *handle, uschar *filename, uschar *keystring,
77 int length, uschar **result, uschar **errmsg, int type)
79 FILE *f = (FILE *)handle;
80 BOOL last_was_eol = TRUE;
81 BOOL this_is_eol = TRUE;
82 int old_pool = store_pool;
83 void *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_get(0);
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)
107 int p = Ustrlen(buffer);
109 BOOL this_is_comment;
113 /* Check whether this the final segment of a line. If it follows an
114 incomplete part-line, skip it. */
116 this_is_eol = p > 0 && buffer[p-1] == '\n';
117 if (!last_was_eol) continue;
119 /* We now have the start of a physical line. If this is a final line segment,
120 remove trailing white space. */
124 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
128 /* If the buffer is empty it might be (a) a complete empty line, or (b) the
129 start of a line that begins with so much white space that it doesn't all fit
130 in the buffer. In both cases we want to skip the entire physical line.
132 If the buffer begins with # it is a comment line; if it begins with white
133 space it is a logical continuation; again, we want to skip the entire
136 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
138 /* We assume that they key will fit in the buffer. If the key starts with ",
139 read it as a quoted string. We don't use string_dequote() because that uses
140 new store for the result, and we may be doing this many times in a long file.
141 We know that the dequoted string must be shorter than the original, because
142 we are removing the quotes, and also any escape sequences always turn two or
143 more characters into one character. Therefore, we can store the new string in
149 while (*s != 0 && *s != '\"')
151 if (*s == '\\') *t++ = string_interpret_escape(&s);
155 if (*s != 0) s++; /* Past terminating " */
156 linekeylength = t - buffer;
159 /* Otherwise it is terminated by a colon or white space */
163 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
164 linekeylength = s - buffer;
167 /* The matching test depends on which kind of lsearch we are doing */
171 /* A plain lsearch treats each key as a literal */
174 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
176 break; /* Key matched */
178 /* A wild lsearch treats each key as a possible wildcarded string; no
179 expansion is done for nwildlsearch. */
185 int save = buffer[linekeylength];
186 uschar *list = buffer;
187 buffer[linekeylength] = 0;
188 rc = match_isinlist(keystring,
190 UCHAR_MAX+1, /* Single-item list */
191 NULL, /* No anchor */
192 NULL, /* No caching */
193 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
196 buffer[linekeylength] = save;
197 if (rc == FAIL) continue;
198 if (rc == DEFER) return DEFER;
201 /* The key has matched. If the search involved a regular expression, it
202 might have caused numerical variables to be set. However, their values will
203 be in the wrong storage pool for external use. Copying them to the standard
204 pool is not feasible because of the caching of lookup results - a repeated
205 lookup will not match the regular expression again. Therefore, we flatten
206 all numeric variables at this point. */
211 /* Compare an ip address against a list of network/ip addresses. We have to
212 allow for the "*" case specially. */
215 if (linekeylength == 1 && buffer[0] == '*')
217 if (length != 1 || keystring[0] != '*') continue;
219 else if (length == 1 && keystring[0] == '*') continue;
223 int save = buffer[linekeylength];
224 buffer[linekeylength] = 0;
225 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
226 !host_is_in_net(keystring, buffer, maskoffset)) continue;
227 buffer[linekeylength] = save;
229 break; /* Key matched */
232 /* The key has matched. Skip spaces after the key, and allow an optional
233 colon after the spaces. This is an odd specification, but it's for
236 while (isspace((uschar)*s)) s++;
240 while (isspace((uschar)*s)) s++;
243 /* Reset dynamic store, if we need to, and revert to the search pool */
245 if (reset_point != NULL)
247 store_reset(reset_point);
248 store_pool = old_pool;
251 /* Now we want to build the result string to contain the data. There can be
252 two kinds of continuation: (a) the physical line may not all have fitted into
253 the buffer, and (b) there may be logical continuation lines, for which we
254 must convert all leading white space into a single blank.
256 Initialize, and copy the first segment of data. */
258 this_is_comment = FALSE;
261 yield = store_get(size);
263 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
265 /* Now handle continuations */
267 for (last_was_eol = this_is_eol;
268 Ufgets(buffer, sizeof(buffer), f) != NULL;
269 last_was_eol = this_is_eol)
273 this_is_eol = p > 0 && buffer[p-1] == '\n';
275 /* Remove trailing white space from a physical line end */
279 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
283 /* If this is not a physical line continuation, skip it entirely if it's
284 empty or starts with #. Otherwise, break the loop if it doesn't start with
285 white space. Otherwise, replace leading white space with a single blank. */
289 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
290 if (this_is_comment) continue;
291 if (!isspace((uschar)buffer[0])) break;
292 while (isspace((uschar)*s)) s++;
295 if (this_is_comment) continue;
297 /* Join a physical or logical line continuation onto the result string. */
299 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
303 store_reset(yield + ptr + 1);
308 /* Reset dynamic store, if we need to */
310 if (reset_point != NULL)
312 store_reset(reset_point);
313 store_pool = old_pool;
320 /*************************************************
321 * Find entry point for lsearch *
322 *************************************************/
324 /* See local README for interface description */
327 lsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
328 uschar **result, uschar **errmsg, BOOL *do_cache)
330 do_cache = do_cache; /* Keep picky compilers happy */
331 return internal_lsearch_find(handle, filename, keystring, length, result,
332 errmsg, LSEARCH_PLAIN);
337 /*************************************************
338 * Find entry point for wildlsearch *
339 *************************************************/
341 /* See local README for interface description */
344 wildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
345 uschar **result, uschar **errmsg, BOOL *do_cache)
347 do_cache = do_cache; /* Keep picky compilers happy */
348 return internal_lsearch_find(handle, filename, keystring, length, result,
349 errmsg, LSEARCH_WILD);
354 /*************************************************
355 * Find entry point for nwildlsearch *
356 *************************************************/
358 /* See local README for interface description */
361 nwildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
362 uschar **result, uschar **errmsg, BOOL *do_cache)
364 do_cache = do_cache; /* Keep picky compilers happy */
365 return internal_lsearch_find(handle, filename, keystring, length, result,
366 errmsg, LSEARCH_NWILD);
372 /*************************************************
373 * Find entry point for iplsearch *
374 *************************************************/
376 /* See local README for interface description */
379 iplsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
380 uschar **result, uschar **errmsg, BOOL *do_cache)
382 do_cache = do_cache; /* Keep picky compilers happy */
383 if ((length == 1 && keystring[0] == '*') ||
384 string_is_ip_address(keystring, NULL) != 0)
386 return internal_lsearch_find(handle, filename, keystring, length, result,
391 *errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
392 "address, with optional CIDR mask, is wanted): "
393 "in a host list, use net-iplsearch as the search type", keystring);
401 /*************************************************
402 * Close entry point *
403 *************************************************/
405 /* See local README for interface description */
408 lsearch_close(void *handle)
410 (void)fclose((FILE *)handle);
415 /*************************************************
416 * Version reporting entry point *
417 *************************************************/
419 /* See local README for interface description. */
421 #include "../version.h"
424 lsearch_version_report(FILE *f)
427 fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
432 static lookup_info iplsearch_lookup_info = {
433 US"iplsearch", /* lookup name */
434 lookup_absfile, /* uses absolute file name */
435 lsearch_open, /* open function */
436 lsearch_check, /* check function */
437 iplsearch_find, /* find function */
438 lsearch_close, /* close function */
439 NULL, /* no tidy function */
440 NULL, /* no quoting function */
441 NULL /* no version reporting (redundant) */
444 static lookup_info lsearch_lookup_info = {
445 US"lsearch", /* lookup name */
446 lookup_absfile, /* uses absolute file name */
447 lsearch_open, /* open function */
448 lsearch_check, /* check function */
449 lsearch_find, /* find function */
450 lsearch_close, /* close function */
451 NULL, /* no tidy function */
452 NULL, /* no quoting function */
453 lsearch_version_report /* version reporting */
456 static lookup_info nwildlsearch_lookup_info = {
457 US"nwildlsearch", /* lookup name */
458 lookup_absfile, /* uses absolute file name */
459 lsearch_open, /* open function */
460 lsearch_check, /* check function */
461 nwildlsearch_find, /* find function */
462 lsearch_close, /* close function */
463 NULL, /* no tidy function */
464 NULL, /* no quoting function */
465 NULL /* no version reporting (redundant) */
468 static lookup_info wildlsearch_lookup_info = {
469 US"wildlsearch", /* lookup name */
470 lookup_absfile, /* uses absolute file name */
471 lsearch_open, /* open function */
472 lsearch_check, /* check function */
473 wildlsearch_find, /* find function */
474 lsearch_close, /* close function */
475 NULL, /* no tidy function */
476 NULL, /* no quoting function */
477 NULL /* no version reporting (redundant) */
481 #define lsearch_lookup_module_info _lookup_module_info
484 static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
485 &lsearch_lookup_info,
486 &nwildlsearch_lookup_info,
487 &wildlsearch_lookup_info };
488 lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
490 /* End of lookups/lsearch.c */