1 /* $Cambridge: exim/src/src/lookups/lsearch.c,v 1.10 2009/10/15 07:23:56 tom Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2007 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 #include "lf_functions.h"
14 /* Codes for the different kinds of lsearch that are supported */
17 LSEARCH_PLAIN, /* Literal keys */
18 LSEARCH_WILD, /* Wild card keys, expanded */
19 LSEARCH_NWILD, /* Wild card keys, not expanded */
20 LSEARCH_IP /* IP addresses and networks */
25 /*************************************************
27 *************************************************/
29 /* See local README for interface description */
32 lsearch_open(uschar *filename, uschar **errmsg)
34 FILE *f = Ufopen(filename, "rb");
37 int save_errno = errno;
38 *errmsg = string_open_failed(errno, "%s for linear search", filename);
47 /*************************************************
49 *************************************************/
52 lsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
53 gid_t *owngroups, uschar **errmsg)
55 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
56 owners, owngroups, "lsearch", errmsg) == 0;
61 /*************************************************
62 * Internal function for the various lsearches *
63 *************************************************/
65 /* See local README for interface description, plus:
69 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
72 There is some messy logic in here to cope with very long data lines that do not
73 fit into the fixed sized buffer. Most of the time this will never be exercised,
74 but people do occasionally do weird things. */
77 internal_lsearch_find(void *handle, uschar *filename, uschar *keystring,
78 int length, uschar **result, uschar **errmsg, int type)
80 FILE *f = (FILE *)handle;
81 BOOL last_was_eol = TRUE;
82 BOOL this_is_eol = TRUE;
83 int old_pool = store_pool;
84 void *reset_point = NULL;
87 /* Wildcard searches may use up some store, because of expansions. We don't
88 want them to fill up our search store. What we do is set the pool to the main
89 pool and get a point to reset to later. Wildcard searches could also issue
90 lookups, but internal_search_find will take care of that, and the cache will be
91 safely stored in the search pool again. */
93 if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
95 store_pool = POOL_MAIN;
96 reset_point = store_get(0);
99 filename = filename; /* Keep picky compilers happy */
103 for (last_was_eol = TRUE;
104 Ufgets(buffer, sizeof(buffer), f) != NULL;
105 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 != 0 && *s != '\"')
152 if (*s == '\\') *t++ = string_interpret_escape(&s);
156 if (*s != 0) s++; /* Past terminating " */
157 linekeylength = t - buffer;
160 /* Otherwise it is terminated by a colon or white space */
164 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
165 linekeylength = s - buffer;
168 /* The matching test depends on which kind of lsearch we are doing */
172 /* A plain lsearch treats each key as a literal */
175 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
177 break; /* Key matched */
179 /* A wild lsearch treats each key as a possible wildcarded string; no
180 expansion is done for nwildlsearch. */
186 int save = buffer[linekeylength];
187 uschar *list = buffer;
188 buffer[linekeylength] = 0;
189 rc = match_isinlist(keystring,
191 UCHAR_MAX+1, /* Single-item list */
192 NULL, /* No anchor */
193 NULL, /* No caching */
194 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
197 buffer[linekeylength] = save;
198 if (rc == FAIL) continue;
199 if (rc == DEFER) return DEFER;
202 /* The key has matched. If the search involved a regular expression, it
203 might have caused numerical variables to be set. However, their values will
204 be in the wrong storage pool for external use. Copying them to the standard
205 pool is not feasible because of the caching of lookup results - a repeated
206 lookup will not match the regular expression again. Therefore, we flatten
207 all numeric variables at this point. */
212 /* Compare an ip address against a list of network/ip addresses. We have to
213 allow for the "*" case specially. */
216 if (linekeylength == 1 && buffer[0] == '*')
218 if (length != 1 || keystring[0] != '*') continue;
220 else if (length == 1 && keystring[0] == '*') continue;
224 int save = buffer[linekeylength];
225 buffer[linekeylength] = 0;
226 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
227 !host_is_in_net(keystring, buffer, maskoffset)) continue;
228 buffer[linekeylength] = save;
230 break; /* Key matched */
233 /* The key has matched. Skip spaces after the key, and allow an optional
234 colon after the spaces. This is an odd specification, but it's for
237 while (isspace((uschar)*s)) s++;
241 while (isspace((uschar)*s)) s++;
244 /* Reset dynamic store, if we need to, and revert to the search pool */
246 if (reset_point != NULL)
248 store_reset(reset_point);
249 store_pool = old_pool;
252 /* Now we want to build the result string to contain the data. There can be
253 two kinds of continuation: (a) the physical line may not all have fitted into
254 the buffer, and (b) there may be logical continuation lines, for which we
255 must convert all leading white space into a single blank.
257 Initialize, and copy the first segment of data. */
259 this_is_comment = FALSE;
262 yield = store_get(size);
264 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
266 /* Now handle continuations */
268 for (last_was_eol = this_is_eol;
269 Ufgets(buffer, sizeof(buffer), f) != NULL;
270 last_was_eol = this_is_eol)
274 this_is_eol = p > 0 && buffer[p-1] == '\n';
276 /* Remove trailing white space from a physical line end */
280 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
284 /* If this is not a physical line continuation, skip it entirely if it's
285 empty or starts with #. Otherwise, break the loop if it doesn't start with
286 white space. Otherwise, replace leading white space with a single blank. */
290 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
291 if (this_is_comment) continue;
292 if (!isspace((uschar)buffer[0])) break;
293 while (isspace((uschar)*s)) s++;
296 if (this_is_comment) continue;
298 /* Join a physical or logical line continuation onto the result string. */
300 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
304 store_reset(yield + ptr + 1);
309 /* Reset dynamic store, if we need to */
311 if (reset_point != NULL)
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, uschar *filename, uschar *keystring, int length,
329 uschar **result, uschar **errmsg, BOOL *do_cache)
331 do_cache = do_cache; /* Keep picky compilers happy */
332 return internal_lsearch_find(handle, filename, keystring, length, result,
333 errmsg, LSEARCH_PLAIN);
338 /*************************************************
339 * Find entry point for wildlsearch *
340 *************************************************/
342 /* See local README for interface description */
345 wildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
346 uschar **result, uschar **errmsg, BOOL *do_cache)
348 do_cache = do_cache; /* Keep picky compilers happy */
349 return internal_lsearch_find(handle, filename, keystring, length, result,
350 errmsg, LSEARCH_WILD);
355 /*************************************************
356 * Find entry point for nwildlsearch *
357 *************************************************/
359 /* See local README for interface description */
362 nwildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
363 uschar **result, uschar **errmsg, BOOL *do_cache)
365 do_cache = do_cache; /* Keep picky compilers happy */
366 return internal_lsearch_find(handle, filename, keystring, length, result,
367 errmsg, LSEARCH_NWILD);
373 /*************************************************
374 * Find entry point for iplsearch *
375 *************************************************/
377 /* See local README for interface description */
380 iplsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
381 uschar **result, uschar **errmsg, BOOL *do_cache)
383 do_cache = do_cache; /* Keep picky compilers happy */
384 if ((length == 1 && keystring[0] == '*') ||
385 string_is_ip_address(keystring, NULL) != 0)
387 return internal_lsearch_find(handle, filename, keystring, length, result,
392 *errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
393 "address, with optional CIDR mask, is wanted): "
394 "in a host list, use net-iplsearch as the search type", keystring);
402 /*************************************************
403 * Close entry point *
404 *************************************************/
406 /* See local README for interface description */
409 lsearch_close(void *handle)
411 (void)fclose((FILE *)handle);
414 /* End of lookups/lsearch.c */