From 12c3b456d868b136fcfbeda6fbc82300ab2486a9 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 8 Jul 2014 09:52:21 -0600 Subject: [PATCH] Add sudo_warn_strerror() that wraps strerror() with calls to setlocale() in sudoers so we always get the error string in the user's locale. Also change _warning() to take the error number as a parameter instead of examining errno. --- include/fatal.h | 1 + lib/util/fatal.c | 22 +++++++++++----------- lib/util/util.exp | 1 + plugins/sudoers/locale.c | 15 ++++++++++++++- src/locale_stub.c | 15 ++++++++++++++- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/fatal.h b/include/fatal.h index 66207c6dc..865ae94b6 100644 --- a/include/fatal.h +++ b/include/fatal.h @@ -117,6 +117,7 @@ extern int (*sudo_printf)(int msg_type, const char *fmt, ...); __dso_public int sudo_fatal_callback_deregister(void (*func)(void)); __dso_public int sudo_fatal_callback_register(void (*func)(void)); __dso_public char *sudo_warn_gettext(const char *msgid) __format_arg(1); +__dso_public char *sudo_warn_strerror(int errnum); __dso_public void sudo_fatal_nodebug(const char *, ...) __printf0like(1, 2) __attribute__((__noreturn__)); __dso_public void sudo_fatalx_nodebug(const char *, ...) __printflike(1, 2) __attribute__((__noreturn__)); __dso_public void sudo_vfatal_nodebug(const char *, va_list ap) __printf0like(1, 0) __attribute__((__noreturn__)); diff --git a/lib/util/fatal.c b/lib/util/fatal.c index f2cbb37ec..fcfd81d7a 100644 --- a/lib/util/fatal.c +++ b/lib/util/fatal.c @@ -45,7 +45,7 @@ SLIST_HEAD(sudo_fatal_callback_list, sudo_fatal_callback); static struct sudo_fatal_callback_list callbacks; -static void _warning(int, const char *, va_list); +static void _warning(int errnum, const char *fmt, va_list ap); static void do_cleanup(void) @@ -66,7 +66,7 @@ sudo_fatal_nodebug(const char *fmt, ...) va_list ap; va_start(ap, fmt); - _warning(1, fmt, ap); + _warning(errno, fmt, ap); va_end(ap); do_cleanup(); exit(EXIT_FAILURE); @@ -87,7 +87,7 @@ sudo_fatalx_nodebug(const char *fmt, ...) void sudo_vfatal_nodebug(const char *fmt, va_list ap) { - _warning(1, fmt, ap); + _warning(errno, fmt, ap); do_cleanup(); exit(EXIT_FAILURE); } @@ -106,7 +106,7 @@ sudo_warn_nodebug(const char *fmt, ...) va_list ap; va_start(ap, fmt); - _warning(1, fmt, ap); + _warning(errno, fmt, ap); va_end(ap); } @@ -122,7 +122,7 @@ sudo_warnx_nodebug(const char *fmt, ...) void sudo_vwarn_nodebug(const char *fmt, va_list ap) { - _warning(1, fmt, ap); + _warning(errno, fmt, ap); } void @@ -132,26 +132,26 @@ sudo_vwarnx_nodebug(const char *fmt, va_list ap) } static void -_warning(int use_errno, const char *fmt, va_list ap) +_warning(int errnum, const char *fmt, va_list ap) { - int serrno = errno; char *str; sudo_evasprintf(&str, fmt, ap); - if (use_errno) { + if (errnum) { if (fmt != NULL) { sudo_printf(SUDO_CONV_ERROR_MSG, - _("%s: %s: %s\n"), getprogname(), str, strerror(serrno)); + _("%s: %s: %s\n"), getprogname(), str, + sudo_warn_strerror(errnum)); } else { sudo_printf(SUDO_CONV_ERROR_MSG, - _("%s: %s\n"), getprogname(), strerror(serrno)); + _("%s: %s\n"), getprogname(), + sudo_warn_strerror(errnum)); } } else { sudo_printf(SUDO_CONV_ERROR_MSG, _("%s: %s\n"), getprogname(), str ? str : "(null)"); } efree(str); - errno = serrno; } /* diff --git a/lib/util/util.exp b/lib/util/util.exp index 4bb300f4c..646fcf424 100644 --- a/lib/util/util.exp +++ b/lib/util/util.exp @@ -148,4 +148,5 @@ sudo_vwarn_nodebug sudo_vwarnx_nodebug sudo_warn_gettext sudo_warn_nodebug +sudo_warn_strerror sudo_warnx_nodebug diff --git a/plugins/sudoers/locale.c b/plugins/sudoers/locale.c index 22ef35f1b..538a407f8 100644 --- a/plugins/sudoers/locale.c +++ b/plugins/sudoers/locale.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2013 Todd C. Miller + * Copyright (c) 2012-2014 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -124,3 +124,16 @@ sudo_warn_gettext(const char *msgid) return msg; } #endif /* HAVE_LIBINTL_H */ + +char * +sudo_warn_strerror(int errnum) +{ + int warning_locale; + char *errmsg; + + sudoers_setlocale(SUDOERS_LOCALE_USER, &warning_locale); + errmsg = strerror(errnum); + sudoers_setlocale(warning_locale, NULL); + + return errmsg; +} diff --git a/src/locale_stub.c b/src/locale_stub.c index eaa94d563..bb9783c86 100644 --- a/src/locale_stub.c +++ b/src/locale_stub.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Todd C. Miller + * Copyright (c) 2013-2014 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -20,6 +20,12 @@ #include #include +#ifdef HAVE_STRING_H +# include +#endif /* HAVE_STRING_H */ +#ifdef HAVE_STRINGS_H +# include +#endif /* HAVE_STRINGS_H */ #define DEFAULT_TEXT_DOMAIN "sudo" #include "gettext.h" /* must be included before missing.h */ @@ -35,3 +41,10 @@ sudo_warn_gettext(const char *msgid) return gettext(msgid); } #endif /* HAVE_LIBINTL_H */ + +/* No need to swap locales in the front end. */ +char * +sudo_warn_strerror(int errnum) +{ + return strerror(errnum); +} -- 2.40.0