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.
This commit is contained in:
Todd C. Miller
2014-07-08 09:52:21 -06:00
parent 2bad717548
commit 12c3b456d8
5 changed files with 41 additions and 13 deletions

View File

@@ -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__));

View File

@@ -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;
}
/*

View File

@@ -148,4 +148,5 @@ sudo_vwarn_nodebug
sudo_vwarnx_nodebug
sudo_warn_gettext
sudo_warn_nodebug
sudo_warn_strerror
sudo_warnx_nodebug

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2013 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 2012-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* 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;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 2013-2014 Todd C. Miller <Todd.Miller@courtesan.com>
*
* 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 <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#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);
}