Commit | Line | Data |
---|---|---|
0756eb3c PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
3386088d | 5 | /* Copyright (c) University of Cambridge 1995 - 2015 */ |
0756eb3c PH |
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 | ||
7cd1141b PH |
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 | ||
0756eb3c PH |
34 | Arguments: |
35 | rblock the router block | |
36 | addr the address being routed | |
37 | ignore_target_hosts list of hosts to ignore | |
66387a73 JH |
38 | lookup_type LK_DEFAULT or LK_BYNAME or LK_BYDNS, |
39 | plus LK_IPV4_{ONLY,PREFER} | |
0756eb3c PH |
40 | hff_code what to do for host find failed |
41 | addr_new passed to rf_self_action for self=reroute | |
42 | ||
43 | Returns: OK | |
44 | DEFER host lookup defer | |
45 | PASS timeout etc and pass_on_timeout set | |
46 | self_action: PASS, DECLINE, DEFER, FAIL, FREEZE | |
47 | hff_code after host find failed | |
48 | */ | |
49 | ||
50 | int | |
51 | rf_lookup_hostlist(router_instance *rblock, address_item *addr, | |
52 | uschar *ignore_target_hosts, int lookup_type, int hff_code, | |
53 | address_item **addr_new) | |
54 | { | |
55 | BOOL self_send = FALSE; | |
0756eb3c PH |
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 | ||
db3f7b69 | 64 | for (host_item * prev = NULL, * h = addr->host_list, *next_h; h; h = next_h) |
0756eb3c | 65 | { |
55414b25 | 66 | const uschar *canonical_name; |
fff1b300 | 67 | int rc, len, port, mx, sort_key; |
0756eb3c PH |
68 | |
69 | next_h = h->next; | |
fff1b300 | 70 | if (h->address) { prev = h; continue; } |
0756eb3c PH |
71 | |
72 | DEBUG(D_route|D_host_lookup) | |
73 | debug_printf("finding IP address for %s\n", h->name); | |
74 | ||
7cd1141b PH |
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 | ||
fff1b300 EA |
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 | ||
0756eb3c | 86 | /* If the name ends with "/MX", we interpret it to mean "the list of hosts |
fff1b300 EA |
87 | pointed to by MX records with this name", and the MX record values override |
88 | the ordering from host_build_hostlist. */ | |
0756eb3c PH |
89 | |
90 | len = Ustrlen(h->name); | |
91 | if (len > 3 && strcmpic(h->name + len - 3, US"/mx") == 0) | |
92 | { | |
66387a73 JH |
93 | int whichrrs = lookup_type & LK_IPV4_ONLY |
94 | ? HOST_FIND_BY_MX | HOST_FIND_IPV4_ONLY | |
95 | : lookup_type & LK_IPV4_PREFER | |
96 | ? HOST_FIND_BY_MX | HOST_FIND_IPV4_FIRST | |
97 | : HOST_FIND_BY_MX; | |
98 | ||
0756eb3c PH |
99 | DEBUG(D_route|D_host_lookup) |
100 | debug_printf("doing DNS MX lookup for %s\n", h->name); | |
101 | ||
fff1b300 | 102 | mx = MX_NONE; |
7cd1141b | 103 | h->name = string_copyn(h->name, len - 3); |
0756eb3c PH |
104 | rc = host_find_bydns(h, |
105 | ignore_target_hosts, | |
66387a73 JH |
106 | whichrrs, /* look only for MX records */ |
107 | NULL, /* SRV service not relevant */ | |
108 | NULL, /* failing srv domains not relevant */ | |
109 | NULL, /* no special mx failing domains */ | |
7cd171b7 | 110 | &rblock->dnssec, /* dnssec request/require */ |
66387a73 JH |
111 | NULL, /* fully_qualified_name */ |
112 | NULL); /* indicate local host removed */ | |
0756eb3c PH |
113 | } |
114 | ||
115 | /* If explicitly configured to look up by name, or if the "host name" is | |
116 | actually an IP address, do a byname lookup. */ | |
117 | ||
66387a73 | 118 | else if (lookup_type & LK_BYNAME || string_is_ip_address(h->name, NULL) != 0) |
0756eb3c PH |
119 | { |
120 | DEBUG(D_route|D_host_lookup) debug_printf("calling host_find_byname\n"); | |
322050c2 PH |
121 | rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE, |
122 | &canonical_name, TRUE); | |
0756eb3c PH |
123 | } |
124 | ||
125 | /* Otherwise, do a DNS lookup. If that yields "host not found", and the | |
126 | lookup type is the default (i.e. "bydns" is not explicitly configured), | |
127 | follow up with a byname lookup, just in case. */ | |
128 | ||
129 | else | |
130 | { | |
131 | BOOL removed; | |
66387a73 JH |
132 | int whichrrs = lookup_type & LK_IPV4_ONLY |
133 | ? HOST_FIND_BY_A | |
134 | : lookup_type & LK_IPV4_PREFER | |
135 | ? HOST_FIND_BY_A | HOST_FIND_BY_AAAA | HOST_FIND_IPV4_FIRST | |
136 | : HOST_FIND_BY_A | HOST_FIND_BY_AAAA; | |
137 | ||
0756eb3c | 138 | DEBUG(D_route|D_host_lookup) debug_printf("doing DNS lookup\n"); |
66387a73 | 139 | switch (rc = host_find_bydns(h, ignore_target_hosts, whichrrs, NULL, |
fff1b300 EA |
140 | NULL, NULL, |
141 | &rblock->dnssec, /* domains for request/require */ | |
142 | &canonical_name, &removed)) | |
0756eb3c | 143 | { |
fff1b300 EA |
144 | case HOST_FOUND: |
145 | if (removed) setflag(addr, af_local_host_removed); | |
146 | break; | |
147 | case HOST_FIND_FAILED: | |
66387a73 | 148 | if (lookup_type & LK_DEFAULT) |
fff1b300 EA |
149 | { |
150 | DEBUG(D_route|D_host_lookup) | |
151 | debug_printf("DNS lookup failed: trying getipnodebyname\n"); | |
152 | rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE, | |
153 | &canonical_name, TRUE); | |
154 | } | |
155 | break; | |
0756eb3c PH |
156 | } |
157 | } | |
158 | ||
159 | /* Temporary failure defers, unless pass_on_timeout is set */ | |
160 | ||
2546388c JH |
161 | if (rc == HOST_FIND_SECURITY) |
162 | { | |
163 | addr->message = string_sprintf("host lookup for %s done insecurely" , h->name); | |
164 | addr->basic_errno = ERRNO_DNSDEFER; | |
165 | return DEFER; | |
166 | } | |
0756eb3c PH |
167 | if (rc == HOST_FIND_AGAIN) |
168 | { | |
169 | if (rblock->pass_on_timeout) | |
170 | { | |
171 | DEBUG(D_route) | |
172 | debug_printf("%s router timed out and pass_on_timeout set\n", | |
173 | rblock->name); | |
174 | return PASS; | |
175 | } | |
176 | addr->message = string_sprintf("host lookup for %s did not complete " | |
177 | "(DNS timeout?)", h->name); | |
178 | addr->basic_errno = ERRNO_DNSDEFER; | |
179 | return DEFER; | |
180 | } | |
181 | ||
182 | /* Permanent failure is controlled by host_find_failed */ | |
183 | ||
184 | if (rc == HOST_FIND_FAILED) | |
185 | { | |
c456d9bb PH |
186 | if (hff_code == hff_ignore) |
187 | { | |
188 | if (prev == NULL) addr->host_list = next_h; else prev->next = next_h; | |
cd9868ec | 189 | continue; /* With the next host, leave prev unchanged */ |
c456d9bb PH |
190 | } |
191 | ||
0756eb3c PH |
192 | if (hff_code == hff_pass) return PASS; |
193 | if (hff_code == hff_decline) return DECLINE; | |
194 | ||
bd4ece7d | 195 | addr->basic_errno = ERRNO_UNKNOWNHOST; |
0756eb3c PH |
196 | addr->message = |
197 | string_sprintf("lookup of host \"%s\" failed in %s router%s", | |
198 | h->name, rblock->name, | |
8768d548 | 199 | f.host_find_failed_syntax? ": syntax error in name" : ""); |
0756eb3c PH |
200 | |
201 | if (hff_code == hff_defer) return DEFER; | |
202 | if (hff_code == hff_fail) return FAIL; | |
203 | ||
204 | addr->special_action = SPECIAL_FREEZE; | |
205 | return DEFER; | |
206 | } | |
207 | ||
fff1b300 EA |
208 | /* Deal with the settings that were previously cleared: |
209 | port, mx and sort_key. */ | |
7cd1141b PH |
210 | |
211 | if (port != PORT_NONE) | |
db3f7b69 JH |
212 | for (host_item * hh = h; hh != next_h; hh = hh->next) |
213 | hh->port = port; | |
7cd1141b | 214 | |
fff1b300 | 215 | if (mx != MX_NONE) |
db3f7b69 | 216 | for (host_item * hh = h; hh != next_h; hh = hh->next) |
fff1b300 EA |
217 | { |
218 | hh->mx = mx; | |
219 | hh->sort_key = sort_key; | |
220 | } | |
fff1b300 | 221 | |
0756eb3c PH |
222 | /* A local host gets chopped, with its successors, if there are previous |
223 | hosts. Otherwise the self option is used. If it is set to "send", any | |
224 | subsequent hosts that are also the local host do NOT get chopped. */ | |
225 | ||
226 | if (rc == HOST_FOUND_LOCAL && !self_send) | |
227 | { | |
fff1b300 | 228 | if (prev) |
0756eb3c PH |
229 | { |
230 | DEBUG(D_route) | |
231 | { | |
232 | debug_printf("Removed from host list:\n"); | |
190404d7 | 233 | for (; h; h = h->next) debug_printf(" %s\n", h->name); |
0756eb3c PH |
234 | } |
235 | prev->next = NULL; | |
236 | setflag(addr, af_local_host_removed); | |
237 | break; | |
238 | } | |
239 | rc = rf_self_action(addr, h, rblock->self_code, rblock->self_rewrite, | |
240 | rblock->self, addr_new); | |
241 | if (rc != OK) | |
242 | { | |
243 | addr->host_list = NULL; /* Kill the host list for */ | |
244 | return rc; /* anything other than "send" */ | |
245 | } | |
246 | self_send = TRUE; | |
247 | } | |
cd9868ec PH |
248 | |
249 | /* Ensure that prev is the host before next_h; this will not be h if a lookup | |
250 | found multiple addresses or multiple MX records. */ | |
251 | ||
252 | prev = h; | |
253 | while (prev->next != next_h) prev = prev->next; | |
0756eb3c PH |
254 | } |
255 | ||
256 | return OK; | |
257 | } | |
258 | ||
259 | /* End of rf_lookup_hostlist.c */ |