From: Colin Walters <walters@verbum.org>
Date: Sun, 13 Jul 2014 13:35:33 -0700
Subject: sysusers: preserve label of /etc/{passwd, group}

These files are specially labeled on SELinux systems, and we need to
preserve that label.

(cherry picked from commit a334cbba7222d3d7d886c17c828fa4227c656535)
---
 src/sysusers/sysusers.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c
index 61c9bb5..6ec22cc 100644
--- a/src/sysusers/sysusers.c
+++ b/src/sysusers/sysusers.c
@@ -312,7 +312,11 @@ static int write_files(void) {
                 _cleanup_fclose_ FILE *original = NULL;
 
                 group_path = fix_root("/etc/group");
+                r = label_context_set("/etc/group", S_IFREG);
+                if (r < 0)
+                        goto finish;
                 r = fopen_temporary(group_path, &group, &group_tmp);
+                label_context_clear();
                 if (r < 0)
                         goto finish;
 
@@ -388,9 +392,14 @@ static int write_files(void) {
                 _cleanup_fclose_ FILE *original = NULL;
 
                 passwd_path = fix_root("/etc/passwd");
-                r = fopen_temporary(passwd_path, &passwd, &passwd_tmp);
+                r = label_context_set("/etc/passwd", S_IFREG);
                 if (r < 0)
                         goto finish;
+                r = fopen_temporary(passwd_path, &passwd, &passwd_tmp);
+                label_context_clear();
+                if (r < 0) {
+                        goto finish;
+                }
 
                 if (fchmod(fileno(passwd), 0644) < 0) {
                         r = -errno;
@@ -1527,6 +1536,8 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
+        label_init(NULL);
+
         r = 0;
 
         if (optind < argc) {
