#include <config.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#else
# include <termio.h>
#endif
#include <sys/ioctl.h>
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if TIME_WITH_SYS_TIME
# include <time.h>
#endif
#ifdef HAVE_SETLOCALE
# include <locale.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#ifdef HAVE_SELINUX
# include <selinux/selinux.h>
#endif
#include "sudo.h"
#include "sudo_exec.h"
volatile sig_atomic_t recvsig[NSIG];
void handler __P((int s));
int
my_execve(path, argv, envp)
const char *path;
char *argv[];
char *envp[];
{
execve(path, argv, envp);
if (errno == ENOEXEC) {
argv--;
argv[0] = "sh";
argv[1] = (char *)path;
execve(_PATH_BSHELL, argv, envp);
}
return -1;
}
static int fork_cmnd(path, argv, envp, sv, rbac_enabled)
const char *path;
char *argv[];
char *envp[];
int sv[2];
int rbac_enabled;
{
struct command_status cstat;
sigaction_t sa;
int pid;
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_INTERRUPT;
sa.sa_handler = handler;
sigaction(SIGCONT, &sa, NULL);
pid = fork();
switch (pid) {
case -1:
error(1, "fork");
break;
case 0:
close(sv[0]);
fcntl(sv[1], F_SETFD, FD_CLOEXEC);
if (exec_setup(rbac_enabled, user_ttypath, -1) == TRUE) {
closefrom(def_closefrom);
#ifdef HAVE_SELINUX
if (rbac_enabled)
selinux_execve(path, argv, envp);
else
#endif
my_execve(path, argv, envp);
}
cstat.type = CMD_ERRNO;
cstat.val = errno;
send(sv[1], &cstat, sizeof(cstat), 0);
_exit(1);
}
return pid;
}
int
sudo_execve(path, argv, envp, uid, cstat, dowait, bgmode)
const char *path;
char *argv[];
char *envp[];
uid_t uid;
struct command_status *cstat;
int dowait;
int bgmode;
{
sigaction_t sa;
fd_set *fdsr, *fdsw;
int maxfd, n, nready, status, sv[2];
int rbac_enabled = 0;
int log_io;
pid_t child;
if (bgmode) {
switch (fork()) {
case -1:
cstat->type = CMD_ERRNO;
cstat->val = errno;
return -1;
case 0:
break;
default:
exit(0);
}
}
#ifdef _PATH_SUDO_IO_LOGDIR
log_io = def_log_output || def_log_input || def_use_pty;
if (log_io) {
if (!bgmode)
pty_setup(uid);
io_log_open();
dowait = TRUE;
}
#endif
#ifdef HAVE_SELINUX
rbac_enabled = is_selinux_enabled() > 0 && user_role != NULL;
if (rbac_enabled)
dowait = TRUE;
#endif
if (!dowait) {
exec_setup(FALSE, NULL, -1);
closefrom(def_closefrom);
my_execve(path, argv, envp);
cstat->type = CMD_ERRNO;
cstat->val = errno;
return(127);
}
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) != 0)
error(1, "cannot create sockets");
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_INTERRUPT;
sa.sa_handler = handler;
sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
maxfd = sv[0];
#ifdef _PATH_SUDO_IO_LOGDIR
if (log_io)
child = fork_pty(path, argv, envp, sv, rbac_enabled, &maxfd);
else
#endif
child = fork_cmnd(path, argv, envp, sv, rbac_enabled);
close(sv[1]);
#ifdef HAVE_SETLOCALE
setlocale(LC_ALL, "C");
#endif
fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
fdsw = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
for (;;) {
if (recvsig[SIGCHLD]) {
pid_t pid;
recvsig[SIGCHLD] = FALSE;
do {
#ifdef sudo_waitpid
pid = sudo_waitpid(child, &status, WUNTRACED|WNOHANG);
#else
pid = wait(&status);
#endif
} while (pid == -1 && errno == EINTR);
if (pid == child) {
if (!log_io) {
if (WIFSTOPPED(status)) {
kill(getpid(), WSTOPSIG(status));
} else {
cstat->type = CMD_WSTATUS;
cstat->val = status;
return 0;
}
}
}
}
zero_bytes(fdsw, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask));
FD_SET(sv[0], fdsr);
#ifdef _PATH_SUDO_IO_LOGDIR
if (log_io)
fd_set_iobs(fdsr, fdsw);
#endif
for (n = 0; n < NSIG; n++) {
if (recvsig[n] && n != SIGCHLD) {
if (log_io) {
FD_SET(sv[0], fdsw);
break;
} else {
recvsig[n] = FALSE;
kill(child, n);
}
}
}
if (recvsig[SIGCHLD])
continue;
nready = select(maxfd + 1, fdsr, fdsw, NULL, NULL);
if (nready == -1) {
if (errno == EINTR)
continue;
error(1, "select failed");
}
if (FD_ISSET(sv[0], fdsr)) {
n = recv(sv[0], cstat, sizeof(*cstat), 0);
if (n == -1) {
if (errno == EINTR)
continue;
if (log_io && errno != EAGAIN) {
cstat->type = CMD_ERRNO;
cstat->val = errno;
break;
}
}
#ifdef _PATH_SUDO_IO_LOGDIR
if (cstat->type == CMD_WSTATUS) {
if (WIFSTOPPED(cstat->val)) {
n = suspend_parent(WSTOPSIG(cstat->val));
recvsig[n] = TRUE;
continue;
} else {
break;
}
} else
#endif
if (cstat->type == CMD_ERRNO) {
break;
}
}
#ifdef _PATH_SUDO_IO_LOGDIR
if (FD_ISSET(sv[0], fdsw)) {
for (n = 0; n < NSIG; n++) {
if (!recvsig[n])
continue;
recvsig[n] = FALSE;
cstat->type = CMD_SIGNO;
cstat->val = n;
do {
n = send(sv[0], cstat, sizeof(*cstat), 0);
} while (n == -1 && errno == EINTR);
if (n != sizeof(*cstat)) {
recvsig[n] = TRUE;
break;
}
}
}
if (perform_io(fdsr, fdsw, cstat) != 0)
break;
#endif
}
#ifdef _PATH_SUDO_IO_LOGDIR
if (log_io) {
pty_close(cstat);
}
#endif
#ifdef HAVE_SELINUX
if (rbac_enabled) {
if (selinux_restore_tty() != 0)
warningx("unable to restore tty label");
}
#endif
efree(fdsr);
efree(fdsw);
return cstat->type == CMD_ERRNO ? -1 : 0;
}
void
handler(s)
int s;
{
recvsig[s] = TRUE;
}