2 * Copyright (c) 2009-2013 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>
30 #endif /* STDC_HEADERS */
33 #endif /* HAVE_STRING_H */
36 #endif /* HAVE_STRINGS_H */
39 #endif /* HAVE_UNISTD_H */
40 #if TIME_WITH_SYS_TIME
55 int len; /* buffer length (how much read in) */
56 int off; /* write position (how much already consumed) */
60 /* XXX - separate sudoers.h and iolog.h? */
64 struct iolog_details {
69 const char *iolog_path;
70 struct passwd *runas_pw;
71 struct group *runas_gr;
84 static struct io_log_file {
101 #define IOFD_TTYOUT 6
102 { false, "/ttyout" },
107 #define SESSID_MAX 2176782336U
109 static int iolog_compress;
110 static struct timeval last_time;
111 static unsigned int sessid_max = SESSID_MAX;
113 /* sudoers_io is declared at the end of this file. */
114 extern __dso_public struct io_plugin sudoers_io;
117 * Create path and any parent directories as needed.
118 * If is_temp is set, use mkdtemp() for the final directory.
121 io_mkdirs(char *path, mode_t mode, bool is_temp)
124 gid_t parent_gid = 0;
126 debug_decl(io_mkdirs, SUDO_DEBUG_UTIL)
128 /* Fast path: not a temporary and already exists. */
129 if (!is_temp && stat(path, &sb) == 0) {
130 if (!S_ISDIR(sb.st_mode)) {
131 log_fatal(0, N_("%s exists but is not a directory (0%o)"),
132 path, (unsigned int) sb.st_mode);
137 while ((slash = strchr(slash + 1, '/')) != NULL) {
139 if (stat(path, &sb) != 0) {
140 if (mkdir(path, mode) != 0)
141 log_fatal(USE_ERRNO, N_("unable to mkdir %s"), path);
142 ignore_result(chown(path, (uid_t)-1, parent_gid));
143 } else if (!S_ISDIR(sb.st_mode)) {
144 log_fatal(0, N_("%s exists but is not a directory (0%o)"),
145 path, (unsigned int) sb.st_mode);
147 /* Inherit gid of parent dir for ownership. */
148 parent_gid = sb.st_gid;
152 /* Create final path component. */
154 if (mkdtemp(path) == NULL)
155 log_fatal(USE_ERRNO, N_("unable to mkdir %s"), path);
156 ignore_result(chown(path, (uid_t)-1, parent_gid));
158 if (mkdir(path, mode) != 0 && errno != EEXIST)
159 log_fatal(USE_ERRNO, N_("unable to mkdir %s"), path);
160 ignore_result(chown(path, (uid_t)-1, parent_gid));
166 * Set max session ID (aka sequence number)
169 io_set_max_sessid(const char *maxval)
175 ulval = strtoul(maxval, &ep, 0);
176 if (*maxval != '\0' && *ep == '\0' &&
177 (errno != ERANGE || ulval != ULONG_MAX)) {
178 sessid_max = MIN((unsigned int)ulval, SESSID_MAX);
185 * Read the on-disk sequence number, set sessid to the next
186 * number, and update the on-disk copy.
187 * Uses file locking to avoid sequence number collisions.
190 io_nextid(char *iolog_dir, char *iolog_dir_fallback, char sessid[7])
195 unsigned long id = 0;
198 char pathbuf[PATH_MAX];
199 static const char b36char[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
200 debug_decl(io_nextid, SUDO_DEBUG_UTIL)
203 * Create I/O log directory if it doesn't already exist.
205 io_mkdirs(iolog_dir, S_IRWXU, false);
210 len = snprintf(pathbuf, sizeof(pathbuf), "%s/seq", iolog_dir);
211 if (len <= 0 || len >= sizeof(pathbuf)) {
212 errno = ENAMETOOLONG;
213 log_fatal(USE_ERRNO, "%s/seq", pathbuf);
215 fd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
217 log_fatal(USE_ERRNO, N_("unable to open %s"), pathbuf);
218 lock_file(fd, SUDO_LOCK);
221 * If there is no seq file in iolog_dir and a fallback dir was
222 * specified, look for seq in the fallback dir. This is to work
223 * around a bug in sudo 1.8.5 and older where iolog_dir was not
224 * expanded before the sequence number was updated.
226 if (iolog_dir_fallback != NULL && fstat(fd, &sb) == 0 && sb.st_size == 0) {
227 char fallback[PATH_MAX];
229 len = snprintf(fallback, sizeof(fallback), "%s/seq",
231 if (len > 0 && len < sizeof(fallback)) {
232 int fd2 = open(fallback, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
234 nread = read(fd2, buf, sizeof(buf));
236 id = strtoul(buf, &ep, 36);
237 if (buf == ep || id >= sessid_max)
245 /* Read current seq number (base 36). */
247 nread = read(fd, buf, sizeof(buf));
250 log_fatal(USE_ERRNO, N_("unable to read %s"), pathbuf);
251 id = strtoul(buf, &ep, 36);
252 if (buf == ep || id >= sessid_max)
259 * Convert id to a string and stash in sessid.
260 * Note that that least significant digits go at the end of the string.
262 for (i = 5; i >= 0; i--) {
263 buf[i] = b36char[id % 36];
268 /* Stash id for logging purposes. */
269 memcpy(sessid, buf, 6);
272 /* Rewind and overwrite old seq file. */
273 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1 || write(fd, buf, 7) != 7)
274 log_fatal(USE_ERRNO, N_("unable to write to %s"), pathbuf);
281 * Copy iolog_path to pathbuf and create the directory and any intermediate
282 * directories. If iolog_path ends in 'XXXXXX', use mkdtemp().
285 mkdir_iopath(const char *iolog_path, char *pathbuf, size_t pathsize)
288 bool is_temp = false;
289 debug_decl(mkdir_iopath, SUDO_DEBUG_UTIL)
291 len = strlcpy(pathbuf, iolog_path, pathsize);
292 if (len >= pathsize) {
293 errno = ENAMETOOLONG;
294 log_fatal(USE_ERRNO, "%s", iolog_path);
298 * Create path and intermediate subdirs as needed.
299 * If path ends in at least 6 Xs (ala POSIX mktemp), use mkdtemp().
301 if (len >= 6 && strcmp(&pathbuf[len - 6], "XXXXXX") == 0)
303 io_mkdirs(pathbuf, S_IRWXU, is_temp);
305 debug_return_size_t(len);
309 * Append suffix to pathbuf after len chars and open the resulting file.
310 * Note that the size of pathbuf is assumed to be PATH_MAX.
311 * Uses zlib if docompress is true.
312 * Stores the open file handle which has the close-on-exec flag set.
315 open_io_fd(char *pathbuf, size_t len, struct io_log_file *iol, bool docompress)
318 debug_decl(open_io_fd, SUDO_DEBUG_UTIL)
321 strlcat(pathbuf, iol->suffix, PATH_MAX);
323 fd = open(pathbuf, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
325 fcntl(fd, F_SETFD, FD_CLOEXEC);
328 iol->fd.g = gzdopen(fd, "w");
331 iol->fd.f = fdopen(fd, "w");
333 if (fd == -1 || iol->fd.v == NULL) {
334 log_fatal(USE_ERRNO, N_("unable to create %s"), pathbuf);
339 /* Remove old log file if we recycled sequence numbers. */
346 * Pull out I/O log related data from user_info and command_info arrays.
347 * Returns true if I/O logging is enabled, else false.
350 iolog_deserialize_info(struct iolog_details *details, char * const user_info[],
351 char * const command_info[])
353 const char *runas_uid_str = "0", *runas_euid_str = NULL;
354 const char *runas_gid_str = "0", *runas_egid_str = NULL;
355 char id[MAX_UID_T_LEN + 2], *ep;
360 debug_decl(iolog_deserialize_info, SUDO_DEBUG_UTIL)
365 for (cur = user_info; *cur != NULL; cur++) {
368 if (strncmp(*cur, "cols=", sizeof("cols=") - 1) == 0) {
369 details->cols = atoi(*cur + sizeof("cols=") - 1);
372 if (strncmp(*cur, "cwd=", sizeof("cwd=") - 1) == 0) {
373 details->cwd = *cur + sizeof("cwd=") - 1;
378 if (strncmp(*cur, "lines=", sizeof("lines=") - 1) == 0) {
379 details->lines = atoi(*cur + sizeof("lines=") - 1);
384 if (strncmp(*cur, "tty=", sizeof("tty=") - 1) == 0) {
385 details->tty = *cur + sizeof("tty=") - 1;
390 if (strncmp(*cur, "user=", sizeof("user=") - 1) == 0) {
391 details->user = *cur + sizeof("user=") - 1;
398 for (cur = command_info; *cur != NULL; cur++) {
401 if (strncmp(*cur, "command=", sizeof("command=") - 1) == 0) {
402 details->command = *cur + sizeof("command=") - 1;
407 if (strncmp(*cur, "iolog_path=", sizeof("iolog_path=") - 1) == 0) {
408 details->iolog_path = *cur + sizeof("iolog_path=") - 1;
411 if (strncmp(*cur, "iolog_stdin=", sizeof("iolog_stdin=") - 1) == 0) {
412 if (atobool(*cur + sizeof("iolog_stdin=") - 1) == true)
413 io_log_files[IOFD_STDIN].enabled = true;
416 if (strncmp(*cur, "iolog_stdout=", sizeof("iolog_stdout=") - 1) == 0) {
417 if (atobool(*cur + sizeof("iolog_stdout=") - 1) == true)
418 io_log_files[IOFD_STDOUT].enabled = true;
421 if (strncmp(*cur, "iolog_stderr=", sizeof("iolog_stderr=") - 1) == 0) {
422 if (atobool(*cur + sizeof("iolog_stderr=") - 1) == true)
423 io_log_files[IOFD_STDERR].enabled = true;
426 if (strncmp(*cur, "iolog_ttyin=", sizeof("iolog_ttyin=") - 1) == 0) {
427 if (atobool(*cur + sizeof("iolog_ttyin=") - 1) == true)
428 io_log_files[IOFD_TTYIN].enabled = true;
431 if (strncmp(*cur, "iolog_ttyout=", sizeof("iolog_ttyout=") - 1) == 0) {
432 if (atobool(*cur + sizeof("iolog_ttyout=") - 1) == true)
433 io_log_files[IOFD_TTYOUT].enabled = true;
436 if (strncmp(*cur, "iolog_compress=", sizeof("iolog_compress=") - 1) == 0) {
437 if (atobool(*cur + sizeof("iolog_compress=") - 1) == true)
438 iolog_compress = true; /* must be global */
443 if (strncmp(*cur, "maxseq=", sizeof("maxseq=") - 1) == 0)
444 io_set_max_sessid(*cur + sizeof("maxseq=") - 1);
447 if (strncmp(*cur, "runas_gid=", sizeof("runas_gid=") - 1) == 0) {
448 runas_gid_str = *cur + sizeof("runas_gid=") - 1;
451 if (strncmp(*cur, "runas_egid=", sizeof("runas_egid=") - 1) == 0) {
452 runas_egid_str = *cur + sizeof("runas_egid=") - 1;
455 if (strncmp(*cur, "runas_uid=", sizeof("runas_uid=") - 1) == 0) {
456 runas_uid_str = *cur + sizeof("runas_uid=") - 1;
459 if (strncmp(*cur, "runas_euid=", sizeof("runas_euid=") - 1) == 0) {
460 runas_euid_str = *cur + sizeof("runas_euid=") - 1;
468 * Lookup runas user and group, preferring effective over real uid/gid.
470 if (runas_euid_str != NULL)
471 runas_uid_str = runas_euid_str;
472 if (runas_uid_str != NULL) {
474 ulval = strtoul(runas_uid_str, &ep, 0);
475 if (*runas_uid_str != '\0' && *ep == '\0' &&
476 (errno != ERANGE || ulval != ULONG_MAX)) {
477 runas_uid = (uid_t)ulval;
480 if (runas_egid_str != NULL)
481 runas_gid_str = runas_egid_str;
482 if (runas_gid_str != NULL) {
484 ulval = strtoul(runas_gid_str, &ep, 0);
485 if (*runas_gid_str != '\0' && *ep == '\0' &&
486 (errno != ERANGE || ulval != ULONG_MAX)) {
487 runas_gid = (gid_t)ulval;
491 details->runas_pw = sudo_getpwuid(runas_uid);
492 if (details->runas_pw == NULL) {
494 strlcpy(&id[1], runas_uid_str, sizeof(id) - 1);
495 details->runas_pw = sudo_fakepwnam(id, runas_gid);
498 if (runas_gid != details->runas_pw->pw_gid) {
499 details->runas_gr = sudo_getgrgid(runas_gid);
500 if (details->runas_gr == NULL) {
502 strlcpy(&id[1], runas_gid_str, sizeof(id) - 1);
503 details->runas_gr = sudo_fakegrnam(id);
507 io_log_files[IOFD_STDIN].enabled || io_log_files[IOFD_STDOUT].enabled ||
508 io_log_files[IOFD_STDERR].enabled || io_log_files[IOFD_TTYIN].enabled ||
509 io_log_files[IOFD_TTYOUT].enabled);
513 sudoers_io_open(unsigned int version, sudo_conv_t conversation,
514 sudo_printf_t plugin_printf, char * const settings[],
515 char * const user_info[], char * const command_info[],
516 int argc, char * const argv[], char * const user_env[], char * const args[])
518 struct iolog_details details;
519 char pathbuf[PATH_MAX], sessid[7];
522 const char *debug_flags = NULL;
525 debug_decl(sudoers_io_open, SUDO_DEBUG_PLUGIN)
527 sudo_conv = conversation;
528 sudo_printf = plugin_printf;
530 /* If we have no command (because -V was specified) just return. */
532 debug_return_bool(true);
534 memset(&details, 0, sizeof(details));
536 if (fatal_setjmp() != 0) {
537 /* called via fatal(), fatalx() or log_fatal() */
542 bindtextdomain("sudoers", LOCALEDIR);
548 * Check for debug flags in settings list.
550 for (cur = settings; *cur != NULL; cur++) {
551 if (strncmp(*cur, "debug_flags=", sizeof("debug_flags=") - 1) == 0)
552 debug_flags = *cur + sizeof("debug_flags=") - 1;
554 if (debug_flags != NULL)
555 sudo_debug_init(NULL, debug_flags);
558 * Pull iolog settings out of command_info.
560 if (!iolog_deserialize_info(&details, user_info, command_info)) {
565 /* If no I/O log path defined we need to figure it out ourselves. */
566 if (details.iolog_path == NULL) {
567 /* Get next session ID and convert it into a path. */
568 tofree = emalloc(sizeof(_PATH_SUDO_IO_LOGDIR) + sizeof(sessid) + 2);
569 memcpy(tofree, _PATH_SUDO_IO_LOGDIR, sizeof(_PATH_SUDO_IO_LOGDIR));
570 io_nextid(tofree, NULL, sessid);
571 snprintf(tofree + sizeof(_PATH_SUDO_IO_LOGDIR), sizeof(sessid) + 2,
572 "%c%c/%c%c/%c%c", sessid[0], sessid[1], sessid[2], sessid[3],
573 sessid[4], sessid[5]);
574 details.iolog_path = tofree;
578 * Make local copy of I/O log path and create it, along with any
579 * intermediate subdirs. Calls mkdtemp() if iolog_path ends in XXXXXX.
581 len = mkdir_iopath(details.iolog_path, pathbuf, sizeof(pathbuf));
582 if (len >= sizeof(pathbuf))
586 * We create 7 files: a log file, a timing file and 5 for input/output.
588 for (i = 0; i < IOFD_MAX; i++) {
589 open_io_fd(pathbuf, len, &io_log_files[i], i ? iolog_compress : false);
592 gettimeofday(&last_time, NULL);
593 fprintf(io_log_files[IOFD_LOG].fd.f, "%lld:%s:%s:%s:%s:%d:%d\n%s\n%s",
594 (long long)last_time.tv_sec,
595 details.user ? details.user : "unknown", details.runas_pw->pw_name,
596 details.runas_gr ? details.runas_gr->gr_name : "",
597 details.tty ? details.tty : "unknown", details.lines, details.cols,
598 details.cwd ? details.cwd : "unknown",
599 details.command ? details.command : "unknown");
600 for (cur = &argv[1]; *cur != NULL; cur++) {
601 fputc(' ', io_log_files[IOFD_LOG].fd.f);
602 fputs(*cur, io_log_files[IOFD_LOG].fd.f);
604 fputc('\n', io_log_files[IOFD_LOG].fd.f);
605 fclose(io_log_files[IOFD_LOG].fd.f);
606 io_log_files[IOFD_LOG].fd.f = NULL;
609 * Clear I/O log function pointers for disabled log functions.
611 if (!io_log_files[IOFD_STDIN].enabled)
612 sudoers_io.log_stdin = NULL;
613 if (!io_log_files[IOFD_STDOUT].enabled)
614 sudoers_io.log_stdout = NULL;
615 if (!io_log_files[IOFD_STDERR].enabled)
616 sudoers_io.log_stderr = NULL;
617 if (!io_log_files[IOFD_TTYIN].enabled)
618 sudoers_io.log_ttyin = NULL;
619 if (!io_log_files[IOFD_TTYOUT].enabled)
620 sudoers_io.log_ttyout = NULL;
625 fatal_disable_setjmp();
627 if (details.runas_pw)
628 sudo_pw_delref(details.runas_pw);
630 if (details.runas_gr)
631 sudo_gr_delref(details.runas_gr);
634 debug_return_bool(rval);
638 sudoers_io_close(int exit_status, int error)
641 debug_decl(sudoers_io_close, SUDO_DEBUG_PLUGIN)
643 if (fatal_setjmp() != 0) {
644 /* called via fatal(), fatalx() or log_fatal() */
645 fatal_disable_setjmp();
649 for (i = 0; i < IOFD_MAX; i++) {
650 if (io_log_files[i].fd.v == NULL)
654 gzclose(io_log_files[i].fd.g);
657 fclose(io_log_files[i].fd.f);
663 sudoers_io_version(int verbose)
665 debug_decl(sudoers_io_version, SUDO_DEBUG_PLUGIN)
667 if (fatal_setjmp() != 0) {
668 /* called via fatal(), fatalx() or log_fatal() */
669 fatal_disable_setjmp();
670 debug_return_bool(-1);
673 sudo_printf(SUDO_CONV_INFO_MSG, "Sudoers I/O plugin version %s\n",
676 debug_return_bool(true);
680 * Generic I/O logging function. Called by the I/O logging entry points.
683 sudoers_io_log(const char *buf, unsigned int len, int idx)
685 struct timeval now, delay;
686 debug_decl(sudoers_io_version, SUDO_DEBUG_PLUGIN)
688 gettimeofday(&now, NULL);
690 if (fatal_setjmp() != 0) {
691 /* called via fatal(), fatalx() or log_fatal() */
692 fatal_disable_setjmp();
693 debug_return_bool(-1);
698 ignore_result(gzwrite(io_log_files[idx].fd.g, (const voidp)buf, len));
701 ignore_result(fwrite(buf, 1, len, io_log_files[idx].fd.f));
702 delay.tv_sec = now.tv_sec;
703 delay.tv_usec = now.tv_usec;
704 timevalsub(&delay, &last_time);
707 gzprintf(io_log_files[IOFD_TIMING].fd.g, "%d %f %d\n", idx,
708 delay.tv_sec + ((double)delay.tv_usec / 1000000), len);
711 fprintf(io_log_files[IOFD_TIMING].fd.f, "%d %f %d\n", idx,
712 delay.tv_sec + ((double)delay.tv_usec / 1000000), len);
713 last_time.tv_sec = now.tv_sec;
714 last_time.tv_usec = now.tv_usec;
716 debug_return_bool(true);
720 sudoers_io_log_ttyin(const char *buf, unsigned int len)
722 return sudoers_io_log(buf, len, IOFD_TTYIN);
726 sudoers_io_log_ttyout(const char *buf, unsigned int len)
728 return sudoers_io_log(buf, len, IOFD_TTYOUT);
732 sudoers_io_log_stdin(const char *buf, unsigned int len)
734 return sudoers_io_log(buf, len, IOFD_STDIN);
738 sudoers_io_log_stdout(const char *buf, unsigned int len)
740 return sudoers_io_log(buf, len, IOFD_STDOUT);
744 sudoers_io_log_stderr(const char *buf, unsigned int len)
746 return sudoers_io_log(buf, len, IOFD_STDERR);
749 __dso_public struct io_plugin sudoers_io = {
755 sudoers_io_log_ttyin,
756 sudoers_io_log_ttyout,
757 sudoers_io_log_stdin,
758 sudoers_io_log_stdout,
759 sudoers_io_log_stderr