blob: 6ddb2366646e19fa7cdafd773cc821b608bde35e [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 Vlasenko34e68c82008-10-29 00:27:31 +000039static int print_common(unsigned id,
40 char* FAST_FUNC bb_getXXXid(char *name, int bufsize, long uid),
41 const char *prefix)
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000042{
Denis Vlasenko34e68c82008-10-29 00:27:31 +000043 const char *name = bb_getXXXid(NULL, 0, id);
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000044
Denis Vlasenko34e68c82008-10-29 00:27:31 +000045 if (prefix) {
46 printf("%s", prefix);
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000047 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +000048 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
49 printf("%u", id);
50 }
51 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
52 if (name) {
53 printf(option_mask32 ? "%s" : "(%s)", name);
54 } else {
55 /* Don't set error status flag in default mode */
56 if (option_mask32) {
57 if (ENABLE_DESKTOP)
58 bb_error_msg("unknown ID %u", id);
59 return EXIT_FAILURE;
60 }
61 }
62 }
63 return EXIT_SUCCESS;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000064}
Manuel Novoa III cad53642003-03-19 09:13:01 +000065
Denis Vlasenko34e68c82008-10-29 00:27:31 +000066static int print_group(gid_t id, const char *prefix)
67{
68 return print_common(id, bb_getgrgid, prefix);
69}
70
Bernhard Reutner-Fischer65591002008-11-05 08:15:13 +000071static int print_user(uid_t id, const char *prefix)
Denis Vlasenko34e68c82008-10-29 00:27:31 +000072{
73 return print_common(id, bb_getpwuid, prefix);
74}
75
76/* On error set *n < 0 and return >= 0
77 * If *n is too small, update it and return < 0
78 * (ok to trash groups[] in both cases)
79 * Otherwise fill in groups[] and return >= 0
80 */
81static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
82{
83 int m;
84
85 if (username) {
86 /* If the user is a member of more than
87 * *n groups, then -1 is returned. Otherwise >= 0.
88 * (and no defined way of detecting errors?!) */
89 m = getgrouplist(username, rgid, groups, n);
90 /* I guess *n < 0 might indicate error. Anyway,
91 * malloc'ing -1 bytes won't be good, so: */
92 //if (*n < 0)
93 // return 0;
94 //return m;
Denis Vlasenkoe7368f12008-10-29 10:30:54 +000095 //commented out here, happens below anyway
Denis Vlasenko34e68c82008-10-29 00:27:31 +000096 } else {
97 /* On error -1 is returned, which ends up in *n */
98 int nn = getgroups(*n, groups);
99 /* 0: nn <= *n, groups[] was big enough; -1 otherwise */
100 m = - (nn > *n);
101 *n = nn;
102 }
103 if (*n < 0)
104 return 0; /* error, don't return < 0! */
105 return m;
106}
Denis Vlasenko22284262008-09-18 00:56:24 +0000107
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000108int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000109int id_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000110{
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000111 uid_t ruid;
112 gid_t rgid;
113 uid_t euid;
114 gid_t egid;
115 unsigned opt;
116 int i;
117 int status = EXIT_SUCCESS;
118 const char *prefix;
Denis Vlasenko22284262008-09-18 00:56:24 +0000119 const char *username;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000120#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000121 security_context_t scontext = NULL;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000122#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000123 /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000124 /* Don't allow more than one username */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000125 opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
126 USE_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
127 opt = getopt32(argv, "rnugG" USE_SELINUX("Z"));
128
Denis Vlasenko22284262008-09-18 00:56:24 +0000129 username = argv[optind];
Denis Vlasenko22284262008-09-18 00:56:24 +0000130 if (username) {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000131 struct passwd *p = getpwnam(username);
132 if (!p)
133 bb_error_msg_and_die("unknown user %s", username);
134 euid = ruid = p->pw_uid;
135 egid = rgid = p->pw_gid;
Denis Vlasenko22284262008-09-18 00:56:24 +0000136 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000137 egid = getegid();
138 rgid = getgid();
139 euid = geteuid();
140 ruid = getuid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000141 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000142 /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
143 /* id says: print the real ID instead of the effective ID, with -ugG */
Bernhard Reutner-Fischera6438162008-11-05 08:18:16 +0000144 /* in fact in this case egid is always printed if egid != rgid */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000145 if (!opt || (opt & JUST_ALL_GROUPS)) {
146 gid_t *groups;
147 int n;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000148
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000149 if (!opt) {
150 /* Default Mode */
151 status |= print_user(ruid, "uid=");
152 status |= print_group(rgid, " gid=");
153 if (euid != ruid)
154 status |= print_user(euid, " euid=");
155 if (egid != rgid)
156 status |= print_group(egid, " egid=");
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000157 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000158 /* JUST_ALL_GROUPS */
159 status |= print_group(rgid, NULL);
160 if (egid != rgid)
161 status |= print_group(egid, " ");
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000162 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000163 /* We are supplying largish buffer, trying
164 * to not run get_groups() twice. That might be slow
165 * ("user database in remote SQL server" case) */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000166 groups = xmalloc(64 * sizeof(gid_t));
167 n = 64;
168 if (get_groups(username, rgid, groups, &n) < 0) {
169 /* Need bigger buffer after all */
170 groups = xrealloc(groups, n * sizeof(gid_t));
171 get_groups(username, rgid, groups, &n);
172 }
173 if (n > 0) {
174 /* Print the list */
175 prefix = " groups=";
176 for (i = 0; i < n; i++) {
177 if (opt && (groups[i] == rgid || groups[i] == egid))
178 continue;
179 status |= print_group(groups[i], opt ? " " : prefix);
180 prefix = ",";
181 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000182 } else if (n < 0) { /* error in get_groups() */
183 if (!ENABLE_DESKTOP)
184 bb_error_msg_and_die("cannot get groups");
185 else
186 return EXIT_FAILURE;
187 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000188 if (ENABLE_FEATURE_CLEAN_UP)
189 free(groups);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000190#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000191 if (is_selinux_enabled()) {
192 if (getcon(&scontext) == 0)
193 printf(" context=%s", scontext);
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000194 }
195#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000196 } else if (opt & PRINT_REAL) {
197 euid = ruid;
198 egid = rgid;
Eric Andersenc1b8f122001-01-25 05:12:02 +0000199 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000200
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000201 if (opt & JUST_USER)
202 status |= print_user(euid, NULL);
203 else if (opt & JUST_GROUP)
204 status |= print_group(egid, NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000205#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000206 else if (opt & JUST_CONTEXT) {
207 selinux_or_die();
208 if (username || getcon(&scontext)) {
209 bb_error_msg_and_die("can't get process context%s",
210 username ? " for a different user" : "");
211 }
212 fputs(scontext, stdout);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000213 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000214 /* freecon(NULL) seems to be harmless */
215 if (ENABLE_FEATURE_CLEAN_UP)
216 freecon(scontext);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000217#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +0000218 bb_putchar('\n');
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000219 fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000220}