[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Mini id implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
|
---|
[2725] | 6 | * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
|
---|
[821] | 7 | *
|
---|
[2725] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 9 | */
|
---|
| 10 |
|
---|
[2725] | 11 | /* BB_AUDIT SUSv3 compliant. */
|
---|
| 12 | /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever
|
---|
| 13 | * length and to be more similar to GNU id.
|
---|
[1765] | 14 | * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
|
---|
[2725] | 15 | * Added -G option Tito Ragusa (C) 2008 for SUSv3.
|
---|
[821] | 16 | */
|
---|
| 17 |
|
---|
[3232] | 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 |
|
---|
| 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 ))
|
---|
| 35 |
|
---|
| 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"
|
---|
| 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 |
|
---|
| 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 |
|
---|
[1765] | 62 | #include "libbb.h"
|
---|
[821] | 63 |
|
---|
[2725] | 64 | /* This is a NOEXEC applet. Be very careful! */
|
---|
| 65 |
|
---|
| 66 | #if !ENABLE_USE_BB_PWD_GRP
|
---|
| 67 | #if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0)
|
---|
| 68 | #if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 30)
|
---|
| 69 | #error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
|
---|
| 70 | #endif
|
---|
| 71 | #endif
|
---|
| 72 | #endif
|
---|
| 73 |
|
---|
| 74 | enum {
|
---|
| 75 | PRINT_REAL = (1 << 0),
|
---|
| 76 | NAME_NOT_NUMBER = (1 << 1),
|
---|
| 77 | JUST_USER = (1 << 2),
|
---|
| 78 | JUST_GROUP = (1 << 3),
|
---|
| 79 | JUST_ALL_GROUPS = (1 << 4),
|
---|
[1765] | 80 | #if ENABLE_SELINUX
|
---|
[2725] | 81 | JUST_CONTEXT = (1 << 5),
|
---|
[1765] | 82 | #endif
|
---|
[2725] | 83 | };
|
---|
[821] | 84 |
|
---|
[2725] | 85 | static int print_common(unsigned id, const char *name, const char *prefix)
|
---|
[821] | 86 | {
|
---|
[2725] | 87 | if (prefix) {
|
---|
| 88 | printf("%s", prefix);
|
---|
| 89 | }
|
---|
| 90 | if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
|
---|
| 91 | printf("%u", id);
|
---|
| 92 | }
|
---|
| 93 | if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
|
---|
| 94 | if (name) {
|
---|
| 95 | printf(option_mask32 ? "%s" : "(%s)", name);
|
---|
| 96 | } else {
|
---|
| 97 | /* Don't set error status flag in default mode */
|
---|
| 98 | if (option_mask32) {
|
---|
| 99 | if (ENABLE_DESKTOP)
|
---|
| 100 | bb_error_msg("unknown ID %u", id);
|
---|
| 101 | return EXIT_FAILURE;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | return EXIT_SUCCESS;
|
---|
| 106 | }
|
---|
[821] | 107 |
|
---|
[2725] | 108 | static int print_group(gid_t id, const char *prefix)
|
---|
| 109 | {
|
---|
| 110 | return print_common(id, gid2group(id), prefix);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static int print_user(uid_t id, const char *prefix)
|
---|
| 114 | {
|
---|
| 115 | return print_common(id, uid2uname(id), prefix);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /* On error set *n < 0 and return >= 0
|
---|
| 119 | * If *n is too small, update it and return < 0
|
---|
[3232] | 120 | * (ok to trash groups[] in both cases)
|
---|
[2725] | 121 | * Otherwise fill in groups[] and return >= 0
|
---|
| 122 | */
|
---|
| 123 | static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
|
---|
| 124 | {
|
---|
| 125 | int m;
|
---|
| 126 |
|
---|
| 127 | if (username) {
|
---|
| 128 | /* If the user is a member of more than
|
---|
| 129 | * *n groups, then -1 is returned. Otherwise >= 0.
|
---|
| 130 | * (and no defined way of detecting errors?!) */
|
---|
| 131 | m = getgrouplist(username, rgid, groups, n);
|
---|
| 132 | /* I guess *n < 0 might indicate error. Anyway,
|
---|
| 133 | * malloc'ing -1 bytes won't be good, so: */
|
---|
[3232] | 134 | if (*n < 0)
|
---|
| 135 | return 0;
|
---|
| 136 | return m;
|
---|
[821] | 137 | }
|
---|
[3232] | 138 |
|
---|
| 139 | *n = getgroups(*n, groups);
|
---|
| 140 | if (*n >= 0)
|
---|
| 141 | return *n;
|
---|
| 142 | /* Error */
|
---|
| 143 | if (errno == EINVAL) /* *n is too small? */
|
---|
| 144 | *n = getgroups(0, groups); /* get needed *n */
|
---|
| 145 | /* if *n >= 0, return -1 (got new *n), else return 0 (error): */
|
---|
| 146 | return -(*n >= 0);
|
---|
[821] | 147 | }
|
---|
| 148 |
|
---|
[2725] | 149 | int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 150 | int id_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 151 | {
|
---|
[2725] | 152 | uid_t ruid;
|
---|
| 153 | gid_t rgid;
|
---|
| 154 | uid_t euid;
|
---|
| 155 | gid_t egid;
|
---|
| 156 | unsigned opt;
|
---|
| 157 | int i;
|
---|
| 158 | int status = EXIT_SUCCESS;
|
---|
| 159 | const char *prefix;
|
---|
| 160 | const char *username;
|
---|
[1765] | 161 | #if ENABLE_SELINUX
|
---|
[2725] | 162 | security_context_t scontext = NULL;
|
---|
[1765] | 163 | #endif
|
---|
[821] | 164 |
|
---|
[3232] | 165 | if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) {
|
---|
| 166 | /* TODO: coreutils groups prepend "USER : " prefix,
|
---|
| 167 | * and accept many usernames. Example:
|
---|
| 168 | * # groups root root
|
---|
| 169 | * root : root
|
---|
| 170 | * root : root
|
---|
| 171 | */
|
---|
| 172 | opt = option_mask32 = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER;
|
---|
| 173 | } else {
|
---|
| 174 | /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
|
---|
| 175 | /* Don't allow more than one username */
|
---|
| 176 | opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
|
---|
| 177 | IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
|
---|
| 178 | opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[2725] | 181 | username = argv[optind];
|
---|
| 182 | if (username) {
|
---|
| 183 | struct passwd *p = xgetpwnam(username);
|
---|
| 184 | euid = ruid = p->pw_uid;
|
---|
| 185 | egid = rgid = p->pw_gid;
|
---|
| 186 | } else {
|
---|
| 187 | egid = getegid();
|
---|
| 188 | rgid = getgid();
|
---|
| 189 | euid = geteuid();
|
---|
| 190 | ruid = getuid();
|
---|
[821] | 191 | }
|
---|
[2725] | 192 | /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
|
---|
| 193 | /* id says: print the real ID instead of the effective ID, with -ugG */
|
---|
| 194 | /* in fact in this case egid is always printed if egid != rgid */
|
---|
| 195 | if (!opt || (opt & JUST_ALL_GROUPS)) {
|
---|
| 196 | gid_t *groups;
|
---|
| 197 | int n;
|
---|
[821] | 198 |
|
---|
[2725] | 199 | if (!opt) {
|
---|
| 200 | /* Default Mode */
|
---|
| 201 | status |= print_user(ruid, "uid=");
|
---|
| 202 | status |= print_group(rgid, " gid=");
|
---|
| 203 | if (euid != ruid)
|
---|
| 204 | status |= print_user(euid, " euid=");
|
---|
| 205 | if (egid != rgid)
|
---|
| 206 | status |= print_group(egid, " egid=");
|
---|
[821] | 207 | } else {
|
---|
[2725] | 208 | /* JUST_ALL_GROUPS */
|
---|
| 209 | status |= print_group(rgid, NULL);
|
---|
| 210 | if (egid != rgid)
|
---|
| 211 | status |= print_group(egid, " ");
|
---|
| 212 | }
|
---|
| 213 | /* We are supplying largish buffer, trying
|
---|
| 214 | * to not run get_groups() twice. That might be slow
|
---|
| 215 | * ("user database in remote SQL server" case) */
|
---|
[3232] | 216 | groups = xmalloc(64 * sizeof(groups[0]));
|
---|
[2725] | 217 | n = 64;
|
---|
| 218 | if (get_groups(username, rgid, groups, &n) < 0) {
|
---|
| 219 | /* Need bigger buffer after all */
|
---|
[3232] | 220 | groups = xrealloc(groups, n * sizeof(groups[0]));
|
---|
[2725] | 221 | get_groups(username, rgid, groups, &n);
|
---|
| 222 | }
|
---|
| 223 | if (n > 0) {
|
---|
| 224 | /* Print the list */
|
---|
| 225 | prefix = " groups=";
|
---|
| 226 | for (i = 0; i < n; i++) {
|
---|
| 227 | if (opt && (groups[i] == rgid || groups[i] == egid))
|
---|
| 228 | continue;
|
---|
| 229 | status |= print_group(groups[i], opt ? " " : prefix);
|
---|
| 230 | prefix = ",";
|
---|
[1765] | 231 | }
|
---|
[2725] | 232 | } else if (n < 0) { /* error in get_groups() */
|
---|
[3232] | 233 | if (ENABLE_DESKTOP)
|
---|
[2725] | 234 | bb_error_msg_and_die("can't get groups");
|
---|
[3232] | 235 | return EXIT_FAILURE;
|
---|
[821] | 236 | }
|
---|
[2725] | 237 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 238 | free(groups);
|
---|
[1765] | 239 | #if ENABLE_SELINUX
|
---|
[2725] | 240 | if (is_selinux_enabled()) {
|
---|
| 241 | if (getcon(&scontext) == 0)
|
---|
| 242 | printf(" context=%s", scontext);
|
---|
[1765] | 243 | }
|
---|
| 244 | #endif
|
---|
[2725] | 245 | } else if (opt & PRINT_REAL) {
|
---|
| 246 | euid = ruid;
|
---|
| 247 | egid = rgid;
|
---|
[821] | 248 | }
|
---|
| 249 |
|
---|
[2725] | 250 | if (opt & JUST_USER)
|
---|
| 251 | status |= print_user(euid, NULL);
|
---|
| 252 | else if (opt & JUST_GROUP)
|
---|
| 253 | status |= print_group(egid, NULL);
|
---|
[1765] | 254 | #if ENABLE_SELINUX
|
---|
[2725] | 255 | else if (opt & JUST_CONTEXT) {
|
---|
| 256 | selinux_or_die();
|
---|
| 257 | if (username || getcon(&scontext)) {
|
---|
| 258 | bb_error_msg_and_die("can't get process context%s",
|
---|
| 259 | username ? " for a different user" : "");
|
---|
[1765] | 260 | }
|
---|
[2725] | 261 | fputs(scontext, stdout);
|
---|
[821] | 262 | }
|
---|
[2725] | 263 | /* freecon(NULL) seems to be harmless */
|
---|
| 264 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 265 | freecon(scontext);
|
---|
[821] | 266 | #endif
|
---|
[2725] | 267 | bb_putchar('\n');
|
---|
[1765] | 268 | fflush_stdout_and_exit(status);
|
---|
[821] | 269 | }
|
---|