diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-18 00:56:24 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-18 00:56:24 +0000 |
| commit | 2228426512eabc05767b5abac69770e3ae9cd9bb (patch) | |
| tree | 913718f56916075be130a9ea2290ff36664d4803 /coreutils | |
| parent | 28f5619e842d4a4e03abe6f96d7f9fd5a70e223f (diff) | |
| download | busybox-w32-2228426512eabc05767b5abac69770e3ae9cd9bb.tar.gz busybox-w32-2228426512eabc05767b5abac69770e3ae9cd9bb.tar.bz2 busybox-w32-2228426512eabc05767b5abac69770e3ae9cd9bb.zip | |
id: fix "id <user>" case. Requires getgrouplist().
function old new delta
getgrouplist_internal - 200 +200
id_main 462 539 +77
bb_internal_getgrouplist - 67 +67
bb__parsespent 119 117 -2
bb_internal_initgroups 213 58 -155
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 1/2 up/down: 344/-157) Total: 187 bytes
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/id.c | 50 |
1 files changed, 40 insertions, 10 deletions
diff --git a/coreutils/id.c b/coreutils/id.c index 5225f357c..aa27ed394 100644 --- a/coreutils/id.c +++ b/coreutils/id.c | |||
| @@ -38,14 +38,25 @@ static int printf_full(unsigned id, const char *arg, const char *prefix) | |||
| 38 | return status; | 38 | return status; |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 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 | |||
| 41 | int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 49 | int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 42 | int id_main(int argc UNUSED_PARAM, char **argv) | 50 | int id_main(int argc UNUSED_PARAM, char **argv) |
| 43 | { | 51 | { |
| 52 | const char *username; | ||
| 44 | struct passwd *p; | 53 | struct passwd *p; |
| 45 | uid_t uid; | 54 | uid_t uid; |
| 46 | gid_t gid; | 55 | gid_t gid; |
| 56 | #if HAVE_getgrouplist | ||
| 47 | gid_t *groups; | 57 | gid_t *groups; |
| 48 | int n; | 58 | int n; |
| 59 | #endif | ||
| 49 | unsigned flags; | 60 | unsigned flags; |
| 50 | short status; | 61 | short status; |
| 51 | #if ENABLE_SELINUX | 62 | #if ENABLE_SELINUX |
| @@ -55,6 +66,7 @@ int id_main(int argc UNUSED_PARAM, char **argv) | |||
| 55 | /* Don't allow more than one username */ | 66 | /* Don't allow more than one username */ |
| 56 | 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"); | 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"); |
| 57 | flags = getopt32(argv, "rnugG" USE_SELINUX("Z")); | 68 | flags = getopt32(argv, "rnugG" USE_SELINUX("Z")); |
| 69 | username = argv[optind]; | ||
| 58 | 70 | ||
| 59 | /* This values could be overwritten later */ | 71 | /* This values could be overwritten later */ |
| 60 | uid = geteuid(); | 72 | uid = geteuid(); |
| @@ -64,19 +76,34 @@ int id_main(int argc UNUSED_PARAM, char **argv) | |||
| 64 | gid = getgid(); | 76 | gid = getgid(); |
| 65 | } | 77 | } |
| 66 | 78 | ||
| 67 | if (argv[optind]) { | 79 | if (username) { |
| 68 | p = getpwnam(argv[optind]); | 80 | #if HAVE_getgrouplist |
| 81 | int m; | ||
| 82 | #endif | ||
| 83 | p = getpwnam(username); | ||
| 69 | /* xuname2uid is needed because it exits on failure */ | 84 | /* xuname2uid is needed because it exits on failure */ |
| 70 | uid = xuname2uid(argv[optind]); | 85 | uid = xuname2uid(username); |
| 71 | gid = p->pw_gid; | 86 | gid = p->pw_gid; /* in this case PRINT_REAL is the same */ |
| 72 | /* 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 | ||
| 73 | } | 103 | } |
| 74 | 104 | ||
| 75 | n = getgroups(0, NULL); | ||
| 76 | groups = xmalloc(sizeof(groups[0]) * n); | ||
| 77 | getgroups(n, groups); | ||
| 78 | |||
| 79 | if (flags & JUST_ALL_GROUPS) { | 105 | if (flags & JUST_ALL_GROUPS) { |
| 106 | #if HAVE_getgrouplist | ||
| 80 | while (n--) { | 107 | while (n--) { |
| 81 | if (flags & NAME_NOT_NUMBER) | 108 | if (flags & NAME_NOT_NUMBER) |
| 82 | printf("%s", bb_getgrgid(NULL, 0, *groups++)); | 109 | printf("%s", bb_getgrgid(NULL, 0, *groups++)); |
| @@ -84,6 +111,7 @@ int id_main(int argc UNUSED_PARAM, char **argv) | |||
| 84 | printf("%u", (unsigned) *groups++); | 111 | printf("%u", (unsigned) *groups++); |
| 85 | bb_putchar((n > 0) ? ' ' : '\n'); | 112 | bb_putchar((n > 0) ? ' ' : '\n'); |
| 86 | } | 113 | } |
| 114 | #endif | ||
| 87 | /* exit */ | 115 | /* exit */ |
| 88 | fflush_stdout_and_exit(EXIT_SUCCESS); | 116 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 89 | } | 117 | } |
| @@ -105,7 +133,7 @@ int id_main(int argc UNUSED_PARAM, char **argv) | |||
| 105 | #if ENABLE_SELINUX | 133 | #if ENABLE_SELINUX |
| 106 | if (flags & JUST_CONTEXT) { | 134 | if (flags & JUST_CONTEXT) { |
| 107 | selinux_or_die(); | 135 | selinux_or_die(); |
| 108 | if (argv[optind]) { | 136 | if (username) { |
| 109 | bb_error_msg_and_die("user name can't be passed with -Z"); | 137 | bb_error_msg_and_die("user name can't be passed with -Z"); |
| 110 | } | 138 | } |
| 111 | 139 | ||
| @@ -123,6 +151,7 @@ int id_main(int argc UNUSED_PARAM, char **argv) | |||
| 123 | /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */ | 151 | /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */ |
| 124 | status = printf_full(uid, bb_getpwuid(NULL, 0, uid), "uid="); | 152 | status = printf_full(uid, bb_getpwuid(NULL, 0, uid), "uid="); |
| 125 | status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), " gid="); | 153 | status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), " gid="); |
| 154 | #if HAVE_getgrouplist | ||
| 126 | { | 155 | { |
| 127 | const char *msg = " groups="; | 156 | const char *msg = " groups="; |
| 128 | while (n--) { | 157 | while (n--) { |
| @@ -132,6 +161,7 @@ int id_main(int argc UNUSED_PARAM, char **argv) | |||
| 132 | } | 161 | } |
| 133 | } | 162 | } |
| 134 | /* we leak groups vector... */ | 163 | /* we leak groups vector... */ |
| 164 | #endif | ||
| 135 | 165 | ||
| 136 | #if ENABLE_SELINUX | 166 | #if ENABLE_SELINUX |
| 137 | if (is_selinux_enabled()) { | 167 | if (is_selinux_enabled()) { |
