]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Fetch more user/group information at startup
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 May 2023 16:41:47 +0000 (16:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 May 2023 16:41:47 +0000 (16:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pakfire.c

index 765f075b3b244ac9c7b673abbd469965e52b44b1..bb2831ab28ce5b5ae1d0899a81cbca56bd335d8e 100644 (file)
@@ -22,6 +22,7 @@
 #include <errno.h>
 #include <fts.h>
 #include <linux/limits.h>
+#include <pwd.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -81,8 +82,16 @@ struct pakfire {
        FILE* lock;
 
        // UID/GID of running user
-       uid_t uid;
-       gid_t gid;
+       struct pakfire_user {
+               uid_t uid;
+               char name[NAME_MAX];
+               char home[PATH_MAX];
+       } user;
+
+       struct pakfire_group {
+               gid_t gid;
+               char name[NAME_MAX];
+       } group;
 
        // Mapped UID/GID
        struct pakfire_subid subuid;
@@ -168,11 +177,11 @@ int pakfire_on_root(struct pakfire* pakfire) {
 }
 
 uid_t pakfire_uid(struct pakfire* pakfire) {
-       return pakfire->uid;
+       return pakfire->user.uid;
 }
 
 gid_t pakfire_gid(struct pakfire* pakfire) {
-       return pakfire->gid;
+       return pakfire->group.gid;
 }
 
 const struct pakfire_subid* pakfire_subuid(struct pakfire* pakfire) {
@@ -416,7 +425,7 @@ static int pakfire_safety_checks(struct pakfire* pakfire) {
                return 0;
 
        // We must be root in order to operate in /
-       if (pakfire->uid) {
+       if (pakfire->user.uid) {
                ERROR(pakfire, "Must be running as root on /\n");
                errno = EPERM;
                return 1;
@@ -728,7 +737,7 @@ static int pakfire_set_cache_path(struct pakfire* pakfire) {
        // Fetch the path from the configuration file
        const char* path = pakfire_config_get(pakfire->config, "general", "cache_path", NULL);
        if (!path) {
-               const char* home = pakfire_get_home(pakfire, pakfire->uid);
+               const char* home = pakfire_get_home(pakfire, pakfire->user.uid);
                if (!home)
                        return 1;
 
@@ -746,6 +755,53 @@ static int pakfire_set_cache_path(struct pakfire* pakfire) {
                path, pakfire->distro.id, pakfire->distro.version_id, pakfire->arch);
 }
 
+static int pakfire_setup_user(struct pakfire* pakfire) {
+       struct passwd user;
+       struct passwd* u = NULL;
+       struct group group;
+       struct group* g = NULL;
+       char buffer[1024];
+       int r;
+
+       // Fetch the UID/GID we are running as
+       const uid_t uid = geteuid();
+       const gid_t gid = getegid();
+
+       // Fetch all user information
+       r = getpwuid_r(uid, &user, buffer, sizeof(buffer), &u);
+       if (r)
+               goto ERROR;
+
+       // Store UID
+       pakfire->user.uid = uid;
+
+       // Store username
+       r = pakfire_string_set(pakfire->user.name, user.pw_name);
+       if (r)
+               goto ERROR;
+
+       // Store home directory
+       r = pakfire_string_set(pakfire->user.home, user.pw_dir);
+       if (r)
+               goto ERROR;
+
+       // Fetch all group information
+       r = getgrgid_r(gid, &group, buffer, sizeof(buffer), &g);
+       if (r)
+               goto ERROR;
+
+       // Store GID
+       pakfire->group.gid = gid;
+
+       // Store name
+       r = pakfire_string_set(pakfire->group.name, group.gr_name);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       return r;
+}
+
 PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
                const char* arch, const char* conf, int flags,
                int loglevel, pakfire_log_callback log_callback, void* log_data) {
@@ -779,10 +835,6 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
        p->nrefs = 1;
        p->flags = flags;
 
-       // Store the UID/GID we are running as
-       p->uid = geteuid();
-       p->gid = getegid();
-
        // Set architecture
        pakfire_string_set(p->arch, arch);
 
@@ -801,6 +853,11 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
                        pakfire_log_set_priority(p, log_priority(env));
        }
 
+       // Setup user/group
+       r = pakfire_setup_user(p);
+       if (r)
+               goto ERROR;
+
        // Initialise configuration
        r = pakfire_config_create(&p->config);
        if (r)
@@ -838,14 +895,14 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
        // Fetch sub UID/GIDs
        if (!pakfire_on_root(p)) {
                // UID
-               r = pakfire_getsubid(p, "/etc/subuid", p->uid, &p->subuid);
+               r = pakfire_getsubid(p, "/etc/subuid", p->user.uid, &p->subuid);
                if (r) {
                        ERROR(p, "Could not fetch subuid: %m\n");
                        goto ERROR;
                }
 
                // GID
-               r = pakfire_getsubid(p, "/etc/subgid", p->uid, &p->subgid);
+               r = pakfire_getsubid(p, "/etc/subgid", p->user.uid, &p->subgid);
                if (r) {
                        ERROR(p, "Could not fetch subgid: %m\n");
                        goto ERROR;