File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / sudo / plugins / sudoers / logging.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:26:49 2012 UTC (12 years, 3 months ago) by misho
Branches: sudo, MAIN
CVS tags: v1_8_5p1, HEAD
sudo 1.8.5p1

    1: /*
    2:  * Copyright (c) 1994-1996, 1998-2011 Todd C. Miller <Todd.Miller@courtesan.com>
    3:  *
    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.
    7:  *
    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.
   15:  *
   16:  * Sponsored in part by the Defense Advanced Research Projects
   17:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
   18:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
   19:  */
   20: 
   21: #ifdef __TANDEM
   22: # include <floss.h>
   23: #endif
   24: 
   25: #include <config.h>
   26: 
   27: #include <sys/types.h>
   28: #include <sys/param.h>
   29: #include <sys/stat.h>
   30: #include <sys/ioctl.h>
   31: #include <sys/wait.h>
   32: #include <stdio.h>
   33: #ifdef STDC_HEADERS
   34: # include <stdlib.h>
   35: # include <stddef.h>
   36: #else
   37: # ifdef HAVE_STDLIB_H
   38: #  include <stdlib.h>
   39: # endif
   40: #endif /* STDC_HEADERS */
   41: #ifdef HAVE_STRING_H
   42: # include <string.h>
   43: #endif /* HAVE_STRING_H */
   44: #ifdef HAVE_STRINGS_H
   45: # include <strings.h>
   46: #endif /* HAVE_STRINGS_H */
   47: #ifdef HAVE_UNISTD_H
   48: # include <unistd.h>
   49: #endif /* HAVE_UNISTD_H */
   50: #ifdef HAVE_SETLOCALE
   51: # include <locale.h>
   52: #endif /* HAVE_SETLOCALE */
   53: #ifdef HAVE_NL_LANGINFO
   54: # include <langinfo.h>
   55: #endif /* HAVE_NL_LANGINFO */
   56: #include <pwd.h>
   57: #include <grp.h>
   58: #include <signal.h>
   59: #include <time.h>
   60: #include <ctype.h>
   61: #include <errno.h>
   62: #include <fcntl.h>
   63: #include <setjmp.h>
   64: 
   65: #include "sudoers.h"
   66: 
   67: static void do_syslog(int, char *);
   68: static void do_logfile(char *);
   69: static void send_mail(const char *fmt, ...);
   70: static int should_mail(int);
   71: static void mysyslog(int, const char *, ...);
   72: static char *new_logline(const char *, int);
   73: 
   74: extern sigjmp_buf error_jmp;
   75: 
   76: #define MAXSYSLOGTRIES	16	/* num of retries for broken syslogs */
   77: 
   78: /*
   79:  * We do an openlog(3)/closelog(3) for each message because some
   80:  * authentication methods (notably PAM) use syslog(3) for their
   81:  * own nefarious purposes and may call openlog(3) and closelog(3).
   82:  * Note that because we don't want to assume that all systems have
   83:  * vsyslog(3) (HP-UX doesn't) "%m" will not be expanded.
   84:  * Sadly this is a maze of #ifdefs.
   85:  */
   86: static void
   87: mysyslog(int pri, const char *fmt, ...)
   88: {
   89: #ifdef BROKEN_SYSLOG
   90:     int i;
   91: #endif
   92:     char buf[MAXSYSLOGLEN+1];
   93:     va_list ap;
   94:     debug_decl(mysyslog, SUDO_DEBUG_LOGGING)
   95: 
   96:     va_start(ap, fmt);
   97: #ifdef LOG_NFACILITIES
   98:     openlog("sudo", 0, def_syslog);
   99: #else
  100:     openlog("sudo", 0);
  101: #endif
  102:     vsnprintf(buf, sizeof(buf), fmt, ap);
  103: #ifdef BROKEN_SYSLOG
  104:     /*
  105:      * Some versions of syslog(3) don't guarantee success and return
  106:      * an int (notably HP-UX < 10.0).  So, if at first we don't succeed,
  107:      * try, try again...
  108:      */
  109:     for (i = 0; i < MAXSYSLOGTRIES; i++)
  110: 	if (syslog(pri, "%s", buf) == 0)
  111: 	    break;
  112: #else
  113:     syslog(pri, "%s", buf);
  114: #endif /* BROKEN_SYSLOG */
  115:     va_end(ap);
  116:     closelog();
  117:     debug_return;
  118: }
  119: 
  120: #define FMT_FIRST "%8s : %s"
  121: #define FMT_CONTD "%8s : (command continued) %s"
  122: 
  123: /*
  124:  * Log a message to syslog, pre-pending the username and splitting the
  125:  * message into parts if it is longer than MAXSYSLOGLEN.
  126:  */
  127: static void
  128: do_syslog(int pri, char *msg)
  129: {
  130:     size_t len, maxlen;
  131:     char *p, *tmp, save;
  132:     const char *fmt;
  133:     debug_decl(do_syslog, SUDO_DEBUG_LOGGING)
  134: 
  135: #ifdef HAVE_SETLOCALE
  136:     const char *old_locale = estrdup(setlocale(LC_ALL, NULL));
  137:     if (!setlocale(LC_ALL, def_sudoers_locale))
  138: 	setlocale(LC_ALL, "C");
  139: #endif /* HAVE_SETLOCALE */
  140: 
  141:     /*
  142:      * Log the full line, breaking into multiple syslog(3) calls if necessary
  143:      */
  144:     fmt = _(FMT_FIRST);
  145:     maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));
  146:     for (p = msg; *p != '\0'; ) {
  147: 	len = strlen(p);
  148: 	if (len > maxlen) {
  149: 	    /*
  150: 	     * Break up the line into what will fit on one syslog(3) line
  151: 	     * Try to avoid breaking words into several lines if possible.
  152: 	     */
  153: 	    tmp = memrchr(p, ' ', maxlen);
  154: 	    if (tmp == NULL)
  155: 		tmp = p + maxlen;
  156: 
  157: 	    /* NULL terminate line, but save the char to restore later */
  158: 	    save = *tmp;
  159: 	    *tmp = '\0';
  160: 
  161: 	    mysyslog(pri, fmt, user_name, p);
  162: 
  163: 	    *tmp = save;			/* restore saved character */
  164: 
  165: 	    /* Advance p and eliminate leading whitespace */
  166: 	    for (p = tmp; *p == ' '; p++)
  167: 		;
  168: 	} else {
  169: 	    mysyslog(pri, fmt, user_name, p);
  170: 	    p += len;
  171: 	}
  172: 	fmt = _(FMT_CONTD);
  173: 	maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));
  174:     }
  175: 
  176: #ifdef HAVE_SETLOCALE
  177:     setlocale(LC_ALL, old_locale);
  178:     efree((void *)old_locale);
  179: #endif /* HAVE_SETLOCALE */
  180: 
  181:     debug_return;
  182: }
  183: 
  184: static void
  185: do_logfile(char *msg)
  186: {
  187:     char *full_line;
  188:     size_t len;
  189:     mode_t oldmask;
  190:     time_t now;
  191:     FILE *fp;
  192:     debug_decl(do_logfile, SUDO_DEBUG_LOGGING)
  193: 
  194:     oldmask = umask(077);
  195:     fp = fopen(def_logfile, "a");
  196:     (void) umask(oldmask);
  197:     if (fp == NULL) {
  198: 	send_mail(_("unable to open log file: %s: %s"),
  199: 	    def_logfile, strerror(errno));
  200:     } else if (!lock_file(fileno(fp), SUDO_LOCK)) {
  201: 	send_mail(_("unable to lock log file: %s: %s"),
  202: 	    def_logfile, strerror(errno));
  203:     } else {
  204: #ifdef HAVE_SETLOCALE
  205: 	const char *old_locale = estrdup(setlocale(LC_ALL, NULL));
  206: 	if (!setlocale(LC_ALL, def_sudoers_locale))
  207: 	    setlocale(LC_ALL, "C");
  208: #endif /* HAVE_SETLOCALE */
  209: 
  210: 	now = time(NULL);
  211: 	if (def_loglinelen < sizeof(LOG_INDENT)) {
  212: 	    /* Don't pretty-print long log file lines (hard to grep) */
  213: 	    if (def_log_host)
  214: 		(void) fprintf(fp, "%s : %s : HOST=%s : %s\n",
  215: 		    get_timestr(now, def_log_year), user_name, user_shost, msg);
  216: 	    else
  217: 		(void) fprintf(fp, "%s : %s : %s\n",
  218: 		    get_timestr(now, def_log_year), user_name, msg);
  219: 	} else {
  220: 	    if (def_log_host)
  221: 		len = easprintf(&full_line, "%s : %s : HOST=%s : %s",
  222: 		    get_timestr(now, def_log_year), user_name, user_shost, msg);
  223: 	    else
  224: 		len = easprintf(&full_line, "%s : %s : %s",
  225: 		    get_timestr(now, def_log_year), user_name, msg);
  226: 
  227: 	    /*
  228: 	     * Print out full_line with word wrap around def_loglinelen chars.
  229: 	     */
  230: 	    writeln_wrap(fp, full_line, len, def_loglinelen);
  231: 	    efree(full_line);
  232: 	}
  233: 	(void) fflush(fp);
  234: 	(void) lock_file(fileno(fp), SUDO_UNLOCK);
  235: 	(void) fclose(fp);
  236: 
  237: #ifdef HAVE_SETLOCALE
  238: 	setlocale(LC_ALL, old_locale);
  239: 	efree((void *)old_locale);
  240: #endif /* HAVE_SETLOCALE */
  241:     }
  242:     debug_return;
  243: }
  244: 
  245: /*
  246:  * Log and mail the denial message, optionally informing the user.
  247:  */
  248: void
  249: log_denial(int status, int inform_user)
  250: {
  251:     char *logline, *message;
  252:     debug_decl(log_denial, SUDO_DEBUG_LOGGING)
  253: 
  254:     /* Set error message. */
  255:     if (ISSET(status, FLAG_NO_USER))
  256: 	message = _("user NOT in sudoers");
  257:     else if (ISSET(status, FLAG_NO_HOST))
  258: 	message = _("user NOT authorized on host");
  259:     else
  260: 	message = _("command not allowed");
  261: 
  262:     logline = new_logline(message, 0);
  263: 
  264:     if (should_mail(status))
  265: 	send_mail("%s", logline);	/* send mail based on status */
  266: 
  267:     /* Inform the user if they failed to authenticate.  */
  268:     if (inform_user) {
  269: 	if (ISSET(status, FLAG_NO_USER)) {
  270: 	    sudo_printf(SUDO_CONV_ERROR_MSG, _("%s is not in the sudoers "
  271: 		"file.  This incident will be reported.\n"), user_name);
  272: 	} else if (ISSET(status, FLAG_NO_HOST)) {
  273: 	    sudo_printf(SUDO_CONV_ERROR_MSG, _("%s is not allowed to run sudo "
  274: 		"on %s.  This incident will be reported.\n"),
  275: 		user_name, user_shost);
  276: 	} else if (ISSET(status, FLAG_NO_CHECK)) {
  277: 	    sudo_printf(SUDO_CONV_ERROR_MSG, _("Sorry, user %s may not run "
  278: 		"sudo on %s.\n"), user_name, user_shost);
  279: 	} else {
  280: 	    sudo_printf(SUDO_CONV_ERROR_MSG, _("Sorry, user %s is not allowed "
  281: 		"to execute '%s%s%s' as %s%s%s on %s.\n"),
  282: 		user_name, user_cmnd, user_args ? " " : "",
  283: 		user_args ? user_args : "",
  284: 		list_pw ? list_pw->pw_name : runas_pw ?
  285: 		runas_pw->pw_name : user_name, runas_gr ? ":" : "",
  286: 		runas_gr ? runas_gr->gr_name : "", user_host);
  287: 	}
  288:     }
  289: 
  290:     /*
  291:      * Log via syslog and/or a file.
  292:      */
  293:     if (def_syslog)
  294: 	do_syslog(def_syslog_badpri, logline);
  295:     if (def_logfile)
  296: 	do_logfile(logline);
  297: 
  298:     efree(logline);
  299:     debug_return;
  300: }
  301: 
  302: /*
  303:  * Log and potentially mail the allowed command.
  304:  */
  305: void
  306: log_allowed(int status)
  307: {
  308:     char *logline;
  309:     debug_decl(log_allowed, SUDO_DEBUG_LOGGING)
  310: 
  311:     logline = new_logline(NULL, 0);
  312: 
  313:     if (should_mail(status))
  314: 	send_mail("%s", logline);	/* send mail based on status */
  315: 
  316:     /*
  317:      * Log via syslog and/or a file.
  318:      */
  319:     if (def_syslog)
  320: 	do_syslog(def_syslog_goodpri, logline);
  321:     if (def_logfile)
  322: 	do_logfile(logline);
  323: 
  324:     efree(logline);
  325:     debug_return;
  326: }
  327: 
  328: /*
  329:  * Perform logging for log_error()/log_fatal()
  330:  */
  331: static void
  332: vlog_error(int flags, const char *fmt, va_list ap)
  333: {
  334:     int serrno = errno;
  335:     char *logline, *message;
  336:     debug_decl(vlog_error, SUDO_DEBUG_LOGGING)
  337: 
  338:     /* Expand printf-style format + args. */
  339:     evasprintf(&message, fmt, ap);
  340: 
  341:     /* Become root if we are not already to avoid user interference */
  342:     set_perms(PERM_ROOT|PERM_NOEXIT);
  343: 
  344:     if (ISSET(flags, MSG_ONLY))
  345: 	logline = message;
  346:     else
  347: 	logline = new_logline(message, ISSET(flags, USE_ERRNO) ? serrno : 0);
  348: 
  349:     /*
  350:      * Tell the user.
  351:      */
  352:     if (!ISSET(flags, NO_STDERR)) {
  353: 	if (ISSET(flags, USE_ERRNO))
  354: 	    warning("%s", message);
  355: 	else
  356: 	    warningx("%s", message);
  357:     }
  358:     if (logline != message)
  359:         efree(message);
  360: 
  361:     /*
  362:      * Send a copy of the error via mail.
  363:      */
  364:     if (!ISSET(flags, NO_MAIL))
  365: 	send_mail("%s", logline);
  366: 
  367:     /*
  368:      * Log to syslog and/or a file.
  369:      */
  370:     if (def_syslog)
  371: 	do_syslog(def_syslog_badpri, logline);
  372:     if (def_logfile)
  373: 	do_logfile(logline);
  374: 
  375:     efree(logline);
  376: 
  377:     restore_perms();
  378: 
  379:     debug_return;
  380: }
  381: 
  382: void
  383: log_error(int flags, const char *fmt, ...)
  384: {
  385:     va_list ap;
  386:     debug_decl(log_error, SUDO_DEBUG_LOGGING)
  387: 
  388:     /* Log the error. */
  389:     va_start(ap, fmt);
  390:     vlog_error(flags, fmt, ap);
  391:     va_end(ap);
  392: 
  393:     debug_return;
  394: }
  395: 
  396: void
  397: log_fatal(int flags, const char *fmt, ...)
  398: {
  399:     va_list ap;
  400:     debug_decl(log_error, SUDO_DEBUG_LOGGING)
  401: 
  402:     /* Log the error. */
  403:     va_start(ap, fmt);
  404:     vlog_error(flags, fmt, ap);
  405:     va_end(ap);
  406: 
  407:     /* Exit the plugin. */
  408:     plugin_cleanup(0);
  409:     sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
  410:     siglongjmp(error_jmp, 1);
  411: }
  412: 
  413: #define MAX_MAILFLAGS	63
  414: 
  415: /*
  416:  * Send a message to MAILTO user
  417:  */
  418: static void
  419: send_mail(const char *fmt, ...)
  420: {
  421:     FILE *mail;
  422:     char *p;
  423:     int fd, pfd[2], status;
  424:     pid_t pid, rv;
  425:     sigaction_t sa;
  426:     va_list ap;
  427: #ifndef NO_ROOT_MAILER
  428:     static char *root_envp[] = {
  429: 	"HOME=/",
  430: 	"PATH=/usr/bin:/bin:/usr/sbin:/sbin",
  431: 	"LOGNAME=root",
  432: 	"USERNAME=root",
  433: 	"USER=root",
  434: 	NULL
  435:     };
  436: #endif /* NO_ROOT_MAILER */
  437:     debug_decl(send_mail, SUDO_DEBUG_LOGGING)
  438: 
  439:     /* Just return if mailer is disabled. */
  440:     if (!def_mailerpath || !def_mailto)
  441: 	debug_return;
  442: 
  443:     /* Fork and return, child will daemonize. */
  444:     switch (pid = sudo_debug_fork()) {
  445: 	case -1:
  446: 	    /* Error. */
  447: 	    error(1, _("unable to fork"));
  448: 	    break;
  449: 	case 0:
  450: 	    /* Child. */
  451: 	    switch (pid = fork()) {
  452: 		case -1:
  453: 		    /* Error. */
  454: 		    mysyslog(LOG_ERR, _("unable to fork: %m"));
  455: 		    sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to fork: %s",
  456: 			strerror(errno));
  457: 		    _exit(1);
  458: 		case 0:
  459: 		    /* Grandchild continues below. */
  460: 		    break;
  461: 		default:
  462: 		    /* Parent will wait for us. */
  463: 		    _exit(0);
  464: 	    }
  465: 	    break;
  466: 	default:
  467: 	    /* Parent. */
  468: 	    do {
  469: 		rv = waitpid(pid, &status, 0);
  470: 	    } while (rv == -1 && errno == EINTR);
  471: 	    return; /* not debug */
  472:     }
  473: 
  474:     /* Daemonize - disassociate from session/tty. */
  475:     if (setsid() == -1)
  476:       warning("setsid");
  477:     if (chdir("/") == -1)
  478:       warning("chdir(/)");
  479:     if ((fd = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) {
  480: 	(void) dup2(fd, STDIN_FILENO);
  481: 	(void) dup2(fd, STDOUT_FILENO);
  482: 	(void) dup2(fd, STDERR_FILENO);
  483:     }
  484: 
  485: #ifdef HAVE_SETLOCALE
  486:     if (!setlocale(LC_ALL, def_sudoers_locale)) {
  487: 	setlocale(LC_ALL, "C");
  488: 	efree(def_sudoers_locale);
  489: 	def_sudoers_locale = estrdup("C");
  490:     }
  491: #endif /* HAVE_SETLOCALE */
  492: 
  493:     /* Close password, group and other fds so we don't leak. */
  494:     sudo_endpwent();
  495:     sudo_endgrent();
  496:     closefrom(STDERR_FILENO + 1);
  497: 
  498:     /* Ignore SIGPIPE in case mailer exits prematurely (or is missing). */
  499:     zero_bytes(&sa, sizeof(sa));
  500:     sigemptyset(&sa.sa_mask);
  501:     sa.sa_flags = SA_INTERRUPT;
  502:     sa.sa_handler = SIG_IGN;
  503:     (void) sigaction(SIGPIPE, &sa, NULL);
  504: 
  505:     if (pipe(pfd) == -1) {
  506: 	mysyslog(LOG_ERR, _("unable to open pipe: %m"));
  507: 	sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to open pipe: %s",
  508: 	    strerror(errno));
  509: 	sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
  510: 	_exit(1);
  511:     }
  512: 
  513:     switch (pid = sudo_debug_fork()) {
  514: 	case -1:
  515: 	    /* Error. */
  516: 	    mysyslog(LOG_ERR, _("unable to fork: %m"));
  517: 	    sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to fork: %s",
  518: 		strerror(errno));
  519: 	    sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
  520: 	    _exit(1);
  521: 	    break;
  522: 	case 0:
  523: 	    {
  524: 		char *argv[MAX_MAILFLAGS + 1];
  525: 		char *mpath, *mflags;
  526: 		int i;
  527: 
  528: 		/* Child, set stdin to output side of the pipe */
  529: 		if (pfd[0] != STDIN_FILENO) {
  530: 		    if (dup2(pfd[0], STDIN_FILENO) == -1) {
  531: 			mysyslog(LOG_ERR, _("unable to dup stdin: %m"));
  532: 			sudo_debug_printf(SUDO_DEBUG_ERROR,
  533: 			    "unable to dup stdin: %s", strerror(errno));
  534: 			_exit(127);
  535: 		    }
  536: 		    (void) close(pfd[0]);
  537: 		}
  538: 		(void) close(pfd[1]);
  539: 
  540: 		/* Build up an argv based on the mailer path and flags */
  541: 		mflags = estrdup(def_mailerflags);
  542: 		mpath = estrdup(def_mailerpath);
  543: 		if ((argv[0] = strrchr(mpath, ' ')))
  544: 		    argv[0]++;
  545: 		else
  546: 		    argv[0] = mpath;
  547: 
  548: 		i = 1;
  549: 		if ((p = strtok(mflags, " \t"))) {
  550: 		    do {
  551: 			argv[i] = p;
  552: 		    } while (++i < MAX_MAILFLAGS && (p = strtok(NULL, " \t")));
  553: 		}
  554: 		argv[i] = NULL;
  555: 
  556: 		/*
  557: 		 * Depending on the config, either run the mailer as root
  558: 		 * (so user cannot kill it) or as the user (for the paranoid).
  559: 		 */
  560: #ifndef NO_ROOT_MAILER
  561: 		set_perms(PERM_ROOT|PERM_NOEXIT);
  562: 		execve(mpath, argv, root_envp);
  563: #else
  564: 		set_perms(PERM_FULL_USER|PERM_NOEXIT);
  565: 		execv(mpath, argv);
  566: #endif /* NO_ROOT_MAILER */
  567: 		mysyslog(LOG_ERR, _("unable to execute %s: %m"), mpath);
  568: 		sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to execute %s: %s",
  569: 		    mpath, strerror(errno));
  570: 		_exit(127);
  571: 	    }
  572: 	    break;
  573:     }
  574: 
  575:     (void) close(pfd[0]);
  576:     mail = fdopen(pfd[1], "w");
  577: 
  578:     /* Pipes are all setup, send message. */
  579:     (void) fprintf(mail, "To: %s\nFrom: %s\nAuto-Submitted: %s\nSubject: ",
  580: 	def_mailto, def_mailfrom ? def_mailfrom : user_name, "auto-generated");
  581:     for (p = def_mailsub; *p; p++) {
  582: 	/* Expand escapes in the subject */
  583: 	if (*p == '%' && *(p+1) != '%') {
  584: 	    switch (*(++p)) {
  585: 		case 'h':
  586: 		    (void) fputs(user_host, mail);
  587: 		    break;
  588: 		case 'u':
  589: 		    (void) fputs(user_name, mail);
  590: 		    break;
  591: 		default:
  592: 		    p--;
  593: 		    break;
  594: 	    }
  595: 	} else
  596: 	    (void) fputc(*p, mail);
  597:     }
  598: 
  599: #ifdef HAVE_NL_LANGINFO
  600:     if (strcmp(def_sudoers_locale, "C") != 0)
  601: 	(void) fprintf(mail, "\nContent-Type: text/plain; charset=\"%s\"\nContent-Transfer-Encoding: 8bit", nl_langinfo(CODESET));
  602: #endif /* HAVE_NL_LANGINFO */
  603: 
  604:     (void) fprintf(mail, "\n\n%s : %s : %s : ", user_host,
  605: 	get_timestr(time(NULL), def_log_year), user_name);
  606:     va_start(ap, fmt);
  607:     (void) vfprintf(mail, fmt, ap);
  608:     va_end(ap);
  609:     fputs("\n\n", mail);
  610: 
  611:     fclose(mail);
  612:     do {
  613:         rv = waitpid(pid, &status, 0);
  614:     } while (rv == -1 && errno == EINTR);
  615:     sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys);
  616:     _exit(0);
  617: }
  618: 
  619: /*
  620:  * Determine whether we should send mail based on "status" and defaults options.
  621:  */
  622: static int
  623: should_mail(int status)
  624: {
  625:     debug_decl(should_mail, SUDO_DEBUG_LOGGING)
  626: 
  627:     debug_return_bool(def_mail_always || ISSET(status, VALIDATE_ERROR) ||
  628: 	(def_mail_no_user && ISSET(status, FLAG_NO_USER)) ||
  629: 	(def_mail_no_host && ISSET(status, FLAG_NO_HOST)) ||
  630: 	(def_mail_no_perms && !ISSET(status, VALIDATE_OK)));
  631: }
  632: 
  633: #define	LL_TTY_STR	"TTY="
  634: #define	LL_CWD_STR	"PWD="		/* XXX - should be CWD= */
  635: #define	LL_USER_STR	"USER="
  636: #define	LL_GROUP_STR	"GROUP="
  637: #define	LL_ENV_STR	"ENV="
  638: #define	LL_CMND_STR	"COMMAND="
  639: #define	LL_TSID_STR	"TSID="
  640: 
  641: #define IS_SESSID(s) ( \
  642:     isalnum((unsigned char)(s)[0]) && isalnum((unsigned char)(s)[1]) && \
  643:     (s)[2] == '/' && \
  644:     isalnum((unsigned char)(s)[3]) && isalnum((unsigned char)(s)[4]) && \
  645:     (s)[5] == '/' && \
  646:     isalnum((unsigned char)(s)[6]) && isalnum((unsigned char)(s)[7]) && \
  647:     (s)[8] == '\0')
  648: 
  649: /*
  650:  * Allocate and fill in a new logline.
  651:  */
  652: static char *
  653: new_logline(const char *message, int serrno)
  654: {
  655:     size_t len = 0;
  656:     char *errstr = NULL;
  657:     char *evstr = NULL;
  658:     char *line, sessid[7], *tsid = NULL;
  659:     debug_decl(new_logline, SUDO_DEBUG_LOGGING)
  660: 
  661:     /* A TSID may be a sudoers-style session ID or a free-form string. */
  662:     if (sudo_user.iolog_file != NULL) {
  663: 	if (IS_SESSID(sudo_user.iolog_file)) {
  664: 	    sessid[0] = sudo_user.iolog_file[0];
  665: 	    sessid[1] = sudo_user.iolog_file[1];
  666: 	    sessid[2] = sudo_user.iolog_file[3];
  667: 	    sessid[3] = sudo_user.iolog_file[4];
  668: 	    sessid[4] = sudo_user.iolog_file[6];
  669: 	    sessid[5] = sudo_user.iolog_file[7];
  670: 	    sessid[6] = '\0';
  671: 	    tsid = sessid;
  672: 	} else {
  673: 	    tsid = sudo_user.iolog_file;
  674: 	}
  675:     }
  676: 
  677:     /*
  678:      * Compute line length
  679:      */
  680:     if (message != NULL)
  681: 	len += strlen(message) + 3;
  682:     if (serrno) {
  683: 	errstr = strerror(serrno);
  684: 	len += strlen(errstr) + 3;
  685:     }
  686:     len += sizeof(LL_TTY_STR) + 2 + strlen(user_tty);
  687:     len += sizeof(LL_CWD_STR) + 2 + strlen(user_cwd);
  688:     if (runas_pw != NULL)
  689: 	len += sizeof(LL_USER_STR) + 2 + strlen(runas_pw->pw_name);
  690:     if (runas_gr != NULL)
  691: 	len += sizeof(LL_GROUP_STR) + 2 + strlen(runas_gr->gr_name);
  692:     if (tsid != NULL)
  693: 	len += sizeof(LL_TSID_STR) + 2 + strlen(tsid);
  694:     if (sudo_user.env_vars != NULL) {
  695: 	size_t evlen = 0;
  696: 	char * const *ep;
  697: 
  698: 	for (ep = sudo_user.env_vars; *ep != NULL; ep++)
  699: 	    evlen += strlen(*ep) + 1;
  700: 	evstr = emalloc(evlen);
  701: 	evstr[0] = '\0';
  702: 	for (ep = sudo_user.env_vars; *ep != NULL; ep++) {
  703: 	    strlcat(evstr, *ep, evlen);
  704: 	    strlcat(evstr, " ", evlen);	/* NOTE: last one will fail */
  705: 	}
  706: 	len += sizeof(LL_ENV_STR) + 2 + evlen;
  707:     }
  708:     if (user_cmnd != NULL) {
  709: 	/* Note: we log "sudo -l command arg ..." as "list command arg ..." */
  710: 	len += sizeof(LL_CMND_STR) - 1 + strlen(user_cmnd);
  711: 	if (ISSET(sudo_mode, MODE_CHECK))
  712: 	    len += sizeof("list ") - 1;
  713: 	if (user_args != NULL)
  714: 	    len += strlen(user_args) + 1;
  715:     }
  716: 
  717:     /*
  718:      * Allocate and build up the line.
  719:      */
  720:     line = emalloc(++len);
  721:     line[0] = '\0';
  722: 
  723:     if (message != NULL) {
  724: 	if (strlcat(line, message, len) >= len ||
  725: 	    strlcat(line, errstr ? " : " : " ; ", len) >= len)
  726: 	    goto toobig;
  727:     }
  728:     if (serrno) {
  729: 	if (strlcat(line, errstr, len) >= len ||
  730: 	    strlcat(line, " ; ", len) >= len)
  731: 	    goto toobig;
  732:     }
  733:     if (strlcat(line, LL_TTY_STR, len) >= len ||
  734: 	strlcat(line, user_tty, len) >= len ||
  735: 	strlcat(line, " ; ", len) >= len)
  736: 	goto toobig;
  737:     if (strlcat(line, LL_CWD_STR, len) >= len ||
  738: 	strlcat(line, user_cwd, len) >= len ||
  739: 	strlcat(line, " ; ", len) >= len)
  740: 	goto toobig;
  741:     if (runas_pw != NULL) {
  742: 	if (strlcat(line, LL_USER_STR, len) >= len ||
  743: 	    strlcat(line, runas_pw->pw_name, len) >= len ||
  744: 	    strlcat(line, " ; ", len) >= len)
  745: 	    goto toobig;
  746:     }
  747:     if (runas_gr != NULL) {
  748: 	if (strlcat(line, LL_GROUP_STR, len) >= len ||
  749: 	    strlcat(line, runas_gr->gr_name, len) >= len ||
  750: 	    strlcat(line, " ; ", len) >= len)
  751: 	    goto toobig;
  752:     }
  753:     if (tsid != NULL) {
  754: 	if (strlcat(line, LL_TSID_STR, len) >= len ||
  755: 	    strlcat(line, tsid, len) >= len ||
  756: 	    strlcat(line, " ; ", len) >= len)
  757: 	    goto toobig;
  758:     }
  759:     if (evstr != NULL) {
  760: 	if (strlcat(line, LL_ENV_STR, len) >= len ||
  761: 	    strlcat(line, evstr, len) >= len ||
  762: 	    strlcat(line, " ; ", len) >= len)
  763: 	    goto toobig;
  764: 	efree(evstr);
  765:     }
  766:     if (user_cmnd != NULL) {
  767: 	if (strlcat(line, LL_CMND_STR, len) >= len)
  768: 	    goto toobig;
  769: 	if (ISSET(sudo_mode, MODE_CHECK) && strlcat(line, "list ", len) >= len)
  770: 	    goto toobig;
  771: 	if (strlcat(line, user_cmnd, len) >= len)
  772: 	    goto toobig;
  773: 	if (user_args != NULL) {
  774: 	    if (strlcat(line, " ", len) >= len ||
  775: 		strlcat(line, user_args, len) >= len)
  776: 		goto toobig;
  777: 	}
  778:     }
  779: 
  780:     debug_return_str(line);
  781: toobig:
  782:     errorx(1, _("internal error: insufficient space for log line"));
  783: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>