2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1989 - 1992, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 1999, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2003 - 2005, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2008 - 2009, Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2009-04-28 01:37:59 +05:30
|
|
|
#include <assert.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-02-20 01:10:16 +05:30
|
|
|
|
|
|
|
#include "alloc.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* NEWENVP_STEP must be a power of two. This is the number
|
|
|
|
* of (char *) pointers to allocate at a time, to avoid using
|
|
|
|
* realloc() too often.
|
2007-10-07 17:15:23 +05:30
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
#define NEWENVP_STEP 16
|
|
|
|
size_t newenvc = 0;
|
2009-04-23 23:03:21 +05:30
|
|
|
/*@null@*/char **newenvp = NULL;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2022-08-05 21:10:31 +05:30
|
|
|
static const char *const forbid[] = {
|
2007-10-07 17:14:02 +05:30
|
|
|
"_RLD_=",
|
2007-10-07 17:15:23 +05:30
|
|
|
"BASH_ENV=", /* GNU creeping featurism strikes again... */
|
2007-10-07 17:14:02 +05:30
|
|
|
"ENV=",
|
|
|
|
"HOME=",
|
|
|
|
"IFS=",
|
|
|
|
"KRB_CONF=",
|
2007-10-07 17:15:23 +05:30
|
|
|
"LD_", /* anything with the LD_ prefix */
|
2007-10-07 17:14:02 +05:30
|
|
|
"LIBPATH=",
|
|
|
|
"MAIL=",
|
|
|
|
"NLSPATH=",
|
|
|
|
"PATH=",
|
|
|
|
"SHELL=",
|
|
|
|
"SHLIB_PATH=",
|
2023-02-01 07:20:14 +05:30
|
|
|
NULL
|
2007-10-07 17:14:02 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
/* these are allowed, but with no slashes inside
|
|
|
|
(to work around security problems in GNU gettext) */
|
2022-08-05 21:10:31 +05:30
|
|
|
static const char *const noslash[] = {
|
2007-10-07 17:14:02 +05:30
|
|
|
"LANG=",
|
|
|
|
"LANGUAGE=",
|
2007-10-07 17:15:23 +05:30
|
|
|
"LC_", /* anything with the LC_ prefix */
|
2023-02-01 07:20:14 +05:30
|
|
|
NULL
|
2007-10-07 17:14:02 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initenv() must be called once before using addenv().
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
void initenv (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
newenvp = (char **) xmalloc (NEWENVP_STEP * sizeof (char *));
|
2007-10-07 17:14:02 +05:30
|
|
|
*newenvp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-23 23:03:21 +05:30
|
|
|
void addenv (const char *string, /*@null@*/const char *value)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *cp, *newstring;
|
|
|
|
size_t i;
|
|
|
|
size_t n;
|
|
|
|
|
2008-06-18 03:28:46 +05:30
|
|
|
if (NULL != value) {
|
2009-04-28 01:37:59 +05:30
|
|
|
size_t len = strlen (string) + strlen (value) + 2;
|
|
|
|
int wlen;
|
|
|
|
newstring = xmalloc (len);
|
|
|
|
wlen = snprintf (newstring, len, "%s=%s", string, value);
|
|
|
|
assert (wlen == (int) len -1);
|
2007-10-07 17:14:02 +05:30
|
|
|
} else {
|
2007-10-07 17:15:23 +05:30
|
|
|
newstring = xstrdup (string);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for a '=' character within the string and if none is found
|
|
|
|
* just ignore the whole string.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
cp = strchr (newstring, '=');
|
2008-06-18 03:28:46 +05:30
|
|
|
if (NULL == cp) {
|
2007-10-07 17:15:23 +05:30
|
|
|
free (newstring);
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
n = (size_t) (cp - newstring);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2011-06-17 02:55:36 +05:30
|
|
|
/*
|
|
|
|
* If this environment variable is already set, change its value.
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
for (i = 0; i < newenvc; i++) {
|
2008-06-18 03:28:46 +05:30
|
|
|
if ( (strncmp (newstring, newenvp[i], n) == 0)
|
|
|
|
&& (('=' == newenvp[i][n]) || ('\0' == newenvp[i][n]))) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2008-06-18 03:28:46 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (i < newenvc) {
|
2007-10-07 17:15:23 +05:30
|
|
|
free (newenvp[i]);
|
2007-10-07 17:14:02 +05:30
|
|
|
newenvp[i] = newstring;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-17 02:55:36 +05:30
|
|
|
/*
|
|
|
|
* Otherwise, save the new environment variable
|
|
|
|
*/
|
2007-10-07 17:14:02 +05:30
|
|
|
newenvp[newenvc++] = newstring;
|
|
|
|
|
2011-06-17 02:55:36 +05:30
|
|
|
/*
|
|
|
|
* And extend the environment if needed.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Check whether newenvc is a multiple of NEWENVP_STEP.
|
|
|
|
* If so we have to resize the vector.
|
|
|
|
* the expression (newenvc & (NEWENVP_STEP - 1)) == 0
|
|
|
|
* is equal to (newenvc % NEWENVP_STEP) == 0
|
|
|
|
* as long as NEWENVP_STEP is a power of 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((newenvc & (NEWENVP_STEP - 1)) == 0) {
|
|
|
|
char **__newenvp;
|
|
|
|
|
|
|
|
/*
|
2011-06-17 02:55:36 +05:30
|
|
|
* If the resize operation succeeds we can
|
2007-10-07 17:14:02 +05:30
|
|
|
* happily go on, else print a message.
|
|
|
|
*/
|
|
|
|
|
2023-02-05 03:50:38 +05:30
|
|
|
__newenvp = (char **) reallocarray (newenvp, newenvc + NEWENVP_STEP,
|
|
|
|
sizeof (char *));
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2011-06-17 02:55:36 +05:30
|
|
|
if (NULL != __newenvp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* If this is our current environment, update
|
|
|
|
* environ so that it doesn't point to some
|
|
|
|
* free memory area (realloc() could move it).
|
|
|
|
*/
|
2008-06-18 03:28:46 +05:30
|
|
|
if (environ == newenvp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
environ = __newenvp;
|
2008-06-18 03:28:46 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
newenvp = __newenvp;
|
|
|
|
} else {
|
2021-11-29 05:07:53 +05:30
|
|
|
(void) fputs (_("Environment overflow\n"), log_get_logfd());
|
2008-06-18 03:28:46 +05:30
|
|
|
newenvc--;
|
|
|
|
free (newenvp[newenvc]);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The last entry of newenvp must be NULL
|
|
|
|
*/
|
|
|
|
|
|
|
|
newenvp[newenvc] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set_env - copy command line arguments into the environment
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
void set_env (int argc, char *const *argv)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
int noname = 1;
|
|
|
|
char variable[1024];
|
|
|
|
char *cp;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
for (; argc > 0; argc--, argv++) {
|
2008-06-18 03:28:46 +05:30
|
|
|
if (strlen (*argv) >= sizeof variable) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue; /* ignore long entries */
|
2008-06-18 03:28:46 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-18 03:28:46 +05:30
|
|
|
cp = strchr (*argv, '=');
|
|
|
|
if (NULL == cp) {
|
2009-04-28 01:37:59 +05:30
|
|
|
int wlen;
|
|
|
|
wlen = snprintf (variable, sizeof variable, "L%d", noname);
|
|
|
|
assert (wlen < (int) sizeof(variable));
|
|
|
|
noname++;
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv (variable, *argv);
|
2007-10-07 17:14:02 +05:30
|
|
|
} else {
|
2022-08-05 21:10:31 +05:30
|
|
|
const char *const *p;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-18 03:28:46 +05:30
|
|
|
for (p = forbid; NULL != *p; p++) {
|
|
|
|
if (strncmp (*argv, *p, strlen (*p)) == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2008-06-18 03:28:46 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-18 03:28:46 +05:30
|
|
|
if (NULL != *p) {
|
|
|
|
strncpy (variable, *argv, (size_t)(cp - *argv));
|
2007-10-07 17:14:02 +05:30
|
|
|
variable[cp - *argv] = '\0';
|
2007-10-07 17:15:23 +05:30
|
|
|
printf (_("You may not change $%s\n"),
|
|
|
|
variable);
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
addenv (*argv, NULL);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sanitize_env - remove some nasty environment variables
|
|
|
|
* If you fall into a total paranoia, you should call this
|
|
|
|
* function for any root-setuid program or anything the user
|
|
|
|
* might change the environment with. 99% useless as almost
|
|
|
|
* all modern Unixes will handle setuid executables properly,
|
|
|
|
* but... I feel better with that silly precaution. -j.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
void sanitize_env (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char **envp = environ;
|
2022-08-05 21:10:31 +05:30
|
|
|
const char *const *bad;
|
2007-10-07 17:14:02 +05:30
|
|
|
char **cur;
|
|
|
|
char **move;
|
|
|
|
|
2008-06-18 03:28:46 +05:30
|
|
|
for (cur = envp; NULL != *cur; cur++) {
|
|
|
|
for (bad = forbid; NULL != *bad; bad++) {
|
2007-10-07 17:15:23 +05:30
|
|
|
if (strncmp (*cur, *bad, strlen (*bad)) == 0) {
|
2008-06-18 03:28:46 +05:30
|
|
|
for (move = cur; NULL != *move; move++) {
|
2007-10-07 17:14:02 +05:30
|
|
|
*move = *(move + 1);
|
2008-06-18 03:28:46 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
cur--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-18 03:28:46 +05:30
|
|
|
for (cur = envp; NULL != *cur; cur++) {
|
|
|
|
for (bad = noslash; NULL != *bad; bad++) {
|
|
|
|
if (strncmp (*cur, *bad, strlen (*bad)) != 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-18 03:28:46 +05:30
|
|
|
}
|
2010-01-24 22:53:42 +05:30
|
|
|
if (strchr (*cur, '/') == NULL) {
|
2007-10-07 17:15:23 +05:30
|
|
|
continue; /* OK */
|
2008-06-18 03:28:46 +05:30
|
|
|
}
|
|
|
|
for (move = cur; NULL != *move; move++) {
|
2007-10-07 17:14:02 +05:30
|
|
|
*move = *(move + 1);
|
2008-06-18 03:28:46 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
cur--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-18 03:28:46 +05:30
|
|
|
|