| 1 | /************************************************* |
| 2 | * Exim - an Internet mail transport agent * |
| 3 | *************************************************/ |
| 4 | |
| 5 | /* Copyright (c) University of Cambridge 1995 - 2016 */ |
| 6 | /* See the file NOTICE for conditions of use and distribution. */ |
| 7 | |
| 8 | |
| 9 | #include "../exim.h" |
| 10 | #include "rf_functions.h" |
| 11 | #include "manualroute.h" |
| 12 | |
| 13 | |
| 14 | /* Options specific to the manualroute router. */ |
| 15 | |
| 16 | optionlist manualroute_router_options[] = { |
| 17 | { "host_all_ignored", opt_stringptr, |
| 18 | (void *)(offsetof(manualroute_router_options_block, host_all_ignored)) }, |
| 19 | { "host_find_failed", opt_stringptr, |
| 20 | (void *)(offsetof(manualroute_router_options_block, host_find_failed)) }, |
| 21 | { "hosts_randomize", opt_bool, |
| 22 | (void *)(offsetof(manualroute_router_options_block, hosts_randomize)) }, |
| 23 | { "route_data", opt_stringptr, |
| 24 | (void *)(offsetof(manualroute_router_options_block, route_data)) }, |
| 25 | { "route_list", opt_stringptr, |
| 26 | (void *)(offsetof(manualroute_router_options_block, route_list)) }, |
| 27 | { "same_domain_copy_routing", opt_bool|opt_public, |
| 28 | (void *)(offsetof(router_instance, same_domain_copy_routing)) } |
| 29 | }; |
| 30 | |
| 31 | /* Size of the options list. An extern variable has to be used so that its |
| 32 | address can appear in the tables drtables.c. */ |
| 33 | |
| 34 | int manualroute_router_options_count = |
| 35 | sizeof(manualroute_router_options)/sizeof(optionlist); |
| 36 | |
| 37 | /* Default private options block for the manualroute router. */ |
| 38 | |
| 39 | manualroute_router_options_block manualroute_router_option_defaults = { |
| 40 | -1, /* host_all_ignored code (unset) */ |
| 41 | -1, /* host_find_failed code (unset) */ |
| 42 | FALSE, /* hosts_randomize */ |
| 43 | US"defer", /* host_all_ignored */ |
| 44 | US"freeze", /* host_find_failed */ |
| 45 | NULL, /* route_data */ |
| 46 | NULL /* route_list */ |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | /* Names and values for host_find_failed and host_all_ignored. */ |
| 51 | |
| 52 | static uschar *hff_names[] = { |
| 53 | US"ignore", /* MUST be first - not valid for host_all_ignored */ |
| 54 | US"decline", |
| 55 | US"defer", |
| 56 | US"fail", |
| 57 | US"freeze", |
| 58 | US"pass" }; |
| 59 | |
| 60 | static int hff_codes[] = { hff_ignore, hff_decline, hff_defer, hff_fail, |
| 61 | hff_freeze, hff_pass }; |
| 62 | |
| 63 | static int hff_count= sizeof(hff_codes)/sizeof(int); |
| 64 | |
| 65 | |
| 66 | |
| 67 | /************************************************* |
| 68 | * Initialization entry point * |
| 69 | *************************************************/ |
| 70 | |
| 71 | /* Called for each instance, after its options have been read, to enable |
| 72 | consistency checks to be done, or anything else that needs to be set up. */ |
| 73 | |
| 74 | void |
| 75 | manualroute_router_init(router_instance *rblock) |
| 76 | { |
| 77 | manualroute_router_options_block *ob = |
| 78 | (manualroute_router_options_block *)(rblock->options_block); |
| 79 | int i; |
| 80 | |
| 81 | /* Host_find_failed must be a recognized word */ |
| 82 | |
| 83 | for (i = 0; i < hff_count; i++) |
| 84 | { |
| 85 | if (Ustrcmp(ob->host_find_failed, hff_names[i]) == 0) |
| 86 | { |
| 87 | ob->hff_code = hff_codes[i]; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | if (ob->hff_code < 0) |
| 92 | log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n " |
| 93 | "unrecognized setting for host_find_failed option", rblock->name); |
| 94 | |
| 95 | for (i = 1; i < hff_count; i++) /* NB starts at 1 to skip "ignore" */ |
| 96 | { |
| 97 | if (Ustrcmp(ob->host_all_ignored, hff_names[i]) == 0) |
| 98 | { |
| 99 | ob->hai_code = hff_codes[i]; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | if (ob->hai_code < 0) |
| 104 | log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n " |
| 105 | "unrecognized setting for host_all_ignored option", rblock->name); |
| 106 | |
| 107 | /* One of route_list or route_data must be specified */ |
| 108 | |
| 109 | if ((ob->route_list == NULL && ob->route_data == NULL) || |
| 110 | (ob->route_list != NULL && ob->route_data != NULL)) |
| 111 | log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n " |
| 112 | "route_list or route_data (but not both) must be specified", |
| 113 | rblock->name); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | /************************************************* |
| 120 | * Parse a route item * |
| 121 | *************************************************/ |
| 122 | |
| 123 | /* The format of a route list item is: |
| 124 | |
| 125 | <domain> [<host[list]> [<options>]] |
| 126 | |
| 127 | if obtained from route_list. The domain is absent if the string came from |
| 128 | route_data, in which case domain==NULL. The domain and the host list may be |
| 129 | enclosed in quotes. |
| 130 | |
| 131 | Arguments: |
| 132 | s pointer to route list item |
| 133 | domain if not NULL, where to put the domain pointer |
| 134 | hostlist where to put the host[list] pointer |
| 135 | options where to put the options pointer |
| 136 | |
| 137 | Returns: FALSE if domain expected and string is empty; |
| 138 | TRUE otherwise |
| 139 | */ |
| 140 | |
| 141 | static BOOL |
| 142 | parse_route_item(const uschar *s, const uschar **domain, const uschar **hostlist, |
| 143 | const uschar **options) |
| 144 | { |
| 145 | while (*s != 0 && isspace(*s)) s++; |
| 146 | |
| 147 | if (domain != NULL) |
| 148 | { |
| 149 | if (*s == 0) return FALSE; /* missing data */ |
| 150 | *domain = string_dequote(&s); |
| 151 | while (*s != 0 && isspace(*s)) s++; |
| 152 | } |
| 153 | |
| 154 | *hostlist = string_dequote(&s); |
| 155 | while (*s != 0 && isspace(*s)) s++; |
| 156 | *options = s; |
| 157 | return TRUE; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | /************************************************* |
| 163 | * Main entry point * |
| 164 | *************************************************/ |
| 165 | |
| 166 | /* The manualroute router provides a manual routing facility (surprise, |
| 167 | surprise). The data that defines the routing can either be set in route_data |
| 168 | (which means it can be found by, for example, looking up the domain in a file), |
| 169 | or a list of domain patterns and their corresponding data can be provided in |
| 170 | route_list. */ |
| 171 | |
| 172 | /* See local README for interface details. This router returns: |
| 173 | |
| 174 | DECLINE |
| 175 | . no pattern in route_list matched (route_data not set) |
| 176 | . route_data was an empty string (route_list not set) |
| 177 | . forced expansion failure in route_data (rf_expand_data) |
| 178 | . forced expansion of host list |
| 179 | . host_find_failed = decline |
| 180 | |
| 181 | DEFER |
| 182 | . transport not defined when needed |
| 183 | . lookup defer in route_list when matching domain pattern |
| 184 | . non-forced expansion failure in route_data |
| 185 | . non-forced expansion failure in host list |
| 186 | . unknown routing option |
| 187 | . host list missing for remote transport (not verifying) |
| 188 | . timeout etc on host lookup (pass_on_timeout not set) |
| 189 | . verifying the errors address caused a deferment or a big disaster such |
| 190 | as an expansion failure (rf_get_errors_address) |
| 191 | . expanding a headers_{add,remove} string caused a deferment or another |
| 192 | expansion error (rf_get_munge_headers) |
| 193 | . a problem in rf_get_transport: no transport when one is needed; |
| 194 | failed to expand dynamic transport; failed to find dynamic transport |
| 195 | . failure to expand or find a uid/gid (rf_get_ugid via rf_queue_add) |
| 196 | . host_find_failed = freeze or defer |
| 197 | . self = freeze or defer |
| 198 | |
| 199 | PASS |
| 200 | . timeout etc on host lookup (pass_on_timeout set) |
| 201 | . host_find_failed = pass |
| 202 | . self = pass |
| 203 | |
| 204 | REROUTED |
| 205 | . self = reroute |
| 206 | |
| 207 | FAIL |
| 208 | . host_find_failed = fail |
| 209 | . self = fail |
| 210 | |
| 211 | OK |
| 212 | . added address to addr_local or addr_remote, as appropriate for the |
| 213 | type of transport; this includes the self="send" case. |
| 214 | */ |
| 215 | |
| 216 | int |
| 217 | manualroute_router_entry( |
| 218 | router_instance *rblock, /* data for this instantiation */ |
| 219 | address_item *addr, /* address we are working on */ |
| 220 | struct passwd *pw, /* passwd entry after check_local_user */ |
| 221 | int verify, /* v_none/v_recipient/v_sender/v_expn */ |
| 222 | address_item **addr_local, /* add it to this if it's local */ |
| 223 | address_item **addr_remote, /* add it to this if it's remote */ |
| 224 | address_item **addr_new, /* put new addresses on here */ |
| 225 | address_item **addr_succeed) /* put old address here on success */ |
| 226 | { |
| 227 | int rc, lookup_type; |
| 228 | uschar *route_item = NULL; |
| 229 | const uschar *options = NULL; |
| 230 | const uschar *hostlist = NULL; |
| 231 | const uschar *domain; |
| 232 | uschar *newhostlist; |
| 233 | const uschar *listptr; |
| 234 | manualroute_router_options_block *ob = |
| 235 | (manualroute_router_options_block *)(rblock->options_block); |
| 236 | transport_instance *transport = NULL; |
| 237 | BOOL individual_transport_set = FALSE; |
| 238 | BOOL randomize = ob->hosts_randomize; |
| 239 | |
| 240 | addr_new = addr_new; /* Keep picky compilers happy */ |
| 241 | addr_succeed = addr_succeed; |
| 242 | |
| 243 | DEBUG(D_route) debug_printf("%s router called for %s\n domain = %s\n", |
| 244 | rblock->name, addr->address, addr->domain); |
| 245 | |
| 246 | /* The initialization check ensures that either route_list or route_data is |
| 247 | set. */ |
| 248 | |
| 249 | if (ob->route_list != NULL) |
| 250 | { |
| 251 | int sep = -(';'); /* Default is semicolon */ |
| 252 | listptr = ob->route_list; |
| 253 | |
| 254 | while ((route_item = string_nextinlist(&listptr, &sep, NULL, 0)) != NULL) |
| 255 | { |
| 256 | int rc; |
| 257 | |
| 258 | DEBUG(D_route) debug_printf("route_item = %s\n", route_item); |
| 259 | if (!parse_route_item(route_item, &domain, &hostlist, &options)) |
| 260 | continue; /* Ignore blank items */ |
| 261 | |
| 262 | /* Check the current domain; if it matches, break the loop */ |
| 263 | |
| 264 | if ((rc = match_isinlist(addr->domain, &domain, UCHAR_MAX+1, |
| 265 | &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, CUSS &lookup_value)) == OK) |
| 266 | break; |
| 267 | |
| 268 | /* If there was a problem doing the check, defer */ |
| 269 | |
| 270 | if (rc == DEFER) |
| 271 | { |
| 272 | addr->message = US"lookup defer in route_list"; |
| 273 | return DEFER; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (route_item == NULL) return DECLINE; /* No pattern in the list matched */ |
| 278 | } |
| 279 | |
| 280 | /* Handle a single routing item in route_data. If it expands to an empty |
| 281 | string, decline. */ |
| 282 | |
| 283 | else |
| 284 | { |
| 285 | route_item = rf_expand_data(addr, ob->route_data, &rc); |
| 286 | if (route_item == NULL) return rc; |
| 287 | (void) parse_route_item(route_item, NULL, &hostlist, &options); |
| 288 | if (hostlist[0] == 0) return DECLINE; |
| 289 | } |
| 290 | |
| 291 | /* Expand the hostlist item. It may then pointing to an empty string, or to a |
| 292 | single host or a list of hosts; options is pointing to the rest of the |
| 293 | routelist item, which is either empty or contains various option words. */ |
| 294 | |
| 295 | DEBUG(D_route) debug_printf("original list of hosts = \"%s\" options = %s\n", |
| 296 | hostlist, options); |
| 297 | |
| 298 | newhostlist = expand_string_copy(hostlist); |
| 299 | lookup_value = NULL; /* Finished with */ |
| 300 | expand_nmax = -1; |
| 301 | |
| 302 | /* If the expansion was forced to fail, just decline. Otherwise there is a |
| 303 | configuration problem. */ |
| 304 | |
| 305 | if (newhostlist == NULL) |
| 306 | { |
| 307 | if (expand_string_forcedfail) return DECLINE; |
| 308 | addr->message = string_sprintf("%s router: failed to expand \"%s\": %s", |
| 309 | rblock->name, hostlist, expand_string_message); |
| 310 | return DEFER; |
| 311 | } |
| 312 | else hostlist = newhostlist; |
| 313 | |
| 314 | DEBUG(D_route) debug_printf("expanded list of hosts = \"%s\" options = %s\n", |
| 315 | hostlist, options); |
| 316 | |
| 317 | /* Set default lookup type and scan the options */ |
| 318 | |
| 319 | lookup_type = lk_default; |
| 320 | |
| 321 | while (*options != 0) |
| 322 | { |
| 323 | unsigned n; |
| 324 | const uschar *s = options; |
| 325 | while (*options != 0 && !isspace(*options)) options++; |
| 326 | n = options-s; |
| 327 | |
| 328 | if (Ustrncmp(s, "randomize", n) == 0) randomize = TRUE; |
| 329 | else if (Ustrncmp(s, "no_randomize", n) == 0) randomize = FALSE; |
| 330 | else if (Ustrncmp(s, "byname", n) == 0) lookup_type = lk_byname; |
| 331 | else if (Ustrncmp(s, "bydns", n) == 0) lookup_type = lk_bydns; |
| 332 | else |
| 333 | { |
| 334 | transport_instance *t; |
| 335 | for (t = transports; t != NULL; t = t->next) |
| 336 | if (Ustrncmp(t->name, s, n) == 0) |
| 337 | { |
| 338 | transport = t; |
| 339 | individual_transport_set = TRUE; |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | if (t == NULL) |
| 344 | { |
| 345 | s = string_sprintf("unknown routing option or transport name \"%s\"", s); |
| 346 | log_write(0, LOG_MAIN, "Error in %s router: %s", rblock->name, s); |
| 347 | addr->message = string_sprintf("error in router: %s", s); |
| 348 | return DEFER; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (*options) |
| 353 | { |
| 354 | options++; |
| 355 | while (*options != 0 && isspace(*options)) options++; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* Set up the errors address, if any. */ |
| 360 | |
| 361 | rc = rf_get_errors_address(addr, rblock, verify, &addr->prop.errors_address); |
| 362 | if (rc != OK) return rc; |
| 363 | |
| 364 | /* Set up the additional and removeable headers for this address. */ |
| 365 | |
| 366 | rc = rf_get_munge_headers(addr, rblock, &addr->prop.extra_headers, |
| 367 | &addr->prop.remove_headers); |
| 368 | if (rc != OK) return rc; |
| 369 | |
| 370 | /* If an individual transport is not set, get the transport for this router, if |
| 371 | any. It might be expanded, or it might be unset if this router has verify_only |
| 372 | set. */ |
| 373 | |
| 374 | if (!individual_transport_set) |
| 375 | { |
| 376 | if (!rf_get_transport(rblock->transport_name, &(rblock->transport), addr, |
| 377 | rblock->name, NULL)) |
| 378 | return DEFER; |
| 379 | transport = rblock->transport; |
| 380 | } |
| 381 | |
| 382 | /* Deal with the case of a local transport. The host list is passed over as a |
| 383 | single text string that ends up in $host. */ |
| 384 | |
| 385 | if (transport != NULL && transport->info->local) |
| 386 | { |
| 387 | if (hostlist[0] != 0) |
| 388 | { |
| 389 | host_item *h; |
| 390 | addr->host_list = h = store_get(sizeof(host_item)); |
| 391 | h->name = string_copy(hostlist); |
| 392 | h->address = NULL; |
| 393 | h->port = PORT_NONE; |
| 394 | h->mx = MX_NONE; |
| 395 | h->status = hstatus_unknown; |
| 396 | h->why = hwhy_unknown; |
| 397 | h->last_try = 0; |
| 398 | h->next = NULL; |
| 399 | } |
| 400 | |
| 401 | /* There is nothing more to do other than to queue the address for the |
| 402 | local transport, filling in any uid/gid. This can be done by the common |
| 403 | rf_queue_add() function. */ |
| 404 | |
| 405 | addr->transport = transport; |
| 406 | return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)? |
| 407 | OK : DEFER; |
| 408 | } |
| 409 | |
| 410 | /* There is either no transport (verify_only) or a remote transport. A host |
| 411 | list is mandatory in either case, except when verifying, in which case the |
| 412 | address is just accepted. */ |
| 413 | |
| 414 | if (hostlist[0] == 0) |
| 415 | { |
| 416 | if (verify != v_none) goto ROUTED; |
| 417 | addr->message = string_sprintf("error in %s router: no host(s) specified " |
| 418 | "for domain %s", rblock->name, addr->domain); |
| 419 | log_write(0, LOG_MAIN, "%s", addr->message); |
| 420 | return DEFER; |
| 421 | } |
| 422 | |
| 423 | /* Otherwise we finish the routing here by building a chain of host items |
| 424 | for the list of configured hosts, and then finding their addresses. */ |
| 425 | |
| 426 | host_build_hostlist(&(addr->host_list), hostlist, randomize); |
| 427 | rc = rf_lookup_hostlist(rblock, addr, rblock->ignore_target_hosts, lookup_type, |
| 428 | ob->hff_code, addr_new); |
| 429 | if (rc != OK) return rc; |
| 430 | |
| 431 | /* If host_find_failed is set to "ignore", it is possible for all the hosts to |
| 432 | be ignored, in which case we will end up with an empty host list. What happens |
| 433 | is controlled by host_all_ignored. */ |
| 434 | |
| 435 | if (addr->host_list == NULL) |
| 436 | { |
| 437 | int i; |
| 438 | DEBUG(D_route) debug_printf("host_find_failed ignored every host\n"); |
| 439 | if (ob->hai_code == hff_decline) return DECLINE; |
| 440 | if (ob->hai_code == hff_pass) return PASS; |
| 441 | |
| 442 | for (i = 0; i < hff_count; i++) |
| 443 | if (ob->hai_code == hff_codes[i]) break; |
| 444 | |
| 445 | addr->message = string_sprintf("lookup failed for all hosts in %s router: " |
| 446 | "host_find_failed=ignore host_all_ignored=%s", rblock->name, hff_names[i]); |
| 447 | |
| 448 | if (ob->hai_code == hff_defer) return DEFER; |
| 449 | if (ob->hai_code == hff_fail) return FAIL; |
| 450 | |
| 451 | addr->special_action = SPECIAL_FREEZE; |
| 452 | return DEFER; |
| 453 | } |
| 454 | |
| 455 | /* Finally, since we have done all the routing here, there must be a transport |
| 456 | defined for these hosts. It will be a remote one, as a local transport is |
| 457 | dealt with above. However, we don't need one if verifying only. */ |
| 458 | |
| 459 | if (transport == NULL && verify == v_none) |
| 460 | { |
| 461 | log_write(0, LOG_MAIN, "Error in %s router: no transport defined", |
| 462 | rblock->name); |
| 463 | addr->message = US"error in router: transport missing"; |
| 464 | return DEFER; |
| 465 | } |
| 466 | |
| 467 | /* Fill in the transport, queue for remote delivery. The yield of |
| 468 | rf_queue_add() is always TRUE for a remote transport. */ |
| 469 | |
| 470 | ROUTED: |
| 471 | |
| 472 | addr->transport = transport; |
| 473 | (void)rf_queue_add(addr, addr_local, addr_remote, rblock, NULL); |
| 474 | return OK; |
| 475 | } |
| 476 | |
| 477 | /* End of routers/manualroute.c */ |