| 1 | /* $Cambridge: exim/src/src/lookups/lsearch.c,v 1.4 2005/06/27 14:29:44 ph10 Exp $ */ |
| 2 | |
| 3 | /************************************************* |
| 4 | * Exim - an Internet mail transport agent * |
| 5 | *************************************************/ |
| 6 | |
| 7 | /* Copyright (c) University of Cambridge 1995 - 2005 */ |
| 8 | /* See the file NOTICE for conditions of use and distribution. */ |
| 9 | |
| 10 | #include "../exim.h" |
| 11 | #include "lf_functions.h" |
| 12 | #include "lsearch.h" |
| 13 | |
| 14 | /* Codes for the different kinds of lsearch that are supported */ |
| 15 | |
| 16 | enum { |
| 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 */ |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | |
| 25 | /************************************************* |
| 26 | * Open entry point * |
| 27 | *************************************************/ |
| 28 | |
| 29 | /* See local README for interface description */ |
| 30 | |
| 31 | void * |
| 32 | lsearch_open(uschar *filename, uschar **errmsg) |
| 33 | { |
| 34 | FILE *f = Ufopen(filename, "rb"); |
| 35 | if (f == NULL) |
| 36 | { |
| 37 | int save_errno = errno; |
| 38 | *errmsg = string_open_failed(errno, "%s for linear search", filename); |
| 39 | errno = save_errno; |
| 40 | return NULL; |
| 41 | } |
| 42 | return f; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
| 47 | /************************************************* |
| 48 | * Check entry point * |
| 49 | *************************************************/ |
| 50 | |
| 51 | BOOL |
| 52 | lsearch_check(void *handle, uschar *filename, int modemask, uid_t *owners, |
| 53 | gid_t *owngroups, uschar **errmsg) |
| 54 | { |
| 55 | return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask, |
| 56 | owners, owngroups, "lsearch", errmsg) == 0; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | /************************************************* |
| 62 | * Internal function for the various lsearches * |
| 63 | *************************************************/ |
| 64 | |
| 65 | /* See local README for interface description, plus: |
| 66 | |
| 67 | Extra argument: |
| 68 | |
| 69 | type one of the values LSEARCH_PLAIN, LSEARCH_WILD, LSEARCH_NWILD, or |
| 70 | LSEARCH_IP |
| 71 | |
| 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. */ |
| 75 | |
| 76 | static int |
| 77 | internal_lsearch_find(void *handle, uschar *filename, uschar *keystring, |
| 78 | int length, uschar **result, uschar **errmsg, int type) |
| 79 | { |
| 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; |
| 85 | uschar buffer[4096]; |
| 86 | |
| 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. */ |
| 92 | |
| 93 | if(type == LSEARCH_WILD || type == LSEARCH_NWILD) |
| 94 | { |
| 95 | store_pool = POOL_MAIN; |
| 96 | reset_point = store_get(0); |
| 97 | } |
| 98 | |
| 99 | filename = filename; /* Keep picky compilers happy */ |
| 100 | errmsg = errmsg; |
| 101 | |
| 102 | rewind(f); |
| 103 | for (last_was_eol = TRUE; |
| 104 | Ufgets(buffer, sizeof(buffer), f) != NULL; |
| 105 | last_was_eol = this_is_eol) |
| 106 | { |
| 107 | int ptr, size; |
| 108 | int p = Ustrlen(buffer); |
| 109 | int linekeylength; |
| 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 | } |
| 200 | break; /* Key matched */ |
| 201 | |
| 202 | /* Compare an ip address against a list of network/ip addresses. We have to |
| 203 | allow for the "*" case specially. */ |
| 204 | |
| 205 | case LSEARCH_IP: |
| 206 | if (linekeylength == 1 && buffer[0] == '*') |
| 207 | { |
| 208 | if (length != 1 || keystring[0] != '*') continue; |
| 209 | } |
| 210 | else if (length == 1 && keystring[0] == '*') continue; |
| 211 | else |
| 212 | { |
| 213 | int maskoffset; |
| 214 | int save = buffer[linekeylength]; |
| 215 | buffer[linekeylength] = 0; |
| 216 | if (string_is_ip_address(buffer, &maskoffset) == 0 || |
| 217 | !host_is_in_net(keystring, buffer, maskoffset)) continue; |
| 218 | buffer[linekeylength] = save; |
| 219 | } |
| 220 | break; /* Key matched */ |
| 221 | } |
| 222 | |
| 223 | /* The key has matched. Skip spaces after the key, and allow an optional |
| 224 | colon after the spaces. This is an odd specification, but it's for |
| 225 | compatibility. */ |
| 226 | |
| 227 | while (isspace((uschar)*s)) s++; |
| 228 | if (*s == ':') |
| 229 | { |
| 230 | s++; |
| 231 | while (isspace((uschar)*s)) s++; |
| 232 | } |
| 233 | |
| 234 | /* Reset dynamic store, if we need to, and revert to the search pool */ |
| 235 | |
| 236 | if (reset_point != NULL) |
| 237 | { |
| 238 | store_reset(reset_point); |
| 239 | store_pool = old_pool; |
| 240 | } |
| 241 | |
| 242 | /* Now we want to build the result string to contain the data. There can be |
| 243 | two kinds of continuation: (a) the physical line may not all have fitted into |
| 244 | the buffer, and (b) there may be logical continuation lines, for which we |
| 245 | must convert all leading white space into a single blank. |
| 246 | |
| 247 | Initialize, and copy the first segment of data. */ |
| 248 | |
| 249 | size = 100; |
| 250 | ptr = 0; |
| 251 | yield = store_get(size); |
| 252 | if (*s != 0) |
| 253 | yield = string_cat(yield, &size, &ptr, s, Ustrlen(s)); |
| 254 | |
| 255 | /* Now handle continuations */ |
| 256 | |
| 257 | for (last_was_eol = this_is_eol; |
| 258 | Ufgets(buffer, sizeof(buffer), f) != NULL; |
| 259 | last_was_eol = this_is_eol) |
| 260 | { |
| 261 | s = buffer; |
| 262 | p = Ustrlen(buffer); |
| 263 | this_is_eol = p > 0 && buffer[p-1] == '\n'; |
| 264 | |
| 265 | /* Remove trailing white space from a physical line end */ |
| 266 | |
| 267 | if (this_is_eol) |
| 268 | { |
| 269 | while (p > 0 && isspace((uschar)buffer[p-1])) p--; |
| 270 | buffer[p] = 0; |
| 271 | } |
| 272 | |
| 273 | /* If this is not a physical line continuation, skip it entirely if it's |
| 274 | empty or starts with #. Otherwise, break the loop if it doesn't start with |
| 275 | white space. Otherwise, replace leading white space with a single blank. */ |
| 276 | |
| 277 | if (last_was_eol) |
| 278 | { |
| 279 | if (buffer[0] == 0 || buffer[0] == '#') continue; |
| 280 | if (!isspace((uschar)buffer[0])) break; |
| 281 | while (isspace((uschar)*s)) s++; |
| 282 | *(--s) = ' '; |
| 283 | } |
| 284 | |
| 285 | /* Join a physical or logical line continuation onto the result string. */ |
| 286 | |
| 287 | yield = string_cat(yield, &size, &ptr, s, Ustrlen(s)); |
| 288 | } |
| 289 | |
| 290 | yield[ptr] = 0; |
| 291 | store_reset(yield + ptr + 1); |
| 292 | *result = yield; |
| 293 | return OK; |
| 294 | } |
| 295 | |
| 296 | /* Reset dynamic store, if we need to */ |
| 297 | |
| 298 | if (reset_point != NULL) |
| 299 | { |
| 300 | store_reset(reset_point); |
| 301 | store_pool = old_pool; |
| 302 | } |
| 303 | |
| 304 | return FAIL; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /************************************************* |
| 309 | * Find entry point for lsearch * |
| 310 | *************************************************/ |
| 311 | |
| 312 | /* See local README for interface description */ |
| 313 | |
| 314 | int |
| 315 | lsearch_find(void *handle, uschar *filename, uschar *keystring, int length, |
| 316 | uschar **result, uschar **errmsg, BOOL *do_cache) |
| 317 | { |
| 318 | do_cache = do_cache; /* Keep picky compilers happy */ |
| 319 | return internal_lsearch_find(handle, filename, keystring, length, result, |
| 320 | errmsg, LSEARCH_PLAIN); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | |
| 325 | /************************************************* |
| 326 | * Find entry point for wildlsearch * |
| 327 | *************************************************/ |
| 328 | |
| 329 | /* See local README for interface description */ |
| 330 | |
| 331 | int |
| 332 | wildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length, |
| 333 | uschar **result, uschar **errmsg, BOOL *do_cache) |
| 334 | { |
| 335 | do_cache = do_cache; /* Keep picky compilers happy */ |
| 336 | return internal_lsearch_find(handle, filename, keystring, length, result, |
| 337 | errmsg, LSEARCH_WILD); |
| 338 | } |
| 339 | |
| 340 | |
| 341 | |
| 342 | /************************************************* |
| 343 | * Find entry point for nwildlsearch * |
| 344 | *************************************************/ |
| 345 | |
| 346 | /* See local README for interface description */ |
| 347 | |
| 348 | int |
| 349 | nwildlsearch_find(void *handle, uschar *filename, uschar *keystring, int length, |
| 350 | uschar **result, uschar **errmsg, BOOL *do_cache) |
| 351 | { |
| 352 | do_cache = do_cache; /* Keep picky compilers happy */ |
| 353 | return internal_lsearch_find(handle, filename, keystring, length, result, |
| 354 | errmsg, LSEARCH_NWILD); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | /************************************************* |
| 361 | * Find entry point for iplsearch * |
| 362 | *************************************************/ |
| 363 | |
| 364 | /* See local README for interface description */ |
| 365 | |
| 366 | int |
| 367 | iplsearch_find(void *handle, uschar *filename, uschar *keystring, int length, |
| 368 | uschar **result, uschar **errmsg, BOOL *do_cache) |
| 369 | { |
| 370 | do_cache = do_cache; /* Keep picky compilers happy */ |
| 371 | if ((length == 1 && keystring[0] == '*') || |
| 372 | string_is_ip_address(keystring, NULL) > 0) |
| 373 | { |
| 374 | return internal_lsearch_find(handle, filename, keystring, length, result, |
| 375 | errmsg, LSEARCH_IP); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | *errmsg = string_sprintf("\"%s\" is not a valid iplsearch key (an IP " |
| 380 | "address, with optional CIDR mask, is wanted): " |
| 381 | "in a host list, use net-iplsearch as the search type", keystring); |
| 382 | return DEFER; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | /************************************************* |
| 390 | * Close entry point * |
| 391 | *************************************************/ |
| 392 | |
| 393 | /* See local README for interface description */ |
| 394 | |
| 395 | void |
| 396 | lsearch_close(void *handle) |
| 397 | { |
| 398 | (void)fclose((FILE *)handle); |
| 399 | } |
| 400 | |
| 401 | /* End of lookups/lsearch.c */ |