1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
10 #include "rf_functions.h"
11 #include "manualroute.h"
14 /* Options specific to the manualroute router. */
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)) }
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. */
34 int manualroute_router_options_count =
35 sizeof(manualroute_router_options)/sizeof(optionlist);
37 /* Default private options block for the manualroute router. */
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 */
50 /* Names and values for host_find_failed and host_all_ignored. */
52 static uschar *hff_names[] = {
53 US"ignore", /* MUST be first - not valid for host_all_ignored */
60 static int hff_codes[] = { hff_ignore, hff_decline, hff_defer, hff_fail,
61 hff_freeze, hff_pass };
63 static int hff_count= sizeof(hff_codes)/sizeof(int);
67 /*************************************************
68 * Initialization entry point *
69 *************************************************/
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. */
75 manualroute_router_init(router_instance *rblock)
77 manualroute_router_options_block *ob =
78 (manualroute_router_options_block *)(rblock->options_block);
81 /* Host_find_failed must be a recognized word */
83 for (i = 0; i < hff_count; i++)
85 if (Ustrcmp(ob->host_find_failed, hff_names[i]) == 0)
87 ob->hff_code = hff_codes[i];
92 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
93 "unrecognized setting for host_find_failed option", rblock->name);
95 for (i = 1; i < hff_count; i++) /* NB starts at 1 to skip "ignore" */
97 if (Ustrcmp(ob->host_all_ignored, hff_names[i]) == 0)
99 ob->hai_code = hff_codes[i];
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);
107 /* One of route_list or route_data must be specified */
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",
119 /*************************************************
120 * Parse a route item *
121 *************************************************/
123 /* The format of a route list item is:
125 <domain> [<host[list]> [<options>]]
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
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
137 Returns: FALSE if domain expected and string is empty;
142 parse_route_item(uschar *s, uschar **domain, uschar **hostlist,
145 while (*s != 0 && isspace(*s)) s++;
149 if (*s == 0) return FALSE; /* missing data */
150 *domain = string_dequote(&s);
151 while (*s != 0 && isspace(*s)) s++;
154 *hostlist = string_dequote(&s);
155 while (*s != 0 && isspace(*s)) s++;
162 /*************************************************
164 *************************************************/
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
172 /* See local README for interface details. This router returns:
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
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
200 . timeout etc on host lookup (pass_on_timeout set)
201 . host_find_failed = pass
208 . host_find_failed = fail
212 . added address to addr_local or addr_remote, as appropriate for the
213 type of transport; this includes the self="send" case.
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 */
228 uschar *route_item = NULL;
229 uschar *options = NULL;
230 uschar *hostlist = NULL;
231 uschar *domain, *newhostlist, *listptr;
232 manualroute_router_options_block *ob =
233 (manualroute_router_options_block *)(rblock->options_block);
234 transport_instance *transport = NULL;
235 BOOL individual_transport_set = FALSE;
236 BOOL randomize = ob->hosts_randomize;
238 addr_new = addr_new; /* Keep picky compilers happy */
239 addr_succeed = addr_succeed;
241 DEBUG(D_route) debug_printf("%s router called for %s\n domain = %s\n",
242 rblock->name, addr->address, addr->domain);
244 /* The initialization check ensures that either route_list or route_data is
247 if (ob->route_list != NULL)
249 int sep = -(';'); /* Default is semicolon */
250 listptr = ob->route_list;
252 while ((route_item = string_nextinlist(&listptr, &sep, NULL, 0)) != NULL)
256 DEBUG(D_route) debug_printf("route_item = %s\n", route_item);
257 if (!parse_route_item(route_item, &domain, &hostlist, &options))
258 continue; /* Ignore blank items */
260 /* Check the current domain; if it matches, break the loop */
262 if ((rc = match_isinlist(addr->domain, &domain, UCHAR_MAX+1,
263 &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, &lookup_value)) == OK)
266 /* If there was a problem doing the check, defer */
270 addr->message = US"lookup defer in route_list";
275 if (route_item == NULL) return DECLINE; /* No pattern in the list matched */
278 /* Handle a single routing item in route_data. If it expands to an empty
283 route_item = rf_expand_data(addr, ob->route_data, &rc);
284 if (route_item == NULL) return rc;
285 (void) parse_route_item(route_item, NULL, &hostlist, &options);
286 if (hostlist[0] == 0) return DECLINE;
289 /* Expand the hostlist item. It may then pointing to an empty string, or to a
290 single host or a list of hosts; options is pointing to the rest of the
291 routelist item, which is either empty or contains various option words. */
293 DEBUG(D_route) debug_printf("original list of hosts = \"%s\" options = %s\n",
296 newhostlist = expand_string_copy(hostlist);
297 lookup_value = NULL; /* Finished with */
300 /* If the expansion was forced to fail, just decline. Otherwise there is a
301 configuration problem. */
303 if (newhostlist == NULL)
305 if (expand_string_forcedfail) return DECLINE;
306 addr->message = string_sprintf("%s router: failed to expand \"%s\": %s",
307 rblock->name, hostlist, expand_string_message);
310 else hostlist = newhostlist;
312 DEBUG(D_route) debug_printf("expanded list of hosts = \"%s\" options = %s\n",
315 /* Set default lookup type and scan the options */
317 lookup_type = lk_default;
319 while (*options != 0)
323 while (*options != 0 && !isspace(*options)) options++;
327 if (Ustrcmp(s, "randomize") == 0) randomize = TRUE;
328 else if (Ustrcmp(s, "no_randomize") == 0) randomize = FALSE;
329 else if (Ustrcmp(s, "byname") == 0) lookup_type = lk_byname;
330 else if (Ustrcmp(s, "bydns") == 0) lookup_type = lk_bydns;
333 transport_instance *t;
334 for (t = transports; t != NULL; t = t->next)
336 if (Ustrcmp(t->name, s) == 0)
339 individual_transport_set = TRUE;
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);
355 while (*options != 0 && isspace(*options)) options++;
359 /* Set up the errors address, if any. */
361 rc = rf_get_errors_address(addr, rblock, verify, &(addr->p.errors_address));
362 if (rc != OK) return rc;
364 /* Set up the additional and removeable headers for this address. */
366 rc = rf_get_munge_headers(addr, rblock, &(addr->p.extra_headers),
367 &(addr->p.remove_headers));
368 if (rc != OK) return rc;
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
374 if (!individual_transport_set)
376 if (!rf_get_transport(rblock->transport_name, &(rblock->transport), addr,
379 transport = rblock->transport;
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. */
385 if (transport != NULL && transport->info->local)
387 if (hostlist[0] != 0)
390 addr->host_list = h = store_get(sizeof(host_item));
391 h->name = string_copy(hostlist);
395 h->status = hstatus_unknown;
396 h->why = hwhy_unknown;
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. */
405 addr->transport = transport;
406 return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)?
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. */
414 if (hostlist[0] == 0)
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, domain);
419 log_write(0, LOG_MAIN, "%s", addr->message);
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. */
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;
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. */
435 if (addr->host_list == NULL)
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;
442 for (i = 0; i < hff_count; i++)
443 if (ob->hai_code == hff_codes[i]) break;
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]);
448 if (ob->hai_code == hff_defer) return DEFER;
449 if (ob->hai_code == hff_fail) return FAIL;
451 addr->special_action = SPECIAL_FREEZE;
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. */
459 if (transport == NULL && verify == v_none)
461 log_write(0, LOG_MAIN, "Error in %s router: no transport defined",
463 addr->message = US"error in router: transport missing";
467 /* Fill in the transport, queue for remote delivery. The yield of
468 rf_queue_add() is always TRUE for a remote transport. */
472 addr->transport = transport;
473 (void)rf_queue_add(addr, addr_local, addr_remote, rblock, NULL);
477 /* End of routers/manualroute.c */