1 /* vi: set sw=4 ts=4: */
3 * Mini id implementation for busybox
5 * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
6 * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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.
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"
29 PRINT_REAL = (1 << 0),
30 NAME_NOT_NUMBER = (1 << 1),
32 JUST_GROUP = (1 << 3),
33 JUST_ALL_GROUPS = (1 << 4),
35 JUST_CONTEXT = (1 << 5),
39 static int print_common(unsigned id,
40 char* FAST_FUNC bb_getXXXid(char *name, int bufsize, long uid),
43 const char *name = bb_getXXXid(NULL, 0, id);
48 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
51 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
53 printf(option_mask32 ? "%s" : "(%s)", name);
55 /* Don't set error status flag in default mode */
58 bb_error_msg("unknown ID %u", id);
66 static int print_group(gid_t id, const char *prefix)
68 return print_common(id, bb_getgrgid, prefix);
71 static int print_user(uid_t id, const char *prefix)
73 return print_common(id, bb_getpwuid, prefix);
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
81 static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
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: */
95 //commented out here, happens below anyway
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 */
104 return 0; /* error, don't return < 0! */
108 int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
109 int id_main(int argc UNUSED_PARAM, char **argv)
117 int status = EXIT_SUCCESS;
119 const char *username;
121 security_context_t scontext = NULL;
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"));
129 username = argv[optind];
131 struct passwd *p = getpwnam(username);
133 bb_error_msg_and_die("unknown user %s", username);
134 euid = ruid = p->pw_uid;
135 egid = rgid = p->pw_gid;
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)) {
151 status |= print_user(ruid, "uid=");
152 status |= print_group(rgid, " gid=");
154 status |= print_user(euid, " euid=");
156 status |= print_group(egid, " egid=");
158 /* JUST_ALL_GROUPS */
159 status |= print_group(rgid, NULL);
161 status |= print_group(egid, " ");
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));
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);
176 for (i = 0; i < n; i++) {
177 if (opt && (groups[i] == rgid || groups[i] == egid))
179 status |= print_group(groups[i], opt ? " " : prefix);
182 } else if (n < 0) { /* error in get_groups() */
184 bb_error_msg_and_die("cannot get groups");
188 if (ENABLE_FEATURE_CLEAN_UP)
191 if (is_selinux_enabled()) {
192 if (getcon(&scontext) == 0)
193 printf(" context=%s", scontext);
196 } else if (opt & PRINT_REAL) {
202 status |= print_user(euid, NULL);
203 else if (opt & JUST_GROUP)
204 status |= print_group(egid, NULL);
206 else if (opt & JUST_CONTEXT) {
208 if (username || getcon(&scontext)) {
209 bb_error_msg_and_die("can't get process context%s",
210 username ? " for a different user" : "");
212 fputs(scontext, stdout);
214 /* freecon(NULL) seems to be harmless */
215 if (ENABLE_FEATURE_CLEAN_UP)
219 fflush_stdout_and_exit(status);