Copyright updates:
[exim.git] / src / src / lookups / lsearch.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9 #include "../exim.h"
10 #include "lf_functions.h"
11
12 /* Codes for the different kinds of lsearch that are supported */
13
14 enum {
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
29 static void *
30 lsearch_open(const uschar * filename, uschar ** errmsg)
31 {
32 FILE *f = Ufopen(filename, "rb");
33 if (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 }
40 return f;
41 }
42
43
44
45 /*************************************************
46 * Check entry point *
47 *************************************************/
48
49 static BOOL
50 lsearch_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
51 gid_t *owngroups, uschar **errmsg)
52 {
53 return 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
65 Extra argument:
66
67 type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or
68 LSEARCH_IP
69
70 There is some messy logic in here to cope with very long data lines that do not
71 fit into the fixed sized buffer. Most of the time this will never be exercised,
72 but people do occasionally do weird things. */
73
74 static int
75 internal_lsearch_find(void * handle, const uschar * filename,
76 const uschar * keystring, int length, uschar ** result, uschar ** errmsg,
77 int type)
78 {
79 FILE *f = (FILE *)handle;
80 BOOL last_was_eol = TRUE;
81 BOOL this_is_eol = TRUE;
82 int old_pool = store_pool;
83 rmark reset_point = NULL;
84 uschar buffer[4096];
85
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. */
91
92 if(type == LSEARCH_WILD || type == LSEARCH_NWILD)
93 {
94 store_pool = POOL_MAIN;
95 reset_point = store_mark();
96 }
97
98 filename = filename; /* Keep picky compilers happy */
99 errmsg = errmsg;
100
101 rewind(f);
102 for (last_was_eol = TRUE;
103 Ufgets(buffer, sizeof(buffer), f) != NULL;
104 last_was_eol = this_is_eol)
105 {
106 int p = Ustrlen(buffer);
107 int linekeylength;
108 BOOL this_is_comment;
109 gstring * yield;
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 {
150 if (*s == '\\') *t++ = string_interpret_escape(CUSS &s);
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];
185 const uschar *list = buffer;
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 }
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;
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;
224 if (string_is_ip_address(buffer, &maskoffset) == 0 ||
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
244 if (reset_point)
245 {
246 reset_point = store_reset(reset_point);
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
257 this_is_comment = FALSE;
258 yield = string_get(100);
259 if (*s != 0)
260 yield = string_cat(yield, s);
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 {
286 this_is_comment = (this_is_comment || (buffer[0] == 0 || buffer[0] == '#'));
287 if (this_is_comment) continue;
288 if (!isspace((uschar)buffer[0])) break;
289 while (isspace((uschar)*s)) s++;
290 *(--s) = ' ';
291 }
292 if (this_is_comment) continue;
293
294 /* Join a physical or logical line continuation onto the result string. */
295
296 yield = string_cat(yield, s);
297 }
298
299 gstring_release_unused(yield);
300 *result = string_from_gstring(yield);
301 return OK;
302 }
303
304 /* Reset dynamic store, if we need to */
305
306 if (reset_point)
307 {
308 store_reset(reset_point);
309 store_pool = old_pool;
310 }
311
312 return FAIL;
313 }
314
315
316 /*************************************************
317 * Find entry point for lsearch *
318 *************************************************/
319
320 /* See local README for interface description */
321
322 static int
323 lsearch_find(void * handle, const uschar * filename, const uschar * keystring,
324 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
325 const uschar * opts)
326 {
327 do_cache = do_cache; /* Keep picky compilers happy */
328 return 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
340 static int
341 wildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
342 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
343 const uschar * opts)
344 {
345 do_cache = do_cache; /* Keep picky compilers happy */
346 return 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
358 static int
359 nwildlsearch_find(void * handle, const uschar * filename, const uschar * keystring,
360 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
361 const uschar * opts)
362 {
363 do_cache = do_cache; /* Keep picky compilers happy */
364 return 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
377 static int
378 iplsearch_find(void * handle, uschar const * filename, const uschar * keystring,
379 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
380 const uschar * opts)
381 {
382 do_cache = do_cache; /* Keep picky compilers happy */
383
384 if ((length == 1 && keystring[0] == '*') ||
385 string_is_ip_address(keystring, NULL) != 0)
386 return internal_lsearch_find(handle, filename, keystring, length, result,
387 errmsg, LSEARCH_IP);
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);
392 return DEFER;
393 }
394
395
396
397
398 /*************************************************
399 * Close entry point *
400 *************************************************/
401
402 /* See local README for interface description */
403
404 static void
405 lsearch_close(void *handle)
406 {
407 (void)fclose((FILE *)handle);
408 }
409
410
411
412 /*************************************************
413 * Version reporting entry point *
414 *************************************************/
415
416 /* See local README for interface description. */
417
418 #include "../version.h"
419
420 void
421 lsearch_version_report(FILE *f)
422 {
423 #ifdef DYNLOOKUP
424 fprintf(f, "Library version: lsearch: Exim version %s\n", EXIM_VERSION_STR);
425 #endif
426 }
427
428
429 static lookup_info iplsearch_lookup_info = {
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) */
439 };
440
441 static lookup_info lsearch_lookup_info = {
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 */
451 };
452
453 static lookup_info nwildlsearch_lookup_info = {
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) */
463 };
464
465 static lookup_info wildlsearch_lookup_info = {
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) */
475 };
476
477 #ifdef DYNLOOKUP
478 #define lsearch_lookup_module_info _lookup_module_info
479 #endif
480
481 static lookup_info *_lookup_list[] = { &iplsearch_lookup_info,
482 &lsearch_lookup_info,
483 &nwildlsearch_lookup_info,
484 &wildlsearch_lookup_info };
485 lookup_module_info lsearch_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 4 };
486
487 /* End of lookups/lsearch.c */