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#include "libbb.h"
29
30
31int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug)
32{
33 struct passwd *pwd;
34 struct group *gr;
35 char *user, *group;
36 unsigned n;
37
38 user = (char*)ug;
39 group = strchr(ug, ':');
40 if (group) {
41 int sz = (++group) - ug;
42 user = alloca(sz);
43
44 safe_strncpy(user, ug, sz);
45 }
46 n = bb_strtou(user, NULL, 10);
47 if (!errno) {
48 u->uid = n;
49 pwd = getpwuid(n);
50
51
52 u->gid = pwd ? pwd->pw_gid : n;
53 goto skip;
54 }
55
56 pwd = getpwnam(user);
57 if (!pwd)
58 return 0;
59 u->uid = pwd->pw_uid;
60 u->gid = pwd->pw_gid;
61
62 skip:
63 if (group) {
64 n = bb_strtou(group, NULL, 10);
65 if (!errno) {
66 u->gid = n;
67 return 1;
68 }
69 gr = getgrnam(group);
70 if (!gr)
71 return 0;
72 u->gid = gr->gr_gid;
73 }
74 return 1;
75}
76void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug)
77{
78 if (!get_uidgid(u, ug))
79 bb_error_msg_and_die("unknown user/group %s", ug);
80}
81
82
83
84
85
86
87
88
89void FAST_FUNC parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
90{
91 char *group;
92
93 u->uid = u->gid = (gid_t)-1L;
94
95
96 group = strchr(user_group, '.');
97 if (!group)
98 group = strchr(user_group, ':');
99 else
100 *group = ':';
101
102
103 if (!group) {
104 u->uid = get_ug_id(user_group, xuname2uid);
105 } else if (group == user_group) {
106 u->gid = get_ug_id(group + 1, xgroup2gid);
107 } else {
108 if (!group[1])
109 *group = '\0';
110 xget_uidgid(u, user_group);
111 }
112}
113
114#if 0
115#include <stdio.h>
116int main()
117{
118 unsigned u;
119 struct bb_uidgid_t ug;
120 u = get_uidgid(&ug, "apache");
121 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
122 ug.uid = ug.gid = 1111;
123 u = get_uidgid(&ug, "apache");
124 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
125 ug.uid = ug.gid = 1111;
126 u = get_uidgid(&ug, "apache:users");
127 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
128 ug.uid = ug.gid = 1111;
129 u = get_uidgid(&ug, "apache:users");
130 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
131 return 0;
132}
133#endif
134