2 * Copyright (c) 1994-1996, 1998-2010 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.
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.
27 #include <sys/types.h>
28 #include <sys/param.h>
30 #include <sys/ioctl.h>
40 #endif /* STDC_HEADERS */
43 #endif /* HAVE_STRING_H */
46 #endif /* HAVE_STRINGS_H */
49 #endif /* HAVE_UNISTD_H */
59 static void do_syslog __P((int, char *));
60 static void do_logfile __P((char *));
61 static void send_mail __P((const char *fmt, ...));
62 static int should_mail __P((int));
63 static void mysyslog __P((int, const char *, ...));
64 static char *new_logline __P((const char *, int));
66 #define MAXSYSLOGTRIES 16 /* num of retries for broken syslogs */
69 * We do an openlog(3)/closelog(3) for each message because some
70 * authentication methods (notably PAM) use syslog(3) for their
71 * own nefarious purposes and may call openlog(3) and closelog(3).
72 * Note that because we don't want to assume that all systems have
73 * vsyslog(3) (HP-UX doesn't) "%m" will not be expanded.
74 * Sadly this is a maze of #ifdefs.
78 mysyslog(int pri, const char *fmt, ...)
80 mysyslog(pri, fmt, va_alist)
89 char buf[MAXSYSLOGLEN+1];
97 #ifdef LOG_NFACILITIES
98 openlog("sudo", 0, def_syslog);
102 vsnprintf(buf, sizeof(buf), fmt, ap);
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,
109 for (i = 0; i < MAXSYSLOGTRIES; i++)
110 if (syslog(pri, "%s", buf) == 0)
113 syslog(pri, "%s", buf);
114 #endif /* BROKEN_SYSLOG */
119 #define FMT_FIRST "%8s : %s"
120 #define FMT_CONTD "%8s : (command continued) %s"
123 * Log a message to syslog, pre-pending the username and splitting the
124 * message into parts if it is longer than MAXSYSLOGLEN.
136 * Log the full line, breaking into multiple syslog(3) calls if necessary
139 maxlen = MAXSYSLOGLEN - (sizeof(FMT_FIRST) - 6 + strlen(user_name));
140 for (p = msg; *p != '\0'; ) {
144 * Break up the line into what will fit on one syslog(3) line
145 * Try to avoid breaking words into several lines if possible.
147 tmp = memrchr(p, ' ', maxlen);
151 /* NULL terminate line, but save the char to restore later */
155 mysyslog(pri, fmt, user_name, p);
157 *tmp = save; /* restore saved character */
159 /* Advance p and eliminate leading whitespace */
160 for (p = tmp; *p == ' '; p++)
163 mysyslog(pri, fmt, user_name, p);
167 maxlen = MAXSYSLOGLEN - (sizeof(FMT_CONTD) - 6 + strlen(user_name));
176 char *beg, *oldend, *end;
181 oldmask = umask(077);
182 maxlen = def_loglinelen > 0 ? def_loglinelen : 0;
183 fp = fopen(def_logfile, "a");
184 (void) umask(oldmask);
186 send_mail("Can't open log file: %s: %s", def_logfile, strerror(errno));
187 } else if (!lock_file(fileno(fp), SUDO_LOCK)) {
188 send_mail("Can't lock log file: %s: %s", def_logfile, strerror(errno));
193 if (def_loglinelen == 0) {
194 /* Don't pretty-print long log file lines (hard to grep) */
196 (void) fprintf(fp, "%s : %s : HOST=%s : %s\n",
197 get_timestr(now, def_log_year), user_name, user_shost, msg);
199 (void) fprintf(fp, "%s : %s : %s\n",
200 get_timestr(now, def_log_year), user_name, msg);
203 easprintf(&full_line, "%s : %s : HOST=%s : %s",
204 get_timestr(now, def_log_year), user_name, user_shost, msg);
206 easprintf(&full_line, "%s : %s : %s",
207 get_timestr(now, def_log_year), user_name, msg);
210 * Print out full_line with word wrap
212 beg = end = full_line;
215 end = strchr(oldend, ' ');
217 if (maxlen > 0 && end) {
219 if (strlen(beg) > maxlen) {
220 /* too far, need to back up & print the line */
222 if (beg == (char *)full_line)
223 maxlen -= 4; /* don't indent first line */
232 (void) fprintf(fp, "%s\n ", beg);
235 (void) fprintf(fp, "%s\n ", beg);
238 /* reset beg to point to the start of the new substr */
243 /* we still have room */
247 /* remove leading whitespace */
252 (void) fprintf(fp, "%s\n", beg);
253 beg = NULL; /* exit condition */
259 (void) lock_file(fileno(fp), SUDO_UNLOCK);
265 * Log and mail the denial message, optionally informing the user.
268 log_denial(status, inform_user)
275 /* Set error message. */
276 if (ISSET(status, FLAG_NO_USER))
277 message = "user NOT in sudoers";
278 else if (ISSET(status, FLAG_NO_HOST))
279 message = "user NOT authorized on host";
281 message = "command not allowed";
283 logline = new_logline(message, 0);
285 if (should_mail(status))
286 send_mail("%s", logline); /* send mail based on status */
288 /* Inform the user if they failed to authenticate. */
290 if (ISSET(status, FLAG_NO_USER))
291 (void) fprintf(stderr, "%s is not in the sudoers file. %s",
292 user_name, "This incident will be reported.\n");
293 else if (ISSET(status, FLAG_NO_HOST))
294 (void) fprintf(stderr, "%s is not allowed to run sudo on %s. %s",
295 user_name, user_shost, "This incident will be reported.\n");
296 else if (ISSET(status, FLAG_NO_CHECK))
297 (void) fprintf(stderr, "Sorry, user %s may not run sudo on %s.\n",
298 user_name, user_shost);
300 (void) fprintf(stderr,
301 "Sorry, user %s is not allowed to execute '%s%s%s' as %s%s%s on %s.\n",
302 user_name, user_cmnd, user_args ? " " : "",
303 user_args ? user_args : "",
304 list_pw ? list_pw->pw_name : runas_pw ?
305 runas_pw->pw_name : user_name, runas_gr ? ":" : "",
306 runas_gr ? runas_gr->gr_name : "", user_host);
310 * Log via syslog and/or a file.
313 do_syslog(def_syslog_badpri, logline);
321 * Log and potentially mail the allowed command.
329 logline = new_logline(NULL, 0);
331 if (should_mail(status))
332 send_mail("%s", logline); /* send mail based on status */
335 * Log via syslog and/or a file.
338 do_syslog(def_syslog_goodpri, logline);
347 log_error(int flags, const char *fmt, ...)
349 log_error(flags, fmt, va_alist)
365 /* Become root if we are not already to avoid user interference */
366 set_perms(PERM_ROOT|PERM_NOEXIT);
368 /* Expand printf-style format + args. */
369 evasprintf(&message, fmt, ap);
372 if (ISSET(flags, MSG_ONLY))
375 logline = new_logline(message, ISSET(flags, USE_ERRNO) ? serrno : 0);
380 if (!ISSET(flags, NO_STDERR)) {
381 if (ISSET(flags, USE_ERRNO))
382 warning("%s", message);
384 warningx("%s", message);
386 if (logline != message)
390 * Send a copy of the error via mail.
392 if (!ISSET(flags, NO_MAIL))
393 send_mail("%s", logline);
396 * Log to syslog and/or a file.
399 do_syslog(def_syslog_badpri, logline);
405 if (!ISSET(flags, NO_EXIT)) {
411 #define MAX_MAILFLAGS 63
414 * Send a message to MAILTO user
418 send_mail(const char *fmt, ...)
420 send_mail(fmt, va_alist)
427 int fd, pfd[2], status;
431 #ifndef NO_ROOT_MAILER
432 static char *root_envp[] = {
434 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
442 /* Just return if mailer is disabled. */
443 if (!def_mailerpath || !def_mailto)
446 /* Fork and return, child will daemonize. */
447 switch (pid = fork()) {
450 error(1, "cannot fork");
454 switch (pid = fork()) {
457 mysyslog(LOG_ERR, "cannot fork: %m");
460 /* Grandchild continues below. */
463 /* Parent will wait for us. */
471 rv = waitpid(pid, &status, 0);
475 } while (rv == -1 && errno == EINTR);
479 /* Daemonize - disassociate from session/tty. */
483 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) {
484 (void) dup2(fd, STDIN_FILENO);
485 (void) dup2(fd, STDOUT_FILENO);
486 (void) dup2(fd, STDERR_FILENO);
489 /* Close password, group and other fds so we don't leak. */
492 closefrom(STDERR_FILENO + 1);
494 /* Ignore SIGPIPE in case mailer exits prematurely (or is missing). */
495 zero_bytes(&sa, sizeof(sa));
496 sigemptyset(&sa.sa_mask);
497 sa.sa_flags = SA_INTERRUPT;
498 sa.sa_handler = SIG_IGN;
499 (void) sigaction(SIGPIPE, &sa, NULL);
501 if (pipe(pfd) == -1) {
502 mysyslog(LOG_ERR, "cannot open pipe: %m");
506 switch (pid = fork()) {
509 mysyslog(LOG_ERR, "cannot fork: %m");
514 char *argv[MAX_MAILFLAGS + 1];
515 char *mpath, *mflags;
518 /* Child, set stdin to output side of the pipe */
519 if (pfd[0] != STDIN_FILENO) {
520 if (dup2(pfd[0], STDIN_FILENO) == -1) {
521 mysyslog(LOG_ERR, "cannot dup stdin: %m");
524 (void) close(pfd[0]);
526 (void) close(pfd[1]);
528 /* Build up an argv based on the mailer path and flags */
529 mflags = estrdup(def_mailerflags);
530 mpath = estrdup(def_mailerpath);
531 if ((argv[0] = strrchr(mpath, ' ')))
537 if ((p = strtok(mflags, " \t"))) {
540 } while (++i < MAX_MAILFLAGS && (p = strtok(NULL, " \t")));
545 * Depending on the config, either run the mailer as root
546 * (so user cannot kill it) or as the user (for the paranoid).
548 #ifndef NO_ROOT_MAILER
549 set_perms(PERM_ROOT|PERM_NOEXIT);
550 execve(mpath, argv, root_envp);
552 set_perms(PERM_FULL_USER|PERM_NOEXIT);
554 #endif /* NO_ROOT_MAILER */
555 mysyslog(LOG_ERR, "cannot execute %s: %m", mpath);
561 (void) close(pfd[0]);
562 mail = fdopen(pfd[1], "w");
564 /* Pipes are all setup, send message. */
565 (void) fprintf(mail, "To: %s\nFrom: %s\nAuto-Submitted: %s\nSubject: ",
566 def_mailto, def_mailfrom ? def_mailfrom : user_name, "auto-generated");
567 for (p = def_mailsub; *p; p++) {
568 /* Expand escapes in the subject */
569 if (*p == '%' && *(p+1) != '%') {
572 (void) fputs(user_host, mail);
575 (void) fputs(user_name, mail);
582 (void) fputc(*p, mail);
585 (void) fprintf(mail, "\n\n%s : %s : %s : ", user_host,
586 get_timestr(time(NULL), def_log_year), user_name);
592 (void) vfprintf(mail, fmt, ap);
599 rv = waitpid(pid, &status, 0);
603 } while (rv == -1 && errno == EINTR);
608 * Determine whether we should send mail based on "status" and defaults options.
615 return(def_mail_always || ISSET(status, VALIDATE_ERROR) ||
616 (def_mail_no_user && ISSET(status, FLAG_NO_USER)) ||
617 (def_mail_no_host && ISSET(status, FLAG_NO_HOST)) ||
618 (def_mail_no_perms && !ISSET(status, VALIDATE_OK)));
621 #define LL_TTY_STR "TTY="
622 #define LL_CWD_STR "PWD=" /* XXX - should be CWD= */
623 #define LL_USER_STR "USER="
624 #define LL_GROUP_STR "GROUP="
625 #define LL_ENV_STR "ENV="
626 #define LL_CMND_STR "COMMAND="
627 #define LL_TSID_STR "TSID="
630 * Allocate and fill in a new logline.
633 new_logline(message, serrno)
643 * Compute line length
646 len += strlen(message) + 3;
648 errstr = strerror(serrno);
649 len += strlen(errstr) + 3;
651 len += sizeof(LL_TTY_STR) + 2 + strlen(user_tty);
652 len += sizeof(LL_CWD_STR) + 2 + strlen(user_cwd);
653 if (runas_pw != NULL)
654 len += sizeof(LL_USER_STR) + 2 + strlen(runas_pw->pw_name);
655 if (runas_gr != NULL)
656 len += sizeof(LL_GROUP_STR) + 2 + strlen(runas_gr->gr_name);
657 if (sudo_user.sessid[0] != '\0')
658 len += sizeof(LL_TSID_STR) + 2 + strlen(sudo_user.sessid);
659 if (sudo_user.env_vars != NULL) {
661 struct list_member *cur;
662 for (cur = sudo_user.env_vars; cur != NULL; cur = cur->next)
663 evlen += strlen(cur->value) + 1;
664 evstr = emalloc(evlen);
666 for (cur = sudo_user.env_vars; cur != NULL; cur = cur->next) {
667 strlcat(evstr, cur->value, evlen);
668 strlcat(evstr, " ", evlen); /* NOTE: last one will fail */
670 len += sizeof(LL_ENV_STR) + 2 + evlen;
672 len += sizeof(LL_CMND_STR) - 1 + strlen(user_cmnd);
673 if (user_args != NULL)
674 len += strlen(user_args) + 1;
677 * Allocate and build up the line.
679 line = emalloc(++len);
682 if (message != NULL) {
683 if (strlcat(line, message, len) >= len ||
684 strlcat(line, errstr ? " : " : " ; ", len) >= len)
688 if (strlcat(line, errstr, len) >= len ||
689 strlcat(line, " ; ", len) >= len)
692 if (strlcat(line, LL_TTY_STR, len) >= len ||
693 strlcat(line, user_tty, len) >= len ||
694 strlcat(line, " ; ", len) >= len)
696 if (strlcat(line, LL_CWD_STR, len) >= len ||
697 strlcat(line, user_cwd, len) >= len ||
698 strlcat(line, " ; ", len) >= len)
700 if (runas_pw != NULL) {
701 if (strlcat(line, LL_USER_STR, len) >= len ||
702 strlcat(line, runas_pw->pw_name, len) >= len ||
703 strlcat(line, " ; ", len) >= len)
706 if (runas_gr != NULL) {
707 if (strlcat(line, LL_GROUP_STR, len) >= len ||
708 strlcat(line, runas_gr->gr_name, len) >= len ||
709 strlcat(line, " ; ", len) >= len)
712 if (sudo_user.sessid[0] != '\0') {
713 if (strlcat(line, LL_TSID_STR, len) >= len ||
714 strlcat(line, sudo_user.sessid, len) >= len ||
715 strlcat(line, " ; ", len) >= len)
719 if (strlcat(line, LL_ENV_STR, len) >= len ||
720 strlcat(line, evstr, len) >= len ||
721 strlcat(line, " ; ", len) >= len)
725 if (strlcat(line, LL_CMND_STR, len) >= len ||
726 strlcat(line, user_cmnd, len) >= len)
728 if (user_args != NULL) {
729 if (strlcat(line, " ", len) >= len ||
730 strlcat(line, user_args, len) >= len)
736 errorx(1, "internal error: insufficient space for log line");