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 | |
| 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."
| -rw-r--r-- | coreutils/id.c | 76 | ||||
| -rw-r--r-- | include/libbb.h | 1 | ||||
| -rw-r--r-- | libbb/Makefile.in | 2 | ||||
| -rw-r--r-- | libbb/my_getgrgid.c | 46 | ||||
| -rw-r--r-- | libbb/my_getpwuid.c | 45 | ||||
| -rw-r--r-- | libbb/my_getug.c | 64 |
6 files changed, 126 insertions, 108 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 */ |
diff --git a/include/libbb.h b/include/libbb.h index 51afd1e9d..93ab5375c 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -230,6 +230,7 @@ extern unsigned long bb_xparse_number(const char *numstr, | |||
| 230 | * increases target size and is often not needed embedded systems. */ | 230 | * increases target size and is often not needed embedded systems. */ |
| 231 | extern long my_getpwnam(const char *name); | 231 | extern long my_getpwnam(const char *name); |
| 232 | extern long my_getgrnam(const char *name); | 232 | extern long my_getgrnam(const char *name); |
| 233 | extern char * my_getug(char *buffer, char *idname, long id, int bufsize, char prefix); | ||
| 233 | extern char * my_getpwuid(char *name, long uid, int bufsize); | 234 | extern char * my_getpwuid(char *name, long uid, int bufsize); |
| 234 | extern char * my_getgrgid(char *group, long gid, int bufsize); | 235 | extern char * my_getgrgid(char *group, long gid, int bufsize); |
| 235 | extern char *bb_askpass(int timeout, const char * prompt); | 236 | extern char *bb_askpass(int timeout, const char * prompt); |
diff --git a/libbb/Makefile.in b/libbb/Makefile.in index 26ed5b132..f86664f15 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in | |||
| @@ -34,7 +34,7 @@ LIBBB_SRC:= \ | |||
| 34 | human_readable.c inet_common.c inode_hash.c interface.c isdirectory.c \ | 34 | human_readable.c inet_common.c inode_hash.c interface.c isdirectory.c \ |
| 35 | kernel_version.c last_char_is.c llist_add_to.c login.c loop.c \ | 35 | kernel_version.c last_char_is.c llist_add_to.c login.c loop.c \ |
| 36 | make_directory.c mode_string.c module_syscalls.c mtab.c mtab_file.c \ | 36 | make_directory.c mode_string.c module_syscalls.c mtab.c mtab_file.c \ |
| 37 | my_getgrgid.c my_getgrnam.c my_getpwnam.c \ | 37 | my_getgrgid.c my_getgrnam.c my_getpwnam.c my_getug.c\ |
| 38 | my_getpwuid.c obscure.c parse_mode.c parse_number.c perror_msg.c \ | 38 | my_getpwuid.c obscure.c parse_mode.c parse_number.c perror_msg.c \ |
| 39 | perror_msg_and_die.c print_file.c get_console.c \ | 39 | perror_msg_and_die.c print_file.c get_console.c \ |
| 40 | process_escape_sequence.c procps.c pwd2spwd.c pw_encrypt.c qmodule.c \ | 40 | process_escape_sequence.c procps.c pwd2spwd.c pw_encrypt.c qmodule.c \ |
diff --git a/libbb/my_getgrgid.c b/libbb/my_getgrgid.c index 8c530964c..9ac14a34e 100644 --- a/libbb/my_getgrgid.c +++ b/libbb/my_getgrgid.c | |||
| @@ -22,48 +22,28 @@ | |||
| 22 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more | 22 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more |
| 23 | * flexible : | 23 | * flexible : |
| 24 | * | 24 | * |
| 25 | * if bufsize is > 0 char *group cannot be set to NULL | 25 | * if bufsize is > 0 char *group cannot be set to NULL. |
| 26 | * on success groupname is written on static allocated buffer | 26 | * On success groupname is written on static allocated buffer group |
| 27 | * on failure gid as string is written to buffer and NULL is returned | 27 | * (and a pointer to it is returned). |
| 28 | * if bufsize is = 0 char *group can be set to NULL | 28 | * On failure gid as string is written to static allocated buffer |
| 29 | * on success groupname is returned | 29 | * group and NULL is returned. |
| 30 | * on failure NULL is returned | 30 | * if bufsize is = 0 char *group can be set to NULL. |
| 31 | * if bufsize is < 0 char *group can be set to NULL | 31 | * On success groupname is returned. |
| 32 | * on success groupname is returned | 32 | * On failure NULL is returned. |
| 33 | * on failure an error message is printed and the program exits | 33 | * if bufsize is < 0 char *group can be set to NULL. |
| 34 | * On success groupname is returned. | ||
| 35 | * On failure an error message is printed and the program exits. | ||
| 34 | */ | 36 | */ |
| 35 | 37 | ||
| 36 | #include <stdio.h> | ||
| 37 | #include <string.h> | ||
| 38 | #include <assert.h> | ||
| 39 | #include "libbb.h" | 38 | #include "libbb.h" |
| 40 | #include "pwd_.h" | ||
| 41 | #include "grp_.h" | 39 | #include "grp_.h" |
| 42 | 40 | ||
| 43 | |||
| 44 | /* gets a groupname given a gid */ | 41 | /* gets a groupname given a gid */ |
| 45 | char * my_getgrgid(char *group, long gid, int bufsize) | 42 | char * my_getgrgid(char *group, long gid, int bufsize) |
| 46 | { | 43 | { |
| 47 | struct group *mygroup; | 44 | struct group *mygroup = getgrgid(gid); |
| 48 | 45 | ||
| 49 | mygroup = getgrgid(gid); | 46 | return my_getug(group, (mygroup) ? mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g'); |
| 50 | if (mygroup==NULL) { | ||
| 51 | if(bufsize > 0) { | ||
| 52 | assert(group != NULL); | ||
| 53 | snprintf(group, bufsize, "%ld", (long)gid); | ||
| 54 | } | ||
| 55 | if( bufsize < 0 ) { | ||
| 56 | bb_error_msg_and_die("unknown gid %ld", (long)gid); | ||
| 57 | } | ||
| 58 | return NULL; | ||
| 59 | } else { | ||
| 60 | if(bufsize > 0) | ||
| 61 | { | ||
| 62 | assert(group != NULL); | ||
| 63 | return safe_strncpy(group, mygroup->gr_name, bufsize); | ||
| 64 | } | ||
| 65 | return mygroup->gr_name; | ||
| 66 | } | ||
| 67 | } | 47 | } |
| 68 | 48 | ||
| 69 | 49 | ||
diff --git a/libbb/my_getpwuid.c b/libbb/my_getpwuid.c index 1e8b11a09..3195a2182 100644 --- a/libbb/my_getpwuid.c +++ b/libbb/my_getpwuid.c | |||
| @@ -22,49 +22,28 @@ | |||
| 22 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more | 22 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more |
| 23 | * flexible : | 23 | * flexible : |
| 24 | * | 24 | * |
| 25 | * if bufsize is > 0 char *user can not be set to NULL | 25 | * if bufsize is > 0 char *user can not be set to NULL. |
| 26 | * on success username is written on static allocated buffer | 26 | * On success username is written on static allocated buffer name |
| 27 | * on failure uid as string is written to buffer and NULL is returned | 27 | * (and a pointer to it is returned). |
| 28 | * if bufsize is = 0 char *user can be set to NULL | 28 | * On failure uid as string is written to static allocated buffer name |
| 29 | * on success username is returned | 29 | * and NULL is returned. |
| 30 | * on failure NULL is returned | 30 | * if bufsize is = 0 char *user can be set to NULL. |
| 31 | * On success username is returned. | ||
| 32 | * On failure NULL is returned. | ||
| 31 | * if bufsize is < 0 char *user can be set to NULL | 33 | * if bufsize is < 0 char *user can be set to NULL |
| 32 | * on success username is returned | 34 | * On success username is returned. |
| 33 | * on failure an error message is printed and the program exits | 35 | * On failure an error message is printed and the program exits. |
| 34 | */ | 36 | */ |
| 35 | 37 | ||
| 36 | #include <stdio.h> | ||
| 37 | #include <string.h> | ||
| 38 | #include <assert.h> | ||
| 39 | #include "libbb.h" | 38 | #include "libbb.h" |
| 40 | #include "pwd_.h" | 39 | #include "pwd_.h" |
| 41 | #include "grp_.h" | ||
| 42 | |||
| 43 | |||
| 44 | 40 | ||
| 45 | /* gets a username given a uid */ | 41 | /* gets a username given a uid */ |
| 46 | char * my_getpwuid(char *name, long uid, int bufsize) | 42 | char * my_getpwuid(char *name, long uid, int bufsize) |
| 47 | { | 43 | { |
| 48 | struct passwd *myuser; | 44 | struct passwd *myuser = getpwuid(uid); |
| 49 | 45 | ||
| 50 | myuser = getpwuid(uid); | 46 | return my_getug(name, (myuser) ? myuser->pw_name : (char *)myuser , uid, bufsize, 'u'); |
| 51 | if (myuser==NULL) { | ||
| 52 | if(bufsize > 0) { | ||
| 53 | assert(name != NULL); | ||
| 54 | snprintf(name, bufsize, "%ld", (long)uid); | ||
| 55 | } | ||
| 56 | if (bufsize < 0 ) { | ||
| 57 | bb_error_msg_and_die("unknown uid %ld", (long)uid); | ||
| 58 | } | ||
| 59 | return NULL; | ||
| 60 | } else { | ||
| 61 | if(bufsize > 0 ) | ||
| 62 | { | ||
| 63 | assert(name != NULL); | ||
| 64 | return safe_strncpy(name, myuser->pw_name, bufsize); | ||
| 65 | } | ||
| 66 | return myuser->pw_name; | ||
| 67 | } | ||
| 68 | } | 47 | } |
| 69 | 48 | ||
| 70 | /* END CODE */ | 49 | /* END CODE */ |
diff --git a/libbb/my_getug.c b/libbb/my_getug.c new file mode 100644 index 000000000..894dc5fdd --- /dev/null +++ b/libbb/my_getug.c | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 2 | /* | ||
| 3 | * Utility routines. | ||
| 4 | * | ||
| 5 | * Copyright (C) 2004 by Tito Ragusa <farmatito@tiscali.it> | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License as published by | ||
| 9 | * the Free Software Foundation; either version 2 of the License, or | ||
| 10 | * (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This program is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License | ||
| 18 | * along with this program; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | /* | ||
| 23 | * | ||
| 24 | * if bufsize is > 0 char *idname can not be set to NULL. | ||
| 25 | * On success idname is written on static allocated buffer | ||
| 26 | * (and a pointer to it is returned). | ||
| 27 | * On failure uid or gid as string is written to static allocated buffer | ||
| 28 | * and NULL is returned. | ||
| 29 | * if bufsize is = 0 char *idname can be set to NULL. | ||
| 30 | * On success idname is returned. | ||
| 31 | * On failure NULL is returned. | ||
| 32 | * if bufsize is < 0 char *idname can be set to NULL. | ||
| 33 | * On success idname is returned. | ||
| 34 | * On failure an error message is printed and the program exits. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #include <stdio.h> | ||
| 38 | #include <assert.h> | ||
| 39 | #include "libbb.h" | ||
| 40 | |||
| 41 | |||
| 42 | /* internal function for my_getpwuid and my_getgrgid */ | ||
| 43 | char * my_getug(char *buffer, char *idname, long id, int bufsize, char prefix) | ||
| 44 | { | ||
| 45 | if(bufsize > 0 ) { | ||
| 46 | assert(buffer!=NULL); | ||
| 47 | if(idname) { | ||
| 48 | return safe_strncpy(buffer, idname, bufsize); | ||
| 49 | } | ||
| 50 | snprintf(buffer, bufsize, "%ld", id); | ||
| 51 | } else if(bufsize < 0 && !idname) { | ||
| 52 | bb_error_msg_and_die("unknown %cid %ld", prefix, id); | ||
| 53 | } | ||
| 54 | return idname; | ||
| 55 | } | ||
| 56 | |||
| 57 | /* END CODE */ | ||
| 58 | /* | ||
| 59 | Local Variables: | ||
| 60 | c-file-style: "linux" | ||
| 61 | c-basic-offset: 4 | ||
| 62 | tab-width: 4 | ||
| 63 | End: | ||
| 64 | */ | ||
