Contents of /tags/mkinitrd-6_1_4/busybox/coreutils/id.c
Parent Directory | Revision Log
Revision 896 -
(show annotations)
(download)
Wed Aug 5 16:25:39 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 5895 byte(s)
Wed Aug 5 16:25:39 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 5895 byte(s)
tagged 'mkinitrd-6_1_4'
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Mini id implementation for busybox |
4 | * |
5 | * Copyright (C) 2000 by Randolph Chung <tausq@debian.org> |
6 | * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it> |
7 | * |
8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
9 | */ |
10 | |
11 | /* BB_AUDIT SUSv3 compliant. */ |
12 | /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever |
13 | * length and to be more similar to GNU id. |
14 | * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp> |
15 | * Added -G option Tito Ragusa (C) 2008 for SUSv3. |
16 | */ |
17 | |
18 | #include "libbb.h" |
19 | |
20 | #if !ENABLE_USE_BB_PWD_GRP |
21 | #if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0) |
22 | #if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 30) |
23 | #error "Sorry, you need at least uClibc version 0.9.30 for id applet to build" |
24 | #endif |
25 | #endif |
26 | #endif |
27 | |
28 | enum { |
29 | PRINT_REAL = (1 << 0), |
30 | NAME_NOT_NUMBER = (1 << 1), |
31 | JUST_USER = (1 << 2), |
32 | JUST_GROUP = (1 << 3), |
33 | JUST_ALL_GROUPS = (1 << 4), |
34 | #if ENABLE_SELINUX |
35 | JUST_CONTEXT = (1 << 5), |
36 | #endif |
37 | }; |
38 | |
39 | static int print_common(unsigned id, |
40 | char* FAST_FUNC bb_getXXXid(char *name, int bufsize, long uid), |
41 | const char *prefix) |
42 | { |
43 | const char *name = bb_getXXXid(NULL, 0, id); |
44 | |
45 | if (prefix) { |
46 | printf("%s", prefix); |
47 | } |
48 | if (!(option_mask32 & NAME_NOT_NUMBER) || !name) { |
49 | printf("%u", id); |
50 | } |
51 | if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) { |
52 | if (name) { |
53 | printf(option_mask32 ? "%s" : "(%s)", name); |
54 | } else { |
55 | /* Don't set error status flag in default mode */ |
56 | if (option_mask32) { |
57 | if (ENABLE_DESKTOP) |
58 | bb_error_msg("unknown ID %u", id); |
59 | return EXIT_FAILURE; |
60 | } |
61 | } |
62 | } |
63 | return EXIT_SUCCESS; |
64 | } |
65 | |
66 | static int print_group(gid_t id, const char *prefix) |
67 | { |
68 | return print_common(id, bb_getgrgid, prefix); |
69 | } |
70 | |
71 | static int print_user(uid_t id, const char *prefix) |
72 | { |
73 | return print_common(id, bb_getpwuid, prefix); |
74 | } |
75 | |
76 | /* On error set *n < 0 and return >= 0 |
77 | * If *n is too small, update it and return < 0 |
78 | * (ok to trash groups[] in both cases) |
79 | * Otherwise fill in groups[] and return >= 0 |
80 | */ |
81 | static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n) |
82 | { |
83 | int m; |
84 | |
85 | if (username) { |
86 | /* If the user is a member of more than |
87 | * *n groups, then -1 is returned. Otherwise >= 0. |
88 | * (and no defined way of detecting errors?!) */ |
89 | m = getgrouplist(username, rgid, groups, n); |
90 | /* I guess *n < 0 might indicate error. Anyway, |
91 | * malloc'ing -1 bytes won't be good, so: */ |
92 | //if (*n < 0) |
93 | // return 0; |
94 | //return m; |
95 | //commented out here, happens below anyway |
96 | } else { |
97 | /* On error -1 is returned, which ends up in *n */ |
98 | int nn = getgroups(*n, groups); |
99 | /* 0: nn <= *n, groups[] was big enough; -1 otherwise */ |
100 | m = - (nn > *n); |
101 | *n = nn; |
102 | } |
103 | if (*n < 0) |
104 | return 0; /* error, don't return < 0! */ |
105 | return m; |
106 | } |
107 | |
108 | int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
109 | int id_main(int argc UNUSED_PARAM, char **argv) |
110 | { |
111 | uid_t ruid; |
112 | gid_t rgid; |
113 | uid_t euid; |
114 | gid_t egid; |
115 | unsigned opt; |
116 | int i; |
117 | int status = EXIT_SUCCESS; |
118 | const char *prefix; |
119 | const char *username; |
120 | #if ENABLE_SELINUX |
121 | security_context_t scontext = NULL; |
122 | #endif |
123 | /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/ |
124 | /* Don't allow more than one username */ |
125 | opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG" |
126 | USE_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G"); |
127 | opt = getopt32(argv, "rnugG" USE_SELINUX("Z")); |
128 | |
129 | username = argv[optind]; |
130 | if (username) { |
131 | struct passwd *p = getpwnam(username); |
132 | if (!p) |
133 | bb_error_msg_and_die("unknown user %s", username); |
134 | euid = ruid = p->pw_uid; |
135 | egid = rgid = p->pw_gid; |
136 | } else { |
137 | egid = getegid(); |
138 | rgid = getgid(); |
139 | euid = geteuid(); |
140 | ruid = getuid(); |
141 | } |
142 | /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */ |
143 | /* id says: print the real ID instead of the effective ID, with -ugG */ |
144 | /* in fact in this case egid is always printed if egid != rgid */ |
145 | if (!opt || (opt & JUST_ALL_GROUPS)) { |
146 | gid_t *groups; |
147 | int n; |
148 | |
149 | if (!opt) { |
150 | /* Default Mode */ |
151 | status |= print_user(ruid, "uid="); |
152 | status |= print_group(rgid, " gid="); |
153 | if (euid != ruid) |
154 | status |= print_user(euid, " euid="); |
155 | if (egid != rgid) |
156 | status |= print_group(egid, " egid="); |
157 | } else { |
158 | /* JUST_ALL_GROUPS */ |
159 | status |= print_group(rgid, NULL); |
160 | if (egid != rgid) |
161 | status |= print_group(egid, " "); |
162 | } |
163 | /* We are supplying largish buffer, trying |
164 | * to not run get_groups() twice. That might be slow |
165 | * ("user database in remote SQL server" case) */ |
166 | groups = xmalloc(64 * sizeof(gid_t)); |
167 | n = 64; |
168 | if (get_groups(username, rgid, groups, &n) < 0) { |
169 | /* Need bigger buffer after all */ |
170 | groups = xrealloc(groups, n * sizeof(gid_t)); |
171 | get_groups(username, rgid, groups, &n); |
172 | } |
173 | if (n > 0) { |
174 | /* Print the list */ |
175 | prefix = " groups="; |
176 | for (i = 0; i < n; i++) { |
177 | if (opt && (groups[i] == rgid || groups[i] == egid)) |
178 | continue; |
179 | status |= print_group(groups[i], opt ? " " : prefix); |
180 | prefix = ","; |
181 | } |
182 | } else if (n < 0) { /* error in get_groups() */ |
183 | if (!ENABLE_DESKTOP) |
184 | bb_error_msg_and_die("cannot get groups"); |
185 | else |
186 | return EXIT_FAILURE; |
187 | } |
188 | if (ENABLE_FEATURE_CLEAN_UP) |
189 | free(groups); |
190 | #if ENABLE_SELINUX |
191 | if (is_selinux_enabled()) { |
192 | if (getcon(&scontext) == 0) |
193 | printf(" context=%s", scontext); |
194 | } |
195 | #endif |
196 | } else if (opt & PRINT_REAL) { |
197 | euid = ruid; |
198 | egid = rgid; |
199 | } |
200 | |
201 | if (opt & JUST_USER) |
202 | status |= print_user(euid, NULL); |
203 | else if (opt & JUST_GROUP) |
204 | status |= print_group(egid, NULL); |
205 | #if ENABLE_SELINUX |
206 | else if (opt & JUST_CONTEXT) { |
207 | selinux_or_die(); |
208 | if (username || getcon(&scontext)) { |
209 | bb_error_msg_and_die("can't get process context%s", |
210 | username ? " for a different user" : ""); |
211 | } |
212 | fputs(scontext, stdout); |
213 | } |
214 | /* freecon(NULL) seems to be harmless */ |
215 | if (ENABLE_FEATURE_CLEAN_UP) |
216 | freecon(scontext); |
217 | #endif |
218 | bb_putchar('\n'); |
219 | fflush_stdout_and_exit(status); |
220 | } |