1 |
commit: 6738f1fe7622a73f4d1c5024b03fa538de98db37 |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Sep 26 23:46:22 2015 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Sep 26 23:46:22 2015 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=6738f1fe |
7 |
|
8 |
libsbutil: add helpers for reading config options (w/out env export) |
9 |
|
10 |
All sandbox settings thus far have been for libsandbox.so to process. |
11 |
With newer features though, we have settings that might only apply to |
12 |
the main sandbox program. Add some helper functions for parsing out |
13 |
those settings (which a later commit will utilize). |
14 |
|
15 |
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org> |
16 |
|
17 |
libsbutil/environment.c | 43 ++++++++++++++++++++++++++++++------------- |
18 |
libsbutil/sbutil.h | 2 ++ |
19 |
src/environ.c | 6 ++++++ |
20 |
src/sandbox.h | 2 ++ |
21 |
4 files changed, 40 insertions(+), 13 deletions(-) |
22 |
|
23 |
diff --git a/libsbutil/environment.c b/libsbutil/environment.c |
24 |
index 70fdb72..805b9e6 100644 |
25 |
--- a/libsbutil/environment.c |
26 |
+++ b/libsbutil/environment.c |
27 |
@@ -10,9 +10,27 @@ |
28 |
#include "headers.h" |
29 |
#include "sbutil.h" |
30 |
|
31 |
-static bool env_is_in(const char *env, const char *values[], bool *set) |
32 |
+static const char * const true_values[] = { |
33 |
+ "1", "true", "yes", NULL, |
34 |
+}; |
35 |
+ |
36 |
+static const char * const false_values[] = { |
37 |
+ "0", "false", "no", NULL, |
38 |
+}; |
39 |
+ |
40 |
+static bool val_is_in(const char *val, const char * const values[]) |
41 |
{ |
42 |
size_t i = 0; |
43 |
+ |
44 |
+ while (values[i]) |
45 |
+ if (!strcasecmp(val, values[i++])) |
46 |
+ return true; |
47 |
+ |
48 |
+ return false; |
49 |
+} |
50 |
+ |
51 |
+static bool env_is_in(const char *env, const char * const values[], bool *set) |
52 |
+{ |
53 |
const char *val; |
54 |
|
55 |
if (unlikely(!env)) |
56 |
@@ -23,19 +41,21 @@ static bool env_is_in(const char *env, const char *values[], bool *set) |
57 |
if (unlikely(!*set)) |
58 |
return false; |
59 |
|
60 |
- while (values[i]) |
61 |
- if (!strcasecmp(val, values[i++])) |
62 |
- return true; |
63 |
+ return val_is_in(val, values); |
64 |
+} |
65 |
|
66 |
- return false; |
67 |
+bool is_val_on(const char *val) |
68 |
+{ |
69 |
+ return val_is_in(val, true_values); |
70 |
+} |
71 |
+bool is_val_off(const char *val) |
72 |
+{ |
73 |
+ return val_is_in(val, false_values); |
74 |
} |
75 |
|
76 |
bool is_env_set_on(const char *env, bool *set) |
77 |
{ |
78 |
- static const char *values[] = { |
79 |
- "1", "true", "yes", NULL, |
80 |
- }; |
81 |
- return env_is_in(env, values, set); |
82 |
+ return env_is_in(env, true_values, set); |
83 |
} |
84 |
bool is_env_on(const char *env) |
85 |
{ |
86 |
@@ -45,10 +65,7 @@ bool is_env_on(const char *env) |
87 |
|
88 |
bool is_env_set_off(const char *env, bool *set) |
89 |
{ |
90 |
- static const char *values[] = { |
91 |
- "0", "false", "no", NULL, |
92 |
- }; |
93 |
- return env_is_in(env, values, set); |
94 |
+ return env_is_in(env, false_values, set); |
95 |
} |
96 |
bool is_env_off(const char *env) |
97 |
{ |
98 |
|
99 |
diff --git a/libsbutil/sbutil.h b/libsbutil/sbutil.h |
100 |
index 56fe6d3..15979da 100644 |
101 |
--- a/libsbutil/sbutil.h |
102 |
+++ b/libsbutil/sbutil.h |
103 |
@@ -73,6 +73,8 @@ void get_sandbox_log(char *path, const char *tmpdir); |
104 |
void get_sandbox_debug_log(char *path, const char *tmpdir); |
105 |
void get_sandbox_message_path(char *path); |
106 |
int get_tmp_dir(char *path); |
107 |
+bool is_val_on(const char *); |
108 |
+bool is_val_off(const char *); |
109 |
bool is_env_on(const char *); |
110 |
bool is_env_off(const char *); |
111 |
bool is_env_set_on(const char *, bool *); |
112 |
|
113 |
diff --git a/src/environ.c b/src/environ.c |
114 |
index 5f22829..346bc26 100644 |
115 |
--- a/src/environ.c |
116 |
+++ b/src/environ.c |
117 |
@@ -101,6 +101,12 @@ static void setup_cfg_var(const char *env_var) |
118 |
} |
119 |
} |
120 |
|
121 |
+bool sb_get_cnf_bool(const char *key, bool default_val) |
122 |
+{ |
123 |
+ const char *val = rc_get_cnf_entry(sb_conf_file(), key, NULL); |
124 |
+ return val ? is_val_on(val) : default_val; |
125 |
+} |
126 |
+ |
127 |
/* Get passed access variable from sandbox.conf for sandbox.d/, and set it in |
128 |
* the environment. */ |
129 |
static int setup_access_var(const char *access_var) |
130 |
|
131 |
diff --git a/src/sandbox.h b/src/sandbox.h |
132 |
index 361d468..4233bd6 100644 |
133 |
--- a/src/sandbox.h |
134 |
+++ b/src/sandbox.h |
135 |
@@ -26,6 +26,8 @@ struct sandbox_info_t { |
136 |
|
137 |
extern char **setup_environ(struct sandbox_info_t *sandbox_info); |
138 |
|
139 |
+extern bool sb_get_cnf_bool(const char *, bool); |
140 |
+ |
141 |
#define sb_warn(fmt, args...) fprintf(stderr, "%s:%s " fmt "\n", "sandbox", __func__, ## args) |
142 |
#define sb_pwarn(fmt, args...) sb_warn(fmt ": %s\n", ## args, strerror(errno)) |
143 |
#define _sb_err(func, fmt, args...) do { sb_##func(fmt, ## args); exit(EXIT_FAILURE); } while (0) |