]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
src/useradd.c: Add fmkomstemp() to fix mode of </etc/default/useradd>
authorAlejandro Colomar <alx@kernel.org>
Sun, 29 Sep 2024 11:09:40 +0000 (13:09 +0200)
committerSerge Hallyn <serge@hallyn.com>
Tue, 1 Oct 2024 19:38:59 +0000 (14:38 -0500)
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: <https://github.com/shadow-maint/shadow/pull/1080>
Reported-by: kugarocks <kugacola@gmail.com>
Suggested-by: kugarocks <kugacola@gmail.com>
Tested-by: kugarocks <kugacola@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
src/useradd.c

index d64fd892dd3f29cc8078d969b37d4de052a63130..1b0c10e3d45d2d8c9def6fbe5ed5ed4cddd7e019 100644 (file)
@@ -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;
 }