Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure proper file ownerships if pihole-FTL is started as root #366

Merged
merged 2 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dnsmasq/dnsmasq.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ int main_dnsmasq (int argc, char **argv)
}
}

FTL_fork_and_bind_sockets();
FTL_fork_and_bind_sockets(ent_pw);

log_err = log_start(ent_pw, err_pipe[1]);

Expand Down
12 changes: 11 additions & 1 deletion dnsmasq_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ pthread_t socket_listenthread;
pthread_t DBthread;
pthread_t GCthread;

void FTL_fork_and_bind_sockets(void)
void FTL_fork_and_bind_sockets(struct passwd *ent_pw)
{
if(!debug && daemonmode)
go_daemon();
Expand Down Expand Up @@ -919,6 +919,16 @@ void FTL_fork_and_bind_sockets(void)
logg("Unable to open GC thread. Exiting...");
exit(EXIT_FAILURE);
}

// Chown files if FTL started as user root but a dnsmasq config option
// states to run as a different user/group (e.g. "nobody")
if(ent_pw != NULL && getuid() == 0)
{
if(chown(FTLfiles.log, ent_pw->pw_uid, ent_pw->pw_gid) == -1)
logg("Setting ownership (%i:%i) of %s failed: %s (%i)", ent_pw->pw_uid, ent_pw->pw_gid, FTLfiles.log, strerror(errno), errno);
if(database && chown(FTLfiles.db, ent_pw->pw_uid, ent_pw->pw_gid) == -1)
logg("Setting ownership (%i:%i) of %s failed: %s (%i)", ent_pw->pw_uid, ent_pw->pw_gid, FTLfiles.db, strerror(errno), errno);
}
}

// int cache_inserted, cache_live_freed are defined in dnsmasq/cache.c
Expand Down
2 changes: 1 addition & 1 deletion dnsmasq_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void FTL_reply(unsigned short flags, char *name, struct all_addr *addr, int id);
void FTL_cache(unsigned int flags, char *name, struct all_addr *addr, char * arg, int id);
void FTL_dnssec(int status, int id);
void FTL_dnsmasq_reload(void);
void FTL_fork_and_bind_sockets(void);
void FTL_fork_and_bind_sockets(struct passwd *ent_pw);

void FTL_header_ADbit(unsigned char header4, int id);

Expand Down