blob: f33f9ab254367221b29db3bfb6799ea12b756eeb [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 Vlasenko5ff12652008-11-16 15:03:11 +000020#if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0)
21#if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 30)
22#error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
23#endif
24#endif
25
Denis Vlasenko34e68c82008-10-29 00:27:31 +000026enum {
27 PRINT_REAL = (1 << 0),
28 NAME_NOT_NUMBER = (1 << 1),
29 JUST_USER = (1 << 2),
30 JUST_GROUP = (1 << 3),
31 JUST_ALL_GROUPS = (1 << 4),
Denis Vlasenko49622d72007-03-10 16:58:49 +000032#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +000033 JUST_CONTEXT = (1 << 5),
Denis Vlasenko49622d72007-03-10 16:58:49 +000034#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +000035};
Eric Andersen7eb79ff2004-09-02 22:21:41 +000036
Denis Vlasenko34e68c82008-10-29 00:27:31 +000037static int print_common(unsigned id,
38 char* FAST_FUNC bb_getXXXid(char *name, int bufsize, long uid),
39 const char *prefix)
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000040{
Denis Vlasenko34e68c82008-10-29 00:27:31 +000041 const char *name = bb_getXXXid(NULL, 0, id);
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000042
Denis Vlasenko34e68c82008-10-29 00:27:31 +000043 if (prefix) {
44 printf("%s", prefix);
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000045 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +000046 if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
47 printf("%u", id);
48 }
49 if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
50 if (name) {
51 printf(option_mask32 ? "%s" : "(%s)", name);
52 } else {
53 /* Don't set error status flag in default mode */
54 if (option_mask32) {
55 if (ENABLE_DESKTOP)
56 bb_error_msg("unknown ID %u", id);
57 return EXIT_FAILURE;
58 }
59 }
60 }
61 return EXIT_SUCCESS;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000062}
Manuel Novoa III cad53642003-03-19 09:13:01 +000063
Denis Vlasenko34e68c82008-10-29 00:27:31 +000064static int print_group(gid_t id, const char *prefix)
65{
66 return print_common(id, bb_getgrgid, prefix);
67}
68
Bernhard Reutner-Fischer65591002008-11-05 08:15:13 +000069static int print_user(uid_t id, const char *prefix)
Denis Vlasenko34e68c82008-10-29 00:27:31 +000070{
71 return print_common(id, bb_getpwuid, prefix);
72}
73
74/* On error set *n < 0 and return >= 0
75 * If *n is too small, update it and return < 0
76 * (ok to trash groups[] in both cases)
77 * Otherwise fill in groups[] and return >= 0
78 */
79static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
80{
81 int m;
82
83 if (username) {
84 /* If the user is a member of more than
85 * *n groups, then -1 is returned. Otherwise >= 0.
86 * (and no defined way of detecting errors?!) */
87 m = getgrouplist(username, rgid, groups, n);
88 /* I guess *n < 0 might indicate error. Anyway,
89 * malloc'ing -1 bytes won't be good, so: */
90 //if (*n < 0)
91 // return 0;
92 //return m;
Denis Vlasenkoe7368f12008-10-29 10:30:54 +000093 //commented out here, happens below anyway
Denis Vlasenko34e68c82008-10-29 00:27:31 +000094 } else {
95 /* On error -1 is returned, which ends up in *n */
96 int nn = getgroups(*n, groups);
97 /* 0: nn <= *n, groups[] was big enough; -1 otherwise */
98 m = - (nn > *n);
99 *n = nn;
100 }
101 if (*n < 0)
102 return 0; /* error, don't return < 0! */
103 return m;
104}
Denis Vlasenko22284262008-09-18 00:56:24 +0000105
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000106int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000107int id_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000108{
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000109 uid_t ruid;
110 gid_t rgid;
111 uid_t euid;
112 gid_t egid;
113 unsigned opt;
114 int i;
115 int status = EXIT_SUCCESS;
116 const char *prefix;
Denis Vlasenko22284262008-09-18 00:56:24 +0000117 const char *username;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000118#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000119 security_context_t scontext = NULL;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000120#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000121 /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000122 /* Don't allow more than one username */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000123 opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
124 USE_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
125 opt = getopt32(argv, "rnugG" USE_SELINUX("Z"));
126
Denis Vlasenko22284262008-09-18 00:56:24 +0000127 username = argv[optind];
Denis Vlasenko22284262008-09-18 00:56:24 +0000128 if (username) {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000129 struct passwd *p = getpwnam(username);
130 if (!p)
131 bb_error_msg_and_die("unknown user %s", username);
132 euid = ruid = p->pw_uid;
133 egid = rgid = p->pw_gid;
Denis Vlasenko22284262008-09-18 00:56:24 +0000134 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000135 egid = getegid();
136 rgid = getgid();
137 euid = geteuid();
138 ruid = getuid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000139 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000140 /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
141 /* id says: print the real ID instead of the effective ID, with -ugG */
Bernhard Reutner-Fischera6438162008-11-05 08:18:16 +0000142 /* in fact in this case egid is always printed if egid != rgid */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000143 if (!opt || (opt & JUST_ALL_GROUPS)) {
144 gid_t *groups;
145 int n;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000146
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000147 if (!opt) {
148 /* Default Mode */
149 status |= print_user(ruid, "uid=");
150 status |= print_group(rgid, " gid=");
151 if (euid != ruid)
152 status |= print_user(euid, " euid=");
153 if (egid != rgid)
154 status |= print_group(egid, " egid=");
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000155 } else {
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000156 /* JUST_ALL_GROUPS */
157 status |= print_group(rgid, NULL);
158 if (egid != rgid)
159 status |= print_group(egid, " ");
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000160 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000161 /* We are supplying largish buffer, trying
162 * to not run get_groups() twice. That might be slow
163 * ("user database in remote SQL server" case) */
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000164 groups = xmalloc(64 * sizeof(gid_t));
165 n = 64;
166 if (get_groups(username, rgid, groups, &n) < 0) {
167 /* Need bigger buffer after all */
168 groups = xrealloc(groups, n * sizeof(gid_t));
169 get_groups(username, rgid, groups, &n);
170 }
171 if (n > 0) {
172 /* Print the list */
173 prefix = " groups=";
174 for (i = 0; i < n; i++) {
175 if (opt && (groups[i] == rgid || groups[i] == egid))
176 continue;
177 status |= print_group(groups[i], opt ? " " : prefix);
178 prefix = ",";
179 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000180 } else if (n < 0) { /* error in get_groups() */
181 if (!ENABLE_DESKTOP)
182 bb_error_msg_and_die("cannot get groups");
183 else
184 return EXIT_FAILURE;
185 }
Denis Vlasenkoe7368f12008-10-29 10:30:54 +0000186 if (ENABLE_FEATURE_CLEAN_UP)
187 free(groups);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000188#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000189 if (is_selinux_enabled()) {
190 if (getcon(&scontext) == 0)
191 printf(" context=%s", scontext);
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000192 }
193#endif
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000194 } else if (opt & PRINT_REAL) {
195 euid = ruid;
196 egid = rgid;
Eric Andersenc1b8f122001-01-25 05:12:02 +0000197 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000198
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000199 if (opt & JUST_USER)
200 status |= print_user(euid, NULL);
201 else if (opt & JUST_GROUP)
202 status |= print_group(egid, NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000203#if ENABLE_SELINUX
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000204 else if (opt & JUST_CONTEXT) {
205 selinux_or_die();
206 if (username || getcon(&scontext)) {
207 bb_error_msg_and_die("can't get process context%s",
208 username ? " for a different user" : "");
209 }
210 fputs(scontext, stdout);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000211 }
Denis Vlasenko34e68c82008-10-29 00:27:31 +0000212 /* freecon(NULL) seems to be harmless */
213 if (ENABLE_FEATURE_CLEAN_UP)
214 freecon(scontext);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000215#endif
Denis Vlasenko4daad902007-09-27 10:20:47 +0000216 bb_putchar('\n');
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000217 fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000218}