diff options
author | Kelvin M. Klann <kmk3.code@protonmail.com> | 2023-06-30 17:00:01 -0300 |
---|---|---|
committer | Kelvin M. Klann <kmk3.code@protonmail.com> | 2023-08-04 17:25:20 -0300 |
commit | 62162e3a49cd8db335733664d07ffd747a065928 (patch) | |
tree | 6152a92107c2f1bbde372e3c667d29e1bf7b033f /src | |
parent | firecfg: fix missing free and formatting (diff) | |
download | firejail-62162e3a49cd8db335733664d07ffd747a065928.tar.gz firejail-62162e3a49cd8db335733664d07ffd747a065928.tar.zst firejail-62162e3a49cd8db335733664d07ffd747a065928.zip |
firecfg: turn constant strings into constants
Instead of using asprintf + free.
Also, use LIBDIR instead of hardcoded "/usr/lib" for fzenity.
Diffstat (limited to 'src')
-rw-r--r-- | src/firecfg/firecfg.h | 6 | ||||
-rw-r--r-- | src/firecfg/main.c | 62 |
2 files changed, 25 insertions, 43 deletions
diff --git a/src/firecfg/firecfg.h b/src/firecfg/firecfg.h index 8f74a1198..6e17c20cf 100644 --- a/src/firecfg/firecfg.h +++ b/src/firecfg/firecfg.h | |||
@@ -37,6 +37,12 @@ | |||
37 | #include "../include/common.h" | 37 | #include "../include/common.h" |
38 | #define MAX_BUF 4096 | 38 | #define MAX_BUF 4096 |
39 | 39 | ||
40 | #define FIRECFG_CFGFILE SYSCONFDIR "/firecfg.config" | ||
41 | #define FIREJAIL_EXEC PREFIX "/bin/firejail" | ||
42 | #define FIREJAIL_WELCOME_SH LIBDIR "/firejail/firejail-welcome.sh" | ||
43 | #define FZENITY_EXEC LIBDIR "/firejail/fzenity" | ||
44 | #define ZENITY_EXEC "/usr/bin/zenity" | ||
45 | #define SUDO_EXEC "sudo" | ||
40 | 46 | ||
41 | // main.c | 47 | // main.c |
42 | extern int arg_debug; | 48 | extern int arg_debug; |
diff --git a/src/firecfg/main.c b/src/firecfg/main.c index 90cea6d52..0d995a6dd 100644 --- a/src/firecfg/main.c +++ b/src/firecfg/main.c | |||
@@ -76,10 +76,6 @@ static void list(void) { | |||
76 | exit(1); | 76 | exit(1); |
77 | } | 77 | } |
78 | 78 | ||
79 | char *firejail_exec; | ||
80 | if (asprintf(&firejail_exec, "%s/bin/firejail", PREFIX) == -1) | ||
81 | errExit("asprintf"); | ||
82 | |||
83 | struct dirent *entry; | 79 | struct dirent *entry; |
84 | while ((entry = readdir(dir)) != NULL) { | 80 | while ((entry = readdir(dir)) != NULL) { |
85 | if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) | 81 | if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) |
@@ -92,7 +88,7 @@ static void list(void) { | |||
92 | if (is_link(fullname)) { | 88 | if (is_link(fullname)) { |
93 | char* fname = realpath(fullname, NULL); | 89 | char* fname = realpath(fullname, NULL); |
94 | if (fname) { | 90 | if (fname) { |
95 | if (strcmp(fname, firejail_exec) == 0) | 91 | if (strcmp(fname, FIREJAIL_EXEC) == 0) |
96 | printf("%s\n", fullname); | 92 | printf("%s\n", fullname); |
97 | free(fname); | 93 | free(fname); |
98 | } | 94 | } |
@@ -101,7 +97,6 @@ static void list(void) { | |||
101 | } | 97 | } |
102 | 98 | ||
103 | closedir(dir); | 99 | closedir(dir); |
104 | free(firejail_exec); | ||
105 | } | 100 | } |
106 | 101 | ||
107 | static void clean(void) { | 102 | static void clean(void) { |
@@ -114,10 +109,6 @@ static void clean(void) { | |||
114 | exit(1); | 109 | exit(1); |
115 | } | 110 | } |
116 | 111 | ||
117 | char *firejail_exec; | ||
118 | if (asprintf(&firejail_exec, "%s/bin/firejail", PREFIX) == -1) | ||
119 | errExit("asprintf"); | ||
120 | |||
121 | struct dirent *entry; | 112 | struct dirent *entry; |
122 | while ((entry = readdir(dir)) != NULL) { | 113 | while ((entry = readdir(dir)) != NULL) { |
123 | if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) | 114 | if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) |
@@ -130,7 +121,7 @@ static void clean(void) { | |||
130 | if (is_link(fullname)) { | 121 | if (is_link(fullname)) { |
131 | char* fname = realpath(fullname, NULL); | 122 | char* fname = realpath(fullname, NULL); |
132 | if (fname) { | 123 | if (fname) { |
133 | if (strcmp(fname, firejail_exec) == 0) { | 124 | if (strcmp(fname, FIREJAIL_EXEC) == 0) { |
134 | char *ptr = strrchr(fullname, '/'); | 125 | char *ptr = strrchr(fullname, '/'); |
135 | assert(ptr); | 126 | assert(ptr); |
136 | ptr++; | 127 | ptr++; |
@@ -147,7 +138,6 @@ static void clean(void) { | |||
147 | } | 138 | } |
148 | 139 | ||
149 | closedir(dir); | 140 | closedir(dir); |
150 | free(firejail_exec); | ||
151 | printf("\n"); | 141 | printf("\n"); |
152 | } | 142 | } |
153 | 143 | ||
@@ -175,24 +165,16 @@ static void set_file(const char *name, const char *firejail_exec) { | |||
175 | free(fname); | 165 | free(fname); |
176 | } | 166 | } |
177 | 167 | ||
178 | // parse /etc/firejail/firecfg.config file | 168 | // parse a single config file |
179 | static void set_links_firecfg(void) { | 169 | static void set_links_firecfg(const char *cfgfile) { |
180 | char *cfgfile; | 170 | printf("Configuring symlinks in %s based on %s\n", arg_bindir, cfgfile); |
181 | if (asprintf(&cfgfile, "%s/firecfg.config", SYSCONFDIR) == -1) | ||
182 | errExit("asprintf"); | ||
183 | |||
184 | char *firejail_exec; | ||
185 | if (asprintf(&firejail_exec, "%s/bin/firejail", PREFIX) == -1) | ||
186 | errExit("asprintf"); | ||
187 | 171 | ||
188 | // parse /etc/firejail/firecfg.config file | ||
189 | FILE *fp = fopen(cfgfile, "r"); | 172 | FILE *fp = fopen(cfgfile, "r"); |
190 | if (!fp) { | 173 | if (!fp) { |
191 | perror("fopen"); | 174 | perror("fopen"); |
192 | fprintf(stderr, "Error: cannot open %s\n", cfgfile); | 175 | fprintf(stderr, "Error: cannot open %s\n", cfgfile); |
193 | exit(1); | 176 | exit(1); |
194 | } | 177 | } |
195 | printf("Configuring symlinks in %s based on firecfg.config\n", arg_bindir); | ||
196 | 178 | ||
197 | char buf[MAX_BUF]; | 179 | char buf[MAX_BUF]; |
198 | int lineno = 0; | 180 | int lineno = 0; |
@@ -223,12 +205,10 @@ static void set_links_firecfg(void) { | |||
223 | continue; | 205 | continue; |
224 | 206 | ||
225 | // set link | 207 | // set link |
226 | set_file(start, firejail_exec); | 208 | set_file(start, FIREJAIL_EXEC); |
227 | } | 209 | } |
228 | 210 | ||
229 | fclose(fp); | 211 | fclose(fp); |
230 | free(cfgfile); | ||
231 | free(firejail_exec); | ||
232 | } | 212 | } |
233 | 213 | ||
234 | // parse ~/.config/firejail/ directory | 214 | // parse ~/.config/firejail/ directory |
@@ -245,10 +225,6 @@ static void set_links_homedir(const char *homedir) { | |||
245 | return; | 225 | return; |
246 | } | 226 | } |
247 | 227 | ||
248 | char *firejail_exec; | ||
249 | if (asprintf(&firejail_exec, "%s/bin/firejail", PREFIX) == -1) | ||
250 | errExit("asprintf"); | ||
251 | |||
252 | // parse ~/.config/firejail/ directory | 228 | // parse ~/.config/firejail/ directory |
253 | printf("\nConfiguring symlinks in %s based on local firejail config directory\n", arg_bindir); | 229 | printf("\nConfiguring symlinks in %s based on local firejail config directory\n", arg_bindir); |
254 | 230 | ||
@@ -280,12 +256,10 @@ static void set_links_homedir(const char *homedir) { | |||
280 | } | 256 | } |
281 | 257 | ||
282 | *ptr = '\0'; | 258 | *ptr = '\0'; |
283 | set_file(exec, firejail_exec); | 259 | set_file(exec, FIREJAIL_EXEC); |
284 | free(exec); | 260 | free(exec); |
285 | } | 261 | } |
286 | closedir(dir); | 262 | closedir(dir); |
287 | |||
288 | free(firejail_exec); | ||
289 | } | 263 | } |
290 | 264 | ||
291 | static const char *get_sudo_user(void) { | 265 | static const char *get_sudo_user(void) { |
@@ -449,18 +423,20 @@ int main(int argc, char **argv) { | |||
449 | } | 423 | } |
450 | 424 | ||
451 | if (arg_guide) { | 425 | if (arg_guide) { |
426 | const char *zenity_exec; | ||
427 | if (arg_debug) | ||
428 | zenity_exec = FZENITY_EXEC; | ||
429 | else | ||
430 | zenity_exec = ZENITY_EXEC; | ||
431 | |||
452 | char *cmd; | 432 | char *cmd; |
453 | if (arg_debug) { | 433 | if (asprintf(&cmd, "%s %s %s %s %s", |
454 | if (asprintf(&cmd, "sudo %s/firejail/firejail-welcome.sh /usr/lib/firejail/fzenity %s %s", LIBDIR, SYSCONFDIR, user) == -1) | 434 | SUDO_EXEC, FIREJAIL_WELCOME_SH, zenity_exec, SYSCONFDIR, user) == -1) |
455 | errExit("asprintf"); | 435 | errExit("asprintf"); |
456 | } | 436 | |
457 | else { | ||
458 | if (asprintf(&cmd, "sudo %s/firejail/firejail-welcome.sh /usr/bin/zenity %s %s", LIBDIR, SYSCONFDIR, user) == -1) | ||
459 | errExit("asprintf"); | ||
460 | } | ||
461 | int status = system(cmd); | 437 | int status = system(cmd); |
462 | if (status == -1) { | 438 | if (status == -1) { |
463 | fprintf(stderr, "Error: cannot run firejail-welcome.sh\n"); | 439 | fprintf(stderr, "Error: cannot run %s\n", FIREJAIL_WELCOME_SH); |
464 | exit(1); | 440 | exit(1); |
465 | } | 441 | } |
466 | free(cmd); | 442 | free(cmd); |
@@ -474,8 +450,8 @@ else { | |||
474 | // clear all symlinks | 450 | // clear all symlinks |
475 | clean(); | 451 | clean(); |
476 | 452 | ||
477 | // set new symlinks based on /etc/firejail/firecfg.config | 453 | // set new symlinks based on firecfg.config |
478 | set_links_firecfg(); | 454 | set_links_firecfg(FIRECFG_CFGFILE); |
479 | 455 | ||
480 | if (getuid() == 0) { | 456 | if (getuid() == 0) { |
481 | // add user to firejail access database - only for root | 457 | // add user to firejail access database - only for root |