5646315cbb8608ad2adb950de848d40991915d4a
[exim.git] / src / src / routers / rf_lookup_hostlist.c
1 /* $Cambridge: exim/src/src/routers/rf_lookup_hostlist.c,v 1.3 2005/01/11 15:51:03 ph10 Exp $ */
2
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2005 */
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
30 Arguments:
31 rblock the router block
32 addr the address being routed
33 ignore_target_hosts list of hosts to ignore
34 lookup_type lk_default or lk_byname or lk_bydns
35 hff_code what to do for host find failed
36 addr_new passed to rf_self_action for self=reroute
37
38 Returns: OK
39 DEFER host lookup defer
40 PASS timeout etc and pass_on_timeout set
41 self_action: PASS, DECLINE, DEFER, FAIL, FREEZE
42 hff_code after host find failed
43 */
44
45 int
46 rf_lookup_hostlist(router_instance *rblock, address_item *addr,
47 uschar *ignore_target_hosts, int lookup_type, int hff_code,
48 address_item **addr_new)
49 {
50 BOOL self_send = FALSE;
51 host_item *h, *next_h, *prev;
52
53 /* Look up each host address. A lookup may add additional items into the chain
54 if there are multiple addresses. Hence the use of next_h to start each cycle of
55 the loop at the next original host. If any host is identified as being the local
56 host, omit it and any subsequent hosts - i.e. treat the list like an ordered
57 list of MX hosts. If the first host is the local host, act according to the
58 "self" option in the configuration. */
59
60 prev = NULL;
61 for (h = addr->host_list; h != NULL; prev = h, h = next_h)
62 {
63 uschar *canonical_name;
64 int rc, len;
65
66 next_h = h->next;
67 if (h->address != NULL) continue;
68
69 DEBUG(D_route|D_host_lookup)
70 debug_printf("finding IP address for %s\n", h->name);
71
72 /* If the name ends with "/MX", we interpret it to mean "the list of hosts
73 pointed to by MX records with this name". */
74
75 len = Ustrlen(h->name);
76 if (len > 3 && strcmpic(h->name + len - 3, US"/mx") == 0)
77 {
78 DEBUG(D_route|D_host_lookup)
79 debug_printf("doing DNS MX lookup for %s\n", h->name);
80
81 h->name[len-3] = 0;
82 rc = host_find_bydns(h,
83 ignore_target_hosts,
84 HOST_FIND_BY_MX, /* look only for MX records */
85 NULL, /* SRV service not relevant */
86 NULL, /* failing srv domains not relevant */
87 NULL, /* no special mx failing domains */
88 NULL, /* fully_qualified_name */
89 NULL); /* indicate local host removed */
90 }
91
92 /* If explicitly configured to look up by name, or if the "host name" is
93 actually an IP address, do a byname lookup. */
94
95 else if (lookup_type == lk_byname || string_is_ip_address(h->name, NULL) > 0)
96 {
97 DEBUG(D_route|D_host_lookup) debug_printf("calling host_find_byname\n");
98 rc = host_find_byname(h, ignore_target_hosts, &canonical_name, TRUE);
99 }
100
101 /* Otherwise, do a DNS lookup. If that yields "host not found", and the
102 lookup type is the default (i.e. "bydns" is not explicitly configured),
103 follow up with a byname lookup, just in case. */
104
105 else
106 {
107 BOOL removed;
108 DEBUG(D_route|D_host_lookup) debug_printf("doing DNS lookup\n");
109 rc = host_find_bydns(h, ignore_target_hosts, HOST_FIND_BY_A, NULL, NULL,
110 NULL, &canonical_name, &removed);
111 if (rc == HOST_FOUND)
112 {
113 if (removed) setflag(addr, af_local_host_removed);
114 }
115 else if (rc == HOST_FIND_FAILED)
116 {
117 if (lookup_type == lk_default)
118 {
119 DEBUG(D_route|D_host_lookup)
120 debug_printf("DNS lookup failed: trying getipnodebyname\n");
121 rc = host_find_byname(h, ignore_target_hosts, &canonical_name, TRUE);
122 }
123 }
124 }
125
126 /* Temporary failure defers, unless pass_on_timeout is set */
127
128 if (rc == HOST_FIND_AGAIN)
129 {
130 if (rblock->pass_on_timeout)
131 {
132 DEBUG(D_route)
133 debug_printf("%s router timed out and pass_on_timeout set\n",
134 rblock->name);
135 return PASS;
136 }
137 addr->message = string_sprintf("host lookup for %s did not complete "
138 "(DNS timeout?)", h->name);
139 addr->basic_errno = ERRNO_DNSDEFER;
140 return DEFER;
141 }
142
143 /* Permanent failure is controlled by host_find_failed */
144
145 if (rc == HOST_FIND_FAILED)
146 {
147 if (hff_code == hff_pass) return PASS;
148 if (hff_code == hff_decline) return DECLINE;
149
150 addr->message =
151 string_sprintf("lookup of host \"%s\" failed in %s router%s",
152 h->name, rblock->name,
153 host_find_failed_syntax? ": syntax error in name" : "");
154
155 if (hff_code == hff_defer) return DEFER;
156 if (hff_code == hff_fail) return FAIL;
157
158 addr->special_action = SPECIAL_FREEZE;
159 return DEFER;
160 }
161
162 /* A local host gets chopped, with its successors, if there are previous
163 hosts. Otherwise the self option is used. If it is set to "send", any
164 subsequent hosts that are also the local host do NOT get chopped. */
165
166 if (rc == HOST_FOUND_LOCAL && !self_send)
167 {
168 if (prev != NULL)
169 {
170 DEBUG(D_route)
171 {
172 debug_printf("Removed from host list:\n");
173 for (; h != NULL; h = h->next) debug_printf(" %s\n", h->name);
174 }
175 prev->next = NULL;
176 setflag(addr, af_local_host_removed);
177 break;
178 }
179 rc = rf_self_action(addr, h, rblock->self_code, rblock->self_rewrite,
180 rblock->self, addr_new);
181 if (rc != OK)
182 {
183 addr->host_list = NULL; /* Kill the host list for */
184 return rc; /* anything other than "send" */
185 }
186 self_send = TRUE;
187 }
188 }
189
190 return OK;
191 }
192
193 /* End of rf_lookup_hostlist.c */