SPDX: license tags (mostly by guesswork)
[exim.git] / src / src / routers / rf_lookup_hostlist.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* Copyright (c) The Exim Maintainers 2020 */
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
14
15
16 /*************************************************
17 *     Look up IP addresses for a set of hosts    *
18 *************************************************/
19
20 /* This function is called by a router to fill in the IP addresses for a set of
21 hosts that are attached to an address. Each host has its name and MX value set;
22 and those that need processing have their address fields set NULL. Multihomed
23 hosts cause additional blocks to be inserted into the chain.
24
25 This function also supports pseudo-hosts whose names end with "/MX". In this
26 case, MX records are looked up for the name, and the list of hosts obtained
27 replaces the incoming "host". In other words, "x/MX" is shorthand for "those
28 hosts pointed to by x's MX records".
29
30 It is also possible for a port to be specified along with the host name or IP
31 address. The syntax is to add ":port" on to the end. This doesn't work with
32 IPv6 addresses, so we allow IP addresses to be enclosed in [] in order to make
33 this work. The specification of the port must come last, that is, after "/MX"
34 if that is present.
35
36 Arguments:
37   rblock               the router block
38   addr                 the address being routed
39   ignore_target_hosts  list of hosts to ignore
40   lookup_type          LK_DEFAULT or LK_BYNAME or LK_BYDNS,
41                        plus LK_IPV4_{ONLY,PREFER}
42   hff_code             what to do for host find failed
43   addr_new             passed to rf_self_action for self=reroute
44
45 Returns:               OK
46                        DEFER host lookup defer
47                        PASS  timeout etc and pass_on_timeout set
48                        self_action: PASS, DECLINE, DEFER, FAIL, FREEZE
49                        hff_code after host find failed
50 */
51
52 int
53 rf_lookup_hostlist(router_instance *rblock, address_item *addr,
54   uschar *ignore_target_hosts, int lookup_type, int hff_code,
55   address_item **addr_new)
56 {
57 BOOL self_send = FALSE;
58
59 /* Look up each host address. A lookup may add additional items into the chain
60 if there are multiple addresses. Hence the use of next_h to start each cycle of
61 the loop at the next original host. If any host is identified as being the local
62 host, omit it and any subsequent hosts - i.e. treat the list like an ordered
63 list of MX hosts. If the first host is the local host, act according to the
64 "self" option in the configuration. */
65
66 for (host_item * prev = NULL, * h = addr->host_list, *next_h; h; h = next_h)
67   {
68   const uschar *canonical_name;
69   int rc, len, port, mx, sort_key;
70
71   next_h = h->next;
72   if (h->address) { prev = h; continue; }
73
74   DEBUG(D_route|D_host_lookup)
75     debug_printf("finding IP address for %s\n", h->name);
76
77   /* Handle any port setting that may be on the name; it will be removed
78   from the end of the name. */
79
80   port = host_item_get_port(h);
81
82   /* Store the previous mx and sort_key values, which were assigned in
83   host_build_hostlist and will be overwritten by host_find_bydns. */
84
85   mx = h->mx;
86   sort_key = h->sort_key;
87
88   /* If the name ends with "/MX", we interpret it to mean "the list of hosts
89   pointed to by MX records with this name", and the MX record values override
90   the ordering from host_build_hostlist. */
91
92   len = Ustrlen(h->name);
93   if (len > 3 && strcmpic(h->name + len - 3, US"/mx") == 0)
94     {
95     int whichrrs = lookup_type & LK_IPV4_ONLY
96       ? HOST_FIND_BY_MX | HOST_FIND_IPV4_ONLY
97       : lookup_type & LK_IPV4_PREFER
98       ? HOST_FIND_BY_MX | HOST_FIND_IPV4_FIRST
99       : HOST_FIND_BY_MX;
100
101     DEBUG(D_route|D_host_lookup)
102       debug_printf("doing DNS MX lookup for %s\n", h->name);
103
104     mx = MX_NONE;
105     h->name = string_copyn(h->name, len - 3);
106     rc = host_find_bydns(h,
107         ignore_target_hosts,
108         whichrrs,                       /* look only for MX records */
109         NULL,                           /* SRV service not relevant */
110         NULL,                           /* failing srv domains not relevant */
111         NULL,                           /* no special mx failing domains */
112         &rblock->dnssec,                /* dnssec request/require */
113         NULL,                           /* fully_qualified_name */
114         NULL);                          /* indicate local host removed */
115     }
116
117   /* If explicitly configured to look up by name, or if the "host name" is
118   actually an IP address, do a byname lookup. */
119
120   else if (lookup_type & LK_BYNAME || string_is_ip_address(h->name, NULL) != 0)
121     {
122     DEBUG(D_route|D_host_lookup) debug_printf("calling host_find_byname\n");
123     rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
124       &canonical_name, TRUE);
125     }
126
127   /* Otherwise, do a DNS lookup. If that yields "host not found", and the
128   lookup type is the default (i.e. "bydns" is not explicitly configured),
129   follow up with a byname lookup, just in case. */
130
131   else
132     {
133     BOOL removed;
134     int whichrrs = lookup_type & LK_IPV4_ONLY
135       ? HOST_FIND_BY_A
136       : lookup_type & LK_IPV4_PREFER
137       ? HOST_FIND_BY_A | HOST_FIND_BY_AAAA | HOST_FIND_IPV4_FIRST
138       : HOST_FIND_BY_A | HOST_FIND_BY_AAAA;
139
140     DEBUG(D_route|D_host_lookup) debug_printf("doing DNS lookup\n");
141     switch (rc = host_find_bydns(h, ignore_target_hosts, whichrrs, NULL,
142         NULL, NULL,
143         &rblock->dnssec,                        /* domains for request/require */
144         &canonical_name, &removed))
145       {
146       case HOST_FOUND:
147         if (removed) setflag(addr, af_local_host_removed);
148         break;
149       case HOST_FIND_FAILED:
150         if (lookup_type & LK_DEFAULT)
151           {
152           DEBUG(D_route|D_host_lookup)
153             debug_printf("DNS lookup failed: trying %s\n",
154               f.running_in_test_harness
155               ? "host_fake_gethostbyname" : "getipnodebyname");
156           rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
157             &canonical_name, TRUE);
158           }
159         break;
160       }
161     }
162
163   /* Temporary failure defers, unless pass_on_timeout is set */
164
165   if (rc == HOST_FIND_SECURITY)
166     {
167     addr->message = string_sprintf("host lookup for %s done insecurely" , h->name);
168     addr->basic_errno = ERRNO_DNSDEFER;
169     return DEFER;
170     }
171   if (rc == HOST_FIND_AGAIN)
172     {
173     if (rblock->pass_on_timeout)
174       {
175       DEBUG(D_route)
176         debug_printf("%s router timed out and pass_on_timeout set\n",
177           rblock->name);
178       return PASS;
179       }
180     addr->message = string_sprintf("host lookup for %s did not complete "
181       "(DNS timeout?)", h->name);
182     addr->basic_errno = ERRNO_DNSDEFER;
183     return DEFER;
184     }
185
186   /* Permanent failure is controlled by host_find_failed */
187
188   if (rc == HOST_FIND_FAILED)
189     {
190     if (hff_code == hff_ignore)
191       {
192       if (prev == NULL) addr->host_list = next_h; else prev->next = next_h;
193       continue;   /* With the next host, leave prev unchanged */
194       }
195
196     if (hff_code == hff_pass) return PASS;
197     if (hff_code == hff_decline) return DECLINE;
198
199     addr->basic_errno = ERRNO_UNKNOWNHOST;
200     addr->message =
201       string_sprintf("lookup of host \"%s\" failed in %s router%s",
202         h->name, rblock->name,
203         f.host_find_failed_syntax? ": syntax error in name" : "");
204
205     if (hff_code == hff_defer) return DEFER;
206     if (hff_code == hff_fail) return FAIL;
207
208     addr->special_action = SPECIAL_FREEZE;
209     return DEFER;
210     }
211
212   /* Deal with the settings that were previously cleared:
213   port, mx and sort_key. */
214
215   if (port != PORT_NONE)
216     for (host_item * hh = h; hh != next_h; hh = hh->next)
217       hh->port = port;
218
219   if (mx != MX_NONE)
220     for (host_item * hh = h; hh != next_h; hh = hh->next)
221       {
222       hh->mx = mx;
223       hh->sort_key = sort_key;
224       }
225
226   /* A local host gets chopped, with its successors, if there are previous
227   hosts. Otherwise the self option is used. If it is set to "send", any
228   subsequent hosts that are also the local host do NOT get chopped. */
229
230   if (rc == HOST_FOUND_LOCAL && !self_send)
231     {
232     if (prev)
233       {
234       DEBUG(D_route)
235         {
236         debug_printf("Removed from host list:\n");
237         for (; h; h = h->next) debug_printf("  %s\n", h->name);
238         }
239       prev->next = NULL;
240       setflag(addr, af_local_host_removed);
241       break;
242       }
243     rc = rf_self_action(addr, h, rblock->self_code, rblock->self_rewrite,
244       rblock->self, addr_new);
245     if (rc != OK)
246       {
247       addr->host_list = NULL;   /* Kill the host list for */
248       return rc;                /* anything other than "send" */
249       }
250     self_send = TRUE;
251     }
252
253   /* Ensure that prev is the host before next_h; this will not be h if a lookup
254   found multiple addresses or multiple MX records. */
255
256   prev = h;
257   while (prev->next != next_h) prev = prev->next;
258   }
259
260 return OK;
261 }
262
263 /* End of rf_lookup_hostlist.c */