]> granicus.if.org Git - sudo/commitdiff
Convert setlocale() to sudoers_setlocale() in the sudoers module.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 8 Nov 2012 20:37:43 +0000 (15:37 -0500)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 8 Nov 2012 20:37:43 +0000 (15:37 -0500)
This only converts existing uses, there are more places where we
need to sprinkle sudoers_setlocale() calls.

plugins/sudoers/iolog_path.c
plugins/sudoers/logging.c
plugins/sudoers/sudoers.c

index fec178142cee15b674cf2de46d6fdb9f516ccd76..1f615f3d76411a68cb089a02d30ceab5fbb778d1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2011-2012 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
@@ -165,7 +165,7 @@ expand_iolog_path(const char *prefix, const char *dir, const char *file,
     char *slash = NULL;
     const char *endbrace, *src = dir;
     static struct path_escape *escapes;
-    int pass;
+    int pass, oldlocale;
     bool strfit;
     debug_decl(expand_iolog_path, SUDO_DEBUG_UTIL)
 
@@ -254,20 +254,16 @@ expand_iolog_path(const char *prefix, const char *dir, const char *file,
            time(&now);
            timeptr = localtime(&now);
 
-#ifdef HAVE_SETLOCALE
-           if (!setlocale(LC_ALL, def_sudoers_locale)) {
-               warningx(_("unable to set locale to \"%s\", using \"C\""),
-                   def_sudoers_locale);
-               setlocale(LC_ALL, "C");
-           }
-#endif
+           /* Use sudoers locale for strftime() */
+           sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
+
            /* We only calls strftime() on the current part of the buffer. */
            tmpbuf[sizeof(tmpbuf) - 1] = '\0';
            len = strftime(tmpbuf, sizeof(tmpbuf), dst0, timeptr);
 
-#ifdef HAVE_SETLOCALE
-           setlocale(LC_ALL, "");
-#endif
+           /* Restore old locale. */
+           sudoers_setlocale(oldlocale, NULL);
+
            if (len == 0 || tmpbuf[sizeof(tmpbuf) - 1] != '\0')
                goto bad;               /* strftime() failed, buf too small? */
 
index f316a18aa17adf85cc99ec41e0c3d59db8af5a1c..c9e6dd58b4829a374267047492d44e54c3c949a3 100644 (file)
@@ -47,9 +47,6 @@
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
-#ifdef HAVE_SETLOCALE
-# include <locale.h>
-#endif /* HAVE_SETLOCALE */
 #ifdef HAVE_NL_LANGINFO
 # include <langinfo.h>
 #endif /* HAVE_NL_LANGINFO */
@@ -132,15 +129,10 @@ do_syslog(int pri, char *msg)
     size_t len, maxlen;
     char *p, *tmp, save;
     const char *fmt;
-#ifdef HAVE_SETLOCALE
-    const char *old_locale = estrdup(setlocale(LC_ALL, NULL));
-#endif
+    int oldlocale;
     debug_decl(do_syslog, SUDO_DEBUG_LOGGING)
 
-#ifdef HAVE_SETLOCALE
-    if (!setlocale(LC_ALL, def_sudoers_locale))
-       setlocale(LC_ALL, "C");
-#endif /* HAVE_SETLOCALE */
+    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
 
     /*
      * Log the full line, breaking into multiple syslog(3) calls if necessary
@@ -177,10 +169,7 @@ do_syslog(int pri, char *msg)
        maxlen = MAXSYSLOGLEN - (strlen(fmt) - 5 + strlen(user_name));
     }
 
-#ifdef HAVE_SETLOCALE
-    setlocale(LC_ALL, old_locale);
-    efree((void *)old_locale);
-#endif /* HAVE_SETLOCALE */
+    sudoers_setlocale(oldlocale, NULL);
 
     debug_return;
 }
@@ -192,9 +181,12 @@ do_logfile(char *msg)
     size_t len;
     mode_t oldmask;
     time_t now;
+    int oldlocale;
     FILE *fp;
     debug_decl(do_logfile, SUDO_DEBUG_LOGGING)
 
+    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
+
     oldmask = umask(077);
     fp = fopen(def_logfile, "a");
     (void) umask(oldmask);
@@ -205,12 +197,6 @@ do_logfile(char *msg)
        send_mail(_("unable to lock log file: %s: %s"),
            def_logfile, strerror(errno));
     } else {
-#ifdef HAVE_SETLOCALE
-       const char *old_locale = estrdup(setlocale(LC_ALL, NULL));
-       if (!setlocale(LC_ALL, def_sudoers_locale))
-           setlocale(LC_ALL, "C");
-#endif /* HAVE_SETLOCALE */
-
        now = time(NULL);
        if (def_loglinelen < sizeof(LOG_INDENT)) {
            /* Don't pretty-print long log file lines (hard to grep) */
@@ -237,12 +223,9 @@ do_logfile(char *msg)
        (void) fflush(fp);
        (void) lock_file(fileno(fp), SUDO_UNLOCK);
        (void) fclose(fp);
-
-#ifdef HAVE_SETLOCALE
-       setlocale(LC_ALL, old_locale);
-       efree((void *)old_locale);
-#endif /* HAVE_SETLOCALE */
     }
+    sudoers_setlocale(oldlocale, NULL);
+
     debug_return;
 }
 
@@ -569,13 +552,7 @@ send_mail(const char *fmt, ...)
        (void) dup2(fd, STDERR_FILENO);
     }
 
-#ifdef HAVE_SETLOCALE
-    if (!setlocale(LC_ALL, def_sudoers_locale)) {
-       setlocale(LC_ALL, "C");
-       efree(def_sudoers_locale);
-       def_sudoers_locale = estrdup("C");
-    }
-#endif /* HAVE_SETLOCALE */
+    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, NULL);
 
     /* Close password, group and other fds so we don't leak. */
     sudo_endpwent();
index 0f3ce0fe1590e984a8eebbc92a7875fba02b8e6e..5ab6304998885a6903e7bea055e1d9ee5560e787 100644 (file)
@@ -217,7 +217,7 @@ sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[],
     char *iolog_path = NULL;
     mode_t cmnd_umask = 0777;
     struct sudo_nss *nss;
-    int cmnd_status = -1, validated;
+    int cmnd_status = -1, oldlocale, validated;
     volatile int rval = true;
     sigaction_t sa, saved_sa_int, saved_sa_quit, saved_sa_tstp;
     debug_decl(sudoers_policy_main, SUDO_DEBUG_PLUGIN)
@@ -290,17 +290,10 @@ sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[],
     /* Find command in path */
     cmnd_status = set_cmnd();
 
-#ifdef HAVE_SETLOCALE
-    if (!setlocale(LC_ALL, def_sudoers_locale)) {
-       warningx(_("unable to set locale to \"%s\", using \"C\""),
-           def_sudoers_locale);
-       setlocale(LC_ALL, "C");
-    }
-#endif
-
     /*
-     * Check sudoers sources.
+     * Check sudoers sources, using the locale specified in sudoers.
      */
+    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
     validated = FLAG_NO_USER | FLAG_NO_HOST;
     tq_foreach_fwd(snl, nss) {
        validated = nss->lookup(nss, validated, pwflag);
@@ -316,13 +309,12 @@ sudoers_policy_main(int argc, char * const argv[], int pwflag, char *env_add[],
        }
     }
 
+    /* Restore user's locale. */
+    sudoers_setlocale(oldlocale, NULL);
+
     if (safe_cmnd == NULL)
        safe_cmnd = estrdup(user_cmnd);
 
-#ifdef HAVE_SETLOCALE
-    setlocale(LC_ALL, "");
-#endif
-
     /* If only a group was specified, set runas_pw based on invoking user. */
     if (runas_pw == NULL)
        set_runaspw(user_name);