env-util.c revision 93cc7779e0c121b75183920173f37cd1ee9d59cf
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "alloc-util.h"
#include "env-util.h"
#include "extract-word.h"
#include "macro.h"
#include "parse-util.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
#define VALID_CHARS_ENV_NAME \
"_"
#ifndef ARG_MAX
#endif
static bool env_name_is_valid_n(const char *e, size_t n) {
const char *p;
if (!e)
return false;
if (n <= 0)
return false;
if (e[0] >= '0' && e[0] <= '9')
return false;
/* POSIX says the overall size of the environment block cannot
* be > ARG_MAX, an individual assignment hence cannot be
* either. Discounting the equal sign and trailing NUL this
* hence leaves ARG_MAX-2 as longest possible variable
* name. */
if (n > ARG_MAX - 2)
return false;
for (p = e; p < e + n; p++)
if (!strchr(VALID_CHARS_ENV_NAME, *p))
return false;
return true;
}
bool env_name_is_valid(const char *e) {
if (!e)
return false;
return env_name_is_valid_n(e, strlen(e));
}
bool env_value_is_valid(const char *e) {
if (!e)
return false;
if (!utf8_is_valid(e))
return false;
/* bash allows tabs in environment variables, and so should
* we */
if (string_has_cc(e, "\t"))
return false;
/* POSIX says the overall size of the environment block cannot
* be > ARG_MAX, an individual assignment hence cannot be
* either. Discounting the shortest possible variable name of
* length 1, the equal sign and trailing NUL this hence leaves
* ARG_MAX-3 as longest possible variable value. */
return false;
return true;
}
bool env_assignment_is_valid(const char *e) {
const char *eq;
if (!eq)
return false;
if (!env_name_is_valid_n(e, eq - e))
return false;
return false;
/* POSIX says the overall size of the environment block cannot
* be > ARG_MAX, hence the individual variable assignments
* cannot be either, but let's leave room for one trailing NUL
* byte. */
return false;
return true;
}
bool strv_env_is_valid(char **e) {
char **p, **q;
STRV_FOREACH(p, e) {
size_t k;
if (!env_assignment_is_valid(*p))
return false;
/* Check if there are duplicate assginments */
k = strcspn(*p, "=");
STRV_FOREACH(q, p + 1)
if (strneq(*p, *q, k) && (*q)[k] == '=')
return false;
}
return true;
}
bool strv_env_name_is_valid(char **l) {
char **p, **q;
STRV_FOREACH(p, l) {
if (!env_name_is_valid(*p))
return false;
STRV_FOREACH(q, p + 1)
if (streq(*p, *q))
return false;
}
return true;
}
bool strv_env_name_or_assignment_is_valid(char **l) {
char **p, **q;
STRV_FOREACH(p, l) {
if (!env_assignment_is_valid(*p) && !env_name_is_valid(*p))
return false;
STRV_FOREACH(q, p + 1)
if (streq(*p, *q))
return false;
}
return true;
}
static int env_append(char **r, char ***k, char **a) {
assert(r);
assert(k);
if (!a)
return 0;
/* Add the entries of a to *k unless they already exist in *r
* in which case they are overridden instead. This assumes
* there is enough space in the r array. */
for (; *a; a++) {
char **j;
size_t n;
n = strcspn(*a, "=");
if ((*a)[n] == '=')
n++;
for (j = r; j < *k; j++)
if (strneq(*j, *a, n))
break;
if (j >= *k)
(*k)++;
else
free(*j);
*j = strdup(*a);
if (!*j)
return -ENOMEM;
}
return 0;
}
char **strv_env_merge(unsigned n_lists, ...) {
size_t n = 0;
char **l, **k, **r;
unsigned i;
/* Merges an arbitrary number of environment sets */
for (i = 0; i < n_lists; i++) {
n += strv_length(l);
}
r = new(char*, n+1);
if (!r)
return NULL;
k = r;
for (i = 0; i < n_lists; i++) {
if (env_append(r, &k, l) < 0)
goto fail;
}
*k = NULL;
return r;
fail:
strv_free(r);
return NULL;
}
assert(t);
/* pattern a matches string a
* a matches a=
* a matches a=b
* a= matches a=
* a=b matches a=b
* a= does not match a
* a=b does not match a=
* a=b does not match a
* a=b does not match a=c */
return true;
}
return false;
}
char **strv_env_delete(char **x, unsigned n_lists, ...) {
size_t n, i = 0;
char **k, **r;
/* Deletes every entry from x that is mentioned in the other
* string lists */
n = strv_length(x);
r = new(char*, n+1);
if (!r)
return NULL;
STRV_FOREACH(k, x) {
unsigned v;
for (v = 0; v < n_lists; v++) {
char **l, **j;
STRV_FOREACH(j, l)
if (env_match(*k, *j))
goto skip;
}
r[i] = strdup(*k);
if (!r[i]) {
strv_free(r);
return NULL;
}
i++;
continue;
skip:
}
r[i] = NULL;
assert(i <= n);
return r;
}
char **strv_env_unset(char **l, const char *p) {
char **f, **t;
if (!l)
return NULL;
assert(p);
/* Drops every occurrence of the env var setting p in the
* string list. Edits in-place. */
for (f = t = l; *f; f++) {
if (env_match(*f, p)) {
free(*f);
continue;
}
*(t++) = *f;
}
*t = NULL;
return l;
}
char **strv_env_unset_many(char **l, ...) {
char **f, **t;
if (!l)
return NULL;
/* Like strv_env_unset() but applies many at once. Edits in-place. */
for (f = t = l; *f; f++) {
bool found = false;
const char *p;
if (env_match(*f, p)) {
found = true;
break;
}
}
if (found) {
free(*f);
continue;
}
*(t++) = *f;
}
*t = NULL;
return l;
}
char **strv_env_set(char **x, const char *p) {
char **k, **r;
char* m[2] = { (char*) p, NULL };
/* Overrides the env var setting of p, returns a new copy */
if (!r)
return NULL;
k = r;
if (env_append(r, &k, x) < 0)
goto fail;
if (env_append(r, &k, m) < 0)
goto fail;
*k = NULL;
return r;
fail:
strv_free(r);
return NULL;
}
char **i;
if (k <= 0)
return NULL;
STRV_FOREACH(i, l)
(*i)[k] == '=')
return *i + k + 1;
return NULL;
}
char *strv_env_get(char **l, const char *name) {
}
char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const char *p, void *userdata), void *userdata) {
char **p, **q;
int k = 0;
STRV_FOREACH(p, e) {
size_t n;
bool duplicate = false;
if (!env_assignment_is_valid(*p)) {
if (invalid_callback)
invalid_callback(*p, userdata);
free(*p);
continue;
}
n = strcspn(*p, "=");
STRV_FOREACH(q, p + 1)
if (strneq(*p, *q, n) && (*q)[n] == '=') {
duplicate = true;
break;
}
if (duplicate) {
free(*p);
continue;
}
e[k++] = *p;
}
if (e)
e[k] = NULL;
return e;
}
enum {
WORD,
char *r = NULL, *k;
for (e = format; *e; e ++) {
switch (state) {
case WORD:
if (*e == '$')
break;
case CURLY:
if (*e == '{') {
if (!k)
goto fail;
free(r);
r = k;
word = e-1;
} else if (*e == '$') {
if (!k)
goto fail;
free(r);
r = k;
word = e+1;
} else
break;
case VARIABLE:
if (*e == '}') {
const char *t;
k = strappend(r, t);
if (!k)
goto fail;
free(r);
r = k;
word = e+1;
}
break;
}
}
if (!k)
goto fail;
free(r);
return k;
fail:
free(r);
return NULL;
}
char **ret, **i;
unsigned k = 0, l = 0;
l = strv_length(argv);
if (!ret)
return NULL;
STRV_FOREACH(i, argv) {
/* If $FOO appears as single word, replace it by the split up variable */
if ((*i)[0] == '$' && (*i)[1] != '{' && (*i)[1] != '$') {
char *e;
char **w, **m = NULL;
unsigned q;
if (e) {
int r;
if (r < 0) {
return NULL;
}
} else
m = NULL;
q = strv_length(m);
l = l + q - 1;
if (!w) {
strv_free(m);
return NULL;
}
ret = w;
if (m) {
free(m);
}
k += q;
continue;
}
/* If ${FOO} appears as part of a word, replace it by the variable as-is */
if (!ret[k]) {
return NULL;
}
k++;
}
return ret;
}
int getenv_bool(const char *p) {
const char *e;
e = getenv(p);
if (!e)
return -ENXIO;
return parse_boolean(e);
}