#include <sys/time.h>
#include <linux/rtc.h>
#include <glob.h>
+#include <grp.h>
#include "macro.h"
#include "util.h"
assert(username);
assert(*username);
- assert(uid);
- assert(gid);
- assert(home);
/* We enforce some special rules for uid=0: in order to avoid
* NSS lookups for root we hardcode its data. */
if (streq(*username, "root") || streq(*username, "0")) {
*username = "root";
- *uid = 0;
- *gid = 0;
- *home = "/root";
+
+ if (uid)
+ *uid = 0;
+
+ if (gid)
+ *gid = 0;
+
+ if (home)
+ *home = "/root";
return 0;
}
if (!p)
return errno != 0 ? -errno : -ESRCH;
- *uid = p->pw_uid;
- *gid = p->pw_gid;
- *home = p->pw_dir;
+ if (uid)
+ *uid = p->pw_uid;
+
+ if (gid)
+ *gid = p->pw_gid;
+
+ if (home)
+ *home = p->pw_dir;
+
+ return 0;
+}
+
+int get_group_creds(const char **groupname, gid_t *gid) {
+ struct group *g;
+ gid_t id;
+
+ assert(groupname);
+
+ /* We enforce some special rules for gid=0: in order to avoid
+ * NSS lookups for root we hardcode its data. */
+
+ if (streq(*groupname, "root") || streq(*groupname, "0")) {
+ *groupname = "root";
+
+ if (gid)
+ *gid = 0;
+
+ return 0;
+ }
+
+ if (parse_gid(*groupname, &id) >= 0) {
+ errno = 0;
+ g = getgrgid(id);
+
+ if (g)
+ *groupname = g->gr_name;
+ } else {
+ errno = 0;
+ g = getgrnam(*groupname);
+ }
+
+ if (!g)
+ return errno != 0 ? -errno : -ESRCH;
+
+ if (gid)
+ *gid = g->gr_gid;
+
return 0;
}
char **l = NULL;
assert(path);
- assert(list);
+
+ /* Returns all files in a directory in *list, and the number
+ * of files as return value. If list is NULL returns only the
+ * number */
d = opendir(path);
for (;;) {
if (!dirent_is_file(de))
continue;
- if ((unsigned) r >= n) {
- char **t;
+ if (list) {
+ if ((unsigned) r >= n) {
+ char **t;
- n = MAX(16, 2*r);
- t = realloc(l, sizeof(char*) * n);
- if (!t) {
- r = -ENOMEM;
- goto finish;
- }
+ n = MAX(16, 2*r);
+ t = realloc(l, sizeof(char*) * n);
+ if (!t) {
+ r = -ENOMEM;
+ goto finish;
+ }
- l = t;
- }
+ l = t;
+ }
- assert((unsigned) r < n);
+ assert((unsigned) r < n);
- l[r] = strdup(de->d_name);
- if (!l[r]) {
- r = -ENOMEM;
- goto finish;
- }
+ l[r] = strdup(de->d_name);
+ if (!l[r]) {
+ r = -ENOMEM;
+ goto finish;
+ }
- l[++r] = NULL;
+ l[++r] = NULL;
+ } else
+ r++;
}
finish:
if (d)
closedir(d);
- if (r >= 0)
- *list = l;
- else
+ if (r >= 0) {
+ if (list)
+ *list = l;
+ } else
strv_free(l);
return r;