2 * Copyright (c) 2000-2005, 2007-2009
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 */
39 # ifdef HAVE_STRINGS_H
42 #endif /* HAVE_STRING_H */
45 #endif /* HAVE_UNISTD_H */
53 __unused static const char rcsid[] = "$Sudo: env.c,v 1.106 2009/06/23 18:24:42 millert Exp $";
57 * Flags used in rebuild_env()
60 #define DID_TERM 0x0001
62 #define DID_PATH 0x0002
64 #define DID_HOME 0x0004
66 #define DID_SHELL 0x0008
68 #define DID_LOGNAME 0x0010
70 #define DID_USER 0x0020
72 #define DID_USERNAME 0x0040
74 #define DID_MAX 0x00ff
77 #define KEPT_TERM 0x0100
79 #define KEPT_PATH 0x0200
81 #define KEPT_HOME 0x0400
83 #define KEPT_SHELL 0x0800
85 #define KEPT_LOGNAME 0x1000
87 #define KEPT_USER 0x2000
89 #define KEPT_USERNAME 0x4000
91 #define KEPT_MAX 0xff00
94 #define VNULL (void *)NULL
97 char **envp; /* pointer to the new environment */
98 size_t env_size; /* size of new_environ in char **'s */
99 size_t env_len; /* number of slots used, not counting NULL */
105 void rebuild_env __P((int, int));
106 static void sudo_setenv __P((const char *, const char *, int));
107 static void sudo_putenv __P((char *, int, int));
109 extern char **environ; /* global environment */
112 * Copy of the sudo-managed environment.
114 static struct environment env;
117 * Default table of "bad" variables to remove from the environment.
118 * XXX - how to omit TERMCAP if it starts with '/'?
120 static const char *initial_badenv_table[] = {
145 #endif /* HAVE_KERB4 */
149 #endif /* HAVE_KERB5 */
154 #endif /* HAVE_SECURID */
155 "TERMINFO", /* terminfo, exclusive path to terminfo files */
156 "TERMINFO_DIRS", /* terminfo, path(s) to terminfo files */
157 "TERMPATH", /* termcap, path(s) to termcap files */
158 "TERMCAP", /* XXX - only if it starts with '/' */
159 "ENV", /* ksh, file to source before script runs */
160 "BASH_ENV", /* bash, file to source before script runs */
161 "PS4", /* bash, prefix for lines in xtrace mode */
162 "GLOBIGNORE", /* bash, globbing patterns to ignore */
163 "SHELLOPTS", /* bash, extra command line options */
164 "JAVA_TOOL_OPTIONS", /* java, extra command line options */
165 "PERLIO_DEBUG ", /* perl, debugging output file */
166 "PERLLIB", /* perl, search path for modules/includes */
167 "PERL5LIB", /* perl 5, search path for modules/includes */
168 "PERL5OPT", /* perl 5, extra command line options */
169 "PERL5DB", /* perl 5, command used to load debugger */
170 "FPATH", /* ksh, search path for functions */
171 "NULLCMD", /* zsh, command for null file redirection */
172 "READNULLCMD", /* zsh, command for null file redirection */
173 "ZDOTDIR", /* zsh, search path for dot files */
174 "TMPPREFIX", /* zsh, prefix for temporary files */
175 "PYTHONHOME", /* python, module search path */
176 "PYTHONPATH", /* python, search path */
177 "PYTHONINSPECT", /* python, allow inspection */
178 "RUBYLIB", /* ruby, library load path */
179 "RUBYOPT", /* ruby, extra command line options */
184 * Default table of variables to check for '%' and '/' characters.
186 static const char *initial_checkenv_table[] = {
197 * Default table of variables to preserve in the environment.
199 static const char *initial_keepenv_table[] = {
217 * Similar to setenv(3) but operates on sudo's private copy of the environment
218 * (not environ) and it always overwrites. The dupcheck param determines
219 * whether we need to verify that the variable is not already set.
222 sudo_setenv(var, val, dupcheck)
230 esize = strlen(var) + 1 + strlen(val) + 1;
231 estring = emalloc(esize);
233 /* Build environment string and insert it. */
234 if (strlcpy(estring, var, esize) >= esize ||
235 strlcat(estring, "=", esize) >= esize ||
236 strlcat(estring, val, esize) >= esize) {
238 errorx(1, "internal error, sudo_setenv() overflow");
240 sudo_putenv(estring, dupcheck, TRUE);
244 * Version of setenv(3) that uses our own environ pointer.
245 * Will sync with environ as needed.
248 setenv(var, val, overwrite)
257 if (!var || *var == '\0')
261 * POSIX says a var name with '=' is an error but BSD
262 * just ignores the '=' and anything after it.
264 for (cp = var; *cp && *cp != '='; cp++)
266 esize = (size_t)(cp - var) + 2;
268 esize += strlen(val); /* glibc treats a NULL val as "" */
271 /* Allocate and fill in estring. */
272 estring = ep = emalloc(esize);
273 for (cp = var; *cp && *cp != '='; cp++)
277 for (cp = val; *cp; cp++)
282 /* Sync env.envp with environ as needed. */
283 if (env.envp != environ) {
287 for (ep = environ; *ep != NULL; ep++)
290 if (len + 2 > env.env_size) {
292 env.env_size = len + 2 + 128;
293 env.envp = emalloc2(env.env_size, sizeof(char *));
295 memset(env.envp, 0, env.env_size * sizeof(char *));
298 memcpy(env.envp, environ, len * sizeof(char *));
299 env.envp[len] = NULL;
304 if (env.envp[env.env_len] != NULL)
305 errorx(1, "setenv: corrupted envp, len mismatch");
308 sudo_putenv(estring, TRUE, overwrite);
313 * Version of unsetenv(3) that uses our own environ pointer.
314 * Will sync with environ as needed.
327 if (strchr(var, '=') != NULL) {
336 /* Make sure we are operating on the current environment. */
337 /* XXX - this could be optimized to include the search */
338 if (env.envp != environ) {
339 for (ep = environ; *ep != NULL; ep++)
342 if (len + 1 > env.env_size) {
344 env.env_size = len + 1 + 128;
345 env.envp = emalloc2(env.env_size, sizeof(char *));
347 memset(env.envp, 0, env.env_size * sizeof(char *));
350 memcpy(env.envp, environ, len * sizeof(char *));
351 env.envp[len] = NULL;
356 if (env.envp[env.env_len] != NULL)
357 errorx(1, "unsetenv: corrupted envp, len mismatch");
362 for (ep = env.envp; *ep; ep++) {
363 if (strncmp(var, *ep, len) == 0 && (*ep)[len] == '=') {
364 /* Found it; shift remainder + NULL over by one and update len. */
366 (env.env_len - (ep - env.envp)) * sizeof(char *));
371 #ifndef UNSETENV_VOID
377 * Version of putenv(3) that uses our own environ pointer.
378 * Will sync with environ as needed.
382 putenv(const char *string)
388 if (strchr(string, '=') == NULL) {
392 /* Sync env.envp with environ as needed. */
393 if (env.envp != environ) {
397 for (ep = environ; *ep != NULL; ep++)
400 if (len + 2 > env.env_size) {
402 env.env_size = len + 2 + 128;
403 env.envp = emalloc2(env.env_size, sizeof(char *));
405 memset(env.envp, 0, env.env_size * sizeof(char *));
408 memcpy(env.envp, environ, len * sizeof(char *));
409 env.envp[len] = NULL;
414 if (env.envp[env.env_len] != NULL)
415 errorx(1, "putenv: corrupted envp, len mismatch");
418 sudo_putenv((char *)string, TRUE, TRUE);
423 * Similar to putenv(3) but operates on sudo's private copy of the
424 * environment (not environ) and it always overwrites. The dupcheck param
425 * determines whether we need to verify that the variable is not already set.
426 * Will only overwrite an existing variable if overwrite is set.
429 sudo_putenv(str, dupcheck, overwrite)
437 /* Make sure there is room for the new entry plus a NULL. */
438 if (env.env_len + 2 > env.env_size) {
440 env.envp = erealloc3(env.envp, env.env_size, sizeof(char *));
442 memset(env.envp + env.env_len, 0,
443 (env.env_size - env.env_len) * sizeof(char *));
449 if (env.envp[env.env_len] != NULL)
450 errorx(1, "sudo_putenv: corrupted envp, len mismatch");
454 len = (strchr(str, '=') - str) + 1;
455 for (ep = env.envp; *ep; ep++) {
456 if (strncmp(str, *ep, len) == 0) {
463 ep = env.envp + env.env_len;
471 * Check the env_delete blacklist.
472 * Returns TRUE if the variable was found, else false.
475 matches_env_delete(var)
478 struct list_member *cur;
480 int iswild, match = FALSE;
482 /* Skip anything listed in env_delete. */
483 for (cur = def_env_delete; cur; cur = cur->next) {
484 len = strlen(cur->value);
485 /* Deal with '*' wildcard */
486 if (cur->value[len - 1] == '*') {
491 if (strncmp(cur->value, var, len) == 0 &&
492 (iswild || var[len] == '=')) {
501 * Apply the env_check list.
502 * Returns TRUE if the variable is allowed, FALSE if denied
506 matches_env_check(var)
509 struct list_member *cur;
511 int iswild, keepit = -1;
513 for (cur = def_env_check; cur; cur = cur->next) {
514 len = strlen(cur->value);
515 /* Deal with '*' wildcard */
516 if (cur->value[len - 1] == '*') {
521 if (strncmp(cur->value, var, len) == 0 &&
522 (iswild || var[len] == '=')) {
523 keepit = !strpbrk(var, "/%");
531 * Check the env_keep list.
532 * Returns TRUE if the variable is allowed else FALSE.
535 matches_env_keep(var)
538 struct list_member *cur;
540 int iswild, keepit = FALSE;
542 for (cur = def_env_keep; cur; cur = cur->next) {
543 len = strlen(cur->value);
544 /* Deal with '*' wildcard */
545 if (cur->value[len - 1] == '*') {
550 if (strncmp(cur->value, var, len) == 0 &&
551 (iswild || var[len] == '=')) {
560 * Build a new environment and ether clear potentially dangerous
561 * variables from the old one or start with a clean slate.
562 * Also adds sudo-specific variables (SUDO_*).
565 rebuild_env(sudo_mode, noexec)
569 char **old_envp, **ep, *cp, *ps1;
570 char idbuf[MAX_UID_T_LEN];
574 * Either clean out the environment or reset to a safe default.
581 env.envp = emalloc2(env.env_size, sizeof(char *));
583 memset(env.envp, 0, env.env_size * sizeof(char *));
585 if (def_env_reset || ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
586 /* Pull in vars we want to keep from the old environment. */
587 for (ep = environ; *ep; ep++) {
590 /* Skip variables with values beginning with () (bash functions) */
591 if ((cp = strchr(*ep, '=')) != NULL) {
592 if (strncmp(cp, "=() ", 3) == 0)
597 * First check certain variables for '%' and '/' characters.
598 * If no match there, check the keep list.
599 * If nothing matched, we remove it from the environment.
601 keepit = matches_env_check(*ep);
603 keepit = matches_env_keep(*ep);
605 /* For SUDO_PS1 -> PS1 conversion. */
606 if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
610 /* Preserve variable. */
613 if (strncmp(*ep, "HOME=", 5) == 0)
614 SET(didvar, DID_HOME);
617 if (strncmp(*ep, "LOGNAME=", 8) == 0)
618 SET(didvar, DID_LOGNAME);
621 if (strncmp(*ep, "PATH=", 5) == 0)
622 SET(didvar, DID_PATH);
625 if (strncmp(*ep, "SHELL=", 6) == 0)
626 SET(didvar, DID_SHELL);
629 if (strncmp(*ep, "TERM=", 5) == 0)
630 SET(didvar, DID_TERM);
633 if (strncmp(*ep, "USER=", 5) == 0)
634 SET(didvar, DID_USER);
635 if (strncmp(*ep, "USERNAME=", 5) == 0)
636 SET(didvar, DID_USERNAME);
639 sudo_putenv(*ep, FALSE, FALSE);
642 didvar |= didvar << 8; /* convert DID_* to KEPT_* */
645 * Add in defaults. In -i mode these come from the runas user,
646 * otherwise they may be from the user's environment (depends
647 * on sudoers options).
649 if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
650 sudo_setenv("HOME", runas_pw->pw_dir, ISSET(didvar, DID_HOME));
651 sudo_setenv("SHELL", runas_pw->pw_shell, ISSET(didvar, DID_SHELL));
652 sudo_setenv("LOGNAME", runas_pw->pw_name,
653 ISSET(didvar, DID_LOGNAME));
654 sudo_setenv("USER", runas_pw->pw_name, ISSET(didvar, DID_USER));
655 sudo_setenv("USERNAME", runas_pw->pw_name,
656 ISSET(didvar, DID_USERNAME));
658 if (!ISSET(didvar, DID_HOME))
659 sudo_setenv("HOME", user_dir, FALSE);
660 if (!ISSET(didvar, DID_SHELL))
661 sudo_setenv("SHELL", sudo_user.pw->pw_shell, FALSE);
662 if (!ISSET(didvar, DID_LOGNAME))
663 sudo_setenv("LOGNAME", user_name, FALSE);
664 if (!ISSET(didvar, DID_USER))
665 sudo_setenv("USER", user_name, FALSE);
666 if (!ISSET(didvar, DID_USERNAME))
667 sudo_setenv("USERNAME", user_name, FALSE);
671 * Copy environ entries as long as they don't match env_delete or
674 for (ep = environ; *ep; ep++) {
677 /* Skip variables with values beginning with () (bash functions) */
678 if ((cp = strchr(*ep, '=')) != NULL) {
679 if (strncmp(cp, "=() ", 3) == 0)
684 * First check variables against the blacklist in env_delete.
685 * If no match there check for '%' and '/' characters.
687 okvar = matches_env_delete(*ep) != TRUE;
689 okvar = matches_env_check(*ep) != FALSE;
692 if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
694 else if (strncmp(*ep, "PATH=", 5) == 0)
695 SET(didvar, DID_PATH);
696 else if (strncmp(*ep, "TERM=", 5) == 0)
697 SET(didvar, DID_TERM);
698 sudo_putenv(*ep, FALSE, FALSE);
702 /* Replace the PATH envariable with a secure one? */
703 if (def_secure_path && !user_is_exempt()) {
704 sudo_setenv("PATH", def_secure_path, TRUE);
705 SET(didvar, DID_PATH);
708 /* Set $USER, $LOGNAME and $USERNAME to target if "set_logname" is true. */
709 /* XXX - not needed for MODE_LOGIN_SHELL */
710 if (def_set_logname && runas_pw->pw_name) {
711 if (!ISSET(didvar, KEPT_LOGNAME))
712 sudo_setenv("LOGNAME", runas_pw->pw_name, TRUE);
713 if (!ISSET(didvar, KEPT_USER))
714 sudo_setenv("USER", runas_pw->pw_name, TRUE);
715 if (!ISSET(didvar, KEPT_USERNAME))
716 sudo_setenv("USERNAME", runas_pw->pw_name, TRUE);
719 /* Set $HOME for `sudo -H'. Only valid at PERM_FULL_RUNAS. */
720 /* XXX - not needed for MODE_LOGIN_SHELL */
721 if (runas_pw->pw_dir) {
722 if (ISSET(sudo_mode, MODE_RESET_HOME) ||
723 (ISSET(sudo_mode, MODE_RUN) && (def_always_set_home ||
724 (ISSET(sudo_mode, MODE_SHELL) && def_set_home))))
725 sudo_setenv("HOME", runas_pw->pw_dir, TRUE);
728 /* Provide default values for $TERM and $PATH if they are not set. */
729 if (!ISSET(didvar, DID_TERM))
730 sudo_putenv("TERM=unknown", FALSE, FALSE);
731 if (!ISSET(didvar, DID_PATH))
732 sudo_setenv("PATH", _PATH_DEFPATH, FALSE);
735 * Preload a noexec file? For a list of LD_PRELOAD-alikes, see
736 * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
737 * XXX - should prepend to original value, if any
739 if (noexec && def_noexec_file != NULL) {
740 #if defined(__darwin__) || defined(__APPLE__)
741 sudo_setenv("DYLD_INSERT_LIBRARIES", def_noexec_file, TRUE);
742 sudo_setenv("DYLD_FORCE_FLAT_NAMESPACE", "", TRUE);
744 # if defined(__osf__) || defined(__sgi)
745 easprintf(&cp, "%s:DEFAULT", def_noexec_file);
746 sudo_setenv("_RLD_LIST", cp, TRUE);
750 sudo_setenv("LDR_PRELOAD", def_noexec_file, TRUE);
752 sudo_setenv("LD_PRELOAD", def_noexec_file, TRUE);
754 # endif /* __osf__ || __sgi */
755 #endif /* __darwin__ || __APPLE__ */
758 /* Set PS1 if SUDO_PS1 is set. */
760 sudo_putenv(ps1, TRUE, TRUE);
762 /* Add the SUDO_COMMAND envariable (cmnd + args). */
764 easprintf(&cp, "%s %s", user_cmnd, user_args);
765 sudo_setenv("SUDO_COMMAND", cp, TRUE);
768 sudo_setenv("SUDO_COMMAND", user_cmnd, TRUE);
770 /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */
771 sudo_setenv("SUDO_USER", user_name, TRUE);
772 snprintf(idbuf, sizeof(idbuf), "%lu", (unsigned long) user_uid);
773 sudo_setenv("SUDO_UID", idbuf, TRUE);
774 snprintf(idbuf, sizeof(idbuf), "%lu", (unsigned long) user_gid);
775 sudo_setenv("SUDO_GID", idbuf, TRUE);
777 /* Install new environment. */
783 insert_env_vars(env_vars)
784 struct list_member *env_vars;
786 struct list_member *cur;
788 if (env_vars == NULL)
791 /* Add user-specified environment variables. */
792 for (cur = env_vars; cur != NULL; cur = cur->next)
797 * Validate the list of environment variables passed in on the command
798 * line against env_delete, env_check, and env_keep.
799 * Calls log_error() if any specified variables are not allowed.
802 validate_env_vars(env_vars)
803 struct list_member *env_vars;
805 struct list_member *var;
806 char *eq, *bad = NULL;
807 size_t len, blen = 0, bsize = 0;
810 for (var = env_vars; var != NULL; var = var->next) {
811 if (def_secure_path && !user_is_exempt() &&
812 strncmp(var->value, "PATH=", 5) == 0) {
814 } else if (def_env_reset) {
815 okvar = matches_env_check(var->value);
817 okvar = matches_env_keep(var->value);
819 okvar = matches_env_delete(var->value) == FALSE;
821 okvar = matches_env_check(var->value) != FALSE;
823 if (okvar == FALSE) {
824 /* Not allowed, add to error string, allocating as needed. */
825 if ((eq = strchr(var->value, '=')) != NULL)
827 len = strlen(var->value) + 2;
828 if (blen + len >= bsize) {
831 } while (blen + len >= bsize);
832 bad = erealloc(bad, bsize);
835 strlcat(bad, var->value, bsize);
836 strlcat(bad, ", ", bsize);
843 bad[blen - 2] = '\0'; /* remove trailing ", " */
845 "sorry, you are not allowed to set the following environment variables: %s", bad);
852 * Read in /etc/environment ala AIX and Linux.
853 * Lines may be in either of three formats:
857 * with an optional "export" prefix so the shell can source the file.
858 * Invalid lines, blank lines, or lines consisting solely of a comment
859 * character are skipped.
862 read_env_file(path, overwrite)
867 char *cp, *var, *val;
868 size_t var_len, val_len;
870 if ((fp = fopen(path, "r")) == NULL)
873 while ((var = sudo_parseln(fp)) != NULL) {
874 /* Skip blank or comment lines */
878 /* Skip optional "export " */
879 if (strncmp(var, "export", 6) == 0 && isspace((unsigned char) var[6])) {
881 while (isspace((unsigned char) *var)) {
886 /* Must be of the form name=["']value['"] */
887 for (val = var; *val != '\0' && *val != '='; val++)
889 if (var == val || *val != '=')
891 var_len = (size_t)(val - var);
892 val_len = strlen(++val);
894 /* Strip leading and trailing single/double quotes */
895 if ((val[0] == '\'' || val[0] == '\"') && val[0] == val[val_len - 1]) {
896 val[val_len - 1] = '\0';
901 cp = emalloc(var_len + 1 + val_len + 1);
902 memcpy(cp, var, var_len + 1); /* includes '=' */
903 memcpy(cp + var_len + 1, val, val_len + 1); /* includes NUL */
905 sudo_putenv(cp, TRUE, overwrite);
913 struct list_member *cur;
916 /* Fill in the "env_delete" list. */
917 for (p = initial_badenv_table; *p; p++) {
918 cur = emalloc(sizeof(struct list_member));
919 cur->value = estrdup(*p);
920 cur->next = def_env_delete;
921 def_env_delete = cur;
924 /* Fill in the "env_check" list. */
925 for (p = initial_checkenv_table; *p; p++) {
926 cur = emalloc(sizeof(struct list_member));
927 cur->value = estrdup(*p);
928 cur->next = def_env_check;
932 /* Fill in the "env_keep" list. */
933 for (p = initial_keepenv_table; *p; p++) {
934 cur = emalloc(sizeof(struct list_member));
935 cur->value = estrdup(*p);
936 cur->next = def_env_keep;