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