aboutsummaryrefslogtreecommitdiffstats
path: root/src/firejail/env.c
blob: b4557e56f100f0f4b1f3cb68689f5cbf6a4053a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (C) 2014, 2015 Firejail Authors
 *
 * This file is part of firejail project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include "firejail.h"

typedef struct env_t {
	struct env_t *next;
	char *name;
	char *value;
} Env;
static Env *envlist = NULL;

static void env_add(Env *env) {
	env->next = envlist;
	envlist = env;
}

// parse and store the environment setting 
void env_store(const char *str) {
	assert(str);
	
	// some basic checking
	if (*str == '\0')
		goto errexit;
	char *ptr = strchr(str, '=');
	if (!ptr)
		goto errexit;
	ptr++;
	if (*ptr == '\0')
		goto errexit;

	// build list entry
	Env *env = malloc(sizeof(Env));
	if (!env)
		errExit("malloc");
	memset(env, 0, sizeof(Env));
	env->name = strdup(str);
	if (env->name == NULL)
		errExit("strdup");
	char *ptr2 = strchr(env->name, '=');
	assert(ptr2);
	*ptr2 = '\0';
	env->value = ptr2 + 1;
	
	// add entry to the list
	env_add(env);
	return;
	
errexit:
	fprintf(stderr, "Error: invalid --env setting\n");
	exit(1);
}

// set env variables in the new sandbox process
void env_apply(void) {
	Env *env = envlist;
	
	while (env) {
		setenv(env->name, env->value, 1);
		env = env->next;
	}
}