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