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_LOGIN_CAP_H
46 # include <login_cap.h>
48 # define LOGIN_SETENV 0
50 #endif /* HAVE_LOGIN_CAP_H */
58 * Flags used in rebuild_env()
61 #define DID_TERM 0x0001
63 #define DID_PATH 0x0002
65 #define DID_HOME 0x0004
67 #define DID_SHELL 0x0008
69 #define DID_LOGNAME 0x0010
71 #define DID_USER 0x0020
73 #define DID_USERNAME 0x0040
75 #define DID_MAIL 0x0080
77 #define DID_MAX 0x00ff
80 #define KEPT_TERM 0x0100
82 #define KEPT_PATH 0x0200
84 #define KEPT_HOME 0x0400
86 #define KEPT_SHELL 0x0800
88 #define KEPT_LOGNAME 0x1000
90 #define KEPT_USER 0x2000
92 #define KEPT_USERNAME 0x4000
94 #define KEPT_MAIL 0x8000
96 #define KEPT_MAX 0xff00
99 char * const *old_envp; /* pointer the environment we passed back */
100 char **envp; /* pointer to the new environment */
101 size_t env_size; /* size of new_environ in char **'s */
102 size_t env_len; /* number of slots used, not counting NULL */
106 * Copy of the sudo-managed environment.
108 static struct environment env;
111 * Default table of "bad" variables to remove from the environment.
112 * XXX - how to omit TERMCAP if it starts with '/'?
114 static const char *initial_badenv_table[] = {
138 #endif /* HAVE_KERB5 */
143 #endif /* HAVE_SECURID */
144 "TERMINFO", /* terminfo, exclusive path to terminfo files */
145 "TERMINFO_DIRS", /* terminfo, path(s) to terminfo files */
146 "TERMPATH", /* termcap, path(s) to termcap files */
147 "TERMCAP", /* XXX - only if it starts with '/' */
148 "ENV", /* ksh, file to source before script runs */
149 "BASH_ENV", /* bash, file to source before script runs */
150 "PS4", /* bash, prefix for lines in xtrace mode */
151 "GLOBIGNORE", /* bash, globbing patterns to ignore */
152 "SHELLOPTS", /* bash, extra command line options */
153 "JAVA_TOOL_OPTIONS", /* java, extra command line options */
154 "PERLIO_DEBUG ", /* perl, debugging output file */
155 "PERLLIB", /* perl, search path for modules/includes */
156 "PERL5LIB", /* perl 5, search path for modules/includes */
157 "PERL5OPT", /* perl 5, extra command line options */
158 "PERL5DB", /* perl 5, command used to load debugger */
159 "FPATH", /* ksh, search path for functions */
160 "NULLCMD", /* zsh, command for null file redirection */
161 "READNULLCMD", /* zsh, command for null file redirection */
162 "ZDOTDIR", /* zsh, search path for dot files */
163 "TMPPREFIX", /* zsh, prefix for temporary files */
164 "PYTHONHOME", /* python, module search path */
165 "PYTHONPATH", /* python, search path */
166 "PYTHONINSPECT", /* python, allow inspection */
167 "PYTHONUSERBASE", /* python, per user site-packages directory */
168 "RUBYLIB", /* ruby, library load path */
169 "RUBYOPT", /* ruby, extra command line options */
174 * Default table of variables to check for '%' and '/' characters.
176 static const char *initial_checkenv_table[] = {
187 * Default table of variables to preserve in the environment.
189 static const char *initial_keepenv_table[] = {
205 * Initialize env based on envp.
208 env_init(char * const envp[])
212 debug_decl(env_init, SUDO_DEBUG_ENV)
215 /* Reset to initial state but keep a pointer to what we allocated. */
217 memset(&env, 0, sizeof(env));
220 /* Make private copy of envp. */
221 for (ep = envp; *ep != NULL; ep++)
223 len = (size_t)(ep - envp);
226 env.env_size = len + 1 + 128;
227 env.envp = emalloc2(env.env_size, sizeof(char *));
229 memset(env.envp, 0, env.env_size * sizeof(char *));
231 memcpy(env.envp, envp, len * sizeof(char *));
232 env.envp[len] = '\0';
234 /* Free the old envp we allocated, if any. */
235 if (env.old_envp != NULL)
236 efree((void *)env.old_envp);
243 * Getter for private copy of the environment.
252 * Similar to putenv(3) but operates on sudo's private copy of the
253 * environment (not environ) and it always overwrites. The dupcheck param
254 * determines whether we need to verify that the variable is not already set.
255 * Will only overwrite an existing variable if overwrite is set.
256 * Does not include warnings or debugging to avoid recursive calls.
259 sudo_putenv_nodebug(char *str, bool dupcheck, bool overwrite)
265 /* Make sure there is room for the new entry plus a NULL. */
266 if (env.env_len + 2 > env.env_size) {
268 size_t nsize = env.env_size + 128;
269 nenvp = env.envp ? realloc(env.envp, nsize * sizeof(char *)) :
270 malloc(nsize * sizeof(char *));
276 env.env_size = nsize;
278 memset(env.envp + env.env_len, 0,
279 (env.env_size - env.env_len) * sizeof(char *));
284 if (env.envp[env.env_len] != NULL) {
291 len = (strchr(str, '=') - str) + 1;
292 for (ep = env.envp; !found && *ep != NULL; ep++) {
293 if (strncmp(str, *ep, len) == 0) {
299 /* Prune out duplicate variables. */
300 if (found && overwrite) {
301 while (*ep != NULL) {
302 if (strncmp(str, *ep, len) == 0) {
304 while ((*cur = *(cur + 1)) != NULL)
310 env.env_len = ep - env.envp;
315 ep = env.envp + env.env_len;
324 * Similar to putenv(3) but operates on sudo's private copy of the
325 * environment (not environ) and it always overwrites. The dupcheck param
326 * determines whether we need to verify that the variable is not already set.
327 * Will only overwrite an existing variable if overwrite is set.
330 sudo_putenv(char *str, bool dupcheck, bool overwrite)
333 debug_decl(sudo_putenv, SUDO_DEBUG_ENV)
335 rval = sudo_putenv_nodebug(str, dupcheck, overwrite);
338 if (env.envp[env.env_len] != NULL)
339 errorx(1, _("sudo_putenv: corrupted envp, length mismatch"));
341 errorx(1, _("unable to allocate memory"));
343 debug_return_int(rval);
347 * Similar to setenv(3) but operates on a private copy of the environment.
348 * The dupcheck param determines whether we need to verify that the variable
349 * is not already set.
352 sudo_setenv2(const char *var, const char *val, bool dupcheck, bool overwrite)
356 debug_decl(sudo_setenv2, SUDO_DEBUG_ENV)
358 esize = strlen(var) + 1 + strlen(val) + 1;
359 estring = emalloc(esize);
361 /* Build environment string and insert it. */
362 if (strlcpy(estring, var, esize) >= esize ||
363 strlcat(estring, "=", esize) >= esize ||
364 strlcat(estring, val, esize) >= esize) {
366 errorx(1, _("internal error, sudo_setenv2() overflow"));
368 debug_return_int(sudo_putenv(estring, dupcheck, overwrite));
372 * Similar to setenv(3) but operates on a private copy of the environment.
373 * Does not include warnings or debugging to avoid recursive calls.
376 sudo_setenv_nodebug(const char *var, const char *val, int overwrite)
381 esize = strlen(var) + 1 + strlen(val) + 1;
382 if ((estring = malloc(esize)) == NULL) {
387 /* Build environment string and insert it. */
388 if (strlcpy(estring, var, esize) >= esize ||
389 strlcat(estring, "=", esize) >= esize ||
390 strlcat(estring, val, esize) >= esize) {
395 return sudo_putenv_nodebug(estring, true, overwrite);
399 * Similar to setenv(3) but operates on a private copy of the environment.
402 sudo_setenv(const char *var, const char *val, int overwrite)
405 debug_decl(sudo_setenv, SUDO_DEBUG_ENV)
407 rval = sudo_setenv_nodebug(var, val, overwrite);
410 errorx(1, _("internal error, sudo_setenv() overflow"));
411 errorx(1, _("unable to allocate memory"));
413 debug_return_int(rval);
417 * Similar to unsetenv(3) but operates on a private copy of the environment.
418 * Does not include warnings or debugging to avoid recursive calls.
421 sudo_unsetenv_nodebug(const char *var)
423 char **ep = env.envp;
426 if (ep == NULL || var == NULL || *var == '\0' || strchr(var, '=') != NULL) {
432 while (*ep != NULL) {
433 if (strncmp(var, *ep, len) == 0 && (*ep)[len] == '=') {
434 /* Found it; shift remainder + NULL over by one. */
436 while ((*cur = *(cur + 1)) != NULL)
438 /* Keep going, could be multiple instances of the var. */
447 * Similar to unsetenv(3) but operates on a private copy of the environment.
450 sudo_unsetenv(const char *name)
453 debug_decl(sudo_unsetenv, SUDO_DEBUG_ENV)
455 rval = sudo_unsetenv_nodebug(name);
457 debug_return_int(rval);
461 * Similar to getenv(3) but operates on a private copy of the environment.
462 * Does not include warnings or debugging to avoid recursive calls.
465 sudo_getenv_nodebug(const char *name)
467 char **ep, *val = NULL;
470 if (env.env_len != 0) {
471 /* For BSD compatibility, treat '=' in name like end of string. */
472 while (name[namelen] != '\0' && name[namelen] != '=')
474 for (ep = env.envp; *ep != NULL; ep++) {
475 if (strncmp(*ep, name, namelen) == 0 && (*ep)[namelen] == '=') {
476 val = *ep + namelen + 1;
485 * Similar to getenv(3) but operates on a private copy of the environment.
488 sudo_getenv(const char *name)
491 debug_decl(sudo_getenv, SUDO_DEBUG_ENV)
493 val = sudo_getenv_nodebug(name);
495 debug_return_str(val);
499 * Merge another environment with our private copy.
502 env_merge(char * const envp[], bool overwrite)
505 debug_decl(env_merge, SUDO_DEBUG_ENV)
507 for (ep = envp; *ep != NULL; ep++)
508 sudo_putenv(*ep, true, overwrite);
514 * Check the env_delete blacklist.
515 * Returns true if the variable was found, else false.
518 matches_env_delete(const char *var)
520 struct list_member *cur;
524 debug_decl(matches_env_delete, SUDO_DEBUG_ENV)
526 /* Skip anything listed in env_delete. */
527 for (cur = def_env_delete; cur; cur = cur->next) {
528 len = strlen(cur->value);
529 /* Deal with '*' wildcard */
530 if (cur->value[len - 1] == '*') {
535 if (strncmp(cur->value, var, len) == 0 &&
536 (iswild || var[len] == '=')) {
541 debug_return_bool(match);
545 * Apply the env_check list.
546 * Returns true if the variable is allowed, false if denied
550 matches_env_check(const char *var)
552 struct list_member *cur;
556 debug_decl(matches_env_check, SUDO_DEBUG_ENV)
558 for (cur = def_env_check; cur; cur = cur->next) {
559 len = strlen(cur->value);
560 /* Deal with '*' wildcard */
561 if (cur->value[len - 1] == '*') {
566 if (strncmp(cur->value, var, len) == 0 &&
567 (iswild || var[len] == '=')) {
568 keepit = !strpbrk(var, "/%");
572 debug_return_bool(keepit);
576 * Check the env_keep list.
577 * Returns true if the variable is allowed else false.
580 matches_env_keep(const char *var)
582 struct list_member *cur;
584 bool iswild, keepit = false;
585 debug_decl(matches_env_keep, SUDO_DEBUG_ENV)
587 /* Preserve SHELL variable for "sudo -s". */
588 if (ISSET(sudo_mode, MODE_SHELL) && strncmp(var, "SHELL=", 6) == 0) {
593 for (cur = def_env_keep; cur; cur = cur->next) {
594 len = strlen(cur->value);
595 /* Deal with '*' wildcard */
596 if (cur->value[len - 1] == '*') {
601 if (strncmp(cur->value, var, len) == 0 &&
602 (iswild || var[len] == '=')) {
608 debug_return_bool(keepit);
612 * Look up var in the env_delete and env_check.
613 * Returns true if we should delete the variable, else false.
616 env_should_delete(const char *var)
619 debug_decl(env_should_delete, SUDO_DEBUG_ENV);
621 delete_it = matches_env_delete(var);
623 delete_it = matches_env_check(var) == false;
624 debug_return_bool(delete_it);
628 * Lookup var in the env_check and env_keep lists.
629 * Returns true if the variable is allowed else false.
632 env_should_keep(const char *var)
635 debug_decl(env_should_keep, SUDO_DEBUG_ENV)
637 keepit = matches_env_check(var);
639 keepit = matches_env_keep(var);
641 debug_return_bool(keepit == true);
645 env_update_didvar(const char *ep, unsigned int *didvar)
649 if (strncmp(ep, "HOME=", 5) == 0)
650 SET(*didvar, DID_HOME);
653 if (strncmp(ep, "LOGNAME=", 8) == 0)
654 SET(*didvar, DID_LOGNAME);
657 if (strncmp(ep, "MAIL=", 5) == 0)
658 SET(*didvar, DID_MAIL);
661 if (strncmp(ep, "PATH=", 5) == 0)
662 SET(*didvar, DID_PATH);
665 if (strncmp(ep, "SHELL=", 6) == 0)
666 SET(*didvar, DID_SHELL);
669 if (strncmp(ep, "TERM=", 5) == 0)
670 SET(*didvar, DID_TERM);
673 if (strncmp(ep, "USER=", 5) == 0)
674 SET(*didvar, DID_USER);
675 if (strncmp(ep, "USERNAME=", 5) == 0)
676 SET(*didvar, DID_USERNAME);
682 * Build a new environment and ether clear potentially dangerous
683 * variables from the old one or start with a clean slate.
684 * Also adds sudo-specific variables (SUDO_*).
689 char **old_envp, **ep, *cp, *ps1;
690 char idbuf[MAX_UID_T_LEN];
692 bool reset_home = false;
695 * Either clean out the environment or reset to a safe default.
702 env.envp = emalloc2(env.env_size, sizeof(char *));
704 memset(env.envp, 0, env.env_size * sizeof(char *));
709 /* Reset HOME based on target user if configured to. */
710 if (ISSET(sudo_mode, MODE_RUN)) {
711 if (def_always_set_home ||
712 ISSET(sudo_mode, MODE_RESET_HOME | MODE_LOGIN_SHELL) ||
713 (ISSET(sudo_mode, MODE_SHELL) && def_set_home))
717 if (def_env_reset || ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
719 * If starting with a fresh environment, initialize it based on
720 * /etc/environment or login.conf. For "sudo -i" we want those
721 * variables to override the invoking user's environment, so we
722 * defer reading them until later.
724 if (!ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
725 #ifdef HAVE_LOGIN_CAP_H
726 /* Insert login class environment variables. */
728 login_cap_t *lc = login_getclass(login_class);
730 setusercontext(lc, runas_pw, runas_pw->pw_uid,
731 LOGIN_SETPATH|LOGIN_SETENV);
735 #endif /* HAVE_LOGIN_CAP_H */
736 #if defined(_AIX) || (defined(__linux__) && !defined(HAVE_PAM))
737 /* Insert system-wide environment variables. */
738 read_env_file(_PATH_ENVIRONMENT, true);
740 for (ep = env.envp; *ep; ep++)
741 env_update_didvar(*ep, &didvar);
744 /* Pull in vars we want to keep from the old environment. */
745 for (ep = old_envp; *ep; ep++) {
748 /* Skip variables with values beginning with () (bash functions) */
749 if ((cp = strchr(*ep, '=')) != NULL) {
750 if (strncmp(cp, "=() ", 3) == 0)
755 * Look up the variable in the env_check and env_keep lists.
757 keepit = env_should_keep(*ep);
760 * Do SUDO_PS1 -> PS1 conversion.
761 * This must happen *after* env_should_keep() is called.
763 if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
767 /* Preserve variable. */
768 sudo_putenv(*ep, false, false);
769 env_update_didvar(*ep, &didvar);
772 didvar |= didvar << 8; /* convert DID_* to KEPT_* */
775 * Add in defaults. In -i mode these come from the runas user,
776 * otherwise they may be from the user's environment (depends
777 * on sudoers options).
779 if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
780 sudo_setenv2("SHELL", runas_pw->pw_shell,
781 ISSET(didvar, DID_SHELL), true);
782 sudo_setenv2("LOGNAME", runas_pw->pw_name,
783 ISSET(didvar, DID_LOGNAME), true);
784 sudo_setenv2("USER", runas_pw->pw_name,
785 ISSET(didvar, DID_USER), true);
786 sudo_setenv2("USERNAME", runas_pw->pw_name,
787 ISSET(didvar, DID_USERNAME), true);
789 if (!ISSET(didvar, DID_SHELL))
790 sudo_setenv2("SHELL", sudo_user.pw->pw_shell, false, true);
791 if (!ISSET(didvar, DID_LOGNAME))
792 sudo_setenv2("LOGNAME", user_name, false, true);
793 if (!ISSET(didvar, DID_USER))
794 sudo_setenv2("USER", user_name, false, true);
795 if (!ISSET(didvar, DID_USERNAME))
796 sudo_setenv2("USERNAME", user_name, false, true);
799 /* If we didn't keep HOME, reset it based on target user. */
800 if (!ISSET(didvar, KEPT_HOME))
804 * Set MAIL to target user in -i mode or if MAIL is not preserved
805 * from user's environment.
807 if (ISSET(sudo_mode, MODE_LOGIN_SHELL) || !ISSET(didvar, KEPT_MAIL)) {
809 if (cp[sizeof(_PATH_MAILDIR) - 2] == '/')
810 easprintf(&cp, "MAIL=%s%s", _PATH_MAILDIR, runas_pw->pw_name);
812 easprintf(&cp, "MAIL=%s/%s", _PATH_MAILDIR, runas_pw->pw_name);
813 sudo_putenv(cp, ISSET(didvar, DID_MAIL), true);
817 * Copy environ entries as long as they don't match env_delete or
820 for (ep = old_envp; *ep; ep++) {
821 /* Skip variables with values beginning with () (bash functions) */
822 if ((cp = strchr(*ep, '=')) != NULL) {
823 if (strncmp(cp, "=() ", 3) == 0)
827 /* Add variable unless it matches a black list. */
828 if (!env_should_delete(*ep)) {
829 if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
831 else if (strncmp(*ep, "PATH=", 5) == 0)
832 SET(didvar, DID_PATH);
833 else if (strncmp(*ep, "TERM=", 5) == 0)
834 SET(didvar, DID_TERM);
835 sudo_putenv(*ep, false, false);
839 /* Replace the PATH envariable with a secure one? */
840 if (def_secure_path && !user_is_exempt()) {
841 sudo_setenv2("PATH", def_secure_path, true, true);
842 SET(didvar, DID_PATH);
846 * Set $USER, $LOGNAME and $USERNAME to target if "set_logname" is not
847 * disabled. We skip this if we are running a login shell (because
848 * they have already been set them) or sudoedit (because we want the
849 * editor to find the user's startup files).
851 if (def_set_logname && !ISSET(sudo_mode, MODE_LOGIN_SHELL|MODE_EDIT)) {
852 if (!ISSET(didvar, KEPT_LOGNAME))
853 sudo_setenv2("LOGNAME", runas_pw->pw_name, true, true);
854 if (!ISSET(didvar, KEPT_USER))
855 sudo_setenv2("USER", runas_pw->pw_name, true, true);
856 if (!ISSET(didvar, KEPT_USERNAME))
857 sudo_setenv2("USERNAME", runas_pw->pw_name, true, true);
860 /* Set $HOME to target user if not preserving user's value. */
862 sudo_setenv2("HOME", runas_pw->pw_dir, true, true);
864 /* Provide default values for $TERM and $PATH if they are not set. */
865 if (!ISSET(didvar, DID_TERM))
866 sudo_putenv("TERM=unknown", false, false);
867 if (!ISSET(didvar, DID_PATH))
868 sudo_setenv2("PATH", _PATH_STDPATH, false, true);
870 /* Set PS1 if SUDO_PS1 is set. */
872 sudo_putenv(ps1, true, true);
874 /* Add the SUDO_COMMAND envariable (cmnd + args). */
876 easprintf(&cp, "%s %s", user_cmnd, user_args);
877 sudo_setenv2("SUDO_COMMAND", cp, true, true);
880 sudo_setenv2("SUDO_COMMAND", user_cmnd, true, true);
883 /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */
884 sudo_setenv2("SUDO_USER", user_name, true, true);
885 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) user_uid);
886 sudo_setenv2("SUDO_UID", idbuf, true, true);
887 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) user_gid);
888 sudo_setenv2("SUDO_GID", idbuf, true, true);
890 /* Free old environment. */
895 insert_env_vars(char * const envp[])
902 /* Add user-specified environment variables. */
903 for (ep = envp; *ep != NULL; ep++)
904 sudo_putenv(*ep, true, true);
908 * Validate the list of environment variables passed in on the command
909 * line against env_delete, env_check, and env_keep.
910 * Calls log_fatal() if any specified variables are not allowed.
913 validate_env_vars(char * const env_vars[])
916 char *eq, *bad = NULL;
917 size_t len, blen = 0, bsize = 0;
920 if (env_vars == NULL)
923 /* Add user-specified environment variables. */
924 for (ep = env_vars; *ep != NULL; ep++) {
925 if (def_secure_path && !user_is_exempt() &&
926 strncmp(*ep, "PATH=", 5) == 0) {
928 } else if (def_env_reset) {
929 okvar = env_should_keep(*ep);
931 okvar = !env_should_delete(*ep);
933 if (okvar == false) {
934 /* Not allowed, add to error string, allocating as needed. */
935 if ((eq = strchr(*ep, '=')) != NULL)
937 len = strlen(*ep) + 2;
938 if (blen + len >= bsize) {
941 } while (blen + len >= bsize);
942 bad = erealloc(bad, bsize);
945 strlcat(bad, *ep, bsize);
946 strlcat(bad, ", ", bsize);
953 bad[blen - 2] = '\0'; /* remove trailing ", " */
955 _("sorry, you are not allowed to set the following environment variables: %s"), bad);
962 * Read in /etc/environment ala AIX and Linux.
963 * Lines may be in either of three formats:
967 * with an optional "export" prefix so the shell can source the file.
968 * Invalid lines, blank lines, or lines consisting solely of a comment
969 * character are skipped.
972 read_env_file(const char *path, int overwrite)
975 char *cp, *var, *val;
976 size_t var_len, val_len;
978 if ((fp = fopen(path, "r")) == NULL)
981 while ((var = sudo_parseln(fp)) != NULL) {
982 /* Skip blank or comment lines */
986 /* Skip optional "export " */
987 if (strncmp(var, "export", 6) == 0 && isspace((unsigned char) var[6])) {
989 while (isspace((unsigned char) *var)) {
994 /* Must be of the form name=["']value['"] */
995 for (val = var; *val != '\0' && *val != '='; val++)
997 if (var == val || *val != '=')
999 var_len = (size_t)(val - var);
1000 val_len = strlen(++val);
1002 /* Strip leading and trailing single/double quotes */
1003 if ((val[0] == '\'' || val[0] == '\"') && val[0] == val[val_len - 1]) {
1004 val[val_len - 1] = '\0';
1009 cp = emalloc(var_len + 1 + val_len + 1);
1010 memcpy(cp, var, var_len + 1); /* includes '=' */
1011 memcpy(cp + var_len + 1, val, val_len + 1); /* includes NUL */
1013 sudo_putenv(cp, true, overwrite);
1019 init_envtables(void)
1021 struct list_member *cur;
1024 /* Fill in the "env_delete" list. */
1025 for (p = initial_badenv_table; *p; p++) {
1026 cur = ecalloc(1, sizeof(struct list_member));
1027 cur->value = estrdup(*p);
1028 cur->next = def_env_delete;
1029 def_env_delete = cur;
1032 /* Fill in the "env_check" list. */
1033 for (p = initial_checkenv_table; *p; p++) {
1034 cur = ecalloc(1, sizeof(struct list_member));
1035 cur->value = estrdup(*p);
1036 cur->next = def_env_check;
1037 def_env_check = cur;
1040 /* Fill in the "env_keep" list. */
1041 for (p = initial_keepenv_table; *p; p++) {
1042 cur = ecalloc(1, sizeof(struct list_member));
1043 cur->value = estrdup(*p);
1044 cur->next = def_env_keep;
1050 sudoers_hook_getenv(const char *name, char **value, void *closure)
1052 static bool in_progress = false; /* avoid recursion */
1054 if (in_progress || env.envp == NULL)
1055 return SUDO_HOOK_RET_NEXT;
1058 *value = sudo_getenv_nodebug(name);
1059 in_progress = false;
1060 return SUDO_HOOK_RET_STOP;
1064 sudoers_hook_putenv(char *string, void *closure)
1066 static bool in_progress = false; /* avoid recursion */
1068 if (in_progress || env.envp == NULL)
1069 return SUDO_HOOK_RET_NEXT;
1072 sudo_putenv_nodebug(string, true, true);
1073 in_progress = false;
1074 return SUDO_HOOK_RET_STOP;
1078 sudoers_hook_setenv(const char *name, const char *value, int overwrite, void *closure)
1080 static bool in_progress = false; /* avoid recursion */
1082 if (in_progress || env.envp == NULL)
1083 return SUDO_HOOK_RET_NEXT;
1086 sudo_setenv_nodebug(name, value, overwrite);
1087 in_progress = false;
1088 return SUDO_HOOK_RET_STOP;
1092 sudoers_hook_unsetenv(const char *name, void *closure)
1094 static bool in_progress = false; /* avoid recursion */
1096 if (in_progress || env.envp == NULL)
1097 return SUDO_HOOK_RET_NEXT;
1100 sudo_unsetenv_nodebug(name);
1101 in_progress = false;
1102 return SUDO_HOOK_RET_STOP;