Debian Codegraph @

contrib/mod_exec.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_exec -- a module for executing external scripts
3 * Copyright (c) 2002-2025 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders gives permission to link this program
20 * with OpenSSL, and distribute the resulting executable, without including
21 * the source code for OpenSSL in the source distribution.
22 *
23 * This is mod_exec, contrib software for proftpd 1.3.x and above.
24 * For more information contact TJ Saunders <[email protected]>.
25 */
26
27 #include "conf.h"
28 #include "privs.h"
29 #include "logfmt.h"
30 #include "jot.h"
31
32 #if defined(HAVE_SYS_RESOURCE_H)
33 # include <sys/resource.h>
34 #endif
35
36 #define MOD_EXEC_VERSION "mod_exec/1.0"
37
38 /* Make sure the version of proftpd is as necessary. */
39 #if PROFTPD_VERSION_NUMBER < 0x0001030701
40 # error "ProFTPD 1.3.7rc1 or later required"
41 #endif
42
43 module exec_module;
44
45 static pool *exec_pool = NULL;
46 static int exec_engine = FALSE;
47 static unsigned int exec_nexecs = 0;
48
49 static int exec_logfd = -1;
50 static char *exec_logname = NULL;
51
52 static int exec_stdin_pipe[2];
53 static int exec_stdout_pipe[2];
54 static int exec_stderr_pipe[2];
55
56 static unsigned int exec_opts = 0U;
57 #define EXEC_OPT_LOG_STDOUT 0x0001
58 #define EXEC_OPT_LOG_STDERR 0x0002
59 #define EXEC_OPT_SEND_STDOUT 0x0004
60 #define EXEC_OPT_USE_STDIN 0x0008
61
62 static int exec_timeout = 0;
63
64 /* Flags for exec_ssystem() */
65 #define EXEC_FL_CLEAR_GROUPS 0x0010 /* Clear supplemental groups */
66 #define EXEC_FL_NO_SEND 0x0020 /* Do not send output via response */
67 #define EXEC_FL_USE_SEND 0x0040 /* Use pr_response_send() instead of
68 * pr_response_add()
69 */
70 #define EXEC_FL_RUN_AS_ROOT 0x0080 /* Use root privs when executing
71 * the command. USE CAREFULLY!
72 */
73 #define EXEC_FL_RUN_AS_USER 0x0100 /* Use user privs when executing
74 * the command. Useful for pre-login
75 * events.
76 */
77
78 /* config_rec index for various stashed info */
79 #define EXEC_IDX_TRIGGER_CMDS 1
80 #define EXEC_IDX_LOGFMTS 2
81
82 struct exec_jot_buffer {
83 char *ptr, *buf;
84 size_t bufsz, buflen;
85 };
86
87 struct exec_event_data {
88 unsigned int flags;
89 config_rec *c;
90 const char *event;
91 };
92
93 /* Prototypes */
94 static void exec_any_ev(const void *, void *);
95 static const char *exec_subst_var(pool *p, cmd_rec *cmd,
96 const char *text, unsigned char *logfmt);
97 static int exec_log(const char *, ...)
98 #if defined(__GNUC__)
99 __attribute__ ((format (printf, 1, 2)));
100 #else
101 ;
102 #endif
103 static int exec_sess_init(void);
104
105 static const char *trace_channel = "exec";
106
107 /* Support routines
108 */
109
110 static int exec_closelog(void) {
111 /* sanity check */
112 if (exec_logfd != -1) {
113 (void) close(exec_logfd);
114 exec_logfd = -1;
115 exec_logname = NULL;
116 }
117
118 return 0;
119 }
120
121 static int exec_enabled(void) {
122 config_rec *c;
123 int enabled = TRUE;
124
125 c = find_config(CURRENT_CONF, CONF_PARAM, "ExecEnable", FALSE);
126 if (c != NULL) {
127 enabled = *((int *) c->argv[0]);
128 }
129
130 return enabled;
131 }
132
133 static char *exec_get_cmd(char **list) {
134 char *res = NULL, *dst = NULL;
135 unsigned char quote_mode = FALSE;
136
137 while (**list && PR_ISSPACE(**list)) {
138 (*list)++;
139 }
140
141 if (!**list) {
142 return NULL;
143 }
144
145 res = dst = *list;
146
147 if (**list == '\"') {
148 quote_mode = TRUE;
149 (*list)++;
150 }
151
152 while (**list && **list != ',' &&
153 (quote_mode ? (**list != '\"') : (!PR_ISSPACE(**list)))) {
154
155 if (**list == '\\' && quote_mode) {
156 /* Escaped char */
157 if (*((*list) + 1)) {
158 *dst = *(++(*list));
159 }
160 }
161
162 *dst++ = **list;
163 ++(*list);
164 }
165
166 if (**list) {
167 (*list)++;
168 }
169
170 *dst = '\0';
171
172 return res;
173 }
174
175 static int exec_log(const char *fmt, ...) {
176 va_list msg;
177 int res;
178
179 if (exec_logname == NULL) {
180 return 0;
181 }
182
183 va_start(msg, fmt);
184 res = pr_log_vwritefile(exec_logfd, MOD_EXEC_VERSION, fmt, msg);
185 va_end(msg);
186
187 return res;
188 }
189
190 static int exec_match_cmd(cmd_rec *cmd, array_header *cmd_array) {
191 register unsigned int i = 0;
192 char **cmds = NULL;
193
194 cmds = (char **) cmd_array->elts;
195
196 for (i = 0; i < cmd_array->nelts && cmds[i]; i++) {
197 if (strcasecmp(cmd->argv[0], cmds[i]) == 0) {
198 return TRUE;
199 }
200
201 if (cmd->group != NULL &&
202 strcasecmp(cmds[i], cmd->group) == 0) {
203 return TRUE;
204 }
205
206 if (strcasecmp(cmds[i], "ALL") == 0) {
207 return TRUE;
208 }
209 }
210
211 return FALSE;
212 }
213
214 static int exec_openlog(void) {
215 int res = 0;
216
217 /* Sanity check */
218 exec_logname = (char *) get_param_ptr(main_server->conf, "ExecLog", FALSE);
219 if (exec_logname == NULL) {
220 return 0;
221 }
222
223 /* Check for "none". */
224 if (strcasecmp(exec_logname, "none") == 0) {
225 exec_logname = NULL;
226 return 0;
227 }
228
229 pr_signals_block();
230 PRIVS_ROOT
231 res = pr_log_openfile(exec_logname, &exec_logfd, PR_LOG_SYSTEM_MODE);
232 PRIVS_RELINQUISH
233 pr_signals_unblock();
234
235 return res;
236 }
237
238 static void exec_parse_cmd_args(config_rec *c, cmd_rec *cmd,
239 unsigned int start_idx) {
240 register unsigned int i, j;
241 pool *tmp_pool;
242 array_header *logfmts = NULL;
243 pr_jot_ctx_t *jot_ctx;
244 pr_jot_parsed_t *jot_parsed;
245 unsigned char parsed_buf[1024];
246
247 logfmts = make_array(c->pool, 0, sizeof(unsigned char *));
248
249 tmp_pool = make_sub_pool(c->pool);
250 pr_pool_tag(tmp_pool, "exec cmd args pool");
251
252 jot_parsed = pcalloc(tmp_pool, sizeof(pr_jot_parsed_t));
253 jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t));
254 jot_ctx->log = jot_parsed;
255
256 for (i = start_idx, j = 2; i < cmd->argc; i++, j++) {
257 int res;
258 char *text;
259 unsigned char *logfmt;
260
261 /* We make a copy of the plaintext, AND we parse it for any LogFormat
262 * variables for later resolution.
263 */
264 text = pstrdup(c->pool, cmd->argv[i]);
265
266 jot_parsed->bufsz = jot_parsed->buflen = sizeof(parsed_buf);
267 jot_parsed->ptr = jot_parsed->buf = parsed_buf;
268
269 res = pr_jot_parse_logfmt(tmp_pool, text, jot_ctx, pr_jot_parse_on_meta,
270 pr_jot_parse_on_unknown, pr_jot_parse_on_other,
271 PR_JOT_LOGFMT_PARSE_FL_UNKNOWN_AS_CUSTOM);
272 if (res < 0) {
273 pr_trace_msg(trace_channel, 2, "error parsing text '%s' for %s: %s",
274 text, (char *) c->argv[0], strerror(errno));
275 logfmt = (unsigned char *) text;
276
277 } else {
278 size_t logfmt_len;
279
280 logfmt_len = jot_parsed->bufsz - jot_parsed->buflen;
281 logfmt = palloc(c->pool, logfmt_len + 1);
282 memcpy(logfmt, parsed_buf, logfmt_len);
283 logfmt[logfmt_len] = '\0';
284 }
285
286 *((unsigned char **) push_array(logfmts)) = logfmt;
287 c->argv[EXEC_IDX_LOGFMTS + j] = text;
288 }
289
290 /* Store the array of logfmts in the config_rec. */
291 c->argv[EXEC_IDX_LOGFMTS] = logfmts;
292
293 destroy_pool(tmp_pool);
294 }
295
296 static void exec_parse_trigger_cmds(config_rec *c, char *cmds) {
297 char *cmd = NULL;
298 array_header *cmd_array = NULL;
299
300 cmd_array = make_array(c->pool, 0, sizeof(char *));
301
302 /* Add each command to the array. */
303 while ((cmd = exec_get_cmd(&cmds)) != NULL) {
304 *((char **) push_array(cmd_array)) = pstrdup(c->pool, cmd);
305 }
306
307 /* Terminate the array with a NULL. */
308 *((char **) push_array(cmd_array)) = NULL;
309
310 /* Store the array of commands in the config_rec. */
311 c->argv[EXEC_IDX_TRIGGER_CMDS] = cmd_array;
312 }
313
314 static char **exec_prepare_environ(pool *env_pool, cmd_rec *cmd) {
315 config_rec *c = NULL;
316 array_header *env = make_array(env_pool, 0, sizeof(char *));
317
318 c = find_config(main_server->conf, CONF_PARAM, "ExecEnviron", FALSE);
319 while (c != NULL) {
320 const char *key, *val = NULL, *text;
321 unsigned char *logfmt;
322
323 pr_signals_handle();
324
325 key = c->argv[0];
326 text = c->argv[1];
327 logfmt = c->argv[2];
328
329 if (strcmp("-", text) == 0) {
330 val = getenv(key);
331
332 } else {
333 val = exec_subst_var(env_pool, cmd, text, logfmt);
334 }
335
336 *((char **) push_array(env)) = pstrcat(env_pool, key, "=",
337 val != NULL ? val : "", NULL);
338
339 c = find_config_next(c, c->next, CONF_PARAM, "ExecEnviron", FALSE);
340 }
341
342 /* Make sure the environment is NULL-terminated. */
343 *((char **) push_array(env)) = NULL;
344
345 return (char **) env->elts;
346 }
347
348 static void exec_prepare_fds(int stdin_fd, int stdout_fd, int stderr_fd) {
349 if (stdin_fd < 0) {
350 stdin_fd = open("/dev/null", O_RDONLY);
351 if (stdin_fd < 0) {
352 exec_log("error: unable to open /dev/null for stdin: %s",
353 strerror(errno));
354
355 } else {
356 if (dup2(stdin_fd, STDIN_FILENO) < 0)
357 exec_log("error: unable to dup fd %d to stdin: %s", stdin_fd,
358 strerror(errno));
359
360 (void) close(stdin_fd);
361 }
362
363 } else {
364 if (stdin_fd != STDIN_FILENO) {
365 if (dup2(stdin_fd, STDIN_FILENO) < 0) {
366 exec_log("error: unable to dup fd %d to stdin: %s", stdin_fd,
367 strerror(errno));
368 }
369
370 (void) close(stdin_fd);
371 }
372 }
373
374 if (stdout_fd != STDOUT_FILENO) {
375 if (dup2(stdout_fd, STDOUT_FILENO) < 0) {
376 exec_log("error: unable to dup fd %d to stdout: %s", stdout_fd,
377 strerror(errno));
378 }
379
380 (void) close(stdout_fd);
381 }
382
383 if (stderr_fd != STDERR_FILENO) {
384 if (dup2(stderr_fd, STDERR_FILENO) < 0) {
385 exec_log("error: unable to dup fd %d to stderr: %s", stderr_fd,
386 strerror(errno));
387 }
388
389 (void) close(stderr_fd);
390 }
391
392 /* Make sure not to pass on open file descriptors. For stdin, we
393 * dup /dev/null. For stdout and stderr, we dup some pipes, so that
394 * we can capture what the command may write to stdout or stderr. The
395 * stderr output will be logged to the ExecLog.
396 */
397 pr_fs_close_extra_fds();
398 }
399
400 static void exec_prepare_pipes(void) {
401
402 /* Open pipes for stdin, stdout and stderr. */
403 if (exec_opts & EXEC_OPT_USE_STDIN) {
404 if (pipe(exec_stdin_pipe) < 0) {
405 exec_log("error: unable to open stdin pipe: %s", strerror(errno));
406 exec_stdin_pipe[0] = STDIN_FILENO;
407 exec_stdin_pipe[1] = -1;
408
409 } else {
410 if (fcntl(exec_stdin_pipe[0], F_SETFD, 0) < 0) {
411 exec_log("error: unable to set cloexec flag on stdin pipe read fd: %s",
412 strerror(errno));
413 }
414
415 if (fcntl(exec_stdin_pipe[1], F_SETFD, FD_CLOEXEC) < 0) {
416 exec_log("error: unable to set cloexec flag on stdin pipe write fd: %s",
417 strerror(errno));
418 }
419 }
420 }
421
422 if (pipe(exec_stdout_pipe) < 0) {
423 exec_log("error: unable to open stdout pipe: %s", strerror(errno));
424 exec_stdout_pipe[0] = -1;
425 exec_stdout_pipe[1] = STDOUT_FILENO;
426
427 } else {
428 if (fcntl(exec_stdout_pipe[0], F_SETFD, FD_CLOEXEC) < 0) {
429 exec_log("error: unable to set cloexec flag on stdout pipe read fd: %s",
430 strerror(errno));
431 }
432
433 if (fcntl(exec_stdout_pipe[1], F_SETFD, 0) < 0) {
434 exec_log("error: unable to set cloexec flag on stdout pipe write fd: %s",
435 strerror(errno));
436 }
437 }
438
439 if (pipe(exec_stderr_pipe) < 0) {
440 exec_log("error: unable to open stderr pipe: %s", strerror(errno));
441 exec_stderr_pipe[0] = -1;
442 exec_stderr_pipe[1] = STDERR_FILENO;
443
444 } else {
445 if (fcntl(exec_stderr_pipe[0], F_SETFD, FD_CLOEXEC) < 0) {
446 exec_log("error: unable to set cloexec flag on stderr pipe read fd: %s",
447 strerror(errno));
448 }
449
450 if (fcntl(exec_stderr_pipe[1], F_SETFD, 0) < 0) {
451 exec_log("error: unable to set cloexec flag on stderr pipe write fd: %s",
452 strerror(errno));
453 }
454 }
455 }
456
457 /* Provides a "safe" version of the system(2) call by dropping all special
458 * privileges, currently retained by the daemon, before exec()'ing the
459 * given command.
460 */
461 static int exec_ssystem(cmd_rec *cmd, config_rec *c, int flags) {
462 pid_t pid;
463 int status;
464 unsigned int path_idx = EXEC_IDX_LOGFMTS+1;
465 const char *path;
466
467 struct sigaction sa_ignore, sa_intr, sa_quit;
468 sigset_t set_chldmask, set_save;
469
470 /* Prepare signal dispositions. */
471 sa_ignore.sa_handler = SIG_IGN;
472 sigemptyset(&sa_ignore.sa_mask);
473 sa_ignore.sa_flags = 0;
474
475 if (sigaction(SIGINT, &sa_ignore, &sa_intr) < 0) {
476 return errno;
477 }
478
479 if (sigaction(SIGQUIT, &sa_ignore, &sa_quit) < 0) {
480 return errno;
481 }
482
483 sigemptyset(&set_chldmask);
484 sigaddset(&set_chldmask, SIGCHLD);
485
486 if (sigprocmask(SIG_BLOCK, &set_chldmask, &set_save) < 0) {
487 exec_log("sigprocmask error: %s", strerror(errno));
488 return errno;
489 }
490
491 exec_prepare_pipes();
492 path = c->argv[path_idx];
493
494 pid = fork();
495 if (pid < 0) {
496 int xerrno = errno;
497
498 pr_log_pri(PR_LOG_ALERT,
499 MOD_EXEC_VERSION ": error: unable to fork: %s", strerror(xerrno));
500 exec_log("error: unable to fork: %s", strerror(xerrno));
501
502 errno = xerrno;
503 status = -1;
504
505 } else if (pid == 0) {
506 register unsigned int i = 0;
507 char **env = NULL, *ptr = NULL;
508 pool *tmp_pool;
509
510 /* Child process */
511
512 /* Note: there is no need to clean up this temporary pool, as we've
513 * forked. If the exec call succeeds, this child process will exit
514 * normally, and its process space recovered by the OS. If the exec
515 * call fails, we still exit, and the process space is recovered by
516 * the OS. Either way, the memory will be cleaned up without need for
517 * us to do it explicitly (unless one wanted to be pedantic about it,
518 * of course).
519 */
520 tmp_pool = cmd ? cmd->tmp_pool : make_sub_pool(session.pool);
521
522 /* Don't forget to update the PID. */
523 session.pid = getpid();
524
525 if (!(exec_opts & EXEC_OPT_USE_STDIN)) {
526 register unsigned int j;
527 array_header *logfmts;
528
529 /* Prepare the environment. */
530 env = exec_prepare_environ(tmp_pool, cmd);
531
532 logfmts = c->argv[EXEC_IDX_LOGFMTS];
533
534 /* Perform any required substitution on the command arguments. */
535 for (i = path_idx+1, j = 0; i < c->argc; i++, j++) {
536 unsigned char *logfmt;
537
538 pr_signals_handle();
539
540 logfmt = ((unsigned char **) logfmts->elts)[j];
541 c->argv[i] = (void *) exec_subst_var(tmp_pool, cmd, c->argv[i], logfmt);
542 }
543
544 } else {
545 /* Make sure that env is at least a NULL-terminated array. */
546 env = pcalloc(tmp_pool, sizeof(char **));
547 }
548
549 /* Restore previous signal actions. */
550 sigaction(SIGINT, &sa_intr, NULL);
551 sigaction(SIGQUIT, &sa_quit, NULL);
552 sigprocmask(SIG_SETMASK, &set_save, NULL);
553
554 /* Per Bug#4049, when running the command as the user, do NOT clear
555 * the supplemental groups.
556 */
557 if (flags & EXEC_FL_RUN_AS_USER) {
558 flags &= ~EXEC_FL_CLEAR_GROUPS;
559 }
560
561 /* If requested, clear the supplemental group membership of the process. */
562 if (flags & EXEC_FL_CLEAR_GROUPS) {
563 PRIVS_ROOT
564 setgroups(0, NULL);
565 PRIVS_RELINQUISH
566 }
567
568 if (flags & EXEC_FL_RUN_AS_ROOT) {
569 /* We were asked to run using root privs. Yuck. */
570 PRIVS_ROOT
571
572 } else if (flags & EXEC_FL_RUN_AS_USER) {
573 /* We were asked to run using user privs. Sigh. */
574 if (geteuid() != session.login_uid) {
575 PRIVS_SETUP(session.login_uid, session.login_gid)
576 }
577
578 PRIVS_REVOKE
579
580 } else {
581 /* Drop all special privileges before exec()'ing the command. This
582 * allows for the user to specify arbitrary input via the given
583 * filename without the admin worrying that some arbitrary command
584 * is being executed that could take advantage of proftpd's retention
585 * of root real user ID.
586 */
587 PRIVS_REVOKE
588 }
589
590 exec_log("preparing to execute '%s' with uid %s (euid %s), "
591 "gid %s (egid %s)", path,
592 pr_uid2str(tmp_pool, getuid()), pr_uid2str(tmp_pool, geteuid()),
593 pr_gid2str(tmp_pool, getgid()), pr_gid2str(tmp_pool, getegid()));
594
595 /* Trim the given path to the command to execute to just the last
596 * component; this name will be the first argument to the executed
597 * command, as per execve(2) convention.
598 */
599 ptr = strrchr(c->argv[path_idx], '/');
600 c->argv[path_idx] = ptr + 1;
601
602 for (i = path_idx+1; i < c->argc; i++) {
603 if (c->argv[i] != NULL) {
604 exec_log(" + '%s': argv[%u] = %s", (const char *) path,
605 i - path_idx, (const char *) c->argv[i]);
606 }
607 }
608
609 /* Prepare the file descriptors that the process will inherit. */
610 if (exec_opts & EXEC_OPT_USE_STDIN) {
611 exec_prepare_fds(exec_stdin_pipe[0], exec_stdout_pipe[1],
612 exec_stderr_pipe[1]);
613
614 } else {
615 exec_prepare_fds(-1, exec_stdout_pipe[1], exec_stderr_pipe[1]);
616 }
617
618 errno = 0;
619
620 /* If we are using stdin to convey all of the arguments, then we need
621 * not provide any sort of environment variables.
622 */
623 if (exec_opts & EXEC_OPT_USE_STDIN) {
624 char *args[] = { ptr + 1, NULL };
625
626 execve(path, args, env);
627
628 } else {
629 execve(path, (char **) (c->argv + path_idx), env);
630 }
631
632 /* Since all previous file descriptors (including those for log files)
633 * have been closed, and root privs have been revoked, there's little
634 * chance of directing a message of execve() failure to proftpd's log
635 * files. execve() only returns if there's an error; the only way we
636 * can signal this to the waiting parent process is to exit with a
637 * non-zero value (the value of errno will do nicely).
638 */
639 exit(errno);
640
641 } else {
642 int res;
643
644 /* Parent process */
645
646 /* If we are using stdin, then we need to write all of the arguments to
647 * stdin before closing that pipe.
648 */
649 if (exec_opts & EXEC_OPT_USE_STDIN) {
650 register unsigned int i, j;
651 int maxfd = -1, fds;
652 fd_set writefds;
653 struct timeval tv;
654 array_header *logfmts;
655 pool *tmp_pool;
656
657 tmp_pool = cmd ? cmd->tmp_pool : make_sub_pool(session.pool);
658
659 /* Wait for stdin to be available for writing. */
660
661 FD_ZERO(&writefds);
662 FD_SET(exec_stdin_pipe[1], &writefds);
663 if (exec_stdin_pipe[1] > maxfd) {
664 maxfd = exec_stdin_pipe[1];
665 }
666
667 /* Note: this delay should be configurable somehow. */
668 tv.tv_sec = 2L;
669 tv.tv_usec = 0L;
670
671 fds = select(maxfd + 1, &writefds, NULL, NULL, &tv);
672
673 if (fds == -1 &&
674 errno == EINTR) {
675 pr_signals_handle();
676 }
677
678 logfmts = c->argv[EXEC_IDX_LOGFMTS];
679
680 /* Perform any required substitution on the command arguments. */
681 for (i = path_idx+1, j = 0; i < c->argc; i++, j++) {
682 unsigned char *logfmt;
683
684 pr_signals_handle();
685
686 logfmt = ((unsigned char **) logfmts->elts)[j];
687
688 /* Handle the NULL-terminated argv lists here, since we are processing
689 * it manually for sending via stdin.
690 */
691 if (c->argv[i] == NULL ||
692 logfmt == NULL) {
693 break;
694 }
695
696 c->argv[i] = (void *) exec_subst_var(tmp_pool, cmd, c->argv[i], logfmt);
697
698 /* Write the argument to stdin, terminated by a newline. */
699 if (write(exec_stdin_pipe[1], c->argv[i], strlen(c->argv[i])) < 0) {
700 exec_log("error writing argument to stdin: %s", strerror(errno));
701
702 } else {
703 exec_log("wrote argument %u (%s) to stdin (%d)", i - path_idx,
704 (char *) c->argv[i], exec_stdin_pipe[1]);
705 }
706
707 if (write(exec_stdin_pipe[1], "\n", 1) < 0) {
708 exec_log("error writing newline to stdin: %s", strerror(errno));
709 }
710 }
711
712 /* Signal the end of stdin arguments using a period followed by a newline.
713 * (This is the end-of-arguments indicated used by other protocols such
714 * as SMTP.)
715 */
716 if (write(exec_stdin_pipe[1], ".\n", 2) < 0) {
717 exec_log("error writing end-of-argument indicator: %s",
718 strerror(errno));
719 }
720
721 if (cmd == NULL) {
722 destroy_pool(tmp_pool);
723 }
724 }
725
726 if (exec_opts & EXEC_OPT_USE_STDIN) {
727 (void) close(exec_stdin_pipe[0]);
728 exec_stdin_pipe[0] = -1;
729 }
730
731 (void) close(exec_stdout_pipe[1]);
732 exec_stdout_pipe[1] = -1;
733
734 (void) close(exec_stderr_pipe[1]);
735 exec_stderr_pipe[1] = -1;
736
737 if ((exec_opts & EXEC_OPT_LOG_STDOUT) ||
738 (exec_opts & EXEC_OPT_LOG_STDERR) ||
739 (exec_opts & EXEC_OPT_SEND_STDOUT) ||
740 exec_timeout > 0) {
741 int maxfd = -1, fds, send_sigterm = 1;
742 fd_set readfds;
743 struct timeval tv;
744 time_t start_time = time(NULL);
745 pool *tmp_pool;
746
747 tmp_pool = cmd ? cmd->tmp_pool : make_sub_pool(session.pool);
748
749 /* We set the result value to zero initially, so that at least one
750 * pass through the stdout/stderr reading code happens.
751 */
752 res = 0;
753 while (res <= 0) {
754 if (res < 0) {
755 if (errno != EINTR) {
756 exec_log("error: unable to wait for pid %d: %s", pid,
757 strerror(errno));
758 status = -1;
759 break;
760
761 } else {
762 pr_signals_handle();
763 continue;
764 }
765 }
766
767 if (exec_timeout > 0) {
768 /* Check the time elapsed since we started. */
769 if ((time(NULL) - start_time) > exec_timeout) {
770
771 /* Send TERM, the first time, to be polite. */
772 if (send_sigterm) {
773 send_sigterm = 0;
774 exec_log("'%s' has exceeded ExecTimeout (%lu seconds), sending "
775 "SIGTERM (signal %d)", path, (unsigned long) exec_timeout,
776 SIGTERM);
777 kill(pid, SIGTERM);
778
779 } else {
780 /* The child is still around? Terminate with extreme
781 * prejudice.
782 */
783 exec_log("'%s' has exceeded ExecTimeout (%lu seconds), sending "
784 "SIGKILL (signal %d)", path, (unsigned long) exec_timeout,
785 SIGKILL);
786 kill(pid, SIGKILL);
787 }
788 }
789 }
790
791 /* Select on the pipe read fds, to see if the child has anything
792 * to tell us.
793 */
794 FD_ZERO(&readfds);
795
796 if ((exec_opts & EXEC_OPT_LOG_STDOUT) ||
797 (exec_opts & EXEC_OPT_SEND_STDOUT)) {
798 FD_SET(exec_stdout_pipe[0], &readfds);
799
800 if (exec_stdout_pipe[0] > maxfd) {
801 maxfd = exec_stdout_pipe[0];
802 }
803 }
804
805 if (exec_opts & EXEC_OPT_LOG_STDERR) {
806 FD_SET(exec_stderr_pipe[0], &readfds);
807
808 if (exec_stderr_pipe[0] > maxfd) {
809 maxfd = exec_stderr_pipe[0];
810 }
811 }
812
813 /* Note: this delay should be configurable somehow. */
814 tv.tv_sec = 2L;
815 tv.tv_usec = 0L;
816
817 fds = select(maxfd + 1, &readfds, NULL, NULL, &tv);
818 if (fds == -1 &&
819 errno == EINTR) {
820 pr_signals_handle();
821 }
822
823 if (fds >= 0) {
824 long buflen, bufsz;
825 char *buf;
826
827 buf = pr_fsio_getpipebuf(tmp_pool, exec_stdout_pipe[0], &bufsz);
828
829 /* The child sent us something. How thoughtful. */
830
831 if (FD_ISSET(exec_stdout_pipe[0], &readfds)) {
832 memset(buf, '\0', bufsz);
833
834 buflen = read(exec_stdout_pipe[0], buf, bufsz-1);
835 if (buflen > 0) {
836 if (exec_opts & EXEC_OPT_SEND_STDOUT) {
837
838 if (!(flags & EXEC_FL_NO_SEND)) {
839 if (flags & EXEC_FL_USE_SEND) {
840 pr_response_send(R_220, "%s", buf);
841
842 } else {
843 pr_response_add(R_DUP, "%s", buf);
844 }
845
846 } else {
847 exec_log("not appropriate to send stdout to client at "
848 "this time");
849 }
850 }
851
852 /* Trim trailing CRs and LFs. */
853 while (buflen > 0 &&
854 (buf[buflen-1] == '\r' || buf[buflen-1] == '\n')) {
855 pr_signals_handle();
856 buf[buflen-1] = '\0';
857 buflen--;
858 }
859
860 /* We told read(2) that the size of buf is one less than its
861 * actual size. Which means that the buflen value returned
862 * by read(2) can, at most, be one less than the size of buf.
863 * Thus it should be OK to do the following.
864 */
865 buf[buflen] = '\0';
866
867 if (exec_opts & EXEC_OPT_LOG_STDOUT) {
868 exec_log("stdout from '%s': '%s'", path, buf);
869 }
870
871 } else if (buflen < 0) {
872 if (errno != 0) {
873 exec_log("error reading stdout from '%s': %s", path,
874 strerror(errno));
875 }
876 }
877 }
878
879 if (FD_ISSET(exec_stderr_pipe[0], &readfds)) {
880 memset(buf, '\0', bufsz);
881
882 buflen = read(exec_stderr_pipe[0], buf, bufsz-1);
883 if (buflen > 0) {
884
885 /* Trim trailing CRs and LFs. */
886 while (buflen > 0 &&
887 (buf[buflen-1] == '\r' || buf[buflen-1] == '\n')) {
888 pr_signals_handle();
889 buf[buflen-1] = '\0';
890 buflen--;
891 }
892
893 /* We told read(2) that the size of buf is one less than its
894 * actual size. Which means that the buflen value returned
895 * by read(2) can, at most, be one less than the size of buf.
896 * Thus it should be OK to do the following.
897 */
898 buf[buflen] = '\0';
899
900 if (exec_opts & EXEC_OPT_LOG_STDERR) {
901 exec_log("stderr from '%s': '%s'", path, buf);
902 }
903
904 } else if (buflen < 0) {
905 if (errno != 0) {
906 exec_log("error reading stderr from '%s': %s", path,
907 strerror(errno));
908 }
909 }
910 }
911 }
912
913 res = waitpid(pid, &status, WNOHANG);
914 }
915
916 if (cmd == NULL) {
917 destroy_pool(tmp_pool);
918 }
919
920 } else {
921 res = waitpid(pid, &status, 0);
922 while (res <= 0) {
923 if (res < 0) {
924 if (errno != EINTR) {
925 exec_log("error: unable to wait for pid %d: %s", pid,
926 strerror(errno));
927 status = -1;
928 break;
929
930 } else {
931 pr_signals_handle();
932 continue;
933 }
934 }
935
936 res = waitpid(pid, &status, 0);
937 }
938 }
939 }
940
941 close(exec_stdout_pipe[0]);
942 close(exec_stderr_pipe[0]);
943
944 /* Restore the previous signal actions. */
945 if (sigaction(SIGINT, &sa_intr, NULL) < 0) {
946 exec_log("sigaction() error: %s", strerror(errno));
947 return errno;
948 }
949
950 if (sigaction(SIGQUIT, &sa_quit, NULL) < 0) {
951 exec_log("sigaction() error: %s", strerror(errno));
952 return errno;
953 }
954
955 if (sigprocmask(SIG_SETMASK, &set_save, NULL) < 0) {
956 exec_log("sigprocmask() error: %s", strerror(errno));
957 return errno;
958 }
959
960 if (WIFEXITED(status)) {
961 int exit_status;
962
963 exit_status = WEXITSTATUS(status);
964 exec_log("'%s' terminated normally, with exit status %d", path,
965 exit_status);
966 return exit_status;
967 }
968
969 if (WIFSIGNALED(status)) {
970 exec_log("'%s' died from signal %d", path, WTERMSIG(status));
971
972 if (WCOREDUMP(status)) {
973 exec_log("'%s' created a coredump", path);
974 }
975
976 return EPERM;
977 }
978
979 return status;
980 }
981
982 static void exec_jot_append_text(struct exec_jot_buffer *log, const char *text,
983 size_t text_len) {
984 if (text == NULL ||
985 text_len == 0) {
986 return;
987 }
988
989 if (text_len > log->buflen) {
990 text_len = log->buflen;
991 }
992
993 pr_trace_msg(trace_channel, 19, "appending text '%.*s' (%lu) to buffer",
994 (int) text_len, text, (unsigned long) text_len);
995 memcpy(log->buf, text, text_len);
996 log->buf += text_len;
997 log->buflen -= text_len;
998 }
999
1000 static int resolve_on_meta(pool *p, pr_jot_ctx_t *jot_ctx,
1001 unsigned char logfmt_id, const char *jot_hint, const void *val) {
1002 struct exec_jot_buffer *log;
1003
1004 log = jot_ctx->log;
1005 if (log->buflen > 0) {
1006 const char *text = NULL;
1007 size_t text_len = 0;
1008 char buf[1024];
1009
1010 switch (logfmt_id) {
1011 case LOGFMT_META_CUSTOM: {
1012 const char *key;
1013
1014 /* The Var API is particular about the format of the keys. */
1015 key = pstrcat(p, "%{", val, "}", NULL);
1016 text = pr_var_get(key);
1017 break;
1018 }
1019
1020 case LOGFMT_META_MICROSECS: {
1021 unsigned long num;
1022
1023 num = *((double *) val);
1024 text_len = pr_snprintf(buf, sizeof(buf)-1, "%06lu", num);
1025 buf[text_len] = '\0';
1026 text = buf;
1027 break;
1028 }
1029
1030 case LOGFMT_META_MILLISECS: {
1031 unsigned long num;
1032
1033 num = *((double *) val);
1034 text_len = pr_snprintf(buf, sizeof(buf)-1, "%03lu", num);
1035 buf[text_len] = '\0';
1036 text = buf;
1037 break;
1038 }
1039
1040 case LOGFMT_META_LOCAL_PORT:
1041 case LOGFMT_META_REMOTE_PORT:
1042 case LOGFMT_META_RESPONSE_CODE:
1043 case LOGFMT_META_XFER_PORT: {
1044 int num;
1045
1046 num = *((double *) val);
1047 text_len = pr_snprintf(buf, sizeof(buf)-1, "%d", num);
1048 buf[text_len] = '\0';
1049 text = buf;
1050 break;
1051 }
1052
1053 case LOGFMT_META_UID: {
1054 uid_t uid;
1055
1056 uid = *((double *) val);
1057 text = pr_uid2str(p, uid);
1058 break;
1059 }
1060
1061 case LOGFMT_META_GID: {
1062 gid_t gid;
1063
1064 gid = *((double *) val);
1065 text = pr_gid2str(p, gid);
1066 break;
1067 }
1068
1069 case LOGFMT_META_BYTES_SENT:
1070 case LOGFMT_META_FILE_OFFSET:
1071 case LOGFMT_META_FILE_SIZE:
1072 case LOGFMT_META_RAW_BYTES_IN:
1073 case LOGFMT_META_RAW_BYTES_OUT:
1074 case LOGFMT_META_RESPONSE_MS:
1075 case LOGFMT_META_XFER_MS: {
1076 off_t num;
1077
1078 num = *((double *) val);
1079 text_len = pr_snprintf(buf, sizeof(buf)-1, "%" PR_LU, (pr_off_t) num);
1080 buf[text_len] = '\0';
1081 text = buf;
1082 break;
1083 }
1084
1085 case LOGFMT_META_EPOCH:
1086 case LOGFMT_META_PID: {
1087 unsigned long num;
1088
1089 num = *((double *) val);
1090 text_len = pr_snprintf(buf, sizeof(buf)-1, "%lu", num);
1091 buf[text_len] = '\0';
1092 text = buf;
1093 break;
1094 }
1095
1096 case LOGFMT_META_FILE_MODIFIED: {
1097 int truth;
1098
1099 truth = *((int *) val);
1100 text = truth ? "true" : "false";
1101 break;
1102 }
1103
1104 case LOGFMT_META_SECONDS: {
1105 float num;
1106
1107 num = *((double *) val);
1108 text_len = pr_snprintf(buf, sizeof(buf)-1, "%0.3f", num);
1109 buf[text_len] = '\0';
1110 text = buf;
1111 break;
1112 }
1113
1114 case LOGFMT_META_ANON_PASS:
1115 case LOGFMT_META_BASENAME:
1116 case LOGFMT_META_CLASS:
1117 case LOGFMT_META_CMD_PARAMS:
1118 case LOGFMT_META_COMMAND:
1119 case LOGFMT_META_DIR_NAME:
1120 case LOGFMT_META_DIR_PATH:
1121 case LOGFMT_META_ENV_VAR:
1122 case LOGFMT_META_EOS_REASON:
1123 case LOGFMT_META_FILENAME:
1124 case LOGFMT_META_GROUP:
1125 case LOGFMT_META_IDENT_USER:
1126 case LOGFMT_META_ISO8601:
1127 case LOGFMT_META_LOCAL_FQDN:
1128 case LOGFMT_META_LOCAL_IP:
1129 case LOGFMT_META_LOCAL_NAME:
1130 case LOGFMT_META_METHOD:
1131 case LOGFMT_META_NOTE_VAR:
1132 case LOGFMT_META_ORIGINAL_USER:
1133 case LOGFMT_META_PROTOCOL:
1134 case LOGFMT_META_REMOTE_HOST:
1135 case LOGFMT_META_REMOTE_IP:
1136 case LOGFMT_META_RENAME_FROM:
1137 case LOGFMT_META_RESPONSE_STR:
1138 case LOGFMT_META_TIME:
1139 case LOGFMT_META_USER:
1140 case LOGFMT_META_VERSION:
1141 case LOGFMT_META_VHOST_IP:
1142 case LOGFMT_META_XFER_FAILURE:
1143 case LOGFMT_META_XFER_PATH:
1144 case LOGFMT_META_XFER_SPEED:
1145 case LOGFMT_META_XFER_STATUS:
1146 case LOGFMT_META_XFER_TYPE:
1147 default:
1148 text = val;
1149 }
1150
1151 if (text != NULL &&
1152 text_len == 0) {
1153 text_len = strlen(text);
1154 }
1155
1156 exec_jot_append_text(log, text, text_len);
1157 }
1158
1159 return 0;
1160 }
1161
1162 static int resolve_on_other(pool *p, pr_jot_ctx_t *jot_ctx, unsigned char *text,
1163 size_t text_len) {
1164 struct exec_jot_buffer *log;
1165
1166 log = jot_ctx->log;
1167 exec_jot_append_text(log, (const char *) text, text_len);
1168 return 0;
1169 }
1170
1171 /* Perform any substitution of "magic cookie" values. */
1172 static const char *exec_subst_var(pool *p, cmd_rec *cmd,
1173 const char *text, unsigned char *logfmt) {
1174 int res;
1175 pool *tmp_pool;
1176 pr_jot_ctx_t *jot_ctx;
1177 struct exec_jot_buffer *ejb;
1178 char resolved_buf[2048];
1179 const char *resolved_text = NULL;
1180
1181 if (text == NULL ||
1182 logfmt == NULL) {
1183 return NULL;
1184 }
1185
1186 tmp_pool = make_sub_pool(p);
1187 pr_pool_tag(tmp_pool, "exec jot pool");
1188
1189 ejb = pcalloc(tmp_pool, sizeof(struct exec_jot_buffer));
1190 ejb->bufsz = ejb->buflen = sizeof(resolved_buf)-1;
1191 ejb->ptr = ejb->buf = resolved_buf;
1192
1193 jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t));
1194 jot_ctx->log = ejb;
1195
1196 res = pr_jot_resolve_logfmt(tmp_pool, cmd, NULL, logfmt, jot_ctx,
1197 resolve_on_meta, NULL, resolve_on_other);
1198 if (res == 0) {
1199 size_t resolved_buflen;
1200
1201 resolved_buflen = ejb->bufsz - ejb->buflen;
1202 resolved_text = pstrndup(p, resolved_buf, resolved_buflen);
1203
1204 } else {
1205 pr_trace_msg(trace_channel, 3, "error resolving '%s' text: %s", text,
1206 strerror(errno));
1207 }
1208
1209 destroy_pool(tmp_pool);
1210 return resolved_text;
1211 }
1212
1213 /* Command handlers
1214 */
1215
1216 MODRET exec_log_exit(cmd_rec *cmd) {
1217 config_rec *c;
1218
1219 if (exec_engine == FALSE) {
1220 return PR_DECLINED(cmd);
1221 }
1222
1223 c = find_config(main_server->conf, CONF_PARAM, "ExecOnExit", FALSE);
1224 while (c != NULL) {
1225 int res;
1226 unsigned int path_idx = EXEC_IDX_LOGFMTS+1;
1227 const char *path;
1228
1229 pr_signals_handle();
1230
1231 path = c->argv[path_idx];
1232 res = exec_ssystem(cmd, c, EXEC_FL_CLEAR_GROUPS|EXEC_FL_NO_SEND);
1233 if (res != 0) {
1234 exec_log("ExecOnExit '%s' failed: %s", path, strerror(res));
1235
1236 } else {
1237 exec_log("ExecOnExit '%s' succeeded", path);
1238 }
1239
1240 c = find_config_next(c, c->next, CONF_PARAM, "ExecOnExit", FALSE);
1241 }
1242
1243 return PR_DECLINED(cmd);
1244 }
1245
1246 MODRET exec_pre_cmd(cmd_rec *cmd) {
1247 config_rec *c = NULL;
1248 array_header *seen_execs = NULL;
1249
1250 if (exec_engine == FALSE) {
1251 return PR_DECLINED(cmd);
1252 }
1253
1254 if (exec_enabled() != TRUE) {
1255 return PR_DECLINED(cmd);
1256 }
1257
1258 /* Create an array that will contain the IDs of the Execs we've
1259 * already processed.
1260 */
1261 seen_execs = make_array(cmd->tmp_pool, 0, sizeof(unsigned int));
1262
1263 c = find_config(CURRENT_CONF, CONF_PARAM, "ExecBeforeCommand", FALSE);
1264 while (c != NULL) {
1265 pr_signals_handle();
1266
1267 /* If we've already seen this Exec, skip on to the next Exec. */
1268 if (seen_execs->nelts > 0) {
1269 register unsigned int i = 0;
1270 unsigned char saw_exec = FALSE;
1271 unsigned int id = *((unsigned int *) c->argv[0]), *ids = seen_execs->elts;
1272
1273 for (i = 0; i < seen_execs->nelts; i++) {
1274 if (ids[i] == id) {
1275 saw_exec = TRUE;
1276 break;
1277 }
1278 }
1279
1280 if (saw_exec == TRUE) {
1281 exec_log("already saw this Exec, skipping");
1282 c = find_config_next(c, c->next, CONF_PARAM, "ExecBeforeCommand",
1283 FALSE);
1284 continue;
1285 }
1286 }
1287
1288 /* Add this Exec's ID to the list of seen Execs. */
1289 *((unsigned int *) push_array(seen_execs)) = *((unsigned int *) c->argv[0]);
1290
1291 /* Check the command list for this program against the current command. */
1292 if (exec_match_cmd(cmd, c->argv[EXEC_IDX_TRIGGER_CMDS]) == TRUE) {
1293 int res;
1294 unsigned int path_idx = EXEC_IDX_LOGFMTS+1;
1295 const char *path;
1296
1297 path = c->argv[path_idx];
1298 res = exec_ssystem(cmd, c, EXEC_FL_NO_SEND);
1299 if (res != 0) {
1300 exec_log("%s ExecBeforeCommand '%s' failed: %s", (char *) cmd->argv[0],
1301 path, strerror(res));
1302
1303 } else {
1304 exec_log("%s ExecBeforeCommand '%s' succeeded", (char *) cmd->argv[0],
1305 path);
1306 }
1307 }
1308
1309 c = find_config_next(c, c->next, CONF_PARAM, "ExecBeforeCommand", FALSE);
1310 }
1311
1312 return PR_DECLINED(cmd);
1313 }
1314
1315 MODRET exec_post_cmd(cmd_rec *cmd) {
1316 config_rec *c = NULL;
1317 array_header *seen_execs = NULL;
1318
1319 if (exec_engine == FALSE) {
1320 return PR_DECLINED(cmd);
1321 }
1322
1323 if (exec_enabled() != TRUE) {
1324 return PR_DECLINED(cmd);
1325 }
1326
1327 /* Create an array that will contain the IDs of the Execs we've
1328 * already processed.
1329 */
1330 seen_execs = make_array(cmd->tmp_pool, 0, sizeof(unsigned int));
1331
1332 c = find_config(CURRENT_CONF, CONF_PARAM, "ExecOnCommand", FALSE);
1333 while (c != NULL) {
1334 pr_signals_handle();
1335
1336 /* If we've already seen this Exec, skip on to the next Exec. */
1337 if (seen_execs->nelts > 0) {
1338 register unsigned int i = 0;
1339 unsigned char saw_exec = FALSE;
1340 unsigned int id = *((unsigned int *) c->argv[0]), *ids = seen_execs->elts;
1341
1342 for (i = 0; i < seen_execs->nelts; i++) {
1343 if (ids[i] == id) {
1344 saw_exec = TRUE;
1345 break;
1346 }
1347 }
1348
1349 if (saw_exec == TRUE) {
1350 exec_log("already saw this Exec, skipping");
1351 c = find_config_next(c, c->next, CONF_PARAM, "ExecOnCommand", FALSE);
1352 continue;
1353 }
1354 }
1355
1356 /* Add this Exec's ID to the list of seen Execs. */
1357 *((unsigned int *) push_array(seen_execs)) = *((unsigned int *) c->argv[0]);
1358
1359 /* Check the command list for this program against the command. */
1360 if (exec_match_cmd(cmd, c->argv[EXEC_IDX_TRIGGER_CMDS]) == TRUE) {
1361 int res;
1362 unsigned int path_idx = EXEC_IDX_LOGFMTS+1;
1363 const char *path;
1364
1365 path = c->argv[path_idx];
1366 res = exec_ssystem(cmd, c, 0);
1367 if (res != 0) {
1368 exec_log("%s ExecOnCommand '%s' failed: %s", (char *) cmd->argv[0],
1369 path, strerror(res));
1370
1371 } else {
1372 exec_log("%s ExecOnCommand '%s' succeeded", (char *) cmd->argv[0],
1373 path);
1374 }
1375 }
1376
1377 c = find_config_next(c, c->next, CONF_PARAM, "ExecOnCommand", FALSE);
1378 }
1379
1380 return PR_DECLINED(cmd);
1381 }
1382
1383 MODRET exec_post_cmd_err(cmd_rec *cmd) {
1384 config_rec *c = NULL;
1385 array_header *seen_execs = NULL;
1386
1387 if (exec_engine == FALSE) {
1388 return PR_DECLINED(cmd);
1389 }
1390
1391 if (exec_enabled() != TRUE) {
1392 return PR_DECLINED(cmd);
1393 }
1394
1395 /* Create an array that will contain the IDs of the Execs we've
1396 * already processed.
1397 */
1398 seen_execs = make_array(cmd->tmp_pool, 0, sizeof(unsigned int));
1399
1400 c = find_config(CURRENT_CONF, CONF_PARAM, "ExecOnError", FALSE);
1401 while (c != NULL) {
1402 pr_signals_handle();
1403
1404 /* If we've already seen this Exec, skip on to the next Exec. */
1405 if (seen_execs->nelts > 0) {
1406 register unsigned int i = 0;
1407 unsigned char saw_exec = FALSE;
1408 unsigned int id = *((unsigned int *) c->argv[0]), *ids = seen_execs->elts;
1409
1410 for (i = 0; i < seen_execs->nelts; i++) {
1411 if (ids[i] == id) {
1412 saw_exec = TRUE;
1413 break;
1414 }
1415 }
1416
1417 if (saw_exec == TRUE) {
1418 exec_log("already saw this Exec, skipping");
1419 c = find_config_next(c, c->next, CONF_PARAM, "ExecOnError", FALSE);
1420 continue;
1421 }
1422 }
1423
1424 /* Add this Exec's ID to the list of seen Execs. */
1425 *((unsigned int *) push_array(seen_execs)) = *((unsigned int *) c->argv[0]);
1426
1427 /* Check the command list for this program against the errored command. */
1428 if (exec_match_cmd(cmd, c->argv[EXEC_IDX_TRIGGER_CMDS]) == TRUE) {
1429 int res;
1430 unsigned int path_idx = EXEC_IDX_LOGFMTS+1;
1431 const char *path;
1432
1433 path = c->argv[path_idx];
1434 res = exec_ssystem(cmd, c, 0);
1435 if (res != 0) {
1436 exec_log("%s ExecOnError '%s' failed: %s", (char *) cmd->argv[0],
1437 path, strerror(res));
1438
1439 } else {
1440 exec_log("%s ExecOnError '%s' succeeded", (char *) cmd->argv[0],
1441 path);
1442 }
1443 }
1444
1445 c = find_config_next(c, c->next, CONF_PARAM, "ExecOnError", FALSE);
1446 }
1447
1448 return PR_DECLINED(cmd);
1449 }
1450
1451 /* Configuration directive handlers
1452 */
1453
1454 /* usage: ExecBeforeCommand cmds path [args] */
1455 MODRET set_execbeforecommand(cmd_rec *cmd) {
1456 config_rec *c = NULL;
1457 unsigned int path_idx = 2;
1458 char *path;
1459
1460 if (cmd->argc-1 < 2) {
1461 CONF_ERROR(cmd, "wrong number of parameters");
1462 }
1463
1464 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON|
1465 CONF_DIR);
1466
1467 path = cmd->argv[path_idx];
1468 if (*path != '/') {
1469 CONF_ERROR(cmd, "path to program must be a full path");
1470 }
1471
1472 c = add_config_param(cmd->argv[0], 0);
1473 c->argc = cmd->argc + 2;
1474 c->argv = pcalloc(c->pool, sizeof(void *) * (c->argc + 2));
1475
1476 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1477 *((unsigned int *) c->argv[0]) = exec_nexecs++;
1478
1479 exec_parse_trigger_cmds(c, cmd->argv[1]);
1480
1481 /* Store the executable path. */
1482 c->argv[EXEC_IDX_LOGFMTS+1] = pstrdup(c->pool, path);
1483
1484 exec_parse_cmd_args(c, cmd, path_idx+1);
1485
1486 c->flags |= CF_MERGEDOWN_MULTI;
1487 return PR_HANDLED(cmd);
1488 }
1489
1490 /* usage: ExecEnable on|off */
1491 MODRET set_execenable(cmd_rec *cmd) {
1492 int enable = -1;
1493 config_rec *c;
1494
1495 CHECK_ARGS(cmd, 1);
1496 CHECK_CONF(cmd, CONF_ANON|CONF_DIR|CONF_DYNDIR);
1497
1498 enable = get_boolean(cmd, 1);
1499 if (enable == -1) {
1500 CONF_ERROR(cmd, "expected Boolean parameter");
1501 }
1502
1503 c = add_config_param(cmd->argv[0], 1, NULL);
1504 c->argv[0] = palloc(c->pool, sizeof(int));
1505 *((int *) c->argv[0]) = enable;
1506
1507 return PR_HANDLED(cmd);
1508 }
1509
1510 /* usage: ExecEngine on|off */
1511 MODRET set_execengine(cmd_rec *cmd) {
1512 int engine = -1;
1513 config_rec *c = NULL;
1514
1515 CHECK_ARGS(cmd, 1);
1516 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1517
1518 engine = get_boolean(cmd, 1);
1519 if (engine == -1) {
1520 CONF_ERROR(cmd, "expected Boolean parameter");
1521 }
1522
1523 c = add_config_param(cmd->argv[0], 1, NULL);
1524 c->argv[0] = pcalloc(c->pool, sizeof(int));
1525 *((int *) c->argv[0]) = engine;
1526
1527 /* Also set this here, for the daemon process. */
1528 exec_engine = engine;
1529
1530 return PR_HANDLED(cmd);
1531 }
1532
1533 /* usage: ExecEnviron variable value */
1534 MODRET set_execenviron(cmd_rec *cmd) {
1535 register unsigned int i = 0;
1536 config_rec *c = NULL;
1537 int res;
1538 char *key, *text;
1539 pr_jot_ctx_t *jot_ctx;
1540 pr_jot_parsed_t *jot_parsed;
1541 unsigned char parsed_buf[1024], *logfmt = NULL;
1542 size_t logfmt_len = 0;
1543
1544 CHECK_ARGS(cmd, 2);
1545 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1546
1547 c = add_config_param_str(cmd->argv[0], 3, NULL, cmd->argv[2], NULL);
1548
1549 /* Make sure the given environment variable name is uppercased.
1550 * NOTE: Are there cases where this SHOULD NOT happen? Why should
1551 * environment variable names always be uppercased?
1552 */
1553 key = cmd->argv[1];
1554
1555 for (i = 0; i < strlen(key); i++) {
1556 key[i] = toupper(key[i]);
1557 }
1558
1559 c->argv[0] = pstrdup(c->pool, key);
1560
1561 text = cmd->argv[2];
1562
1563 jot_parsed = pcalloc(cmd->tmp_pool, sizeof(pr_jot_parsed_t));
1564 jot_parsed->bufsz = jot_parsed->buflen = sizeof(parsed_buf);
1565 jot_parsed->ptr = jot_parsed->buf = parsed_buf;
1566
1567 jot_ctx = pcalloc(cmd->tmp_pool, sizeof(pr_jot_ctx_t));
1568 jot_ctx->log = jot_parsed;
1569
1570 res = pr_jot_parse_logfmt(cmd->tmp_pool, text, jot_ctx, pr_jot_parse_on_meta,
1571 pr_jot_parse_on_unknown, pr_jot_parse_on_other, 0);
1572 if (res < 0) {
1573 pr_log_pri(PR_LOG_INFO, MOD_EXEC_VERSION ": error parsing '%s': %s",
1574 text, strerror(errno));
1575 logfmt = (unsigned char *) text;
1576 logfmt_len = strlen(text);
1577
1578 } else {
1579 logfmt_len = jot_parsed->bufsz - jot_parsed->buflen;
1580 logfmt = palloc(cmd->tmp_pool, logfmt_len + 1);
1581 memcpy(logfmt, parsed_buf, logfmt_len);
1582 logfmt[logfmt_len] = '\0';
1583 }
1584
1585 c->argv[1] = pstrdup(c->pool, text);
1586 c->argv[2] = pstrndup(c->pool, (char *) logfmt, logfmt_len);
1587
1588 if (pr_module_exists("mod_ifsession.c")) {
1589 /* These are needed in case this directive is used with mod_ifsession
1590 * configuration.
1591 */
1592 c->flags |= CF_MULTI;
1593 }
1594
1595 return PR_HANDLED(cmd);
1596 }
1597
1598 /* usage: ExecLog path|"none" */
1599 MODRET set_execlog(cmd_rec *cmd) {
1600 CHECK_ARGS(cmd, 1);
1601 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1602
1603 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1604 return PR_HANDLED(cmd);
1605 }
1606
1607 /* usage: ExecOnCommand cmds path [args] */
1608 MODRET set_execoncommand(cmd_rec *cmd) {
1609 config_rec *c = NULL;
1610 unsigned int path_idx = 2;
1611 char *path;
1612
1613 if (cmd->argc-1 < 2) {
1614 CONF_ERROR(cmd, "wrong number of parameters");
1615 }
1616
1617 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON|
1618 CONF_DIR);
1619
1620 path = cmd->argv[path_idx];
1621 if (*path != '/') {
1622 CONF_ERROR(cmd, "path to program must be a full path");
1623 }
1624
1625 c = add_config_param(cmd->argv[0], 0);
1626 c->argc = cmd->argc + 2;
1627 c->argv = pcalloc(c->pool, sizeof(void *) * (c->argc + 2));
1628
1629 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1630 *((unsigned int *) c->argv[0]) = exec_nexecs++;
1631
1632 exec_parse_trigger_cmds(c, cmd->argv[1]);
1633
1634 /* Store the executable path. */
1635 c->argv[EXEC_IDX_LOGFMTS+1] = pstrdup(c->pool, path);
1636
1637 exec_parse_cmd_args(c, cmd, path_idx+1);
1638
1639 c->flags |= CF_MERGEDOWN_MULTI;
1640 return PR_HANDLED(cmd);
1641 }
1642
1643 /* usage: ExecOnConnect path [args] */
1644 MODRET set_execonconnect(cmd_rec *cmd) {
1645 config_rec *c = NULL;
1646 unsigned int path_idx = 1;
1647 char *path;
1648
1649 if (cmd->argc-1 < 1) {
1650 CONF_ERROR(cmd, "wrong number of parameters");
1651 }
1652
1653 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1654
1655 path = cmd->argv[path_idx];
1656 if (*path != '/') {
1657 CONF_ERROR(cmd, "path to program must be a full path");
1658 }
1659
1660 c = add_config_param(cmd->argv[0], 0);
1661 c->argc = cmd->argc + 2;
1662 c->argv = pcalloc(c->pool, sizeof(void *) * (c->argc + 2));
1663
1664 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1665 *((unsigned int *) c->argv[0]) = exec_nexecs++;
1666
1667 if (pr_module_exists("mod_ifsession.c")) {
1668 /* These are needed in case this directive is used with mod_ifsession
1669 * configuration.
1670 */
1671 c->flags |= CF_MULTI;
1672 }
1673
1674 /* Store the executable path. */
1675 c->argv[EXEC_IDX_LOGFMTS+1] = pstrdup(c->pool, path);
1676
1677 exec_parse_cmd_args(c, cmd, path_idx+1);
1678 return PR_HANDLED(cmd);
1679 }
1680
1681 /* usage: ExecOnError cmds path [args] */
1682 MODRET set_execonerror(cmd_rec *cmd) {
1683 config_rec *c = NULL;
1684 unsigned int path_idx = 2;
1685 char *path;
1686
1687 if (cmd->argc-1 < 2) {
1688 CONF_ERROR(cmd, "wrong number of parameters");
1689 }
1690
1691 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON|
1692 CONF_DIR);
1693
1694 path = cmd->argv[path_idx];
1695 if (*path != '/') {
1696 CONF_ERROR(cmd, "path to program must be a full path");
1697 }
1698
1699 c = add_config_param(cmd->argv[0], 0);
1700 c->argc = cmd->argc + 2;
1701 c->argv = pcalloc(c->pool, sizeof(void *) * (c->argc + 2));
1702
1703 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1704 *((unsigned int *) c->argv[0]) = exec_nexecs++;
1705
1706 exec_parse_trigger_cmds(c, cmd->argv[1]);
1707
1708 /* Store the executable path. */
1709 c->argv[EXEC_IDX_LOGFMTS+1] = pstrdup(c->pool, path);
1710
1711 exec_parse_cmd_args(c, cmd, path_idx+1);
1712
1713 c->flags |= CF_MERGEDOWN_MULTI;
1714 return PR_HANDLED(cmd);
1715 }
1716
1717 /* usage: ExecOnEvent event path [args] */
1718 MODRET set_execonevent(cmd_rec *cmd) {
1719 unsigned int flags = EXEC_FL_CLEAR_GROUPS|EXEC_FL_NO_SEND;
1720 unsigned int path_idx = 2;
1721 char *event_name, *path;
1722 size_t event_namelen;
1723 config_rec *c;
1724 struct exec_event_data *eed;
1725
1726 if (cmd->argc-1 < 2) {
1727 CONF_ERROR(cmd, "wrong number of parameters");
1728 }
1729
1730 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1731
1732 event_name = cmd->argv[1];
1733 event_namelen = strlen(event_name);
1734
1735 if (event_name[event_namelen-1] == '*') {
1736 flags |= EXEC_FL_RUN_AS_ROOT;
1737 event_name[event_namelen-1] = '\0';
1738 event_namelen--;
1739
1740 } else if (event_name[event_namelen-1] == '~') {
1741 flags |= EXEC_FL_RUN_AS_USER;
1742 event_name[event_namelen-1] = '\0';
1743 event_namelen--;
1744 }
1745
1746 path = cmd->argv[path_idx];
1747 if (*path != '/') {
1748 CONF_ERROR(cmd, "path to program must be a full path");
1749 }
1750
1751 c = pcalloc(cmd->server->pool, sizeof(config_rec));
1752 c->pool = make_sub_pool(cmd->server->pool);
1753 pr_pool_tag(c->pool, cmd->argv[0]);
1754 c->argc = cmd->argc + 2;
1755 c->argv = pcalloc(c->pool, sizeof(void *) * (c->argc + 2));
1756
1757 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1758 c->argv[1] = NULL;
1759
1760 if (pr_module_exists("mod_ifsession.c")) {
1761 /* These are needed in case this directive is used with mod_ifsession
1762 * configuration.
1763 */
1764 c->flags |= CF_MULTI;
1765 }
1766
1767 /* Store the executable path. */
1768 c->argv[EXEC_IDX_LOGFMTS+1] = pstrdup(c->pool, path);
1769
1770 exec_parse_cmd_args(c, cmd, path_idx+1);
1771
1772 eed = pcalloc(c->pool, sizeof(struct exec_event_data));
1773 eed->flags = flags;
1774 eed->event = pstrdup(c->pool, event_name);
1775 eed->c = c;
1776
1777 if (strcasecmp(eed->event, "MaxConnectionRate") == 0) {
1778 pr_event_register(&exec_module, "core.max-connection-rate", exec_any_ev,
1779 eed);
1780
1781 } else if (strcasecmp(eed->event, "MaxInstances") == 0) {
1782 pr_event_register(&exec_module, "core.max-instances", exec_any_ev, eed);
1783
1784 } else {
1785 /* Assume that the sysadmin knows the name of the event to use. */
1786 pr_event_register(&exec_module, eed->event, exec_any_ev, eed);
1787 }
1788
1789 return PR_HANDLED(cmd);
1790 }
1791
1792 /* usage: ExecOnExit path [args] */
1793 MODRET set_execonexit(cmd_rec *cmd) {
1794 config_rec *c = NULL;
1795 unsigned int path_idx = 1;
1796 char *path;
1797
1798 if (cmd->argc-1 < 1) {
1799 CONF_ERROR(cmd, "wrong number of parameters");
1800 }
1801
1802 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1803
1804 path = cmd->argv[path_idx];
1805 if (*path != '/') {
1806 CONF_ERROR(cmd, "path to program must be a full path");
1807 }
1808
1809 c = add_config_param(cmd->argv[0], 0);
1810 c->argc = cmd->argc + 2;
1811 c->argv = pcalloc(c->pool, sizeof(void *) * (c->argc + 2));
1812
1813 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1814 *((unsigned int *) c->argv[0]) = exec_nexecs++;
1815
1816 if (pr_module_exists("mod_ifsession.c")) {
1817 /* These are needed in case this directive is used with mod_ifsession
1818 * configuration.
1819 */
1820 c->flags |= CF_MULTI;
1821 }
1822
1823 /* Store the executable path. */
1824 c->argv[EXEC_IDX_LOGFMTS+1] = pstrdup(c->pool, path);
1825
1826 exec_parse_cmd_args(c, cmd, path_idx+1);
1827 return PR_HANDLED(cmd);
1828 }
1829
1830 /* usage: ExecOnRestart path [args] */
1831 MODRET set_execonrestart(cmd_rec *cmd) {
1832 config_rec *c = NULL;
1833 unsigned int path_idx = 1;
1834 char *path;
1835
1836 if (cmd->argc-1 < 1) {
1837 CONF_ERROR(cmd, "wrong number of parameters");
1838 }
1839
1840 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1841
1842 path = cmd->argv[path_idx];
1843 if (*path != '/') {
1844 CONF_ERROR(cmd, "path to program must be a full path");
1845 }
1846
1847 c = add_config_param(cmd->argv[0], 0);
1848 c->argc = cmd->argc + 2;
1849 c->argv = pcalloc(c->pool, sizeof(void *) * (c->argc + 2));
1850
1851 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1852 *((unsigned int *) c->argv[0]) = exec_nexecs++;
1853
1854 if (pr_module_exists("mod_ifsession.c")) {
1855 /* These are needed in case this directive is used with mod_ifsession
1856 * configuration.
1857 */
1858 c->flags |= CF_MULTI;
1859 }
1860
1861 /* Store the executable path. */
1862 c->argv[EXEC_IDX_LOGFMTS+1] = pstrdup(c->pool, path);
1863
1864 exec_parse_cmd_args(c, cmd, path_idx+1);
1865 return PR_HANDLED(cmd);
1866 }
1867
1868 /* usage: ExecOptions opt1 opt2 ... optN */
1869 MODRET set_execoptions(cmd_rec *cmd) {
1870 config_rec *c = NULL;
1871 register unsigned int i;
1872 unsigned int opts = 0U;
1873
1874 if (cmd->argc-1 == 0) {
1875 CONF_ERROR(cmd, "wrong number of parameters");
1876 }
1877
1878 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1879
1880 c = add_config_param(cmd->argv[0], 1, NULL);
1881
1882 for (i = 1; i < cmd->argc; i++) {
1883 if (strcmp(cmd->argv[i], "logStdout") == 0) {
1884 opts |= EXEC_OPT_LOG_STDOUT;
1885
1886 } else if (strcmp(cmd->argv[i], "logStderr") == 0) {
1887 opts |= EXEC_OPT_LOG_STDERR;
1888
1889 } else if (strcmp(cmd->argv[i], "sendStdout") == 0) {
1890 opts |= EXEC_OPT_SEND_STDOUT;
1891
1892 } else if (strcmp(cmd->argv[i], "useStdin") == 0) {
1893 opts |= EXEC_OPT_USE_STDIN;
1894
1895 } else {
1896 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown ExecOption: '",
1897 (char *) cmd->argv[i], "'", NULL));
1898 }
1899 }
1900
1901 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
1902 *((unsigned int *) c->argv[0]) = opts;
1903
1904 if (pr_module_exists("mod_ifsession.c")) {
1905 /* These are needed in case this directive is used with mod_ifsession
1906 * configuration.
1907 */
1908 c->flags |= CF_MULTI;
1909 }
1910
1911 return PR_HANDLED(cmd);
1912 }
1913
1914 /* usage: ExecTimeout <seconds> */
1915 MODRET set_exectimeout(cmd_rec *cmd) {
1916 int timeout = -1;
1917 config_rec *c = NULL;
1918
1919 CHECK_ARGS(cmd, 1);
1920 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1921
1922 if (pr_str_get_duration(cmd->argv[1], &timeout) < 0) {
1923 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '",
1924 cmd->argv[1], "': ", strerror(errno), NULL));
1925 }
1926
1927 c = add_config_param(cmd->argv[0], 1, NULL);
1928 c->argv[0] = pcalloc(c->pool, sizeof(int));
1929 *((int *) c->argv[0]) = timeout;
1930
1931 return PR_HANDLED(cmd);
1932 }
1933
1934 /* Event handlers
1935 */
1936
1937 static void exec_any_ev(const void *event_data, void *user_data) {
1938 struct exec_event_data *eed = user_data;
1939 int res;
1940 unsigned int path_idx = EXEC_IDX_LOGFMTS+1;
1941 const char *path;
1942
1943 if (exec_engine == FALSE) {
1944 return;
1945 }
1946
1947 path = eed->c->argv[path_idx];
1948 res = exec_ssystem(NULL, eed->c, eed->flags);
1949 if (res != 0) {
1950 exec_log("ExecOnEvent '%s' for %s failed: %s", eed->event,
1951 path, strerror(res));
1952
1953 } else {
1954 exec_log("ExecOnEvent '%s' for %s succeeded", eed->event, path);
1955 }
1956 }
1957
1958 #if defined(PR_SHARED_MODULE)
1959 static void exec_mod_unload_ev(const void *event_data, void *user_data) {
1960 if (strcmp("mod_exec.c", (const char *) event_data) != 0) {
1961 return;
1962 }
1963
1964 pr_event_unregister(&exec_module, NULL, NULL);
1965
1966 if (exec_pool != NULL) {
1967 destroy_pool(exec_pool);
1968 exec_pool = NULL;
1969 }
1970
1971 (void) close(exec_logfd);
1972 exec_logfd = -1;
1973 }
1974 #endif /* PR_SHARED_MODULE */
1975
1976 static void exec_postparse_ev(const void *event_data, void *user_data) {
1977 exec_openlog();
1978 }
1979
1980 static void exec_restart_ev(const void *event_data, void *user_data) {
1981 if (exec_pool != NULL) {
1982 destroy_pool(exec_pool);
1983 exec_pool = NULL;
1984 }
1985
1986 if (exec_engine == TRUE) {
1987 config_rec *c = NULL;
1988 cmd_rec *cmd = NULL;
1989 pool *tmp_pool;
1990
1991 exec_pool = make_sub_pool(permanent_pool);
1992 pr_pool_tag(exec_pool, MOD_EXEC_VERSION);
1993
1994 /* Make sure the User/Group IDs are set, so the the PRIVS_REVOKE call
1995 * later succeeds properly.
1996 */
1997 {
1998 uid_t *uid = (uid_t *) get_param_ptr(main_server->conf, "UserID", FALSE);
1999 gid_t *gid = (gid_t *) get_param_ptr(main_server->conf, "GroupID", FALSE);
2000
2001 session.uid = uid ? *uid : geteuid();
2002 session.gid = gid ? *gid : getegid();
2003 }
2004
2005 tmp_pool = make_sub_pool(exec_pool);
2006 cmd = pr_cmd_alloc(tmp_pool, 1, pstrdup(tmp_pool, "RESTART"));
2007
2008 c = find_config(main_server->conf, CONF_PARAM, "ExecOnRestart", FALSE);
2009 while (c != NULL) {
2010 int res;
2011 unsigned int path_idx = EXEC_IDX_LOGFMTS+1;
2012 const char *path;
2013
2014 pr_signals_handle();
2015
2016 path = c->argv[path_idx];
2017 res = exec_ssystem(cmd, c, EXEC_FL_CLEAR_GROUPS|EXEC_FL_NO_SEND);
2018 if (res != 0) {
2019 exec_log("ExecOnRestart '%s' failed: %s", path, strerror(res));
2020
2021 } else {
2022 exec_log("ExecOnRestart '%s' succeeded", path);
2023 }
2024
2025 c = find_config_next(c, c->next, CONF_PARAM, "ExecOnRestart", FALSE);
2026 }
2027
2028 destroy_pool(tmp_pool);
2029 }
2030
2031 pr_event_unregister(&exec_module, "core.max-connection-rate", NULL);
2032 pr_event_unregister(&exec_module, "core.max-instances", NULL);
2033
2034 /* Bounce the log file descriptor. */
2035 exec_closelog();
2036 exec_openlog();
2037 }
2038
2039 static void exec_sess_reinit_ev(const void *event_data, void *user_data) {
2040 int res;
2041
2042 /* A HOST command changed the main_server pointer, reinitialize ourselves. */
2043
2044 pr_event_unregister(&exec_module, "core.session-reinit", exec_sess_reinit_ev);
2045
2046 exec_engine = FALSE;
2047 exec_opts = 0U;
2048 exec_timeout = 0;
2049
2050 (void) close(exec_logfd);
2051 exec_logfd = -1;
2052 exec_logname = NULL;
2053
2054 res = exec_sess_init();
2055 if (res < 0) {
2056 pr_session_disconnect(&exec_module,
2057 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
2058 }
2059 }
2060
2061 /* Initialization routines
2062 */
2063
2064 static int exec_sess_init(void) {
2065 int *use_exec = NULL;
2066 config_rec *c = NULL;
2067 const char *proto;
2068 pool *tmp_pool = NULL;
2069 cmd_rec *cmd = NULL;
2070
2071 pr_event_register(&exec_module, "core.session-reinit", exec_sess_reinit_ev,
2072 NULL);
2073
2074 use_exec = get_param_ptr(main_server->conf, "ExecEngine", FALSE);
2075 if (use_exec != NULL &&
2076 *use_exec == TRUE) {
2077 exec_engine = TRUE;
2078
2079 } else {
2080 exec_engine = FALSE;
2081 return 0;
2082 }
2083
2084 c = find_config(main_server->conf, CONF_PARAM, "ExecOptions", FALSE);
2085 while (c != NULL) {
2086 unsigned long opts;
2087
2088 pr_signals_handle();
2089
2090 opts = *((unsigned int *) c->argv[0]);
2091 exec_opts |= opts;
2092
2093 c = find_config_next(c, c->next, CONF_PARAM, "ExecOptions", FALSE);
2094 }
2095
2096 /* If we are handling an SSH2 session, then disable the sendStdout
2097 * ExecOption, if present.
2098 *
2099 * Attempting to send the stdout of commands to connecting SSH2 clients
2100 * can confuse them and lead to connection problems.
2101 */
2102 proto = pr_session_get_protocol(0);
2103 if (strcmp(proto, "ssh2") == 0) {
2104 exec_opts &= ~EXEC_OPT_SEND_STDOUT;
2105 }
2106
2107 c = find_config(main_server->conf, CONF_PARAM, "ExecTimeout", FALSE);
2108 if (c != NULL) {
2109 exec_timeout = *((int *) c->argv[0]);
2110 }
2111
2112 exec_closelog();
2113 exec_openlog();
2114
2115 /* Make sure the User/Group IDs are set, so the the PRIVS_REVOKE call
2116 * later succeeds properly.
2117 */
2118 {
2119 uid_t *uid = (uid_t *) get_param_ptr(main_server->conf, "UserID", FALSE);
2120 gid_t *gid = (gid_t *) get_param_ptr(main_server->conf, "GroupID", FALSE);
2121
2122 session.uid = uid ? *uid : geteuid();
2123 session.gid = gid ? *gid : getegid();
2124 }
2125
2126 /* Create fake "CONNECT" command for any ExecOnConnect directives. */
2127 tmp_pool = make_sub_pool(exec_pool);
2128 pr_pool_tag(tmp_pool, "exec sess init pool");
2129
2130 cmd = pr_cmd_alloc(tmp_pool, 1, pstrdup(tmp_pool, "CONNECT"));
2131 cmd->cmd_class |= CL_CONNECT;
2132
2133 c = find_config(main_server->conf, CONF_PARAM, "ExecOnConnect", FALSE);
2134 while (c != NULL) {
2135 int res;
2136 unsigned int path_idx = EXEC_IDX_LOGFMTS+1;
2137 const char *path;
2138
2139 pr_signals_handle();
2140
2141 path = c->argv[path_idx];
2142 res = exec_ssystem(cmd, c, EXEC_FL_CLEAR_GROUPS|EXEC_FL_USE_SEND);
2143 if (res != 0) {
2144 exec_log("ExecOnConnect '%s' failed: %s", path, strerror(res));
2145
2146 } else {
2147 exec_log("ExecOnConnect '%s' succeeded", path);
2148 }
2149
2150 c = find_config_next(c, c->next, CONF_PARAM, "ExecOnConnect", FALSE);
2151 }
2152
2153 destroy_pool(tmp_pool);
2154 return 0;
2155 }
2156
2157 static int exec_init(void) {
2158 exec_pool = make_sub_pool(permanent_pool);
2159 pr_pool_tag(exec_pool, MOD_EXEC_VERSION);
2160
2161 /* Register event handlers. */
2162 #if defined(PR_SHARED_MODULE)
2163 pr_event_register(&exec_module, "core.module-unload", exec_mod_unload_ev,
2164 NULL);
2165 #endif /* PR_SHARED_MODULE */
2166 pr_event_register(&exec_module, "core.postparse", exec_postparse_ev, NULL);
2167 pr_event_register(&exec_module, "core.restart", exec_restart_ev, NULL);
2168
2169 return 0;
2170 }
2171
2172 /* Module API tables
2173 */
2174
2175 static conftable exec_conftab[] = {
2176 { "ExecBeforeCommand",set_execbeforecommand, NULL },
2177 { "ExecEnable", set_execenable, NULL },
2178 { "ExecEngine", set_execengine, NULL },
2179 { "ExecEnviron", set_execenviron, NULL },
2180 { "ExecLog", set_execlog, NULL },
2181 { "ExecOnCommand", set_execoncommand, NULL },
2182 { "ExecOnConnect", set_execonconnect, NULL },
2183 { "ExecOnError", set_execonerror, NULL },
2184 { "ExecOnEvent", set_execonevent, NULL },
2185 { "ExecOnExit", set_execonexit, NULL },
2186 { "ExecOnRestart", set_execonrestart, NULL },
2187 { "ExecOptions", set_execoptions, NULL },
2188 { "ExecTimeout", set_exectimeout, NULL },
2189 { NULL }
2190 };
2191
2192 static cmdtable exec_cmdtab[] = {
2193 { LOG_CMD, "EXIT", G_NONE, exec_log_exit, FALSE, FALSE },
2194 { PRE_CMD, C_ANY, G_NONE, exec_pre_cmd, FALSE, FALSE },
2195 { POST_CMD, C_ANY, G_NONE, exec_post_cmd, FALSE, FALSE },
2196 { POST_CMD_ERR, C_ANY, G_NONE, exec_post_cmd_err, FALSE, FALSE },
2197 { 0, NULL }
2198 };
2199
2200 module exec_module = {
2201
2202 /* Always NULL */
2203 NULL, NULL,
2204
2205 /* Module API version */
2206 0x20,
2207
2208 /* Module name */
2209 "exec",
2210
2211 /* Configuration handler table */
2212 exec_conftab,
2213
2214 /* Command handler table */
2215 exec_cmdtab,
2216
2217 /* Authentication handler table */
2218 NULL,
2219
2220 /* Module initialization */
2221 exec_init,
2222
2223 /* Session initialization */
2224 exec_sess_init,
2225
2226 /* Module version */
2227 MOD_EXEC_VERSION
2228 };