diff options
| author | Glenn L McGrath <bug1@ihug.co.nz> | 2004-09-15 03:04:08 +0000 |
|---|---|---|
| committer | Glenn L McGrath <bug1@ihug.co.nz> | 2004-09-15 03:04:08 +0000 |
| commit | f15dfc557048ca28c8b71ecbcfb9b8f229f2e2e0 (patch) | |
| tree | f34d5e6241ef8f0a1a95502128789b2edd2c1a71 /coreutils | |
| parent | 995d96a99d5f2d546d5e15b2614ae7408da27631 (diff) | |
| download | busybox-w32-f15dfc557048ca28c8b71ecbcfb9b8f229f2e2e0.tar.gz busybox-w32-f15dfc557048ca28c8b71ecbcfb9b8f229f2e2e0.tar.bz2 busybox-w32-f15dfc557048ca28c8b71ecbcfb9b8f229f2e2e0.zip | |
Tito writes,
"This patch fixes all the bugs in id previously spotted by vodz and me.
The binary size increased a bit, but now it should work as expected."
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/id.c | 76 |
1 files changed, 35 insertions, 41 deletions
diff --git a/coreutils/id.c b/coreutils/id.c index 76331e48f..d5182b953 100644 --- a/coreutils/id.c +++ b/coreutils/id.c | |||
| @@ -26,12 +26,9 @@ | |||
| 26 | */ | 26 | */ |
| 27 | 27 | ||
| 28 | #include "busybox.h" | 28 | #include "busybox.h" |
| 29 | #include "grp_.h" | ||
| 30 | #include "pwd_.h" | 29 | #include "pwd_.h" |
| 31 | #include <stdio.h> | 30 | #include <stdio.h> |
| 32 | #include <unistd.h> | 31 | #include <unistd.h> |
| 33 | #include <getopt.h> | ||
| 34 | #include <string.h> | ||
| 35 | #include <sys/types.h> | 32 | #include <sys/types.h> |
| 36 | 33 | ||
| 37 | #ifdef CONFIG_SELINUX | 34 | #ifdef CONFIG_SELINUX |
| @@ -44,21 +41,26 @@ | |||
| 44 | #define JUST_USER 4 | 41 | #define JUST_USER 4 |
| 45 | #define JUST_GROUP 8 | 42 | #define JUST_GROUP 8 |
| 46 | 43 | ||
| 47 | void printf_full(unsigned int id, char *arg, char prefix) | 44 | static short printf_full(unsigned int id, const char *arg, const char prefix) |
| 48 | { | 45 | { |
| 49 | printf("%cid=%u",prefix, id); | 46 | const char *fmt = "%cid=%u"; |
| 50 | if(arg) | 47 | short status=EXIT_FAILURE; |
| 51 | printf("(%s) ", arg); | 48 | |
| 49 | if(arg) { | ||
| 50 | fmt = "%cid=%u(%s)"; | ||
| 51 | status=EXIT_SUCCESS; | ||
| 52 | } | ||
| 53 | bb_printf(fmt, prefix, id, arg); | ||
| 54 | return status; | ||
| 52 | } | 55 | } |
| 53 | 56 | ||
| 54 | extern int id_main(int argc, char **argv) | 57 | extern int id_main(int argc, char **argv) |
| 55 | { | 58 | { |
| 56 | struct passwd *p; | 59 | struct passwd *p; |
| 57 | char *user; | ||
| 58 | char *group; | ||
| 59 | uid_t uid; | 60 | uid_t uid; |
| 60 | gid_t gid; | 61 | gid_t gid; |
| 61 | int flags; | 62 | unsigned long flags; |
| 63 | short status; | ||
| 62 | #ifdef CONFIG_SELINUX | 64 | #ifdef CONFIG_SELINUX |
| 63 | int is_flask_enabled_flag = is_flask_enabled(); | 65 | int is_flask_enabled_flag = is_flask_enabled(); |
| 64 | #endif | 66 | #endif |
| @@ -67,8 +69,9 @@ extern int id_main(int argc, char **argv) | |||
| 67 | flags = bb_getopt_ulflags(argc, argv, "rnug"); | 69 | flags = bb_getopt_ulflags(argc, argv, "rnug"); |
| 68 | 70 | ||
| 69 | if ((flags & 0x80000000UL) | 71 | if ((flags & 0x80000000UL) |
| 70 | /* Don't allow -n -r -nr */ | 72 | /* Don't allow -n -r -nr */ |
| 71 | || (flags <= 3 && flags > 0) | 73 | || (flags <= 3 && flags > 0) |
| 74 | /* Don't allow more than one username */ | ||
| 72 | || (argc > optind + 1)) | 75 | || (argc > optind + 1)) |
| 73 | bb_show_usage(); | 76 | bb_show_usage(); |
| 74 | 77 | ||
| @@ -80,54 +83,45 @@ extern int id_main(int argc, char **argv) | |||
| 80 | gid = getgid(); | 83 | gid = getgid(); |
| 81 | } | 84 | } |
| 82 | 85 | ||
| 83 | if(argv[optind]) | 86 | if(argv[optind]) { |
| 84 | { | ||
| 85 | |||
| 86 | p=getpwnam(argv[optind]); | 87 | p=getpwnam(argv[optind]); |
| 87 | /* this is needed because it exits on failure */ | 88 | /* my_getpwnam is needed because it exits on failure */ |
| 88 | uid = my_getpwnam(argv[optind]); | 89 | uid = my_getpwnam(argv[optind]); |
| 89 | gid = p->pw_gid; | 90 | gid = p->pw_gid; |
| 90 | /* in this case PRINT_REAL is the same */ | 91 | /* in this case PRINT_REAL is the same */ |
| 91 | } | 92 | } |
| 92 | |||
| 93 | user=my_getpwuid(NULL, uid, (flags & JUST_USER) ? -1 : 0); | ||
| 94 | 93 | ||
| 95 | if(flags & JUST_USER) | 94 | if(flags & (JUST_GROUP | JUST_USER)) { |
| 96 | { | 95 | /* JUST_GROUP and JUST_USER are mutually exclusive */ |
| 97 | gid=uid; | 96 | if(flags & NAME_NOT_NUMBER) { |
| 98 | group=user; | 97 | /* my_getpwuid and my_getgrgid exit on failure so puts cannot segfault */ |
| 99 | goto PRINT; | 98 | puts((flags & JUST_USER) ? my_getpwuid(NULL, uid, -1 ) : my_getgrgid(NULL, gid, -1 )); |
| 100 | } | 99 | } else { |
| 101 | 100 | bb_printf("%u\n",(flags & JUST_USER) ? uid : gid); | |
| 102 | group=my_getgrgid(NULL, gid, (flags & JUST_GROUP) ? -1 : 0); | 101 | } |
| 103 | 102 | /* exit */ | |
| 104 | if(flags & JUST_GROUP) | ||
| 105 | { | ||
| 106 | PRINT: | ||
| 107 | if(flags & NAME_NOT_NUMBER) | ||
| 108 | puts(group); | ||
| 109 | else | ||
| 110 | printf ("%u\n", gid); | ||
| 111 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); | 103 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
| 112 | } | 104 | } |
| 113 | 105 | ||
| 114 | /* Print full info like GNU id */ | 106 | /* Print full info like GNU id */ |
| 115 | printf_full(uid, user, 'u'); | 107 | /* my_getpwuid doesn't exit on failure here */ |
| 116 | printf_full(gid, group, 'g'); | 108 | status=printf_full(uid, my_getpwuid(NULL, uid, 0), 'u'); |
| 109 | putchar(' '); | ||
| 110 | /* my_getgrgid doesn't exit on failure here */ | ||
| 111 | status|=printf_full(gid, my_getgrgid(NULL, gid, 0), 'g'); | ||
| 117 | #ifdef CONFIG_SELINUX | 112 | #ifdef CONFIG_SELINUX |
| 118 | if(is_flask_enabled_flag) | 113 | if(is_flask_enabled_flag) { |
| 119 | { | ||
| 120 | security_id_t mysid = getsecsid(); | 114 | security_id_t mysid = getsecsid(); |
| 121 | char context[80]; | 115 | char context[80]; |
| 122 | int len = sizeof(context); | 116 | int len = sizeof(context); |
| 123 | context[0] = '\0'; | 117 | context[0] = '\0'; |
| 124 | if(security_sid_to_context(mysid, context, &len)) | 118 | if(security_sid_to_context(mysid, context, &len)) |
| 125 | strcpy(context, "unknown"); | 119 | strcpy(context, "unknown"); |
| 126 | printf("context=%s", context); | 120 | bb_printf(" context=%s", context); |
| 127 | } | 121 | } |
| 128 | #endif | 122 | #endif |
| 129 | puts(""); | 123 | putchar('\n'); |
| 130 | bb_fflush_stdout_and_exit((user && group) ? EXIT_SUCCESS : EXIT_FAILURE); | 124 | bb_fflush_stdout_and_exit(status); |
| 131 | } | 125 | } |
| 132 | 126 | ||
| 133 | /* END CODE */ | 127 | /* END CODE */ |
