1  /*
2   * shell_cmd() takes a shell command after %<character> substitutions. The
3   * command is executed by a /bin/sh child process, with standard input,
4   * standard output and standard error connected to /dev/null.
5   *
6   * Diagnostics are reported through syslog(3).
7   *
8   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
9   */
10 
11 /* System libraries. */
12 
13 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <sys/wait.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <string.h>
21 #include <fcntl.h>
22 
23 extern void exit();
24 
25 /* Local stuff. */
26 
27 #include "tcpd.h"
28 
29 /* Forward declarations. */
30 
31 static void do_child();
32 
33 /* shell_cmd - execute shell command */
34 
35 void    shell_cmd(command)
36 char   *command;
37 {
38     int     child_pid;
39     int     wait_pid;
40 
41     /*
42      * Most of the work is done within the child process, to minimize the
43      * risk of damage to the parent.
44      */
45 
46     switch (child_pid = fork()) {
47     case -1:					/* error */
48 	tcpd_warn("cannot fork: %m");
49 	break;
50     case 00:					/* child */
51 	do_child(command);
52 	/* NOTREACHED */
53     default:					/* parent */
54 	while ((wait_pid = wait((int *) 0)) != -1 && wait_pid != child_pid)
55 	     /* void */ ;
56     }
57 }
58 
59 /* do_child - exec command with { stdin, stdout, stderr } to /dev/null */
60 
61 static void do_child(command)
62 char   *command;
63 {
64     char   *error;
65     int     tmp_fd;
66 
67     /*
68      * Systems with POSIX sessions may send a SIGHUP to grandchildren if the
69      * child exits first. This is sick, sessions were invented for terminals.
70      */
71 
72     signal(SIGHUP, SIG_IGN);
73 
74     /* Set up new stdin, stdout, stderr, and exec the shell command. */
75 
76     for (tmp_fd = 0; tmp_fd < 3; tmp_fd++)
77 	(void) close(tmp_fd);
78     if (open("/dev/null", 2) != 0) {
79 	error = "open /dev/null: %m";
80     } else if (dup(0) != 1 || dup(0) != 2) {
81 	error = "dup: %m";
82     } else {
83 	(void) execl("/bin/sh", "sh", "-c", command, (char *) 0);
84 	error = "execl /bin/sh: %m";
85     }
86 
87     /* Something went wrong. We MUST terminate the child process. */
88 
89     tcpd_warn("%s", error);
90     _exit(0);
91 }
92