#define KEPT_MAX 0xff00
struct environment {
- char * const *old_envp; /* pointer the environment we passed back */
char **envp; /* pointer to the new environment */
+ char **old_envp; /* pointer the old environment we allocated */
size_t env_size; /* size of new_environ in char **'s */
size_t env_len; /* number of slots used, not counting NULL */
};
debug_decl(env_init, SUDOERS_DEBUG_ENV)
if (envp == NULL) {
+ /* Free the old envp we allocated, if any. */
+ sudo_efree(env.old_envp);
+
/* Reset to initial state but keep a pointer to what we allocated. */
- envp = env.envp;
- memset(&env, 0, sizeof(env));
- env.old_envp = envp;
+ env.old_envp = env.envp;
+ env.envp = NULL;
+ env.env_size = 0;
+ env.env_len = 0;
} else {
/* Make private copy of envp. */
for (ep = envp; *ep != NULL; ep++)
env.envp[len] = NULL;
/* Free the old envp we allocated, if any. */
- if (env.old_envp != NULL)
- sudo_efree((void *)env.old_envp);
+ sudo_efree(env.old_envp);
+ env.old_envp = NULL;
}
debug_return;
return env.envp;
}
+/*
+ * Swap the old and new copies of the environment.
+ */
+bool
+env_swap_old(void)
+{
+ char **old_envp;
+
+ if (env.old_envp == NULL)
+ return false;
+ old_envp = env.old_envp;
+ env.old_envp = env.envp;
+ env.envp = old_envp;
+ return true;
+}
+
/*
* Similar to putenv(3) but operates on sudo's private copy of the
* environment (not environ) and it always overwrites. The dupcheck param
bool
rebuild_env(void)
{
- char **old_envp, **ep, *cp, *ps1;
+ char **ep, *cp, *ps1;
char idbuf[MAX_UID_T_LEN + 1];
unsigned int didvar;
bool reset_home = false;
didvar = 0;
env.env_len = 0;
env.env_size = 128;
- old_envp = env.envp;
+ sudo_efree(env.old_envp);
+ env.old_envp = env.envp;
env.envp = sudo_emallocarray(env.env_size, sizeof(char *));
#ifdef ENV_DEBUG
memset(env.envp, 0, env.env_size * sizeof(char *));
}
/* Pull in vars we want to keep from the old environment. */
- for (ep = old_envp; *ep; ep++) {
+ for (ep = env.old_envp; *ep; ep++) {
bool keepit;
/*
* Copy environ entries as long as they don't match env_delete or
* env_check.
*/
- for (ep = old_envp; *ep; ep++) {
+ for (ep = env.old_envp; *ep; ep++) {
/* Add variable unless it matches a black list. */
if (!env_should_delete(*ep)) {
if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
* they have already been set) or sudoedit (because we want the editor
* to find the invoking user's startup files).
*/
- if (def_set_logname && !ISSET(sudo_mode, MODE_LOGIN_SHELL|MODE_EDIT)) {
+ if (def_set_logname && !ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
if (!ISSET(didvar, KEPT_LOGNAME))
sudo_setenv2("LOGNAME", runas_pw->pw_name, true, true);
if (!ISSET(didvar, KEPT_USER))
if (sudo_setenv2("SUDO_GID", idbuf, true, true) == -1)
goto bad;
- /* Free old environment. */
- sudo_efree(old_envp);
-
debug_return_bool(true);
bad: