blob: 399d25e34232eda0f3c90d775543d66e4c9310d1 [file] [log] [blame]
Erik Andersen94f5e0b2000-05-01 19:10:52 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini id implementation for busybox
4 *
Erik Andersen94f5e0b2000-05-01 19:10:52 +00005 * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
Denis Vlasenko34e68c82008-10-29 00:27:31 +00006 * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
Erik Andersen94f5e0b2000-05-01 19:10:52 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen94f5e0b2000-05-01 19:10:52 +00009 */
10
Bernhard Reutner-Fischer0ee1cb02008-09-12 09:58:11 +000011/* BB_AUDIT SUSv3 compliant. */
Denis Vlasenko34e68c82008-10-29 00:27:31 +000012/* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever
13 * length and to be more similar to GNU id.
Denis Vlasenko49622d72007-03-10 16:58:49 +000014 * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
Bernhard Reutner-Fischer0ee1cb02008-09-12 09:58:11 +000015 * Added -G option Tito Ragusa (C) 2008 for SUSv3.
Eric Andersen7eb79ff2004-09-02 22:21:41 +000016 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000017
Tito Ragusa33092f12011-06-21 17:11:40 +020018//config:config ID
19//config: bool "id"
20//config: default y
21//config: help
22//config: id displays the current user and group ID names.
23
24//config:config GROUPS
25//config: bool "groups"
26//config: default y
27//config: help
28//config: Print the group names associated with current user id.
29
30//kbuild:lib-$(CONFIG_GROUPS) += id.o
31//kbuild:lib-$(CONFIG_ID) += id.o
32
Denys Vlasenkod5ac9c82011-06-22 04:17:49 +020033//applet:IF_GROUPS(APPLET_NOEXEC(groups, id, BB_DIR_USR_BIN, BB_SUID_DROP, groups))
34//applet:IF_ID( APPLET_NOEXEC(id, id, BB_DIR_USR_BIN, BB_SUID_DROP, id ))
Tito Ragusa33092f12011-06-21 17:11:40 +020035
Pere Orga34425382011-03-31 14:43:25 +020036//usage:#define id_trivial_usage
37//usage: "[OPTIONS] [USER]"
38//usage:#define id_full_usage "\n\n"
39//usage: "Print information about USER or the current user\n"
Pere Orga34425382011-03-31 14:43:25 +020040//usage: IF_SELINUX(
41//usage: "\n -Z Security context"
42//usage: )
43//usage: "\n -u User ID"
44//usage: "\n -g Group ID"
45//usage: "\n -G Supplementary group IDs"
46//usage: "\n -n Print names instead of numbers"
47//usage: "\n -r Print real ID instead of effective ID"
48//usage:
49//usage:#define id_example_usage
50//usage: "$ id\n"
51//usage: "uid=1000(andersen) gid=1000(andersen)\n"
52
Tito Ragusa33092f12011-06-21 17:11:40 +020053//usage:#define groups_trivial_usage
54//usage: "[USER]"
55//usage:#define groups_full_usage "\n\n"
56//usage: "Print the group memberships of USER or for the current process"
57//usage:
58//usage:#define groups_example_usage
59//usage: "$ groups\n"
60//usage: "andersen lp dialout cdrom floppy\n"
61
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000062#include "libbb.h"
Eric Andersen7eb79ff2004-09-02 22:21:41 +000063
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070064/* This is a NOEXEC applet. Be very careful! */
65
Denis Vlasenkode7a52f2008-11-17 00:12:17 +000066#if !ENABLE_USE_BB_PWD_GRP
Denis Vlasenko5ff12652008-11-16 15:03:11 +000067#if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0)
68#if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 30)
69#error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
70#endif
71#endif
Denis Vlasenkode7a52f2008-11-17 00:12:17 +000072#endif
Denis Vlasenko5ff12652008-11-16 15:03:11 +000073
Denis Vlasenko34e68c82008-10-29 00:27:31 +000074enum {
75 PRINT_REAL = (1 << 0),
76 NAME_NOT_NUMBER = (1 << 1),
77 JUST_USER = (1 << 2),
78 JUST_GROUP = (1 << 3),
79 JUST_ALL_GROUPS = (1 << 4),
Denis Vlasenko49622d72007-03-10 16:58:49 +000080#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +000081 JUST_CONTEXT = (1 << 5),
Denis Vlasenko49622d72007-03-10 16:58:49 +000082#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +000083};
Eric Andersen7eb79ff2004-09-02 22:21:41 +000084
Denis Vlasenko0c68a872008-12-02 22:56:59 +000085static int print_common(unsigned id, const char *name, const char *prefix)
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000086{
Denis Vlasenko34e68c82008-10-29 00:27:31 +000087 if (prefix) {
88 printf("%s", prefix);
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000089 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +000090 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
91 printf("%u", id);
92 }
93 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
94 if (name) {
95 printf(option_mask32 ? "%s" : "(%s)", name);
96 } else {
97 /* Don't set error status flag in default mode */
98 if (option_mask32) {
99 if (ENABLE_DESKTOP)
100 bb_error_msg("unknown ID %u", id);
101 return EXIT_FAILURE;
102 }
103 }
104 }
105 return EXIT_SUCCESS;
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000106}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000108static int print_group(gid_t id, const char *prefix)
109{
Denis Vlasenko0c68a872008-12-02 22:56:59 +0000110 return print_common(id, gid2group(id), prefix);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000111}
112
Bernhard Reutner-Fischer65591002008-11-05 08:15:13 +0000113static int print_user(uid_t id, const char *prefix)
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000114{
Denis Vlasenko0c68a872008-12-02 22:56:59 +0000115 return print_common(id, uid2uname(id), prefix);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000116}
117
118/* On error set *n < 0 and return >= 0
119 * If *n is too small, update it and return < 0
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200120 * (ok to trash groups[] in both cases)
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000121 * Otherwise fill in groups[] and return >= 0
122 */
123static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
124{
125 int m;
126
127 if (username) {
128 /* If the user is a member of more than
129 * *n groups, then -1 is returned. Otherwise >= 0.
130 * (and no defined way of detecting errors?!) */
131 m = getgrouplist(username, rgid, groups, n);
132 /* I guess *n < 0 might indicate error. Anyway,
133 * malloc'ing -1 bytes won't be good, so: */
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200134 if (*n < 0)
135 return 0;
136 return m;
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000137 }
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200138
139 *n = getgroups(*n, groups);
140 if (*n >= 0)
141 return *n;
142 /* Error */
143 if (errno == EINVAL) /* *n is too small? */
144 *n = getgroups(0, groups); /* get needed *n */
145 /* if *n >= 0, return -1 (got new *n), else return 0 (error): */
146 return -(*n >= 0);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000147}
Denis Vlasenko22284262008-09-18 00:56:24 +0000148
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000149int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000150int id_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000151{
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000152 uid_t ruid;
153 gid_t rgid;
154 uid_t euid;
155 gid_t egid;
156 unsigned opt;
157 int i;
158 int status = EXIT_SUCCESS;
159 const char *prefix;
Denis Vlasenko22284262008-09-18 00:56:24 +0000160 const char *username;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000161#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000162 security_context_t scontext = NULL;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000163#endif
Tito Ragusa33092f12011-06-21 17:11:40 +0200164
165 if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) {
Denys Vlasenkod5ac9c82011-06-22 04:17:49 +0200166 /* TODO: coreutils groups prepend "USER : " prefix,
167 * and accept many usernames. Example:
168 * # groups root root
169 * root : root
170 * root : root
171 */
172 opt = option_mask32 = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER;
Tito Ragusa33092f12011-06-21 17:11:40 +0200173 } else {
174 /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
175 /* Don't allow more than one username */
176 opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000177 IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
Tito Ragusa33092f12011-06-21 17:11:40 +0200178 opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
179 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000180
Denis Vlasenko22284262008-09-18 00:56:24 +0000181 username = argv[optind];
Denis Vlasenko22284262008-09-18 00:56:24 +0000182 if (username) {
Denis Vlasenkod7a805e2008-12-03 19:05:55 +0000183 struct passwd *p = xgetpwnam(username);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000184 euid = ruid = p->pw_uid;
185 egid = rgid = p->pw_gid;
Denis Vlasenko22284262008-09-18 00:56:24 +0000186 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000187 egid = getegid();
188 rgid = getgid();
189 euid = geteuid();
190 ruid = getuid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000191 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000192 /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
193 /* id says: print the real ID instead of the effective ID, with -ugG */
Bernhard Reutner-Fischera6438162008-11-05 08:18:16 +0000194 /* in fact in this case egid is always printed if egid != rgid */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000195 if (!opt || (opt & JUST_ALL_GROUPS)) {
196 gid_t *groups;
197 int n;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000198
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000199 if (!opt) {
200 /* Default Mode */
201 status |= print_user(ruid, "uid=");
202 status |= print_group(rgid, " gid=");
203 if (euid != ruid)
204 status |= print_user(euid, " euid=");
205 if (egid != rgid)
206 status |= print_group(egid, " egid=");
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000207 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000208 /* JUST_ALL_GROUPS */
209 status |= print_group(rgid, NULL);
210 if (egid != rgid)
211 status |= print_group(egid, " ");
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000212 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000213 /* We are supplying largish buffer, trying
214 * to not run get_groups() twice. That might be slow
215 * ("user database in remote SQL server" case) */
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200216 groups = xmalloc(64 * sizeof(groups[0]));
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000217 n = 64;
218 if (get_groups(username, rgid, groups, &n) < 0) {
219 /* Need bigger buffer after all */
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200220 groups = xrealloc(groups, n * sizeof(groups[0]));
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000221 get_groups(username, rgid, groups, &n);
222 }
223 if (n > 0) {
224 /* Print the list */
225 prefix = " groups=";
226 for (i = 0; i < n; i++) {
227 if (opt && (groups[i] == rgid || groups[i] == egid))
228 continue;
229 status |= print_group(groups[i], opt ? " " : prefix);
230 prefix = ",";
231 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000232 } else if (n < 0) { /* error in get_groups() */
Denys Vlasenkodf7b6572011-01-26 16:11:19 +0100233 if (ENABLE_DESKTOP)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100234 bb_error_msg_and_die("can't get groups");
Denys Vlasenkodf7b6572011-01-26 16:11:19 +0100235 return EXIT_FAILURE;
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000236 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000237 if (ENABLE_FEATURE_CLEAN_UP)
238 free(groups);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000239#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000240 if (is_selinux_enabled()) {
241 if (getcon(&scontext) == 0)
242 printf(" context=%s", scontext);
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000243 }
244#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000245 } else if (opt & PRINT_REAL) {
246 euid = ruid;
247 egid = rgid;
Eric Andersenc1b8f122001-01-25 05:12:02 +0000248 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000249
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000250 if (opt & JUST_USER)
251 status |= print_user(euid, NULL);
252 else if (opt & JUST_GROUP)
253 status |= print_group(egid, NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000254#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000255 else if (opt & JUST_CONTEXT) {
256 selinux_or_die();
257 if (username || getcon(&scontext)) {
258 bb_error_msg_and_die("can't get process context%s",
259 username ? " for a different user" : "");
260 }
261 fputs(scontext, stdout);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000262 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000263 /* freecon(NULL) seems to be harmless */
264 if (ENABLE_FEATURE_CLEAN_UP)
265 freecon(scontext);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000266#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +0000267 bb_putchar('\n');
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000268 fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000269}