From 7e4c69635ae2ed6f073e4b4b3b67f3c31c554813 Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Tue, 12 Nov 2024 13:12:08 +0100 Subject: [PATCH] Use `chmod_chown()` API from utils * `chmod_chown()` returns EINVAL if path is NULL Signed-off-by: Toni Uhlig --- examples/c-analysed/c-analysed.c | 67 ++++++++++++++------------------ utils.c | 5 +++ 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/examples/c-analysed/c-analysed.c b/examples/c-analysed/c-analysed.c index 2f175982..f1a0cf98 100644 --- a/examples/c-analysed/c-analysed.c +++ b/examples/c-analysed/c-analysed.c @@ -1307,7 +1307,9 @@ static enum nDPIsrvd_callback_return process_analyse_events(struct nDPIsrvd_sock if (BUFFER_REMAINING(csv_risks_used) > 0) { risks[csv_risks_used] = '\0'; - } else { + } + else + { risks[csv_risks_used - 1] = '\0'; } csv_buf_add(buf, &csv_buf_used, risks, csv_risks_used); @@ -1436,6 +1438,13 @@ static int parse_options(int argc, char ** argv) { opt = 1; } + else + { + if (chmod_chown(csv_outfile, S_IRUSR | S_IWUSR, "root", "root") != 0) + { + // skip "unused result" warning + } + } csv_fp = fopen(csv_outfile, "a+"); if (csv_fp == NULL) @@ -1474,6 +1483,13 @@ static int parse_options(int argc, char ** argv) { opt = 1; } + else + { + if (chmod_chown(stats_csv_outfile, S_IRUSR | S_IWUSR, "root", "root") != 0) + { + // skip "unused result" warning + } + } stats_csv_fp = fopen(stats_csv_outfile, "a+"); if (stats_csv_fp == NULL) @@ -2026,47 +2042,22 @@ int main(int argc, char ** argv) goto failure; } - if (user != NULL) + if (csv_outfile != NULL) { - struct passwd * pwd; - struct group * grp; - gid_t gid; - - errno = 0; - pwd = getpwnam(user); - if (pwd == NULL) + int ret = chmod_chown(csv_outfile, S_IRUSR | S_IWUSR | S_IRGRP, user, group); + if (ret != 0) { - logger_early(1, "Get user failed: %s", strerror(errno)); - goto failure; + logger_early(1, "Could not chmod/chown `%s': %s", csv_outfile, strerror(ret)); + return 1; } - - if (group != NULL) + } + if (stats_csv_outfile != NULL) + { + int ret = chmod_chown(stats_csv_outfile, S_IRUSR | S_IWUSR | S_IRGRP, user, group); + if (ret != 0) { - errno = 0; - grp = getgrnam(group); - if (grp == NULL) - { - logger_early(1, "Get group failed: %s", strerror(errno)); - goto failure; - } - gid = grp->gr_gid; - } - else - { - gid = pwd->pw_gid; - } - - if (csv_outfile != NULL && - (chmod(csv_outfile, S_IRUSR | S_IWUSR) != 0 || chown(csv_outfile, pwd->pw_uid, gid) != 0)) - { - logger_early(1, "Change user/group of `%s' failed: %s", csv_outfile, strerror(errno)); - goto failure; - } - if (stats_csv_outfile != NULL && - (chmod(stats_csv_outfile, S_IRUSR | S_IWUSR) != 0 || chown(stats_csv_outfile, pwd->pw_uid, gid) != 0)) - { - logger_early(1, "Change user/group of `%s' failed: %s", stats_csv_outfile, strerror(errno)); - goto failure; + logger_early(1, "Could not chmod/chown `%s': %s", stats_csv_outfile, strerror(ret)); + return 1; } } diff --git a/utils.c b/utils.c index 25f16f81..716bed23 100644 --- a/utils.c +++ b/utils.c @@ -396,6 +396,11 @@ int chmod_chown(char const * const path, mode_t mode, char const * const user, c uid_t path_uid = (uid_t)-1; gid_t path_gid = (gid_t)-1; + if (path == NULL) + { + return EINVAL; + } + if (mode != 0) { if (chmod(path, mode) != 0)