chiark / gitweb /
treewide: more log_*_errno + return simplifications
[elogind.git] / src / sysusers / sysusers.c
index 1ff0e21a06eefaa389fed2e9a6557892f62f380d..88111029060af27df99839122f27d8523efb05de 100644 (file)
@@ -67,15 +67,7 @@ typedef struct Item {
 
 static char *arg_root = NULL;
 
-static const char conf_file_dirs[] =
-        "/etc/sysusers.d\0"
-        "/run/sysusers.d\0"
-        "/usr/local/lib/sysusers.d\0"
-        "/usr/lib/sysusers.d\0"
-#ifdef HAVE_SPLIT_USR
-        "/lib/sysusers.d\0"
-#endif
-        ;
+static const char conf_file_dirs[] = CONF_DIRS_NULSTR("sysusers");
 
 static Hashmap *users = NULL, *groups = NULL;
 static Hashmap *todo_uids = NULL, *todo_gids = NULL;
@@ -107,11 +99,11 @@ static int load_user_database(void) {
         if (!f)
                 return errno == ENOENT ? 0 : -errno;
 
-        r = hashmap_ensure_allocated(&database_user, string_hash_func, string_compare_func);
+        r = hashmap_ensure_allocated(&database_user, &string_hash_ops);
         if (r < 0)
                 return r;
 
-        r = hashmap_ensure_allocated(&database_uid, trivial_hash_func, trivial_compare_func);
+        r = hashmap_ensure_allocated(&database_uid, NULL);
         if (r < 0)
                 return r;
 
@@ -159,11 +151,11 @@ static int load_group_database(void) {
         if (!f)
                 return errno == ENOENT ? 0 : -errno;
 
-        r = hashmap_ensure_allocated(&database_group, string_hash_func, string_compare_func);
+        r = hashmap_ensure_allocated(&database_group, &string_hash_ops);
         if (r < 0)
                 return r;
 
-        r = hashmap_ensure_allocated(&database_gid, trivial_hash_func, trivial_compare_func);
+        r = hashmap_ensure_allocated(&database_gid, NULL);
         if (r < 0)
                 return r;
 
@@ -241,7 +233,8 @@ static int make_backup(const char *target, const char *x) {
 
         ts[0] = st.st_atim;
         ts[1] = st.st_mtim;
-        futimens(fileno(dst), ts);
+        if (futimens(fileno(dst), ts) < 0)
+                log_warning("Failed to fix access and modification time of %s: %m", backup);
 
         if (rename(temp, backup) < 0)
                 goto fail;
@@ -353,6 +346,21 @@ static int putsgent_with_members(const struct sgrp *sg, FILE *gshadow) {
         return 0;
 }
 
+static int sync_rights(FILE *from, FILE *to) {
+        struct stat st;
+
+        if (fstat(fileno(from), &st) < 0)
+                return -errno;
+
+        if (fchmod(fileno(to), st.st_mode & 07777) < 0)
+                return -errno;
+
+        if (fchown(fileno(to), st.st_uid, st.st_gid) < 0)
+                return -errno;
+
+        return 0;
+}
+
 static int write_files(void) {
 
         _cleanup_fclose_ FILE *passwd = NULL, *group = NULL, *shadow = NULL, *gshadow = NULL;
@@ -372,15 +380,14 @@ static int write_files(void) {
                 if (r < 0)
                         goto finish;
 
-                if (fchmod(fileno(group), 0644) < 0) {
-                        r = -errno;
-                        goto finish;
-                }
-
                 original = fopen(group_path, "re");
                 if (original) {
                         struct group *gr;
 
+                        r = sync_rights(original, group);
+                        if (r < 0)
+                                goto finish;
+
                         errno = 0;
                         while ((gr = fgetgrent(original))) {
                                 /* Safety checks against name and GID
@@ -418,6 +425,9 @@ static int write_files(void) {
                 } else if (errno != ENOENT) {
                         r = -errno;
                         goto finish;
+                } else if (fchmod(fileno(group), 0644) < 0) {
+                        r = -errno;
+                        goto finish;
                 }
 
                 HASHMAP_FOREACH(i, todo_gids, iterator) {
@@ -449,15 +459,14 @@ static int write_files(void) {
                 if (r < 0)
                         goto finish;
 
-                if (fchmod(fileno(gshadow), 0000) < 0) {
-                        r = -errno;
-                        goto finish;
-                }
-
                 original = fopen(gshadow_path, "re");
                 if (original) {
                         struct sgrp *sg;
 
+                        r = sync_rights(original, gshadow);
+                        if (r < 0)
+                                goto finish;
+
                         errno = 0;
                         while ((sg = fgetsgent(original))) {
 
@@ -483,6 +492,9 @@ static int write_files(void) {
                 } else if (errno != ENOENT) {
                         r = -errno;
                         goto finish;
+                } else if (fchmod(fileno(gshadow), 0000) < 0) {
+                        r = -errno;
+                        goto finish;
                 }
 
                 HASHMAP_FOREACH(i, todo_gids, iterator) {
@@ -513,15 +525,14 @@ static int write_files(void) {
                 if (r < 0)
                         goto finish;
 
-                if (fchmod(fileno(passwd), 0644) < 0) {
-                        r = -errno;
-                        goto finish;
-                }
-
                 original = fopen(passwd_path, "re");
                 if (original) {
                         struct passwd *pw;
 
+                        r = sync_rights(original, passwd);
+                        if (r < 0)
+                                goto finish;
+
                         errno = 0;
                         while ((pw = fgetpwent(original))) {
 
@@ -552,6 +563,9 @@ static int write_files(void) {
                 } else if (errno != ENOENT) {
                         r = -errno;
                         goto finish;
+                } else if (fchmod(fileno(passwd), 0644) < 0) {
+                        r = -errno;
+                        goto finish;
                 }
 
                 HASHMAP_FOREACH(i, todo_uids, iterator) {
@@ -596,15 +610,14 @@ static int write_files(void) {
                 if (r < 0)
                         goto finish;
 
-                if (fchmod(fileno(shadow), 0000) < 0) {
-                        r = -errno;
-                        goto finish;
-                }
-
                 original = fopen(shadow_path, "re");
                 if (original) {
                         struct spwd *sp;
 
+                        r = sync_rights(original, shadow);
+                        if (r < 0)
+                                goto finish;
+
                         errno = 0;
                         while ((sp = fgetspent(original))) {
 
@@ -629,6 +642,9 @@ static int write_files(void) {
                 } else if (errno != ENOENT) {
                         r = -errno;
                         goto finish;
+                } else if (fchmod(fileno(shadow), 0000) < 0) {
+                        r = -errno;
+                        goto finish;
                 }
 
                 lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
@@ -796,8 +812,8 @@ static int root_stat(const char *p, struct stat *st) {
 static int read_id_from_file(Item *i, uid_t *_uid, gid_t *_gid) {
         struct stat st;
         bool found_uid = false, found_gid = false;
-        uid_t uid;
-        gid_t gid;
+        uid_t uid = 0;
+        gid_t gid = 0;
 
         assert(i);
 
@@ -904,7 +920,7 @@ static int add_user(Item *i) {
         if (i->uid_set) {
                 r = uid_is_ok(i->uid, i->name);
                 if (r < 0) {
-                        log_error("Failed to verify uid " UID_FMT ": %s", i->uid, strerror(-r));
+                        log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid);
                         return r;
                 }
                 if (r == 0) {
@@ -924,7 +940,7 @@ static int add_user(Item *i) {
                         else {
                                 r = uid_is_ok(c, i->name);
                                 if (r < 0) {
-                                        log_error("Failed to verify uid " UID_FMT ": %s", i->uid, strerror(-r));
+                                        log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid);
                                         return r;
                                 } else if (r > 0) {
                                         i->uid = c;
@@ -939,7 +955,7 @@ static int add_user(Item *i) {
         if (!i->uid_set && i->gid_set) {
                 r = uid_is_ok((uid_t) i->gid, i->name);
                 if (r < 0) {
-                        log_error("Failed to verify uid " UID_FMT ": %s", i->uid, strerror(-r));
+                        log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid);
                         return r;
                 }
                 if (r > 0) {
@@ -959,7 +975,7 @@ static int add_user(Item *i) {
 
                         r = uid_is_ok(search_uid, i->name);
                         if (r < 0) {
-                                log_error("Failed to verify uid " UID_FMT ": %s", i->uid, strerror(-r));
+                                log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid);
                                 return r;
                         } else if (r > 0)
                                 break;
@@ -969,7 +985,7 @@ static int add_user(Item *i) {
                 i->uid = search_uid;
         }
 
-        r = hashmap_ensure_allocated(&todo_uids, trivial_hash_func, trivial_compare_func);
+        r = hashmap_ensure_allocated(&todo_uids, NULL);
         if (r < 0)
                 return log_oom();
 
@@ -1056,7 +1072,7 @@ static int add_group(Item *i) {
         if (i->gid_set) {
                 r = gid_is_ok(i->gid);
                 if (r < 0) {
-                        log_error("Failed to verify gid " GID_FMT ": %s", i->gid, strerror(-r));
+                        log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid);
                         return r;
                 }
                 if (r == 0) {
@@ -1069,7 +1085,7 @@ static int add_group(Item *i) {
         if (!i->gid_set && i->uid_set) {
                 r = gid_is_ok((gid_t) i->uid);
                 if (r < 0) {
-                        log_error("Failed to verify gid " GID_FMT ": %s", i->gid, strerror(-r));
+                        log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid);
                         return r;
                 }
                 if (r > 0) {
@@ -1089,7 +1105,7 @@ static int add_group(Item *i) {
                         else {
                                 r = gid_is_ok(c);
                                 if (r < 0) {
-                                        log_error("Failed to verify gid " GID_FMT ": %s", i->gid, strerror(-r));
+                                        log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid);
                                         return r;
                                 } else if (r > 0) {
                                         i->gid = c;
@@ -1112,7 +1128,7 @@ static int add_group(Item *i) {
 
                         r = gid_is_ok(search_uid);
                         if (r < 0) {
-                                log_error("Failed to verify gid " GID_FMT ": %s", i->gid, strerror(-r));
+                                log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid);
                                 return r;
                         } else if (r > 0)
                                 break;
@@ -1122,7 +1138,7 @@ static int add_group(Item *i) {
                 i->gid = search_uid;
         }
 
-        r = hashmap_ensure_allocated(&todo_gids, trivial_hash_func, trivial_compare_func);
+        r = hashmap_ensure_allocated(&todo_gids, NULL);
         if (r < 0)
                 return log_oom();
 
@@ -1210,7 +1226,7 @@ static int add_implicit(void) {
                 if (!i) {
                         _cleanup_(item_freep) Item *j = NULL;
 
-                        r = hashmap_ensure_allocated(&groups, string_hash_func, string_compare_func);
+                        r = hashmap_ensure_allocated(&groups, &string_hash_ops);
                         if (r < 0)
                                 return log_oom();
 
@@ -1237,7 +1253,7 @@ static int add_implicit(void) {
                         if (!i) {
                                 _cleanup_(item_freep) Item *j = NULL;
 
-                                r = hashmap_ensure_allocated(&users, string_hash_func, string_compare_func);
+                                r = hashmap_ensure_allocated(&users, &string_hash_ops);
                                 if (r < 0)
                                         return log_oom();
 
@@ -1542,7 +1558,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                         return -EINVAL;
                 }
 
-                r = hashmap_ensure_allocated(&members, string_hash_func, string_compare_func);
+                r = hashmap_ensure_allocated(&members, &string_hash_ops);
                 if (r < 0)
                         return log_oom();
 
@@ -1584,7 +1600,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                         return -EINVAL;
                 }
 
-                r = hashmap_ensure_allocated(&users, string_hash_func, string_compare_func);
+                r = hashmap_ensure_allocated(&users, &string_hash_ops);
                 if (r < 0)
                         return log_oom();
 
@@ -1634,7 +1650,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                         return -EINVAL;
                 }
 
-                r = hashmap_ensure_allocated(&groups, string_hash_func, string_compare_func);
+                r = hashmap_ensure_allocated(&groups, &string_hash_ops);
                 if (r < 0)
                         return log_oom();
 
@@ -1693,7 +1709,7 @@ static int read_config_file(const char *fn, bool ignore_enoent) {
         FILE *f = NULL;
         char line[LINE_MAX];
         unsigned v = 0;
-        int r;
+        int r = 0;
 
         assert(fn);
 
@@ -1705,7 +1721,7 @@ static int read_config_file(const char *fn, bool ignore_enoent) {
                         if (ignore_enoent && r == -ENOENT)
                                 return 0;
 
-                        log_error("Failed to open '%s', ignoring: %s", fn, strerror(-r));
+                        log_error_errno(r, "Failed to open '%s', ignoring: %m", fn);
                         return r;
                 }
 
@@ -1835,9 +1851,9 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        r = label_init(NULL);
+        r = mac_selinux_init(NULL);
         if (r < 0) {
-                log_error("SELinux setup failed: %s", strerror(-r));
+                log_error_errno(r, "SELinux setup failed: %m");
                 goto finish;
         }
 
@@ -1855,7 +1871,7 @@ int main(int argc, char *argv[]) {
 
                 r = conf_files_list_nulstr(&files, ".conf", arg_root, conf_file_dirs);
                 if (r < 0) {
-                        log_error("Failed to enumerate sysusers.d files: %s", strerror(-r));
+                        log_error_errno(r, "Failed to enumerate sysusers.d files: %m");
                         goto finish;
                 }
 
@@ -1881,19 +1897,19 @@ int main(int argc, char *argv[]) {
 
         lock = take_password_lock(arg_root);
         if (lock < 0) {
-                log_error("Failed to take lock: %s", strerror(-lock));
+                log_error_errno(lock, "Failed to take lock: %m");
                 goto finish;
         }
 
         r = load_user_database();
         if (r < 0) {
-                log_error("Failed to load user database: %s", strerror(-r));
+                log_error_errno(r, "Failed to load user database: %m");
                 goto finish;
         }
 
         r = load_group_database();
         if (r < 0) {
-                log_error("Failed to read group database: %s", strerror(-r));
+                log_error_errno(r, "Failed to read group database: %m");
                 goto finish;
         }
 
@@ -1905,7 +1921,7 @@ int main(int argc, char *argv[]) {
 
         r = write_files();
         if (r < 0)
-                log_error("Failed to write files: %s", strerror(-r));
+                log_error_errno(r, "Failed to write files: %m");
 
 finish:
         while ((i = hashmap_steal_first(groups)))