1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#include "busybox.h"
unsigned uidgid_get(struct bb_uidgid_t *u, const char *ug /*, unsigned dogrp */)
{
struct passwd *pwd;
struct group *gr;
const char *g;
/* g = 0; if (dogrp) g = strchr(ug, ':'); */
g = strchr(ug, ':');
if (g) {
int sz = (++g) - ug;
char buf[sz];
safe_strncpy(buf, ug, sz);
pwd = getpwnam(buf);
} else
pwd = getpwnam(ug);
if (!pwd)
return 0;
u->uid = pwd->pw_uid;
u->gid = pwd->pw_gid;
if (g) {
gr = getgrnam(g);
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;
u = uidgid_get(&ug, "apache");
printf("%u = %u:%u\n", u, ug.uid, ug.gid);
ug.uid = ug.gid = 1111;
u = uidgid_get(&ug, "apache");
printf("%u = %u:%u\n", u, ug.uid, ug.gid);
ug.uid = ug.gid = 1111;
u = uidgid_get(&ug, "apache:users");
printf("%u = %u:%u\n", u, ug.uid, ug.gid);
ug.uid = ug.gid = 1111;
u = uidgid_get(&ug, "apache:users");
printf("%u = %u:%u\n", u, ug.uid, ug.gid);
return 0;
}
#endif
|