Skip to content

Commit

Permalink
firecfg: refactor config parse functions
Browse files Browse the repository at this point in the history
Changes:

* Export `in_ignorelist` function
* Allow only building the ignorelist without setting the symlinks
* Rename the functions to reflect the above
* Add a function that parses all config files (`parse_config_all`)

Also, make sure that `parse_config_all` only parses config files once,
even if called multiple times.

Relates to netblue30#5876.
  • Loading branch information
kmk3 committed Jan 11, 2024
1 parent 69f804b commit 859d215
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/firecfg/firecfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

// main.c
extern int arg_debug;
int in_ignorelist(const char *const str);
void parse_config_all(int do_symlink);

// util.c
int which(const char *program);
Expand Down
46 changes: 33 additions & 13 deletions src/firecfg/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
int arg_debug = 0;
char *arg_bindir = "/usr/local/bin";
int arg_guide = 0;
int done_config = 0;

static const char *const usage_str =
"Firecfg is the desktop configuration utility for Firejail software. The utility\n"
Expand Down Expand Up @@ -166,7 +167,7 @@ static int append_ignorelist(const char *const str) {
return 1;
}

static int in_ignorelist(const char *const str) {
int in_ignorelist(const char *const str) {
assert(str);
int i;
for (i = 0; i < ignorelist_len; i++) {
Expand Down Expand Up @@ -202,8 +203,11 @@ static void set_file(const char *name, const char *firejail_exec) {
}

// parse a single config file
static void set_links_firecfg(const char *cfgfile) {
printf("Configuring symlinks in %s based on %s\n", arg_bindir, cfgfile);
static void parse_config_file(const char *cfgfile, int do_symlink) {
if (do_symlink)
printf("Configuring symlinks in %s\n", arg_bindir);

printf("Parsing %s\n", cfgfile);

FILE *fp = fopen(cfgfile, "r");
if (!fp) {
Expand Down Expand Up @@ -246,19 +250,23 @@ static void set_links_firecfg(const char *cfgfile) {
continue;
}

// skip ignored programs
if (in_ignorelist(start)) {
printf(" %s ignored\n", start);
continue;
}

// set link
if (!in_ignorelist(start))
if (do_symlink)
set_file(start, FIREJAIL_EXEC);
else
printf(" %s ignored\n", start);
}

fclose(fp);
printf("\n");
}

// parse all config files matching pattern
static void set_links_firecfg_glob(const char *pattern) {
static void parse_config_glob(const char *pattern, int do_symlink) {
printf("Looking for config files in %s\n", pattern);

glob_t globbuf;
Expand All @@ -274,11 +282,26 @@ static void set_links_firecfg_glob(const char *pattern) {

size_t i;
for (i = 0; i < globbuf.gl_pathc; i++)
set_links_firecfg(globbuf.gl_pathv[i]);
parse_config_file(globbuf.gl_pathv[i], do_symlink);
out:
globfree(&globbuf);
}

// parse all config files
// do_symlink 0 just builds the ignorelist, 1 creates the symlinks
void parse_config_all(int do_symlink) {
if (done_config)
return;

// set new symlinks based on .conf files
parse_config_glob(FIRECFG_CONF_GLOB, do_symlink);

// set new symlinks based on firecfg.config
parse_config_file(FIRECFG_CFGFILE, do_symlink);

done_config = 1;
}

// parse ~/.config/firejail/ directory
static void set_links_homedir(const char *homedir) {
assert(homedir);
Expand Down Expand Up @@ -520,11 +543,8 @@ int main(int argc, char **argv) {
// clear all symlinks
clean();

// set new symlinks based on .conf files
set_links_firecfg_glob(FIRECFG_CONF_GLOB);

// set new symlinks based on firecfg.config
set_links_firecfg(FIRECFG_CFGFILE);
// set new symlinks based on config files
parse_config_all(1);

if (getuid() == 0) {
// add user to firejail access database - only for root
Expand Down

0 comments on commit 859d215

Please sign in to comment.