2017-05-08 15:30:03 +02:00
|
|
|
/* Copyright 1988,1990,1993,1994 by Paul Vixie
|
|
|
|
* All rights reserved
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
* Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-08-06 18:08:05 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2021-08-09 15:08:54 +02:00
|
|
|
#include <stddef.h>
|
2019-08-06 18:08:05 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "globals.h"
|
|
|
|
#include "funcs.h"
|
|
|
|
|
|
|
|
#if defined(BSD)
|
|
|
|
extern char **environ;
|
|
|
|
#endif
|
2017-05-08 15:30:03 +02:00
|
|
|
|
|
|
|
char **env_init(void) {
|
|
|
|
char **p = (char **) malloc(sizeof (char *));
|
|
|
|
|
|
|
|
if (p != NULL)
|
|
|
|
p[0] = NULL;
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void env_free(char **envp) {
|
|
|
|
char **p;
|
|
|
|
|
|
|
|
for (p = envp; *p != NULL; p++)
|
|
|
|
free(*p);
|
|
|
|
free(envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
char **env_copy(char **envp) {
|
2019-08-06 18:08:05 +02:00
|
|
|
int save_errno;
|
|
|
|
size_t count, i;
|
2017-05-08 15:30:03 +02:00
|
|
|
char **p;
|
|
|
|
|
|
|
|
for (count = 0; envp[count] != NULL; count++) ;
|
|
|
|
|
|
|
|
p = (char **) malloc((count + 1) * sizeof (char *)); /* 1 for the NULL */
|
|
|
|
if (p != NULL) {
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
if ((p[i] = strdup(envp[i])) == NULL) {
|
|
|
|
save_errno = errno;
|
2019-08-06 18:08:05 +02:00
|
|
|
while (i-- > 0)
|
2017-05-08 15:30:03 +02:00
|
|
|
free(p[i]);
|
|
|
|
free(p);
|
|
|
|
errno = save_errno;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
p[count] = NULL;
|
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:08:05 +02:00
|
|
|
char **env_set(char **envp, const char *envstr) {
|
|
|
|
size_t count, found;
|
2017-05-08 15:30:03 +02:00
|
|
|
char **p, *envtmp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* count the number of elements, including the null pointer;
|
|
|
|
* also set 'found' to -1 or index of entry if already in here.
|
|
|
|
*/
|
2019-08-06 18:08:05 +02:00
|
|
|
found = (size_t)-1;
|
2017-05-08 15:30:03 +02:00
|
|
|
for (count = 0; envp[count] != NULL; count++) {
|
|
|
|
if (!strcmp_until(envp[count], envstr, '='))
|
|
|
|
found = count;
|
|
|
|
}
|
|
|
|
count++; /* for the NULL */
|
|
|
|
|
2019-08-06 18:08:05 +02:00
|
|
|
if (found != (size_t)-1) {
|
2017-05-08 15:30:03 +02:00
|
|
|
/*
|
|
|
|
* it exists already, so just free the existing setting,
|
|
|
|
* save our new one there, and return the existing array.
|
|
|
|
*/
|
|
|
|
if ((envtmp = strdup(envstr)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
free(envp[found]);
|
|
|
|
envp[found] = envtmp;
|
|
|
|
return (envp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* it doesn't exist yet, so resize the array, move null pointer over
|
|
|
|
* one, save our string over the old null pointer, and return resized
|
|
|
|
* array.
|
|
|
|
*/
|
|
|
|
if ((envtmp = strdup(envstr)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
p = (char **) realloc((void *) envp,
|
2019-08-06 18:08:05 +02:00
|
|
|
(count + 1) * sizeof (char *));
|
2017-05-08 15:30:03 +02:00
|
|
|
if (p == NULL) {
|
|
|
|
free(envtmp);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
p[count] = p[count - 1];
|
|
|
|
p[count - 1] = envtmp;
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:08:05 +02:00
|
|
|
int env_set_from_environ(char ***envpp) {
|
|
|
|
static const char *names[] = {
|
|
|
|
"LANG",
|
|
|
|
"LC_CTYPE",
|
|
|
|
"LC_NUMERIC",
|
|
|
|
"LC_TIME",
|
|
|
|
"LC_COLLATE",
|
|
|
|
"LC_MONETARY",
|
|
|
|
"LC_MESSAGES",
|
|
|
|
"LC_PAPER",
|
|
|
|
"LC_NAME",
|
|
|
|
"LC_ADDRESS",
|
|
|
|
"LC_TELEPHONE",
|
|
|
|
"LC_MEASUREMENT",
|
|
|
|
"LC_IDENTIFICATION",
|
|
|
|
"LC_ALL",
|
|
|
|
"LANGUAGE",
|
|
|
|
"RANDOM_DELAY",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
const char **name;
|
|
|
|
char **procenv;
|
|
|
|
|
|
|
|
for (procenv = environ; *procenv != NULL; ++procenv) {
|
|
|
|
for (name = names; *name != NULL; ++name) {
|
|
|
|
size_t namelen;
|
|
|
|
|
|
|
|
namelen = strlen(*name);
|
|
|
|
if (strncmp(*name, *procenv, namelen) == 0
|
|
|
|
&& (*procenv)[namelen] == '=') {
|
|
|
|
char **tmpenv;
|
|
|
|
|
|
|
|
tmpenv = env_set(*envpp, *procenv);
|
|
|
|
if (tmpenv == NULL)
|
|
|
|
return FALSE;
|
|
|
|
*envpp = tmpenv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-05-08 15:30:03 +02:00
|
|
|
/* The following states are used by load_env(), traversed in order: */
|
|
|
|
enum env_state {
|
|
|
|
NAMEI, /* First char of NAME, may be quote */
|
|
|
|
NAME, /* Subsequent chars of NAME */
|
|
|
|
EQ1, /* After end of name, looking for '=' sign */
|
|
|
|
EQ2, /* After '=', skipping whitespace */
|
|
|
|
VALUEI, /* First char of VALUE, may be quote */
|
|
|
|
VALUE, /* Subsequent chars of VALUE */
|
|
|
|
FINI, /* All done, skipping trailing whitespace */
|
|
|
|
ERROR, /* Error */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* return ERR = end of file
|
|
|
|
* FALSE = not an env setting (file was repositioned)
|
|
|
|
* TRUE = was an env setting
|
|
|
|
*/
|
|
|
|
int load_env(char *envstr, FILE * f) {
|
|
|
|
long filepos;
|
|
|
|
int fileline;
|
|
|
|
enum env_state state;
|
2019-08-06 18:08:05 +02:00
|
|
|
char quotechar, *c, *str, *val;
|
2017-05-08 15:30:03 +02:00
|
|
|
|
|
|
|
filepos = ftell(f);
|
|
|
|
fileline = LineNumber;
|
|
|
|
if (EOF == get_string(envstr, MAX_ENVSTR, f, "\n"))
|
|
|
|
return (ERR);
|
|
|
|
|
2019-08-06 18:08:05 +02:00
|
|
|
Debug(DPARS, ("load_env, read <%s>\n", envstr));
|
2017-05-08 15:30:03 +02:00
|
|
|
|
2023-07-01 12:15:55 +02:00
|
|
|
val = str = envstr;
|
2017-05-08 15:30:03 +02:00
|
|
|
state = NAMEI;
|
|
|
|
quotechar = '\0';
|
|
|
|
c = envstr;
|
|
|
|
while (state != ERROR && *c) {
|
|
|
|
switch (state) {
|
|
|
|
case NAMEI:
|
|
|
|
case VALUEI:
|
|
|
|
if (*c == '\'' || *c == '"')
|
|
|
|
quotechar = *c++;
|
|
|
|
state++;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case NAME:
|
|
|
|
case VALUE:
|
|
|
|
if (quotechar) {
|
|
|
|
if (*c == quotechar) {
|
|
|
|
state++;
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (state == NAME && *c == '=') {
|
|
|
|
state = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (state == NAME) {
|
|
|
|
if (isspace((unsigned char) *c)) {
|
|
|
|
c++;
|
|
|
|
state++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*c == '=') {
|
|
|
|
state++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*str++ = *c++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EQ1:
|
|
|
|
if (*c == '=') {
|
|
|
|
state++;
|
|
|
|
quotechar = '\0';
|
2019-08-06 18:08:05 +02:00
|
|
|
*str++ = *c;
|
|
|
|
val = str;
|
2017-05-08 15:30:03 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!isspace((unsigned char) *c))
|
|
|
|
state = ERROR;
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EQ2:
|
|
|
|
case FINI:
|
|
|
|
if (isspace((unsigned char) *c))
|
|
|
|
c++;
|
|
|
|
else
|
|
|
|
state++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2019-08-06 18:08:05 +02:00
|
|
|
if (state != FINI && state != EQ2 && !(state == VALUE && !quotechar)) {
|
|
|
|
Debug(DPARS, ("load_env, not an env var, state = %d\n", state));
|
|
|
|
if (fseek(f, filepos, 0)) {
|
|
|
|
return ERR;
|
|
|
|
}
|
2017-05-08 15:30:03 +02:00
|
|
|
Set_LineNum(fileline);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
2019-08-06 18:08:05 +02:00
|
|
|
*str = '\0';
|
2017-05-08 15:30:03 +02:00
|
|
|
if (state == VALUE) {
|
|
|
|
/* End of unquoted value: trim trailing whitespace */
|
2019-08-06 18:08:05 +02:00
|
|
|
while (str > val && isspace((unsigned char)str[-1]))
|
|
|
|
*(--str) = '\0';
|
2017-05-08 15:30:03 +02:00
|
|
|
}
|
2019-08-06 18:08:05 +02:00
|
|
|
return TRUE;
|
2017-05-08 15:30:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:08:05 +02:00
|
|
|
char *env_get(const char *name, char **envp) {
|
|
|
|
size_t len = strlen(name);
|
2017-05-08 15:30:03 +02:00
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
while ((p = *envp++) != NULL) {
|
|
|
|
if (!(q = strchr(p, '=')))
|
|
|
|
continue;
|
2021-08-09 15:08:54 +02:00
|
|
|
if ((size_t)(q - p) == len && !strncmp(p, name, len))
|
2017-05-08 15:30:03 +02:00
|
|
|
return (q + 1);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
2019-08-06 18:08:05 +02:00
|
|
|
|
|
|
|
char **env_update_home(char **envp, const char *dir) {
|
|
|
|
char envstr[MAX_ENVSTR];
|
|
|
|
|
|
|
|
if (dir == NULL || *dir == '\0' || env_get("HOME", envp)) {
|
|
|
|
return envp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glue_strings(envstr, sizeof envstr, "HOME", dir, '=')) {
|
|
|
|
envp = env_set(envp, envstr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log_it("CRON", getpid(), "ERROR", "can't set HOME", 0);
|
|
|
|
|
|
|
|
return envp;
|
|
|
|
}
|