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