2 * Copyright (c) 2009-2010 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/socket.h>
29 #endif /* HAVE_TERMIOS_H */
30 #include <sys/ioctl.h>
31 #ifdef HAVE_SYS_SELECT_H
32 # include <sys/select.h>
33 #endif /* HAVE_SYS_SELECT_H */
42 #endif /* STDC_HEADERS */
45 #endif /* HAVE_STRING_H */
48 #endif /* HAVE_STRINGS_H */
51 #endif /* HAVE_UNISTD_H */
52 #if TIME_WITH_SYS_TIME
62 # include <selinux/selinux.h>
66 #include "sudo_exec.h"
68 /* shared with exec_pty.c */
69 sig_atomic_t recvsig[NSIG];
70 void handler __P((int s));
73 * Like execve(2) but falls back to running through /bin/sh
74 * ala execvp(3) if we get ENOEXEC.
77 my_execve(path, argv, envp)
82 execve(path, argv, envp);
83 if (errno == ENOEXEC) {
84 argv--; /* at least one extra slot... */
86 argv[1] = (char *)path;
87 execve(_PATH_BSHELL, argv, envp);
93 * Fork and execute a command, returns the child's pid.
94 * Sends errno back on sv[1] if execve() fails.
96 static int fork_cmnd(path, argv, envp, sv, rbac_enabled)
103 struct command_status cstat;
114 fcntl(sv[1], F_SETFD, FD_CLOEXEC);
115 if (exec_setup(rbac_enabled, user_ttypath, -1) == TRUE) {
116 /* headed for execve() */
117 closefrom(def_closefrom);
120 selinux_execve(path, argv, envp);
123 my_execve(path, argv, envp);
125 cstat.type = CMD_ERRNO;
127 send(sv[1], &cstat, sizeof(cstat), 0);
134 * Execute a command, potentially in a pty with I/O loggging.
135 * This is a little bit tricky due to how POSIX job control works and
136 * we fact that we have two different controlling terminals to deal with.
139 sudo_execve(path, argv, envp, uid, cstat, dowait, bgmode)
144 struct command_status *cstat;
150 int maxfd, n, nready, status, sv[2];
151 int rbac_enabled = 0;
155 /* If running in background mode, fork and exit. */
159 cstat->type = CMD_ERRNO;
163 /* child continues */
171 #ifdef _PATH_SUDO_IO_LOGDIR
172 log_io = def_log_output || def_log_input || def_use_pty;
179 #endif /* _PATH_SUDO_IO_LOGDIR */
182 rbac_enabled = is_selinux_enabled() > 0 && user_role != NULL;
188 * If we don't need to wait for the command to finish, just exec it.
191 exec_setup(FALSE, NULL, -1);
192 closefrom(def_closefrom);
193 my_execve(path, argv, envp);
194 cstat->type = CMD_ERRNO;
200 * We communicate with the child over a bi-directional pair of sockets.
201 * Parent sends signal info to child and child sends back wait status.
203 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) != 0)
204 error(1, "cannot create sockets");
206 zero_bytes(&sa, sizeof(sa));
207 sigemptyset(&sa.sa_mask);
209 /* Note: HP-UX select() will not be interrupted if SA_RESTART set */
210 sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */
211 sa.sa_handler = handler;
212 sigaction(SIGCHLD, &sa, NULL);
213 sigaction(SIGHUP, &sa, NULL);
214 sigaction(SIGINT, &sa, NULL);
215 sigaction(SIGPIPE, &sa, NULL);
216 sigaction(SIGQUIT, &sa, NULL);
217 sigaction(SIGTERM, &sa, NULL);
219 /* Max fd we will be selecting on. */
223 * Child will run the command in the pty, parent will pass data
224 * to and from pty. Adjusts maxfd as needed.
226 #ifdef _PATH_SUDO_IO_LOGDIR
228 child = fork_pty(path, argv, envp, sv, rbac_enabled, &maxfd);
231 child = fork_cmnd(path, argv, envp, sv, rbac_enabled);
234 #ifdef HAVE_SETLOCALE
236 * I/O logging must be in the C locale for floating point numbers
237 * to be logged consistently.
239 setlocale(LC_ALL, "C");
243 * In the event loop we pass input from user tty to master
244 * and pass output from master to stdout and IO plugin.
246 fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
247 fdsw = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
249 if (recvsig[SIGCHLD]) {
253 * If logging I/O, child is the intermediate process,
254 * otherwise it is the command itself.
256 recvsig[SIGCHLD] = FALSE;
259 pid = sudo_waitpid(child, &status, WUNTRACED|WNOHANG);
265 if (WIFSTOPPED(status)) {
266 /* Child may not have privs to suspend us itself. */
267 kill(getpid(), WSTOPSIG(status));
269 /* Child has exited, we are done. */
270 cstat->type = CMD_WSTATUS;
275 /* Else we get ECONNRESET on sv[0] if child dies. */
277 } while (pid != -1 || errno == EINTR);
280 zero_bytes(fdsw, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
281 zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
284 #ifdef _PATH_SUDO_IO_LOGDIR
286 fd_set_iobs(fdsr, fdsw); /* XXX - better name */
288 for (n = 0; n < NSIG; n++) {
289 if (recvsig[n] && n != SIGCHLD) {
294 /* nothing listening on sv[0], send directly */
300 if (recvsig[SIGCHLD])
302 nready = select(maxfd + 1, fdsr, fdsw, NULL, NULL);
306 error(1, "select failed");
308 if (FD_ISSET(sv[0], fdsr)) {
309 /* read child status */
310 n = recv(sv[0], cstat, sizeof(*cstat), 0);
315 * If not logging I/O we will receive ECONNRESET when
316 * the command is executed. It is safe to ignore this.
318 if (log_io && errno != EAGAIN) {
319 cstat->type = CMD_ERRNO;
324 #ifdef _PATH_SUDO_IO_LOGDIR /* XXX */
325 if (cstat->type == CMD_WSTATUS) {
326 if (WIFSTOPPED(cstat->val)) {
327 /* Suspend parent and tell child how to resume on return. */
328 n = suspend_parent(WSTOPSIG(cstat->val));
332 /* Child exited or was killed, either way we are done. */
336 #endif /* _PATH_SUDO_IO_LOGDIR */
337 if (cstat->type == CMD_ERRNO) {
338 /* Child was unable to execute command or broken pipe. */
343 #ifdef _PATH_SUDO_IO_LOGDIR
344 /* XXX - move this too */
345 if (FD_ISSET(sv[0], fdsw)) {
346 for (n = 0; n < NSIG; n++) {
350 cstat->type = CMD_SIGNO;
353 n = send(sv[0], cstat, sizeof(*cstat), 0);
354 } while (n == -1 && errno == EINTR);
355 if (n != sizeof(*cstat)) {
361 if (perform_io(fdsr, fdsw, cstat) != 0)
363 #endif /* _PATH_SUDO_IO_LOGDIR */
366 #ifdef _PATH_SUDO_IO_LOGDIR
368 /* Flush any remaining output and free pty-related memory. */
371 #endif /* _PATH_SUDO_IO_LOGDIR */
375 /* This is probably not needed in log_io mode. */
376 if (selinux_restore_tty() != 0)
377 warningx("unable to restore tty label");
384 return cstat->type == CMD_ERRNO ? -1 : 0;
388 * Generic handler for signals passed from parent -> child.
389 * The recvsig[] array is checked in the main event loop.