2 * Copyright (c) 2000-2005, 2007-2013
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>
34 #endif /* STDC_HEADERS */
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_STRINGS_H */
43 #endif /* HAVE_UNISTD_H */
44 #ifdef HAVE_INTTYPES_H
45 # include <inttypes.h>
47 #ifdef HAVE_LOGIN_CAP_H
48 # include <login_cap.h>
50 # define LOGIN_SETENV 0
52 #endif /* HAVE_LOGIN_CAP_H */
61 * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
62 * could be signed (as it is on SunOS 4.x). This just means that
63 * emalloc2() and erealloc3() cannot allocate huge amounts on such a
64 * platform but that is OK since sudo doesn't need to do so anyway.
68 # define SIZE_MAX SIZE_T_MAX
70 # define SIZE_MAX INT_MAX
71 # endif /* SIZE_T_MAX */
75 * Flags used in rebuild_env()
78 #define DID_TERM 0x0001
80 #define DID_PATH 0x0002
82 #define DID_HOME 0x0004
84 #define DID_SHELL 0x0008
86 #define DID_LOGNAME 0x0010
88 #define DID_USER 0x0020
90 #define DID_USERNAME 0x0040
92 #define DID_MAIL 0x0080
94 #define DID_MAX 0x00ff
97 #define KEPT_TERM 0x0100
99 #define KEPT_PATH 0x0200
101 #define KEPT_HOME 0x0400
103 #define KEPT_SHELL 0x0800
105 #define KEPT_LOGNAME 0x1000
107 #define KEPT_USER 0x2000
109 #define KEPT_USERNAME 0x4000
111 #define KEPT_MAIL 0x8000
113 #define KEPT_MAX 0xff00
116 char * const *old_envp; /* pointer the environment we passed back */
117 char **envp; /* pointer to the new environment */
118 size_t env_size; /* size of new_environ in char **'s */
119 size_t env_len; /* number of slots used, not counting NULL */
123 * Copy of the sudo-managed environment.
125 static struct environment env;
128 * Default table of "bad" variables to remove from the environment.
129 * XXX - how to omit TERMCAP if it starts with '/'?
131 static const char *initial_badenv_table[] = {
155 #endif /* HAVE_KERB5 */
160 #endif /* HAVE_SECURID */
161 "TERMINFO", /* terminfo, exclusive path to terminfo files */
162 "TERMINFO_DIRS", /* terminfo, path(s) to terminfo files */
163 "TERMPATH", /* termcap, path(s) to termcap files */
164 "TERMCAP", /* XXX - only if it starts with '/' */
165 "ENV", /* ksh, file to source before script runs */
166 "BASH_ENV", /* bash, file to source before script runs */
167 "PS4", /* bash, prefix for lines in xtrace mode */
168 "GLOBIGNORE", /* bash, globbing patterns to ignore */
169 "SHELLOPTS", /* bash, extra command line options */
170 "JAVA_TOOL_OPTIONS", /* java, extra command line options */
171 "PERLIO_DEBUG ", /* perl, debugging output file */
172 "PERLLIB", /* perl, search path for modules/includes */
173 "PERL5LIB", /* perl 5, search path for modules/includes */
174 "PERL5OPT", /* perl 5, extra command line options */
175 "PERL5DB", /* perl 5, command used to load debugger */
176 "FPATH", /* ksh, search path for functions */
177 "NULLCMD", /* zsh, command for null file redirection */
178 "READNULLCMD", /* zsh, command for null file redirection */
179 "ZDOTDIR", /* zsh, search path for dot files */
180 "TMPPREFIX", /* zsh, prefix for temporary files */
181 "PYTHONHOME", /* python, module search path */
182 "PYTHONPATH", /* python, search path */
183 "PYTHONINSPECT", /* python, allow inspection */
184 "PYTHONUSERBASE", /* python, per user site-packages directory */
185 "RUBYLIB", /* ruby, library load path */
186 "RUBYOPT", /* ruby, extra command line options */
191 * Default table of variables to check for '%' and '/' characters.
193 static const char *initial_checkenv_table[] = {
204 * Default table of variables to preserve in the environment.
206 static const char *initial_keepenv_table[] = {
222 * Initialize env based on envp.
225 env_init(char * const envp[])
229 debug_decl(env_init, SUDO_DEBUG_ENV)
232 /* Reset to initial state but keep a pointer to what we allocated. */
234 memset(&env, 0, sizeof(env));
237 /* Make private copy of envp. */
238 for (ep = envp; *ep != NULL; ep++)
240 len = (size_t)(ep - envp);
243 env.env_size = len + 1 + 128;
244 env.envp = emalloc2(env.env_size, sizeof(char *));
246 memset(env.envp, 0, env.env_size * sizeof(char *));
248 memcpy(env.envp, envp, len * sizeof(char *));
249 env.envp[len] = NULL;
251 /* Free the old envp we allocated, if any. */
252 if (env.old_envp != NULL)
253 efree((void *)env.old_envp);
260 * Getter for private copy of the environment.
269 * Similar to putenv(3) but operates on sudo's private copy of the
270 * environment (not environ) and it always overwrites. The dupcheck param
271 * determines whether we need to verify that the variable is not already set.
272 * Will only overwrite an existing variable if overwrite is set.
273 * Does not include warnings or debugging to avoid recursive calls.
276 sudo_putenv_nodebug(char *str, bool dupcheck, bool overwrite)
282 /* Make sure there is room for the new entry plus a NULL. */
283 if (env.env_size > 2 && env.env_len > env.env_size - 2) {
287 if (env.env_size > SIZE_MAX - 128) {
288 fatalx_nodebug(_("internal error, %s overflow"),
289 "sudo_putenv_nodebug()");
291 nsize = env.env_size + 128;
292 if (nsize > SIZE_MAX / sizeof(char *)) {
293 fatalx_nodebug(_("internal error, %s overflow"),
294 "sudo_putenv_nodebug()");
296 nenvp = realloc(env.envp, nsize * sizeof(char *));
302 env.env_size = nsize;
304 memset(env.envp + env.env_len, 0,
305 (env.env_size - env.env_len) * sizeof(char *));
310 if (env.envp[env.env_len] != NULL) {
317 len = (strchr(str, '=') - str) + 1;
318 for (ep = env.envp; *ep != NULL; ep++) {
319 if (strncmp(str, *ep, len) == 0) {
326 /* Prune out extra instances of the variable we just overwrote. */
327 if (found && overwrite) {
328 while (*++ep != NULL) {
329 if (strncmp(str, *ep, len) == 0) {
331 while ((*cur = *(cur + 1)) != NULL)
336 env.env_len = ep - env.envp;
341 ep = env.envp + env.env_len;
350 * Similar to putenv(3) but operates on sudo's private copy of the
351 * environment (not environ) and it always overwrites. The dupcheck param
352 * determines whether we need to verify that the variable is not already set.
353 * Will only overwrite an existing variable if overwrite is set.
356 sudo_putenv(char *str, bool dupcheck, bool overwrite)
359 debug_decl(sudo_putenv, SUDO_DEBUG_ENV)
361 sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_putenv: %s", str);
363 rval = sudo_putenv_nodebug(str, dupcheck, overwrite);
366 if (env.envp[env.env_len] != NULL)
367 fatalx(_("sudo_putenv: corrupted envp, length mismatch"));
371 debug_return_int(rval);
375 * Similar to setenv(3) but operates on a private copy of the environment.
376 * The dupcheck param determines whether we need to verify that the variable
377 * is not already set.
380 sudo_setenv2(const char *var, const char *val, bool dupcheck, bool overwrite)
385 debug_decl(sudo_setenv2, SUDO_DEBUG_ENV)
387 esize = strlen(var) + 1 + strlen(val) + 1;
388 estring = emalloc(esize);
390 /* Build environment string and insert it. */
391 if (strlcpy(estring, var, esize) >= esize ||
392 strlcat(estring, "=", esize) >= esize ||
393 strlcat(estring, val, esize) >= esize) {
395 fatalx(_("internal error, %s overflow"), "sudo_setenv2()");
397 rval = sudo_putenv(estring, dupcheck, overwrite);
400 debug_return_int(rval);
404 * Similar to setenv(3) but operates on a private copy of the environment.
407 sudo_setenv(const char *var, const char *val, int overwrite)
409 return sudo_setenv2(var, val, true, (bool)overwrite);
413 * Similar to setenv(3) but operates on a private copy of the environment.
414 * Does not include warnings or debugging to avoid recursive calls.
417 sudo_setenv_nodebug(const char *var, const char *val, int overwrite)
419 char *ep, *estring = NULL;
424 if (var == NULL || *var == '\0') {
430 * POSIX says a var name with '=' is an error but BSD
431 * just ignores the '=' and anything after it.
433 for (cp = var; *cp && *cp != '='; cp++)
435 esize = (size_t)(cp - var) + 2;
437 esize += strlen(val); /* glibc treats a NULL val as "" */
440 /* Allocate and fill in estring. */
441 if ((estring = ep = malloc(esize)) == NULL) {
445 for (cp = var; *cp && *cp != '='; cp++)
449 for (cp = val; *cp; cp++)
454 rval = sudo_putenv_nodebug(estring, true, overwrite);
462 * Similar to unsetenv(3) but operates on a private copy of the environment.
463 * Does not include warnings or debugging to avoid recursive calls.
466 sudo_unsetenv_nodebug(const char *var)
468 char **ep = env.envp;
471 if (ep == NULL || var == NULL || *var == '\0' || strchr(var, '=') != NULL) {
477 while (*ep != NULL) {
478 if (strncmp(var, *ep, len) == 0 && (*ep)[len] == '=') {
479 /* Found it; shift remainder + NULL over by one. */
481 while ((*cur = *(cur + 1)) != NULL)
483 /* Keep going, could be multiple instances of the var. */
492 * Similar to unsetenv(3) but operates on a private copy of the environment.
495 sudo_unsetenv(const char *name)
498 debug_decl(sudo_unsetenv, SUDO_DEBUG_ENV)
500 sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_unsetenv: %s", name);
502 rval = sudo_unsetenv_nodebug(name);
504 debug_return_int(rval);
508 * Similar to getenv(3) but operates on a private copy of the environment.
509 * Does not include warnings or debugging to avoid recursive calls.
512 sudo_getenv_nodebug(const char *name)
514 char **ep, *val = NULL;
517 if (env.env_len != 0) {
518 /* For BSD compatibility, treat '=' in name like end of string. */
519 while (name[namelen] != '\0' && name[namelen] != '=')
521 for (ep = env.envp; *ep != NULL; ep++) {
522 if (strncmp(*ep, name, namelen) == 0 && (*ep)[namelen] == '=') {
523 val = *ep + namelen + 1;
532 * Similar to getenv(3) but operates on a private copy of the environment.
535 sudo_getenv(const char *name)
538 debug_decl(sudo_getenv, SUDO_DEBUG_ENV)
540 sudo_debug_printf(SUDO_DEBUG_INFO, "sudo_getenv: %s", name);
542 val = sudo_getenv_nodebug(name);
544 debug_return_str(val);
548 * Merge another environment with our private copy.
551 env_merge(char * const envp[], bool overwrite)
554 debug_decl(env_merge, SUDO_DEBUG_ENV)
556 for (ep = envp; *ep != NULL; ep++)
557 sudo_putenv(*ep, true, overwrite);
563 * Check the env_delete blacklist.
564 * Returns true if the variable was found, else false.
567 matches_env_delete(const char *var)
569 struct list_member *cur;
573 debug_decl(matches_env_delete, SUDO_DEBUG_ENV)
575 /* Skip anything listed in env_delete. */
576 for (cur = def_env_delete; cur; cur = cur->next) {
577 len = strlen(cur->value);
578 /* Deal with '*' wildcard */
579 if (cur->value[len - 1] == '*') {
584 if (strncmp(cur->value, var, len) == 0 &&
585 (iswild || var[len] == '=')) {
590 debug_return_bool(match);
594 * Apply the env_check list.
595 * Returns true if the variable is allowed, false if denied
599 matches_env_check(const char *var)
601 struct list_member *cur;
605 debug_decl(matches_env_check, SUDO_DEBUG_ENV)
607 for (cur = def_env_check; cur; cur = cur->next) {
608 len = strlen(cur->value);
609 /* Deal with '*' wildcard */
610 if (cur->value[len - 1] == '*') {
615 if (strncmp(cur->value, var, len) == 0 &&
616 (iswild || var[len] == '=')) {
617 keepit = !strpbrk(var, "/%");
621 debug_return_bool(keepit);
625 * Check the env_keep list.
626 * Returns true if the variable is allowed else false.
629 matches_env_keep(const char *var)
631 struct list_member *cur;
633 bool iswild, keepit = false;
634 debug_decl(matches_env_keep, SUDO_DEBUG_ENV)
636 /* Preserve SHELL variable for "sudo -s". */
637 if (ISSET(sudo_mode, MODE_SHELL) && strncmp(var, "SHELL=", 6) == 0) {
642 for (cur = def_env_keep; cur; cur = cur->next) {
643 len = strlen(cur->value);
644 /* Deal with '*' wildcard */
645 if (cur->value[len - 1] == '*') {
650 if (strncmp(cur->value, var, len) == 0 &&
651 (iswild || var[len] == '=')) {
657 debug_return_bool(keepit);
661 * Look up var in the env_delete and env_check.
662 * Returns true if we should delete the variable, else false.
665 env_should_delete(const char *var)
668 debug_decl(env_should_delete, SUDO_DEBUG_ENV);
670 delete_it = matches_env_delete(var);
672 delete_it = matches_env_check(var) == false;
674 sudo_debug_printf(SUDO_DEBUG_INFO, "delete %s: %s",
675 var, delete_it ? "YES" : "NO");
676 debug_return_bool(delete_it);
680 * Lookup var in the env_check and env_keep lists.
681 * Returns true if the variable is allowed else false.
684 env_should_keep(const char *var)
687 debug_decl(env_should_keep, SUDO_DEBUG_ENV)
689 keepit = matches_env_check(var);
691 keepit = matches_env_keep(var);
693 sudo_debug_printf(SUDO_DEBUG_INFO, "keep %s: %s",
694 var, keepit ? "YES" : "NO");
695 debug_return_bool(keepit == true);
699 env_update_didvar(const char *ep, unsigned int *didvar)
703 if (strncmp(ep, "HOME=", 5) == 0)
704 SET(*didvar, DID_HOME);
707 if (strncmp(ep, "LOGNAME=", 8) == 0)
708 SET(*didvar, DID_LOGNAME);
711 if (strncmp(ep, "MAIL=", 5) == 0)
712 SET(*didvar, DID_MAIL);
715 if (strncmp(ep, "PATH=", 5) == 0)
716 SET(*didvar, DID_PATH);
719 if (strncmp(ep, "SHELL=", 6) == 0)
720 SET(*didvar, DID_SHELL);
723 if (strncmp(ep, "TERM=", 5) == 0)
724 SET(*didvar, DID_TERM);
727 if (strncmp(ep, "USER=", 5) == 0)
728 SET(*didvar, DID_USER);
729 if (strncmp(ep, "USERNAME=", 5) == 0)
730 SET(*didvar, DID_USERNAME);
736 * Build a new environment and ether clear potentially dangerous
737 * variables from the old one or start with a clean slate.
738 * Also adds sudo-specific variables (SUDO_*).
743 char **old_envp, **ep, *cp, *ps1;
744 char idbuf[MAX_UID_T_LEN + 1];
746 bool reset_home = false;
749 * Either clean out the environment or reset to a safe default.
756 env.envp = emalloc2(env.env_size, sizeof(char *));
758 memset(env.envp, 0, env.env_size * sizeof(char *));
763 /* Reset HOME based on target user if configured to. */
764 if (ISSET(sudo_mode, MODE_RUN)) {
765 if (def_always_set_home ||
766 ISSET(sudo_mode, MODE_RESET_HOME | MODE_LOGIN_SHELL) ||
767 (ISSET(sudo_mode, MODE_SHELL) && def_set_home))
771 if (def_env_reset || ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
773 * If starting with a fresh environment, initialize it based on
774 * /etc/environment or login.conf. For "sudo -i" we want those
775 * variables to override the invoking user's environment, so we
776 * defer reading them until later.
778 if (!ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
779 #ifdef HAVE_LOGIN_CAP_H
780 /* Insert login class environment variables. */
782 login_cap_t *lc = login_getclass(login_class);
784 setusercontext(lc, runas_pw, runas_pw->pw_uid,
785 LOGIN_SETPATH|LOGIN_SETENV);
789 #endif /* HAVE_LOGIN_CAP_H */
790 #if defined(_AIX) || (defined(__linux__) && !defined(HAVE_PAM))
791 /* Insert system-wide environment variables. */
792 read_env_file(_PATH_ENVIRONMENT, true);
794 for (ep = env.envp; *ep; ep++)
795 env_update_didvar(*ep, &didvar);
798 /* Pull in vars we want to keep from the old environment. */
799 for (ep = old_envp; *ep; ep++) {
802 /* Skip variables with values beginning with () (bash functions) */
803 if ((cp = strchr(*ep, '=')) != NULL) {
804 if (strncmp(cp, "=() ", 3) == 0)
809 * Look up the variable in the env_check and env_keep lists.
811 keepit = env_should_keep(*ep);
814 * Do SUDO_PS1 -> PS1 conversion.
815 * This must happen *after* env_should_keep() is called.
817 if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
821 /* Preserve variable. */
822 sudo_putenv(*ep, false, false);
823 env_update_didvar(*ep, &didvar);
826 didvar |= didvar << 8; /* convert DID_* to KEPT_* */
829 * Add in defaults. In -i mode these come from the runas user,
830 * otherwise they may be from the user's environment (depends
831 * on sudoers options).
833 if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
834 sudo_setenv2("SHELL", runas_pw->pw_shell,
835 ISSET(didvar, DID_SHELL), true);
836 sudo_setenv2("LOGNAME", runas_pw->pw_name,
837 ISSET(didvar, DID_LOGNAME), true);
838 sudo_setenv2("USER", runas_pw->pw_name,
839 ISSET(didvar, DID_USER), true);
840 sudo_setenv2("USERNAME", runas_pw->pw_name,
841 ISSET(didvar, DID_USERNAME), true);
843 if (!ISSET(didvar, DID_SHELL))
844 sudo_setenv2("SHELL", sudo_user.pw->pw_shell, false, true);
845 /* We will set LOGNAME later in the !def_set_logname case. */
846 if (!def_set_logname) {
847 if (!ISSET(didvar, DID_LOGNAME))
848 sudo_setenv2("LOGNAME", user_name, false, true);
849 if (!ISSET(didvar, DID_USER))
850 sudo_setenv2("USER", user_name, false, true);
851 if (!ISSET(didvar, DID_USERNAME))
852 sudo_setenv2("USERNAME", user_name, false, true);
856 /* If we didn't keep HOME, reset it based on target user. */
857 if (!ISSET(didvar, KEPT_HOME))
861 * Set MAIL to target user in -i mode or if MAIL is not preserved
862 * from user's environment.
864 if (ISSET(sudo_mode, MODE_LOGIN_SHELL) || !ISSET(didvar, KEPT_MAIL)) {
866 if (cp[sizeof(_PATH_MAILDIR) - 2] == '/')
867 easprintf(&cp, "MAIL=%s%s", _PATH_MAILDIR, runas_pw->pw_name);
869 easprintf(&cp, "MAIL=%s/%s", _PATH_MAILDIR, runas_pw->pw_name);
870 sudo_putenv(cp, ISSET(didvar, DID_MAIL), true);
874 * Copy environ entries as long as they don't match env_delete or
877 for (ep = old_envp; *ep; ep++) {
878 /* Skip variables with values beginning with () (bash functions) */
879 if ((cp = strchr(*ep, '=')) != NULL) {
880 if (strncmp(cp, "=() ", 3) == 0)
884 /* Add variable unless it matches a black list. */
885 if (!env_should_delete(*ep)) {
886 if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
888 else if (strncmp(*ep, "PATH=", 5) == 0)
889 SET(didvar, DID_PATH);
890 else if (strncmp(*ep, "TERM=", 5) == 0)
891 SET(didvar, DID_TERM);
892 sudo_putenv(*ep, false, false);
896 /* Replace the PATH envariable with a secure one? */
897 if (def_secure_path && !user_is_exempt()) {
898 sudo_setenv2("PATH", def_secure_path, true, true);
899 SET(didvar, DID_PATH);
903 * Set $USER, $LOGNAME and $USERNAME to target if "set_logname" is not
904 * disabled. We skip this if we are running a login shell (because
905 * they have already been set) or sudoedit (because we want the editor
906 * to find the invoking user's startup files).
908 if (def_set_logname && !ISSET(sudo_mode, MODE_LOGIN_SHELL|MODE_EDIT)) {
909 if (!ISSET(didvar, KEPT_LOGNAME))
910 sudo_setenv2("LOGNAME", runas_pw->pw_name, true, true);
911 if (!ISSET(didvar, KEPT_USER))
912 sudo_setenv2("USER", runas_pw->pw_name, true, true);
913 if (!ISSET(didvar, KEPT_USERNAME))
914 sudo_setenv2("USERNAME", runas_pw->pw_name, true, true);
917 /* Set $HOME to target user if not preserving user's value. */
919 sudo_setenv2("HOME", runas_pw->pw_dir, true, true);
921 /* Provide default values for $TERM and $PATH if they are not set. */
922 if (!ISSET(didvar, DID_TERM))
923 sudo_putenv("TERM=unknown", false, false);
924 if (!ISSET(didvar, DID_PATH))
925 sudo_setenv2("PATH", _PATH_STDPATH, false, true);
927 /* Set PS1 if SUDO_PS1 is set. */
929 sudo_putenv(ps1, true, true);
931 /* Add the SUDO_COMMAND envariable (cmnd + args). */
933 easprintf(&cp, "%s %s", user_cmnd, user_args);
934 sudo_setenv2("SUDO_COMMAND", cp, true, true);
937 sudo_setenv2("SUDO_COMMAND", user_cmnd, true, true);
940 /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */
941 sudo_setenv2("SUDO_USER", user_name, true, true);
942 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) user_uid);
943 sudo_setenv2("SUDO_UID", idbuf, true, true);
944 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) user_gid);
945 sudo_setenv2("SUDO_GID", idbuf, true, true);
947 /* Free old environment. */
952 insert_env_vars(char * const envp[])
959 /* Add user-specified environment variables. */
960 for (ep = envp; *ep != NULL; ep++)
961 sudo_putenv(*ep, true, true);
965 * Validate the list of environment variables passed in on the command
966 * line against env_delete, env_check, and env_keep.
967 * Calls log_fatal() if any specified variables are not allowed.
970 validate_env_vars(char * const env_vars[])
973 char *eq, *bad = NULL;
974 size_t len, blen = 0, bsize = 0;
977 if (env_vars == NULL)
980 /* Add user-specified environment variables. */
981 for (ep = env_vars; *ep != NULL; ep++) {
982 if (def_secure_path && !user_is_exempt() &&
983 strncmp(*ep, "PATH=", 5) == 0) {
985 } else if (def_env_reset) {
986 okvar = env_should_keep(*ep);
988 okvar = !env_should_delete(*ep);
990 if (okvar == false) {
991 /* Not allowed, add to error string, allocating as needed. */
992 if ((eq = strchr(*ep, '=')) != NULL)
994 len = strlen(*ep) + 2;
995 if (blen + len >= bsize) {
998 } while (blen + len >= bsize);
999 bad = erealloc(bad, bsize);
1002 strlcat(bad, *ep, bsize);
1003 strlcat(bad, ", ", bsize);
1010 bad[blen - 2] = '\0'; /* remove trailing ", " */
1012 N_("sorry, you are not allowed to set the following environment variables: %s"), bad);
1019 * Read in /etc/environment ala AIX and Linux.
1020 * Lines may be in either of three formats:
1024 * with an optional "export" prefix so the shell can source the file.
1025 * Invalid lines, blank lines, or lines consisting solely of a comment
1026 * character are skipped.
1029 read_env_file(const char *path, int overwrite)
1032 char *cp, *var, *val, *line = NULL;
1033 size_t var_len, val_len, linesize = 0;
1035 if ((fp = fopen(path, "r")) == NULL)
1038 while (sudo_parseln(&line, &linesize, NULL, fp) != -1) {
1039 /* Skip blank or comment lines */
1040 if (*(var = line) == '\0')
1043 /* Skip optional "export " */
1044 if (strncmp(var, "export", 6) == 0 && isspace((unsigned char) var[6])) {
1046 while (isspace((unsigned char) *var)) {
1051 /* Must be of the form name=["']value['"] */
1052 for (val = var; *val != '\0' && *val != '='; val++)
1054 if (var == val || *val != '=')
1056 var_len = (size_t)(val - var);
1057 val_len = strlen(++val);
1059 /* Strip leading and trailing single/double quotes */
1060 if ((val[0] == '\'' || val[0] == '\"') && val[0] == val[val_len - 1]) {
1061 val[val_len - 1] = '\0';
1066 cp = emalloc(var_len + 1 + val_len + 1);
1067 memcpy(cp, var, var_len + 1); /* includes '=' */
1068 memcpy(cp + var_len + 1, val, val_len + 1); /* includes NUL */
1070 sudo_putenv(cp, true, overwrite);
1077 init_envtables(void)
1079 struct list_member *cur;
1082 /* Fill in the "env_delete" list. */
1083 for (p = initial_badenv_table; *p; p++) {
1084 cur = ecalloc(1, sizeof(struct list_member));
1085 cur->value = estrdup(*p);
1086 cur->next = def_env_delete;
1087 def_env_delete = cur;
1090 /* Fill in the "env_check" list. */
1091 for (p = initial_checkenv_table; *p; p++) {
1092 cur = ecalloc(1, sizeof(struct list_member));
1093 cur->value = estrdup(*p);
1094 cur->next = def_env_check;
1095 def_env_check = cur;
1098 /* Fill in the "env_keep" list. */
1099 for (p = initial_keepenv_table; *p; p++) {
1100 cur = ecalloc(1, sizeof(struct list_member));
1101 cur->value = estrdup(*p);
1102 cur->next = def_env_keep;
1108 sudoers_hook_getenv(const char *name, char **value, void *closure)
1110 static bool in_progress = false; /* avoid recursion */
1112 if (in_progress || env.envp == NULL)
1113 return SUDO_HOOK_RET_NEXT;
1117 /* Hack to make GNU gettext() find the sudoers locale when needed. */
1118 if (*name == 'L' && sudoers_getlocale() == SUDOERS_LOCALE_SUDOERS) {
1119 if (strcmp(name, "LANGUAGE") == 0 || strcmp(name, "LANG") == 0) {
1123 if (strcmp(name, "LC_ALL") == 0 || strcmp(name, "LC_MESSAGES") == 0) {
1124 *value = def_sudoers_locale;
1129 *value = sudo_getenv_nodebug(name);
1131 in_progress = false;
1132 return SUDO_HOOK_RET_STOP;
1136 sudoers_hook_putenv(char *string, void *closure)
1138 static bool in_progress = false; /* avoid recursion */
1140 if (in_progress || env.envp == NULL)
1141 return SUDO_HOOK_RET_NEXT;
1144 sudo_putenv_nodebug(string, true, true);
1145 in_progress = false;
1146 return SUDO_HOOK_RET_STOP;
1150 sudoers_hook_setenv(const char *name, const char *value, int overwrite, void *closure)
1152 static bool in_progress = false; /* avoid recursion */
1154 if (in_progress || env.envp == NULL)
1155 return SUDO_HOOK_RET_NEXT;
1158 sudo_setenv_nodebug(name, value, overwrite);
1159 in_progress = false;
1160 return SUDO_HOOK_RET_STOP;
1164 sudoers_hook_unsetenv(const char *name, void *closure)
1166 static bool in_progress = false; /* avoid recursion */
1168 if (in_progress || env.envp == NULL)
1169 return SUDO_HOOK_RET_NEXT;
1172 sudo_unsetenv_nodebug(name);
1173 in_progress = false;
1174 return SUDO_HOOK_RET_STOP;