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