diff options
author | 2015-08-24 09:05:18 -0400 | |
---|---|---|
committer | 2015-08-24 09:05:18 -0400 | |
commit | 820de6829fedccffb8b3c32f079436fa7e89273e (patch) | |
tree | a1e0cf62b892e91d18de28d7459180339c5636d1 /src/firejail/env.c | |
parent | private-home testing (diff) | |
download | firejail-820de6829fedccffb8b3c32f079436fa7e89273e.tar.gz firejail-820de6829fedccffb8b3c32f079436fa7e89273e.tar.zst firejail-820de6829fedccffb8b3c32f079436fa7e89273e.zip |
added --env option
Diffstat (limited to 'src/firejail/env.c')
-rw-r--r-- | src/firejail/env.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/firejail/env.c b/src/firejail/env.c new file mode 100644 index 000000000..b4557e56f --- /dev/null +++ b/src/firejail/env.c | |||
@@ -0,0 +1,78 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014, 2015 Firejail Authors | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | #include "firejail.h" | ||
21 | |||
22 | typedef struct env_t { | ||
23 | struct env_t *next; | ||
24 | char *name; | ||
25 | char *value; | ||
26 | } Env; | ||
27 | static Env *envlist = NULL; | ||
28 | |||
29 | static void env_add(Env *env) { | ||
30 | env->next = envlist; | ||
31 | envlist = env; | ||
32 | } | ||
33 | |||
34 | // parse and store the environment setting | ||
35 | void env_store(const char *str) { | ||
36 | assert(str); | ||
37 | |||
38 | // some basic checking | ||
39 | if (*str == '\0') | ||
40 | goto errexit; | ||
41 | char *ptr = strchr(str, '='); | ||
42 | if (!ptr) | ||
43 | goto errexit; | ||
44 | ptr++; | ||
45 | if (*ptr == '\0') | ||
46 | goto errexit; | ||
47 | |||
48 | // build list entry | ||
49 | Env *env = malloc(sizeof(Env)); | ||
50 | if (!env) | ||
51 | errExit("malloc"); | ||
52 | memset(env, 0, sizeof(Env)); | ||
53 | env->name = strdup(str); | ||
54 | if (env->name == NULL) | ||
55 | errExit("strdup"); | ||
56 | char *ptr2 = strchr(env->name, '='); | ||
57 | assert(ptr2); | ||
58 | *ptr2 = '\0'; | ||
59 | env->value = ptr2 + 1; | ||
60 | |||
61 | // add entry to the list | ||
62 | env_add(env); | ||
63 | return; | ||
64 | |||
65 | errexit: | ||
66 | fprintf(stderr, "Error: invalid --env setting\n"); | ||
67 | exit(1); | ||
68 | } | ||
69 | |||
70 | // set env variables in the new sandbox process | ||
71 | void env_apply(void) { | ||
72 | Env *env = envlist; | ||
73 | |||
74 | while (env) { | ||
75 | setenv(env->name, env->value, 1); | ||
76 | env = env->next; | ||
77 | } | ||
78 | } | ||