1 /* $Cambridge: exim/src/src/routers/queryprogram.c,v 1.12 2009/11/16 19:50:38 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 #include "rf_functions.h"
12 #include "queryprogram.h"
16 /* Options specific to the queryprogram router. */
18 optionlist queryprogram_router_options[] = {
19 { "*expand_command_group", opt_bool | opt_hidden,
20 (void *)(offsetof(queryprogram_router_options_block, expand_cmd_gid)) },
21 { "*expand_command_user", opt_bool | opt_hidden,
22 (void *)(offsetof(queryprogram_router_options_block, expand_cmd_uid)) },
23 { "*set_command_group", opt_bool | opt_hidden,
24 (void *)(offsetof(queryprogram_router_options_block, cmd_gid_set)) },
25 { "*set_command_user", opt_bool | opt_hidden,
26 (void *)(offsetof(queryprogram_router_options_block, cmd_uid_set)) },
27 { "command", opt_stringptr,
28 (void *)(offsetof(queryprogram_router_options_block, command)) },
29 { "command_group",opt_expand_gid,
30 (void *)(offsetof(queryprogram_router_options_block, cmd_gid)) },
31 { "command_user", opt_expand_uid,
32 (void *)(offsetof(queryprogram_router_options_block, cmd_uid)) },
33 { "current_directory", opt_stringptr,
34 (void *)(offsetof(queryprogram_router_options_block, current_directory)) },
35 { "timeout", opt_time,
36 (void *)(offsetof(queryprogram_router_options_block, timeout)) }
39 /* Size of the options list. An extern variable has to be used so that its
40 address can appear in the tables drtables.c. */
42 int queryprogram_router_options_count =
43 sizeof(queryprogram_router_options)/sizeof(optionlist);
45 /* Default private options block for the queryprogram router. */
47 queryprogram_router_options_block queryprogram_router_option_defaults = {
50 (uid_t)(-1), /* cmd_uid */
51 (gid_t)(-1), /* cmd_gid */
52 FALSE, /* cmd_uid_set */
53 FALSE, /* cmd_gid_set */
54 US"/", /* current_directory */
55 NULL, /* expand_cmd_gid */
56 NULL /* expand_cmd_uid */
61 /*************************************************
62 * Initialization entry point *
63 *************************************************/
65 /* Called for each instance, after its options have been read, to enable
66 consistency checks to be done, or anything else that needs to be set up. */
69 queryprogram_router_init(router_instance *rblock)
71 queryprogram_router_options_block *ob =
72 (queryprogram_router_options_block *)(rblock->options_block);
74 /* A command must be given */
76 if (ob->command == NULL)
77 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
78 "a command specification is required", rblock->name);
80 /* A uid/gid must be supplied */
82 if (!ob->cmd_uid_set && ob->expand_cmd_uid == NULL)
83 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
84 "command_user must be specified", rblock->name);
89 /*************************************************
90 * Process a set of generated new addresses *
91 *************************************************/
93 /* This function sets up a set of newly generated child addresses and puts them
94 on the new address chain.
98 addr_new new address chain
100 generated list of generated addresses
101 addr_prop the propagated data block, containing errors_to,
102 header change stuff, and address_data
108 add_generated(router_instance *rblock, address_item **addr_new,
109 address_item *addr, address_item *generated,
110 address_item_propagated *addr_prop)
112 while (generated != NULL)
114 address_item *next = generated;
115 generated = next->next;
118 orflag(next, addr, af_propagate);
119 next->p = *addr_prop;
120 next->start_router = rblock->redirect_router;
122 next->next = *addr_new;
125 if (addr->child_count == SHRT_MAX)
126 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
127 "child addresses for <%s>", rblock->name, SHRT_MAX, addr->address);
131 debug_printf("%s router generated %s\n", rblock->name, next->address);
138 /*************************************************
140 *************************************************/
142 /* See local README for interface details. This router returns:
150 . timeout of host lookup and pass_on_timeout set
154 . verifying the errors address caused a deferment or a big disaster such
155 as an expansion failure (rf_get_errors_address)
156 . expanding a headers_{add,remove} string caused a deferment or another
157 expansion error (rf_get_munge_headers)
158 . a problem in rf_get_transport: no transport when one is needed;
159 failed to expand dynamic transport; failed to find dynamic transport
161 . problem looking up host (rf_lookup_hostlist)
162 . self = DEFER or FREEZE
163 . failure to set up uid/gid for running the command
164 . failure of transport_set_up_command: too many arguments, expansion fail
165 . failure to create child process
166 . child process crashed or timed out or didn't return data
168 . DEFER or FREEZE returned
169 . problem in redirection data
170 . unknown transport name or trouble expanding router transport
178 . address added to addr_local or addr_remote for delivery
179 . new addresses added to addr_new
183 queryprogram_router_entry(
184 router_instance *rblock, /* data for this instantiation */
185 address_item *addr, /* address we are working on */
186 struct passwd *pw, /* passwd entry after check_local_user */
187 int verify, /* v_none/v_recipient/v_sender/v_expn */
188 address_item **addr_local, /* add it to this if it's local */
189 address_item **addr_remote, /* add it to this if it's remote */
190 address_item **addr_new, /* put new addresses on here */
191 address_item **addr_succeed) /* put old address here on success */
193 int fd_in, fd_out, len, rc;
195 struct passwd *upw = NULL;
198 uschar *rword, *rdata, *s;
199 address_item_propagated addr_prop;
200 queryprogram_router_options_block *ob =
201 (queryprogram_router_options_block *)(rblock->options_block);
202 uschar *current_directory = ob->current_directory;
204 uid_t curr_uid = getuid();
205 gid_t curr_gid = getgid();
206 uid_t uid = ob->cmd_uid;
207 gid_t gid = ob->cmd_gid;
211 DEBUG(D_route) debug_printf("%s router called for %s: domain = %s\n",
212 rblock->name, addr->address, addr->domain);
214 ugid.uid_set = ugid.gid_set = FALSE;
216 /* Set up the propagated data block with the current address_data and the
217 errors address and extra header stuff. */
219 addr_prop.address_data = deliver_address_data;
221 rc = rf_get_errors_address(addr, rblock, verify, &(addr_prop.errors_address));
222 if (rc != OK) return rc;
224 rc = rf_get_munge_headers(addr, rblock, &(addr_prop.extra_headers),
225 &(addr_prop.remove_headers));
226 if (rc != OK) return rc;
228 /* Get the fixed or expanded uid under which the command is to run
229 (initialization ensures that one or the other is set). */
231 if (!ob->cmd_uid_set)
233 if (!route_find_expanded_user(ob->expand_cmd_uid, rblock->name, US"router",
234 &upw, &uid, &(addr->message)))
238 /* Get the fixed or expanded gid, or take the gid from the passwd entry. */
240 if (!ob->cmd_gid_set)
242 if (ob->expand_cmd_gid != NULL)
244 if (route_find_expanded_group(ob->expand_cmd_gid, rblock->name,
245 US"router", &gid, &(addr->message)))
248 else if (upw != NULL)
254 addr->message = string_sprintf("command_user set without command_group "
255 "for %s router", rblock->name);
260 DEBUG(D_route) debug_printf("requires uid=%ld gid=%ld current_directory=%s\n",
261 (long int)uid, (long int)gid, current_directory);
263 /* If we are not running as root, we will not be able to change uid/gid. */
265 if (curr_uid != root_uid && (uid != curr_uid || gid != curr_gid))
269 debug_printf("not running as root: cannot change uid/gid\n");
270 debug_printf("subprocess will run with uid=%ld gid=%ld\n",
271 (long int)curr_uid, (long int)curr_gid);
276 /* Set up the command to run */
278 if (!transport_set_up_command(&argvptr, /* anchor for arg list */
279 ob->command, /* raw command */
280 TRUE, /* expand the arguments */
281 0, /* not relevant when... */
282 NULL, /* no transporting address */
283 US"queryprogram router", /* for error messages */
284 &(addr->message))) /* where to put error message */
289 /* Create the child process, making it a group leader. */
291 pid = child_open_uid(argvptr, NULL, 0077, puid, pgid, &fd_in, &fd_out,
292 current_directory, TRUE);
296 addr->message = string_sprintf("%s router couldn't create child process: %s",
297 rblock->name, strerror(errno));
301 /* Nothing is written to the standard input. */
305 /* Wait for the process to finish, applying the timeout, and inspect its return
308 if ((rc = child_close(pid, ob->timeout)) != 0)
311 addr->message = string_sprintf("%s router: command returned non-zero "
312 "code %d", rblock->name, rc);
316 addr->message = string_sprintf("%s router: command timed out",
318 killpg(pid, SIGKILL); /* Kill the whole process group */
322 addr->message = string_sprintf("%s router: wait() failed: %s",
323 rblock->name, strerror(errno));
326 addr->message = string_sprintf("%s router: command killed by signal %d",
332 /* Read the pipe to get the command's output, and then close it. */
334 len = read(fd_out, buffer, sizeof(buffer) - 1);
337 /* Failure to return any data is an error. */
341 addr->message = string_sprintf("%s router: command failed to return data",
346 /* Get rid of leading and trailing white space, and pick off the first word of
349 while (len > 0 && isspace(buffer[len-1])) len--;
352 DEBUG(D_route) debug_printf("command wrote: %s\n", buffer);
355 while (isspace(*rword)) rword++;
357 while (*rdata != 0 && !isspace(*rdata)) rdata++;
358 if (*rdata != 0) *rdata++ = 0;
360 /* The word must be a known yield name. If it is "REDIRECT", the rest of the
361 line is redirection data, as for a .forward file. It may not contain filter
362 data, and it may not contain anything other than addresses (no files, no pipes,
365 if (strcmpic(rword, US"REDIRECT") == 0)
368 redirect_block redirect;
369 address_item *generated = NULL;
371 redirect.string = rdata;
372 redirect.isfile = FALSE;
374 rc = rda_interpret(&redirect, /* redirection data */
375 RDO_BLACKHOLE | /* forbid :blackhole: */
376 RDO_FAIL | /* forbid :fail: */
377 RDO_INCLUDE | /* forbid :include: */
378 RDO_REWRITE, /* rewrite generated addresses */
379 NULL, /* :include: directory not relevant */
380 NULL, /* sieve vacation directory not relevant */
381 NULL, /* sieve enotify mailto owner not relevant */
382 NULL, /* sieve useraddress not relevant */
383 NULL, /* sieve subaddress not relevant */
384 &ugid, /* uid/gid (but not set) */
385 &generated, /* where to hang the results */
386 &(addr->message), /* where to put messages */
387 NULL, /* don't skip syntax errors */
388 &filtertype, /* not used; will always be FILTER_FORWARD */
389 string_sprintf("%s router", rblock->name));
393 /* FF_DEFER and FF_FAIL can arise only as a result of explicit commands.
394 If a configured message was supplied, allow it to be included in an SMTP
395 response after verifying. */
398 if (addr->message == NULL) addr->message = US"forced defer";
399 else addr->user_message = addr->message;
403 add_generated(rblock, addr_new, addr, generated, &addr_prop);
404 if (addr->message == NULL) addr->message = US"forced rejection";
405 else addr->user_message = addr->message;
411 case FF_NOTDELIVERED: /* an empty redirection list is bad */
412 addr->message = US"no addresses supplied";
417 addr->basic_errno = ERRNO_BADREDIRECT;
418 addr->message = string_sprintf("error in redirect data: %s", addr->message);
422 /* Handle the generated addresses, if any. */
424 add_generated(rblock, addr_new, addr, generated, &addr_prop);
426 /* Put the original address onto the succeed queue so that any retry items
427 that get attached to it get processed. */
429 addr->next = *addr_succeed;
430 *addr_succeed = addr;
435 /* Handle other returns that are not ACCEPT */
437 if (strcmpic(rword, US"accept") != 0)
439 if (strcmpic(rword, US"decline") == 0) return DECLINE;
440 if (strcmpic(rword, US"pass") == 0) return PASS;
441 addr->message = string_copy(rdata); /* data is a message */
442 if (strcmpic(rword, US"fail") == 0)
444 setflag(addr, af_pass_message);
447 if (strcmpic(rword, US"freeze") == 0) addr->special_action = SPECIAL_FREEZE;
448 else if (strcmpic(rword, US"defer") != 0)
450 addr->message = string_sprintf("bad command yield: %s %s", rword, rdata);
451 log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
456 /* The command yielded "ACCEPT". The rest of the string is a number of keyed
457 fields from which we can fish out values using the "extract" expansion
458 function. To use this feature, we must put the string into the $value variable,
459 i.e. set lookup_value. */
461 lookup_value = rdata;
462 s = expand_string(US"${extract{data}{$value}}");
463 if (*s != 0) addr_prop.address_data = string_copy(s);
465 s = expand_string(US"${extract{transport}{$value}}");
468 /* If we found a transport name, find the actual transport */
472 transport_instance *transport;
473 for (transport = transports; transport != NULL; transport = transport->next)
474 if (Ustrcmp(transport->name, s) == 0) break;
475 if (transport == NULL)
477 addr->message = string_sprintf("unknown transport name %s yielded by "
479 log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
482 addr->transport = transport;
485 /* No transport given; get the transport from the router configuration. It may
486 be fixed or expanded, but there will be an error if it is unset, requested by
487 the last argument not being NULL. */
491 if (!rf_get_transport(rblock->transport_name, &(rblock->transport), addr,
492 rblock->name, US"transport"))
494 addr->transport = rblock->transport;
497 /* See if a host list is given, and if so, look up the addresses. */
499 lookup_value = rdata;
500 s = expand_string(US"${extract{hosts}{$value}}");
504 int lookup_type = lk_default;
505 uschar *ss = expand_string(US"${extract{lookup}{$value}}");
510 if (Ustrcmp(ss, "byname") == 0) lookup_type = lk_byname;
511 else if (Ustrcmp(ss, "bydns") == 0) lookup_type = lk_bydns;
514 addr->message = string_sprintf("bad lookup type \"%s\" yielded by "
516 log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
521 host_build_hostlist(&(addr->host_list), s, FALSE); /* pro tem no randomize */
523 rc = rf_lookup_hostlist(rblock, addr, rblock->ignore_target_hosts,
524 lookup_type, hff_defer, addr_new);
525 if (rc != OK) return rc;
529 /* Put the errors address, extra headers, and address_data into this address */
533 /* Queue the address for local or remote delivery. */
535 return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)?
539 /* End of routers/queryprogram.c */