From 3e5bd8dc52e3eaa2b863da68280e5b0bf8bb5510 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sun, 25 Nov 2012 09:33:33 -0500 Subject: [PATCH] Include setjmp.h in sudoers.h Move error_jmp into plugin_error.c Rename sudoers_plugin_cleanup sudoers_cleanup Make sudoers warning/error functions work when sudo_conv is NULL --- plugins/sudoers/iolog.c | 1 - plugins/sudoers/logging.c | 3 +- plugins/sudoers/plugin_error.c | 97 ++++++++++++++++++++++------------ plugins/sudoers/policy.c | 3 +- plugins/sudoers/sudoers.c | 8 +-- plugins/sudoers/sudoers.h | 6 ++- 6 files changed, 71 insertions(+), 47 deletions(-) diff --git a/plugins/sudoers/iolog.c b/plugins/sudoers/iolog.c index f97ee61e0..40b015257 100644 --- a/plugins/sudoers/iolog.c +++ b/plugins/sudoers/iolog.c @@ -44,7 +44,6 @@ #include #include #include -#include #include #include #ifdef HAVE_ZLIB_H diff --git a/plugins/sudoers/logging.c b/plugins/sudoers/logging.c index c5257dc34..1dd7a429f 100644 --- a/plugins/sudoers/logging.c +++ b/plugins/sudoers/logging.c @@ -57,7 +57,6 @@ #include #include #include -#include #include "sudoers.h" @@ -528,7 +527,7 @@ log_fatal(int flags, const char *fmt, ...) va_end(ap); /* Exit the plugin. */ - sudoers_plugin_cleanup(0); + sudoers_cleanup(0); sudo_debug_exit(__func__, __FILE__, __LINE__, sudo_debug_subsys); siglongjmp(error_jmp, 1); } diff --git a/plugins/sudoers/plugin_error.c b/plugins/sudoers/plugin_error.c index 66871e56b..2152df9f2 100644 --- a/plugins/sudoers/plugin_error.c +++ b/plugins/sudoers/plugin_error.c @@ -39,9 +39,9 @@ #include "gettext.h" static void _warning(int, const char *, va_list); - void sudoers_plugin_cleanup(int); + void sudoers_cleanup(int); -extern sigjmp_buf error_jmp; +sigjmp_buf error_jmp; extern sudo_conv_t sudo_conv; @@ -53,8 +53,11 @@ error2(int eval, const char *fmt, ...) va_start(ap, fmt); _warning(1, fmt, ap); va_end(ap); - sudoers_plugin_cleanup(0); - siglongjmp(error_jmp, eval); + sudoers_cleanup(0); + if (sudo_conv != NULL) + siglongjmp(error_jmp, eval); + else + exit(eval); } void @@ -65,24 +68,33 @@ errorx2(int eval, const char *fmt, ...) va_start(ap, fmt); _warning(0, fmt, ap); va_end(ap); - sudoers_plugin_cleanup(0); - siglongjmp(error_jmp, eval); + sudoers_cleanup(0); + if (sudo_conv != NULL) + siglongjmp(error_jmp, eval); + else + exit(eval); } void verror2(int eval, const char *fmt, va_list ap) { _warning(1, fmt, ap); - sudoers_plugin_cleanup(0); - siglongjmp(error_jmp, eval); + sudoers_cleanup(0); + if (sudo_conv != NULL) + siglongjmp(error_jmp, eval); + else + exit(eval); } void verrorx2(int eval, const char *fmt, va_list ap) { _warning(0, fmt, ap); - sudoers_plugin_cleanup(0); - siglongjmp(error_jmp, eval); + sudoers_cleanup(0); + if (sudo_conv != NULL) + siglongjmp(error_jmp, eval); + else + exit(eval); } void @@ -119,36 +131,51 @@ vwarningx2(const char *fmt, va_list ap) static void _warning(int use_errno, const char *fmt, va_list ap) { - struct sudo_conv_message msg[6]; - struct sudo_conv_reply repl[6]; - char *str; - int oldlocale, nmsgs = 4; + int oldlocale, serrno = errno; /* Warnings are displayed in the user's locale. */ sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale); - evasprintf(&str, _(fmt), ap); - - /* Call conversation function */ - memset(&msg, 0, sizeof(msg)); - msg[0].msg_type = SUDO_CONV_ERROR_MSG; - msg[0].msg = getprogname(); - msg[1].msg_type = SUDO_CONV_ERROR_MSG; - msg[1].msg = _(": "); - msg[2].msg_type = SUDO_CONV_ERROR_MSG; - msg[2].msg = str; - if (use_errno) { - msg[3].msg_type = SUDO_CONV_ERROR_MSG; - msg[3].msg = _(": "); - msg[4].msg_type = SUDO_CONV_ERROR_MSG; - msg[4].msg = strerror(errno); - nmsgs = 6; + if (sudo_conv != NULL) { + struct sudo_conv_message msg[6]; + struct sudo_conv_reply repl[6]; + int nmsgs = 4; + char *str; + + evasprintf(&str, _(fmt), ap); + + /* Call conversation function */ + memset(&msg, 0, sizeof(msg)); + msg[0].msg_type = SUDO_CONV_ERROR_MSG; + msg[0].msg = getprogname(); + msg[1].msg_type = SUDO_CONV_ERROR_MSG; + msg[1].msg = _(": "); + msg[2].msg_type = SUDO_CONV_ERROR_MSG; + msg[2].msg = str; + if (use_errno) { + msg[3].msg_type = SUDO_CONV_ERROR_MSG; + msg[3].msg = _(": "); + msg[4].msg_type = SUDO_CONV_ERROR_MSG; + msg[4].msg = strerror(errno); + nmsgs = 6; + } + msg[nmsgs - 1].msg_type = SUDO_CONV_ERROR_MSG; + msg[nmsgs - 1].msg = "\n"; + memset(&repl, 0, sizeof(repl)); + sudo_conv(nmsgs, msg, repl); + efree(str); + } else { + fputs(getprogname(), stderr); + if (fmt != NULL) { + fputs(_(": "), stderr); + vfprintf(stderr, _(fmt), ap); + } + if (use_errno) { + fputs(_(": "), stderr); + fputs(strerror(serrno), stderr); + } + putc('\n', stderr); } - msg[nmsgs - 1].msg_type = SUDO_CONV_ERROR_MSG; - msg[nmsgs - 1].msg = "\n"; - memset(&repl, 0, sizeof(repl)); - sudo_conv(nmsgs, msg, repl); - efree(str); sudoers_setlocale(oldlocale, NULL); } diff --git a/plugins/sudoers/policy.c b/plugins/sudoers/policy.c index 3f6684cf0..084028add 100644 --- a/plugins/sudoers/policy.c +++ b/plugins/sudoers/policy.c @@ -43,7 +43,6 @@ #include #include #include -#include #include "sudoers.h" #include "sudoers_version.h" @@ -581,7 +580,7 @@ sudoers_policy_invalidate(int remove) user_cmnd = "kill"; if (sigsetjmp(error_jmp, 1) == 0) { remove_timestamp(remove); - sudoers_plugin_cleanup(0); + sudoers_cleanup(0); } debug_return; diff --git a/plugins/sudoers/sudoers.c b/plugins/sudoers/sudoers.c index f03e73adb..314fc05eb 100644 --- a/plugins/sudoers/sudoers.c +++ b/plugins/sudoers/sudoers.c @@ -71,7 +71,6 @@ # include #endif #include -#include #ifndef HAVE_GETADDRINFO # include "compat/getaddrinfo.h" #endif @@ -120,9 +119,6 @@ static struct sudo_nss_list *snl; int NewArgc; char **NewArgv; -/* Declared here instead of plugin_error.c for static sudo builds. */ -sigjmp_buf error_jmp; - int sudoers_policy_init(void *info, char * const envp[]) { @@ -905,12 +901,12 @@ cb_runas_default(const char *user) * Cleanup hook for error()/errorx() */ void -sudoers_plugin_cleanup(int gotsignal) +sudoers_cleanup(int gotsignal) { struct sudo_nss *nss; if (!gotsignal) { - debug_decl(sudoers_plugin_cleanup, SUDO_DEBUG_PLUGIN) + debug_decl(sudoers_cleanup, SUDO_DEBUG_PLUGIN) if (snl != NULL) { tq_foreach_fwd(snl, nss) nss->close(nss); diff --git a/plugins/sudoers/sudoers.h b/plugins/sudoers/sudoers.h index 7a81fff40..a6572d573 100644 --- a/plugins/sudoers/sudoers.h +++ b/plugins/sudoers/sudoers.h @@ -23,6 +23,7 @@ #define _SUDO_SUDOERS_H #include +#include #ifdef HAVE_STDBOOL_H # include #else @@ -352,7 +353,7 @@ char *fmt_string(const char *, const char *); FILE *open_sudoers(const char *, bool, bool *); int sudoers_policy_init(void *info, char * const envp[]); int sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[], void *closure); -void sudoers_plugin_cleanup(int); +void sudoers_cleanup(int); /* policy.c */ int sudoers_policy_deserialize_info(void *v, char **runas_user, char **runas_group); @@ -371,6 +372,9 @@ int group_plugin_query(const char *user, const char *group, /* setgroups.c */ int sudo_setgroups(int ngids, const GETGROUPS_T *gids); +/* plugin_error.c */ +extern sigjmp_buf error_jmp; + #ifndef _SUDO_MAIN extern struct sudo_user sudo_user; extern struct passwd *list_pw; -- 2.40.0