Commit | Line | Data |
---|---|---|
cd9868ec | 1 | /* $Cambridge: exim/src/src/routers/rf_lookup_hostlist.c,v 1.10 2007/03/14 11:22:23 ph10 Exp $ */ |
0756eb3c PH |
2 | |
3 | /************************************************* | |
4 | * Exim - an Internet mail transport agent * | |
5 | *************************************************/ | |
6 | ||
184e8823 | 7 | /* Copyright (c) University of Cambridge 1995 - 2007 */ |
0756eb3c PH |
8 | /* See the file NOTICE for conditions of use and distribution. */ |
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 | ||
7cd1141b PH |
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 | ||
0756eb3c PH |
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 | hff_code what to do for host find failed | |
42 | addr_new passed to rf_self_action for self=reroute | |
43 | ||
44 | Returns: 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 | ||
51 | int | |
52 | rf_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 | { | |
56 | BOOL self_send = FALSE; | |
57 | host_item *h, *next_h, *prev; | |
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 | prev = NULL; | |
cd9868ec | 67 | for (h = addr->host_list; h != NULL; h = next_h) |
0756eb3c PH |
68 | { |
69 | uschar *canonical_name; | |
7cd1141b | 70 | int rc, len, port; |
0756eb3c PH |
71 | |
72 | next_h = h->next; | |
cd9868ec | 73 | if (h->address != NULL) { prev = h; continue; } |
0756eb3c PH |
74 | |
75 | DEBUG(D_route|D_host_lookup) | |
76 | debug_printf("finding IP address for %s\n", h->name); | |
77 | ||
7cd1141b PH |
78 | /* Handle any port setting that may be on the name; it will be removed |
79 | from the end of the name. */ | |
80 | ||
81 | port = host_item_get_port(h); | |
82 | ||
0756eb3c PH |
83 | /* If the name ends with "/MX", we interpret it to mean "the list of hosts |
84 | pointed to by MX records with this name". */ | |
85 | ||
86 | len = Ustrlen(h->name); | |
87 | if (len > 3 && strcmpic(h->name + len - 3, US"/mx") == 0) | |
88 | { | |
89 | DEBUG(D_route|D_host_lookup) | |
90 | debug_printf("doing DNS MX lookup for %s\n", h->name); | |
91 | ||
7cd1141b | 92 | h->name = string_copyn(h->name, len - 3); |
0756eb3c PH |
93 | rc = host_find_bydns(h, |
94 | ignore_target_hosts, | |
95 | HOST_FIND_BY_MX, /* look only for MX records */ | |
96 | NULL, /* SRV service not relevant */ | |
97 | NULL, /* failing srv domains not relevant */ | |
98 | NULL, /* no special mx failing domains */ | |
99 | NULL, /* fully_qualified_name */ | |
100 | NULL); /* indicate local host removed */ | |
101 | } | |
102 | ||
103 | /* If explicitly configured to look up by name, or if the "host name" is | |
104 | actually an IP address, do a byname lookup. */ | |
105 | ||
7e66e54d | 106 | else if (lookup_type == lk_byname || string_is_ip_address(h->name, NULL) != 0) |
0756eb3c PH |
107 | { |
108 | DEBUG(D_route|D_host_lookup) debug_printf("calling host_find_byname\n"); | |
322050c2 PH |
109 | rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE, |
110 | &canonical_name, TRUE); | |
0756eb3c PH |
111 | } |
112 | ||
113 | /* Otherwise, do a DNS lookup. If that yields "host not found", and the | |
114 | lookup type is the default (i.e. "bydns" is not explicitly configured), | |
115 | follow up with a byname lookup, just in case. */ | |
116 | ||
117 | else | |
118 | { | |
119 | BOOL removed; | |
120 | DEBUG(D_route|D_host_lookup) debug_printf("doing DNS lookup\n"); | |
121 | rc = host_find_bydns(h, ignore_target_hosts, HOST_FIND_BY_A, NULL, NULL, | |
122 | NULL, &canonical_name, &removed); | |
123 | if (rc == HOST_FOUND) | |
124 | { | |
125 | if (removed) setflag(addr, af_local_host_removed); | |
126 | } | |
127 | else if (rc == HOST_FIND_FAILED) | |
128 | { | |
129 | if (lookup_type == lk_default) | |
130 | { | |
131 | DEBUG(D_route|D_host_lookup) | |
132 | debug_printf("DNS lookup failed: trying getipnodebyname\n"); | |
322050c2 PH |
133 | rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE, |
134 | &canonical_name, TRUE); | |
0756eb3c PH |
135 | } |
136 | } | |
137 | } | |
138 | ||
139 | /* Temporary failure defers, unless pass_on_timeout is set */ | |
140 | ||
141 | if (rc == HOST_FIND_AGAIN) | |
142 | { | |
143 | if (rblock->pass_on_timeout) | |
144 | { | |
145 | DEBUG(D_route) | |
146 | debug_printf("%s router timed out and pass_on_timeout set\n", | |
147 | rblock->name); | |
148 | return PASS; | |
149 | } | |
150 | addr->message = string_sprintf("host lookup for %s did not complete " | |
151 | "(DNS timeout?)", h->name); | |
152 | addr->basic_errno = ERRNO_DNSDEFER; | |
153 | return DEFER; | |
154 | } | |
155 | ||
156 | /* Permanent failure is controlled by host_find_failed */ | |
157 | ||
158 | if (rc == HOST_FIND_FAILED) | |
159 | { | |
c456d9bb PH |
160 | if (hff_code == hff_ignore) |
161 | { | |
162 | if (prev == NULL) addr->host_list = next_h; else prev->next = next_h; | |
cd9868ec | 163 | continue; /* With the next host, leave prev unchanged */ |
c456d9bb PH |
164 | } |
165 | ||
0756eb3c PH |
166 | if (hff_code == hff_pass) return PASS; |
167 | if (hff_code == hff_decline) return DECLINE; | |
168 | ||
169 | addr->message = | |
170 | string_sprintf("lookup of host \"%s\" failed in %s router%s", | |
171 | h->name, rblock->name, | |
172 | host_find_failed_syntax? ": syntax error in name" : ""); | |
173 | ||
174 | if (hff_code == hff_defer) return DEFER; | |
175 | if (hff_code == hff_fail) return FAIL; | |
176 | ||
177 | addr->special_action = SPECIAL_FREEZE; | |
178 | return DEFER; | |
179 | } | |
180 | ||
7cd1141b PH |
181 | /* Deal with a port setting */ |
182 | ||
183 | if (port != PORT_NONE) | |
184 | { | |
185 | host_item *hh; | |
186 | for (hh = h; hh != next_h; hh = hh->next) hh->port = port; | |
187 | } | |
188 | ||
0756eb3c PH |
189 | /* A local host gets chopped, with its successors, if there are previous |
190 | hosts. Otherwise the self option is used. If it is set to "send", any | |
191 | subsequent hosts that are also the local host do NOT get chopped. */ | |
192 | ||
193 | if (rc == HOST_FOUND_LOCAL && !self_send) | |
194 | { | |
195 | if (prev != NULL) | |
196 | { | |
197 | DEBUG(D_route) | |
198 | { | |
199 | debug_printf("Removed from host list:\n"); | |
200 | for (; h != NULL; h = h->next) debug_printf(" %s\n", h->name); | |
201 | } | |
202 | prev->next = NULL; | |
203 | setflag(addr, af_local_host_removed); | |
204 | break; | |
205 | } | |
206 | rc = rf_self_action(addr, h, rblock->self_code, rblock->self_rewrite, | |
207 | rblock->self, addr_new); | |
208 | if (rc != OK) | |
209 | { | |
210 | addr->host_list = NULL; /* Kill the host list for */ | |
211 | return rc; /* anything other than "send" */ | |
212 | } | |
213 | self_send = TRUE; | |
214 | } | |
cd9868ec PH |
215 | |
216 | /* Ensure that prev is the host before next_h; this will not be h if a lookup | |
217 | found multiple addresses or multiple MX records. */ | |
218 | ||
219 | prev = h; | |
220 | while (prev->next != next_h) prev = prev->next; | |
0756eb3c PH |
221 | } |
222 | ||
223 | return OK; | |
224 | } | |
225 | ||
226 | /* End of rf_lookup_hostlist.c */ |