From 759d2373e43700cb5938c4a110174cbb34fbbecb Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 29 Sep 2024 13:09:40 +0200 Subject: [PATCH] src/useradd.c: Add fmkomstemp() to fix mode of The mode of the file should be 644, but mkstemp(2) was transforming it to 600. To do this, we need a function that accepts a mode parameter. While we don't need a flags parameter, to avoid confusion with mkostemp(2), let's add both a flags and a mode parameter. Link: Reported-by: kugarocks Suggested-by: kugarocks Tested-by: kugarocks Signed-off-by: Alejandro Colomar --- src/useradd.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/useradd.c b/src/useradd.c index d64fd892d..1b0c10e3d 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -242,7 +242,7 @@ static void create_home (void); static void create_mail (void); static void check_uid_range(int rflg, uid_t user_id); -static FILE *fmkstemp(char *template); +static FILE *fmkomstemp(char *template, unsigned int flags, mode_t m); /* @@ -581,7 +581,7 @@ set_defaults(void) /* * Create a temporary file to copy the new output to. */ - ofp = fmkstemp(new_file); + ofp = fmkomstemp(new_file, 0, 0644); if (NULL == ofp) { fprintf (stderr, _("%s: cannot open new defaults file\n"), @@ -2737,21 +2737,25 @@ int main (int argc, char **argv) static FILE * -fmkstemp(char *template) +fmkomstemp(char *template, unsigned int flags, mode_t m) { int fd; FILE *fp; - fd = mkstemp(template); + fd = mkostemp(template, flags); if (fd == -1) return NULL; + if (fchmod(fd, m) == -1) + goto fail; + fp = fdopen(fd, "w"); - if (fp == NULL) { - close(fd); - unlink(template); - return NULL; - } + if (fp == NULL) + goto fail; return fp; +fail: + close(fd); + unlink(template); + return NULL; } -- 2.47.3