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 */
52 * Flags used in rebuild_env()
55 #define DID_TERM 0x0001
57 #define DID_PATH 0x0002
59 #define DID_HOME 0x0004
61 #define DID_SHELL 0x0008
63 #define DID_LOGNAME 0x0010
65 #define DID_USER 0x0020
67 #define DID_USERNAME 0x0040
69 #define DID_MAIL 0x0080
71 #define DID_MAX 0x00ff
74 #define KEPT_TERM 0x0100
76 #define KEPT_PATH 0x0200
78 #define KEPT_HOME 0x0400
80 #define KEPT_SHELL 0x0800
82 #define KEPT_LOGNAME 0x1000
84 #define KEPT_USER 0x2000
86 #define KEPT_USERNAME 0x4000
88 #define KEPT_MAIL 0x8000
90 #define KEPT_MAX 0xff00
93 char **envp; /* pointer to the new environment */
94 size_t env_size; /* size of new_environ in char **'s */
95 size_t env_len; /* number of slots used, not counting NULL */
101 static void sudo_setenv(const char *, const char *, int);
102 static void sudo_putenv(char *, int, int);
105 * Copy of the sudo-managed environment.
107 static struct environment env;
110 * Default table of "bad" variables to remove from the environment.
111 * XXX - how to omit TERMCAP if it starts with '/'?
113 static const char *initial_badenv_table[] = {
138 #endif /* HAVE_KERB4 */
142 #endif /* HAVE_KERB5 */
147 #endif /* HAVE_SECURID */
148 "TERMINFO", /* terminfo, exclusive path to terminfo files */
149 "TERMINFO_DIRS", /* terminfo, path(s) to terminfo files */
150 "TERMPATH", /* termcap, path(s) to termcap files */
151 "TERMCAP", /* XXX - only if it starts with '/' */
152 "ENV", /* ksh, file to source before script runs */
153 "BASH_ENV", /* bash, file to source before script runs */
154 "PS4", /* bash, prefix for lines in xtrace mode */
155 "GLOBIGNORE", /* bash, globbing patterns to ignore */
156 "SHELLOPTS", /* bash, extra command line options */
157 "JAVA_TOOL_OPTIONS", /* java, extra command line options */
158 "PERLIO_DEBUG ", /* perl, debugging output file */
159 "PERLLIB", /* perl, search path for modules/includes */
160 "PERL5LIB", /* perl 5, search path for modules/includes */
161 "PERL5OPT", /* perl 5, extra command line options */
162 "PERL5DB", /* perl 5, command used to load debugger */
163 "FPATH", /* ksh, search path for functions */
164 "NULLCMD", /* zsh, command for null file redirection */
165 "READNULLCMD", /* zsh, command for null file redirection */
166 "ZDOTDIR", /* zsh, search path for dot files */
167 "TMPPREFIX", /* zsh, prefix for temporary files */
168 "PYTHONHOME", /* python, module search path */
169 "PYTHONPATH", /* python, search path */
170 "PYTHONINSPECT", /* python, allow inspection */
171 "PYTHONUSERBASE", /* python, per user site-packages directory */
172 "RUBYLIB", /* ruby, library load path */
173 "RUBYOPT", /* ruby, extra command line options */
178 * Default table of variables to check for '%' and '/' characters.
180 static const char *initial_checkenv_table[] = {
191 * Default table of variables to preserve in the environment.
193 static const char *initial_keepenv_table[] = {
212 * Initialize env based on envp.
215 env_init(char * const envp[])
220 for (ep = envp; *ep != NULL; ep++)
222 len = (size_t)(ep - envp);
225 env.env_size = len + 1 + 128;
226 env.envp = emalloc2(env.env_size, sizeof(char *));
228 memset(env.envp, 0, env.env_size * sizeof(char *));
230 memcpy(env.envp, envp, len * sizeof(char *));
231 env.envp[len] = '\0';
241 * Similar to setenv(3) but operates on sudo's private copy of the environment
242 * (not environ) and it always overwrites. The dupcheck param determines
243 * whether we need to verify that the variable is not already set.
246 sudo_setenv(const char *var, const char *val, int dupcheck)
251 esize = strlen(var) + 1 + strlen(val) + 1;
252 estring = emalloc(esize);
254 /* Build environment string and insert it. */
255 if (strlcpy(estring, var, esize) >= esize ||
256 strlcat(estring, "=", esize) >= esize ||
257 strlcat(estring, val, esize) >= esize) {
259 errorx(1, _("internal error, sudo_setenv() overflow"));
261 sudo_putenv(estring, dupcheck, TRUE);
265 * Similar to putenv(3) but operates on sudo's private copy of the
266 * environment (not environ) and it always overwrites. The dupcheck param
267 * determines whether we need to verify that the variable is not already set.
268 * Will only overwrite an existing variable if overwrite is set.
271 sudo_putenv(char *str, int dupcheck, int overwrite)
277 /* Make sure there is room for the new entry plus a NULL. */
278 if (env.env_len + 2 > env.env_size) {
280 env.envp = erealloc3(env.envp, env.env_size, sizeof(char *));
282 memset(env.envp + env.env_len, 0,
283 (env.env_size - env.env_len) * sizeof(char *));
288 if (env.envp[env.env_len] != NULL)
289 errorx(1, _("sudo_putenv: corrupted envp, length mismatch"));
293 len = (strchr(str, '=') - str) + 1;
294 for (ep = env.envp; !found && *ep != NULL; ep++) {
295 if (strncmp(str, *ep, len) == 0) {
301 /* Prune out duplicate variables. */
302 if (found && overwrite) {
303 while (*ep != NULL) {
304 if (strncmp(str, *ep, len) == 0) {
306 while ((*cur = *(cur + 1)) != NULL)
312 env.env_len = ep - env.envp;
317 ep = env.envp + env.env_len;
325 * Check the env_delete blacklist.
326 * Returns TRUE if the variable was found, else false.
329 matches_env_delete(const char *var)
331 struct list_member *cur;
333 int iswild, match = FALSE;
335 /* Skip anything listed in env_delete. */
336 for (cur = def_env_delete; cur; cur = cur->next) {
337 len = strlen(cur->value);
338 /* Deal with '*' wildcard */
339 if (cur->value[len - 1] == '*') {
344 if (strncmp(cur->value, var, len) == 0 &&
345 (iswild || var[len] == '=')) {
354 * Apply the env_check list.
355 * Returns TRUE if the variable is allowed, FALSE if denied
359 matches_env_check(const char *var)
361 struct list_member *cur;
363 int iswild, keepit = -1;
365 for (cur = def_env_check; cur; cur = cur->next) {
366 len = strlen(cur->value);
367 /* Deal with '*' wildcard */
368 if (cur->value[len - 1] == '*') {
373 if (strncmp(cur->value, var, len) == 0 &&
374 (iswild || var[len] == '=')) {
375 keepit = !strpbrk(var, "/%");
383 * Check the env_keep list.
384 * Returns TRUE if the variable is allowed else FALSE.
387 matches_env_keep(const char *var)
389 struct list_member *cur;
391 int iswild, keepit = FALSE;
393 /* Preserve SHELL variable for "sudo -s". */
394 if (ISSET(sudo_mode, MODE_SHELL) && strncmp(var, "SHELL=", 6) == 0)
397 for (cur = def_env_keep; cur; cur = cur->next) {
398 len = strlen(cur->value);
399 /* Deal with '*' wildcard */
400 if (cur->value[len - 1] == '*') {
405 if (strncmp(cur->value, var, len) == 0 &&
406 (iswild || var[len] == '=')) {
415 * Build a new environment and ether clear potentially dangerous
416 * variables from the old one or start with a clean slate.
417 * Also adds sudo-specific variables (SUDO_*).
422 char **old_envp, **ep, *cp, *ps1;
423 char idbuf[MAX_UID_T_LEN];
425 int reset_home = FALSE;
428 * Either clean out the environment or reset to a safe default.
435 env.envp = emalloc2(env.env_size, sizeof(char *));
437 memset(env.envp, 0, env.env_size * sizeof(char *));
440 /* Reset HOME based on target user if configured to. */
441 if (ISSET(sudo_mode, MODE_RUN)) {
442 if (def_always_set_home ||
443 ISSET(sudo_mode, MODE_RESET_HOME | MODE_LOGIN_SHELL) ||
444 (ISSET(sudo_mode, MODE_SHELL) && def_set_home))
448 if (def_env_reset || ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
449 /* Pull in vars we want to keep from the old environment. */
450 for (ep = old_envp; *ep; ep++) {
453 /* Skip variables with values beginning with () (bash functions) */
454 if ((cp = strchr(*ep, '=')) != NULL) {
455 if (strncmp(cp, "=() ", 3) == 0)
460 * First check certain variables for '%' and '/' characters.
461 * If no match there, check the keep list.
462 * If nothing matched, we remove it from the environment.
464 keepit = matches_env_check(*ep);
466 keepit = matches_env_keep(*ep);
468 /* For SUDO_PS1 -> PS1 conversion. */
469 if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
473 /* Preserve variable. */
476 if (strncmp(*ep, "HOME=", 5) == 0)
477 SET(didvar, DID_HOME);
480 if (strncmp(*ep, "LOGNAME=", 8) == 0)
481 SET(didvar, DID_LOGNAME);
484 if (strncmp(*ep, "MAIL=", 5) == 0)
485 SET(didvar, DID_MAIL);
488 if (strncmp(*ep, "PATH=", 5) == 0)
489 SET(didvar, DID_PATH);
492 if (strncmp(*ep, "SHELL=", 6) == 0)
493 SET(didvar, DID_SHELL);
496 if (strncmp(*ep, "TERM=", 5) == 0)
497 SET(didvar, DID_TERM);
500 if (strncmp(*ep, "USER=", 5) == 0)
501 SET(didvar, DID_USER);
502 if (strncmp(*ep, "USERNAME=", 5) == 0)
503 SET(didvar, DID_USERNAME);
506 sudo_putenv(*ep, FALSE, FALSE);
509 didvar |= didvar << 8; /* convert DID_* to KEPT_* */
512 * Add in defaults. In -i mode these come from the runas user,
513 * otherwise they may be from the user's environment (depends
514 * on sudoers options).
516 if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
517 sudo_setenv("SHELL", runas_pw->pw_shell, ISSET(didvar, DID_SHELL));
518 sudo_setenv("LOGNAME", runas_pw->pw_name,
519 ISSET(didvar, DID_LOGNAME));
520 sudo_setenv("USER", runas_pw->pw_name, ISSET(didvar, DID_USER));
521 sudo_setenv("USERNAME", runas_pw->pw_name,
522 ISSET(didvar, DID_USERNAME));
524 if (!ISSET(didvar, DID_SHELL))
525 sudo_setenv("SHELL", sudo_user.pw->pw_shell, FALSE);
526 if (!ISSET(didvar, DID_LOGNAME))
527 sudo_setenv("LOGNAME", user_name, FALSE);
528 if (!ISSET(didvar, DID_USER))
529 sudo_setenv("USER", user_name, FALSE);
530 if (!ISSET(didvar, DID_USERNAME))
531 sudo_setenv("USERNAME", user_name, FALSE);
534 /* If we didn't keep HOME, reset it based on target user. */
535 if (!ISSET(didvar, KEPT_HOME))
539 * Set MAIL to target user in -i mode or if MAIL is not preserved
540 * from user's environment.
542 if (ISSET(sudo_mode, MODE_LOGIN_SHELL) || !ISSET(didvar, KEPT_MAIL)) {
544 if (cp[sizeof(_PATH_MAILDIR) - 2] == '/')
545 easprintf(&cp, "MAIL=%s%s", _PATH_MAILDIR, runas_pw->pw_name);
547 easprintf(&cp, "MAIL=%s/%s", _PATH_MAILDIR, runas_pw->pw_name);
548 sudo_putenv(cp, ISSET(didvar, DID_MAIL), TRUE);
552 * Copy environ entries as long as they don't match env_delete or
555 for (ep = old_envp; *ep; ep++) {
558 /* Skip variables with values beginning with () (bash functions) */
559 if ((cp = strchr(*ep, '=')) != NULL) {
560 if (strncmp(cp, "=() ", 3) == 0)
565 * First check variables against the blacklist in env_delete.
566 * If no match there check for '%' and '/' characters.
568 okvar = matches_env_delete(*ep) != TRUE;
570 okvar = matches_env_check(*ep) != FALSE;
573 if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
575 else if (strncmp(*ep, "PATH=", 5) == 0)
576 SET(didvar, DID_PATH);
577 else if (strncmp(*ep, "TERM=", 5) == 0)
578 SET(didvar, DID_TERM);
579 sudo_putenv(*ep, FALSE, FALSE);
583 /* Replace the PATH envariable with a secure one? */
584 if (def_secure_path && !user_is_exempt()) {
585 sudo_setenv("PATH", def_secure_path, TRUE);
586 SET(didvar, DID_PATH);
590 * Set $USER, $LOGNAME and $USERNAME to target if "set_logname" is not
591 * disabled. We skip this if we are running a login shell (because
592 * they have already been set them) or sudoedit (because we want the
593 * editor to find the user's startup files).
595 if (def_set_logname && !ISSET(sudo_mode, MODE_LOGIN_SHELL|MODE_EDIT)) {
596 if (!ISSET(didvar, KEPT_LOGNAME))
597 sudo_setenv("LOGNAME", runas_pw->pw_name, TRUE);
598 if (!ISSET(didvar, KEPT_USER))
599 sudo_setenv("USER", runas_pw->pw_name, TRUE);
600 if (!ISSET(didvar, KEPT_USERNAME))
601 sudo_setenv("USERNAME", runas_pw->pw_name, TRUE);
604 /* Set $HOME to target user if not preserving user's value. */
606 sudo_setenv("HOME", runas_pw->pw_dir, TRUE);
608 /* Provide default values for $TERM and $PATH if they are not set. */
609 if (!ISSET(didvar, DID_TERM))
610 sudo_putenv("TERM=unknown", FALSE, FALSE);
611 if (!ISSET(didvar, DID_PATH))
612 sudo_setenv("PATH", _PATH_STDPATH, FALSE);
614 /* Set PS1 if SUDO_PS1 is set. */
616 sudo_putenv(ps1, TRUE, TRUE);
618 /* Add the SUDO_COMMAND envariable (cmnd + args). */
620 easprintf(&cp, "%s %s", user_cmnd, user_args);
621 sudo_setenv("SUDO_COMMAND", cp, TRUE);
624 sudo_setenv("SUDO_COMMAND", user_cmnd, TRUE);
627 /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */
628 sudo_setenv("SUDO_USER", user_name, TRUE);
629 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) user_uid);
630 sudo_setenv("SUDO_UID", idbuf, TRUE);
631 snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) user_gid);
632 sudo_setenv("SUDO_GID", idbuf, TRUE);
634 /* Free old environment. */
639 insert_env_vars(char * const envp[])
646 /* Add user-specified environment variables. */
647 for (ep = envp; *ep != NULL; ep++)
648 sudo_putenv(*ep, TRUE, TRUE);
652 * Validate the list of environment variables passed in on the command
653 * line against env_delete, env_check, and env_keep.
654 * Calls log_error() if any specified variables are not allowed.
657 validate_env_vars(char * const env_vars[])
660 char *eq, *bad = NULL;
661 size_t len, blen = 0, bsize = 0;
664 if (env_vars == NULL)
667 /* Add user-specified environment variables. */
668 for (ep = env_vars; *ep != NULL; ep++) {
669 if (def_secure_path && !user_is_exempt() &&
670 strncmp(*ep, "PATH=", 5) == 0) {
672 } else if (def_env_reset) {
673 okvar = matches_env_check(*ep);
675 okvar = matches_env_keep(*ep);
677 okvar = matches_env_delete(*ep) == FALSE;
679 okvar = matches_env_check(*ep) != FALSE;
681 if (okvar == FALSE) {
682 /* Not allowed, add to error string, allocating as needed. */
683 if ((eq = strchr(*ep, '=')) != NULL)
685 len = strlen(*ep) + 2;
686 if (blen + len >= bsize) {
689 } while (blen + len >= bsize);
690 bad = erealloc(bad, bsize);
693 strlcat(bad, *ep, bsize);
694 strlcat(bad, ", ", bsize);
701 bad[blen - 2] = '\0'; /* remove trailing ", " */
703 _("sorry, you are not allowed to set the following environment variables: %s"), bad);
710 * Read in /etc/environment ala AIX and Linux.
711 * Lines may be in either of three formats:
715 * with an optional "export" prefix so the shell can source the file.
716 * Invalid lines, blank lines, or lines consisting solely of a comment
717 * character are skipped.
720 read_env_file(const char *path, int overwrite)
723 char *cp, *var, *val;
724 size_t var_len, val_len;
726 if ((fp = fopen(path, "r")) == NULL)
729 while ((var = sudo_parseln(fp)) != NULL) {
730 /* Skip blank or comment lines */
734 /* Skip optional "export " */
735 if (strncmp(var, "export", 6) == 0 && isspace((unsigned char) var[6])) {
737 while (isspace((unsigned char) *var)) {
742 /* Must be of the form name=["']value['"] */
743 for (val = var; *val != '\0' && *val != '='; val++)
745 if (var == val || *val != '=')
747 var_len = (size_t)(val - var);
748 val_len = strlen(++val);
750 /* Strip leading and trailing single/double quotes */
751 if ((val[0] == '\'' || val[0] == '\"') && val[0] == val[val_len - 1]) {
752 val[val_len - 1] = '\0';
757 cp = emalloc(var_len + 1 + val_len + 1);
758 memcpy(cp, var, var_len + 1); /* includes '=' */
759 memcpy(cp + var_len + 1, val, val_len + 1); /* includes NUL */
761 sudo_putenv(cp, TRUE, overwrite);
769 struct list_member *cur;
772 /* Fill in the "env_delete" list. */
773 for (p = initial_badenv_table; *p; p++) {
774 cur = emalloc(sizeof(struct list_member));
775 cur->value = estrdup(*p);
776 cur->next = def_env_delete;
777 def_env_delete = cur;
780 /* Fill in the "env_check" list. */
781 for (p = initial_checkenv_table; *p; p++) {
782 cur = emalloc(sizeof(struct list_member));
783 cur->value = estrdup(*p);
784 cur->next = def_env_check;
788 /* Fill in the "env_keep" list. */
789 for (p = initial_keepenv_table; *p; p++) {
790 cur = emalloc(sizeof(struct list_member));
791 cur->value = estrdup(*p);
792 cur->next = def_env_keep;