chiark / gitweb /
sysusers: always treat ENOENT as entry-not-found when doing NSS calls
[elogind.git] / src / sysusers / sysusers.c
index 53ff9509d85202df2c6354365b9efeac7af5f5c3..d549969ff25bde7cb2f68f8ddd80ddc837fb2280 100644 (file)
@@ -53,7 +53,8 @@ typedef struct Item {
         bool gid_set:1;
         bool uid_set:1;
 
-        bool todo:1;
+        bool todo_user:1;
+        bool todo_group:1;
 } Item;
 
 static char *arg_root = NULL;
@@ -279,7 +280,7 @@ static int write_files(void) {
                                  * duplicate entries. */
 
                                 i = hashmap_get(groups, gr->gr_name);
-                                if (i && i->todo) {
+                                if (i && i->todo_group) {
                                         r = -EEXIST;
                                         goto finish;
                                 }
@@ -345,7 +346,7 @@ static int write_files(void) {
                         while ((pw = fgetpwent(original))) {
 
                                 i = hashmap_get(users, pw->pw_name);
-                                if (i && i->todo) {
+                                if (i && i->todo_user) {
                                         r = -EEXIST;
                                         goto finish;
                                 }
@@ -480,7 +481,7 @@ static int uid_is_ok(uid_t uid, const char *name) {
                 p = getpwuid(uid);
                 if (p)
                         return 0;
-                if (errno != 0)
+                if (!IN_SET(errno, 0, ENOENT))
                         return -errno;
 
                 errno = 0;
@@ -488,7 +489,7 @@ static int uid_is_ok(uid_t uid, const char *name) {
                 if (g) {
                         if (!streq(g->gr_name, name))
                                 return 0;
-                } else if (errno != 0)
+                } else if (!IN_SET(errno, 0, ENOENT))
                         return -errno;
         }
 
@@ -573,7 +574,7 @@ static int add_user(Item *i) {
         z = hashmap_get(database_user, i->name);
         if (z) {
                 log_debug("User %s already exists.", i->name);
-                i->uid = PTR_TO_GID(z);
+                i->uid = PTR_TO_UID(z);
                 i->uid_set = true;
                 return 0;
         }
@@ -594,7 +595,7 @@ static int add_user(Item *i) {
                         i->description = strdup(p->pw_gecos);
                         return 0;
                 }
-                if (errno != 0) {
+                if (!IN_SET(errno, 0, ENOENT)) {
                         log_error("Failed to check if user %s already exists: %m", i->name);
                         return -errno;
                 }
@@ -606,7 +607,7 @@ static int add_user(Item *i) {
                         log_error("User %s already exists in shadow database, but not in user database.", i->name);
                         return -EBADMSG;
                 }
-                if (errno != 0) {
+                if (!IN_SET(errno, 0, ENOENT)) {
                         log_error("Failed to check if user %s already exists in shadow database: %m", i->name);
                         return -errno;
                 }
@@ -691,7 +692,7 @@ static int add_user(Item *i) {
         if (r < 0)
                 return log_oom();
 
-        i->todo = true;
+        i->todo_user = true;
         log_info("Creating user %s (%s) with uid " UID_FMT " and gid " GID_FMT ".", i->name, strna(i->description), i->uid, i->gid);
 
         return 0;
@@ -719,14 +720,14 @@ static int gid_is_ok(gid_t gid) {
                 g = getgrgid(gid);
                 if (g)
                         return 0;
-                if (errno != 0)
+                if (!IN_SET(errno, 0, ENOENT))
                         return -errno;
 
                 errno = 0;
                 p = getpwuid((uid_t) gid);
                 if (p)
                         return 0;
-                if (errno != 0)
+                if (!IN_SET(errno, 0, ENOENT))
                         return -errno;
         }
 
@@ -760,7 +761,7 @@ static int add_group(Item *i) {
                         i->gid_set = true;
                         return 0;
                 }
-                if (errno != 0) {
+                if (!IN_SET(errno, 0, ENOENT)) {
                         log_error("Failed to check if group %s already exists: %m", i->name);
                         return -errno;
                 }
@@ -844,7 +845,7 @@ static int add_group(Item *i) {
         if (r < 0)
                 return log_oom();
 
-        i->todo = true;
+        i->todo_group = true;
         log_info("Creating group %s with gid " GID_FMT ".", i->name, i->gid);
 
         return 0;