Skip to content

Commit

Permalink
Refactor: check_config to no longer rely on global booth_conf variable
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Pokorný <[email protected]>
  • Loading branch information
jnpkrn authored and clumens committed Jul 1, 2024
1 parent 261a1d0 commit 20424d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
37 changes: 19 additions & 18 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,56 +934,57 @@ int read_config(struct booth_config **conf, const char *path, int type)
return -1;
}


int check_config(int type)
int check_config(struct booth_config *conf, int type)
{
struct passwd *pw;
struct group *gr;
char *cp, *input;

if (!booth_conf)
if (conf == NULL) {
return -1;

}

input = (type == ARBITRATOR)
? booth_conf->arb_user
: booth_conf->site_user;
? conf->arb_user
: conf->site_user;
if (!*input)
goto u_inval;
if (isdigit(input[0])) {
booth_conf->uid = strtol(input, &cp, 0);
conf->uid = strtol(input, &cp, 0);
if (*cp != 0) {
u_inval:
log_error("User \"%s\" cannot be resolved into a UID.", input);
return ENOENT;
}
}
else {
} else {
pw = getpwnam(input);
if (!pw)
goto u_inval;
booth_conf->uid = pw->pw_uid;
conf->uid = pw->pw_uid;
}


input = (type == ARBITRATOR)
? booth_conf->arb_group
: booth_conf->site_group;
if (!*input)
? conf->arb_group
: conf->site_group;

if (!*input) {
goto g_inval;
}

if (isdigit(input[0])) {
booth_conf->gid = strtol(input, &cp, 0);
conf->gid = strtol(input, &cp, 0);
if (*cp != 0) {
g_inval:
log_error("Group \"%s\" cannot be resolved into a UID.", input);
return ENOENT;
}
}
else {
} else {
gr = getgrnam(input);
if (!gr)
if (!gr) {
goto g_inval;
booth_conf->gid = gr->gr_gid;
}
conf->gid = gr->gr_gid;
}

return 0;
Expand Down
14 changes: 13 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,19 @@ extern struct booth_config *booth_conf;
*/
int read_config(struct booth_config **conf, const char *path, int type);

int check_config(int type);
/**
* @internal
* Check booth configuration
*
* Currently it means checking that login user/group indeed exists,
* while converting it to respective numeric values for further use.
*
* @param[in,out] conf_ptr config object to check
* @param[in] type role currently being acted as
*
* @return 0 or negative value (-1 or -errno) on error
*/
int check_config(struct booth_config *conf, int type);

int find_site_by_name(char *site, struct booth_site **node, int any_type);
int find_site_by_id(uint32_t site_id, struct booth_site **node);
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ static int setup_config(struct booth_config **conf, int type)
find_myself(NULL, type == CLIENT || type == GEOSTORE);


rv = check_config(type);
rv = check_config(booth_conf, type);
if (rv < 0)
goto out;

Expand Down

0 comments on commit 20424d0

Please sign in to comment.