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