2 * Copyright (c) 2000-2005, 2007-2008
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 */
51 __unused static const char rcsid[] = "$Sudo: env.c,v 1.94 2008/11/09 14:13:12 millert Exp $";
55 * Flags used in rebuild_env()
58 #define DID_TERM 0x0001
60 #define DID_PATH 0x0002
62 #define DID_HOME 0x0004
64 #define DID_SHELL 0x0008
66 #define DID_LOGNAME 0x0010
68 #define DID_USER 0x0020
70 #define DID_USERNAME 0x0040
72 #define DID_MAX 0x00ff
75 #define KEPT_TERM 0x0100
77 #define KEPT_PATH 0x0200
79 #define KEPT_HOME 0x0400
81 #define KEPT_SHELL 0x0800
83 #define KEPT_LOGNAME 0x1000
85 #define KEPT_USER 0x2000
87 #define KEPT_USERNAME 0x4000
89 #define KEPT_MAX 0xff00
92 #define VNULL (void *)NULL
95 char **envp; /* pointer to the new environment */
96 size_t env_size; /* size of new_environ in char **'s */
97 size_t env_len; /* number of slots used, not counting NULL */
103 void rebuild_env __P((int, int));
104 void sudo_setenv __P((const char *, const char *, int));
105 void sudo_unsetenv __P((const char *));
106 static void _sudo_setenv __P((const char *, const char *, int));
107 static void insert_env __P((char *, int, int));
108 static void sync_env __P((void));
110 extern char **environ; /* global environment */
113 * Copy of the sudo-managed environment.
115 static struct environment env;
118 * Default table of "bad" variables to remove from the environment.
119 * XXX - how to omit TERMCAP if it starts with '/'?
121 static const char *initial_badenv_table[] = {
148 #endif /* HAVE_KERB4 */
152 #endif /* HAVE_KERB5 */
157 #endif /* HAVE_SECURID */
158 "TERMINFO", /* terminfo, exclusive path to terminfo files */
159 "TERMINFO_DIRS", /* terminfo, path(s) to terminfo files */
160 "TERMPATH", /* termcap, path(s) to termcap files */
161 "TERMCAP", /* XXX - only if it starts with '/' */
162 "ENV", /* ksh, file to source before script runs */
163 "BASH_ENV", /* bash, file to source before script runs */
164 "PS4", /* bash, prefix for lines in xtrace mode */
165 "GLOBIGNORE", /* bash, globbing patterns to ignore */
166 "SHELLOPTS", /* bash, extra command line options */
167 "JAVA_TOOL_OPTIONS", /* java, extra command line options */
168 "PERLIO_DEBUG ", /* perl, debugging output file */
169 "PERLLIB", /* perl, search path for modules/includes */
170 "PERL5LIB", /* perl 5, search path for modules/includes */
171 "PERL5OPT", /* perl 5, extra command line options */
172 "PERL5DB", /* perl 5, command used to load debugger */
173 "FPATH", /* ksh, search path for functions */
174 "NULLCMD", /* zsh, command for null file redirection */
175 "READNULLCMD", /* zsh, command for null file redirection */
176 "ZDOTDIR", /* zsh, search path for dot files */
177 "TMPPREFIX", /* zsh, prefix for temporary files */
178 "PYTHONHOME", /* python, module search path */
179 "PYTHONPATH", /* python, search path */
180 "PYTHONINSPECT", /* python, allow inspection */
181 "RUBYLIB", /* ruby, library load path */
182 "RUBYOPT", /* ruby, extra command line options */
187 * Default table of variables to check for '%' and '/' characters.
189 static const char *initial_checkenv_table[] = {
200 * Default table of variables to preserve in the environment.
202 static const char *initial_keepenv_table[] = {
220 * Syncronize our private copy of the environment with what is
229 for (ep = environ; *ep != NULL; ep++)
231 evlen = ep - environ;
232 if (evlen + 1 > env.env_size) {
234 env.env_size = evlen + 1 + 128;
235 env.envp = emalloc2(env.env_size, sizeof(char *));
237 memcpy(env.envp, environ, (evlen + 1) * sizeof(char *));
243 * Similar to setenv(3) but operates on sudo's private copy of the environment
244 * and it always overwrites. The dupcheck param determines whether we need
245 * to verify that the variable is not already set.
248 _sudo_setenv(var, val, dupcheck)
256 esize = strlen(var) + 1 + strlen(val) + 1;
257 estring = emalloc(esize);
259 /* Build environment string and insert it. */
260 if (strlcpy(estring, var, esize) >= esize ||
261 strlcat(estring, "=", esize) >= esize ||
262 strlcat(estring, val, esize) >= esize) {
264 errorx(1, "internal error, sudo_setenv() overflow");
266 insert_env(estring, dupcheck, FALSE);
271 * External version of sudo_setenv() that keeps things in sync with
272 * the environ pointer.
275 sudo_setenv(var, val, dupcheck)
283 /* Make sure we are operating on the current environment. */
284 if (env.envp != environ)
287 esize = strlen(var) + 1 + strlen(val) + 1;
288 estring = emalloc(esize);
290 /* Build environment string and insert it. */
291 if (strlcpy(estring, var, esize) >= esize ||
292 strlcat(estring, "=", esize) >= esize ||
293 strlcat(estring, val, esize) >= esize) {
295 errorx(1, "internal error, sudo_setenv() overflow");
297 insert_env(estring, dupcheck, TRUE);
299 #endif /* HAVE_LDAP */
301 #if defined(HAVE_LDAP) || defined(HAVE_AIXAUTH)
303 * Similar to unsetenv(3) but operates on sudo's private copy of the
313 /* Make sure we are operating on the current environment. */
314 if (env.envp != environ)
317 varlen = strlen(var);
318 for (nep = env.envp; *nep; nep++) {
319 if (strncmp(var, *nep, varlen) == 0 && (*nep)[varlen] == '=') {
320 /* Found it; move everything over by one and update len. */
321 memmove(nep, nep + 1,
322 (env.env_len - (nep - env.envp)) * sizeof(char *));
328 #endif /* HAVE_LDAP || HAVE_AIXAUTH */
331 * Insert str into env.envp, assumes str has an '=' in it.
334 insert_env(str, dupcheck, dosync)
342 /* Make sure there is room for the new entry plus a NULL. */
343 if (env.env_len + 2 > env.env_size) {
345 env.envp = erealloc3(env.envp, env.env_size, sizeof(char *));
351 varlen = (strchr(str, '=') - str) + 1;
353 for (nep = env.envp; *nep; nep++) {
354 if (strncmp(str, *nep, varlen) == 0) {
361 nep = env.envp + env.env_len;
369 * Check the env_delete blacklist.
370 * Returns TRUE if the variable was found, else false.
373 matches_env_delete(var)
376 struct list_member *cur;
378 int iswild, match = FALSE;
380 /* Skip anything listed in env_delete. */
381 for (cur = def_env_delete; cur; cur = cur->next) {
382 len = strlen(cur->value);
383 /* Deal with '*' wildcard */
384 if (cur->value[len - 1] == '*') {
389 if (strncmp(cur->value, var, len) == 0 &&
390 (iswild || var[len] == '=')) {
399 * Apply the env_check list.
400 * Returns TRUE if the variable is allowed, FALSE if denied
404 matches_env_check(var)
407 struct list_member *cur;
409 int iswild, keepit = -1;
411 for (cur = def_env_check; cur; cur = cur->next) {
412 len = strlen(cur->value);
413 /* Deal with '*' wildcard */
414 if (cur->value[len - 1] == '*') {
419 if (strncmp(cur->value, var, len) == 0 &&
420 (iswild || var[len] == '=')) {
421 keepit = !strpbrk(var, "/%");
429 * Check the env_keep list.
430 * Returns TRUE if the variable is allowed else FALSE.
433 matches_env_keep(var)
436 struct list_member *cur;
438 int iswild, keepit = FALSE;
440 for (cur = def_env_keep; cur; cur = cur->next) {
441 len = strlen(cur->value);
442 /* Deal with '*' wildcard */
443 if (cur->value[len - 1] == '*') {
448 if (strncmp(cur->value, var, len) == 0 &&
449 (iswild || var[len] == '=')) {
458 * Build a new environment and ether clear potentially dangerous
459 * variables from the old one or start with a clean slate.
460 * Also adds sudo-specific variables (SUDO_*).
463 rebuild_env(sudo_mode, noexec)
467 char **old_envp, **ep, *cp, *ps1;
468 char idbuf[MAX_UID_T_LEN];
472 * Either clean out the environment or reset to a safe default.
479 env.envp = emalloc2(env.env_size, sizeof(char *));
480 if (def_env_reset || ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
481 /* Pull in vars we want to keep from the old environment. */
482 for (ep = environ; *ep; ep++) {
485 /* Skip variables with values beginning with () (bash functions) */
486 if ((cp = strchr(*ep, '=')) != NULL) {
487 if (strncmp(cp, "=() ", 3) == 0)
492 * First check certain variables for '%' and '/' characters.
493 * If no match there, check the keep list.
494 * If nothing matched, we remove it from the environment.
496 keepit = matches_env_check(*ep);
498 keepit = matches_env_keep(*ep);
500 if (!strncmp (*ep, "DISPLAY=",8)
501 || !strncmp (*ep, "XAUTHORITY=", 11)
502 || !strncmp (*ep, "XAUTHORIZATION=", 15)
503 || !strncmp (*ep, "XAPPLRESDIR=", 12)
504 || !strncmp (*ep, "XFILESEARCHPATH=", 16)
505 || !strncmp (*ep, "XUSERFILESEARCHPATH=", 20)
506 || !strncmp (*ep, "LANG=", 5)
507 || !strncmp (*ep, "LANGUAGE=", 9)
508 || !strncmp (*ep, "LC_", 3))
511 /* For SUDO_PS1 -> PS1 conversion. */
512 if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
516 /* Preserve variable. */
519 if (strncmp(*ep, "HOME=", 5) == 0)
520 SET(didvar, DID_HOME);
523 if (strncmp(*ep, "LOGNAME=", 8) == 0)
524 SET(didvar, DID_LOGNAME);
527 if (strncmp(*ep, "PATH=", 5) == 0)
528 SET(didvar, DID_PATH);
531 if (strncmp(*ep, "SHELL=", 6) == 0)
532 SET(didvar, DID_SHELL);
535 if (strncmp(*ep, "TERM=", 5) == 0)
536 SET(didvar, DID_TERM);
539 if (strncmp(*ep, "USER=", 5) == 0)
540 SET(didvar, DID_USER);
541 if (strncmp(*ep, "USERNAME=", 5) == 0)
542 SET(didvar, DID_USERNAME);
545 insert_env(*ep, FALSE, FALSE);
548 didvar |= didvar << 8; /* convert DID_* to KEPT_* */
551 * Add in defaults. In -i mode these come from the runas user,
552 * otherwise they may be from the user's environment (depends
553 * on sudoers options).
555 if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
556 _sudo_setenv("HOME", runas_pw->pw_dir, ISSET(didvar, DID_HOME));
557 _sudo_setenv("SHELL", runas_pw->pw_shell, ISSET(didvar, DID_SHELL));
558 _sudo_setenv("LOGNAME", runas_pw->pw_name,
559 ISSET(didvar, DID_LOGNAME));
560 _sudo_setenv("USER", runas_pw->pw_name, ISSET(didvar, DID_USER));
561 _sudo_setenv("USERNAME", runas_pw->pw_name,
562 ISSET(didvar, DID_USERNAME));
564 if (!ISSET(didvar, DID_HOME))
565 _sudo_setenv("HOME", user_dir, FALSE);
566 if (!ISSET(didvar, DID_SHELL))
567 _sudo_setenv("SHELL", sudo_user.pw->pw_shell, FALSE);
568 if (!ISSET(didvar, DID_LOGNAME))
569 _sudo_setenv("LOGNAME", user_name, FALSE);
570 if (!ISSET(didvar, DID_USER))
571 _sudo_setenv("USER", user_name, FALSE);
572 if (!ISSET(didvar, DID_USERNAME))
573 _sudo_setenv("USERNAME", user_name, FALSE);
577 * Copy environ entries as long as they don't match env_delete or
580 for (ep = environ; *ep; ep++) {
583 /* Skip variables with values beginning with () (bash functions) */
584 if ((cp = strchr(*ep, '=')) != NULL) {
585 if (strncmp(cp, "=() ", 3) == 0)
590 * First check variables against the blacklist in env_delete.
591 * If no match there check for '%' and '/' characters.
593 okvar = matches_env_delete(*ep) != TRUE;
595 okvar = matches_env_check(*ep) != FALSE;
598 if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
600 else if (strncmp(*ep, "PATH=", 5) == 0)
601 SET(didvar, DID_PATH);
602 else if (strncmp(*ep, "TERM=", 5) == 0)
603 SET(didvar, DID_TERM);
604 insert_env(*ep, FALSE, FALSE);
608 /* Replace the PATH envariable with a secure one? */
609 if (def_secure_path && !user_is_exempt()) {
610 _sudo_setenv("PATH", def_secure_path, TRUE);
611 SET(didvar, DID_PATH);
614 /* Set $USER, $LOGNAME and $USERNAME to target if "set_logname" is true. */
615 /* XXX - not needed for MODE_LOGIN_SHELL */
616 if (def_set_logname && runas_pw->pw_name) {
617 if (!ISSET(didvar, KEPT_LOGNAME))
618 _sudo_setenv("LOGNAME", runas_pw->pw_name, TRUE);
619 if (!ISSET(didvar, KEPT_USER))
620 _sudo_setenv("USER", runas_pw->pw_name, TRUE);
621 if (!ISSET(didvar, KEPT_USERNAME))
622 _sudo_setenv("USERNAME", runas_pw->pw_name, TRUE);
625 /* Set $HOME for `sudo -H'. Only valid at PERM_FULL_RUNAS. */
626 /* XXX - not needed for MODE_LOGIN_SHELL */
627 if (runas_pw->pw_dir) {
628 if (ISSET(sudo_mode, MODE_RESET_HOME) ||
629 (ISSET(sudo_mode, MODE_RUN) && (def_always_set_home ||
630 (ISSET(sudo_mode, MODE_SHELL) && def_set_home))))
631 _sudo_setenv("HOME", runas_pw->pw_dir, TRUE);
634 /* Provide default values for $TERM and $PATH if they are not set. */
635 if (!ISSET(didvar, DID_TERM))
636 insert_env("TERM=unknown", FALSE, FALSE);
637 if (!ISSET(didvar, DID_PATH))
638 _sudo_setenv("PATH", _PATH_DEFPATH, FALSE);
641 * Preload a noexec file? For a list of LD_PRELOAD-alikes, see
642 * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
643 * XXX - should prepend to original value, if any
645 if (noexec && def_noexec_file != NULL) {
646 #if defined(__darwin__) || defined(__APPLE__)
647 _sudo_setenv("DYLD_INSERT_LIBRARIES", def_noexec_file, TRUE);
648 _sudo_setenv("DYLD_FORCE_FLAT_NAMESPACE", "", TRUE);
650 # if defined(__osf__) || defined(__sgi)
651 easprintf(&cp, "%s:DEFAULT", def_noexec_file);
652 _sudo_setenv("_RLD_LIST", cp, TRUE);
656 _sudo_setenv("LDR_PRELOAD", def_noexec_file, TRUE);
658 _sudo_setenv("LD_PRELOAD", def_noexec_file, TRUE);
660 # endif /* __osf__ || __sgi */
661 #endif /* __darwin__ || __APPLE__ */
664 /* Set PS1 if SUDO_PS1 is set. */
666 insert_env(ps1, TRUE, FALSE);
668 /* Add the SUDO_COMMAND envariable (cmnd + args). */
670 easprintf(&cp, "%s %s", user_cmnd, user_args);
671 _sudo_setenv("SUDO_COMMAND", cp, TRUE);
674 _sudo_setenv("SUDO_COMMAND", user_cmnd, TRUE);
676 /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */
677 _sudo_setenv("SUDO_USER", user_name, TRUE);
678 snprintf(idbuf, sizeof(idbuf), "%lu", (unsigned long) user_uid);
679 _sudo_setenv("SUDO_UID", idbuf, TRUE);
680 snprintf(idbuf, sizeof(idbuf), "%lu", (unsigned long) user_gid);
681 _sudo_setenv("SUDO_GID", idbuf, TRUE);
683 /* Install new environment. */
689 insert_env_vars(env_vars)
690 struct list_member *env_vars;
692 struct list_member *cur;
694 if (env_vars == NULL)
697 /* Make sure we are operating on the current environment. */
698 if (env.envp != environ)
701 /* Add user-specified environment variables. */
702 for (cur = env_vars; cur != NULL; cur = cur->next)
703 insert_env(cur->value, TRUE, TRUE);
707 * Validate the list of environment variables passed in on the command
708 * line against env_delete, env_check, and env_keep.
709 * Calls log_error() if any specified variables are not allowed.
712 validate_env_vars(env_vars)
713 struct list_member *env_vars;
715 struct list_member *var;
716 char *eq, *bad = NULL;
717 size_t len, blen = 0, bsize = 0;
720 for (var = env_vars; var != NULL; var = var->next) {
721 if (def_secure_path && !user_is_exempt() &&
722 strncmp(var->value, "PATH=", 5) == 0) {
724 } else if (def_env_reset) {
725 okvar = matches_env_check(var->value);
727 okvar = matches_env_keep(var->value);
729 okvar = matches_env_delete(var->value) == FALSE;
731 okvar = matches_env_check(var->value) != FALSE;
733 if (okvar == FALSE) {
734 /* Not allowed, add to error string, allocating as needed. */
735 if ((eq = strchr(var->value, '=')) != NULL)
737 len = strlen(var->value) + 2;
738 if (blen + len >= bsize) {
741 } while (blen + len >= bsize);
742 bad = erealloc(bad, bsize);
745 strlcat(bad, var->value, bsize);
746 strlcat(bad, ", ", bsize);
753 bad[blen - 2] = '\0'; /* remove trailing ", " */
755 "sorry, you are not allowed to set the following environment variables: %s", bad);
762 * Read in /etc/environment ala AIX and Linux.
763 * Lines are in the form of NAME=VALUE
764 * Invalid lines, blank lines, or lines consisting solely of a comment
765 * character are skipped.
768 read_env_file(path, replace)
775 if ((fp = fopen(path, "r")) == NULL)
778 /* Make sure we are operating on the current environment. */
779 if (env.envp != environ)
782 while ((cp = sudo_parseln(fp)) != NULL) {
783 /* Skip blank or comment lines */
787 /* Must be of the form name=value */
788 if (strchr(cp, '=') == NULL)
791 insert_env(estrdup(cp), replace ? TRUE : -1, TRUE);
799 struct list_member *cur;
802 /* Fill in the "env_delete" list. */
803 for (p = initial_badenv_table; *p; p++) {
804 cur = emalloc(sizeof(struct list_member));
805 cur->value = estrdup(*p);
806 cur->next = def_env_delete;
807 def_env_delete = cur;
810 /* Fill in the "env_check" list. */
811 for (p = initial_checkenv_table; *p; p++) {
812 cur = emalloc(sizeof(struct list_member));
813 cur->value = estrdup(*p);
814 cur->next = def_env_check;
818 /* Fill in the "env_keep" list. */
819 for (p = initial_keepenv_table; *p; p++) {
820 cur = emalloc(sizeof(struct list_member));
821 cur->value = estrdup(*p);
822 cur->next = def_env_keep;