Skip to content

Commit 628d9d5

Browse files
authored
Merge pull request #1582 from pi-hole/tweak/chown-config
Chown config file after writing (if root)
2 parents 1369d21 + 85c2159 commit 628d9d5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/config/toml_helper.c

+22
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ void closeFTLtoml(FILE *fp)
6060
if(fclose(fp) != 0)
6161
log_err("Cannot close FTL's config file: %s", strerror(errno));
6262

63+
// Chown file if we are root
64+
if(geteuid() == 0)
65+
{
66+
// Get UID and GID of user with name "pihole"
67+
struct passwd *pwd = getpwnam("pihole");
68+
if(pwd == NULL)
69+
{
70+
log_warn("Cannot get UID and GID of user pihole: %s", strerror(errno));
71+
}
72+
else
73+
{
74+
const uid_t pihole_uid = pwd->pw_uid;
75+
const gid_t pihole_gid = pwd->pw_gid;
76+
// Chown file to pihole user
77+
if(chown(GLOBALTOMLPATH, pihole_uid, pihole_gid) != 0)
78+
log_warn("Cannot chown "GLOBALTOMLPATH" to pihole:pihole (%u:%u): %s",
79+
(unsigned int)pihole_uid, (unsigned int)pihole_gid, strerror(errno));
80+
else
81+
log_debug(DEBUG_CONFIG, "Chown-ed "GLOBALTOMLPATH" to pihole:pihole (%u:%u)",
82+
(unsigned int)pihole_uid, (unsigned int)pihole_gid);
83+
}
84+
}
6385
return;
6486
}
6587

0 commit comments

Comments
 (0)