update to pre-4.87 master
[exim.git] / src / src / routers / manualroute.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
bfe645c1 5/* Copyright (c) University of Cambridge 1995 - 2015 */
0756eb3c
PH
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
16optionlist manualroute_router_options[] = {
c456d9bb
PH
17 { "host_all_ignored", opt_stringptr,
18 (void *)(offsetof(manualroute_router_options_block, host_all_ignored)) },
0756eb3c
PH
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
32address can appear in the tables drtables.c. */
33
34int manualroute_router_options_count =
35 sizeof(manualroute_router_options)/sizeof(optionlist);
36
37/* Default private options block for the manualroute router. */
38
39manualroute_router_options_block manualroute_router_option_defaults = {
c456d9bb
PH
40 -1, /* host_all_ignored code (unset) */
41 -1, /* host_find_failed code (unset) */
0756eb3c 42 FALSE, /* hosts_randomize */
c456d9bb 43 US"defer", /* host_all_ignored */
0756eb3c
PH
44 US"freeze", /* host_find_failed */
45 NULL, /* route_data */
46 NULL /* route_list */
47};
48
49
c456d9bb
PH
50/* Names and values for host_find_failed and host_all_ignored. */
51
52static 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
60static int hff_codes[] = { hff_ignore, hff_decline, hff_defer, hff_fail,
61 hff_freeze, hff_pass };
62
63static int hff_count= sizeof(hff_codes)/sizeof(int);
0756eb3c
PH
64
65
66
67/*************************************************
68* Initialization entry point *
69*************************************************/
70
71/* Called for each instance, after its options have been read, to enable
72consistency checks to be done, or anything else that needs to be set up. */
73
74void
75manualroute_router_init(router_instance *rblock)
76{
77manualroute_router_options_block *ob =
78 (manualroute_router_options_block *)(rblock->options_block);
c456d9bb 79int i;
0756eb3c
PH
80
81/* Host_find_failed must be a recognized word */
82
c456d9bb
PH
83for (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 }
91if (ob->hff_code < 0)
0756eb3c
PH
92 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
93 "unrecognized setting for host_find_failed option", rblock->name);
94
c456d9bb
PH
95for (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 }
103if (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
0756eb3c
PH
107/* One of route_list or route_data must be specified */
108
109if ((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
127if obtained from route_list. The domain is absent if the string came from
128route_data, in which case domain==NULL. The domain and the host list may be
129enclosed in quotes.
130
131Arguments:
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
137Returns: FALSE if domain expected and string is empty;
138 TRUE otherwise
139*/
140
141static BOOL
bfe645c1
JH
142parse_route_item(const uschar *s, const uschar **domain, const uschar **hostlist,
143 const uschar **options)
0756eb3c
PH
144{
145while (*s != 0 && isspace(*s)) s++;
146
147if (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);
155while (*s != 0 && isspace(*s)) s++;
156*options = s;
157return TRUE;
158}
159
160
161
162/*************************************************
163* Main entry point *
164*************************************************/
165
166/* The manualroute router provides a manual routing facility (surprise,
167surprise). 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),
169or a list of domain patterns and their corresponding data can be provided in
170route_list. */
171
172/* See local README for interface details. This router returns:
173
174DECLINE
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
181DEFER
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
199PASS
200 . timeout etc on host lookup (pass_on_timeout set)
201 . host_find_failed = pass
202 . self = pass
203
204REROUTED
205 . self = reroute
206
207FAIL
208 . host_find_failed = fail
209 . self = fail
210
211OK
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
216int
217manualroute_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 */
fd6de02e 221 int verify, /* v_none/v_recipient/v_sender/v_expn */
0756eb3c
PH
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{
227int rc, lookup_type;
228uschar *route_item = NULL;
bfe645c1
JH
229const uschar *options = NULL;
230const uschar *hostlist = NULL;
231const uschar *domain;
232uschar *newhostlist;
233const uschar *listptr;
0756eb3c
PH
234manualroute_router_options_block *ob =
235 (manualroute_router_options_block *)(rblock->options_block);
236transport_instance *transport = NULL;
237BOOL individual_transport_set = FALSE;
238BOOL randomize = ob->hosts_randomize;
239
240addr_new = addr_new; /* Keep picky compilers happy */
241addr_succeed = addr_succeed;
242
243DEBUG(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
247set. */
248
249if (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,
bfe645c1 265 &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, CUSS &lookup_value)) == OK)
0756eb3c
PH
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
281string, decline. */
282
283else
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
292single host or a list of hosts; options is pointing to the rest of the
293routelist item, which is either empty or contains various option words. */
294
295DEBUG(D_route) debug_printf("original list of hosts = \"%s\" options = %s\n",
296 hostlist, options);
297
298newhostlist = expand_string_copy(hostlist);
299lookup_value = NULL; /* Finished with */
300expand_nmax = -1;
301
302/* If the expansion was forced to fail, just decline. Otherwise there is a
303configuration problem. */
304
305if (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 }
312else hostlist = newhostlist;
313
314DEBUG(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
319lookup_type = lk_default;
320
321while (*options != 0)
322 {
bfe645c1
JH
323 unsigned n;
324 const uschar *s = options;
0756eb3c 325 while (*options != 0 && !isspace(*options)) options++;
bfe645c1 326 n = options-s;
0756eb3c 327
bfe645c1
JH
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;
0756eb3c
PH
332 else
333 {
334 transport_instance *t;
335 for (t = transports; t != NULL; t = t->next)
0756eb3c
PH
336 if (Ustrcmp(t->name, s) == 0)
337 {
338 transport = t;
339 individual_transport_set = TRUE;
340 break;
341 }
bfe645c1 342
0756eb3c
PH
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
bfe645c1 352 if (*options)
0756eb3c
PH
353 {
354 options++;
355 while (*options != 0 && isspace(*options)) options++;
356 }
357 }
358
359/* Set up the errors address, if any. */
360
bfe645c1 361rc = rf_get_errors_address(addr, rblock, verify, &addr->prop.errors_address);
0756eb3c
PH
362if (rc != OK) return rc;
363
364/* Set up the additional and removeable headers for this address. */
365
bfe645c1
JH
366rc = rf_get_munge_headers(addr, rblock, &addr->prop.extra_headers,
367 &addr->prop.remove_headers);
0756eb3c
PH
368if (rc != OK) return rc;
369
370/* If an individual transport is not set, get the transport for this router, if
371any. It might be expanded, or it might be unset if this router has verify_only
372set. */
373
374if (!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
383single text string that ends up in $host. */
384
385if (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
411list is mandatory in either case, except when verifying, in which case the
412address is just accepted. */
413
414if (hostlist[0] == 0)
415 {
fd6de02e 416 if (verify != v_none) goto ROUTED;
0756eb3c
PH
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);
420 return DEFER;
421 }
422
423/* Otherwise we finish the routing here by building a chain of host items
424for the list of configured hosts, and then finding their addresses. */
425
426host_build_hostlist(&(addr->host_list), hostlist, randomize);
427rc = rf_lookup_hostlist(rblock, addr, rblock->ignore_target_hosts, lookup_type,
428 ob->hff_code, addr_new);
429if (rc != OK) return rc;
430
c456d9bb
PH
431/* If host_find_failed is set to "ignore", it is possible for all the hosts to
432be ignored, in which case we will end up with an empty host list. What happens
433is controlled by host_all_ignored. */
434
435if (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
0756eb3c
PH
455/* Finally, since we have done all the routing here, there must be a transport
456defined for these hosts. It will be a remote one, as a local transport is
457dealt with above. However, we don't need one if verifying only. */
458
fd6de02e 459if (transport == NULL && verify == v_none)
0756eb3c
PH
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
468rf_queue_add() is always TRUE for a remote transport. */
469
470ROUTED:
471
472addr->transport = transport;
473(void)rf_queue_add(addr, addr_local, addr_remote, rblock, NULL);
474return OK;
475}
476
477/* End of routers/manualroute.c */