]> git.ipfire.org Git - pakfire.git/commitdiff
pwd: Sum up reading subuid/subgid
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 31 Oct 2022 10:18:30 +0000 (10:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 31 Oct 2022 10:18:30 +0000 (10:18 +0000)
I misinterpreted the format of /etc/subgid and used the group name when
instead it is the same format as /etc/subuid.

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

index dc8cdc6869fa36cd3348aa088e8307d26cbf1644..b0468e7697e6c3ba3dce2d176defcb0da549c667 100644 (file)
@@ -38,10 +38,8 @@ struct passwd* pakfire_getpwuid(struct pakfire* pakfire, uid_t uid);
 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_subid* subuid);
-int pakfire_getsubgid(struct pakfire* pakfire, const gid_t gid,
-       struct pakfire_subid* subgid);
+int pakfire_getsubid(struct pakfire* pakfire, const char* path,
+       const uid_t uid, struct pakfire_subid* subid);
 
 #endif
 
index 3ef63f34ddcfa189b356fc21cd46f782d9dafc51..8c1223ae688ee76a997a2f48bb625ccdf66b49fb 100644 (file)
@@ -788,14 +788,14 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
        // Fetch sub UID/GIDs
        if (!pakfire_on_root(p)) {
                // UID
-               r = pakfire_getsubuid(p, p->uid, &p->subuid);
+               r = pakfire_getsubid(p, "/etc/subuid", p->uid, &p->subuid);
                if (r) {
                        ERROR(p, "Could not fetch subuid: %m\n");
                        goto ERROR;
                }
 
                // GID
-               r = pakfire_getsubgid(p, p->gid, &p->subgid);
+               r = pakfire_getsubid(p, "/etc/subgid", p->uid, &p->subgid);
                if (r) {
                        ERROR(p, "Could not fetch subgid: %m\n");
                        goto ERROR;
index 3adef05e8d81d6c27f2c886f94b8f70e91a46e76..756339d0bfa0a7089903adbf9aff5e623793ad15 100644 (file)
@@ -226,8 +226,8 @@ ERROR:
        return &subid;
 }
 
-int pakfire_getsubuid(struct pakfire* pakfire, const uid_t uid,
-               struct pakfire_subid* subuid) {
+int pakfire_getsubid(struct pakfire* pakfire, const char* path, const uid_t uid,
+               struct pakfire_subid* subid) {
        struct pakfire_subid* entry = NULL;
        int r = 1;
 
@@ -238,10 +238,10 @@ int pakfire_getsubuid(struct pakfire* pakfire, const uid_t uid,
                return 1;
        }
 
-       DEBUG(pakfire, "Fetching SUBUID for %s (%d)\n", passwd->pw_name, uid);
+       DEBUG(pakfire, "Fetching SUBID from %s for %s (%d)\n", path, passwd->pw_name, uid);
 
        // Open /etc/subuid
-       FILE* f = fopen(ETC_SUBUID, "r");
+       FILE* f = fopen(path, "r");
        if (!f) {
                ERROR(pakfire, "Could not open %s: %m\n", ETC_SUBUID);
                goto ERROR;
@@ -257,54 +257,8 @@ int pakfire_getsubuid(struct pakfire* pakfire, const uid_t uid,
 
                // Check for match
                if (strcmp(entry->name, passwd->pw_name) == 0) {
-                       subuid->id     = entry->id;
-                       subuid->length = entry->length;
-                       r = 0;
-
-                       break;
-               }
-       }
-
-ERROR:
-       if (f)
-               fclose(f);
-
-       return r;
-}
-
-int pakfire_getsubgid(struct pakfire* pakfire, const gid_t gid,
-               struct pakfire_subid* subgid) {
-       struct pakfire_subid* entry = NULL;
-       int r = 1;
-
-       // Fetch information about the running user
-       struct group* group = getgrgid(gid);
-       if (!group) {
-               ERROR(pakfire, "Could not fetch group entry for GID %d: %m\n", gid);
-               return 1;
-       }
-
-       DEBUG(pakfire, "Fetching SUBGID for %s (%d)\n", group->gr_name, gid);
-
-       // Open /etc/subgid
-       FILE* f = fopen(ETC_SUBGID, "r");
-       if (!f) {
-               ERROR(pakfire, "Could not open %s: %m\n", ETC_SUBGID);
-               goto ERROR;
-       }
-
-       // Walk through all entries
-       while (1) {
-               entry = pakfire_fgetsubid(pakfire, f);
-               if (!entry)
-                       break;
-
-               // TODO Check if name matches GID
-
-               // Check for match
-               if (strcmp(entry->name, group->gr_name) == 0) {
-                       subgid->id     = entry->id;
-                       subgid->length = entry->length;
+                       subid->id     = entry->id;
+                       subid->length = entry->length;
                        r = 0;
 
                        break;