Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini id implementation for busybox |
| 4 | * |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 by Randolph Chung <tausq@debian.org> |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it> |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 11 | /* BB_AUDIT SUSv3 compliant. */ |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 12 | /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever |
| 13 | * length and to be more similar to GNU id. |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 14 | * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp> |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 15 | * Added -G option Tito Ragusa (C) 2008 for SUSv3. |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 16 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | |
Tito Ragusa | 33092f1 | 2011-06-21 17:11:40 +0200 | [diff] [blame] | 18 | //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 Vlasenko | d5ac9c8 | 2011-06-22 04:17:49 +0200 | [diff] [blame] | 33 | //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 Ragusa | 33092f1 | 2011-06-21 17:11:40 +0200 | [diff] [blame] | 35 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 36 | //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 Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 40 | //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 Ragusa | 33092f1 | 2011-06-21 17:11:40 +0200 | [diff] [blame] | 53 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 62 | #include "libbb.h" |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 63 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 64 | /* This is a NOEXEC applet. Be very careful! */ |
| 65 | |
Denis Vlasenko | de7a52f | 2008-11-17 00:12:17 +0000 | [diff] [blame] | 66 | #if !ENABLE_USE_BB_PWD_GRP |
Mike Frysinger | 445e754 | 2013-03-12 11:13:22 -0400 | [diff] [blame] | 67 | #if defined(__UCLIBC__) && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 30) |
Denis Vlasenko | 5ff1265 | 2008-11-16 15:03:11 +0000 | [diff] [blame] | 68 | #error "Sorry, you need at least uClibc version 0.9.30 for id applet to build" |
| 69 | #endif |
| 70 | #endif |
| 71 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 72 | enum { |
| 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 Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 78 | #if ENABLE_SELINUX |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 79 | JUST_CONTEXT = (1 << 5), |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 80 | #endif |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 81 | }; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 83 | static int print_common(unsigned id, const char *name, const char *prefix) |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 84 | { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 85 | if (prefix) { |
| 86 | printf("%s", prefix); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 87 | } |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 88 | 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 Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 104 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 106 | static int print_group(gid_t id, const char *prefix) |
| 107 | { |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 108 | return print_common(id, gid2group(id), prefix); |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Bernhard Reutner-Fischer | 6559100 | 2008-11-05 08:15:13 +0000 | [diff] [blame] | 111 | static int print_user(uid_t id, const char *prefix) |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 112 | { |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 113 | return print_common(id, uid2uname(id), prefix); |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* On error set *n < 0 and return >= 0 |
| 117 | * If *n is too small, update it and return < 0 |
Denys Vlasenko | 52f4fe9 | 2011-06-22 16:42:36 +0200 | [diff] [blame] | 118 | * (ok to trash groups[] in both cases) |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 119 | * Otherwise fill in groups[] and return >= 0 |
| 120 | */ |
| 121 | static 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 Vlasenko | 52f4fe9 | 2011-06-22 16:42:36 +0200 | [diff] [blame] | 132 | if (*n < 0) |
| 133 | return 0; |
| 134 | return m; |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 135 | } |
Denys Vlasenko | 52f4fe9 | 2011-06-22 16:42:36 +0200 | [diff] [blame] | 136 | |
| 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 Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 145 | } |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 147 | int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 148 | int id_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 149 | { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 150 | 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 Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 158 | const char *username; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 159 | #if ENABLE_SELINUX |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 160 | security_context_t scontext = NULL; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 161 | #endif |
Tito Ragusa | 33092f1 | 2011-06-21 17:11:40 +0200 | [diff] [blame] | 162 | |
| 163 | if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) { |
Denys Vlasenko | d5ac9c8 | 2011-06-22 04:17:49 +0200 | [diff] [blame] | 164 | /* 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 Ragusa | 33092f1 | 2011-06-21 17:11:40 +0200 | [diff] [blame] | 171 | } 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 Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 175 | IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G"); |
Tito Ragusa | 33092f1 | 2011-06-21 17:11:40 +0200 | [diff] [blame] | 176 | opt = getopt32(argv, "rnugG" IF_SELINUX("Z")); |
| 177 | } |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 178 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 179 | username = argv[optind]; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 180 | if (username) { |
Denis Vlasenko | d7a805e | 2008-12-03 19:05:55 +0000 | [diff] [blame] | 181 | struct passwd *p = xgetpwnam(username); |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 182 | euid = ruid = p->pw_uid; |
| 183 | egid = rgid = p->pw_gid; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 184 | } else { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 185 | egid = getegid(); |
| 186 | rgid = getgid(); |
| 187 | euid = geteuid(); |
| 188 | ruid = getuid(); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 189 | } |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 190 | /* 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-Fischer | a643816 | 2008-11-05 08:18:16 +0000 | [diff] [blame] | 192 | /* in fact in this case egid is always printed if egid != rgid */ |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 193 | if (!opt || (opt & JUST_ALL_GROUPS)) { |
| 194 | gid_t *groups; |
| 195 | int n; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 197 | 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 McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 205 | } else { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 206 | /* JUST_ALL_GROUPS */ |
| 207 | status |= print_group(rgid, NULL); |
| 208 | if (egid != rgid) |
| 209 | status |= print_group(egid, " "); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 210 | } |
Denis Vlasenko | e7368f1 | 2008-10-29 10:30:54 +0000 | [diff] [blame] | 211 | /* 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 Vlasenko | 52f4fe9 | 2011-06-22 16:42:36 +0200 | [diff] [blame] | 214 | groups = xmalloc(64 * sizeof(groups[0])); |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 215 | n = 64; |
| 216 | if (get_groups(username, rgid, groups, &n) < 0) { |
| 217 | /* Need bigger buffer after all */ |
Denys Vlasenko | 52f4fe9 | 2011-06-22 16:42:36 +0200 | [diff] [blame] | 218 | groups = xrealloc(groups, n * sizeof(groups[0])); |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 219 | 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 Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 230 | } else if (n < 0) { /* error in get_groups() */ |
Denys Vlasenko | df7b657 | 2011-01-26 16:11:19 +0100 | [diff] [blame] | 231 | if (ENABLE_DESKTOP) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 232 | bb_error_msg_and_die("can't get groups"); |
Denys Vlasenko | df7b657 | 2011-01-26 16:11:19 +0100 | [diff] [blame] | 233 | return EXIT_FAILURE; |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 234 | } |
Denis Vlasenko | e7368f1 | 2008-10-29 10:30:54 +0000 | [diff] [blame] | 235 | if (ENABLE_FEATURE_CLEAN_UP) |
| 236 | free(groups); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 237 | #if ENABLE_SELINUX |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 238 | if (is_selinux_enabled()) { |
| 239 | if (getcon(&scontext) == 0) |
| 240 | printf(" context=%s", scontext); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 241 | } |
| 242 | #endif |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 243 | } else if (opt & PRINT_REAL) { |
| 244 | euid = ruid; |
| 245 | egid = rgid; |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 246 | } |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 247 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 248 | if (opt & JUST_USER) |
| 249 | status |= print_user(euid, NULL); |
| 250 | else if (opt & JUST_GROUP) |
| 251 | status |= print_group(egid, NULL); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 252 | #if ENABLE_SELINUX |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 253 | 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 Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 260 | } |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 261 | /* freecon(NULL) seems to be harmless */ |
| 262 | if (ENABLE_FEATURE_CLEAN_UP) |
| 263 | freecon(scontext); |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 264 | #endif |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 265 | bb_putchar('\n'); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 266 | fflush_stdout_and_exit(status); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 267 | } |