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, const char *name, const char *prefix)
44 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
47 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
49 printf(option_mask32 ? "%s" : "(%s)", name);
51 /* Don't set error status flag in default mode */
54 bb_error_msg("unknown ID %u", id);
62 static int print_group(gid_t id, const char *prefix)
64 return print_common(id, gid2group(id), prefix);
67 static int print_user(uid_t id, const char *prefix)
69 return print_common(id, uid2uname(id), prefix);
72 /* 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
77 static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
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: */
91 //commented out here, happens below anyway
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 */
100 return 0; /* error, don't return < 0! */
104 int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
105 int id_main(int argc UNUSED_PARAM, char **argv)
113 int status = EXIT_SUCCESS;
115 const char *username;
117 security_context_t scontext = NULL;
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 USE_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
123 opt = getopt32(argv, "rnugG" USE_SELINUX("Z"));
125 username = argv[optind];
127 struct passwd *p = xgetpwnam(username);
128 euid = ruid = p->pw_uid;
129 egid = rgid = p->pw_gid;
136 /* 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)) {
145 status |= print_user(ruid, "uid=");
146 status |= print_group(rgid, " gid=");
148 status |= print_user(euid, " euid=");
150 status |= print_group(egid, " egid=");
152 /* JUST_ALL_GROUPS */
153 status |= print_group(rgid, NULL);
155 status |= print_group(egid, " ");
157 /* 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));
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);
170 for (i = 0; i < n; i++) {
171 if (opt && (groups[i] == rgid || groups[i] == egid))
173 status |= print_group(groups[i], opt ? " " : prefix);
176 } else if (n < 0) { /* error in get_groups() */
178 bb_error_msg_and_die("cannot get groups");
182 if (ENABLE_FEATURE_CLEAN_UP)
185 if (is_selinux_enabled()) {
186 if (getcon(&scontext) == 0)
187 printf(" context=%s", scontext);
190 } else if (opt & PRINT_REAL) {
196 status |= print_user(euid, NULL);
197 else if (opt & JUST_GROUP)
198 status |= print_group(egid, NULL);
200 else if (opt & JUST_CONTEXT) {
202 if (username || getcon(&scontext)) {
203 bb_error_msg_and_die("can't get process context%s",
204 username ? " for a different user" : "");
206 fputs(scontext, stdout);
208 /* freecon(NULL) seems to be harmless */
209 if (ENABLE_FEATURE_CLEAN_UP)
213 fflush_stdout_and_exit(status);