47313b1
From c3a8638cf447ae5901dccb89ea766d768febe529 Mon Sep 17 00:00:00 2001
b397f2a
From: Colin Guthrie <colin@mageia.org>
b397f2a
Date: Wed, 29 Oct 2014 14:03:41 +0000
b397f2a
Subject: [PATCH] sysusers: Preserve ownership and mode on /etc/passwd and
b397f2a
 friends
b397f2a
b397f2a
When running sysusers we would clobber file ownership and permissions
b397f2a
on the files /etc/passwd, /etc/group and /etc/[g]shadow.
b397f2a
b397f2a
This simply preserves the ownership and mode if existing files are
b397f2a
found.
b397f2a
b397f2a
(cherry picked from commit e3c72c21d62aadabf4df436c3e2c7219eeeccc1c)
b397f2a
---
b397f2a
 src/sysusers/sysusers.c | 61 +++++++++++++++++++++++++++++++++----------------
b397f2a
 1 file changed, 41 insertions(+), 20 deletions(-)
b397f2a
b397f2a
diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c
b397f2a
index 9b9be96a0a..c133dc5f10 100644
b397f2a
--- a/src/sysusers/sysusers.c
b397f2a
+++ b/src/sysusers/sysusers.c
b397f2a
@@ -358,6 +358,7 @@ static int write_files(void) {
b397f2a
         _cleanup_fclose_ FILE *passwd = NULL, *group = NULL, *shadow = NULL, *gshadow = NULL;
b397f2a
         _cleanup_free_ char *passwd_tmp = NULL, *group_tmp = NULL, *shadow_tmp = NULL, *gshadow_tmp = NULL;
b397f2a
         const char *passwd_path = NULL, *group_path = NULL, *shadow_path = NULL, *gshadow_path = NULL;
b397f2a
+        struct stat st;
b397f2a
         bool group_changed = false;
b397f2a
         Iterator iterator;
b397f2a
         Item *i;
b397f2a
@@ -372,15 +373,17 @@ static int write_files(void) {
b397f2a
                 if (r < 0)
b397f2a
                         goto finish;
b397f2a
 
b397f2a
-                if (fchmod(fileno(group), 0644) < 0) {
b397f2a
-                        r = -errno;
b397f2a
-                        goto finish;
b397f2a
-                }
b397f2a
-
b397f2a
                 original = fopen(group_path, "re");
b397f2a
                 if (original) {
b397f2a
                         struct group *gr;
b397f2a
 
b397f2a
+                        if (fstat(fileno(original), &st) < 0 ||
b397f2a
+                            fchmod(fileno(group), st.st_mode & 07777) < 0 ||
b397f2a
+                            fchown(fileno(group), st.st_uid, st.st_gid) < 0) {
b397f2a
+                                r = -errno;
b397f2a
+                                goto finish;
b397f2a
+                        }
b397f2a
+
b397f2a
                         errno = 0;
b397f2a
                         while ((gr = fgetgrent(original))) {
b397f2a
                                 /* Safety checks against name and GID
b397f2a
@@ -418,6 +421,9 @@ static int write_files(void) {
b397f2a
                 } else if (errno != ENOENT) {
b397f2a
                         r = -errno;
b397f2a
                         goto finish;
b397f2a
+                } else if (fchmod(fileno(group), 0644) < 0) {
b397f2a
+                        r = -errno;
b397f2a
+                        goto finish;
b397f2a
                 }
b397f2a
 
b397f2a
                 HASHMAP_FOREACH(i, todo_gids, iterator) {
b397f2a
@@ -449,15 +455,17 @@ static int write_files(void) {
b397f2a
                 if (r < 0)
b397f2a
                         goto finish;
b397f2a
 
b397f2a
-                if (fchmod(fileno(gshadow), 0000) < 0) {
b397f2a
-                        r = -errno;
b397f2a
-                        goto finish;
b397f2a
-                }
b397f2a
-
b397f2a
                 original = fopen(gshadow_path, "re");
b397f2a
                 if (original) {
b397f2a
                         struct sgrp *sg;
b397f2a
 
b397f2a
+                        if (fstat(fileno(original), &st) < 0 ||
b397f2a
+                            fchmod(fileno(gshadow), st.st_mode & 07777) < 0 ||
b397f2a
+                            fchown(fileno(gshadow), st.st_uid, st.st_gid) < 0) {
b397f2a
+                                r = -errno;
b397f2a
+                                goto finish;
b397f2a
+                        }
b397f2a
+
b397f2a
                         errno = 0;
b397f2a
                         while ((sg = fgetsgent(original))) {
b397f2a
 
b397f2a
@@ -483,6 +491,9 @@ static int write_files(void) {
b397f2a
                 } else if (errno != ENOENT) {
b397f2a
                         r = -errno;
b397f2a
                         goto finish;
b397f2a
+                } else if (fchmod(fileno(gshadow), 0000) < 0) {
b397f2a
+                        r = -errno;
b397f2a
+                        goto finish;
b397f2a
                 }
b397f2a
 
b397f2a
                 HASHMAP_FOREACH(i, todo_gids, iterator) {
b397f2a
@@ -513,15 +524,17 @@ static int write_files(void) {
b397f2a
                 if (r < 0)
b397f2a
                         goto finish;
b397f2a
 
b397f2a
-                if (fchmod(fileno(passwd), 0644) < 0) {
b397f2a
-                        r = -errno;
b397f2a
-                        goto finish;
b397f2a
-                }
b397f2a
-
b397f2a
                 original = fopen(passwd_path, "re");
b397f2a
                 if (original) {
b397f2a
                         struct passwd *pw;
b397f2a
 
b397f2a
+                        if (fstat(fileno(original), &st) < 0 ||
b397f2a
+                            fchmod(fileno(passwd), st.st_mode & 07777) < 0 ||
b397f2a
+                            fchown(fileno(passwd), st.st_uid, st.st_gid) < 0) {
b397f2a
+                                r = -errno;
b397f2a
+                                goto finish;
b397f2a
+                        }
b397f2a
+
b397f2a
                         errno = 0;
b397f2a
                         while ((pw = fgetpwent(original))) {
b397f2a
 
b397f2a
@@ -552,6 +565,9 @@ static int write_files(void) {
b397f2a
                 } else if (errno != ENOENT) {
b397f2a
                         r = -errno;
b397f2a
                         goto finish;
b397f2a
+                } else if (fchmod(fileno(passwd), 0644) < 0) {
b397f2a
+                        r = -errno;
b397f2a
+                        goto finish;
b397f2a
                 }
b397f2a
 
b397f2a
                 HASHMAP_FOREACH(i, todo_uids, iterator) {
b397f2a
@@ -596,15 +612,17 @@ static int write_files(void) {
b397f2a
                 if (r < 0)
b397f2a
                         goto finish;
b397f2a
 
b397f2a
-                if (fchmod(fileno(shadow), 0000) < 0) {
b397f2a
-                        r = -errno;
b397f2a
-                        goto finish;
b397f2a
-                }
b397f2a
-
b397f2a
                 original = fopen(shadow_path, "re");
b397f2a
                 if (original) {
b397f2a
                         struct spwd *sp;
b397f2a
 
b397f2a
+                        if (fstat(fileno(original), &st) < 0 ||
b397f2a
+                            fchmod(fileno(shadow), st.st_mode & 07777) < 0 ||
b397f2a
+                            fchown(fileno(shadow), st.st_uid, st.st_gid) < 0) {
b397f2a
+                                r = -errno;
b397f2a
+                                goto finish;
b397f2a
+                        }
b397f2a
+
b397f2a
                         errno = 0;
b397f2a
                         while ((sp = fgetspent(original))) {
b397f2a
 
b397f2a
@@ -629,6 +647,9 @@ static int write_files(void) {
b397f2a
                 } else if (errno != ENOENT) {
b397f2a
                         r = -errno;
b397f2a
                         goto finish;
b397f2a
+                } else if (fchmod(fileno(shadow), 0000) < 0) {
b397f2a
+                        r = -errno;
b397f2a
+                        goto finish;
b397f2a
                 }
b397f2a
 
b397f2a
                 lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);