Merge commit 'upstream/1.7.4p4'
[debian/sudo] / env.c
diff --git a/env.c b/env.c
index 029cd6d4ae1e449c7774d89981c16973b0a34a36..739631fd736bed8aac91c80b34ed4afb0978f44b 100644 (file)
--- a/env.c
+++ b/env.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2005, 2007-2008
+ * Copyright (c) 2000-2005, 2007-2010
  *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
 #endif /* STDC_HEADERS */
 #ifdef HAVE_STRING_H
 # include <string.h>
-#else
-# ifdef HAVE_STRINGS_H
-#  include <strings.h>
-# endif
 #endif /* HAVE_STRING_H */
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif /* HAVE_STRINGS_H */
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#include <ctype.h>
+#include <errno.h>
 #include <pwd.h>
 
 #include "sudo.h"
 
-#ifndef lint
-__unused static const char rcsid[] = "$Sudo: env.c,v 1.94 2008/11/09 14:13:12 millert Exp $";
-#endif /* lint */
-
 /*
  * Flags used in rebuild_env()
  */
@@ -68,6 +65,8 @@ __unused static const char rcsid[] = "$Sudo: env.c,v 1.94 2008/11/09 14:13:12 mi
 #define DID_USER       0x0020
 #undef DID_USERNAME
 #define DID_USERNAME           0x0040
+#undef DID_MAIL
+#define DID_MAIL       0x0080
 #undef DID_MAX
 #define DID_MAX        0x00ff
 
@@ -85,27 +84,23 @@ __unused static const char rcsid[] = "$Sudo: env.c,v 1.94 2008/11/09 14:13:12 mi
 #define KEPT_USER      0x2000
 #undef KEPT_USERNAME
 #define KEPT_USERNAME  0x4000
+#undef KEPT_MAIL
+#define KEPT_MAIL      0x8000
 #undef KEPT_MAX
 #define KEPT_MAX       0xff00
 
-#undef VNULL
-#define        VNULL   (void *)NULL
-
 struct environment {
     char **envp;               /* pointer to the new environment */
     size_t env_size;           /* size of new_environ in char **'s */
     size_t env_len;            /* number of slots used, not counting NULL */
+    int owned;                 /* do we own envp or is it the system's? */
 };
 
 /*
  * Prototypes
  */
-void rebuild_env               __P((int, int));
-void sudo_setenv               __P((const char *, const char *, int));
-void sudo_unsetenv             __P((const char *));
-static void _sudo_setenv       __P((const char *, const char *, int));
-static void insert_env         __P((char *, int, int));
-static void sync_env           __P((void));
+static void sudo_setenv                __P((const char *, const char *, int));
+static void sudo_putenv                __P((char *, int, int));
 
 extern char **environ;         /* global environment */
 
@@ -121,8 +116,6 @@ static struct environment env;
 static const char *initial_badenv_table[] = {
     "IFS",
     "CDPATH",
-    "SHELLOPTS",
-    "PS4",
     "LOCALDOMAIN",
     "RES_OPTIONS",
     "HOSTALIASES",
@@ -178,6 +171,7 @@ static const char *initial_badenv_table[] = {
     "PYTHONHOME",              /* python, module search path */
     "PYTHONPATH",              /* python, search path */
     "PYTHONINSPECT",           /* python, allow inspection */
+    "PYTHONUSERBASE",          /* python, per user site-packages directory */
     "RUBYLIB",                 /* ruby, library load path */
     "RUBYOPT",                 /* ruby, extra command line options */
     NULL
@@ -202,11 +196,9 @@ static const char *initial_checkenv_table[] = {
 static const char *initial_keepenv_table[] = {
     "COLORS",
     "DISPLAY",
-    "HOME",
     "HOSTNAME",
     "KRB5CCNAME",
     "LS_COLORS",
-    "MAIL",
     "PATH",
     "PS1",
     "PS2",
@@ -217,35 +209,53 @@ static const char *initial_keepenv_table[] = {
 };
 
 /*
- * Syncronize our private copy of the environment with what is
- * in environ.
+ * Initialize env based on envp.
  */
-static void
-sync_env()
+void
+env_init(lazy)
+    int lazy;
 {
-    size_t evlen;
-    char **ep;
+    char * const *ep;
+    size_t len;
 
     for (ep = environ; *ep != NULL; ep++)
        continue;
-    evlen = ep - environ;
-    if (evlen + 1 > env.env_size) {
-       efree(env.envp);
-       env.env_size = evlen + 1 + 128;
+    len = (size_t)(ep - environ);
+
+    if (lazy) {
+       /*
+        * If we are already initialized due to lazy init (usualy via getenv())
+        * we need to avoid calling malloc() as it may call getenv() itself.
+        */
+       env.envp = environ;
+       env.env_len = len;
+       env.env_size = len;
+    } else if (!env.owned) {
+       env.env_len = len;
+       env.env_size = len + 1 + 128;
        env.envp = emalloc2(env.env_size, sizeof(char *));
+#ifdef ENV_DEBUG
+       memset(env.envp, 0, env.env_size * sizeof(char *));
+#endif
+       memcpy(env.envp, environ, len * sizeof(char *));
+       env.envp[len] = '\0';
+       env.owned = TRUE;
     }
-    memcpy(env.envp, environ, (evlen + 1) * sizeof(char *));
-    env.env_len = evlen;
-    environ = env.envp;
+}
+
+char **
+env_get()
+{
+    return env.envp;
 }
 
 /*
  * Similar to setenv(3) but operates on sudo's private copy of the environment
- * and it always overwrites.  The dupcheck param determines whether we need
- * to verify that the variable is not already set.
+ * (not environ) and it always overwrites.  The dupcheck param determines
+ * whether we need to verify that the variable is not already set.
  */
 static void
-_sudo_setenv(var, val, dupcheck)
+sudo_setenv(var, val, dupcheck)
     const char *var;
     const char *val;
     int dupcheck;
@@ -263,106 +273,224 @@ _sudo_setenv(var, val, dupcheck)
 
        errorx(1, "internal error, sudo_setenv() overflow");
     }
-    insert_env(estring, dupcheck, FALSE);
+    sudo_putenv(estring, dupcheck, TRUE);
 }
 
-#ifdef HAVE_LDAP
 /*
- * External version of sudo_setenv() that keeps things in sync with
- * the environ pointer.
+ * Version of getenv(3) that uses our own environ pointer.
  */
-void
-sudo_setenv(var, val, dupcheck)
+char *
+getenv(var)
+    const char *var;
+{
+    char *cp, **ev;
+    size_t vlen = strlen(var);
+
+    if (env.envp == NULL)
+       env_init(TRUE);
+
+    for (ev = env.envp; (cp = *ev) != NULL; ev++) {
+       if (strncmp(var, cp, vlen) == 0 && cp[vlen] == '=')
+           return cp + vlen + 1;
+    }
+    return NULL;
+}
+
+/*
+ * Version of setenv(3) that uses our own environ pointer.
+ */
+int
+setenv(var, val, overwrite)
     const char *var;
     const char *val;
-    int dupcheck;
+    int overwrite;
 {
-    char *estring;
+    char *estring, *ep;
+    const char *cp;
     size_t esize;
 
-    /* Make sure we are operating on the current environment. */
-    if (env.envp != environ)
-       sync_env();
+    if (!var || *var == '\0') {
+       errno = EINVAL;
+       return(-1);
+    }
 
-    esize = strlen(var) + 1 + strlen(val) + 1;
-    estring = emalloc(esize);
+    if (env.envp == NULL)
+       env_init(TRUE);
 
-    /* Build environment string and insert it. */
-    if (strlcpy(estring, var, esize) >= esize ||
-       strlcat(estring, "=", esize) >= esize ||
-       strlcat(estring, val, esize) >= esize) {
+    /*
+     * POSIX says a var name with '=' is an error but BSD
+     * just ignores the '=' and anything after it.
+     */
+    for (cp = var; *cp && *cp != '='; cp++)
+       ;
+    esize = (size_t)(cp - var) + 2;
+    if (val) {
+       esize += strlen(val);   /* glibc treats a NULL val as "" */
+    }
 
-       errorx(1, "internal error, sudo_setenv() overflow");
+    /* Allocate and fill in estring. */
+    estring = ep = emalloc(esize);
+    for (cp = var; *cp && *cp != '='; cp++)
+       *ep++ = *cp;
+    *ep++ = '=';
+    if (val) {
+       for (cp = val; *cp; cp++)
+           *ep++ = *cp;
     }
-    insert_env(estring, dupcheck, TRUE);
+    *ep = '\0';
+
+#ifdef ENV_DEBUG
+    if (env.envp[env.env_len] != NULL)
+       errorx(1, "setenv: corrupted envp, len mismatch");
+#endif
+    sudo_putenv(estring, TRUE, overwrite);
+    return(0);
 }
-#endif /* HAVE_LDAP */
 
-#if defined(HAVE_LDAP) || defined(HAVE_AIXAUTH)
 /*
- * Similar to unsetenv(3) but operates on sudo's private copy of the
- * environment.
+ * Version of unsetenv(3) that uses our own environ pointer.
  */
+#ifdef UNSETENV_VOID
 void
-sudo_unsetenv(var)
+#else
+int
+#endif
+unsetenv(var)
     const char *var;
 {
-    char **nep;
-    size_t varlen;
-
-    /* Make sure we are operating on the current environment. */
-    if (env.envp != environ)
-       sync_env();
-
-    varlen = strlen(var);
-    for (nep = env.envp; *nep; nep++) {
-       if (strncmp(var, *nep, varlen) == 0 && (*nep)[varlen] == '=') {
-           /* Found it; move everything over by one and update len. */
-           memmove(nep, nep + 1,
-               (env.env_len - (nep - env.envp)) * sizeof(char *));
-           env.env_len--;
-           return;
+    char **ep;
+    size_t len;
+
+    if (var == NULL || *var == '\0' || strchr(var, '=') != NULL) {
+       errno = EINVAL;
+#ifdef UNSETENV_VOID
+       return;
+#else
+       return(-1);
+#endif
+    }
+
+    if (env.envp == NULL)
+       env_init(TRUE);
+
+#ifdef ENV_DEBUG
+    if (env.envp[env.env_len] != NULL)
+       errorx(1, "unsetenv: corrupted envp, len mismatch");
+#endif
+
+    len = strlen(var);
+    for (ep = env.envp; *ep != NULL;) {
+       if (strncmp(var, *ep, len) == 0 && (*ep)[len] == '=') {
+           /* Found it; shift remainder + NULL over by one. */
+           char **cur = ep;
+           while ((*cur = *(cur + 1)) != NULL)
+               cur++;
+           /* Keep going, could be multiple instances of the var. */
+       } else {
+           ep++;
        }
     }
+    env.env_len = ep - env.envp;
+#ifndef UNSETENV_VOID
+    return(0);
+#endif
+}
+
+/*
+ * Version of putenv(3) that uses our own environ pointer.
+ */
+int
+#ifdef PUTENV_CONST
+putenv(const char *string)
+#else
+putenv(string)
+    char *string;
+#endif
+{
+    if (env.envp == NULL)
+       env_init(TRUE);
+
+    if (strchr(string, '=') == NULL) {
+       errno = EINVAL;
+       return(-1);
+    }
+#ifdef ENV_DEBUG
+    if (env.envp[env.env_len] != NULL)
+       errorx(1, "putenv: corrupted envp, len mismatch");
+#endif
+    sudo_putenv((char *)string, TRUE, TRUE);
+    return(0);
 }
-#endif /* HAVE_LDAP || HAVE_AIXAUTH */
 
 /*
- * Insert str into env.envp, assumes str has an '=' in it.
+ * Similar to putenv(3) but operates on sudo's private copy of the
+ * environment (not environ) and it always overwrites.  The dupcheck param
+ * determines whether we need to verify that the variable is not already set.
+ * Will only overwrite an existing variable if overwrite is set.
  */
 static void
-insert_env(str, dupcheck, dosync)
+sudo_putenv(str, dupcheck, overwrite)
     char *str;
     int dupcheck;
-    int dosync;
+    int overwrite;
 {
-    char **nep;
-    size_t varlen;
+    char **ep;
+    size_t len;
+    int found = FALSE;
 
     /* Make sure there is room for the new entry plus a NULL. */
     if (env.env_len + 2 > env.env_size) {
        env.env_size += 128;
-       env.envp = erealloc3(env.envp, env.env_size, sizeof(char *));
-       if (dosync)
-           environ = env.envp;
+       if (env.owned) {
+           env.envp = erealloc3(env.envp, env.env_size, sizeof(char *));
+       } else {
+           /* We don't own env.envp, allocate a new one. */
+           ep = emalloc2(env.env_size, sizeof(char *));
+           memcpy(ep, env.envp, env.env_size * sizeof(char *));
+           env.envp = ep;
+           env.owned = TRUE;
+       }
+#ifdef ENV_DEBUG
+       memset(env.envp + env.env_len, 0,
+           (env.env_size - env.env_len) * sizeof(char *));
+#endif
     }
 
-    if (dupcheck) {
-           varlen = (strchr(str, '=') - str) + 1;
+#ifdef ENV_DEBUG
+    if (env.envp[env.env_len] != NULL)
+       errorx(1, "sudo_putenv: corrupted envp, len mismatch");
+#endif
 
-           for (nep = env.envp; *nep; nep++) {
-               if (strncmp(str, *nep, varlen) == 0) {
-                   if (dupcheck != -1)
-                       *nep = str;
-                   return;
+    if (dupcheck) {
+       len = (strchr(str, '=') - str) + 1;
+       for (ep = env.envp; !found && *ep != NULL; ep++) {
+           if (strncmp(str, *ep, len) == 0) {
+               if (overwrite)
+                   *ep = str;
+               found = TRUE;
+           }
+       }
+       /* Prune out duplicate variables. */
+       if (found && overwrite) {
+           while (*ep != NULL) {
+               if (strncmp(str, *ep, len) == 0) {
+                   char **cur = ep;
+                   while ((*cur = *(cur + 1)) != NULL)
+                       cur++;
+               } else {
+                   ep++;
                }
            }
-    } else
-       nep = env.envp + env.env_len;
+           env.env_len = ep - env.envp;
+       }
+    }
 
-    env.env_len++;
-    *nep++ = str;
-    *nep = NULL;
+    if (!found) {
+       ep = env.envp + env.env_len;
+       env.env_len++;
+       *ep++ = str;
+       *ep = NULL;
+    }
 }
 
 /*
@@ -460,13 +588,13 @@ matches_env_keep(var)
  * Also adds sudo-specific variables (SUDO_*).
  */
 void
-rebuild_env(sudo_mode, noexec)
-    int sudo_mode;
+rebuild_env(noexec)
     int noexec;
 {
     char **old_envp, **ep, *cp, *ps1;
     char idbuf[MAX_UID_T_LEN];
     unsigned int didvar;
+    int reset_home = FALSE;
 
     /*
      * Either clean out the environment or reset to a safe default.
@@ -477,9 +605,15 @@ rebuild_env(sudo_mode, noexec)
     env.env_size = 128;
     old_envp = env.envp;
     env.envp = emalloc2(env.env_size, sizeof(char *));
+#ifdef ENV_DEBUG
+    memset(env.envp, 0, env.env_size * sizeof(char *));
+#endif
     if (def_env_reset || ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
+       /* Reset HOME based on target user unless keeping old value. */
+       reset_home = TRUE;
+
        /* Pull in vars we want to keep from the old environment. */
-       for (ep = environ; *ep; ep++) {
+       for (ep = old_envp; *ep; ep++) {
            int keepit;
 
            /* Skip variables with values beginning with () (bash functions) */
@@ -497,17 +631,6 @@ rebuild_env(sudo_mode, noexec)
            if (keepit == -1)
                keepit = matches_env_keep(*ep);
 
-           if (!strncmp (*ep, "DISPLAY=",8)
-               || !strncmp (*ep, "XAUTHORITY=", 11)
-               || !strncmp (*ep, "XAUTHORIZATION=", 15)
-               || !strncmp (*ep, "XAPPLRESDIR=", 12)
-               || !strncmp (*ep, "XFILESEARCHPATH=", 16)
-               || !strncmp (*ep, "XUSERFILESEARCHPATH=", 20)
-               || !strncmp (*ep, "LANG=", 5)
-               || !strncmp (*ep, "LANGUAGE=", 9)
-               || !strncmp (*ep, "LC_", 3))
-             keepit = 1;
-
            /* For SUDO_PS1 -> PS1 conversion. */
            if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
                ps1 = *ep + 5;
@@ -523,6 +646,10 @@ rebuild_env(sudo_mode, noexec)
                        if (strncmp(*ep, "LOGNAME=", 8) == 0)
                            SET(didvar, DID_LOGNAME);
                        break;
+                   case 'M':
+                       if (strncmp(*ep, "MAIL=", 5) == 0)
+                           SET(didvar, DID_MAIL);
+                       break;
                    case 'P':
                        if (strncmp(*ep, "PATH=", 5) == 0)
                            SET(didvar, DID_PATH);
@@ -542,7 +669,7 @@ rebuild_env(sudo_mode, noexec)
                            SET(didvar, DID_USERNAME);
                        break;
                }
-               insert_env(*ep, FALSE, FALSE);
+               sudo_putenv(*ep, FALSE, FALSE);
            }
        }
        didvar |= didvar << 8;          /* convert DID_* to KEPT_* */
@@ -553,31 +680,47 @@ rebuild_env(sudo_mode, noexec)
         * on sudoers options).
         */
        if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
-           _sudo_setenv("HOME", runas_pw->pw_dir, ISSET(didvar, DID_HOME));
-           _sudo_setenv("SHELL", runas_pw->pw_shell, ISSET(didvar, DID_SHELL));
-           _sudo_setenv("LOGNAME", runas_pw->pw_name,
+           sudo_setenv("SHELL", runas_pw->pw_shell, ISSET(didvar, DID_SHELL));
+           sudo_setenv("LOGNAME", runas_pw->pw_name,
                ISSET(didvar, DID_LOGNAME));
-           _sudo_setenv("USER", runas_pw->pw_name, ISSET(didvar, DID_USER));
-           _sudo_setenv("USERNAME", runas_pw->pw_name,
+           sudo_setenv("USER", runas_pw->pw_name, ISSET(didvar, DID_USER));
+           sudo_setenv("USERNAME", runas_pw->pw_name,
                ISSET(didvar, DID_USERNAME));
        } else {
-           if (!ISSET(didvar, DID_HOME))
-               _sudo_setenv("HOME", user_dir, FALSE);
            if (!ISSET(didvar, DID_SHELL))
-               _sudo_setenv("SHELL", sudo_user.pw->pw_shell, FALSE);
+               sudo_setenv("SHELL", sudo_user.pw->pw_shell, FALSE);
            if (!ISSET(didvar, DID_LOGNAME))
-               _sudo_setenv("LOGNAME", user_name, FALSE);
+               sudo_setenv("LOGNAME", user_name, FALSE);
            if (!ISSET(didvar, DID_USER))
-               _sudo_setenv("USER", user_name, FALSE);
+               sudo_setenv("USER", user_name, FALSE);
            if (!ISSET(didvar, DID_USERNAME))
-               _sudo_setenv("USERNAME", user_name, FALSE);
+               sudo_setenv("USERNAME", user_name, FALSE);
+       }
+       /*
+        * Set MAIL to target user in -i mode or if MAIL is not preserved
+        * from user's environment.
+        */
+       if (ISSET(sudo_mode, MODE_LOGIN_SHELL) || !ISSET(didvar, KEPT_MAIL)) {
+           cp = _PATH_MAILDIR;
+           if (cp[sizeof(_PATH_MAILDIR) - 2] == '/')
+               easprintf(&cp, "MAIL=%s%s", _PATH_MAILDIR, runas_pw->pw_name);
+           else
+               easprintf(&cp, "MAIL=%s/%s", _PATH_MAILDIR, runas_pw->pw_name);
+           sudo_putenv(cp, ISSET(didvar, DID_MAIL), TRUE);
        }
     } else {
+       /* Reset HOME based on target user if configured to. */
+       if (ISSET(sudo_mode, MODE_RUN)) {
+           if (def_always_set_home || ISSET(sudo_mode, MODE_RESET_HOME) || 
+               (ISSET(sudo_mode, MODE_SHELL) && def_set_home))
+               reset_home = TRUE;
+       }
+
        /*
         * Copy environ entries as long as they don't match env_delete or
         * env_check.
         */
-       for (ep = environ; *ep; ep++) {
+       for (ep = old_envp; *ep; ep++) {
            int okvar;
 
            /* Skip variables with values beginning with () (bash functions) */
@@ -601,41 +744,35 @@ rebuild_env(sudo_mode, noexec)
                    SET(didvar, DID_PATH);
                else if (strncmp(*ep, "TERM=", 5) == 0)
                    SET(didvar, DID_TERM);
-               insert_env(*ep, FALSE, FALSE);
+               sudo_putenv(*ep, FALSE, FALSE);
            }
        }
     }
     /* Replace the PATH envariable with a secure one? */
     if (def_secure_path && !user_is_exempt()) {
-       _sudo_setenv("PATH", def_secure_path, TRUE);
+       sudo_setenv("PATH", def_secure_path, TRUE);
        SET(didvar, DID_PATH);
     }
 
     /* Set $USER, $LOGNAME and $USERNAME to target if "set_logname" is true. */
-    /* XXX - not needed for MODE_LOGIN_SHELL */
-    if (def_set_logname && runas_pw->pw_name) {
+    if (def_set_logname && !ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
        if (!ISSET(didvar, KEPT_LOGNAME))
-           _sudo_setenv("LOGNAME", runas_pw->pw_name, TRUE);
+           sudo_setenv("LOGNAME", runas_pw->pw_name, TRUE);
        if (!ISSET(didvar, KEPT_USER))
-           _sudo_setenv("USER", runas_pw->pw_name, TRUE);
+           sudo_setenv("USER", runas_pw->pw_name, TRUE);
        if (!ISSET(didvar, KEPT_USERNAME))
-           _sudo_setenv("USERNAME", runas_pw->pw_name, TRUE);
+           sudo_setenv("USERNAME", runas_pw->pw_name, TRUE);
     }
 
-    /* Set $HOME for `sudo -H'.  Only valid at PERM_FULL_RUNAS. */
-    /* XXX - not needed for MODE_LOGIN_SHELL */
-    if (runas_pw->pw_dir) {
-       if (ISSET(sudo_mode, MODE_RESET_HOME) ||
-           (ISSET(sudo_mode, MODE_RUN) && (def_always_set_home ||
-           (ISSET(sudo_mode, MODE_SHELL) && def_set_home))))
-           _sudo_setenv("HOME", runas_pw->pw_dir, TRUE);
-    }
+    /* Set $HOME to target user if not preserving user's value. */
+    if (reset_home && !ISSET(didvar, KEPT_HOME))
+       sudo_setenv("HOME", runas_pw->pw_dir, TRUE);
 
     /* Provide default values for $TERM and $PATH if they are not set. */
     if (!ISSET(didvar, DID_TERM))
-       insert_env("TERM=unknown", FALSE, FALSE);
+       sudo_putenv("TERM=unknown", FALSE, FALSE);
     if (!ISSET(didvar, DID_PATH))
-       _sudo_setenv("PATH", _PATH_DEFPATH, FALSE);
+       sudo_setenv("PATH", _PATH_STDPATH, FALSE);
 
     /*
      * Preload a noexec file?  For a list of LD_PRELOAD-alikes, see
@@ -644,18 +781,18 @@ rebuild_env(sudo_mode, noexec)
      */
     if (noexec && def_noexec_file != NULL) {
 #if defined(__darwin__) || defined(__APPLE__)
-       _sudo_setenv("DYLD_INSERT_LIBRARIES", def_noexec_file, TRUE);
-       _sudo_setenv("DYLD_FORCE_FLAT_NAMESPACE", "", TRUE);
+       sudo_setenv("DYLD_INSERT_LIBRARIES", def_noexec_file, TRUE);
+       sudo_setenv("DYLD_FORCE_FLAT_NAMESPACE", "", TRUE);
 #else
 # if defined(__osf__) || defined(__sgi)
        easprintf(&cp, "%s:DEFAULT", def_noexec_file);
-       _sudo_setenv("_RLD_LIST", cp, TRUE);
+       sudo_setenv("_RLD_LIST", cp, TRUE);
        efree(cp);
 # else
 #  ifdef _AIX
-       _sudo_setenv("LDR_PRELOAD", def_noexec_file, TRUE);
+       sudo_setenv("LDR_PRELOAD", def_noexec_file, TRUE);
 #  else
-       _sudo_setenv("LD_PRELOAD", def_noexec_file, TRUE);
+       sudo_setenv("LD_PRELOAD", def_noexec_file, TRUE);
 #  endif /* _AIX */
 # endif /* __osf__ || __sgi */
 #endif /* __darwin__ || __APPLE__ */
@@ -663,25 +800,25 @@ rebuild_env(sudo_mode, noexec)
 
     /* Set PS1 if SUDO_PS1 is set. */
     if (ps1 != NULL)
-       insert_env(ps1, TRUE, FALSE);
+       sudo_putenv(ps1, TRUE, TRUE);
 
     /* Add the SUDO_COMMAND envariable (cmnd + args). */
     if (user_args) {
        easprintf(&cp, "%s %s", user_cmnd, user_args);
-       _sudo_setenv("SUDO_COMMAND", cp, TRUE);
+       sudo_setenv("SUDO_COMMAND", cp, TRUE);
        efree(cp);
-    } else
-       _sudo_setenv("SUDO_COMMAND", user_cmnd, TRUE);
+    } else {
+       sudo_setenv("SUDO_COMMAND", user_cmnd, TRUE);
+    }
 
     /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */
-    _sudo_setenv("SUDO_USER", user_name, TRUE);
+    sudo_setenv("SUDO_USER", user_name, TRUE);
     snprintf(idbuf, sizeof(idbuf), "%lu", (unsigned long) user_uid);
-    _sudo_setenv("SUDO_UID", idbuf, TRUE);
+    sudo_setenv("SUDO_UID", idbuf, TRUE);
     snprintf(idbuf, sizeof(idbuf), "%lu", (unsigned long) user_gid);
-    _sudo_setenv("SUDO_GID", idbuf, TRUE);
+    sudo_setenv("SUDO_GID", idbuf, TRUE);
 
-    /* Install new environment. */
-    environ = env.envp;
+    /* Free old environment. */
     efree(old_envp);
 }
 
@@ -691,16 +828,9 @@ insert_env_vars(env_vars)
 {
     struct list_member *cur;
 
-    if (env_vars == NULL)
-       return;
-
-    /* Make sure we are operating on the current environment. */
-    if (env.envp != environ)
-       sync_env();
-
     /* Add user-specified environment variables. */
     for (cur = env_vars; cur != NULL; cur = cur->next)
-       insert_env(cur->value, TRUE, TRUE);
+       putenv(cur->value);
 }
 
 /*
@@ -717,6 +847,7 @@ validate_env_vars(env_vars)
     size_t len, blen = 0, bsize = 0;
     int okvar;
 
+    /* Add user-specified environment variables. */
     for (var = env_vars; var != NULL; var = var->next) {
        if (def_secure_path && !user_is_exempt() &&
            strncmp(var->value, "PATH=", 5) == 0) {
@@ -760,35 +891,59 @@ validate_env_vars(env_vars)
 
 /*
  * Read in /etc/environment ala AIX and Linux.
- * Lines are in the form of NAME=VALUE
+ * Lines may be in either of three formats:
+ *  NAME=VALUE
+ *  NAME="VALUE"
+ *  NAME='VALUE'
+ * with an optional "export" prefix so the shell can source the file.
  * Invalid lines, blank lines, or lines consisting solely of a comment
  * character are skipped.
  */
 void
-read_env_file(path, replace)
+read_env_file(path, overwrite)
     const char *path;
-    int replace;
+    int overwrite;
 {
     FILE *fp;
-    char *cp;
+    char *cp, *var, *val;
+    size_t var_len, val_len;
 
     if ((fp = fopen(path, "r")) == NULL)
        return;
 
-    /* Make sure we are operating on the current environment. */
-    if (env.envp != environ)
-       sync_env();
-
-    while ((cp = sudo_parseln(fp)) != NULL) {
+    while ((var = sudo_parseln(fp)) != NULL) {
        /* Skip blank or comment lines */
-       if (*cp == '\0')
+       if (*var == '\0')
            continue;
 
-       /* Must be of the form name=value */
-       if (strchr(cp, '=') == NULL)
+       /* Skip optional "export " */
+       if (strncmp(var, "export", 6) == 0 && isspace((unsigned char) var[6])) {
+           var += 7;
+           while (isspace((unsigned char) *var)) {
+               var++;
+           }
+       }
+
+       /* Must be of the form name=["']value['"] */
+       for (val = var; *val != '\0' && *val != '='; val++)
+           ;
+       if (var == val || *val != '=')
            continue;
+       var_len = (size_t)(val - var);
+       val_len = strlen(++val);
+
+       /* Strip leading and trailing single/double quotes */
+       if ((val[0] == '\'' || val[0] == '\"') && val[0] == val[val_len - 1]) {
+           val[val_len - 1] = '\0';
+           val++;
+           val_len -= 2;
+       }
+
+       cp = emalloc(var_len + 1 + val_len + 1);
+       memcpy(cp, var, var_len + 1); /* includes '=' */
+       memcpy(cp + var_len + 1, val, val_len + 1); /* includes NUL */
 
-       insert_env(estrdup(cp), replace ? TRUE : -1, TRUE);
+       sudo_putenv(cp, TRUE, overwrite);
     }
     fclose(fp);
 }