diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2024-11-12 13:12:08 +0100 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2024-11-14 13:47:46 +0100 |
commit | 7e4c69635ae2ed6f073e4b4b3b67f3c31c554813 (patch) | |
tree | 6e222b9a46d969e76f473e198263ce9d5090c975 | |
parent | 9105b393e1df76c9d4d11e3a75cd160a724493bd (diff) |
Use `chmod_chown()` API from utils
* `chmod_chown()` returns EINVAL if path is NULL
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | examples/c-analysed/c-analysed.c | 67 | ||||
-rw-r--r-- | 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 2f1759823..f1a0cf982 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; - } - - if (group != NULL) - { - 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; + logger_early(1, "Could not chmod/chown `%s': %s", csv_outfile, strerror(ret)); + return 1; } - if (stats_csv_outfile != NULL && - (chmod(stats_csv_outfile, S_IRUSR | S_IWUSR) != 0 || chown(stats_csv_outfile, pwd->pw_uid, gid) != 0)) + } + if (stats_csv_outfile != NULL) + { + int ret = chmod_chown(stats_csv_outfile, S_IRUSR | S_IWUSR | S_IRGRP, user, group); + if (ret != 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; } } @@ -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) |