Add dynamic lookup support
[exim.git] / src / src / lookups / lsearch.c
CommitLineData
0a49a7a4 1/* $Cambridge: exim/src/src/lookups/lsearch.c,v 1.11 2009/11/16 19:50:38 nm4 Exp $ */
0756eb3c
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
0a49a7a4 7/* Copyright (c) University of Cambridge 1995 - 2009 */
0756eb3c
PH
8/* See the file NOTICE for conditions of use and distribution. */
9
10#include "../exim.h"
11#include "lf_functions.h"
0756eb3c
PH
12
13/* Codes for the different kinds of lsearch that are supported */
14
15enum {
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 */
20};
21
22
23
24/*************************************************
25* Open entry point *
26*************************************************/
27
28/* See local README for interface description */
29
e6d225ae 30static void *
0756eb3c
PH
31lsearch_open(uschar *filename, uschar **errmsg)
32{
33FILE *f = Ufopen(filename, "rb");
34if (f == NULL)
35 {
36 int save_errno = errno;
37 *errmsg = string_open_failed(errno, "%s for linear search", filename);
38 errno = save_errno;
39 return NULL;
40 }
41return f;
42}
43
44
45
46/*************************************************
47* Check entry point *
48*************************************************/
49
e6d225ae 50static BOOL
0756eb3c
PH
51lsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners,
52 gid_t *owngroups, uschar **errmsg)
53{
54return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
55 owners, owngroups, "lsearch", errmsg) == 0;
56}
57
58
59
60/*************************************************
61* Internal function for the various lsearches *
62*************************************************/
63
64/* See local README for interface description, plus:
65
66Extra argument:
67
68 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
69 LSEARCH_IP
70
71There is some messy logic in here to cope with very long data lines that do not
72fit into the fixed sized buffer. Most of the time this will never be exercised,
73but people do occasionally do weird things. */
74
75static int
76internal_lsearch_find(void *handle, uschar *filename, uschar *keystring,
77 int length, uschar **result, uschar **errmsg, int type)
78{
79FILE *f = (FILE *)handle;
80BOOL last_was_eol = TRUE;
81BOOL this_is_eol = TRUE;
82int old_pool = store_pool;
83void *reset_point = NULL;
84uschar buffer[4096];
85
86/* Wildcard searches may use up some store, because of expansions. We don't
87want them to fill up our search store. What we do is set the pool to the main
88pool and get a point to reset to later. Wildcard searches could also issue
89lookups, but internal_search_find will take care of that, and the cache will be
90safely stored in the search pool again. */
91
92if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
93 {
94 store_pool = POOL_MAIN;
95 reset_point = store_get(0);
96 }
97
98filename = filename; /* Keep picky compilers happy */
99errmsg = errmsg;
100
101rewind(f);
102for (last_was_eol = TRUE;
103 Ufgets(buffer, sizeof(buffer), f) != NULL;
104 last_was_eol = this_is_eol)
105 {
106 int ptr, size;
107 int p = Ustrlen(buffer);
108 int linekeylength;
dbb0bf41 109 BOOL this_is_comment;
0756eb3c
PH
110 uschar *yield;
111 uschar *s = buffer;
112
113 /* Check whether this the final segment of a line. If it follows an
114 incomplete part-line, skip it. */
115
116 this_is_eol = p > 0 && buffer[p-1] == '\n';
117 if (!last_was_eol) continue;
118
119 /* We now have the start of a physical line. If this is a final line segment,
120 remove trailing white space. */
121
122 if (this_is_eol)
123 {
124 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
125 buffer[p] = 0;
126 }
127
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.
131
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
134 physical line. */
135
136 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
137
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
144 the same buffer. */
145
146 if (*s == '\"')
147 {
148 uschar *t = s++;
149 while (*s != 0 && *s != '\"')
150 {
151 if (*s == '\\') *t++ = string_interpret_escape(&s);
152 else *t++ = *s;
153 s++;
154 }
155 if (*s != 0) s++; /* Past terminating " */
156 linekeylength = t - buffer;
157 }
158
159 /* Otherwise it is terminated by a colon or white space */
160
161 else
162 {
163 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
164 linekeylength = s - buffer;
165 }
166
167 /* The matching test depends on which kind of lsearch we are doing */
168
169 switch(type)
170 {
171 /* A plain lsearch treats each key as a literal */
172
173 case LSEARCH_PLAIN:
174 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
175 continue;
176 break; /* Key matched */
177
178 /* A wild lsearch treats each key as a possible wildcarded string; no
179 expansion is done for nwildlsearch. */
180
181 case LSEARCH_WILD:
182 case LSEARCH_NWILD:
183 {
184 int rc;
185 int save = buffer[linekeylength];
186 uschar *list = buffer;
187 buffer[linekeylength] = 0;
188 rc = match_isinlist(keystring,
189 &list,
190 UCHAR_MAX+1, /* Single-item list */
191 NULL, /* No anchor */
192 NULL, /* No caching */
193 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
194 TRUE, /* Caseless */
195 NULL);
196 buffer[linekeylength] = save;
197 if (rc == FAIL) continue;
198 if (rc == DEFER) return DEFER;
199 }
eba0c039
PH
200
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. */
207
208 expand_nmax = -1;
209 break;
0756eb3c
PH
210
211 /* Compare an ip address against a list of network/ip addresses. We have to
212 allow for the "*" case specially. */
213
214 case LSEARCH_IP:
215 if (linekeylength == 1 && buffer[0] == '*')
216 {
217 if (length != 1 || keystring[0] != '*') continue;
218 }
219 else if (length == 1 && keystring[0] == '*') continue;
220 else
221 {
222 int maskoffset;
223 int save = buffer[linekeylength];
224 buffer[linekeylength] = 0;
a5a28604 225 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
0756eb3c
PH
226 !host_is_in_net(keystring, buffer, maskoffset)) continue;
227 buffer[linekeylength] = save;
228 }
229 break; /* Key matched */
230 }
231
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
234 compatibility. */
235
236 while (isspace((uschar)*s)) s++;
237 if (*s == ':')
238 {
239 s++;
240 while (isspace((uschar)*s)) s++;
241 }
242
243 /* Reset dynamic store, if we need to, and revert to the search pool */
244
245 if (reset_point != NULL)
246 {
247 store_reset(reset_point);
248 store_pool = old_pool;
249 }
250
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.
255
256 Initialize, and copy the first segment of data. */
257
dbb0bf41 258 this_is_comment = FALSE;
0756eb3c
PH
259 size = 100;
260 ptr = 0;
261 yield = store_get(size);
262 if (*s != 0)
263 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
264
265 /* Now handle continuations */
266
267 for (last_was_eol = this_is_eol;
268 Ufgets(buffer, sizeof(buffer), f) != NULL;
269 last_was_eol = this_is_eol)
270 {
271 s = buffer;
272 p = Ustrlen(buffer);
273 this_is_eol = p > 0 && buffer[p-1] == '\n';
274
275 /* Remove trailing white space from a physical line end */
276
277 if (this_is_eol)
278 {
279 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
280 buffer[p] = 0;
281 }
282
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. */
286
287 if (last_was_eol)
288 {
31ffd7bf 289 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
dbb0bf41 290 if (this_is_comment) continue;
0756eb3c
PH
291 if (!isspace((uschar)buffer[0])) break;
292 while (isspace((uschar)*s)) s++;
293 *(--s) = ' ';
294 }
dbb0bf41 295 if (this_is_comment) continue;
0756eb3c
PH
296
297 /* Join a physical or logical line continuation onto the result string. */
298
299 yield = string_cat(yield, &size, &ptr, s, Ustrlen(s));
300 }
301
302 yield[ptr] = 0;
303 store_reset(yield + ptr + 1);
304 *result = yield;
305 return OK;
306 }
307
308/* Reset dynamic store, if we need to */
309
310if (reset_point != NULL)
311 {
312 store_reset(reset_point);
313 store_pool = old_pool;
314 }
315
316return FAIL;
317}
318
319
320/*************************************************
321* Find entry point for lsearch *
322*************************************************/
323
324/* See local README for interface description */
325
e6d225ae 326static int
0756eb3c
PH
327lsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
328 uschar **result, uschar **errmsg, BOOL *do_cache)
329{
330do_cache = do_cache; /* Keep picky compilers happy */
331return internal_lsearch_find(handle, filename, keystring, length, result,
332 errmsg, LSEARCH_PLAIN);
333}
334
335
336
337/*************************************************
338* Find entry point for wildlsearch *
339*************************************************/
340
341/* See local README for interface description */
342
e6d225ae 343static int
0756eb3c
PH
344wildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
345 uschar **result, uschar **errmsg, BOOL *do_cache)
346{
347do_cache = do_cache; /* Keep picky compilers happy */
348return internal_lsearch_find(handle, filename, keystring, length, result,
349 errmsg, LSEARCH_WILD);
350}
351
352
353
354/*************************************************
355* Find entry point for nwildlsearch *
356*************************************************/
357
358/* See local README for interface description */
359
e6d225ae 360static int
0756eb3c
PH
361nwildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
362 uschar **result, uschar **errmsg, BOOL *do_cache)
363{
364do_cache = do_cache; /* Keep picky compilers happy */
365return internal_lsearch_find(handle, filename, keystring, length, result,
366 errmsg, LSEARCH_NWILD);
367}
368
369
370
371
372/*************************************************
373* Find entry point for iplsearch *
374*************************************************/
375
376/* See local README for interface description */
377
e6d225ae 378static int
0756eb3c
PH
379iplsearch_find(void *handle, uschar *filename, uschar *keystring, int length,
380 uschar **result, uschar **errmsg, BOOL *do_cache)
381{
382do_cache = do_cache; /* Keep picky compilers happy */
383if ((length == 1 && keystring[0] == '*') ||
7e66e54d 384 string_is_ip_address(keystring, NULL) != 0)
0756eb3c
PH
385 {
386 return internal_lsearch_find(handle, filename, keystring, length, result,
387 errmsg, LSEARCH_IP);
388 }
389else
390 {
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);
394 return DEFER;
395 }
396}
397
398
399
400
401/*************************************************
402* Close entry point *
403*************************************************/
404
405/* See local README for interface description */
406
e6d225ae 407static void
0756eb3c
PH
408lsearch_close(void *handle)
409{
f1e894f3 410(void)fclose((FILE *)handle);
0756eb3c
PH
411}
412
e6d225ae
DW
413static lookup_info iplsearch_lookup_info = {
414 US"iplsearch", /* lookup name */
415 lookup_absfile, /* uses absolute file name */
416 lsearch_open, /* open function */
417 lsearch_check, /* check function */
418 iplsearch_find, /* find function */
419 lsearch_close, /* close function */
420 NULL, /* no tidy function */
421 NULL /* no quoting function */
422};
423
424static lookup_info lsearch_lookup_info = {
425 US"lsearch", /* lookup name */
426 lookup_absfile, /* uses absolute file name */
427 lsearch_open, /* open function */
428 lsearch_check, /* check function */
429 lsearch_find, /* find function */
430 lsearch_close, /* close function */
431 NULL, /* no tidy function */
432 NULL /* no quoting function */
433};
434
435static lookup_info nwildlsearch_lookup_info = {
436 US"nwildlsearch", /* lookup name */
437 lookup_absfile, /* uses absolute file name */
438 lsearch_open, /* open function */
439 lsearch_check, /* check function */
440 nwildlsearch_find, /* find function */
441 lsearch_close, /* close function */
442 NULL, /* no tidy function */
443 NULL /* no quoting function */
444};
445
446static lookup_info wildlsearch_lookup_info = {
447 US"wildlsearch", /* lookup name */
448 lookup_absfile, /* uses absolute file name */
449 lsearch_open, /* open function */
450 lsearch_check, /* check function */
451 wildlsearch_find, /* find function */
452 lsearch_close, /* close function */
453 NULL, /* no tidy function */
454 NULL /* no quoting function */
455};
456
457#ifdef DYNLOOKUP
458#define lsearch_lookup_module_info _lookup_module_info
459#endif
460
461static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
462 &lsearch_lookup_info,
463 &nwildlsearch_lookup_info,
464 &wildlsearch_lookup_info };
465lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
466
0756eb3c 467/* End of lookups/lsearch.c */