blob: ec9227d063e4224adb513043c350cb282aed8abb [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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Eric Andersen7eb79ff2004-09-02 22:21:41 +000019
Denis Vlasenkode7a52f2008-11-17 00:12:17 +000020#if !ENABLE_USE_BB_PWD_GRP
Denis Vlasenko5ff12652008-11-16 15:03:11 +000021#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"
24#endif
25#endif
Denis Vlasenkode7a52f2008-11-17 00:12:17 +000026#endif
Denis Vlasenko5ff12652008-11-16 15:03:11 +000027
Denis Vlasenko34e68c82008-10-29 00:27:31 +000028enum {
29 PRINT_REAL = (1 << 0),
30 NAME_NOT_NUMBER = (1 << 1),
31 JUST_USER = (1 << 2),
32 JUST_GROUP = (1 << 3),
33 JUST_ALL_GROUPS = (1 << 4),
Denis Vlasenko49622d72007-03-10 16:58:49 +000034#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +000035 JUST_CONTEXT = (1 << 5),
Denis Vlasenko49622d72007-03-10 16:58:49 +000036#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +000037};
Eric Andersen7eb79ff2004-09-02 22:21:41 +000038
Denis Vlasenko0c68a872008-12-02 22:56:59 +000039static int print_common(unsigned id, const char *name, const char *prefix)
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000040{
Denis Vlasenko34e68c82008-10-29 00:27:31 +000041 if (prefix) {
42 printf("%s", prefix);
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000043 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +000044 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
45 printf("%u", id);
46 }
47 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
48 if (name) {
49 printf(option_mask32 ? "%s" : "(%s)", name);
50 } else {
51 /* Don't set error status flag in default mode */
52 if (option_mask32) {
53 if (ENABLE_DESKTOP)
54 bb_error_msg("unknown ID %u", id);
55 return EXIT_FAILURE;
56 }
57 }
58 }
59 return EXIT_SUCCESS;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000060}
Manuel Novoa III cad53642003-03-19 09:13:01 +000061
Denis Vlasenko34e68c82008-10-29 00:27:31 +000062static int print_group(gid_t id, const char *prefix)
63{
Denis Vlasenko0c68a872008-12-02 22:56:59 +000064 return print_common(id, gid2group(id), prefix);
Denis Vlasenko34e68c82008-10-29 00:27:31 +000065}
66
Bernhard Reutner-Fischer65591002008-11-05 08:15:13 +000067static int print_user(uid_t id, const char *prefix)
Denis Vlasenko34e68c82008-10-29 00:27:31 +000068{
Denis Vlasenko0c68a872008-12-02 22:56:59 +000069 return print_common(id, uid2uname(id), prefix);
Denis Vlasenko34e68c82008-10-29 00:27:31 +000070}
71
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
76 */
77static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
78{
79 int m;
80
81 if (username) {
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: */
88 //if (*n < 0)
89 // return 0;
90 //return m;
Denis Vlasenkoe7368f12008-10-29 10:30:54 +000091 //commented out here, happens below anyway
Denis Vlasenko34e68c82008-10-29 00:27:31 +000092 } else {
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 */
96 m = - (nn > *n);
97 *n = nn;
98 }
99 if (*n < 0)
100 return 0; /* error, don't return < 0! */
101 return m;
102}
Denis Vlasenko22284262008-09-18 00:56:24 +0000103
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000104int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000105int id_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000106{
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000107 uid_t ruid;
108 gid_t rgid;
109 uid_t euid;
110 gid_t egid;
111 unsigned opt;
112 int i;
113 int status = EXIT_SUCCESS;
114 const char *prefix;
Denis Vlasenko22284262008-09-18 00:56:24 +0000115 const char *username;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000116#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000117 security_context_t scontext = NULL;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000118#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000119 /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000120 /* Don't allow more than one username */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000121 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 +0000122 IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
123 opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000124
Denis Vlasenko22284262008-09-18 00:56:24 +0000125 username = argv[optind];
Denis Vlasenko22284262008-09-18 00:56:24 +0000126 if (username) {
Denis Vlasenkod7a805e2008-12-03 19:05:55 +0000127 struct passwd *p = xgetpwnam(username);
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000128 euid = ruid = p->pw_uid;
129 egid = rgid = p->pw_gid;
Denis Vlasenko22284262008-09-18 00:56:24 +0000130 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000131 egid = getegid();
132 rgid = getgid();
133 euid = geteuid();
134 ruid = getuid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000135 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000136 /* 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 */
Bernhard Reutner-Fischera6438162008-11-05 08:18:16 +0000138 /* in fact in this case egid is always printed if egid != rgid */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000139 if (!opt || (opt & JUST_ALL_GROUPS)) {
140 gid_t *groups;
141 int n;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000142
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000143 if (!opt) {
144 /* Default Mode */
145 status |= print_user(ruid, "uid=");
146 status |= print_group(rgid, " gid=");
147 if (euid != ruid)
148 status |= print_user(euid, " euid=");
149 if (egid != rgid)
150 status |= print_group(egid, " egid=");
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000151 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000152 /* JUST_ALL_GROUPS */
153 status |= print_group(rgid, NULL);
154 if (egid != rgid)
155 status |= print_group(egid, " ");
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000156 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000157 /* 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) */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000160 groups = xmalloc(64 * sizeof(gid_t));
161 n = 64;
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);
166 }
167 if (n > 0) {
168 /* Print the list */
169 prefix = " groups=";
170 for (i = 0; i < n; i++) {
171 if (opt && (groups[i] == rgid || groups[i] == egid))
172 continue;
173 status |= print_group(groups[i], opt ? " " : prefix);
174 prefix = ",";
175 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000176 } else if (n < 0) { /* error in get_groups() */
177 if (!ENABLE_DESKTOP)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100178 bb_error_msg_and_die("can't get groups");
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000179 else
180 return EXIT_FAILURE;
181 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000182 if (ENABLE_FEATURE_CLEAN_UP)
183 free(groups);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000184#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000185 if (is_selinux_enabled()) {
186 if (getcon(&scontext) == 0)
187 printf(" context=%s", scontext);
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000188 }
189#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000190 } else if (opt & PRINT_REAL) {
191 euid = ruid;
192 egid = rgid;
Eric Andersenc1b8f122001-01-25 05:12:02 +0000193 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000194
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000195 if (opt & JUST_USER)
196 status |= print_user(euid, NULL);
197 else if (opt & JUST_GROUP)
198 status |= print_group(egid, NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000199#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000200 else if (opt & JUST_CONTEXT) {
201 selinux_or_die();
202 if (username || getcon(&scontext)) {
203 bb_error_msg_and_die("can't get process context%s",
204 username ? " for a different user" : "");
205 }
206 fputs(scontext, stdout);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000207 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000208 /* freecon(NULL) seems to be harmless */
209 if (ENABLE_FEATURE_CLEAN_UP)
210 freecon(scontext);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000211#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +0000212 bb_putchar('\n');
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000213 fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000214}