From: Todd C. Miller Date: Thu, 8 Nov 2012 20:37:43 +0000 (-0500) Subject: Convert setlocale() to sudoers_setlocale() in the sudoers module. X-Git-Tag: SUDO_1_8_7~1^2~354 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3f82a3407eff2bf0721b7664022a5d310acb32dd;p=sudo Convert setlocale() to sudoers_setlocale() in the sudoers module. This only converts existing uses, there are more places where we need to sprinkle sudoers_setlocale() calls. --- diff --git a/plugins/sudoers/iolog_path.c b/plugins/sudoers/iolog_path.c index fec178142..1f615f3d7 100644 --- a/plugins/sudoers/iolog_path.c +++ b/plugins/sudoers/iolog_path.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Todd C. Miller + * Copyright (c) 2011-2012 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 @@ -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? */ diff --git a/plugins/sudoers/logging.c b/plugins/sudoers/logging.c index f316a18aa..c9e6dd58b 100644 --- a/plugins/sudoers/logging.c +++ b/plugins/sudoers/logging.c @@ -47,9 +47,6 @@ #ifdef HAVE_UNISTD_H # include #endif /* HAVE_UNISTD_H */ -#ifdef HAVE_SETLOCALE -# include -#endif /* HAVE_SETLOCALE */ #ifdef HAVE_NL_LANGINFO # include #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(); diff --git a/plugins/sudoers/sudoers.c b/plugins/sudoers/sudoers.c index 0f3ce0fe1..5ab630499 100644 --- a/plugins/sudoers/sudoers.c +++ b/plugins/sudoers/sudoers.c @@ -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);