2 * Copyright (c) 2000-2004 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 #include <sys/types.h>
24 #include <sys/param.h>
34 #endif /* STDC_HEADERS */
38 # ifdef HAVE_STRINGS_H
41 #endif /* HAVE_STRING_H */
44 #endif /* HAVE_UNISTD_H */
48 # include "emul/err.h"
49 #endif /* HAVE_ERR_H */
55 static const char rcsid[] = "$Sudo: env.c,v 1.42 2004/09/08 15:57:49 millert Exp $";
59 * Flags used in rebuild_env()
68 #define DID_SHELL 0x08
70 #define DID_LOGNAME 0x10
75 #define VNULL (VOID *)NULL
80 char **rebuild_env __P((char **, int, int));
81 char **zero_env __P((char **));
82 static void insert_env __P((char *, int));
83 static char *format_env __P((char *, ...));
86 * Default table of "bad" variables to remove from the environment.
87 * XXX - how to omit TERMCAP if it starts with '/'?
89 static const char *initial_badenv_table[] = {
114 #endif /* HAVE_KERB4 */
117 #endif /* HAVE_KERB5 */
122 #endif /* HAVE_SECURID */
126 "TERMCAP", /* XXX - only if it starts with '/' */
139 * Default table of variables to check for '%' and '/' characters.
141 static const char *initial_checkenv_table[] = {
149 static char **new_environ; /* Modified copy of the environment */
150 static size_t env_size; /* size of new_environ in char **'s */
151 static size_t env_len; /* number of slots used, not counting NULL */
154 * Zero out environment and replace with a minimal set of KRB5CCNAME
155 * USER, LOGNAME, HOME, TZ, PATH (XXX - should just set path to default)
156 * May set user_path, user_shell, and/or user_prompt as side effects.
162 static char *newenv[9];
163 char **ep, **nep = newenv;
164 char **ne_last = &newenv[(sizeof(newenv) / sizeof(newenv[0])) - 1];
165 extern char *prev_user;
167 for (ep = envp; *ep; ep++) {
170 if (strncmp("HOME=", *ep, 5) == 0)
174 if (strncmp("KRB5CCNAME=", *ep, 11) == 0)
178 if (strncmp("LOGNAME=", *ep, 8) == 0)
182 if (strncmp("PATH=", *ep, 5) == 0) {
184 /* XXX - set to sane default instead of user's? */
189 if (strncmp("SHELL=", *ep, 6) == 0)
190 user_shell = *ep + 6;
191 else if (!user_prompt && strncmp("SUDO_PROMPT=", *ep, 12) == 0)
192 user_prompt = *ep + 12;
193 else if (strncmp("SUDO_USER=", *ep, 10) == 0)
194 prev_user = *ep + 10;
197 if (strncmp("TZ=", *ep, 3) == 0)
201 if (strncmp("USER=", *ep, 5) == 0)
208 /* Deal with multiply defined variables (take first instance) */
209 for (nep = newenv; *nep; nep++) {
217 errx(1, "internal error, attempt to write outside newenv");
223 * Prevent OpenLDAP from reading any user dotfiles
224 * or files in the current directory.
228 *nep++ = "LDAPNOINIT=1";
230 errx(1, "internal error, attempt to write outside newenv");
237 * Given a variable and value, allocate and format an environment string.
241 format_env(char *var, ...)
243 format_env(var, va_alist)
258 esize = strlen(var) + 2;
259 while ((val = va_arg(ap, char *)) != NULL)
260 esize += strlen(val);
262 estring = (char *) emalloc(esize);
264 /* Store variable name and the '=' separator. */
265 if (strlcpy(estring, var, esize) >= esize ||
266 strlcat(estring, "=", esize) >= esize) {
268 errx(1, "internal error, format_env() overflow");
271 /* Now store the variable's value (if any) */
277 while ((val = va_arg(ap, char *)) != NULL) {
278 if (strlcat(estring, val, esize) >= esize)
279 errx(1, "internal error, format_env() overflow");
287 * Insert str into new_environ, assumes str has an '=' in it.
288 * NOTE: no other routines may modify new_environ, env_size, or env_len.
291 insert_env(str, dupcheck)
298 /* Make sure there is room for the new entry plus a NULL. */
299 if (env_len + 2 > env_size) {
301 new_environ = erealloc3(new_environ, env_size, sizeof(char *));
305 varlen = (strchr(str, '=') - str) + 1;
307 for (nep = new_environ; *nep; nep++) {
308 if (strncmp(str, *nep, varlen) == 0) {
314 nep = &new_environ[env_len];
322 * Build a new environment and ether clear potentially dangerous
323 * variables from the old one or start with a clean slate.
324 * Also adds sudo-specific variables (SUDO_*).
327 rebuild_env(envp, sudo_mode, noexec)
332 char **ep, *cp, *ps1;
333 int okvar, iswild, didvar;
335 struct list_member *cur;
338 * Either clean out the environment or reset to a safe default.
345 /* Pull in vars we want to keep from the old environment. */
346 for (ep = envp; *ep; ep++) {
349 /* Skip variables with values beginning with () (bash functions) */
350 if ((cp = strchr(*ep, '=')) != NULL) {
351 if (strncmp(cp, "=() ", 3) == 0)
355 for (cur = def_env_keep; cur; cur = cur->next) {
356 len = strlen(cur->value);
357 /* Deal with '*' wildcard */
358 if (cur->value[len - 1] == '*') {
363 if (strncmp(cur->value, *ep, len) == 0 &&
364 (iswild || (*ep)[len] == '=')) {
365 /* We always preserve TERM, no special treatment needed. */
366 if (strncmp(*ep, "TERM=", 5) != 0)
372 if (!strncmp (*ep, "DISPLAY=",8)
373 || !strncmp (*ep, "XAUTHORITY=", 11)
374 || !strncmp (*ep, "XAUTHORIZATION=", 15)
375 || !strncmp (*ep, "XAPPLRESDIR=", 12)
376 || !strncmp (*ep, "XFILESEARCHPATH=", 16)
377 || !strncmp (*ep, "XUSERFILESEARCHPATH=", 20)
378 || !strncmp (*ep, "LANG=", 5)
379 || !strncmp (*ep, "LANGUAGE=", 9)
380 || !strncmp (*ep, "LC_", 3))
383 /* For SUDO_PS1 -> PS1 conversion. */
384 if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
388 /* Preserve variable. */
391 if (strncmp(*ep, "HOME=", 5) == 0)
392 SET(didvar, DID_HOME);
395 if (strncmp(*ep, "SHELL=", 6) == 0)
396 SET(didvar, DID_SHELL);
399 if (strncmp(*ep, "LOGNAME=", 8) == 0)
400 SET(didvar, DID_LOGNAME);
403 if (strncmp(*ep, "USER=", 5) == 0)
404 SET(didvar, DID_USER);
407 if (strncmp("PATH=", *ep, 5) == 0)
408 SET(didvar,DID_PATH);
413 /* Preserve TERM and PATH, ignore anything else. */
414 if (!ISSET(didvar, DID_TERM) && strncmp(*ep, "TERM=", 5) == 0) {
416 SET(didvar, DID_TERM);
417 } else if (!ISSET(didvar, DID_PATH) && strncmp(*ep, "PATH=", 5) == 0) {
419 SET(didvar, DID_PATH);
425 * Add in defaults. In -i mode these come from the runas user,
426 * otherwise they may be from the user's environment (depends
427 * on sudoers options).
429 if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
430 insert_env(format_env("HOME", runas_pw->pw_dir, VNULL), 0);
431 insert_env(format_env("SHELL", runas_pw->pw_shell, VNULL), 0);
432 insert_env(format_env("LOGNAME", runas_pw->pw_name, VNULL), 0);
433 insert_env(format_env("USER", runas_pw->pw_name, VNULL), 0);
435 if (!ISSET(didvar, DID_HOME))
436 insert_env(format_env("HOME", user_dir, VNULL), 0);
437 if (!ISSET(didvar, DID_SHELL))
438 insert_env(format_env("SHELL", sudo_user.pw->pw_shell, VNULL), 0);
439 if (!ISSET(didvar, DID_LOGNAME))
440 insert_env(format_env("LOGNAME", user_name, VNULL), 0);
441 if (!ISSET(didvar, DID_USER))
442 insert_env(format_env("USER", user_name, VNULL), 0);
446 * Copy envp entries as long as they don't match env_delete or
449 for (ep = envp; *ep; ep++) {
452 /* Skip variables with values beginning with () (bash functions) */
453 if ((cp = strchr(*ep, '=')) != NULL) {
454 if (strncmp(cp, "=() ", 3) == 0)
458 /* Skip anything listed in env_delete. */
459 for (cur = def_env_delete; cur && okvar; cur = cur->next) {
460 len = strlen(cur->value);
461 /* Deal with '*' wildcard */
462 if (cur->value[len - 1] == '*') {
467 if (strncmp(cur->value, *ep, len) == 0 &&
468 (iswild || (*ep)[len] == '=')) {
473 /* Check certain variables for '%' and '/' characters. */
474 for (cur = def_env_check; cur && okvar; cur = cur->next) {
475 len = strlen(cur->value);
476 /* Deal with '*' wildcard */
477 if (cur->value[len - 1] == '*') {
482 if (strncmp(cur->value, *ep, len) == 0 &&
483 (iswild || (*ep)[len] == '=') &&
484 strpbrk(*ep, "/%")) {
490 if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
492 else if (strncmp(*ep, "PATH=", 5) == 0)
493 SET(didvar, DID_PATH);
494 else if (strncmp(*ep, "TERM=", 5) == 0)
495 SET(didvar, DID_TERM);
500 /* Provide default values for $TERM and $PATH if they are not set. */
501 if (!ISSET(didvar, DID_TERM))
502 insert_env("TERM=unknown", 0);
503 if (!ISSET(didvar, DID_PATH))
504 insert_env(format_env("PATH", _PATH_DEFPATH, VNULL), 0);
507 /* Replace the PATH envariable with a secure one. */
508 insert_env(format_env("PATH", SECURE_PATH, VNULL), 1);
511 /* Set $USER and $LOGNAME to target if "set_logname" is true. */
512 if (def_set_logname && runas_pw->pw_name) {
513 insert_env(format_env("LOGNAME", runas_pw->pw_name, VNULL), 1);
514 insert_env(format_env("USER", runas_pw->pw_name, VNULL), 1);
517 /* Set $HOME for `sudo -H'. Only valid at PERM_FULL_RUNAS. */
518 if (ISSET(sudo_mode, MODE_RESET_HOME) && runas_pw->pw_dir)
519 insert_env(format_env("HOME", runas_pw->pw_dir, VNULL), 1);
522 * Preload a noexec file? For a list of LD_PRELOAD-alikes, see
523 * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
524 * XXX - should prepend to original value, if any
526 if (noexec && def_noexec_file != NULL) {
527 #if defined(__darwin__) || defined(__APPLE__)
528 insert_env(format_env("DYLD_INSERT_LIBRARIES", def_noexec_file, VNULL), 1);
529 insert_env(format_env("DYLD_FORCE_FLAT_NAMESPACE", VNULL), 1);
531 # if defined(__osf__) || defined(__sgi)
532 insert_env(format_env("_RLD_LIST", def_noexec_file, ":DEFAULT", VNULL), 1);
534 insert_env(format_env("LD_PRELOAD", def_noexec_file, VNULL), 1);
539 /* Set PS1 if SUDO_PS1 is set. */
543 /* Add the SUDO_COMMAND envariable (cmnd + args). */
545 insert_env(format_env("SUDO_COMMAND", user_cmnd, " ", user_args, VNULL), 1);
547 insert_env(format_env("SUDO_COMMAND", user_cmnd, VNULL), 1);
549 /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */
550 insert_env(format_env("SUDO_USER", user_name, VNULL), 1);
551 easprintf(&cp, "SUDO_UID=%lu", (unsigned long) user_uid);
553 easprintf(&cp, "SUDO_GID=%lu", (unsigned long) user_gid);
562 struct list_member *cur;
565 /* Fill in "env_delete" variable. */
566 for (p = initial_badenv_table; *p; p++) {
567 cur = emalloc(sizeof(struct list_member));
568 cur->value = estrdup(*p);
569 cur->next = def_env_delete;
570 def_env_delete = cur;
573 /* Fill in "env_check" variable. */
574 for (p = initial_checkenv_table; *p; p++) {
575 cur = emalloc(sizeof(struct list_member));
576 cur->value = estrdup(*p);
577 cur->next = def_env_check;