#include "config.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#else
# ifdef HAVE_STRINGS_H
# include <strings.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_ERR_H
# include <err.h>
#else
# include "emul/err.h"
#endif
#include <pwd.h>
#include "sudo.h"
#ifndef lint
static const char rcsid[] = "$Sudo: env.c,v 1.42 2004/09/08 15:57:49 millert Exp $";
#endif
#undef DID_TERM
#define DID_TERM 0x01
#undef DID_PATH
#define DID_PATH 0x02
#undef DID_HOME
#define DID_HOME 0x04
#undef DID_SHELL
#define DID_SHELL 0x08
#undef DID_LOGNAME
#define DID_LOGNAME 0x10
#undef DID_USER
#define DID_USER 0x12
#undef VNULL
#define VNULL (VOID *)NULL
char **rebuild_env __P((char **, int, int));
char **zero_env __P((char **));
static void insert_env __P((char *, int));
static char *format_env __P((char *, ...));
static const char *initial_badenv_table[] = {
"IFS",
"CDPATH",
"LOCALDOMAIN",
"RES_OPTIONS",
"HOSTALIASES",
"NLSPATH",
"PATH_LOCALE",
"LD_*",
"_RLD*",
#ifdef __hpux
"SHLIB_PATH",
#endif
#ifdef _AIX
"LIBPATH",
#endif
#ifdef __APPLE__
"DYLD_*",
#endif
#ifdef HAVE_KERB4
"KRB_CONF*",
"KRBCONFDIR",
"KRBTKFILE",
#endif
#ifdef HAVE_KERB5
"KRB5_CONFIG*",
#endif
#ifdef HAVE_SECURID
"VAR_ACE",
"USR_ACE",
"DLC_ACE",
#endif
"TERMINFO",
"TERMINFO_DIRS",
"TERMPATH",
"TERMCAP",
"ENV",
"BASH_ENV",
NULL
};
static const char *initial_checkenv_table[] = {
"LC_*",
"LANG",
"LANGUAGE",
NULL
};
static char **new_environ;
static size_t env_size;
static size_t env_len;
char **
zero_env(envp)
char **envp;
{
static char *newenv[9];
char **ep, **nep = newenv;
char **ne_last = &newenv[(sizeof(newenv) / sizeof(newenv[0])) - 1];
extern char *prev_user;
for (ep = envp; *ep; ep++) {
switch (**ep) {
case 'H':
if (strncmp("HOME=", *ep, 5) == 0)
break;
continue;
case 'K':
if (strncmp("KRB5CCNAME=", *ep, 11) == 0)
break;
continue;
case 'L':
if (strncmp("LOGNAME=", *ep, 8) == 0)
break;
continue;
case 'P':
if (strncmp("PATH=", *ep, 5) == 0) {
user_path = *ep + 5;
break;
}
continue;
case 'S':
if (strncmp("SHELL=", *ep, 6) == 0)
user_shell = *ep + 6;
else if (!user_prompt && strncmp("SUDO_PROMPT=", *ep, 12) == 0)
user_prompt = *ep + 12;
else if (strncmp("SUDO_USER=", *ep, 10) == 0)
prev_user = *ep + 10;
continue;
case 'T':
if (strncmp("TZ=", *ep, 3) == 0)
break;
continue;
case 'U':
if (strncmp("USER=", *ep, 5) == 0)
break;
continue;
default:
continue;
}
for (nep = newenv; *nep; nep++) {
if (**nep == **ep)
break;
}
if (*nep == NULL) {
if (nep < ne_last)
*nep++ = *ep;
else
errx(1, "internal error, attempt to write outside newenv");
}
}
#ifdef HAVE_LDAP
if (nep < ne_last)
*nep++ = "LDAPNOINIT=1";
else
errx(1, "internal error, attempt to write outside newenv");
#endif
return(&newenv[0]);
}
static char *
#ifdef __STDC__
format_env(char *var, ...)
#else
format_env(var, va_alist)
char *var;
va_dcl
#endif
{
char *estring;
char *val;
size_t esize;
va_list ap;
#ifdef __STDC__
va_start(ap, var);
#else
va_start(ap);
#endif
esize = strlen(var) + 2;
while ((val = va_arg(ap, char *)) != NULL)
esize += strlen(val);
va_end(ap);
estring = (char *) emalloc(esize);
if (strlcpy(estring, var, esize) >= esize ||
strlcat(estring, "=", esize) >= esize) {
errx(1, "internal error, format_env() overflow");
}
#ifdef __STDC__
va_start(ap, var);
#else
va_start(ap);
#endif
while ((val = va_arg(ap, char *)) != NULL) {
if (strlcat(estring, val, esize) >= esize)
errx(1, "internal error, format_env() overflow");
}
va_end(ap);
return(estring);
}
static void
insert_env(str, dupcheck)
char *str;
int dupcheck;
{
char **nep;
size_t varlen;
if (env_len + 2 > env_size) {
env_size += 128;
new_environ = erealloc3(new_environ, env_size, sizeof(char *));
}
if (dupcheck) {
varlen = (strchr(str, '=') - str) + 1;
for (nep = new_environ; *nep; nep++) {
if (strncmp(str, *nep, varlen) == 0) {
*nep = str;
return;
}
}
} else
nep = &new_environ[env_len];
env_len++;
*nep++ = str;
*nep = NULL;
}
char **
rebuild_env(envp, sudo_mode, noexec)
char **envp;
int sudo_mode;
int noexec;
{
char **ep, *cp, *ps1;
int okvar, iswild, didvar;
size_t len;
struct list_member *cur;
ps1 = NULL;
didvar = 0;
if (def_env_reset) {
int keepit;
for (ep = envp; *ep; ep++) {
keepit = 0;
if ((cp = strchr(*ep, '=')) != NULL) {
if (strncmp(cp, "=() ", 3) == 0)
continue;
}
for (cur = def_env_keep; cur; cur = cur->next) {
len = strlen(cur->value);
if (cur->value[len - 1] == '*') {
len--;
iswild = 1;
} else
iswild = 0;
if (strncmp(cur->value, *ep, len) == 0 &&
(iswild || (*ep)[len] == '=')) {
if (strncmp(*ep, "TERM=", 5) != 0)
keepit = 1;
break;
}
}
if (strncmp(*ep, "SUDO_PS1=", 8) == 0)
ps1 = *ep + 5;
if (keepit) {
switch (**ep) {
case 'H':
if (strncmp(*ep, "HOME=", 5) == 0)
SET(didvar, DID_HOME);
break;
case 'S':
if (strncmp(*ep, "SHELL=", 6) == 0)
SET(didvar, DID_SHELL);
break;
case 'L':
if (strncmp(*ep, "LOGNAME=", 8) == 0)
SET(didvar, DID_LOGNAME);
break;
case 'U':
if (strncmp(*ep, "USER=", 5) == 0)
SET(didvar, DID_USER);
break;
}
insert_env(*ep, 0);
} else {
if (!ISSET(didvar, DID_TERM) && strncmp(*ep, "TERM=", 5) == 0) {
insert_env(*ep, 0);
SET(didvar, DID_TERM);
} else if (!ISSET(didvar, DID_PATH) && strncmp(*ep, "PATH=", 5) == 0) {
insert_env(*ep, 0);
SET(didvar, DID_PATH);
}
}
}
if (ISSET(sudo_mode, MODE_LOGIN_SHELL)) {
insert_env(format_env("HOME", runas_pw->pw_dir, VNULL), 0);
insert_env(format_env("SHELL", runas_pw->pw_shell, VNULL), 0);
insert_env(format_env("LOGNAME", runas_pw->pw_name, VNULL), 0);
insert_env(format_env("USER", runas_pw->pw_name, VNULL), 0);
} else {
if (!ISSET(didvar, DID_HOME))
insert_env(format_env("HOME", user_dir, VNULL), 0);
if (!ISSET(didvar, DID_SHELL))
insert_env(format_env("SHELL", sudo_user.pw->pw_shell, VNULL), 0);
if (!ISSET(didvar, DID_LOGNAME))
insert_env(format_env("LOGNAME", user_name, VNULL), 0);
if (!ISSET(didvar, DID_USER))
insert_env(format_env("USER", user_name, VNULL), 0);
}
} else {
for (ep = envp; *ep; ep++) {
okvar = 1;
if ((cp = strchr(*ep, '=')) != NULL) {
if (strncmp(cp, "=() ", 3) == 0)
continue;
}
for (cur = def_env_delete; cur && okvar; cur = cur->next) {
len = strlen(cur->value);
if (cur->value[len - 1] == '*') {
len--;
iswild = 1;
} else
iswild = 0;
if (strncmp(cur->value, *ep, len) == 0 &&
(iswild || (*ep)[len] == '=')) {
okvar = 0;
}
}
for (cur = def_env_check; cur && okvar; cur = cur->next) {
len = strlen(cur->value);
if (cur->value[len - 1] == '*') {
len--;
iswild = 1;
} else
iswild = 0;
if (strncmp(cur->value, *ep, len) == 0 &&
(iswild || (*ep)[len] == '=') &&
strpbrk(*ep, "/%")) {
okvar = 0;
}
}
if (okvar) {
if (strncmp(*ep, "SUDO_PS1=", 9) == 0)
ps1 = *ep + 5;
else if (strncmp(*ep, "PATH=", 5) == 0)
SET(didvar, DID_PATH);
else if (strncmp(*ep, "TERM=", 5) == 0)
SET(didvar, DID_TERM);
insert_env(*ep, 0);
}
}
}
if (!ISSET(didvar, DID_TERM))
insert_env("TERM=unknown", 0);
if (!ISSET(didvar, DID_PATH))
insert_env(format_env("PATH", _PATH_DEFPATH, VNULL), 0);
#ifdef SECURE_PATH
insert_env(format_env("PATH", SECURE_PATH, VNULL), 1);
#endif
if (def_set_logname && runas_pw->pw_name) {
insert_env(format_env("LOGNAME", runas_pw->pw_name, VNULL), 1);
insert_env(format_env("USER", runas_pw->pw_name, VNULL), 1);
}
if (ISSET(sudo_mode, MODE_RESET_HOME) && runas_pw->pw_dir)
insert_env(format_env("HOME", runas_pw->pw_dir, VNULL), 1);
if (noexec && def_noexec_file != NULL) {
#if defined(__darwin__) || defined(__APPLE__)
insert_env(format_env("DYLD_INSERT_LIBRARIES", def_noexec_file, VNULL), 1);
insert_env(format_env("DYLD_FORCE_FLAT_NAMESPACE", VNULL), 1);
#else
# if defined(__osf__) || defined(__sgi)
insert_env(format_env("_RLD_LIST", def_noexec_file, ":DEFAULT", VNULL), 1);
# else
insert_env(format_env("LD_PRELOAD", def_noexec_file, VNULL), 1);
# endif
#endif
}
if (ps1)
insert_env(ps1, 1);
if (user_args)
insert_env(format_env("SUDO_COMMAND", user_cmnd, " ", user_args, VNULL), 1);
else
insert_env(format_env("SUDO_COMMAND", user_cmnd, VNULL), 1);
insert_env(format_env("SUDO_USER", user_name, VNULL), 1);
easprintf(&cp, "SUDO_UID=%lu", (unsigned long) user_uid);
insert_env(cp, 1);
easprintf(&cp, "SUDO_GID=%lu", (unsigned long) user_gid);
insert_env(cp, 1);
return(new_environ);
}
void
init_envtables()
{
struct list_member *cur;
const char **p;
for (p = initial_badenv_table; *p; p++) {
cur = emalloc(sizeof(struct list_member));
cur->value = estrdup(*p);
cur->next = def_env_delete;
def_env_delete = cur;
}
for (p = initial_checkenv_table; *p; p++) {
cur = emalloc(sizeof(struct list_member));
cur->value = estrdup(*p);
cur->next = def_env_check;
def_env_check = cur;
}
}