2010-03-27 20:19:40 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2010 Todd C. Miller <Todd.Miller@courtesan.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stddef.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
|
|
|
#endif /* STDC_HEADERS */
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif /* HAVE_STRING_H */
|
2010-06-29 13:08:05 -04:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif /* HAVE_STRINGS_H */
|
2010-03-27 20:19:40 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif /* HAVE_UNISTD_H */
|
|
|
|
#if TIME_WITH_SYS_TIME
|
|
|
|
# include <time.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
2010-12-27 11:24:47 -05:00
|
|
|
#include <setjmp.h>
|
2010-03-27 20:19:40 -04:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2010-06-16 16:51:46 -04:00
|
|
|
#ifdef HAVE_ZLIB_H
|
2010-03-27 20:19:40 -04:00
|
|
|
# include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "sudoers.h"
|
|
|
|
|
2010-12-27 11:24:47 -05:00
|
|
|
/* plugin_error.c */
|
|
|
|
extern sigjmp_buf error_jmp;
|
|
|
|
|
2010-05-30 06:29:41 -04:00
|
|
|
union io_fd {
|
2010-03-27 20:19:40 -04:00
|
|
|
FILE *f;
|
2010-06-16 16:51:46 -04:00
|
|
|
#ifdef HAVE_ZLIB_H
|
2010-03-27 20:19:40 -04:00
|
|
|
gzFile g;
|
|
|
|
#endif
|
|
|
|
void *v;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct script_buf {
|
|
|
|
int len; /* buffer length (how much read in) */
|
|
|
|
int off; /* write position (how much already consumed) */
|
|
|
|
char buf[16 * 1024];
|
|
|
|
};
|
|
|
|
|
2010-05-29 19:44:33 -04:00
|
|
|
#define IOFD_STDIN 0
|
|
|
|
#define IOFD_STDOUT 1
|
|
|
|
#define IOFD_STDERR 2
|
|
|
|
#define IOFD_TTYIN 3
|
|
|
|
#define IOFD_TTYOUT 4
|
|
|
|
#define IOFD_TIMING 5
|
|
|
|
#define IOFD_MAX 6
|
|
|
|
|
2010-06-07 18:53:58 -04:00
|
|
|
#define SESSID_MAX 2176782336U
|
|
|
|
|
2010-12-28 12:23:18 -05:00
|
|
|
static int iolog_compress;
|
2010-04-11 17:12:12 -04:00
|
|
|
static struct timeval last_time;
|
2010-05-30 06:29:41 -04:00
|
|
|
static union io_fd io_fds[IOFD_MAX];
|
2010-06-06 11:54:46 -04:00
|
|
|
extern struct io_plugin sudoers_io;
|
2010-04-11 17:12:12 -04:00
|
|
|
|
2010-12-28 12:23:18 -05:00
|
|
|
/*
|
|
|
|
* Create parent directories for path as needed, but not path itself.
|
|
|
|
*/
|
2010-12-27 12:18:32 -05:00
|
|
|
static void
|
|
|
|
mkdir_parents(char *path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
char *slash = path;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if ((slash = strchr(slash + 1, '/')) == NULL)
|
|
|
|
break;
|
|
|
|
*slash = '\0';
|
|
|
|
if (stat(path, &sb) != 0) {
|
|
|
|
if (mkdir(path, S_IRWXU) != 0)
|
|
|
|
log_error(USE_ERRNO, "Can't mkdir %s", path);
|
|
|
|
} else if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
log_error(0, "%s: %s", path, strerror(ENOTDIR));
|
|
|
|
}
|
|
|
|
*slash = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:23:18 -05:00
|
|
|
/*
|
|
|
|
* Read the on-disk sequence number, set sudo_user.sessid to the next
|
|
|
|
* number, and update the on-disk copy.
|
|
|
|
* Uses file locking to avoid sequence number collisions.
|
|
|
|
*/
|
2010-05-24 14:30:54 -04:00
|
|
|
void
|
2010-04-22 18:09:53 -04:00
|
|
|
io_nextid(void)
|
2010-03-27 20:19:40 -04:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
char buf[32], *ep;
|
|
|
|
int fd, i, ch;
|
|
|
|
unsigned long id = 0;
|
|
|
|
int len;
|
|
|
|
ssize_t nread;
|
|
|
|
char pathbuf[PATH_MAX];
|
|
|
|
|
|
|
|
/*
|
2010-12-10 14:14:35 -05:00
|
|
|
* Create I/O log directory if it doesn't already exist.
|
2010-03-27 20:19:40 -04:00
|
|
|
*/
|
2010-12-27 12:18:32 -05:00
|
|
|
mkdir_parents(def_iolog_dir);
|
2010-12-10 14:14:35 -05:00
|
|
|
if (stat(def_iolog_dir, &sb) != 0) {
|
|
|
|
if (mkdir(def_iolog_dir, S_IRWXU) != 0)
|
|
|
|
log_error(USE_ERRNO, "Can't mkdir %s", def_iolog_dir);
|
2010-03-27 20:19:40 -04:00
|
|
|
} else if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
log_error(0, "%s exists but is not a directory (0%o)",
|
2010-12-10 14:14:35 -05:00
|
|
|
def_iolog_dir, (unsigned int) sb.st_mode);
|
2010-03-27 20:19:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open sequence file
|
|
|
|
*/
|
2010-12-10 14:14:35 -05:00
|
|
|
len = snprintf(pathbuf, sizeof(pathbuf), "%s/seq", def_iolog_dir);
|
2010-03-27 20:19:40 -04:00
|
|
|
if (len <= 0 || len >= sizeof(pathbuf)) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
log_error(USE_ERRNO, "%s/seq", pathbuf);
|
|
|
|
}
|
|
|
|
fd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
|
|
|
|
if (fd == -1)
|
|
|
|
log_error(USE_ERRNO, "cannot open %s", pathbuf);
|
|
|
|
lock_file(fd, SUDO_LOCK);
|
|
|
|
|
|
|
|
/* Read seq number (base 36). */
|
|
|
|
nread = read(fd, buf, sizeof(buf));
|
|
|
|
if (nread != 0) {
|
|
|
|
if (nread == -1)
|
|
|
|
log_error(USE_ERRNO, "cannot read %s", pathbuf);
|
|
|
|
id = strtoul(buf, &ep, 36);
|
2010-06-07 18:53:58 -04:00
|
|
|
if (buf == ep || id >= SESSID_MAX)
|
2010-03-27 20:19:40 -04:00
|
|
|
log_error(0, "invalid sequence number %s", pathbuf);
|
|
|
|
}
|
|
|
|
id++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert id to a string and stash in sudo_user.sessid.
|
|
|
|
* Note that that least significant digits go at the end of the string.
|
|
|
|
*/
|
|
|
|
for (i = 5; i >= 0; i--) {
|
|
|
|
ch = id % 36;
|
|
|
|
id /= 36;
|
|
|
|
buf[i] = ch < 10 ? ch + '0' : ch - 10 + 'A';
|
|
|
|
}
|
|
|
|
buf[6] = '\n';
|
|
|
|
|
|
|
|
/* Stash id logging purposes */
|
|
|
|
memcpy(sudo_user.sessid, buf, 6);
|
|
|
|
sudo_user.sessid[6] = '\0';
|
|
|
|
|
|
|
|
/* Rewind and overwrite old seq file. */
|
|
|
|
if (lseek(fd, 0, SEEK_SET) == (off_t)-1 || write(fd, buf, 7) != 7)
|
|
|
|
log_error(USE_ERRNO, "Can't write to %s", pathbuf);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:23:18 -05:00
|
|
|
/*
|
|
|
|
* Join iolog_dir and iolog_file, storing the result as pathbuf and
|
|
|
|
* expanding any escapes sequences that are found.
|
|
|
|
* Creates the resulting directory and any intermediate directories.
|
|
|
|
*/
|
2010-03-27 20:19:40 -04:00
|
|
|
static int
|
2010-12-27 12:18:32 -05:00
|
|
|
build_iopath(const char *iolog_dir, const char *iolog_file, char *pathbuf,
|
|
|
|
size_t pathsize)
|
2010-03-27 20:19:40 -04:00
|
|
|
{
|
2010-12-27 12:18:32 -05:00
|
|
|
int dirlen, filelen, len;
|
|
|
|
|
|
|
|
/* Trim extraneous slashes. */
|
|
|
|
dirlen = strlen(iolog_dir);
|
|
|
|
while (dirlen > 1 && iolog_dir[dirlen - 1] == '/')
|
|
|
|
dirlen--;
|
|
|
|
while (*iolog_file == '/')
|
|
|
|
iolog_file++;
|
|
|
|
filelen = strlen(iolog_file);
|
|
|
|
while (filelen > 1 && iolog_file[filelen - 1] == '/')
|
|
|
|
filelen--;
|
|
|
|
|
|
|
|
if (*iolog_dir != '/' || *iolog_file == '\0')
|
|
|
|
log_error(0, "invalid I/O log path: %s/%s", iolog_dir, iolog_file);
|
|
|
|
|
|
|
|
len = snprintf(pathbuf, pathsize, "%.*s/%.*s", dirlen, iolog_dir,
|
|
|
|
filelen, iolog_file);
|
|
|
|
if (len <= 0 && len >= pathsize) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
log_error(USE_ERRNO, "%.*s/%.*s", dirlen, iolog_dir,
|
|
|
|
filelen, iolog_file);
|
2010-03-27 20:19:40 -04:00
|
|
|
}
|
|
|
|
|
2010-12-27 12:32:28 -05:00
|
|
|
/*
|
|
|
|
* Create path and intermediate subdirs as needed.
|
|
|
|
* If path ends in at least 6 Xs (ala POSIX mktemp), use mkdtemp().
|
|
|
|
*/
|
2010-12-27 12:18:32 -05:00
|
|
|
mkdir_parents(pathbuf);
|
2010-12-27 12:32:28 -05:00
|
|
|
if (len >= 6 && strcmp(&pathbuf[len - 6], "XXXXXX") == 0) {
|
|
|
|
if (mkdtemp(pathbuf) == NULL)
|
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
|
|
|
} else {
|
|
|
|
if (mkdir(pathbuf, S_IRWXU) != 0)
|
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
|
|
|
}
|
2010-03-27 20:19:40 -04:00
|
|
|
|
|
|
|
return(len);
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:23:18 -05:00
|
|
|
/*
|
|
|
|
* Append suffix to pathbuf after len chars and open the resulting file.
|
|
|
|
* Note that the size of pathbuf is assumed to be PATH_MAX.
|
|
|
|
* Uses zlib if docompress is TRUE.
|
|
|
|
* Returns the open file handle which has the close-on-exec flag set.
|
|
|
|
*/
|
2010-05-29 19:44:33 -04:00
|
|
|
static void *
|
|
|
|
open_io_fd(char *pathbuf, int len, const char *suffix, int docompress)
|
|
|
|
{
|
|
|
|
void *vfd = NULL;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
pathbuf[len] = '\0';
|
|
|
|
strlcat(pathbuf, suffix, PATH_MAX);
|
|
|
|
fd = open(pathbuf, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
|
|
|
|
if (fd != -1) {
|
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
2010-06-16 16:51:46 -04:00
|
|
|
#ifdef HAVE_ZLIB_H
|
2010-05-29 19:44:33 -04:00
|
|
|
if (docompress)
|
|
|
|
vfd = gzdopen(fd, "w");
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
vfd = fdopen(fd, "w");
|
|
|
|
}
|
|
|
|
return vfd;
|
|
|
|
}
|
|
|
|
|
2010-06-03 13:13:42 -04:00
|
|
|
static int
|
|
|
|
sudoers_io_open(unsigned int version, sudo_conv_t conversation,
|
2010-05-04 19:17:31 -04:00
|
|
|
sudo_printf_t plugin_printf, char * const settings[],
|
2010-12-20 16:28:20 -05:00
|
|
|
char * const user_info[], char * const command_info[],
|
|
|
|
int argc, char * const argv[], char * const user_env[])
|
2010-03-27 20:19:40 -04:00
|
|
|
{
|
|
|
|
char pathbuf[PATH_MAX];
|
2010-12-28 12:23:18 -05:00
|
|
|
const char *iolog_dir = NULL, *iolog_file = NULL;
|
2010-12-20 16:28:20 -05:00
|
|
|
char * const *cur;
|
2010-03-27 20:19:40 -04:00
|
|
|
FILE *io_logfile;
|
2010-12-28 12:23:18 -05:00
|
|
|
int len, iolog_stdin = FALSE, iolog_stdout = FALSE, iolog_stderr = FALSE;
|
|
|
|
int iolog_ttyin = FALSE, iolog_ttyout = FALSE, iolog_compress = FALSE;
|
2010-03-27 20:19:40 -04:00
|
|
|
|
2010-04-11 17:12:12 -04:00
|
|
|
if (!sudo_conv)
|
|
|
|
sudo_conv = conversation;
|
2010-05-04 19:17:31 -04:00
|
|
|
if (!sudo_printf)
|
|
|
|
sudo_printf = plugin_printf;
|
2010-03-27 20:19:40 -04:00
|
|
|
|
2010-05-28 12:01:06 -04:00
|
|
|
/* If we have no command (because -V was specified) just return. */
|
|
|
|
if (argc == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
2010-12-27 11:24:47 -05:00
|
|
|
if (sigsetjmp(error_jmp, 1)) {
|
|
|
|
/* called via error(), errorx() or log_error() */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-12-20 16:28:20 -05:00
|
|
|
/*
|
2010-12-21 17:43:18 -05:00
|
|
|
* Pull iolog settings out of command_info, if any.
|
2010-12-20 16:28:20 -05:00
|
|
|
*/
|
|
|
|
for (cur = command_info; *cur != NULL; cur++) {
|
|
|
|
if (**cur != 'i')
|
|
|
|
continue;
|
|
|
|
if (strncmp(*cur, "iolog_file=", sizeof("iolog_file=") - 1) == 0) {
|
|
|
|
iolog_file = *cur + sizeof("iolog_file=") - 1;
|
2010-12-21 17:43:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(*cur, "iolog_dir=", sizeof("iolog_dir=") - 1) == 0) {
|
2010-12-20 16:28:20 -05:00
|
|
|
iolog_dir = *cur + sizeof("iolog_dir=") - 1;
|
2010-12-21 17:43:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(*cur, "iolog_stdin=", sizeof("iolog_stdin=") - 1) == 0) {
|
|
|
|
if (atobool(*cur + sizeof("iolog_stdin=") - 1) == TRUE)
|
|
|
|
iolog_stdin = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(*cur, "iolog_stdout=", sizeof("iolog_stdout=") - 1) == 0) {
|
|
|
|
if (atobool(*cur + sizeof("iolog_stdout=") - 1) == TRUE)
|
|
|
|
iolog_stdout = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(*cur, "iolog_stderr=", sizeof("iolog_stderr=") - 1) == 0) {
|
|
|
|
if (atobool(*cur + sizeof("iolog_stderr=") - 1) == TRUE)
|
|
|
|
iolog_stderr = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(*cur, "iolog_ttyin=", sizeof("iolog_ttyin=") - 1) == 0) {
|
|
|
|
if (atobool(*cur + sizeof("iolog_ttyin=") - 1) == TRUE)
|
|
|
|
iolog_ttyin = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(*cur, "iolog_ttyout=", sizeof("iolog_ttyout=") - 1) == 0) {
|
|
|
|
if (atobool(*cur + sizeof("iolog_ttyout=") - 1) == TRUE)
|
|
|
|
iolog_ttyout = TRUE;
|
|
|
|
continue;
|
2010-12-20 16:28:20 -05:00
|
|
|
}
|
2010-12-28 12:23:18 -05:00
|
|
|
if (strncmp(*cur, "iolog_compress=", sizeof("iolog_compress=") - 1) == 0) {
|
|
|
|
if (atobool(*cur + sizeof("iolog_compress=") - 1) == TRUE)
|
|
|
|
iolog_compress = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
2010-12-20 16:28:20 -05:00
|
|
|
}
|
2010-12-21 17:43:18 -05:00
|
|
|
/* Did policy module disable I/O logging? */
|
|
|
|
if (!iolog_stdin && !iolog_ttyin && !iolog_stdout && !iolog_stderr &&
|
|
|
|
!iolog_ttyout)
|
|
|
|
return FALSE;
|
2010-12-20 16:28:20 -05:00
|
|
|
|
|
|
|
/* If no I/O log file defined there is nothing to do. */
|
2010-12-27 12:18:32 -05:00
|
|
|
if (iolog_file == NULL || iolog_dir == NULL)
|
2010-03-27 20:19:40 -04:00
|
|
|
return FALSE;
|
|
|
|
|
2010-12-27 12:32:28 -05:00
|
|
|
/* Build a path from I/O file and dir, creating intermediate subdirs. */
|
2010-12-27 12:18:32 -05:00
|
|
|
len = build_iopath(iolog_dir, iolog_file, pathbuf, sizeof(pathbuf));
|
|
|
|
if (len < 0 || len >= sizeof(pathbuf))
|
2010-03-27 20:19:40 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
2010-05-29 19:44:33 -04:00
|
|
|
* We create 7 files: a log file, a timing file and 5 for input/output.
|
2010-03-27 20:19:40 -04:00
|
|
|
*/
|
2010-05-29 19:44:33 -04:00
|
|
|
io_logfile = open_io_fd(pathbuf, len, "/log", FALSE);
|
2010-03-27 20:19:40 -04:00
|
|
|
if (io_logfile == NULL)
|
2010-05-29 19:44:33 -04:00
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
2010-03-27 20:19:40 -04:00
|
|
|
|
2010-12-28 12:23:18 -05:00
|
|
|
io_fds[IOFD_TIMING].v = open_io_fd(pathbuf, len, "/timing", iolog_compress);
|
2010-05-29 19:44:33 -04:00
|
|
|
if (io_fds[IOFD_TIMING].v == NULL)
|
2010-03-27 20:19:40 -04:00
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
|
|
|
|
2010-12-21 17:43:18 -05:00
|
|
|
if (iolog_ttyin) {
|
2010-12-28 12:23:18 -05:00
|
|
|
io_fds[IOFD_TTYIN].v = open_io_fd(pathbuf, len, "/ttyin", iolog_compress);
|
2010-06-06 11:54:46 -04:00
|
|
|
if (io_fds[IOFD_TTYIN].v == NULL)
|
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
2010-12-21 17:43:18 -05:00
|
|
|
} else {
|
|
|
|
sudoers_io.log_ttyin = NULL;
|
|
|
|
}
|
|
|
|
if (iolog_stdin) {
|
2010-12-28 12:23:18 -05:00
|
|
|
io_fds[IOFD_STDIN].v = open_io_fd(pathbuf, len, "/stdin", iolog_compress);
|
2010-06-06 11:54:46 -04:00
|
|
|
if (io_fds[IOFD_STDIN].v == NULL)
|
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
|
|
|
} else {
|
|
|
|
sudoers_io.log_stdin = NULL;
|
|
|
|
}
|
2010-12-21 17:43:18 -05:00
|
|
|
if (iolog_ttyout) {
|
2010-12-28 12:23:18 -05:00
|
|
|
io_fds[IOFD_TTYOUT].v = open_io_fd(pathbuf, len, "/ttyout", iolog_compress);
|
2010-06-06 11:54:46 -04:00
|
|
|
if (io_fds[IOFD_TTYOUT].v == NULL)
|
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
2010-12-21 17:43:18 -05:00
|
|
|
} else {
|
|
|
|
sudoers_io.log_ttyout = NULL;
|
|
|
|
}
|
|
|
|
if (iolog_stdout) {
|
2010-12-28 12:23:18 -05:00
|
|
|
io_fds[IOFD_STDOUT].v = open_io_fd(pathbuf, len, "/stdout", iolog_compress);
|
2010-06-06 11:54:46 -04:00
|
|
|
if (io_fds[IOFD_STDOUT].v == NULL)
|
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
2010-12-21 17:43:18 -05:00
|
|
|
} else {
|
|
|
|
sudoers_io.log_stdout = NULL;
|
|
|
|
}
|
|
|
|
if (iolog_stderr) {
|
2010-12-28 12:23:18 -05:00
|
|
|
io_fds[IOFD_STDERR].v = open_io_fd(pathbuf, len, "/stderr", iolog_compress);
|
2010-06-06 11:54:46 -04:00
|
|
|
if (io_fds[IOFD_STDERR].v == NULL)
|
|
|
|
log_error(USE_ERRNO, "Can't create %s", pathbuf);
|
|
|
|
} else {
|
|
|
|
sudoers_io.log_stderr = NULL;
|
|
|
|
}
|
2010-03-27 20:19:40 -04:00
|
|
|
|
|
|
|
gettimeofday(&last_time, NULL);
|
|
|
|
|
|
|
|
/* XXX - log more stuff? window size? environment? */
|
2010-12-21 17:43:18 -05:00
|
|
|
/* XXX - don't rely on policy module globals */
|
2010-06-18 18:45:43 -04:00
|
|
|
fprintf(io_logfile, "%ld:%s:%s:%s:%s\n", (long)last_time.tv_sec, user_name,
|
2010-03-27 20:19:40 -04:00
|
|
|
runas_pw->pw_name, runas_gr ? runas_gr->gr_name : "", user_tty);
|
|
|
|
fprintf(io_logfile, "%s\n", user_cwd);
|
|
|
|
fprintf(io_logfile, "%s%s%s\n", user_cmnd, user_args ? " " : "",
|
|
|
|
user_args ? user_args : "");
|
|
|
|
fclose(io_logfile);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-03 13:13:42 -04:00
|
|
|
static void
|
|
|
|
sudoers_io_close(int exit_status, int error)
|
2010-03-27 20:19:40 -04:00
|
|
|
{
|
2010-05-29 19:44:33 -04:00
|
|
|
int i;
|
|
|
|
|
2010-12-28 11:02:12 -05:00
|
|
|
if (sigsetjmp(error_jmp, 1)) {
|
|
|
|
/* called via error(), errorx() or log_error() */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-29 19:44:33 -04:00
|
|
|
for (i = 0; i < IOFD_MAX; i++) {
|
2010-06-08 16:57:18 -04:00
|
|
|
if (io_fds[i].v == NULL)
|
|
|
|
continue;
|
2010-06-16 16:51:46 -04:00
|
|
|
#ifdef HAVE_ZLIB_H
|
2010-12-28 12:23:18 -05:00
|
|
|
if (iolog_compress)
|
2010-05-29 19:44:33 -04:00
|
|
|
gzclose(io_fds[i].g);
|
|
|
|
else
|
2010-03-27 20:19:40 -04:00
|
|
|
#endif
|
2010-05-29 19:44:33 -04:00
|
|
|
fclose(io_fds[i].f);
|
2010-03-27 20:19:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-03 13:13:42 -04:00
|
|
|
static int
|
|
|
|
sudoers_io_version(int verbose)
|
2010-03-27 20:19:40 -04:00
|
|
|
{
|
2010-12-28 11:02:12 -05:00
|
|
|
if (sigsetjmp(error_jmp, 1)) {
|
|
|
|
/* called via error(), errorx() or log_error() */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-10 14:24:06 -04:00
|
|
|
sudo_printf(SUDO_CONV_INFO_MSG, "Sudoers I/O plugin version %s\n",
|
|
|
|
PACKAGE_VERSION);
|
2010-03-27 20:19:40 -04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-12-28 12:23:18 -05:00
|
|
|
/*
|
|
|
|
* Generic I/O logging function. Called by the I/O logging entry points.
|
|
|
|
*/
|
2010-05-29 19:44:33 -04:00
|
|
|
static int
|
|
|
|
sudoers_io_log(const char *buf, unsigned int len, int idx)
|
2010-03-27 20:19:40 -04:00
|
|
|
{
|
2010-06-08 18:38:23 -04:00
|
|
|
struct timeval now, delay;
|
2010-03-27 20:19:40 -04:00
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
2010-12-28 11:02:12 -05:00
|
|
|
if (sigsetjmp(error_jmp, 1)) {
|
|
|
|
/* called via error(), errorx() or log_error() */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-16 16:51:46 -04:00
|
|
|
#ifdef HAVE_ZLIB_H
|
2010-12-28 12:23:18 -05:00
|
|
|
if (iolog_compress)
|
2010-05-29 19:44:33 -04:00
|
|
|
gzwrite(io_fds[idx].g, buf, len);
|
2010-03-27 20:19:40 -04:00
|
|
|
else
|
|
|
|
#endif
|
2010-05-29 19:44:33 -04:00
|
|
|
fwrite(buf, 1, len, io_fds[idx].f);
|
2010-06-08 18:38:23 -04:00
|
|
|
delay.tv_sec = now.tv_sec;
|
|
|
|
delay.tv_usec = now.tv_usec;
|
|
|
|
timevalsub(&delay, &last_time);
|
2010-06-16 16:51:46 -04:00
|
|
|
#ifdef HAVE_ZLIB_H
|
2010-12-28 12:23:18 -05:00
|
|
|
if (iolog_compress)
|
2010-05-29 19:44:33 -04:00
|
|
|
gzprintf(io_fds[IOFD_TIMING].g, "%d %f %d\n", idx,
|
2010-06-08 18:38:23 -04:00
|
|
|
delay.tv_sec + ((double)delay.tv_usec / 1000000), len);
|
2010-03-27 20:19:40 -04:00
|
|
|
else
|
|
|
|
#endif
|
2010-05-29 19:44:33 -04:00
|
|
|
fprintf(io_fds[IOFD_TIMING].f, "%d %f %d\n", idx,
|
2010-06-08 18:38:23 -04:00
|
|
|
delay.tv_sec + ((double)delay.tv_usec / 1000000), len);
|
2010-03-27 20:19:40 -04:00
|
|
|
last_time.tv_sec = now.tv_sec;
|
|
|
|
last_time.tv_usec = now.tv_usec;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2010-05-29 10:43:34 -04:00
|
|
|
|
2010-06-03 13:13:42 -04:00
|
|
|
static int
|
|
|
|
sudoers_io_log_ttyin(const char *buf, unsigned int len)
|
2010-05-29 19:44:33 -04:00
|
|
|
{
|
|
|
|
return sudoers_io_log(buf, len, IOFD_TTYIN);
|
|
|
|
}
|
|
|
|
|
2010-06-03 13:13:42 -04:00
|
|
|
static int
|
|
|
|
sudoers_io_log_ttyout(const char *buf, unsigned int len)
|
2010-05-29 19:44:33 -04:00
|
|
|
{
|
|
|
|
return sudoers_io_log(buf, len, IOFD_TTYOUT);
|
|
|
|
}
|
|
|
|
|
2010-06-03 13:13:42 -04:00
|
|
|
static int
|
|
|
|
sudoers_io_log_stdin(const char *buf, unsigned int len)
|
2010-05-29 19:44:33 -04:00
|
|
|
{
|
|
|
|
return sudoers_io_log(buf, len, IOFD_STDIN);
|
|
|
|
}
|
|
|
|
|
2010-06-03 13:13:42 -04:00
|
|
|
static int
|
|
|
|
sudoers_io_log_stdout(const char *buf, unsigned int len)
|
2010-05-29 19:44:33 -04:00
|
|
|
{
|
|
|
|
return sudoers_io_log(buf, len, IOFD_STDOUT);
|
|
|
|
}
|
|
|
|
|
2010-06-03 13:13:42 -04:00
|
|
|
static int
|
|
|
|
sudoers_io_log_stderr(const char *buf, unsigned int len)
|
2010-05-29 19:44:33 -04:00
|
|
|
{
|
|
|
|
return sudoers_io_log(buf, len, IOFD_STDERR);
|
|
|
|
}
|
2010-05-29 10:43:34 -04:00
|
|
|
|
|
|
|
struct io_plugin sudoers_io = {
|
|
|
|
SUDO_IO_PLUGIN,
|
|
|
|
SUDO_API_VERSION,
|
|
|
|
sudoers_io_open,
|
|
|
|
sudoers_io_close,
|
|
|
|
sudoers_io_version,
|
2010-05-29 19:44:33 -04:00
|
|
|
sudoers_io_log_ttyin,
|
|
|
|
sudoers_io_log_ttyout,
|
|
|
|
sudoers_io_log_stdin,
|
|
|
|
sudoers_io_log_stdout,
|
|
|
|
sudoers_io_log_stderr
|
2010-05-29 10:43:34 -04:00
|
|
|
};
|