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