]> granicus.if.org Git - sudo/commitdiff
Add sudo_warn_strerror() that wraps strerror() with calls to
authorTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 8 Jul 2014 15:52:21 +0000 (09:52 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 8 Jul 2014 15:52:21 +0000 (09:52 -0600)
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
lib/util/fatal.c
lib/util/util.exp
plugins/sudoers/locale.c
src/locale_stub.c

index 66207c6dc3fa8df7b653aa97d808886cc32f6e06..865ae94b601431e024ffcfcc34e01f345c9d5a20 100644 (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__));
index f2cbb37eccbe396d61102487f29342be62e76096..fcfd81d7a66eac3bcdeee3f07cf56d806a859971 100644 (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;
 }
 
 /*
index 4bb300f4c979c852c88e5ec0284694e2a6fadd0c..646fcf424f0f8730df453b1fc86d29f20d9822de 100644 (file)
@@ -148,4 +148,5 @@ sudo_vwarn_nodebug
 sudo_vwarnx_nodebug
 sudo_warn_gettext
 sudo_warn_nodebug
+sudo_warn_strerror
 sudo_warnx_nodebug
index 22ef35f1b78f16ff3066487ebe791f5a79a4c253..538a407f849c05eea10ef08b9b32f0a9413aba55 100644 (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;
+}
index eaa94d56393a3549f9410000522761a312aecf9b..bb9783c8607c4a2302f9a59b345a80badef09854 100644 (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
 
 #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);
+}