]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
Unify duplicated code for UIDs and GIDs
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 16 Aug 2022 12:58:08 +0000 (12:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 16 Aug 2022 12:58:08 +0000 (12:58 +0000)
This patch unifies the type which is the same for both.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/include/pakfire/pwd.h
src/libpakfire/jail.c
src/libpakfire/pakfire.c
src/libpakfire/pwd.c

index d08b29888e5573d85665d4cafd80a7361c9e839b..da9d4c6756742a72df4f6de23986ef519ee386cb 100644 (file)
@@ -136,8 +136,8 @@ int pakfire_sync(struct pakfire* pakfire, int solver_flags, int flags, int* chan
 
 int pakfire_on_root(struct pakfire* pakfire);
 
-const struct pakfire_subuid* pakfire_subuid(struct pakfire* pakfire);
-const struct pakfire_subgid* pakfire_subgid(struct pakfire* pakfire);
+const struct pakfire_subid* pakfire_subuid(struct pakfire* pakfire);
+const struct pakfire_subid* pakfire_subgid(struct pakfire* pakfire);
 
 void pakfire_log(struct pakfire* pakfire, int priority, const char *file,
        int line, const char *fn, const char *format, ...)
index bdf09573b7e716235ec7fa7b5af5fce944990457..dc50e02dd0443e97c7eb8929661c02f86c937949 100644 (file)
 #include <grp.h>
 #include <pwd.h>
 
-struct pakfire_subuid {
+struct pakfire_subid {
        char name[NAME_MAX];
-       uid_t uid;
-       size_t length;
-};
-
-struct pakfire_subgid {
-       char name[NAME_MAX];
-       gid_t gid;
+       uid_t id;
        size_t length;
 };
 
@@ -45,9 +39,9 @@ struct group* pakfire_getgrnam(struct pakfire* pakfire, const char* name);
 struct group* pakfire_getgrgid(struct pakfire* pakfire, gid_t gid);
 
 int pakfire_getsubuid(struct pakfire* pakfire, const uid_t uid,
-       struct pakfire_subuid* subuid);
+       struct pakfire_subid* subuid);
 int pakfire_getsubgid(struct pakfire* pakfire, const gid_t gid,
-       struct pakfire_subgid* subgid);
+       struct pakfire_subid* subgid);
 
 #endif
 
index 86f8d41aa1e8706fc8b78403ae9e6c91f2805029..83063e5662c86a96f2112413dc3f20d06ede5c5b 100644 (file)
@@ -872,7 +872,7 @@ ERROR:
 // UID/GID Mapping
 
 static int pakfire_jail_write_uidgid_mapping(struct pakfire_jail* jail,
-               const char* path, uid_t mapped_id, size_t length) {
+               const char* path, const struct pakfire_subid* subid) {
        int r = 1;
 
        // Open file for writing
@@ -883,7 +883,7 @@ static int pakfire_jail_write_uidgid_mapping(struct pakfire_jail* jail,
        }
 
        // Write configuration
-       int bytes_written = fprintf(f, "%d %u %lu\n", 0, mapped_id, length);
+       int bytes_written = fprintf(f, "%d %u %lu\n", 0, subid->id, subid->length);
        if (bytes_written <= 0) {
                ERROR(jail->pakfire, "Could not write UID/GID mapping: %m\n");
                goto ERROR;
@@ -917,7 +917,7 @@ static int pakfire_jail_setup_uid_mapping(struct pakfire_jail* jail, pid_t pid)
                return 0;
 
        // Fetch SUBUID
-       const struct pakfire_subuid* subuid = pakfire_subuid(jail->pakfire);
+       const struct pakfire_subid* subuid = pakfire_subuid(jail->pakfire);
        if (!subuid)
                return 1;
 
@@ -927,9 +927,9 @@ static int pakfire_jail_setup_uid_mapping(struct pakfire_jail* jail, pid_t pid)
                return 1;
 
        DEBUG(jail->pakfire, "Mapping UID range (%u - %lu)\n",
-               subuid->uid, subuid->uid + subuid->length);
+               subuid->id, subuid->id + subuid->length);
 
-       return pakfire_jail_write_uidgid_mapping(jail, path, subuid->uid, subuid->length);
+       return pakfire_jail_write_uidgid_mapping(jail, path, subuid);
 }
 
 static int pakfire_jail_setup_gid_mapping(struct pakfire_jail* jail, pid_t pid) {
@@ -941,7 +941,7 @@ static int pakfire_jail_setup_gid_mapping(struct pakfire_jail* jail, pid_t pid)
                return 0;
 
        // Fetch SUBGID
-       const struct pakfire_subgid* subgid = pakfire_subgid(jail->pakfire);
+       const struct pakfire_subid* subgid = pakfire_subgid(jail->pakfire);
        if (!subgid)
                return 1;
 
@@ -951,9 +951,9 @@ static int pakfire_jail_setup_gid_mapping(struct pakfire_jail* jail, pid_t pid)
                return 1;
 
        DEBUG(jail->pakfire, "Mapping GID range (%u - %lu)\n",
-               subgid->gid, subgid->gid + subgid->length);
+               subgid->id, subgid->id + subgid->length);
 
-       return pakfire_jail_write_uidgid_mapping(jail, path, subgid->gid, subgid->length);
+       return pakfire_jail_write_uidgid_mapping(jail, path, subgid);
 }
 
 static int pakfire_jail_setgroups(struct pakfire_jail* jail, pid_t pid) {
index a15045f2a3018d74ba86b995ddee13374be1469f..c9f905757ab2722811fb5ea93d720d78a52c3c5e 100644 (file)
@@ -84,8 +84,8 @@ struct pakfire {
        gid_t gid;
 
        // Mapped UID/GID
-       struct pakfire_subuid subuid;
-       struct pakfire_subgid subgid;
+       struct pakfire_subid subuid;
+       struct pakfire_subid subgid;
 
        // Pool
        Pool* pool;
@@ -154,11 +154,11 @@ int pakfire_on_root(struct pakfire* pakfire) {
        return (strcmp(pakfire->path, "/") == 0);
 }
 
-const struct pakfire_subuid* pakfire_subuid(struct pakfire* pakfire) {
+const struct pakfire_subid* pakfire_subuid(struct pakfire* pakfire) {
        return &pakfire->subuid;
 }
 
-const struct pakfire_subgid* pakfire_subgid(struct pakfire* pakfire) {
+const struct pakfire_subid* pakfire_subgid(struct pakfire* pakfire) {
        return &pakfire->subgid;
 }
 
@@ -726,8 +726,8 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
                }
 
                // Log
-               DEBUG(p, "  subuid = %u - %zu\n", p->subuid.uid, p->subuid.uid + p->subuid.length);
-               DEBUG(p, "  subgid = %u - %zu\n", p->subgid.gid, p->subgid.gid + p->subgid.length);
+               DEBUG(p, "  subuid = %u - %zu\n", p->subuid.id, p->subuid.id + p->subuid.length);
+               DEBUG(p, "  subgid = %u - %zu\n", p->subgid.id, p->subgid.id + p->subgid.length);
        }
 
        // Perform some safety checks
index a1b120eabdc578af6b508cb6d2625ae25cdc8f57..33d4b5f47b4eee999c7ec5b04b4fdbfcdd9e0e61 100644 (file)
@@ -154,10 +154,8 @@ struct group* pakfire_getgrgid(struct pakfire* pakfire, gid_t gid) {
 
 // SUBUID/SUBGID
 
-static struct pakfire_subuid __pakfire_subuid;
-static struct pakfire_subgid __pakfire_subgid;
-
-static struct pakfire_subuid* pakfire_fgetsubuid(struct pakfire* pakfire, FILE* f) {
+static struct pakfire_subid* pakfire_fgetsubid(struct pakfire* pakfire, FILE* f) {
+       static struct pakfire_subid subid;
        int r;
 
        char* line = NULL;
@@ -189,17 +187,17 @@ static struct pakfire_subuid* pakfire_fgetsubuid(struct pakfire* pakfire, FILE*
                switch (i++) {
                        // First field has the name
                        case 0:
-                               pakfire_string_set(__pakfire_subuid.name, token);
+                               pakfire_string_set(subid.name, token);
                                break;
 
-                       // Second field has the UID
+                       // Second field has the ID
                        case 1:
-                               __pakfire_subuid.uid = strtoul(token, NULL, 10);
+                               subid.id = strtoul(token, NULL, 10);
                                break;
 
                        // Third field has the length
                        case 2:
-                               __pakfire_subuid.length = strtoul(token, NULL, 10);
+                               subid.length = strtoul(token, NULL, 10);
                                break;
                }
 
@@ -207,7 +205,7 @@ static struct pakfire_subuid* pakfire_fgetsubuid(struct pakfire* pakfire, FILE*
        }
 
        // Check if length is greater than zero
-       if (__pakfire_subuid.length == 0) {
+       if (subid.length == 0) {
                DEBUG(pakfire, "Length equals zero: %s\n", line);
                r = 1;
        }
@@ -219,15 +217,15 @@ ERROR:
        if (r)
                return NULL;
 
-       DEBUG(pakfire, "Parsed SUBUID entry: name=%s, subuid=%d, length=%zu\n",
-               __pakfire_subuid.name, __pakfire_subuid.uid, __pakfire_subuid.length);
+       DEBUG(pakfire, "Parsed SUBID entry: name=%s, id=%d, length=%zu\n",
+               subid.name, subid.id, subid.length);
 
-       return &__pakfire_subuid;
+       return &subid;
 }
 
 int pakfire_getsubuid(struct pakfire* pakfire, const uid_t uid,
-               struct pakfire_subuid* subuid) {
-       struct pakfire_subuid* entry = NULL;
+               struct pakfire_subid* subuid) {
+       struct pakfire_subid* entry = NULL;
        int r = 1;
 
        // Fetch information about the running user
@@ -248,7 +246,7 @@ int pakfire_getsubuid(struct pakfire* pakfire, const uid_t uid,
 
        // Walk through all entries
        while (1) {
-               entry = pakfire_fgetsubuid(pakfire, f);
+               entry = pakfire_fgetsubid(pakfire, f);
                if (!entry)
                        break;
 
@@ -256,7 +254,7 @@ int pakfire_getsubuid(struct pakfire* pakfire, const uid_t uid,
 
                // Check for match
                if (strcmp(entry->name, passwd->pw_name) == 0) {
-                       subuid->uid    = entry->uid;
+                       subuid->id     = entry->id;
                        subuid->length = entry->length;
                        r = 0;
 
@@ -271,77 +269,9 @@ ERROR:
        return r;
 }
 
-static struct pakfire_subgid* pakfire_fgetsubgid(struct pakfire* pakfire, FILE* f) {
-       int r;
-
-       char* line = NULL;
-       size_t length = 0;
-       char* p = NULL;
-
-       // Read the next line
-       while (1) {
-               r = getline(&line, &length, f);
-               if (r < 0)
-                       goto ERROR;
-
-               // Try reading the next line if this one was empty
-               else if (r == 0)
-                       continue;
-
-               // Fall through
-               else
-                       break;
-       }
-
-       // Reset r
-       r = 0;
-
-       int i = 0;
-
-       char* token = strtok_r(line, ":", &p);
-       while (token) {
-               switch (i++) {
-                       // First field has the name
-                       case 0:
-                               pakfire_string_set(__pakfire_subgid.name, token);
-                               break;
-
-                       // Second field has the GID
-                       case 1:
-                               __pakfire_subgid.gid = strtoul(token, NULL, 10);
-                               break;
-
-                       // Third field has the length
-                       case 2:
-                               __pakfire_subgid.length = strtoul(token, NULL, 10);
-                               break;
-               }
-
-               token = strtok_r(NULL, ":", &p);
-       }
-
-       // Check if length is greater than zero
-       if (__pakfire_subgid.length == 0) {
-               DEBUG(pakfire, "Length equals zero: %s\n", line);
-               r = 1;
-       }
-
-ERROR:
-       if (line)
-               free(line);
-
-       if (r)
-               return NULL;
-
-       DEBUG(pakfire, "Parsed SUBGID entry: name=%s, subgid=%d, length=%zu\n",
-               __pakfire_subgid.name, __pakfire_subgid.gid, __pakfire_subgid.length);
-
-       return &__pakfire_subgid;
-}
-
 int pakfire_getsubgid(struct pakfire* pakfire, const gid_t gid,
-               struct pakfire_subgid* subgid) {
-       struct pakfire_subgid* entry = NULL;
+               struct pakfire_subid* subgid) {
+       struct pakfire_subid* entry = NULL;
        int r = 1;
 
        // Fetch information about the running user
@@ -362,7 +292,7 @@ int pakfire_getsubgid(struct pakfire* pakfire, const gid_t gid,
 
        // Walk through all entries
        while (1) {
-               entry = pakfire_fgetsubgid(pakfire, f);
+               entry = pakfire_fgetsubid(pakfire, f);
                if (!entry)
                        break;
 
@@ -370,7 +300,7 @@ int pakfire_getsubgid(struct pakfire* pakfire, const gid_t gid,
 
                // Check for match
                if (strcmp(entry->name, group->gr_name) == 0) {
-                       subgid->gid    = entry->gid;
+                       subgid->id     = entry->id;
                        subgid->length = entry->length;
                        r = 0;