1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* See the file NOTICE for conditions of use and distribution. */
9 #include "rf_functions.h"
10 #include "queryprogram.h"
14 /* Options specific to the queryprogram router. */
16 optionlist queryprogram_router_options[] = {
17 { "*expand_command_group", opt_bool | opt_hidden,
18 (void *)(offsetof(queryprogram_router_options_block, expand_cmd_gid)) },
19 { "*expand_command_user", opt_bool | opt_hidden,
20 (void *)(offsetof(queryprogram_router_options_block, expand_cmd_uid)) },
21 { "*set_command_group", opt_bool | opt_hidden,
22 (void *)(offsetof(queryprogram_router_options_block, cmd_gid_set)) },
23 { "*set_command_user", opt_bool | opt_hidden,
24 (void *)(offsetof(queryprogram_router_options_block, cmd_uid_set)) },
25 { "command", opt_stringptr,
26 (void *)(offsetof(queryprogram_router_options_block, command)) },
27 { "command_group",opt_expand_gid,
28 (void *)(offsetof(queryprogram_router_options_block, cmd_gid)) },
29 { "command_user", opt_expand_uid,
30 (void *)(offsetof(queryprogram_router_options_block, cmd_uid)) },
31 { "current_directory", opt_stringptr,
32 (void *)(offsetof(queryprogram_router_options_block, current_directory)) },
33 { "timeout", opt_time,
34 (void *)(offsetof(queryprogram_router_options_block, timeout)) }
37 /* Size of the options list. An extern variable has to be used so that its
38 address can appear in the tables drtables.c. */
40 int queryprogram_router_options_count =
41 sizeof(queryprogram_router_options)/sizeof(optionlist);
43 /* Default private options block for the queryprogram router. */
45 queryprogram_router_options_block queryprogram_router_option_defaults = {
48 (uid_t)(-1), /* cmd_uid */
49 (gid_t)(-1), /* cmd_gid */
50 FALSE, /* cmd_uid_set */
51 FALSE, /* cmd_gid_set */
52 US"/", /* current_directory */
53 NULL, /* expand_cmd_gid */
54 NULL /* expand_cmd_uid */
59 /*************************************************
60 * Initialization entry point *
61 *************************************************/
63 /* Called for each instance, after its options have been read, to enable
64 consistency checks to be done, or anything else that needs to be set up. */
67 queryprogram_router_init(router_instance *rblock)
69 queryprogram_router_options_block *ob =
70 (queryprogram_router_options_block *)(rblock->options_block);
72 /* A command must be given */
74 if (ob->command == NULL)
75 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
76 "a command specification is required", rblock->name);
78 /* A uid/gid must be supplied */
80 if (!ob->cmd_uid_set && ob->expand_cmd_uid == NULL)
81 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
82 "command_user must be specified", rblock->name);
87 /*************************************************
88 * Process a set of generated new addresses *
89 *************************************************/
91 /* This function sets up a set of newly generated child addresses and puts them
92 on the new address chain.
96 addr_new new address chain
98 generated list of generated addresses
99 addr_prop the propagated data block, containing errors_to,
100 header change stuff, and address_data
106 add_generated(router_instance *rblock, address_item **addr_new,
107 address_item *addr, address_item *generated,
108 address_item_propagated *addr_prop)
110 while (generated != NULL)
112 address_item *next = generated;
113 generated = next->next;
116 orflag(next, addr, af_propagate);
117 next->prop = *addr_prop;
118 next->start_router = rblock->redirect_router;
120 next->next = *addr_new;
123 if (addr->child_count == SHRT_MAX)
124 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
125 "child addresses for <%s>", rblock->name, SHRT_MAX, addr->address);
129 debug_printf("%s router generated %s\n", rblock->name, next->address);
136 /*************************************************
138 *************************************************/
140 /* See local README for interface details. This router returns:
148 . timeout of host lookup and pass_on_timeout set
152 . verifying the errors address caused a deferment or a big disaster such
153 as an expansion failure (rf_get_errors_address)
154 . expanding a headers_{add,remove} string caused a deferment or another
155 expansion error (rf_get_munge_headers)
156 . a problem in rf_get_transport: no transport when one is needed;
157 failed to expand dynamic transport; failed to find dynamic transport
159 . problem looking up host (rf_lookup_hostlist)
160 . self = DEFER or FREEZE
161 . failure to set up uid/gid for running the command
162 . failure of transport_set_up_command: too many arguments, expansion fail
163 . failure to create child process
164 . child process crashed or timed out or didn't return data
166 . DEFER or FREEZE returned
167 . problem in redirection data
168 . unknown transport name or trouble expanding router transport
176 . address added to addr_local or addr_remote for delivery
177 . new addresses added to addr_new
181 queryprogram_router_entry(
182 router_instance *rblock, /* data for this instantiation */
183 address_item *addr, /* address we are working on */
184 struct passwd *pw, /* passwd entry after check_local_user */
185 int verify, /* v_none/v_recipient/v_sender/v_expn */
186 address_item **addr_local, /* add it to this if it's local */
187 address_item **addr_remote, /* add it to this if it's remote */
188 address_item **addr_new, /* put new addresses on here */
189 address_item **addr_succeed) /* put old address here on success */
191 int fd_in, fd_out, len, rc;
193 struct passwd *upw = NULL;
195 const uschar **argvptr;
196 uschar *rword, *rdata, *s;
197 address_item_propagated addr_prop;
198 queryprogram_router_options_block *ob =
199 (queryprogram_router_options_block *)(rblock->options_block);
200 uschar *current_directory = ob->current_directory;
202 uid_t curr_uid = getuid();
203 gid_t curr_gid = getgid();
204 uid_t uid = ob->cmd_uid;
205 gid_t gid = ob->cmd_gid;
209 DEBUG(D_route) debug_printf("%s router called for %s: domain = %s\n",
210 rblock->name, addr->address, addr->domain);
212 ugid.uid_set = ugid.gid_set = FALSE;
214 /* Set up the propagated data block with the current address_data and the
215 errors address and extra header stuff. */
217 addr_prop.address_data = deliver_address_data;
219 rc = rf_get_errors_address(addr, rblock, verify, &addr_prop.errors_address);
220 if (rc != OK) return rc;
222 rc = rf_get_munge_headers(addr, rblock, &addr_prop.extra_headers,
223 &addr_prop.remove_headers);
224 if (rc != OK) return rc;
226 #ifdef EXPERIMENTAL_SRS
227 addr_prop.srs_sender = NULL;
230 /* Get the fixed or expanded uid under which the command is to run
231 (initialization ensures that one or the other is set). */
233 if (!ob->cmd_uid_set)
235 if (!route_find_expanded_user(ob->expand_cmd_uid, rblock->name, US"router",
236 &upw, &uid, &(addr->message)))
240 /* Get the fixed or expanded gid, or take the gid from the passwd entry. */
242 if (!ob->cmd_gid_set)
244 if (ob->expand_cmd_gid != NULL)
246 if (route_find_expanded_group(ob->expand_cmd_gid, rblock->name,
247 US"router", &gid, &(addr->message)))
250 else if (upw != NULL)
256 addr->message = string_sprintf("command_user set without command_group "
257 "for %s router", rblock->name);
262 DEBUG(D_route) debug_printf("requires uid=%ld gid=%ld current_directory=%s\n",
263 (long int)uid, (long int)gid, current_directory);
265 /* If we are not running as root, we will not be able to change uid/gid. */
267 if (curr_uid != root_uid && (uid != curr_uid || gid != curr_gid))
271 debug_printf("not running as root: cannot change uid/gid\n");
272 debug_printf("subprocess will run with uid=%ld gid=%ld\n",
273 (long int)curr_uid, (long int)curr_gid);
278 /* Set up the command to run */
280 if (!transport_set_up_command(&argvptr, /* anchor for arg list */
281 ob->command, /* raw command */
282 TRUE, /* expand the arguments */
283 0, /* not relevant when... */
284 NULL, /* no transporting address */
285 US"queryprogram router", /* for error messages */
286 &(addr->message))) /* where to put error message */
291 /* Create the child process, making it a group leader. */
293 pid = child_open_uid(argvptr, NULL, 0077, puid, pgid, &fd_in, &fd_out,
294 current_directory, TRUE);
298 addr->message = string_sprintf("%s router couldn't create child process: %s",
299 rblock->name, strerror(errno));
303 /* Nothing is written to the standard input. */
307 /* Wait for the process to finish, applying the timeout, and inspect its return
310 if ((rc = child_close(pid, ob->timeout)) != 0)
313 addr->message = string_sprintf("%s router: command returned non-zero "
314 "code %d", rblock->name, rc);
318 addr->message = string_sprintf("%s router: command timed out",
320 killpg(pid, SIGKILL); /* Kill the whole process group */
324 addr->message = string_sprintf("%s router: wait() failed: %s",
325 rblock->name, strerror(errno));
328 addr->message = string_sprintf("%s router: command killed by signal %d",
334 /* Read the pipe to get the command's output, and then close it. */
336 len = read(fd_out, buffer, sizeof(buffer) - 1);
339 /* Failure to return any data is an error. */
343 addr->message = string_sprintf("%s router: command failed to return data",
348 /* Get rid of leading and trailing white space, and pick off the first word of
351 while (len > 0 && isspace(buffer[len-1])) len--;
354 DEBUG(D_route) debug_printf("command wrote: %s\n", buffer);
357 while (isspace(*rword)) rword++;
359 while (*rdata != 0 && !isspace(*rdata)) rdata++;
360 if (*rdata != 0) *rdata++ = 0;
362 /* The word must be a known yield name. If it is "REDIRECT", the rest of the
363 line is redirection data, as for a .forward file. It may not contain filter
364 data, and it may not contain anything other than addresses (no files, no pipes,
367 if (strcmpic(rword, US"REDIRECT") == 0)
370 redirect_block redirect;
371 address_item *generated = NULL;
373 redirect.string = rdata;
374 redirect.isfile = FALSE;
376 rc = rda_interpret(&redirect, /* redirection data */
377 RDO_BLACKHOLE | /* forbid :blackhole: */
378 RDO_FAIL | /* forbid :fail: */
379 RDO_INCLUDE | /* forbid :include: */
380 RDO_REWRITE, /* rewrite generated addresses */
381 NULL, /* :include: directory not relevant */
382 NULL, /* sieve vacation directory not relevant */
383 NULL, /* sieve enotify mailto owner not relevant */
384 NULL, /* sieve useraddress not relevant */
385 NULL, /* sieve subaddress not relevant */
386 &ugid, /* uid/gid (but not set) */
387 &generated, /* where to hang the results */
388 &(addr->message), /* where to put messages */
389 NULL, /* don't skip syntax errors */
390 &filtertype, /* not used; will always be FILTER_FORWARD */
391 string_sprintf("%s router", rblock->name));
395 /* FF_DEFER and FF_FAIL can arise only as a result of explicit commands.
396 If a configured message was supplied, allow it to be included in an SMTP
397 response after verifying. */
400 if (addr->message == NULL) addr->message = US"forced defer";
401 else addr->user_message = addr->message;
405 add_generated(rblock, addr_new, addr, generated, &addr_prop);
406 if (addr->message == NULL) addr->message = US"forced rejection";
407 else addr->user_message = addr->message;
413 case FF_NOTDELIVERED: /* an empty redirection list is bad */
414 addr->message = US"no addresses supplied";
419 addr->basic_errno = ERRNO_BADREDIRECT;
420 addr->message = string_sprintf("error in redirect data: %s", addr->message);
424 /* Handle the generated addresses, if any. */
426 add_generated(rblock, addr_new, addr, generated, &addr_prop);
428 /* Put the original address onto the succeed queue so that any retry items
429 that get attached to it get processed. */
431 addr->next = *addr_succeed;
432 *addr_succeed = addr;
437 /* Handle other returns that are not ACCEPT */
439 if (strcmpic(rword, US"accept") != 0)
441 if (strcmpic(rword, US"decline") == 0) return DECLINE;
442 if (strcmpic(rword, US"pass") == 0) return PASS;
443 addr->message = string_copy(rdata); /* data is a message */
444 if (strcmpic(rword, US"fail") == 0)
446 setflag(addr, af_pass_message);
449 if (strcmpic(rword, US"freeze") == 0) addr->special_action = SPECIAL_FREEZE;
450 else if (strcmpic(rword, US"defer") != 0)
452 addr->message = string_sprintf("bad command yield: %s %s", rword, rdata);
453 log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
458 /* The command yielded "ACCEPT". The rest of the string is a number of keyed
459 fields from which we can fish out values using the "extract" expansion
460 function. To use this feature, we must put the string into the $value variable,
461 i.e. set lookup_value. */
463 lookup_value = rdata;
464 s = expand_string(US"${extract{data}{$value}}");
465 if (*s != 0) addr_prop.address_data = string_copy(s);
467 s = expand_string(US"${extract{transport}{$value}}");
470 /* If we found a transport name, find the actual transport */
474 transport_instance *transport;
475 for (transport = transports; transport != NULL; transport = transport->next)
476 if (Ustrcmp(transport->name, s) == 0) break;
477 if (transport == NULL)
479 addr->message = string_sprintf("unknown transport name %s yielded by "
481 log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
484 addr->transport = transport;
487 /* No transport given; get the transport from the router configuration. It may
488 be fixed or expanded, but there will be an error if it is unset, requested by
489 the last argument not being NULL. */
493 if (!rf_get_transport(rblock->transport_name, &(rblock->transport), addr,
494 rblock->name, US"transport"))
496 addr->transport = rblock->transport;
499 /* See if a host list is given, and if so, look up the addresses. */
501 lookup_value = rdata;
502 s = expand_string(US"${extract{hosts}{$value}}");
506 int lookup_type = lk_default;
507 uschar *ss = expand_string(US"${extract{lookup}{$value}}");
512 if (Ustrcmp(ss, "byname") == 0) lookup_type = lk_byname;
513 else if (Ustrcmp(ss, "bydns") == 0) lookup_type = lk_bydns;
516 addr->message = string_sprintf("bad lookup type \"%s\" yielded by "
518 log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
523 host_build_hostlist(&(addr->host_list), s, FALSE); /* pro tem no randomize */
525 rc = rf_lookup_hostlist(rblock, addr, rblock->ignore_target_hosts,
526 lookup_type, hff_defer, addr_new);
527 if (rc != OK) return rc;
531 /* Put the errors address, extra headers, and address_data into this address */
533 addr->prop = addr_prop;
535 /* Queue the address for local or remote delivery. */
537 return rf_queue_add(addr, addr_local, addr_remote, rblock, pw)?
541 /* End of routers/queryprogram.c */