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