2 * Copyright (c) 2000-2005, 2007-2011
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #include <sys/types.h>
25 #include <sys/param.h>
35 #endif /* STDC_HEADERS */
38 #endif /* HAVE_STRING_H */
41 #endif /* HAVE_STRINGS_H */
44 #endif /* HAVE_UNISTD_H */
45 #ifdef HAVE_INTTYPES_H
46 # include <inttypes.h>
48 #ifdef HAVE_LOGIN_CAP_H
49 # include <login_cap.h>
51 # define LOGIN_SETENV 0
53 #endif /* HAVE_LOGIN_CAP_H */
62 * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
63 * could be signed (as it is on SunOS 4.x). This just means that
64 * emalloc2() and erealloc3() cannot allocate huge amounts on such a
65 * platform but that is OK since sudo doesn't need to do so anyway.
69 # define SIZE_MAX SIZE_T_MAX
71 # define SIZE_MAX INT_MAX
72 # endif /* SIZE_T_MAX */
76 * Flags used in rebuild_env()
79 #define DID_TERM 0x0001
81 #define DID_PATH 0x0002
83 #define DID_HOME 0x0004
85 #define DID_SHELL 0x0008
87 #define DID_LOGNAME 0x0010
89 #define DID_USER 0x0020
91 #define DID_USERNAME 0x0040
93 #define DID_MAIL 0x0080
95 #define DID_MAX 0x00ff
98 #define KEPT_TERM 0x0100
100 #define KEPT_PATH 0x0200
102 #define KEPT_HOME 0x0400
104 #define KEPT_SHELL 0x0800
106 #define KEPT_LOGNAME 0x1000
108 #define KEPT_USER 0x2000
110 #define KEPT_USERNAME 0x4000
112 #define KEPT_MAIL 0x8000
114 #define KEPT_MAX 0xff00
117 char * const *old_envp; /* pointer the environment we passed back */
118 char **envp; /* pointer to the new environment */
119 size_t env_size; /* size of new_environ in char **'s */
120 size_t env_len; /* number of slots used, not counting NULL */
124 * Copy of the sudo-managed environment.
126 static struct environment env;
129 * Default table of "bad" variables to remove from the environment.
130 * XXX - how to omit TERMCAP if it starts with '/'?
132 static const char *initial_badenv_table[] = {
156 #endif /* HAVE_KERB5 */
161 #endif /* HAVE_SECURID */
162 "TERMINFO", /* terminfo, exclusive path to terminfo files */
163 "TERMINFO_DIRS", /* terminfo, path(s) to terminfo files */
164 "TERMPATH", /* termcap, path(s) to termcap files */
165 "TERMCAP", /* XXX - only if it starts with '/' */
166 "ENV", /* ksh, file to source before script runs */
167 "BASH_ENV", /* bash, file to source before script runs */
168 "PS4", /* bash, prefix for lines in xtrace mode */
169 "GLOBIGNORE", /* bash, globbing patterns to ignore */
170 "SHELLOPTS", /* bash, extra command line options */
171 "JAVA_TOOL_OPTIONS", /* java, extra command line options */
172 "PERLIO_DEBUG ", /* perl, debugging output file */
173 "PERLLIB", /* perl, search path for modules/includes */
174 "PERL5LIB", /* perl 5, search path for modules/includes */
175 "PERL5OPT", /* perl 5, extra command line options */
176 "PERL5DB", /* perl 5, command used to load debugger */
177 "FPATH", /* ksh, search path for functions */
178 "NULLCMD", /* zsh, command for null file redirection */
179 "READNULLCMD", /* zsh, command for null file redirection */
180 "ZDOTDIR", /* zsh, search path for dot files */
181 "TMPPREFIX", /* zsh, prefix for temporary files */
182 "PYTHONHOME", /* python, module search path */
183 "PYTHONPATH", /* python, search path */
184 "PYTHONINSPECT", /* python, allow inspection */
185 "PYTHONUSERBASE", /* python, per user site-packages directory */
186 "RUBYLIB", /* ruby, library load path */
187 "RUBYOPT", /* ruby, extra command line options */
192 * Default table of variables to check for '%' and '/' characters.
194 static const char *initial_checkenv_table[] = {
205 * Default table of variables to preserve in the environment.
207 static const char *initial_keepenv_table[] = {
223 * Initialize env based on envp.
226 env_init(char * const envp[])
230 debug_decl(env_init, SUDO_DEBUG_ENV)
233 /* Reset to initial state but keep a pointer to what we allocated. */
235 memset(&env, 0, sizeof(env));
238 /* Make private copy of envp. */
239 for (ep = envp; *ep != NULL; ep++)
241 len = (size_t)(ep - envp);
244 env.env_size = len + 1 + 128;
245 env.envp = emalloc2(env.env_size, sizeof(char *));
247 memset(env.envp, 0, env.env_size * sizeof(char *));
249 memcpy(env.envp, envp, len * sizeof(char *));
250 env.envp[len] = NULL;
252 /* Free the old envp we allocated, if any. */
253 if (env.old_envp != NULL)
254 efree((void *)env.old_envp);
261 * Getter for private copy of the environment.
270 * Similar to putenv(3) but operates on sudo's private copy of the
271 * environment (not environ) and it always overwrites. The dupcheck param
272 * determines whether we need to verify that the variable is not already set.
273 * Will only overwrite an existing variable if overwrite is set.
274 * Does not include warnings or debugging to avoid recursive calls.
277 sudo_putenv_nodebug(char *str, bool dupcheck, bool overwrite)
283 /* Make sure there is room for the new entry plus a NULL. */
284 if (env.env_size > 2 && env.env_len > env.env_size - 2) {
288 if (env.env_size > SIZE_MAX - 128) {
289 errorx2(1, _("internal error, %s overflow"),
290 "sudo_putenv_nodebug()");
292 nsize = env.env_size + 128;
293 if (nsize > SIZE_MAX / sizeof(char *)) {
294 errorx2(1, _("internal error, %s overflow"),
295 "sudo_putenv_nodebug()");
297 nenvp = realloc(env.envp, nsize * sizeof(char *));
303 env.env_size = nsize;
305 memset(env.envp + env.env_len, 0,
306 (env.env_size - env.env_len) * sizeof(char *));
311 if (env.envp[env.env_len] != NULL) {
318 len = (strchr(str, '=') - str) + 1;
319 for (ep = env.envp; *ep != NULL; ep++) {
320 if (strncmp(str, *ep, len) == 0) {
327 /* Prune out extra instances of the variable we just overwrote. */
328 if (found && overwrite) {
329 while (*++ep != NULL) {
330 if (strncmp(str, *ep, len) == 0) {
332 while ((*cur = *(cur + 1)) != NULL)
337 env.env_len = ep - env.envp;
342 ep = env.envp + env.env_len;
351 * Similar to putenv(3) but operates on sudo's private copy of the
352 * environment (not environ) and it always overwrites. The dupcheck param
353 * determines whether we need to verify that the variable is not already set.
354 * Will only overwrite an existing variable if overwrite is set.
357 sudo_putenv(char *str, bool dupcheck, bool overwrite)
360 debug_decl(sudo_putenv, SUDO_DEBUG_ENV)
362 sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_putenv: %s", str);
364 rval = sudo_putenv_nodebug(str, dupcheck, overwrite);
367 if (env.envp[env.env_len] != NULL)
368 errorx(1, _("sudo_putenv: corrupted envp, length mismatch"));
370 errorx(1, _("unable to allocate memory"));
372 debug_return_int(rval);
376 * Similar to setenv(3) but operates on a private copy of the environment.
377 * The dupcheck param determines whether we need to verify that the variable
378 * is not already set.
381 sudo_setenv2(const char *var, const char *val, bool dupcheck, bool overwrite)
386 debug_decl(sudo_setenv2, SUDO_DEBUG_ENV)
388 esize = strlen(var) + 1 + strlen(val) + 1;
389 estring = emalloc(esize);
391 /* Build environment string and insert it. */
392 if (strlcpy(estring, var, esize) >= esize ||
393 strlcat(estring, "=", esize) >= esize ||
394 strlcat(estring, val, esize) >= esize) {
396 errorx(1, _("internal error, %s overflow"), "sudo_setenv2()");
398 rval = sudo_putenv(estring, dupcheck, overwrite);
401 debug_return_int(rval);
405 * Similar to setenv(3) but operates on a private copy of the environment.
406 * Does not include warnings or debugging to avoid recursive calls.
409 sudo_setenv_nodebug(const char *var, const char *val, int overwrite)
415 esize = strlen(var) + 1 + strlen(val) + 1;
416 if ((estring = malloc(esize)) == NULL) {
421 /* Build environment string and insert it. */
422 if (strlcpy(estring, var, esize) >= esize ||
423 strlcat(estring, "=", esize) >= esize ||
424 strlcat(estring, val, esize) >= esize) {
429 rval = sudo_putenv_nodebug(estring, true, overwrite);
437 * Similar to setenv(3) but operates on a private copy of the environment.
440 sudo_setenv(const char *var, const char *val, int overwrite)
443 debug_decl(sudo_setenv, SUDO_DEBUG_ENV)
445 rval = sudo_setenv_nodebug(var, val, overwrite);
448 errorx(1, _("internal error, %s overflow"), "sudo_setenv()");
449 errorx(1, _("unable to allocate memory"));
451 debug_return_int(rval);
455 * Similar to unsetenv(3) but operates on a private copy of the environment.
456 * Does not include warnings or debugging to avoid recursive calls.
459 sudo_unsetenv_nodebug(const char *var)
461 char **ep = env.envp;
464 if (ep == NULL || var == NULL || *var == '\0' || strchr(var, '=') != NULL) {
470 while (*ep != NULL) {
471 if (strncmp(var, *ep, len) == 0 && (*ep)[len] == '=') {
472 /* Found it; shift remainder + NULL over by one. */
474 while ((*cur = *(cur + 1)) != NULL)
476 /* Keep going, could be multiple instances of the var. */
485 * Similar to unsetenv(3) but operates on a private copy of the environment.
488 sudo_unsetenv(const char *name)
491 debug_decl(sudo_unsetenv, SUDO_DEBUG_ENV)
493 sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_unsetenv: %s", name);
495 rval = sudo_unsetenv_nodebug(name);
497 debug_return_int(rval);
501 * Similar to getenv(3) but operates on a private copy of the environment.
502 * Does not include warnings or debugging to avoid recursive calls.
505 sudo_getenv_nodebug(const char *name)
507 char **ep, *val = NULL;
510 if (env.env_len != 0) {
511 /* For BSD compatibility, treat '=' in name like end of string. */
512 while (name[namelen] != '\0' && name[namelen] != '=')
514 for (ep = env.envp; *ep != NULL; ep++) {
515 if (strncmp(*ep, name, namelen) == 0 && (*ep)[namelen] == '=') {
516 val = *ep + namelen + 1;
525 * Similar to getenv(3) but operates on a private copy of the environment.
528 sudo_getenv(const char *name)
531 debug_decl(sudo_getenv, SUDO_DEBUG_ENV)
533 sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_getenv: %s", name);
535 val = sudo_getenv_nodebug(name);
537 debug_return_str(val);
541 * Merge another environment with our private copy.
544 env_merge(char * const envp[], bool overwrite)
547 debug_decl(env_merge, SUDO_DEBUG_ENV)
549 for (ep = envp; *ep != NULL; ep++)
550 sudo_putenv(*ep, true, overwrite);
556 * Check the env_delete blacklist.
557 * Returns true if the variable was found, else false.
560 matches_env_delete(const char *var)
562 struct list_member *cur;
566 debug_decl(matches_env_delete, SUDO_DEBUG_ENV)
568 /* Skip anything listed in env_delete. */
569 for (cur = def_env_delete; cur; cur = cur->next) {
570 len = strlen(cur->value);
571 /* Deal with '*' wildcard */
572 if (cur->value[len - 1] == '*') {
577 if (strncmp(cur->value, var, len) == 0 &&
578 (iswild || var[len] == '=')) {
583 debug_return_bool(match);
587 * Apply the env_check list.
588 * Returns true if the variable is allowed, false if denied
592 matches_env_check(const char *var)
594 struct list_member *cur;
598 debug_decl(matches_env_check, SUDO_DEBUG_ENV)
600 for (cur = def_env_check; cur; cur = cur->next) {
601 len = strlen(cur->value);
602 /* Deal with '*' wildcard */
603 if (cur->value[len - 1] == '*') {
608 if (strncmp(cur->value, var, len) == 0 &&
609 (iswild || var[len] == '=')) {
610 keepit = !strpbrk(var, "/%");
614 debug_return_bool(keepit);
618 * Check the env_keep list.
619 * Returns true if the variable is allowed else false.
622 matches_env_keep(const char *var)
624 struct list_member *cur;
626 bool iswild, keepit = false;
627 debug_decl(matches_env_keep, SUDO_DEBUG_ENV)
629 /* Preserve SHELL variable for "sudo -s". */
630 if (ISSET(sudo_mode, MODE_SHELL) && strncmp(var, "SHELL=", 6) == 0) {
635 for (cur = def_env_keep; cur; cur = cur->next) {
636 len = strlen(cur->value);
637 /* Deal with '*' wildcard */
638 if (cur->value[len - 1] == '*') {
643 if (strncmp(cur->value, var, len) == 0 &&
644 (iswild || var[len] == '=')) {
650 debug_return_bool(keepit);
654 * Look up var in the env_delete and env_check.
655 * Returns true if we should delete the variable, else false.
658 env_should_delete(const char *var)
661 debug_decl(env_should_delete, SUDO_DEBUG_ENV);
663 delete_it = matches_env_delete(var);
665 delete_it = matches_env_check(var) == false;
667 sudo_debug_printf(SUDO_DEBUG_INFO, "delete %s: %s",
668 var, delete_it ? "YES" : "NO");
669 debug_return_bool(delete_it);
673 * Lookup var in the env_check and env_keep lists.
674 * Returns true if the variable is allowed else false.
677 env_should_keep(const char *var)
680 debug_decl(env_should_keep, SUDO_DEBUG_ENV)
682 keepit = matches_env_check(var);
684 keepit = matches_env_keep(var);
686 sudo_debug_printf(SUDO_DEBUG_INFO, "keep %s: %s",
687 var, keepit ? "YES" : "NO");
688 debug_return_bool(keepit == true);
692 env_update_didvar(const char *ep, unsigned int *didvar)
696 if (strncmp(ep, "HOME=", 5) == 0)
697 SET(*didvar, DID_HOME);
700 if (strncmp(ep, "LOGNAME=", 8) == 0)
701 SET(*didvar, DID_LOGNAME);
704 if (strncmp(ep, "MAIL=", 5) == 0)
705 SET(*didvar, DID_MAIL);
708 if (strncmp(ep, "PATH=", 5) == 0)
709 SET(*didvar, DID_PATH);
712 if (strncmp(ep, "SHELL=", 6) == 0)
713 SET(*didvar, DID_SHELL);
716 if (strncmp(ep, "TERM=", 5) == 0)
717 SET(*didvar, DID_TERM);
720 if (strncmp(ep, "USER=", 5) == 0)
721 SET(*didvar, DID_USER);
722 if (strncmp(ep, "USERNAME=", 5) == 0)
723 SET(*didvar, DID_USERNAME);
729 * Build a new environment and ether clear potentially dangerous
730 * variables from the old one or start with a clean slate.
731 * Also adds sudo-specific variables (SUDO_*).
736 char **old_envp, **ep, *cp, *ps1;
737 char idbuf[MAX_UID_T_LEN + 1];
739 bool reset_home = false;
742 * Either clean out the environment or reset to a safe default.
749 env.envp = emalloc2(env.env_size, sizeof(char *));
751 memset(env.envp, 0, env.env_size * sizeof(char *));
756 /* Reset HOME based on target user if configured to. */
757 if (ISSET(sudo_mode, MODE_RUN)) {
758 if (def_always_set_home ||
759 ISSET(sudo_mode, MODE_RESET_HOME | MODE_LOGIN_SHELL) ||
760 (ISSET(sudo_mode, MODE_SHELL) && def_set_home))
764 if (def_env_reset || ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
766 * If starting with a fresh environment, initialize it based on
767 * /etc/environment or login.conf. For "sudo -i" we want those
768 * variables to override the invoking user's environment, so we
769 * defer reading them until later.
771 if (!ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
772 #ifdef HAVE_LOGIN_CAP_H
773 /* Insert login class environment variables. */
775 login_cap_t *lc = login_getclass(login_class);
777 setusercontext(lc, runas_pw, runas_pw->pw_uid,
778 LOGIN_SETPATH|LOGIN_SETENV);
782 #endif /* HAVE_LOGIN_CAP_H */
783 #if defined(_AIX) || (defined(__linux__) && !defined(HAVE_PAM))
784 /* Insert system-wide environment variables. */
785 read_env_file(_PATH_ENVIRONMENT, true);
787 for (ep = env.envp; *ep; ep++)
788 env_update_didvar(*ep, &didvar);
791 /* Pull in vars we want to keep from the old environment. */
792 for (ep = old_envp; *ep; ep++) {
795 /* Skip variables with values beginning with () (bash functions) */
796 if ((cp = strchr(*ep, '=')) != NULL) {
797 if (strncmp(cp, "=() ", 3) == 0)
802 * Look up the variable in the env_check and env_keep lists.
804 keepit = env_should_keep(*ep);
807 * Do SUDO_PS1 -> PS1 conversion.
808 * This must happen *after* env_should_keep() is called.
810 if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
814 /* Preserve variable. */
815 sudo_putenv(*ep, false, false);
816 env_update_didvar(*ep, &didvar);
819 didvar |= didvar << 8; /* convert DID_* to KEPT_* */
822 * Add in defaults. In -i mode these come from the runas user,
823 * otherwise they may be from the user's environment (depends
824 * on sudoers options).
826 if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
827 sudo_setenv2("SHELL", runas_pw->pw_shell,
828 ISSET(didvar, DID_SHELL), true);
829 sudo_setenv2("LOGNAME", runas_pw->pw_name,
830 ISSET(didvar, DID_LOGNAME), true);
831 sudo_setenv2("USER", runas_pw->pw_name,
832 ISSET(didvar, DID_USER), true);
833 sudo_setenv2("USERNAME", runas_pw->pw_name,
834 ISSET(didvar, DID_USERNAME), true);
836 if (!ISSET(didvar, DID_SHELL))
837 sudo_setenv2("SHELL", sudo_user.pw->pw_shell, false, true);
838 /* We will set LOGNAME later in the !def_set_logname case. */
839 if (!def_set_logname) {
840 if (!ISSET(didvar, DID_LOGNAME))
841 sudo_setenv2("LOGNAME", user_name, false, true);
842 if (!ISSET(didvar, DID_USER))
843 sudo_setenv2("USER", user_name, false, true);
844 if (!ISSET(didvar, DID_USERNAME))
845 sudo_setenv2("USERNAME", user_name, false, true);
849 /* If we didn't keep HOME, reset it based on target user. */
850 if (!ISSET(didvar, KEPT_HOME))
854 * Set MAIL to target user in -i mode or if MAIL is not preserved
855 * from user's environment.
857 if (ISSET(sudo_mode, MODE_LOGIN_SHELL) || !ISSET(didvar, KEPT_MAIL)) {
859 if (cp[sizeof(_PATH_MAILDIR) - 2] == '/')
860 easprintf(&cp, "MAIL=%s%s", _PATH_MAILDIR, runas_pw->pw_name);
862 easprintf(&cp, "MAIL=%s/%s", _PATH_MAILDIR, runas_pw->pw_name);
863 sudo_putenv(cp, ISSET(didvar, DID_MAIL), true);
867 * Copy environ entries as long as they don't match env_delete or
870 for (ep = old_envp; *ep; ep++) {
871 /* Skip variables with values beginning with () (bash functions) */
872 if ((cp = strchr(*ep, '=')) != NULL) {
873 if (strncmp(cp, "=() ", 3) == 0)
877 /* Add variable unless it matches a black list. */
878 if (!env_should_delete(*ep)) {
879 if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
881 else if (strncmp(*ep, "PATH=", 5) == 0)
882 SET(didvar, DID_PATH);
883 else if (strncmp(*ep, "TERM=", 5) == 0)
884 SET(didvar, DID_TERM);
885 sudo_putenv(*ep, false, false);
889 /* Replace the PATH envariable with a secure one? */
890 if (def_secure_path && !user_is_exempt()) {
891 sudo_setenv2("PATH", def_secure_path, true, true);
892 SET(didvar, DID_PATH);
896 * Set $USER, $LOGNAME and $USERNAME to target if "set_logname" is not
897 * disabled. We skip this if we are running a login shell (because
898 * they have already been set) or sudoedit (because we want the editor
899 * to find the invoking user's startup files).
901 if (def_set_logname && !ISSET(sudo_mode, MODE_LOGIN_SHELL|MODE_EDIT)) {
902 if (!ISSET(didvar, KEPT_LOGNAME))
903 sudo_setenv2("LOGNAME", runas_pw->pw_name, true, true);
904 if (!ISSET(didvar, KEPT_USER))
905 sudo_setenv2("USER", runas_pw->pw_name, true, true);
906 if (!ISSET(didvar, KEPT_USERNAME))
907 sudo_setenv2("USERNAME", runas_pw->pw_name, true, true);
910 /* Set $HOME to target user if not preserving user's value. */
912 sudo_setenv2("HOME", runas_pw->pw_dir, true, true);
914 /* Provide default values for $TERM and $PATH if they are not set. */
915 if (!ISSET(didvar, DID_TERM))
916 sudo_putenv("TERM=unknown", false, false);
917 if (!ISSET(didvar, DID_PATH))
918 sudo_setenv2("PATH", _PATH_STDPATH, false, true);
920 /* Set PS1 if SUDO_PS1 is set. */
922 sudo_putenv(ps1, true, true);
924 /* Add the SUDO_COMMAND envariable (cmnd + args). */
926 easprintf(&cp, "%s %s", user_cmnd, user_args);
927 sudo_setenv2("SUDO_COMMAND", cp, true, true);
930 sudo_setenv2("SUDO_COMMAND", user_cmnd, true, true);
933 /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */
934 sudo_setenv2("SUDO_USER", user_name, true, true);
935 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) user_uid);
936 sudo_setenv2("SUDO_UID", idbuf, true, true);
937 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) user_gid);
938 sudo_setenv2("SUDO_GID", idbuf, true, true);
940 /* Free old environment. */
945 insert_env_vars(char * const envp[])
952 /* Add user-specified environment variables. */
953 for (ep = envp; *ep != NULL; ep++)
954 sudo_putenv(*ep, true, true);
958 * Validate the list of environment variables passed in on the command
959 * line against env_delete, env_check, and env_keep.
960 * Calls log_fatal() if any specified variables are not allowed.
963 validate_env_vars(char * const env_vars[])
966 char *eq, *bad = NULL;
967 size_t len, blen = 0, bsize = 0;
970 if (env_vars == NULL)
973 /* Add user-specified environment variables. */
974 for (ep = env_vars; *ep != NULL; ep++) {
975 if (def_secure_path && !user_is_exempt() &&
976 strncmp(*ep, "PATH=", 5) == 0) {
978 } else if (def_env_reset) {
979 okvar = env_should_keep(*ep);
981 okvar = !env_should_delete(*ep);
983 if (okvar == false) {
984 /* Not allowed, add to error string, allocating as needed. */
985 if ((eq = strchr(*ep, '=')) != NULL)
987 len = strlen(*ep) + 2;
988 if (blen + len >= bsize) {
991 } while (blen + len >= bsize);
992 bad = erealloc(bad, bsize);
995 strlcat(bad, *ep, bsize);
996 strlcat(bad, ", ", bsize);
1003 bad[blen - 2] = '\0'; /* remove trailing ", " */
1005 _("sorry, you are not allowed to set the following environment variables: %s"), bad);
1012 * Read in /etc/environment ala AIX and Linux.
1013 * Lines may be in either of three formats:
1017 * with an optional "export" prefix so the shell can source the file.
1018 * Invalid lines, blank lines, or lines consisting solely of a comment
1019 * character are skipped.
1022 read_env_file(const char *path, int overwrite)
1025 char *cp, *var, *val;
1026 size_t var_len, val_len;
1028 if ((fp = fopen(path, "r")) == NULL)
1031 while ((var = sudo_parseln(fp)) != NULL) {
1032 /* Skip blank or comment lines */
1036 /* Skip optional "export " */
1037 if (strncmp(var, "export", 6) == 0 && isspace((unsigned char) var[6])) {
1039 while (isspace((unsigned char) *var)) {
1044 /* Must be of the form name=["']value['"] */
1045 for (val = var; *val != '\0' && *val != '='; val++)
1047 if (var == val || *val != '=')
1049 var_len = (size_t)(val - var);
1050 val_len = strlen(++val);
1052 /* Strip leading and trailing single/double quotes */
1053 if ((val[0] == '\'' || val[0] == '\"') && val[0] == val[val_len - 1]) {
1054 val[val_len - 1] = '\0';
1059 cp = emalloc(var_len + 1 + val_len + 1);
1060 memcpy(cp, var, var_len + 1); /* includes '=' */
1061 memcpy(cp + var_len + 1, val, val_len + 1); /* includes NUL */
1063 sudo_putenv(cp, true, overwrite);
1069 init_envtables(void)
1071 struct list_member *cur;
1074 /* Fill in the "env_delete" list. */
1075 for (p = initial_badenv_table; *p; p++) {
1076 cur = ecalloc(1, sizeof(struct list_member));
1077 cur->value = estrdup(*p);
1078 cur->next = def_env_delete;
1079 def_env_delete = cur;
1082 /* Fill in the "env_check" list. */
1083 for (p = initial_checkenv_table; *p; p++) {
1084 cur = ecalloc(1, sizeof(struct list_member));
1085 cur->value = estrdup(*p);
1086 cur->next = def_env_check;
1087 def_env_check = cur;
1090 /* Fill in the "env_keep" list. */
1091 for (p = initial_keepenv_table; *p; p++) {
1092 cur = ecalloc(1, sizeof(struct list_member));
1093 cur->value = estrdup(*p);
1094 cur->next = def_env_keep;
1100 sudoers_hook_getenv(const char *name, char **value, void *closure)
1102 static bool in_progress = false; /* avoid recursion */
1104 if (in_progress || env.envp == NULL)
1105 return SUDO_HOOK_RET_NEXT;
1108 *value = sudo_getenv_nodebug(name);
1109 in_progress = false;
1110 return SUDO_HOOK_RET_STOP;
1114 sudoers_hook_putenv(char *string, void *closure)
1116 static bool in_progress = false; /* avoid recursion */
1118 if (in_progress || env.envp == NULL)
1119 return SUDO_HOOK_RET_NEXT;
1122 sudo_putenv_nodebug(string, true, true);
1123 in_progress = false;
1124 return SUDO_HOOK_RET_STOP;
1128 sudoers_hook_setenv(const char *name, const char *value, int overwrite, void *closure)
1130 static bool in_progress = false; /* avoid recursion */
1132 if (in_progress || env.envp == NULL)
1133 return SUDO_HOOK_RET_NEXT;
1136 sudo_setenv_nodebug(name, value, overwrite);
1137 in_progress = false;
1138 return SUDO_HOOK_RET_STOP;
1142 sudoers_hook_unsetenv(const char *name, void *closure)
1144 static bool in_progress = false; /* avoid recursion */
1146 if (in_progress || env.envp == NULL)
1147 return SUDO_HOOK_RET_NEXT;
1150 sudo_unsetenv_nodebug(name);
1151 in_progress = false;
1152 return SUDO_HOOK_RET_STOP;