debian experimental exim-daemon-heavy config
[exim.git] / src / src / lookups / lsearch.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
f9ba5e22 5/* Copyright (c) University of Cambridge 1995 - 2018 */
1e1ddfac 6/* Copyright (c) The Exim Maintainers 2020 */
0756eb3c
PH
7/* See the file NOTICE for conditions of use and distribution. */
8
9#include "../exim.h"
10#include "lf_functions.h"
0756eb3c
PH
11
12/* Codes for the different kinds of lsearch that are supported */
13
14enum {
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 */
19};
20
21
22
23/*************************************************
24* Open entry point *
25*************************************************/
26
27/* See local README for interface description */
28
e6d225ae 29static void *
d447dbd1 30lsearch_open(const uschar * filename, uschar ** errmsg)
0756eb3c
PH
31{
32FILE *f = Ufopen(filename, "rb");
33if (f == NULL)
34 {
35 int save_errno = errno;
36 *errmsg = string_open_failed(errno, "%s for linear search", filename);
37 errno = save_errno;
38 return NULL;
39 }
40return f;
41}
42
43
44
45/*************************************************
46* Check entry point *
47*************************************************/
48
e6d225ae 49static BOOL
d447dbd1 50lsearch_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
0756eb3c
PH
51 gid_t *owngroups, uschar **errmsg)
52{
53return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
54 owners, owngroups, "lsearch", errmsg) == 0;
55}
56
57
58
59/*************************************************
60* Internal function for the various lsearches *
61*************************************************/
62
63/* See local README for interface description, plus:
64
65Extra argument:
66
67 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
68 LSEARCH_IP
69
70There is some messy logic in here to cope with very long data lines that do not
71fit into the fixed sized buffer. Most of the time this will never be exercised,
72but people do occasionally do weird things. */
73
74static int
d447dbd1
JH
75internal_lsearch_find(void * handle, const uschar * filename,
76 const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
77 int type)
0756eb3c
PH
78{
79FILE *f = (FILE *)handle;
80BOOL last_was_eol = TRUE;
81BOOL this_is_eol = TRUE;
82int old_pool = store_pool;
f3ebb786 83rmark reset_point = NULL;
0756eb3c
PH
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;
f3ebb786 95 reset_point = store_mark();
0756eb3c
PH
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 {
0756eb3c
PH
106 int p = Ustrlen(buffer);
107 int linekeylength;
dbb0bf41 108 BOOL this_is_comment;
acec9514 109 gstring * yield;
0756eb3c
PH
110 uschar *s = buffer;
111
112 /* Check whether this the final segment of a line. If it follows an
113 incomplete part-line, skip it. */
114
115 this_is_eol = p > 0 && buffer[p-1] == '\n';
116 if (!last_was_eol) continue;
117
118 /* We now have the start of a physical line. If this is a final line segment,
119 remove trailing white space. */
120
121 if (this_is_eol)
122 {
123 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
124 buffer[p] = 0;
125 }
126
127 /* If the buffer is empty it might be (a) a complete empty line, or (b) the
128 start of a line that begins with so much white space that it doesn't all fit
129 in the buffer. In both cases we want to skip the entire physical line.
130
131 If the buffer begins with # it is a comment line; if it begins with white
132 space it is a logical continuation; again, we want to skip the entire
133 physical line. */
134
135 if (buffer[0] == 0 || buffer[0] == '#' || isspace(buffer[0])) continue;
136
137 /* We assume that they key will fit in the buffer. If the key starts with ",
138 read it as a quoted string. We don't use string_dequote() because that uses
139 new store for the result, and we may be doing this many times in a long file.
140 We know that the dequoted string must be shorter than the original, because
141 we are removing the quotes, and also any escape sequences always turn two or
142 more characters into one character. Therefore, we can store the new string in
143 the same buffer. */
144
145 if (*s == '\"')
146 {
147 uschar *t = s++;
148 while (*s != 0 && *s != '\"')
149 {
55414b25 150 if (*s == '\\') *t++ = string_interpret_escape(CUSS &s);
0756eb3c
PH
151 else *t++ = *s;
152 s++;
153 }
154 if (*s != 0) s++; /* Past terminating " */
155 linekeylength = t - buffer;
156 }
157
158 /* Otherwise it is terminated by a colon or white space */
159
160 else
161 {
162 while (*s != 0 && *s != ':' && !isspace(*s)) s++;
163 linekeylength = s - buffer;
164 }
165
166 /* The matching test depends on which kind of lsearch we are doing */
167
168 switch(type)
169 {
170 /* A plain lsearch treats each key as a literal */
171
172 case LSEARCH_PLAIN:
173 if (linekeylength != length || strncmpic(buffer, keystring, length) != 0)
174 continue;
175 break; /* Key matched */
176
177 /* A wild lsearch treats each key as a possible wildcarded string; no
178 expansion is done for nwildlsearch. */
179
180 case LSEARCH_WILD:
181 case LSEARCH_NWILD:
182 {
183 int rc;
184 int save = buffer[linekeylength];
55414b25 185 const uschar *list = buffer;
0756eb3c
PH
186 buffer[linekeylength] = 0;
187 rc = match_isinlist(keystring,
188 &list,
189 UCHAR_MAX+1, /* Single-item list */
190 NULL, /* No anchor */
191 NULL, /* No caching */
192 MCL_STRING + ((type == LSEARCH_WILD)? 0:MCL_NOEXPAND),
193 TRUE, /* Caseless */
194 NULL);
195 buffer[linekeylength] = save;
196 if (rc == FAIL) continue;
197 if (rc == DEFER) return DEFER;
198 }
eba0c039
PH
199
200 /* The key has matched. If the search involved a regular expression, it
201 might have caused numerical variables to be set. However, their values will
202 be in the wrong storage pool for external use. Copying them to the standard
203 pool is not feasible because of the caching of lookup results - a repeated
204 lookup will not match the regular expression again. Therefore, we flatten
205 all numeric variables at this point. */
206
207 expand_nmax = -1;
208 break;
0756eb3c
PH
209
210 /* Compare an ip address against a list of network/ip addresses. We have to
211 allow for the "*" case specially. */
212
213 case LSEARCH_IP:
214 if (linekeylength == 1 && buffer[0] == '*')
215 {
216 if (length != 1 || keystring[0] != '*') continue;
217 }
218 else if (length == 1 && keystring[0] == '*') continue;
219 else
220 {
221 int maskoffset;
222 int save = buffer[linekeylength];
223 buffer[linekeylength] = 0;
a5a28604 224 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
0756eb3c
PH
225 !host_is_in_net(keystring, buffer, maskoffset)) continue;
226 buffer[linekeylength] = save;
227 }
228 break; /* Key matched */
229 }
230
231 /* The key has matched. Skip spaces after the key, and allow an optional
232 colon after the spaces. This is an odd specification, but it's for
233 compatibility. */
234
235 while (isspace((uschar)*s)) s++;
236 if (*s == ':')
237 {
238 s++;
239 while (isspace((uschar)*s)) s++;
240 }
241
242 /* Reset dynamic store, if we need to, and revert to the search pool */
243
acec9514 244 if (reset_point)
0756eb3c 245 {
f3ebb786 246 reset_point = store_reset(reset_point);
0756eb3c
PH
247 store_pool = old_pool;
248 }
249
250 /* Now we want to build the result string to contain the data. There can be
251 two kinds of continuation: (a) the physical line may not all have fitted into
252 the buffer, and (b) there may be logical continuation lines, for which we
253 must convert all leading white space into a single blank.
254
255 Initialize, and copy the first segment of data. */
256
dbb0bf41 257 this_is_comment = FALSE;
acec9514 258 yield = string_get(100);
0756eb3c 259 if (*s != 0)
acec9514 260 yield = string_cat(yield, s);
0756eb3c
PH
261
262 /* Now handle continuations */
263
264 for (last_was_eol = this_is_eol;
265 Ufgets(buffer, sizeof(buffer), f) != NULL;
266 last_was_eol = this_is_eol)
267 {
268 s = buffer;
269 p = Ustrlen(buffer);
270 this_is_eol = p > 0 && buffer[p-1] == '\n';
271
272 /* Remove trailing white space from a physical line end */
273
274 if (this_is_eol)
275 {
276 while (p > 0 && isspace((uschar)buffer[p-1])) p--;
277 buffer[p] = 0;
278 }
279
280 /* If this is not a physical line continuation, skip it entirely if it's
281 empty or starts with #. Otherwise, break the loop if it doesn't start with
282 white space. Otherwise, replace leading white space with a single blank. */
283
284 if (last_was_eol)
285 {
31ffd7bf 286 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
dbb0bf41 287 if (this_is_comment) continue;
0756eb3c
PH
288 if (!isspace((uschar)buffer[0])) break;
289 while (isspace((uschar)*s)) s++;
290 *(--s) = ' ';
291 }
dbb0bf41 292 if (this_is_comment) continue;
0756eb3c
PH
293
294 /* Join a physical or logical line continuation onto the result string. */
295
acec9514 296 yield = string_cat(yield, s);
0756eb3c
PH
297 }
298
f3ebb786 299 gstring_release_unused(yield);
acec9514 300 *result = string_from_gstring(yield);
0756eb3c
PH
301 return OK;
302 }
303
304/* Reset dynamic store, if we need to */
305
acec9514 306if (reset_point)
0756eb3c
PH
307 {
308 store_reset(reset_point);
309 store_pool = old_pool;
310 }
311
312return FAIL;
313}
314
315
316/*************************************************
317* Find entry point for lsearch *
318*************************************************/
319
320/* See local README for interface description */
321
e6d225ae 322static int
d447dbd1 323lsearch_find(void * handle, const uschar * filename, const uschar * keystring,
67a57a5a
JH
324 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
325 const uschar * opts)
0756eb3c
PH
326{
327do_cache = do_cache; /* Keep picky compilers happy */
328return internal_lsearch_find(handle, filename, keystring, length, result,
329 errmsg, LSEARCH_PLAIN);
330}
331
332
333
334/*************************************************
335* Find entry point for wildlsearch *
336*************************************************/
337
338/* See local README for interface description */
339
e6d225ae 340static int
d447dbd1 341wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
67a57a5a
JH
342 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
343 const uschar * opts)
0756eb3c
PH
344{
345do_cache = do_cache; /* Keep picky compilers happy */
346return internal_lsearch_find(handle, filename, keystring, length, result,
347 errmsg, LSEARCH_WILD);
348}
349
350
351
352/*************************************************
353* Find entry point for nwildlsearch *
354*************************************************/
355
356/* See local README for interface description */
357
e6d225ae 358static int
d447dbd1 359nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
67a57a5a
JH
360 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
361 const uschar * opts)
0756eb3c
PH
362{
363do_cache = do_cache; /* Keep picky compilers happy */
364return internal_lsearch_find(handle, filename, keystring, length, result,
365 errmsg, LSEARCH_NWILD);
366}
367
368
369
370
371/*************************************************
372* Find entry point for iplsearch *
373*************************************************/
374
375/* See local README for interface description */
376
e6d225ae 377static int
d447dbd1 378iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
67a57a5a
JH
379 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
380 const uschar * opts)
0756eb3c
PH
381{
382do_cache = do_cache; /* Keep picky compilers happy */
d447dbd1 383
0756eb3c 384if ((length == 1 && keystring[0] == '*') ||
7e66e54d 385 string_is_ip_address(keystring, NULL) != 0)
0756eb3c
PH
386 return internal_lsearch_find(handle, filename, keystring, length, result,
387 errmsg, LSEARCH_IP);
d447dbd1
JH
388
389*errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP "
390"address, with optional CIDR mask, is wanted): "
391"in a host list, use net-iplsearch as the search type", keystring);
392return DEFER;
0756eb3c
PH
393}
394
395
396
397
398/*************************************************
399* Close entry point *
400*************************************************/
401
402/* See local README for interface description */
403
e6d225ae 404static void
0756eb3c
PH
405lsearch_close(void *handle)
406{
f1e894f3 407(void)fclose((FILE *)handle);
0756eb3c
PH
408}
409
6545de78
PP
410
411
412/*************************************************
413* Version reporting entry point *
414*************************************************/
415
416/* See local README for interface description. */
417
418#include "../version.h"
419
420void
421lsearch_version_report(FILE *f)
422{
423#ifdef DYNLOOKUP
424fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
425#endif
426}
427
428
e6d225ae 429static lookup_info iplsearch_lookup_info = {
9f400174
JH
430 .name = US"iplsearch", /* lookup name */
431 .type = lookup_absfile, /* uses absolute file name */
432 .open = lsearch_open, /* open function */
433 .check = lsearch_check, /* check function */
434 .find = iplsearch_find, /* find function */
435 .close = lsearch_close, /* close function */
436 .tidy = NULL, /* no tidy function */
437 .quote = NULL, /* no quoting function */
438 .version_report = NULL /* no version reporting (redundant) */
e6d225ae
DW
439};
440
441static lookup_info lsearch_lookup_info = {
9f400174
JH
442 .name = US"lsearch", /* lookup name */
443 .type = lookup_absfile, /* uses absolute file name */
444 .open = lsearch_open, /* open function */
445 .check = lsearch_check, /* check function */
446 .find = lsearch_find, /* find function */
447 .close = lsearch_close, /* close function */
448 .tidy = NULL, /* no tidy function */
449 .quote = NULL, /* no quoting function */
450 .version_report = lsearch_version_report /* version reporting */
e6d225ae
DW
451};
452
453static lookup_info nwildlsearch_lookup_info = {
9f400174
JH
454 .name = US"nwildlsearch", /* lookup name */
455 .type = lookup_absfile, /* uses absolute file name */
456 .open = lsearch_open, /* open function */
457 .check = lsearch_check, /* check function */
458 .find = nwildlsearch_find, /* find function */
459 .close = lsearch_close, /* close function */
460 .tidy = NULL, /* no tidy function */
461 .quote = NULL, /* no quoting function */
462 .version_report = NULL /* no version reporting (redundant) */
e6d225ae
DW
463};
464
465static lookup_info wildlsearch_lookup_info = {
9f400174
JH
466 .name = US"wildlsearch", /* lookup name */
467 .type = lookup_absfile, /* uses absolute file name */
468 .open = lsearch_open, /* open function */
469 .check = lsearch_check, /* check function */
470 .find = wildlsearch_find, /* find function */
471 .close = lsearch_close, /* close function */
472 .tidy = NULL, /* no tidy function */
473 .quote = NULL, /* no quoting function */
474 .version_report = NULL /* no version reporting (redundant) */
e6d225ae
DW
475};
476
477#ifdef DYNLOOKUP
478#define lsearch_lookup_module_info _lookup_module_info
479#endif
480
481static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
482 &lsearch_lookup_info,
483 &nwildlsearch_lookup_info,
484 &wildlsearch_lookup_info };
485lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
486
0756eb3c 487/* End of lookups/lsearch.c */