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