blob: 1f3e1c4c2ffeadba8a89faf345049434355e9187 [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
Mike Frysinger445e7542013-03-12 11:13:22 -040067#if defined(__UCLIBC__) && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 30)
Denis Vlasenko5ff12652008-11-16 15:03:11 +000068#error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
69#endif
70#endif
71
Denis Vlasenko34e68c82008-10-29 00:27:31 +000072enum {
73 PRINT_REAL = (1 << 0),
74 NAME_NOT_NUMBER = (1 << 1),
75 JUST_USER = (1 << 2),
76 JUST_GROUP = (1 << 3),
77 JUST_ALL_GROUPS = (1 << 4),
Denis Vlasenko49622d72007-03-10 16:58:49 +000078#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +000079 JUST_CONTEXT = (1 << 5),
Denis Vlasenko49622d72007-03-10 16:58:49 +000080#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +000081};
Eric Andersen7eb79ff2004-09-02 22:21:41 +000082
Denis Vlasenko0c68a872008-12-02 22:56:59 +000083static int print_common(unsigned id, const char *name, const char *prefix)
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000084{
Denis Vlasenko34e68c82008-10-29 00:27:31 +000085 if (prefix) {
86 printf("%s", prefix);
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000087 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +000088 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
89 printf("%u", id);
90 }
91 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
92 if (name) {
93 printf(option_mask32 ? "%s" : "(%s)", name);
94 } else {
95 /* Don't set error status flag in default mode */
96 if (option_mask32) {
97 if (ENABLE_DESKTOP)
98 bb_error_msg("unknown ID %u", id);
99 return EXIT_FAILURE;
100 }
101 }
102 }
103 return EXIT_SUCCESS;
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000104}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000106static int print_group(gid_t id, const char *prefix)
107{
Denis Vlasenko0c68a872008-12-02 22:56:59 +0000108 return print_common(id, gid2group(id), prefix);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000109}
110
Bernhard Reutner-Fischer65591002008-11-05 08:15:13 +0000111static int print_user(uid_t id, const char *prefix)
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000112{
Denis Vlasenko0c68a872008-12-02 22:56:59 +0000113 return print_common(id, uid2uname(id), prefix);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000114}
115
116/* On error set *n < 0 and return >= 0
117 * If *n is too small, update it and return < 0
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200118 * (ok to trash groups[] in both cases)
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000119 * Otherwise fill in groups[] and return >= 0
120 */
121static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
122{
123 int m;
124
125 if (username) {
126 /* If the user is a member of more than
127 * *n groups, then -1 is returned. Otherwise >= 0.
128 * (and no defined way of detecting errors?!) */
129 m = getgrouplist(username, rgid, groups, n);
130 /* I guess *n < 0 might indicate error. Anyway,
131 * malloc'ing -1 bytes won't be good, so: */
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200132 if (*n < 0)
133 return 0;
134 return m;
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000135 }
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200136
137 *n = getgroups(*n, groups);
138 if (*n >= 0)
139 return *n;
140 /* Error */
141 if (errno == EINVAL) /* *n is too small? */
142 *n = getgroups(0, groups); /* get needed *n */
143 /* if *n >= 0, return -1 (got new *n), else return 0 (error): */
144 return -(*n >= 0);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000145}
Denis Vlasenko22284262008-09-18 00:56:24 +0000146
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000147int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000148int id_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000149{
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000150 uid_t ruid;
151 gid_t rgid;
152 uid_t euid;
153 gid_t egid;
154 unsigned opt;
155 int i;
156 int status = EXIT_SUCCESS;
157 const char *prefix;
Denis Vlasenko22284262008-09-18 00:56:24 +0000158 const char *username;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000159#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000160 security_context_t scontext = NULL;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000161#endif
Tito Ragusa33092f12011-06-21 17:11:40 +0200162
163 if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) {
Denys Vlasenkod5ac9c82011-06-22 04:17:49 +0200164 /* TODO: coreutils groups prepend "USER : " prefix,
165 * and accept many usernames. Example:
166 * # groups root root
167 * root : root
168 * root : root
169 */
170 opt = option_mask32 = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER;
Tito Ragusa33092f12011-06-21 17:11:40 +0200171 } else {
172 /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
173 /* Don't allow more than one username */
174 opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
Denys Vlasenko69675782013-01-14 01:34:48 +0100175 IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
Tito Ragusa33092f12011-06-21 17:11:40 +0200176 opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
177 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000178
Denis Vlasenko22284262008-09-18 00:56:24 +0000179 username = argv[optind];
Denis Vlasenko22284262008-09-18 00:56:24 +0000180 if (username) {
Denis Vlasenkod7a805e2008-12-03 19:05:55 +0000181 struct passwd *p = xgetpwnam(username);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000182 euid = ruid = p->pw_uid;
183 egid = rgid = p->pw_gid;
Denis Vlasenko22284262008-09-18 00:56:24 +0000184 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000185 egid = getegid();
186 rgid = getgid();
187 euid = geteuid();
188 ruid = getuid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000189 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000190 /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
191 /* id says: print the real ID instead of the effective ID, with -ugG */
Bernhard Reutner-Fischera6438162008-11-05 08:18:16 +0000192 /* in fact in this case egid is always printed if egid != rgid */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000193 if (!opt || (opt & JUST_ALL_GROUPS)) {
194 gid_t *groups;
195 int n;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000196
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000197 if (!opt) {
198 /* Default Mode */
199 status |= print_user(ruid, "uid=");
200 status |= print_group(rgid, " gid=");
201 if (euid != ruid)
202 status |= print_user(euid, " euid=");
203 if (egid != rgid)
204 status |= print_group(egid, " egid=");
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000205 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000206 /* JUST_ALL_GROUPS */
207 status |= print_group(rgid, NULL);
208 if (egid != rgid)
209 status |= print_group(egid, " ");
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000210 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000211 /* We are supplying largish buffer, trying
212 * to not run get_groups() twice. That might be slow
213 * ("user database in remote SQL server" case) */
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200214 groups = xmalloc(64 * sizeof(groups[0]));
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000215 n = 64;
216 if (get_groups(username, rgid, groups, &n) < 0) {
217 /* Need bigger buffer after all */
Denys Vlasenko52f4fe92011-06-22 16:42:36 +0200218 groups = xrealloc(groups, n * sizeof(groups[0]));
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000219 get_groups(username, rgid, groups, &n);
220 }
221 if (n > 0) {
222 /* Print the list */
223 prefix = " groups=";
224 for (i = 0; i < n; i++) {
225 if (opt && (groups[i] == rgid || groups[i] == egid))
226 continue;
227 status |= print_group(groups[i], opt ? " " : prefix);
228 prefix = ",";
229 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000230 } else if (n < 0) { /* error in get_groups() */
Denys Vlasenkodf7b6572011-01-26 16:11:19 +0100231 if (ENABLE_DESKTOP)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100232 bb_error_msg_and_die("can't get groups");
Denys Vlasenkodf7b6572011-01-26 16:11:19 +0100233 return EXIT_FAILURE;
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000234 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000235 if (ENABLE_FEATURE_CLEAN_UP)
236 free(groups);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000237#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000238 if (is_selinux_enabled()) {
239 if (getcon(&scontext) == 0)
240 printf(" context=%s", scontext);
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000241 }
242#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000243 } else if (opt & PRINT_REAL) {
244 euid = ruid;
245 egid = rgid;
Eric Andersenc1b8f122001-01-25 05:12:02 +0000246 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000247
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000248 if (opt & JUST_USER)
249 status |= print_user(euid, NULL);
250 else if (opt & JUST_GROUP)
251 status |= print_group(egid, NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000252#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000253 else if (opt & JUST_CONTEXT) {
254 selinux_or_die();
255 if (username || getcon(&scontext)) {
256 bb_error_msg_and_die("can't get process context%s",
257 username ? " for a different user" : "");
258 }
259 fputs(scontext, stdout);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000260 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000261 /* freecon(NULL) seems to be harmless */
262 if (ENABLE_FEATURE_CLEAN_UP)
263 freecon(scontext);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000264#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +0000265 bb_putchar('\n');
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000266 fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000267}