1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
10 #include "rf_functions.h"
14 /* IP connection types */
20 /* Options specific to the iplookup router. */
22 optionlist iplookup_router_options[] = {
23 { "hosts", opt_stringptr,
24 (void *)(offsetof(iplookup_router_options_block, hosts)) },
25 { "optional", opt_bool,
26 (void *)(offsetof(iplookup_router_options_block, optional)) },
28 (void *)(offsetof(iplookup_router_options_block, port)) },
29 { "protocol", opt_stringptr,
30 (void *)(offsetof(iplookup_router_options_block, protocol_name)) },
31 { "query", opt_stringptr,
32 (void *)(offsetof(iplookup_router_options_block, query)) },
33 { "reroute", opt_stringptr,
34 (void *)(offsetof(iplookup_router_options_block, reroute)) },
35 { "response_pattern", opt_stringptr,
36 (void *)(offsetof(iplookup_router_options_block, response_pattern)) },
37 { "timeout", opt_time,
38 (void *)(offsetof(iplookup_router_options_block, timeout)) }
41 /* Size of the options list. An extern variable has to be used so that its
42 address can appear in the tables drtables.c. */
44 int iplookup_router_options_count =
45 sizeof(iplookup_router_options)/sizeof(optionlist);
47 /* Default private options block for the iplookup router. */
49 iplookup_router_options_block iplookup_router_option_defaults = {
51 ip_udp, /* protocol */
53 NULL, /* protocol_name */
55 NULL, /* query; NULL => local_part@domain */
56 NULL, /* response_pattern; NULL => don't apply regex */
57 NULL, /* reroute; NULL => just used returned data */
58 NULL, /* re_response_pattern; compiled pattern */
64 /*************************************************
65 * Initialization entry point *
66 *************************************************/
68 /* Called for each instance, after its options have been read, to enable
69 consistency checks to be done, or anything else that needs to be set up. */
72 iplookup_router_init(router_instance *rblock)
74 iplookup_router_options_block *ob =
75 (iplookup_router_options_block *)(rblock->options_block);
77 /* A port and a host list must be given */
80 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
81 "a port must be specified", rblock->name);
83 if (ob->hosts == NULL)
84 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
85 "a host list must be specified", rblock->name);
87 /* Translate protocol name into value */
89 if (ob->protocol_name != NULL)
91 if (Ustrcmp(ob->protocol_name, "udp") == 0) ob->protocol = ip_udp;
92 else if (Ustrcmp(ob->protocol_name, "tcp") == 0) ob->protocol = ip_tcp;
93 else log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
94 "protocol not specified as udp or tcp", rblock->name);
97 /* If a response pattern is given, compile it now to get the error early. */
99 if (ob->response_pattern != NULL)
100 ob->re_response_pattern =
101 regex_must_compile(ob->response_pattern, FALSE, TRUE);
106 /*************************************************
108 *************************************************/
110 /* See local README for interface details. This router returns:
113 . pattern or identification match on returned data failed
116 . failed to expand the query or rerouting string
117 . failed to create socket ("optional" not set)
118 . failed to find a host, failed to connect, timed out ("optional" not set)
119 . rerouting string is not in the form localpart@domain
120 . verifying the errors address caused a deferment or a big disaster such
121 as an expansion failure (rf_get_errors_address)
122 . expanding a headers_{add,remove} string caused a deferment or another
123 expansion error (rf_get_munge_headers)
126 . failed to create socket ("optional" set)
127 . failed to find a host, failed to connect, timed out ("optional" set)
130 . new address added to addr_new
134 iplookup_router_entry(
135 router_instance *rblock, /* data for this instantiation */
136 address_item *addr, /* address we are working on */
137 struct passwd *pw, /* passwd entry after check_local_user */
138 int verify, /* v_none/v_recipient/v_sender/v_expn */
139 address_item **addr_local, /* add it to this if it's local */
140 address_item **addr_remote, /* add it to this if it's remote */
141 address_item **addr_new, /* put new addresses on here */
142 address_item **addr_succeed) /* put old address here on success */
144 uschar *query = NULL;
146 uschar *hostname, *reroute, *domain, *listptr;
147 uschar host_buffer[256];
148 host_item *host = store_get(sizeof(host_item));
149 address_item *new_addr;
150 iplookup_router_options_block *ob =
151 (iplookup_router_options_block *)(rblock->options_block);
152 const pcre *re = ob->re_response_pattern;
153 int count, query_len, rc;
156 addr_local = addr_local; /* Keep picky compilers happy */
157 addr_remote = addr_remote;
158 addr_succeed = addr_succeed;
161 DEBUG(D_route) debug_printf("%s router called for %s: domain = %s\n",
162 rblock->name, addr->address, addr->domain);
164 /* Build the query string to send. If not explicitly given, a default of
165 "user@domain user@domain" is used. */
167 if (ob->query == NULL)
168 query = string_sprintf("%s@%s %s@%s", addr->local_part, addr->domain,
169 addr->local_part, addr->domain);
172 query = expand_string(ob->query);
175 addr->message = string_sprintf("%s router: failed to expand %s: %s",
176 rblock->name, ob->query, expand_string_message);
181 query_len = Ustrlen(query);
182 DEBUG(D_route) debug_printf("%s router query is \"%s\"\n", rblock->name,
183 string_printing(query));
185 /* Now connect to the required port for each of the hosts in turn, until a
186 response it received. Initialization insists on the port being set and there
187 being a host list. */
190 while ((hostname = string_nextinlist(&listptr, &sep, host_buffer,
191 sizeof(host_buffer))) != NULL)
195 DEBUG(D_route) debug_printf("calling host %s\n", hostname);
197 host->name = hostname;
198 host->address = NULL;
199 host->port = PORT_NONE;
203 if (string_is_ip_address(host->name, NULL) != 0)
204 host->address = host->name;
207 int rc = host_find_byname(host, NULL, HOST_FIND_QUALIFY_SINGLE, NULL, TRUE);
208 if (rc == HOST_FIND_FAILED || rc == HOST_FIND_AGAIN) continue;
211 /* Loop for possible multiple IP addresses for the given name. */
213 for (h = host; h != NULL; h = h->next)
215 int host_af, query_socket;
217 /* Skip any hosts for which we have no address */
219 if (h->address == NULL) continue;
221 /* Create a socket, for UDP or TCP, as configured. IPv6 addresses are
222 detected by checking for a colon in the address. */
224 host_af = (Ustrchr(h->address, ':') != NULL)? AF_INET6 : AF_INET;
225 query_socket = ip_socket((ob->protocol == ip_udp)? SOCK_DGRAM:SOCK_STREAM,
227 if (query_socket < 0)
229 if (ob->optional) return PASS;
230 addr->message = string_sprintf("failed to create socket in %s router",
235 /* Connect to the remote host, under a timeout. In fact, timeouts can occur
236 here only for TCP calls; for a UDP socket, "connect" always works (the
237 router will timeout later on the read call). */
239 if (ip_connect(query_socket, host_af, h->address,ob->port, ob->timeout) < 0)
243 debug_printf("connection to %s failed: %s\n", h->address,
248 /* Send the query. If it fails, just continue with the next address. */
250 if (send(query_socket, query, query_len, 0) < 0)
252 DEBUG(D_route) debug_printf("send to %s failed\n", h->address);
253 (void)close(query_socket);
257 /* Read the response and close the socket. If the read fails, try the
260 count = ip_recv(query_socket, reply, sizeof(reply) - 1, ob->timeout);
261 (void)close(query_socket);
264 DEBUG(D_route) debug_printf("%s from %s\n", (errno == ETIMEDOUT)?
265 "timed out" : "recv failed", h->address);
270 /* Success; break the loop */
273 DEBUG(D_route) debug_printf("%s router received \"%s\" from %s\n",
274 rblock->name, string_printing(reply), h->address);
278 /* If h == NULL we have tried all the IP addresses and failed on all of them,
279 so we must continue to try more host names. Otherwise we have succeeded. */
281 if (h != NULL) break;
285 /* If hostname is NULL, we have failed to find any host, or failed to
286 connect to any of the IP addresses, or timed out while reading or writing to
287 those we have connected to. In all cases, we must pass if optional and
290 if (hostname == NULL)
292 DEBUG(D_route) debug_printf("%s router failed to get anything\n", rblock->name);
293 if (ob->optional) return PASS;
294 addr->message = string_sprintf("%s router: failed to communicate with any "
295 "host", rblock->name);
300 /* If a response pattern was supplied, match the returned string against it. A
301 failure to match causes the router to decline. After a successful match, the
302 numerical variables for expanding the rerouted address are set up. */
306 if (!regex_match_and_setup(re, reply, 0, -1))
308 DEBUG(D_route) debug_printf("%s router: %s failed to match response %s\n",
309 rblock->name, ob->response_pattern, reply);
315 /* If no response pattern was supplied, set up $0 as the response up to the
316 first white space (if any). Also, if no query was specified, check that what
317 follows the white space matches user@domain. */
322 while (reply[n] != 0 && !isspace(reply[n])) n++;
324 expand_nstring[0] = reply;
325 expand_nlength[0] = n;
327 if (ob->query == NULL)
330 while (isspace(reply[nn])) nn++;
331 if (Ustrcmp(query + query_len/2 + 1, reply+nn) != 0)
333 DEBUG(D_route) debug_printf("%s router: failed to match identification "
334 "in response %s\n", rblock->name, reply);
339 reply[n] = 0; /* Terminate for the default case */
342 /* If an explicit rerouting string is specified, expand it. Otherwise, use
343 what was sent back verbatim. */
345 if (ob->reroute != NULL)
347 reroute = expand_string(ob->reroute);
351 addr->message = string_sprintf("%s router: failed to expand %s: %s",
352 rblock->name, ob->reroute, expand_string_message);
356 else reroute = reply;
358 /* We should now have a new address in the form user@domain. */
360 domain = Ustrchr(reroute, '@');
363 log_write(0, LOG_MAIN, "%s router: reroute string %s is not of the form "
364 "user@domain", rblock->name, reroute);
365 addr->message = string_sprintf("%s router: reroute string %s is not of the "
366 "form user@domain", rblock->name, reroute);
370 /* Create a child address with the old one as parent. Put the new address on
371 the chain of new addressess. */
373 new_addr = deliver_make_addr(reroute, TRUE);
374 new_addr->parent = addr;
376 copyflag(new_addr, addr, af_propagate);
377 new_addr->p = addr->p;
379 if (addr->child_count == SHRT_MAX)
380 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
381 "child addresses for <%s>", rblock->name, SHRT_MAX, addr->address);
383 new_addr->next = *addr_new;
384 *addr_new = new_addr;
386 /* Set up the errors address, if any, and the additional and removeable headers
387 for this new address. */
389 rc = rf_get_errors_address(addr, rblock, verify, &(new_addr->p.errors_address));
390 if (rc != OK) return rc;
392 rc = rf_get_munge_headers(addr, rblock, &(new_addr->p.extra_headers),
393 &(new_addr->p.remove_headers));
394 if (rc != OK) return rc;
399 /* End of routers/iplookup.c */