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.
21 PRINT_REAL = (1 << 0),
22 NAME_NOT_NUMBER = (1 << 1),
24 JUST_GROUP = (1 << 3),
25 JUST_ALL_GROUPS = (1 << 4),
27 JUST_CONTEXT = (1 << 5),
31 static int print_common(unsigned id,
32 char* FAST_FUNC bb_getXXXid(char *name, int bufsize, long uid),
35 const char *name = bb_getXXXid(NULL, 0, id);
40 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
43 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
45 printf(option_mask32 ? "%s" : "(%s)", name);
47 /* Don't set error status flag in default mode */
50 bb_error_msg("unknown ID %u", id);
58 static int print_group(gid_t id, const char *prefix)
60 return print_common(id, bb_getgrgid, prefix);
63 static int print_user(gid_t id, const char *prefix)
65 return print_common(id, bb_getpwuid, prefix);
68 /* On error set *n < 0 and return >= 0
69 * If *n is too small, update it and return < 0
70 * (ok to trash groups[] in both cases)
71 * Otherwise fill in groups[] and return >= 0
73 static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
78 /* If the user is a member of more than
79 * *n groups, then -1 is returned. Otherwise >= 0.
80 * (and no defined way of detecting errors?!) */
81 m = getgrouplist(username, rgid, groups, n);
82 /* I guess *n < 0 might indicate error. Anyway,
83 * malloc'ing -1 bytes won't be good, so: */
87 //commented here, happens below anyway
89 /* On error -1 is returned, which ends up in *n */
90 int nn = getgroups(*n, groups);
91 /* 0: nn <= *n, groups[] was big enough; -1 otherwise */
96 return 0; /* error, don't return < 0! */
100 int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
101 int id_main(int argc UNUSED_PARAM, char **argv)
109 int status = EXIT_SUCCESS;
111 const char *username;
113 security_context_t scontext = NULL;
115 /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
116 /* Don't allow more than one username */
117 opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
118 USE_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
119 opt = getopt32(argv, "rnugG" USE_SELINUX("Z"));
121 username = argv[optind];
123 struct passwd *p = getpwnam(username);
125 bb_error_msg_and_die("unknown user %s", username);
126 euid = ruid = p->pw_uid;
127 egid = rgid = p->pw_gid;
134 /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
135 /* id says: print the real ID instead of the effective ID, with -ugG */
136 /* in fact in ths case egid is always printed if egid != rgid */
137 if (!opt || (opt & JUST_ALL_GROUPS)) {
143 status |= print_user(ruid, "uid=");
144 status |= print_group(rgid, " gid=");
146 status |= print_user(euid, " euid=");
148 status |= print_group(egid, " egid=");
150 /* JUST_ALL_GROUPS */
151 status |= print_group(rgid, NULL);
153 status |= print_group(egid, " ");
155 /* We'd rather try supplying largish buffer than
156 * having get_groups() run twice. That might be slow
157 * (think about "user database in remove SQL server" case) */
158 groups = xmalloc(64 * sizeof(gid_t));
160 if (get_groups(username, rgid, groups, &n) < 0) {
161 /* Need bigger buffer after all */
162 groups = xrealloc(groups, n * sizeof(gid_t));
163 get_groups(username, rgid, groups, &n);
168 for (i = 0; i < n; i++) {
169 if (opt && (groups[i] == rgid || groups[i] == egid))
171 status |= print_group(groups[i], opt ? " " : prefix);
174 if (ENABLE_FEATURE_CLEAN_UP)
176 } else if (n < 0) { /* error in get_groups() */
178 bb_error_msg_and_die("cannot get groups");
183 if (is_selinux_enabled()) {
184 if (getcon(&scontext) == 0)
185 printf(" context=%s", scontext);
188 } else if (opt & PRINT_REAL) {
194 status |= print_user(euid, NULL);
195 else if (opt & JUST_GROUP)
196 status |= print_group(egid, NULL);
198 else if (opt & JUST_CONTEXT) {
200 if (username || getcon(&scontext)) {
201 bb_error_msg_and_die("can't get process context%s",
202 username ? " for a different user" : "");
204 fputs(scontext, stdout);
206 /* freecon(NULL) seems to be harmless */
207 if (ENABLE_FEATURE_CLEAN_UP)
211 fflush_stdout_and_exit(status);