X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=logging.c;h=b536305dfd0cada0f3ab2360f62d0ee9d99a2f9e;hb=94380bacfb7fb71dd9d99703b67993ece242e900;hp=723d19e99fcdd3662d1ffacfab4cb9bf1bc6d1c5;hpb=ca3ab12a66fc683cabf546fd405cfbf39ef9fb6f;p=debian%2Fsudo diff --git a/logging.c b/logging.c index 723d19e..b536305 100644 --- a/logging.c +++ b/logging.c @@ -1,42 +1,33 @@ /* - * Copyright (c) 1994-1996,1998-2001 Todd C. Miller - * All rights reserved. + * Copyright (c) 1994-1996, 1998-2008 Todd C. Miller * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * 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. * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * 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. * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * 4. Products derived from this software may not be called "Sudo" nor - * may "Sudo" appear in their names without specific prior written - * permission from the author. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. */ -#include "config.h" +#ifdef __TANDEM +# include +#endif + +#include #include #include #include +#include #include #include #ifdef STDC_HEADERS @@ -58,22 +49,25 @@ # include #endif /* HAVE_UNISTD_H */ #include +#include #include #include #include +#include #include "sudo.h" #ifndef lint -static const char rcsid[] = "$Sudo: logging.c,v 1.153 2002/01/16 21:28:25 millert Exp $"; +__unused static const char rcsid[] = "$Sudo: logging.c,v 1.203 2008/11/09 14:13:12 millert Exp $"; #endif /* lint */ static void do_syslog __P((int, char *)); static void do_logfile __P((char *)); static void send_mail __P((char *)); -static void mail_auth __P((int, char *)); +static int should_mail __P((int)); static char *get_timestr __P((void)); static void mysyslog __P((int, const char *, ...)); +static char *new_logline __P((const char *, int)); #define MAXSYSLOGTRIES 16 /* num of retries for broken syslogs */ @@ -107,9 +101,9 @@ mysyslog(pri, fmt, va_alist) va_start(ap); #endif #ifdef LOG_NFACILITIES - openlog(Argv[0], 0, def_ival(I_LOGFAC)); + openlog("sudo", 0, def_syslog); #else - openlog(Argv[0], 0); + openlog("sudo", 0); #endif vsnprintf(buf, sizeof(buf), fmt, ap); #ifdef BROKEN_SYSLOG @@ -137,46 +131,45 @@ do_syslog(pri, msg) int pri; char *msg; { - int count; - char *p; - char *tmp; - char save; + size_t len, maxlen; + char *p, *tmp, save; + const char *fmt; + const char *fmt_first = "%8s : %s"; + const char *fmt_contd = "%8s : (command continued) %s"; /* * Log the full line, breaking into multiple syslog(3) calls if necessary */ - for (p = msg, count = 0; *p && count < strlen(msg) / MAXSYSLOGLEN + 1; - count++) { - if (strlen(p) > MAXSYSLOGLEN) { + fmt = fmt_first; + maxlen = MAXSYSLOGLEN - (sizeof(fmt_first) - 6 + strlen(user_name)); + for (p = msg; *p != '\0'; ) { + len = strlen(p); + if (len > maxlen) { /* * Break up the line into what will fit on one syslog(3) line - * Try to break on a word boundary if possible. + * Try to avoid breaking words into several lines if possible. */ - for (tmp = p + MAXSYSLOGLEN; tmp > p && *tmp != ' '; tmp--) - ; - if (tmp <= p) - tmp = p + MAXSYSLOGLEN; + tmp = memrchr(p, ' ', maxlen); + if (tmp == NULL) + tmp = p + maxlen; /* NULL terminate line, but save the char to restore later */ save = *tmp; *tmp = '\0'; - if (count == 0) - mysyslog(pri, "%8.8s : %s", user_name, p); - else - mysyslog(pri, "%8.8s : (command continued) %s", user_name, p); + mysyslog(pri, fmt, user_name, p); *tmp = save; /* restore saved character */ - /* Eliminate leading whitespace */ - for (p = tmp; *p != ' ' && *p !='\0'; p++) + /* Advance p and eliminate leading whitespace */ + for (p = tmp; *p == ' '; p++) ; } else { - if (count == 0) - mysyslog(pri, "%8.8s : %s", user_name, p); - else - mysyslog(pri, "%8.8s : (command continued) %s", user_name, p); + mysyslog(pri, fmt, user_name, p); + p += len; } + fmt = fmt_contd; + maxlen = MAXSYSLOGLEN - (sizeof(fmt_contd) - 6 + strlen(user_name)); } } @@ -188,32 +181,33 @@ do_logfile(msg) char *beg, *oldend, *end; FILE *fp; mode_t oldmask; - int maxlen = def_ival(I_LOGLINELEN); + size_t maxlen; oldmask = umask(077); - fp = fopen(def_str(I_LOGFILE), "a"); + maxlen = def_loglinelen > 0 ? def_loglinelen : 0; + fp = fopen(def_logfile, "a"); (void) umask(oldmask); if (fp == NULL) { easprintf(&full_line, "Can't open log file: %s: %s", - def_str(I_LOGFILE), strerror(errno)); + def_logfile, strerror(errno)); send_mail(full_line); - free(full_line); + efree(full_line); } else if (!lock_file(fileno(fp), SUDO_LOCK)) { easprintf(&full_line, "Can't lock log file: %s: %s", - def_str(I_LOGFILE), strerror(errno)); + def_logfile, strerror(errno)); send_mail(full_line); - free(full_line); + efree(full_line); } else { - if (def_ival(I_LOGLINELEN) == 0) { + if (def_loglinelen == 0) { /* Don't pretty-print long log file lines (hard to grep) */ - if (def_flag(I_LOG_HOST)) + if (def_log_host) (void) fprintf(fp, "%s : %s : HOST=%s : %s\n", get_timestr(), user_name, user_shost, msg); else (void) fprintf(fp, "%s : %s : %s\n", get_timestr(), user_name, msg); } else { - if (def_flag(I_LOG_HOST)) + if (def_log_host) easprintf(&full_line, "%s : %s : HOST=%s : %s", get_timestr(), user_name, user_shost, msg); else @@ -267,7 +261,7 @@ do_logfile(msg) beg = NULL; /* exit condition */ } } - free(full_line); + efree(full_line); } (void) fflush(fp); (void) lock_file(fileno(fp), SUDO_UNLOCK); @@ -276,75 +270,93 @@ do_logfile(msg) } /* - * Two main functions, log_error() to log errors and log_auth() to - * log allow/deny messages. + * Log and mail the denial message, optionally informing the user. */ void -log_auth(status, inform_user) +log_denial(status, inform_user) int status; int inform_user; { char *message; char *logline; - int pri; - if (status & VALIDATE_OK) - pri = def_ival(I_GOODPRI); - else - pri = def_ival(I_BADPRI); - - /* Set error message, if any. */ - if (status & VALIDATE_OK) - message = ""; - else if (status & FLAG_NO_USER) - message = "user NOT in sudoers ; "; - else if (status & FLAG_NO_HOST) - message = "user NOT authorized on host ; "; - else if (status & VALIDATE_NOT_OK) - message = "command not allowed ; "; + /* Set error message. */ + if (ISSET(status, FLAG_NO_USER)) + message = "user NOT in sudoers"; + else if (ISSET(status, FLAG_NO_HOST)) + message = "user NOT authorized on host"; else - message = "unknown error ; "; + message = "command not allowed"; - easprintf(&logline, "%sTTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s%s%s", - message, user_tty, user_cwd, *user_runas, user_cmnd, - user_args ? " " : "", user_args ? user_args : ""); + logline = new_logline(message, 0); - mail_auth(status, logline); /* send mail based on status */ + if (should_mail(status)) + send_mail(logline); /* send mail based on status */ /* Inform the user if they failed to authenticate. */ - if (inform_user && (status & VALIDATE_NOT_OK)) { - if (status & FLAG_NO_USER) + if (inform_user) { + if (ISSET(status, FLAG_NO_USER)) (void) fprintf(stderr, "%s is not in the sudoers file. %s", user_name, "This incident will be reported.\n"); - else if (status & FLAG_NO_HOST) + else if (ISSET(status, FLAG_NO_HOST)) (void) fprintf(stderr, "%s is not allowed to run sudo on %s. %s", user_name, user_shost, "This incident will be reported.\n"); - else if (status & FLAG_NO_CHECK) + else if (ISSET(status, FLAG_NO_CHECK)) (void) fprintf(stderr, "Sorry, user %s may not run sudo on %s.\n", user_name, user_shost); else (void) fprintf(stderr, - "Sorry, user %s is not allowed to execute '%s%s%s' as %s on %s.\n", + "Sorry, user %s is not allowed to execute '%s%s%s' as %s%s%s on %s.\n", user_name, user_cmnd, user_args ? " " : "", - user_args ? user_args : "", *user_runas, user_host); + user_args ? user_args : "", + list_pw ? list_pw->pw_name : runas_pw ? + runas_pw->pw_name : user_name, runas_gr ? ":" : "", + runas_gr ? runas_gr->gr_name : "", user_host); } /* * Log via syslog and/or a file. */ - if (def_str(I_SYSLOG)) - do_syslog(pri, logline); - if (def_str(I_LOGFILE)) + if (def_syslog) + do_syslog(def_syslog_badpri, logline); + if (def_logfile) + do_logfile(logline); + + efree(logline); +} + +/* + * Log and potentially mail the allowed command. + */ +void +log_allowed(status) + int status; +{ + char *logline; + + logline = new_logline(NULL, 0); + + if (should_mail(status)) + send_mail(logline); /* send mail based on status */ + + /* + * Log via syslog and/or a file. + */ + if (def_syslog) + do_syslog(def_syslog_goodpri, logline); + if (def_logfile) do_logfile(logline); - free(logline); + efree(logline); } void #ifdef __STDC__ log_error(int flags, const char *fmt, ...) #else -log_error(va_alist) +log_error(flags, fmt, va_alist) + int flags; + const char *fmt; va_dcl #endif { @@ -355,75 +367,53 @@ log_error(va_alist) #ifdef __STDC__ va_start(ap, fmt); #else - int flags; - const char *fmt; - va_start(ap); - flags = va_arg(ap, int); - fmt = va_arg(ap, const char *); #endif - /* Become root if we are not already to avoid user control */ - if (geteuid() != 0) - set_perms(PERM_ROOT, 0); + /* Become root if we are not already to avoid user interference */ + set_perms(PERM_ROOT); /* Expand printf-style format + args. */ evasprintf(&message, fmt, ap); va_end(ap); - if (flags & MSG_ONLY) + if (ISSET(flags, MSG_ONLY)) logline = message; - else if (flags & USE_ERRNO) { - if (user_args) { - easprintf(&logline, - "%s: %s ; TTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s %s", - message, strerror(serrno), user_tty, user_cwd, *user_runas, - user_cmnd, user_args); - } else { - easprintf(&logline, - "%s: %s ; TTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s", message, - strerror(serrno), user_tty, user_cwd, *user_runas, user_cmnd); - } - } else { - if (user_args) { - easprintf(&logline, - "%s ; TTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s %s", message, - user_tty, user_cwd, *user_runas, user_cmnd, user_args); - } else { - easprintf(&logline, - "%s ; TTY=%s ; PWD=%s ; USER=%s ; COMMAND=%s", message, - user_tty, user_cwd, *user_runas, user_cmnd); - } - } + else + logline = new_logline(message, ISSET(flags, USE_ERRNO) ? serrno : 0); /* * Tell the user. */ - (void) fprintf(stderr, "%s: %s", Argv[0], message); - if (flags & USE_ERRNO) - (void) fprintf(stderr, ": %s", strerror(serrno)); - (void) fputc('\n', stderr); + if (!ISSET(flags, NO_STDERR)) { + if (ISSET(flags, USE_ERRNO)) + warning("%s", message); + else + warningx("%s", message); + } + efree(message); /* * Send a copy of the error via mail. */ - if (!(flags & NO_MAIL)) + if (!ISSET(flags, NO_MAIL)) send_mail(logline); /* * Log to syslog and/or a file. */ - if (def_str(I_SYSLOG)) - do_syslog(def_ival(I_BADPRI), logline); - if (def_str(I_LOGFILE)) + if (def_syslog) + do_syslog(def_syslog_badpri, logline); + if (def_logfile) do_logfile(logline); - free(message); if (logline != message) - free(logline); + efree(logline); - if (!(flags & NO_EXIT)) + if (!ISSET(flags, NO_EXIT)) { + cleanup(0); exit(1); + } } #define MAX_MAILFLAGS 63 @@ -437,38 +427,99 @@ send_mail(line) { FILE *mail; char *p; - int pfd[2], pid, status; - sigset_t set, oset; + int fd, pfd[2], status; + pid_t pid, rv; + sigaction_t sa; #ifndef NO_ROOT_MAILER static char *root_envp[] = { "HOME=/", "PATH=/usr/bin:/bin", "LOGNAME=root", + "USERNAME=root", "USER=root", NULL }; #endif /* Just return if mailer is disabled. */ - if (!def_str(I_MAILERPATH) || !def_str(I_MAILTO)) + if (!def_mailerpath || !def_mailto) return; - (void) sigemptyset(&set); - (void) sigaddset(&set, SIGCHLD); - (void) sigprocmask(SIG_BLOCK, &set, &oset); + /* Fork and return, child will daemonize. */ + switch (pid = fork()) { + case -1: + /* Error. */ + error(1, "cannot fork"); + break; + case 0: + /* Child. */ + switch (pid = fork()) { + case -1: + /* Error. */ + mysyslog(LOG_ERR, "cannot fork: %m"); + _exit(1); + case 0: + /* Grandchild continues below. */ + break; + default: + /* Parent will wait for us. */ + _exit(0); + } + break; + default: + /* Parent. */ + do { +#ifdef HAVE_WAITPID + rv = waitpid(pid, &status, 0); +#else + rv = wait(&status); +#endif + } while (rv == -1 && errno == EINTR); + return; + } + + /* Daemonize - disassociate from session/tty. */ +#ifdef HAVE_SETSID + if (setsid() == -1) + warning("setsid"); +#else + setpgrp(0, 0); +# ifdef TIOCNOTTY + if ((fd = open(_PATH_TTY, O_RDWR, 0644)) != -1) { + ioctl(fd, TIOCNOTTY, NULL); + close(fd); + } +# endif +#endif + chdir("/"); + if ((fd = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) { + (void) dup2(fd, STDIN_FILENO); + (void) dup2(fd, STDOUT_FILENO); + (void) dup2(fd, STDERR_FILENO); + } + + /* Close password, group and other fds so we don't leak. */ + sudo_endpwent(); + sudo_endgrent(); + closefrom(STDERR_FILENO + 1); + + /* Ignore SIGPIPE in case mailer exits prematurely (or is missing). */ + zero_bytes(&sa, sizeof(sa)); + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = SIG_IGN; + (void) sigaction(SIGPIPE, &sa, NULL); if (pipe(pfd) == -1) { - (void) fprintf(stderr, "%s: cannot open pipe: %s\n", - Argv[0], strerror(errno)); - exit(1); + mysyslog(LOG_ERR, "cannot open pipe: %m"); + _exit(1); } switch (pid = fork()) { case -1: /* Error. */ - (void) fprintf(stderr, "%s: cannot fork: %s\n", - Argv[0], strerror(errno)); - exit(1); + mysyslog(LOG_ERR, "cannot fork: %m"); + _exit(1); break; case 0: { @@ -484,8 +535,8 @@ send_mail(line) (void) close(pfd[1]); /* Build up an argv based the mailer path and flags */ - mflags = estrdup(def_str(I_MAILERFLAGS)); - mpath = estrdup(def_str(I_MAILERPATH)); + mflags = estrdup(def_mailerflags); + mpath = estrdup(def_mailerpath); if ((argv[0] = strrchr(mpath, ' '))) argv[0]++; else @@ -499,20 +550,18 @@ send_mail(line) } argv[i] = NULL; - /* Close password file so we don't leak the fd. */ - endpwent(); - /* * Depending on the config, either run the mailer as root * (so user cannot kill it) or as the user (for the paranoid). */ #ifndef NO_ROOT_MAILER - set_perms(PERM_FULL_ROOT, 0); + set_perms(PERM_ROOT); execve(mpath, argv, root_envp); #else - set_perms(PERM_FULL_USER, 0); + set_perms(PERM_FULL_USER); execv(mpath, argv); #endif /* NO_ROOT_MAILER */ + mysyslog(LOG_ERR, "cannot execute %s: %m", mpath); _exit(127); } break; @@ -521,10 +570,10 @@ send_mail(line) (void) close(pfd[0]); mail = fdopen(pfd[1], "w"); - /* Pipes are all setup, send message via sendmail. */ - (void) fprintf(mail, "To: %s\nFrom: %s\nSubject: ", - def_str(I_MAILTO), user_name); - for (p = def_str(I_MAILSUB); *p; p++) { + /* Pipes are all setup, send message. */ + (void) fprintf(mail, "To: %s\nFrom: %s\nAuto-Submitted: %s\nSubject: ", + def_mailto, def_mailfrom ? def_mailfrom : user_name, "auto-generated"); + for (p = def_mailsub; *p; p++) { /* Expand escapes in the subject */ if (*p == '%' && *(p+1) != '%') { switch (*(++p)) { @@ -544,58 +593,28 @@ send_mail(line) (void) fprintf(mail, "\n\n%s : %s : %s : %s\n\n", user_host, get_timestr(), user_name, line); fclose(mail); - - /* If mailer is done, wait for it now. If not reapchild will get it. */ -#ifdef sudo_waitpid - (void) sudo_waitpid(pid, &status, WNOHANG); + do { +#ifdef HAVE_WAITPID + rv = waitpid(pid, &status, 0); +#else + rv = wait(&status); #endif - (void) sigprocmask(SIG_SETMASK, &oset, NULL); + } while (rv == -1 && errno == EINTR); + _exit(0); } /* - * Send mail based on the value of "status" and compile-time options. + * Determine whether we should send mail based on "status" and defaults options. */ -static void -mail_auth(status, line) +static int +should_mail(status) int status; - char *line; { - int mail_mask; - - /* If any of these bits are set in status, we send mail. */ - if (def_flag(I_MAIL_ALWAYS)) - mail_mask = - VALIDATE_ERROR|VALIDATE_OK|FLAG_NO_USER|FLAG_NO_HOST|VALIDATE_NOT_OK; - else { - mail_mask = VALIDATE_ERROR; - if (def_flag(I_MAIL_NO_USER)) - mail_mask |= FLAG_NO_USER; - if (def_flag(I_MAIL_NO_HOST)) - mail_mask |= FLAG_NO_HOST; - if (def_flag(I_MAIL_NO_PERMS)) - mail_mask |= VALIDATE_NOT_OK; - } - - if ((status & mail_mask) != 0) - send_mail(line); -} -/* - * SIGCHLD sig handler--wait for children as they die. - */ -RETSIGTYPE -reapchild(sig) - int sig; -{ - int status, serrno = errno; - -#ifdef sudo_waitpid - while (sudo_waitpid(-1, &status, WNOHANG) != -1 && errno == EINTR) - ; -#else - (void) wait(&status); -#endif - errno = serrno; + return(def_mail_always || ISSET(status, VALIDATE_ERROR) || + (def_mail_no_user && ISSET(status, FLAG_NO_USER)) || + (def_mail_no_host && ISSET(status, FLAG_NO_HOST)) || + (def_mail_no_perms && !ISSET(status, VALIDATE_OK))); } /* @@ -612,7 +631,7 @@ get_timestr() struct tm *timeptr; timeptr = localtime(&now); - if (def_flag(I_LOG_YEAR)) + if (def_log_year) s = "%h %e %T %Y"; else s = "%h %e %T"; @@ -625,10 +644,119 @@ get_timestr() #endif /* HAVE_STRFTIME */ s = ctime(&now) + 4; /* skip day of the week */ - if (def_flag(I_LOG_YEAR)) + if (def_log_year) s[20] = '\0'; /* avoid the newline */ else s[15] = '\0'; /* don't care about year */ return(s); } + +#define LL_TTY_STR "TTY=" +#define LL_CWD_STR "PWD=" /* XXX - should be CWD= */ +#define LL_USER_STR "USER=" +#define LL_GROUP_STR "GROUP=" +#define LL_ENV_STR "ENV=" +#define LL_CMND_STR "COMMAND=" + +/* + * Allocate and fill in a new logline. + */ +static char * +new_logline(message, serrno) + const char *message; + int serrno; +{ + size_t len = 0; + char *evstr = NULL; + char *errstr = NULL; + char *line; + + /* + * Compute line length + */ + if (message != NULL) + len += strlen(message) + 3; + if (serrno) { + errstr = strerror(serrno); + len += strlen(errstr) + 3; + } + len += sizeof(LL_TTY_STR) + 2 + strlen(user_tty); + len += sizeof(LL_CWD_STR) + 2 + strlen(user_cwd); + if (runas_pw != NULL) + len += sizeof(LL_USER_STR) + 2 + strlen(runas_pw->pw_name); + if (runas_gr != NULL) + len += sizeof(LL_GROUP_STR) + 2 + strlen(runas_gr->gr_name); + if (sudo_user.env_vars != NULL) { + size_t evlen = 0; + struct list_member *cur; + for (cur = sudo_user.env_vars; cur != NULL; cur = cur->next) + evlen += strlen(cur->value) + 1; + evstr = emalloc(evlen); + evstr[0] = '\0'; + for (cur = sudo_user.env_vars; cur != NULL; cur = cur->next) { + strlcat(evstr, cur->value, evlen); + strlcat(evstr, " ", evlen); /* NOTE: last one will fail */ + } + len += sizeof(LL_ENV_STR) + 2 + evlen; + } + len += sizeof(LL_CMND_STR) - 1 + strlen(user_cmnd); + if (user_args != NULL) + len += strlen(user_args) + 1; + + /* + * Allocate and build up the line. + */ + line = emalloc(++len); + line[0] = '\0'; + + if (message != NULL) { + if (strlcat(line, message, len) >= len || + strlcat(line, errstr ? " : " : " ; ", len) >= len) + goto toobig; + } + if (serrno) { + if (strlcat(line, errstr, len) >= len || + strlcat(line, " ; ", len) >= len) + goto toobig; + } + if (strlcat(line, LL_TTY_STR, len) >= len || + strlcat(line, user_tty, len) >= len || + strlcat(line, " ; ", len) >= len) + goto toobig; + if (strlcat(line, LL_CWD_STR, len) >= len || + strlcat(line, user_cwd, len) >= len || + strlcat(line, " ; ", len) >= len) + goto toobig; + if (runas_pw != NULL) { + if (strlcat(line, LL_USER_STR, len) >= len || + strlcat(line, runas_pw->pw_name, len) >= len || + strlcat(line, " ; ", len) >= len) + goto toobig; + } + if (runas_gr != NULL) { + if (strlcat(line, LL_GROUP_STR, len) >= len || + strlcat(line, runas_gr->gr_name, len) >= len || + strlcat(line, " ; ", len) >= len) + goto toobig; + } + if (evstr != NULL) { + if (strlcat(line, LL_ENV_STR, len) >= len || + strlcat(line, evstr, len) >= len || + strlcat(line, " ; ", len) >= len) + goto toobig; + efree(evstr); + } + if (strlcat(line, LL_CMND_STR, len) >= len || + strlcat(line, user_cmnd, len) >= len) + goto toobig; + if (user_args != NULL) { + if (strlcat(line, " ", len) >= len || + strlcat(line, user_args, len) >= len) + goto toobig; + } + + return (line); +toobig: + errorx(1, "internal error: insufficient space for log line"); +}