From 755a81e946e7ce805d4dfc9bf308183d387e26bc Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sun, 10 May 2009 11:52:13 +0000 Subject: [PATCH] Add option for set_perm to not exit on failure and use this in the logging routines. --- logging.c | 6 +++--- set_perms.c | 60 +++++++++++++++++++++++++++++++++++++-------------- sudo.h | 4 +++- testsudoers.c | 4 ++-- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/logging.c b/logging.c index 2dc0650df..0cecd7cdf 100644 --- a/logging.c +++ b/logging.c @@ -371,7 +371,7 @@ log_error(flags, fmt, va_alist) #endif /* Become root if we are not already to avoid user interference */ - set_perms(PERM_ROOT); + set_perms(PERM_ROOT|PERM_NOEXIT); /* Expand printf-style format + args. */ evasprintf(&message, fmt, ap); @@ -555,10 +555,10 @@ send_mail(line) * (so user cannot kill it) or as the user (for the paranoid). */ #ifndef NO_ROOT_MAILER - set_perms(PERM_ROOT); + set_perms(PERM_ROOT|PERM_NOEXIT); execve(mpath, argv, root_envp); #else - set_perms(PERM_FULL_USER); + set_perms(PERM_FULL_USER|PERM_NOEXIT); execv(mpath, argv); #endif /* NO_ROOT_MAILER */ mysyslog(LOG_ERR, "cannot execute %s: %m", mpath); diff --git a/set_perms.c b/set_perms.c index c9cecb77d..c61256611 100644 --- a/set_perms.c +++ b/set_perms.c @@ -77,14 +77,18 @@ static int current_perm = -1; * We only flip the effective gid since it only changes for PERM_SUDOERS. * This version of set_perms() works fine with the "stay_setuid" option. */ -void +int set_perms(perm) int perm; { const char *errstr; + int noexit; + + noexit = ISSET(perm, PERM_NOEXIT); + CLR(perm, PERM_MASK); if (perm == current_perm) - return; + return(1); switch (perm) { case PERM_ROOT: @@ -169,10 +173,13 @@ set_perms(perm) } current_perm = perm; - return; + return(1); bad: - errorx(1, "%s: %s", errstr, + warningx("%s: %s", errstr, errno == EAGAIN ? "too many processes" : strerror(errno)); + if (noexit) + return(0); + exit(1); } #else @@ -184,14 +191,18 @@ bad: * we are headed for an exec(). * This version of set_perms() works fine with the "stay_setuid" option. */ -void +int set_perms(perm) int perm; { const char *errstr; + int noexit; + + noexit = ISSET(perm, PERM_NOEXIT); + CLR(perm, PERM_MASK); if (perm == current_perm) - return; + return(1); switch (perm) { case PERM_ROOT: @@ -279,10 +290,13 @@ set_perms(perm) } current_perm = perm; - return; + return(1); bad: - errorx(1, "%s: %s", errstr, + warningx("%s: %s", errstr, errno == EAGAIN ? "too many processes" : strerror(errno)); + if (noexit) + return(0); + exit(1); } # else /* !HAVE_SETRESUID && !HAVE_SETREUID */ @@ -292,14 +306,18 @@ bad: * Set real and effective uids and gids based on perm. * NOTE: does not support the "stay_setuid" option. */ -void +int set_perms(perm) int perm; { const char *errstr; + int noexit; + + noexit = ISSET(perm, PERM_NOEXIT); + CLR(perm, PERM_MASK); if (perm == current_perm) - return; + return(1); /* * Since we only have setuid() and seteuid() and semantics @@ -391,10 +409,13 @@ set_perms(perm) } current_perm = perm; - return; + return(1); bad: - errorx(1, "%s: %s", errstr, + warningx("%s: %s", errstr, errno == EAGAIN ? "too many processes" : strerror(errno)); + if (noexit) + return(0); + exit(1); } # else /* !HAVE_SETRESUID && !HAVE_SETREUID && !HAVE_SETEUID */ @@ -404,14 +425,18 @@ bad: * NOTE: does not support the "stay_setuid" or timestampowner options. * Also, SUDOERS_UID and SUDOERS_GID are not used. */ -void +int set_perms(perm) int perm; { const char *errstr; + int noexit; + + noexit = ISSET(perm, PERM_NOEXIT); + CLR(perm, PERM_MASK); if (perm == current_perm) - return; + return(1); switch (perm) { case PERM_ROOT: @@ -448,10 +473,13 @@ set_perms(perm) } current_perm = perm; - return; + return(1); bad: - errorx(1, "%s: %s", errstr, + warningx("%s: %s", errstr, errno == EAGAIN ? "too many processes" : strerror(errno)); + if (noexit) + return(0); + exit(1); } # endif /* HAVE_SETEUID */ # endif /* HAVE_SETREUID */ diff --git a/sudo.h b/sudo.h index 77ed89eef..1a7fcf464 100644 --- a/sudo.h +++ b/sudo.h @@ -129,6 +129,8 @@ struct sudo_user { #define PERM_RUNAS 0x04 #define PERM_FULL_RUNAS 0x05 #define PERM_TIMESTAMP 0x06 +#define PERM_NOEXIT 0x10 /* flag */ +#define PERM_MASK 0xf0 /* * Shortcuts for sudo_user contents. @@ -269,7 +271,7 @@ int sudo_file_display_cmnd __P((struct sudo_nss *, struct passwd *)); int sudo_file_display_defaults __P((struct sudo_nss *, struct passwd *, struct lbuf *)); int sudo_file_display_bound_defaults __P((struct sudo_nss *, struct passwd *, struct lbuf *)); int sudo_file_display_privs __P((struct sudo_nss *, struct passwd *, struct lbuf *)); -void set_perms __P((int)); +int set_perms __P((int)); void remove_timestamp __P((int)); int check_secureware __P((char *)); void sia_attempt_auth __P((void)); diff --git a/testsudoers.c b/testsudoers.c index ebe930f41..95a1389e3 100644 --- a/testsudoers.c +++ b/testsudoers.c @@ -387,11 +387,11 @@ init_envtables() return; } -void +int set_perms(perm) int perm; { - return; + return(1); } void -- 2.40.0