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 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 5ff1265 | 2008-11-16 15:03:11 +0000 | [diff] [blame^] | 20 | #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 Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 26 | enum { |
| 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 Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 32 | #if ENABLE_SELINUX |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 33 | JUST_CONTEXT = (1 << 5), |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 34 | #endif |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 35 | }; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 37 | static int print_common(unsigned id, |
| 38 | char* FAST_FUNC bb_getXXXid(char *name, int bufsize, long uid), |
| 39 | const char *prefix) |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 40 | { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 41 | const char *name = bb_getXXXid(NULL, 0, id); |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 43 | if (prefix) { |
| 44 | printf("%s", prefix); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 45 | } |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 46 | 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 Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 62 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 64 | static int print_group(gid_t id, const char *prefix) |
| 65 | { |
| 66 | return print_common(id, bb_getgrgid, prefix); |
| 67 | } |
| 68 | |
Bernhard Reutner-Fischer | 6559100 | 2008-11-05 08:15:13 +0000 | [diff] [blame] | 69 | static int print_user(uid_t id, const char *prefix) |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 70 | { |
| 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 | */ |
| 79 | static 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 Vlasenko | e7368f1 | 2008-10-29 10:30:54 +0000 | [diff] [blame] | 93 | //commented out here, happens below anyway |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 94 | } 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 Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 105 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 106 | int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 107 | int id_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 109 | 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 Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 117 | const char *username; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 118 | #if ENABLE_SELINUX |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 119 | security_context_t scontext = NULL; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 120 | #endif |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 121 | /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/ |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 122 | /* Don't allow more than one username */ |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 123 | 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 Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 127 | username = argv[optind]; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 128 | if (username) { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 129 | 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 Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 134 | } else { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 135 | egid = getegid(); |
| 136 | rgid = getgid(); |
| 137 | euid = geteuid(); |
| 138 | ruid = getuid(); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 139 | } |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 140 | /* 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-Fischer | a643816 | 2008-11-05 08:18:16 +0000 | [diff] [blame] | 142 | /* in fact in this case egid is always printed if egid != rgid */ |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 143 | if (!opt || (opt & JUST_ALL_GROUPS)) { |
| 144 | gid_t *groups; |
| 145 | int n; |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 147 | 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 McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 155 | } else { |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 156 | /* JUST_ALL_GROUPS */ |
| 157 | status |= print_group(rgid, NULL); |
| 158 | if (egid != rgid) |
| 159 | status |= print_group(egid, " "); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 160 | } |
Denis Vlasenko | e7368f1 | 2008-10-29 10:30:54 +0000 | [diff] [blame] | 161 | /* 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 Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 164 | 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 Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 180 | } 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 Vlasenko | e7368f1 | 2008-10-29 10:30:54 +0000 | [diff] [blame] | 186 | if (ENABLE_FEATURE_CLEAN_UP) |
| 187 | free(groups); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 188 | #if ENABLE_SELINUX |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 189 | if (is_selinux_enabled()) { |
| 190 | if (getcon(&scontext) == 0) |
| 191 | printf(" context=%s", scontext); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 192 | } |
| 193 | #endif |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 194 | } else if (opt & PRINT_REAL) { |
| 195 | euid = ruid; |
| 196 | egid = rgid; |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 197 | } |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 198 | |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 199 | if (opt & JUST_USER) |
| 200 | status |= print_user(euid, NULL); |
| 201 | else if (opt & JUST_GROUP) |
| 202 | status |= print_group(egid, NULL); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 203 | #if ENABLE_SELINUX |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 204 | 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 Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 211 | } |
Denis Vlasenko | 34e68c8 | 2008-10-29 00:27:31 +0000 | [diff] [blame] | 212 | /* freecon(NULL) seems to be harmless */ |
| 213 | if (ENABLE_FEATURE_CLEAN_UP) |
| 214 | freecon(scontext); |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 215 | #endif |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 216 | bb_putchar('\n'); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 217 | fflush_stdout_and_exit(status); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 218 | } |