Make dnssec_request_domains/dnssec_require_domains generic
[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 - 2009 */
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 prev = NULL;
65 for (h = addr->host_list; h != NULL; h = next_h)
66   {
67   const uschar *canonical_name;
68   int rc, len, port;
69
70   next_h = h->next;
71   if (h->address != NULL) { prev = h; continue; }
72
73   DEBUG(D_route|D_host_lookup)
74     debug_printf("finding IP address for %s\n", h->name);
75
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
81   /* If the name ends with "/MX", we interpret it to mean "the list of hosts
82   pointed to by MX records with this name". */
83
84   len = Ustrlen(h->name);
85   if (len > 3 && strcmpic(h->name + len - 3, US"/mx") == 0)
86     {
87     DEBUG(D_route|D_host_lookup)
88       debug_printf("doing DNS MX lookup for %s\n", h->name);
89
90     h->name = string_copyn(h->name, len - 3);
91     rc = host_find_bydns(h,
92         ignore_target_hosts,
93         HOST_FIND_BY_MX,                /* look only for MX records */
94         NULL,                           /* SRV service not relevant */
95         NULL,                           /* failing srv domains not relevant */
96         NULL,                           /* no special mx failing domains */
97         rblock->dnssec_request_domains,         /* no dnssec request XXX ? */
98         rblock->dnssec_require_domains,         /* no dnssec require XXX ? */
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
106   else if (lookup_type == lk_byname || string_is_ip_address(h->name, NULL) != 0)
107     {
108     DEBUG(D_route|D_host_lookup) debug_printf("calling host_find_byname\n");
109     rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
110       &canonical_name, TRUE);
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,
123       rblock->dnssec_request_domains,         /* no dnssec request XXX ? */
124       rblock->dnssec_require_domains,         /* no dnssec require XXX ? */
125       &canonical_name, &removed);
126     if (rc == HOST_FOUND)
127       {
128       if (removed) setflag(addr, af_local_host_removed);
129       }
130     else if (rc == HOST_FIND_FAILED)
131       {
132       if (lookup_type == lk_default)
133         {
134         DEBUG(D_route|D_host_lookup)
135           debug_printf("DNS lookup failed: trying getipnodebyname\n");
136         rc = host_find_byname(h, ignore_target_hosts, HOST_FIND_QUALIFY_SINGLE,
137           &canonical_name, TRUE);
138         }
139       }
140     }
141
142   /* Temporary failure defers, unless pass_on_timeout is set */
143
144   if (rc == HOST_FIND_AGAIN)
145     {
146     if (rblock->pass_on_timeout)
147       {
148       DEBUG(D_route)
149         debug_printf("%s router timed out and pass_on_timeout set\n",
150           rblock->name);
151       return PASS;
152       }
153     addr->message = string_sprintf("host lookup for %s did not complete "
154       "(DNS timeout?)", h->name);
155     addr->basic_errno = ERRNO_DNSDEFER;
156     return DEFER;
157     }
158
159   /* Permanent failure is controlled by host_find_failed */
160
161   if (rc == HOST_FIND_FAILED)
162     {
163     if (hff_code == hff_ignore)
164       {
165       if (prev == NULL) addr->host_list = next_h; else prev->next = next_h;
166       continue;   /* With the next host, leave prev unchanged */
167       }
168
169     if (hff_code == hff_pass) return PASS;
170     if (hff_code == hff_decline) return DECLINE;
171
172     addr->basic_errno = ERRNO_UNKNOWNHOST;
173     addr->message =
174       string_sprintf("lookup of host \"%s\" failed in %s router%s",
175         h->name, rblock->name,
176         host_find_failed_syntax? ": syntax error in name" : "");
177
178     if (hff_code == hff_defer) return DEFER;
179     if (hff_code == hff_fail) return FAIL;
180
181     addr->special_action = SPECIAL_FREEZE;
182     return DEFER;
183     }
184
185   /* Deal with a port setting */
186
187   if (port != PORT_NONE)
188     {
189     host_item *hh;
190     for (hh = h; hh != next_h; hh = hh->next) hh->port = port;
191     }
192
193   /* A local host gets chopped, with its successors, if there are previous
194   hosts. Otherwise the self option is used. If it is set to "send", any
195   subsequent hosts that are also the local host do NOT get chopped. */
196
197   if (rc == HOST_FOUND_LOCAL && !self_send)
198     {
199     if (prev != NULL)
200       {
201       DEBUG(D_route)
202         {
203         debug_printf("Removed from host list:\n");
204         for (; h != NULL; h = h->next) debug_printf("  %s\n", h->name);
205         }
206       prev->next = NULL;
207       setflag(addr, af_local_host_removed);
208       break;
209       }
210     rc = rf_self_action(addr, h, rblock->self_code, rblock->self_rewrite,
211       rblock->self, addr_new);
212     if (rc != OK)
213       {
214       addr->host_list = NULL;   /* Kill the host list for */
215       return rc;                /* anything other than "send" */
216       }
217     self_send = TRUE;
218     }
219
220   /* Ensure that prev is the host before next_h; this will not be h if a lookup
221   found multiple addresses or multiple MX records. */
222
223   prev = h;
224   while (prev->next != next_h) prev = prev->next;
225   }
226
227 return OK;
228 }
229
230 /* End of rf_lookup_hostlist.c */