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