1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 - 2021 */
7 /* See the file NOTICE for conditions of use and distribution. */
10 #include "rf_functions.h"
11 #include "queryprogram.h"
15 /* Options specific to the queryprogram router. */
17 optionlist queryprogram_router_options[] = {
18 { "*expand_command_group", opt_bool | opt_hidden,
19 OPT_OFF(queryprogram_router_options_block, expand_cmd_gid) },
20 { "*expand_command_user", opt_bool | opt_hidden,
21 OPT_OFF(queryprogram_router_options_block, expand_cmd_uid) },
22 { "*set_command_group", opt_bool | opt_hidden,
23 OPT_OFF(queryprogram_router_options_block, cmd_gid_set) },
24 { "*set_command_user", opt_bool | opt_hidden,
25 OPT_OFF(queryprogram_router_options_block, cmd_uid_set) },
26 { "command", opt_stringptr,
27 OPT_OFF(queryprogram_router_options_block, command) },
28 { "command_group",opt_expand_gid,
29 OPT_OFF(queryprogram_router_options_block, cmd_gid) },
30 { "command_user", opt_expand_uid,
31 OPT_OFF(queryprogram_router_options_block, cmd_uid) },
32 { "current_directory", opt_stringptr,
33 OPT_OFF(queryprogram_router_options_block, current_directory) },
34 { "timeout", opt_time,
35 OPT_OFF(queryprogram_router_options_block, timeout) }
38 /* Size of the options list. An extern variable has to be used so that its
39 address can appear in the tables drtables.c. */
41 int queryprogram_router_options_count =
42 sizeof(queryprogram_router_options)/sizeof(optionlist);
48 queryprogram_router_options_block queryprogram_router_option_defaults = {0};
49 void queryprogram_router_init(router_instance *rblock) {}
50 int queryprogram_router_entry(router_instance *rblock, address_item *addr,
51 struct passwd *pw, int verify, address_item **addr_local,
52 address_item **addr_remote, address_item **addr_new,
53 address_item **addr_succeed) {return 0;}
55 #else /*!MACRO_PREDEF*/
58 /* Default private options block for the queryprogram router. */
60 queryprogram_router_options_block queryprogram_router_option_defaults = {
63 (uid_t)(-1), /* cmd_uid */
64 (gid_t)(-1), /* cmd_gid */
65 FALSE, /* cmd_uid_set */
66 FALSE, /* cmd_gid_set */
67 US"/", /* current_directory */
68 NULL, /* expand_cmd_gid */
69 NULL /* expand_cmd_uid */
74 /*************************************************
75 * Initialization entry point *
76 *************************************************/
78 /* Called for each instance, after its options have been read, to enable
79 consistency checks to be done, or anything else that needs to be set up. */
82 queryprogram_router_init(router_instance *rblock)
84 queryprogram_router_options_block *ob =
85 (queryprogram_router_options_block *)(rblock->options_block);
87 /* A command must be given */
90 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
91 "a command specification is required", rblock->name);
93 /* A uid/gid must be supplied */
95 if (!ob->cmd_uid_set && !ob->expand_cmd_uid)
96 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n "
97 "command_user must be specified", rblock->name);
102 /*************************************************
103 * Process a set of generated new addresses *
104 *************************************************/
106 /* This function sets up a set of newly generated child addresses and puts them
107 on the new address chain.
111 addr_new new address chain
112 addr original address
113 generated list of generated addresses
114 addr_prop the propagated data block, containing errors_to,
115 header change stuff, and address_data
121 add_generated(router_instance *rblock, address_item **addr_new,
122 address_item *addr, address_item *generated,
123 address_item_propagated *addr_prop)
125 while (generated != NULL)
127 BOOL ignore_error = addr->prop.ignore_error;
128 address_item *next = generated;
130 generated = next->next;
133 next->prop = *addr_prop;
134 next->prop.ignore_error = next->prop.ignore_error || ignore_error;
135 next->start_router = rblock->redirect_router;
137 next->next = *addr_new;
140 if (addr->child_count == USHRT_MAX)
141 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
142 "child addresses for <%s>", rblock->name, USHRT_MAX, addr->address);
146 debug_printf("%s router generated %s\n", rblock->name, next->address);
153 /*************************************************
155 *************************************************/
157 /* See local README for interface details. This router returns:
165 . timeout of host lookup and pass_on_timeout set
169 . verifying the errors address caused a deferment or a big disaster such
170 as an expansion failure (rf_get_errors_address)
171 . expanding a headers_{add,remove} string caused a deferment or another
172 expansion error (rf_get_munge_headers)
173 . a problem in rf_get_transport: no transport when one is needed;
174 failed to expand dynamic transport; failed to find dynamic transport
176 . problem looking up host (rf_lookup_hostlist)
177 . self = DEFER or FREEZE
178 . failure to set up uid/gid for running the command
179 . failure of transport_set_up_command: too many arguments, expansion fail
180 . failure to create child process
181 . child process crashed or timed out or didn't return data
183 . DEFER or FREEZE returned
184 . problem in redirection data
185 . unknown transport name or trouble expanding router transport
193 . address added to addr_local or addr_remote for delivery
194 . new addresses added to addr_new
198 queryprogram_router_entry(
199 router_instance *rblock, /* data for this instantiation */
200 address_item *addr, /* address we are working on */
201 struct passwd *pw, /* passwd entry after check_local_user */
202 int verify, /* v_none/v_recipient/v_sender/v_expn */
203 address_item **addr_local, /* add it to this if it's local */
204 address_item **addr_remote, /* add it to this if it's remote */
205 address_item **addr_new, /* put new addresses on here */
206 address_item **addr_succeed) /* put old address here on success */
208 int fd_in, fd_out, len, rc;
210 struct passwd *upw = NULL;
212 const uschar **argvptr;
213 uschar *rword, *rdata, *s;
214 address_item_propagated addr_prop;
215 queryprogram_router_options_block *ob =
216 (queryprogram_router_options_block *)(rblock->options_block);
217 uschar *current_directory = ob->current_directory;
219 uid_t curr_uid = getuid();
220 gid_t curr_gid = getgid();
221 uid_t uid = ob->cmd_uid;
222 gid_t gid = ob->cmd_gid;
226 DEBUG(D_route) debug_printf("%s router called for %s: domain = %s\n",
227 rblock->name, addr->address, addr->domain);
229 ugid.uid_set = ugid.gid_set = FALSE;
231 /* Set up the propagated data block with the current address_data and the
232 errors address and extra header stuff. */
234 bzero(&addr_prop, sizeof(addr_prop));
235 addr_prop.address_data = deliver_address_data;
236 tree_dup((tree_node **)&addr_prop.variables, addr->prop.variables);
238 rc = rf_get_errors_address(addr, rblock, verify, &addr_prop.errors_address);
239 if (rc != OK) return rc;
241 rc = rf_get_munge_headers(addr, rblock, &addr_prop.extra_headers,
242 &addr_prop.remove_headers);
243 if (rc != OK) return rc;
245 /* Get the fixed or expanded uid under which the command is to run
246 (initialization ensures that one or the other is set). */
248 if ( !ob->cmd_uid_set
249 && !route_find_expanded_user(ob->expand_cmd_uid, rblock->name, US"router",
250 &upw, &uid, &(addr->message)))
253 /* Get the fixed or expanded gid, or take the gid from the passwd entry. */
255 if (!ob->cmd_gid_set)
256 if (ob->expand_cmd_gid)
258 if (route_find_expanded_group(ob->expand_cmd_gid, rblock->name,
259 US"router", &gid, &(addr->message)))
266 addr->message = string_sprintf("command_user set without command_group "
267 "for %s router", rblock->name);
271 DEBUG(D_route) debug_printf("requires uid=%ld gid=%ld current_directory=%s\n",
272 (long int)uid, (long int)gid, current_directory);
274 /* If we are not running as root, we will not be able to change uid/gid. */
276 if (curr_uid != root_uid && (uid != curr_uid || gid != curr_gid))
280 debug_printf("not running as root: cannot change uid/gid\n");
281 debug_printf("subprocess will run with uid=%ld gid=%ld\n",
282 (long int)curr_uid, (long int)curr_gid);
287 /* Set up the command to run */
289 if (!transport_set_up_command(&argvptr, /* anchor for arg list */
290 ob->command, /* raw command */
291 TRUE, /* expand the arguments */
292 0, /* not relevant when... */
293 NULL, /* no transporting address */
294 US"queryprogram router", /* for error messages */
295 &addr->message)) /* where to put error message */
298 /* Create the child process, making it a group leader. */
300 if ((pid = child_open_uid(argvptr, NULL, 0077, puid, pgid, &fd_in, &fd_out,
301 current_directory, TRUE, US"queryprogram-cmd")) < 0)
303 addr->message = string_sprintf("%s router couldn't create child process: %s",
304 rblock->name, strerror(errno));
308 /* Nothing is written to the standard input. */
312 /* Wait for the process to finish, applying the timeout, and inspect its return
315 if ((rc = child_close(pid, ob->timeout)) != 0)
318 addr->message = string_sprintf("%s router: command returned non-zero "
319 "code %d", rblock->name, rc);
323 addr->message = string_sprintf("%s router: command timed out",
325 killpg(pid, SIGKILL); /* Kill the whole process group */
329 addr->message = string_sprintf("%s router: wait() failed: %s",
330 rblock->name, strerror(errno));
333 addr->message = string_sprintf("%s router: command killed by signal %d",
339 /* Read the pipe to get the command's output, and then close it. */
341 len = read(fd_out, buffer, sizeof(buffer) - 1);
344 /* Failure to return any data is an error. */
348 addr->message = string_sprintf("%s router: command failed to return data",
353 /* Get rid of leading and trailing white space, and pick off the first word of
356 while (len > 0 && isspace(buffer[len-1])) len--;
359 DEBUG(D_route) debug_printf("command wrote: %s\n", buffer);
362 while (isspace(*rword)) rword++;
364 while (*rdata && !isspace(*rdata)) rdata++;
365 if (*rdata) *rdata++ = 0;
367 /* The word must be a known yield name. If it is "REDIRECT", the rest of the
368 line is redirection data, as for a .forward file. It may not contain filter
369 data, and it may not contain anything other than addresses (no files, no pipes,
372 if (strcmpic(rword, US"REDIRECT") == 0)
375 redirect_block redirect;
376 address_item *generated = NULL;
378 redirect.string = rdata;
379 redirect.isfile = FALSE;
381 rc = rda_interpret(&redirect, /* redirection data */
382 RDO_BLACKHOLE | /* forbid :blackhole: */
383 RDO_FAIL | /* forbid :fail: */
384 RDO_INCLUDE | /* forbid :include: */
385 RDO_REWRITE, /* rewrite generated addresses */
386 NULL, /* :include: directory not relevant */
387 NULL, /* sieve vacation directory not relevant */
388 NULL, /* sieve enotify mailto owner not relevant */
389 NULL, /* sieve useraddress not relevant */
390 NULL, /* sieve subaddress not relevant */
391 &ugid, /* uid/gid (but not set) */
392 &generated, /* where to hang the results */
393 &addr->message, /* where to put messages */
394 NULL, /* don't skip syntax errors */
395 &filtertype, /* not used; will always be FILTER_FORWARD */
396 string_sprintf("%s router", rblock->name));
400 /* FF_DEFER and FF_FAIL can arise only as a result of explicit commands.
401 If a configured message was supplied, allow it to be included in an SMTP
402 response after verifying. */
405 if (!addr->message) addr->message = US"forced defer";
406 else addr->user_message = addr->message;
410 add_generated(rblock, addr_new, addr, generated, &addr_prop);
411 if (!addr->message) addr->message = US"forced rejection";
412 else addr->user_message = addr->message;
418 case FF_NOTDELIVERED: /* an empty redirection list is bad */
419 addr->message = US"no addresses supplied";
424 addr->basic_errno = ERRNO_BADREDIRECT;
425 addr->message = string_sprintf("error in redirect data: %s", addr->message);
429 /* Handle the generated addresses, if any. */
431 add_generated(rblock, addr_new, addr, generated, &addr_prop);
433 /* Put the original address onto the succeed queue so that any retry items
434 that get attached to it get processed. */
436 addr->next = *addr_succeed;
437 *addr_succeed = addr;
442 /* Handle other returns that are not ACCEPT */
444 if (strcmpic(rword, US"accept") != 0)
446 if (strcmpic(rword, US"decline") == 0) return DECLINE;
447 if (strcmpic(rword, US"pass") == 0) return PASS;
448 addr->message = string_copy(rdata); /* data is a message */
449 if (strcmpic(rword, US"fail") == 0)
451 setflag(addr, af_pass_message);
454 if (strcmpic(rword, US"freeze") == 0) addr->special_action = SPECIAL_FREEZE;
455 else if (strcmpic(rword, US"defer") != 0)
457 addr->message = string_sprintf("bad command yield: %s %s", rword, rdata);
458 log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
463 /* The command yielded "ACCEPT". The rest of the string is a number of keyed
464 fields from which we can fish out values using the equivalent of the "extract"
465 expansion function. */
467 if ((s = expand_getkeyed(US"data", rdata)) && *s)
468 addr_prop.address_data = string_copy(s);
470 /* If we found a transport name, find the actual transport */
472 if ((s = expand_getkeyed(US"transport", rdata)) && *s)
474 transport_instance *transport;
475 for (transport = transports; transport; transport = transport->next)
476 if (Ustrcmp(transport->name, s) == 0) break;
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 if ((s = expand_getkeyed(US"hosts", rdata)) && *s)
503 int lookup_type = LK_DEFAULT;
504 uschar * ss = expand_getkeyed(US"lookup", rdata);
508 if (Ustrcmp(ss, "byname") == 0) lookup_type = LK_BYNAME;
509 else if (Ustrcmp(ss, "bydns") == 0) lookup_type = LK_BYDNS;
512 addr->message = string_sprintf("bad lookup type \"%s\" yielded by "
514 log_write(0, LOG_PANIC, "%s router: %s", rblock->name, addr->message);
519 host_build_hostlist(&(addr->host_list), s, FALSE); /* pro tem no randomize */
521 rc = rf_lookup_hostlist(rblock, addr, rblock->ignore_target_hosts,
522 lookup_type, hff_defer, addr_new);
523 if (rc != OK) return rc;
527 /* Put the errors address, extra headers, and address_data into this address */
529 addr->prop = addr_prop;
531 /* Queue the address for local or remote delivery. */
533 return rf_queue_add(addr, addr_local, addr_remote, rblock, pw) ? OK : DEFER;
536 #endif /*!MACRO_PREDEF*/
537 /* End of routers/queryprogram.c */