#include <errno.h>
#include <fts.h>
#include <linux/limits.h>
+#include <pwd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
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;
}
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) {
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;
// 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;
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) {
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);
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)
// 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;