From be9fe875f6f415cdb022179eb35db36568e68874 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 20 May 2024 19:33:25 +0200 Subject: [PATCH] Always try to chown auxiliary database files, not only when chown of the database files itself failed Signed-off-by: DL6ER --- .github/.codespellignore | 1 + src/dnsmasq_interface.c | 64 ++++++++++++++++++++++------------------ 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/.github/.codespellignore b/.github/.codespellignore index cdccd1cd..645d300f 100644 --- a/.github/.codespellignore +++ b/.github/.codespellignore @@ -7,3 +7,4 @@ requestor requestors punycode bitap +mmapped diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index dd337c5b..4bef6109 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -2933,50 +2933,58 @@ void FTL_fork_and_bind_sockets(struct passwd *ent_pw, bool dnsmasq_start) if(getuid() == 0) { // Only print this and change ownership of shmem objects when - // we're actually dropping root (user/group my be set to root) + // we're actually dropping root (user/group may be set to root) if(ent_pw != NULL && ent_pw->pw_uid != 0) { log_info("FTL is going to drop from root to user %s (UID %u)", ent_pw->pw_name, ent_pw->pw_uid); + + // Change ownership of shared memory objects + chown_all_shmem(ent_pw); + + // Configured FTL log file if(chown(config.files.log.ftl.v.s, ent_pw->pw_uid, ent_pw->pw_gid) == -1) { log_warn("Setting ownership (%u:%u) of %s failed: %s (%i)", ent_pw->pw_uid, ent_pw->pw_gid, config.files.log.ftl.v.s, strerror(errno), errno); } + // Configured FTL database file if(chown(config.files.database.v.s, ent_pw->pw_uid, ent_pw->pw_gid) == -1) { log_warn("Setting ownership (%u:%u) of %s failed: %s (%i)", ent_pw->pw_uid, ent_pw->pw_gid, config.files.database.v.s, strerror(errno), errno); - // Check if WAL files are present and change - // their ownership, too - char *walname = calloc(strlen(config.files.database.v.s) + 5, sizeof(char)); - if(walname != NULL) - { - strcpy(walname, config.files.database.v.s); - strcat(walname, "-wal"); - if(chown(walname, ent_pw->pw_uid, ent_pw->pw_gid) == -1) - { - log_warn("Setting ownership (%u:%u) of %s failed: %s (%i)", - ent_pw->pw_uid, ent_pw->pw_gid, walname, strerror(errno), errno); - } - free(walname); - } - char *shmname = calloc(strlen(config.files.database.v.s) + 5, sizeof(char)); - if(shmname != NULL) - { - strcpy(shmname, config.files.database.v.s); - strcat(shmname, "-shm"); - if(chown(shmname, ent_pw->pw_uid, ent_pw->pw_gid) == -1) - { - log_warn("Setting ownership (%u:%u) of %s failed: %s (%i)", - ent_pw->pw_uid, ent_pw->pw_gid, shmname, strerror(errno), errno); - } - free(shmname); - } } - chown_all_shmem(ent_pw); + + // Check if auxiliary files exist and change ownership + char *extrafile = calloc(strlen(config.files.database.v.s) + 5, sizeof(char)); + if(extrafile == NULL) + { + log_err("Memory allocation failed. Skipping some file ownership checks."); + return; + } + + // Check -wal file (write-ahead log) + strcpy(extrafile, config.files.database.v.s); + strcat(extrafile, "-wal"); + if(file_exists(extrafile) && chown(extrafile, ent_pw->pw_uid, ent_pw->pw_gid) == -1) + { + log_warn("Setting ownership (%u:%u) of %s failed: %s (%i)", + ent_pw->pw_uid, ent_pw->pw_gid, extrafile, strerror(errno), errno); + } + + // Check -shm file (mmapped shared memory) + strcpy(extrafile, config.files.database.v.s); + strcat(extrafile, "-shm"); + if(file_exists(extrafile) && chown(extrafile, ent_pw->pw_uid, ent_pw->pw_gid) == -1) + { + log_warn("Setting ownership (%u:%u) of %s failed: %s (%i)", + ent_pw->pw_uid, ent_pw->pw_gid, extrafile, strerror(errno), errno); + } + + // Free allocated memory + free(extrafile); } else {