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> |
| 6 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 7 | * 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] | 8 | */ |
| 9 | |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 compliant. */ |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 11 | /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 12 | * be more similar to GNU id. |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 13 | * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp> |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 14 | * Added -G option Tito Ragusa (C) 2008 for SUSv3. |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 15 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 18 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 19 | #define PRINT_REAL 1 |
| 20 | #define NAME_NOT_NUMBER 2 |
| 21 | #define JUST_USER 4 |
| 22 | #define JUST_GROUP 8 |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 23 | #define JUST_ALL_GROUPS 16 |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 24 | #if ENABLE_SELINUX |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 25 | #define JUST_CONTEXT 32 |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 26 | #endif |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 0d602e9 | 2008-09-14 19:31:36 +0000 | [diff] [blame] | 28 | static int printf_full(unsigned id, const char *arg, const char *prefix) |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 29 | { |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 30 | const char *fmt = "%s%u"; |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 31 | int status = EXIT_FAILURE; |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 33 | if (arg) { |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 34 | fmt = "%s%u(%s)"; |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 35 | status = EXIT_SUCCESS; |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 36 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 37 | printf(fmt, prefix, id, arg); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 38 | return status; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 39 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 41 | #if (defined(__GLIBC__) && !defined(__UCLIBC__)) |
| 42 | #define HAVE_getgrouplist 1 |
| 43 | #elif ENABLE_USE_BB_PWD_GRP |
| 44 | #define HAVE_getgrouplist 1 |
| 45 | #else |
| 46 | #define HAVE_getgrouplist 0 |
| 47 | #endif |
| 48 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 49 | int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 50 | int id_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 51 | { |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 52 | const char *username; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 53 | struct passwd *p; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 54 | uid_t uid; |
| 55 | gid_t gid; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 56 | #if HAVE_getgrouplist |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 57 | gid_t *groups; |
Denis Vlasenko | 4f3209b | 2008-09-13 12:51:10 +0000 | [diff] [blame] | 58 | int n; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 59 | #endif |
Denis Vlasenko | 0d602e9 | 2008-09-14 19:31:36 +0000 | [diff] [blame] | 60 | unsigned flags; |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 61 | short status; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 62 | #if ENABLE_SELINUX |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 63 | security_context_t scontext; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 64 | #endif |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 65 | /* Don't allow -n -r -nr -ug -rug -nug -rnug */ |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 66 | /* Don't allow more than one username */ |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 67 | opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g"); |
| 68 | flags = getopt32(argv, "rnugG" USE_SELINUX("Z")); |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 69 | username = argv[optind]; |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 70 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 71 | /* This values could be overwritten later */ |
| 72 | uid = geteuid(); |
| 73 | gid = getegid(); |
| 74 | if (flags & PRINT_REAL) { |
| 75 | uid = getuid(); |
| 76 | gid = getgid(); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 77 | } |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 79 | if (username) { |
| 80 | #if HAVE_getgrouplist |
| 81 | int m; |
| 82 | #endif |
| 83 | p = getpwnam(username); |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 84 | /* xuname2uid is needed because it exits on failure */ |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 85 | uid = xuname2uid(username); |
| 86 | gid = p->pw_gid; /* in this case PRINT_REAL is the same */ |
| 87 | |
| 88 | #if HAVE_getgrouplist |
| 89 | n = 16; |
| 90 | groups = NULL; |
| 91 | do { |
| 92 | m = n; |
| 93 | groups = xrealloc(groups, sizeof(groups[0]) * m); |
| 94 | getgrouplist(username, gid, groups, &n); /* GNUism? */ |
| 95 | } while (n > m); |
| 96 | #endif |
| 97 | } else { |
| 98 | #if HAVE_getgrouplist |
| 99 | n = getgroups(0, NULL); |
| 100 | groups = xmalloc(sizeof(groups[0]) * n); |
| 101 | getgroups(n, groups); |
| 102 | #endif |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Denis Vlasenko | 4f3209b | 2008-09-13 12:51:10 +0000 | [diff] [blame] | 105 | if (flags & JUST_ALL_GROUPS) { |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 106 | #if HAVE_getgrouplist |
Denis Vlasenko | 4f3209b | 2008-09-13 12:51:10 +0000 | [diff] [blame] | 107 | while (n--) { |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 108 | if (flags & NAME_NOT_NUMBER) |
| 109 | printf("%s", bb_getgrgid(NULL, 0, *groups++)); |
| 110 | else |
Denis Vlasenko | 0d602e9 | 2008-09-14 19:31:36 +0000 | [diff] [blame] | 111 | printf("%u", (unsigned) *groups++); |
Denis Vlasenko | 4f3209b | 2008-09-13 12:51:10 +0000 | [diff] [blame] | 112 | bb_putchar((n > 0) ? ' ' : '\n'); |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 113 | } |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 114 | #endif |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 115 | /* exit */ |
| 116 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 117 | } |
| 118 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 119 | if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) { |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 120 | /* JUST_GROUP and JUST_USER are mutually exclusive */ |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 121 | if (flags & NAME_NOT_NUMBER) { |
Denis Vlasenko | 0d602e9 | 2008-09-14 19:31:36 +0000 | [diff] [blame] | 122 | /* bb_getXXXid(-1) exits on failure, puts cannot segfault */ |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 123 | puts((flags & JUST_USER) ? bb_getpwuid(NULL, -1, uid) : bb_getgrgid(NULL, -1, gid)); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 124 | } else { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 125 | if (flags & JUST_USER) { |
Denis Vlasenko | 0d602e9 | 2008-09-14 19:31:36 +0000 | [diff] [blame] | 126 | printf("%u\n", (unsigned)uid); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 127 | } |
| 128 | if (flags & JUST_GROUP) { |
Denis Vlasenko | 0d602e9 | 2008-09-14 19:31:36 +0000 | [diff] [blame] | 129 | printf("%u\n", (unsigned)gid); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 133 | #if ENABLE_SELINUX |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 134 | if (flags & JUST_CONTEXT) { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 135 | selinux_or_die(); |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 136 | if (username) { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 137 | bb_error_msg_and_die("user name can't be passed with -Z"); |
| 138 | } |
| 139 | |
| 140 | if (getcon(&scontext)) { |
| 141 | bb_error_msg_and_die("can't get process context"); |
| 142 | } |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 143 | puts(scontext); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 144 | } |
| 145 | #endif |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 146 | /* exit */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 147 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 148 | } |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 149 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 150 | /* Print full info like GNU id */ |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 151 | /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */ |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 152 | status = printf_full(uid, bb_getpwuid(NULL, 0, uid), "uid="); |
Denis Vlasenko | 4f3209b | 2008-09-13 12:51:10 +0000 | [diff] [blame] | 153 | status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), " gid="); |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 154 | #if HAVE_getgrouplist |
Denis Vlasenko | 4f3209b | 2008-09-13 12:51:10 +0000 | [diff] [blame] | 155 | { |
| 156 | const char *msg = " groups="; |
| 157 | while (n--) { |
| 158 | status |= printf_full(*groups, bb_getgrgid(NULL, 0, *groups), msg); |
| 159 | msg = ","; |
| 160 | groups++; |
| 161 | } |
Bernhard Reutner-Fischer | 0ee1cb0 | 2008-09-12 09:58:11 +0000 | [diff] [blame] | 162 | } |
Denis Vlasenko | 4f3209b | 2008-09-13 12:51:10 +0000 | [diff] [blame] | 163 | /* we leak groups vector... */ |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 164 | #endif |
Denis Vlasenko | 4f3209b | 2008-09-13 12:51:10 +0000 | [diff] [blame] | 165 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 166 | #if ENABLE_SELINUX |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 167 | if (is_selinux_enabled()) { |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 168 | security_context_t mysid; |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 169 | getcon(&mysid); |
Denis Vlasenko | 0d602e9 | 2008-09-14 19:31:36 +0000 | [diff] [blame] | 170 | printf(" context=%s", mysid ? mysid : "unknown"); |
| 171 | if (mysid) /* TODO: maybe freecon(NULL) is harmless? */ |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 172 | freecon(mysid); |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 173 | } |
| 174 | #endif |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 176 | bb_putchar('\n'); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 177 | fflush_stdout_and_exit(status); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 178 | } |