# #
#############################################################################*/
+#include <errno.h>
#include <grp.h>
#include <linux/limits.h>
#include <pwd.h>
// SUBUID/SUBGID
-static struct pakfire_subid* pakfire_fgetsubid(struct pakfire* pakfire, FILE* f) {
- static struct pakfire_subid subid;
+static int pakfire_fgetsubid(struct pakfire* pakfire, struct pakfire_subid* subid, FILE* f) {
int r;
char* line = NULL;
break;
}
- // Reset r
- r = 0;
-
int i = 0;
char* token = strtok_r(line, ":", &p);
switch (i++) {
// First field has the name
case 0:
- pakfire_string_set(subid.name, token);
+ pakfire_string_set(subid->name, token);
break;
// Second field has the ID
case 1:
- subid.id = strtoul(token, NULL, 10);
+ subid->id = strtoul(token, NULL, 10);
break;
// Third field has the length
case 2:
- subid.length = strtoul(token, NULL, 10);
+ subid->length = strtoul(token, NULL, 10);
break;
}
}
// Check if length is greater than zero
- if (subid.length == 0) {
+ if (subid->length == 0) {
DEBUG(pakfire, "Length equals zero: %s\n", line);
r = 1;
+ goto ERROR;
}
+ // Reset r
+ r = 0;
+
ERROR:
if (line)
free(line);
- if (r)
- return NULL;
+ if (!r)
+ DEBUG(pakfire, "Parsed SUBID entry: name=%s, id=%d, length=%zu\n",
+ subid->name, subid->id, subid->length);
- DEBUG(pakfire, "Parsed SUBID entry: name=%s, id=%d, length=%zu\n",
- subid.name, subid.id, subid.length);
-
- return &subid;
+ return r;
}
int pakfire_getsubid(struct pakfire* pakfire, const char* path, const uid_t uid,
struct pakfire_subid* subid) {
- struct pakfire_subid* entry = NULL;
+ struct pakfire_subid entry;
int r = 1;
// Do not lookup root user and set the entire available UID/GID range
FILE* f = fopen(path, "r");
if (!f) {
ERROR(pakfire, "Could not open %s: %m\n", ETC_SUBUID);
- goto ERROR;
+ r = 1;
+ goto END;
}
// Walk through all entries
while (1) {
- entry = pakfire_fgetsubid(pakfire, f);
- if (!entry)
- break;
+ r = pakfire_fgetsubid(pakfire, &entry, f);
+ if (r)
+ goto END;
// TODO Check if name matches UID
// Check for match
- if (strcmp(entry->name, passwd->pw_name) == 0) {
- subid->id = entry->id;
- subid->length = entry->length;
+ if (strcmp(entry.name, passwd->pw_name) == 0) {
+ subid->id = entry.id;
+ subid->length = entry.length;
r = 0;
- break;
+ goto END;
}
}
-ERROR:
+ // No match found
+ ERROR(pakfire, "No match found for %s\n", passwd->pw_name);
+ errno = ENOENT;
+ r = 1;
+
+END:
if (f)
fclose(f);