2006-10-06 04:20:22 +05:30
|
|
|
#include "busybox.h"
|
|
|
|
|
2006-12-28 11:14:47 +05:30
|
|
|
int get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
|
2006-10-06 04:20:22 +05:30
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
struct group *gr;
|
2006-12-28 11:14:47 +05:30
|
|
|
char *user, *group;
|
|
|
|
unsigned n;
|
2006-10-06 04:20:22 +05:30
|
|
|
|
2006-12-28 11:14:47 +05:30
|
|
|
user = (char*)ug;
|
|
|
|
group = strchr(ug, ':');
|
|
|
|
if (group) {
|
|
|
|
int sz = (++group) - ug;
|
|
|
|
user = alloca(sz);
|
|
|
|
/* copies sz-1 bytes, stores terminating '\0' */
|
|
|
|
safe_strncpy(user, ug, sz);
|
|
|
|
}
|
|
|
|
if (numeric_ok) {
|
|
|
|
n = bb_strtou(user, NULL, 10);
|
|
|
|
if (!errno) {
|
|
|
|
u->uid = n;
|
|
|
|
pwd = getpwuid(n);
|
|
|
|
/* If we have e.g. "500" string without user */
|
|
|
|
/* with uid 500 in /etc/passwd, we set gid == uid */
|
|
|
|
u->gid = pwd ? pwd->pw_gid : n;
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pwd = getpwnam(user);
|
2006-10-06 04:20:22 +05:30
|
|
|
if (!pwd)
|
|
|
|
return 0;
|
|
|
|
u->uid = pwd->pw_uid;
|
|
|
|
u->gid = pwd->pw_gid;
|
2006-12-28 11:14:47 +05:30
|
|
|
|
|
|
|
skip:
|
|
|
|
if (group) {
|
|
|
|
if (numeric_ok) {
|
|
|
|
n = bb_strtou(group, NULL, 10);
|
|
|
|
if (!errno) {
|
|
|
|
u->gid = n;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gr = getgrnam(group);
|
2006-10-06 04:20:22 +05:30
|
|
|
if (!gr) return 0;
|
|
|
|
u->gid = gr->gr_gid;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#include <stdio.h>
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
unsigned u;
|
|
|
|
struct bb_uidgid_t ug;
|
2006-12-28 11:14:47 +05:30
|
|
|
u = get_uidgid(&ug, "apache", 0);
|
2006-10-06 04:20:22 +05:30
|
|
|
printf("%u = %u:%u\n", u, ug.uid, ug.gid);
|
|
|
|
ug.uid = ug.gid = 1111;
|
2006-12-28 11:14:47 +05:30
|
|
|
u = get_uidgid(&ug, "apache", 0);
|
2006-10-06 04:20:22 +05:30
|
|
|
printf("%u = %u:%u\n", u, ug.uid, ug.gid);
|
|
|
|
ug.uid = ug.gid = 1111;
|
2006-12-28 11:14:47 +05:30
|
|
|
u = get_uidgid(&ug, "apache:users", 0);
|
2006-10-06 04:20:22 +05:30
|
|
|
printf("%u = %u:%u\n", u, ug.uid, ug.gid);
|
|
|
|
ug.uid = ug.gid = 1111;
|
2006-12-28 11:14:47 +05:30
|
|
|
u = get_uidgid(&ug, "apache:users", 0);
|
2006-10-06 04:20:22 +05:30
|
|
|
printf("%u = %u:%u\n", u, ug.uid, ug.gid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|