diff options
author | Glenn L McGrath | 2004-09-15 03:04:08 +0000 |
---|---|---|
committer | Glenn L McGrath | 2004-09-15 03:04:08 +0000 |
commit | f15dfc557048ca28c8b71ecbcfb9b8f229f2e2e0 (patch) | |
tree | f34d5e6241ef8f0a1a95502128789b2edd2c1a71 | |
parent | 995d96a99d5f2d546d5e15b2614ae7408da27631 (diff) | |
download | busybox-f15dfc557048ca28c8b71ecbcfb9b8f229f2e2e0.zip busybox-f15dfc557048ca28c8b71ecbcfb9b8f229f2e2e0.tar.gz |
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 76331e4..d5182b9 100644 --- a/coreutils/id.c +++ b/coreutils/id.c @@ -26,12 +26,9 @@ */ #include "busybox.h" -#include "grp_.h" #include "pwd_.h" #include <stdio.h> #include <unistd.h> -#include <getopt.h> -#include <string.h> #include <sys/types.h> #ifdef CONFIG_SELINUX @@ -44,21 +41,26 @@ #define JUST_USER 4 #define JUST_GROUP 8 -void printf_full(unsigned int id, char *arg, char prefix) +static short printf_full(unsigned int id, const char *arg, const char prefix) { - printf("%cid=%u",prefix, id); - if(arg) - printf("(%s) ", arg); + const char *fmt = "%cid=%u"; + short status=EXIT_FAILURE; + + if(arg) { + fmt = "%cid=%u(%s)"; + status=EXIT_SUCCESS; + } + bb_printf(fmt, prefix, id, arg); + return status; } extern int id_main(int argc, char **argv) { struct passwd *p; - char *user; - char *group; uid_t uid; gid_t gid; - int flags; + unsigned long flags; + short status; #ifdef CONFIG_SELINUX int is_flask_enabled_flag = is_flask_enabled(); #endif @@ -67,8 +69,9 @@ extern int id_main(int argc, char **argv) flags = bb_getopt_ulflags(argc, argv, "rnug"); if ((flags & 0x80000000UL) - /* Don't allow -n -r -nr */ + /* Don't allow -n -r -nr */ || (flags <= 3 && flags > 0) + /* Don't allow more than one username */ || (argc > optind + 1)) bb_show_usage(); @@ -80,54 +83,45 @@ extern int id_main(int argc, char **argv) gid = getgid(); } - if(argv[optind]) - { - + if(argv[optind]) { p=getpwnam(argv[optind]); - /* this is needed because it exits on failure */ + /* my_getpwnam is needed because it exits on failure */ uid = my_getpwnam(argv[optind]); gid = p->pw_gid; /* in this case PRINT_REAL is the same */ } - - user=my_getpwuid(NULL, uid, (flags & JUST_USER) ? -1 : 0); - if(flags & JUST_USER) - { - gid=uid; - group=user; - goto PRINT; - } - - group=my_getgrgid(NULL, gid, (flags & JUST_GROUP) ? -1 : 0); - - if(flags & JUST_GROUP) - { -PRINT: - if(flags & NAME_NOT_NUMBER) - puts(group); - else - printf ("%u\n", gid); + if(flags & (JUST_GROUP | JUST_USER)) { + /* JUST_GROUP and JUST_USER are mutually exclusive */ + if(flags & NAME_NOT_NUMBER) { + /* my_getpwuid and my_getgrgid exit on failure so puts cannot segfault */ + puts((flags & JUST_USER) ? my_getpwuid(NULL, uid, -1 ) : my_getgrgid(NULL, gid, -1 )); + } else { + bb_printf("%u\n",(flags & JUST_USER) ? uid : gid); + } + /* exit */ bb_fflush_stdout_and_exit(EXIT_SUCCESS); } - + /* Print full info like GNU id */ - printf_full(uid, user, 'u'); - printf_full(gid, group, 'g'); + /* my_getpwuid doesn't exit on failure here */ + status=printf_full(uid, my_getpwuid(NULL, uid, 0), 'u'); + putchar(' '); + /* my_getgrgid doesn't exit on failure here */ + status|=printf_full(gid, my_getgrgid(NULL, gid, 0), 'g'); #ifdef CONFIG_SELINUX - if(is_flask_enabled_flag) - { + if(is_flask_enabled_flag) { security_id_t mysid = getsecsid(); char context[80]; int len = sizeof(context); context[0] = '\0'; if(security_sid_to_context(mysid, context, &len)) strcpy(context, "unknown"); - printf("context=%s", context); + bb_printf(" context=%s", context); } #endif - puts(""); - bb_fflush_stdout_and_exit((user && group) ? EXIT_SUCCESS : EXIT_FAILURE); + putchar('\n'); + bb_fflush_stdout_and_exit(status); } /* END CODE */ diff --git a/include/libbb.h b/include/libbb.h index 51afd1e..93ab537 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -230,6 +230,7 @@ extern unsigned long bb_xparse_number(const char *numstr, * increases target size and is often not needed embedded systems. */ extern long my_getpwnam(const char *name); extern long my_getgrnam(const char *name); +extern char * my_getug(char *buffer, char *idname, long id, int bufsize, char prefix); extern char * my_getpwuid(char *name, long uid, int bufsize); extern char * my_getgrgid(char *group, long gid, int bufsize); extern char *bb_askpass(int timeout, const char * prompt); diff --git a/libbb/Makefile.in b/libbb/Makefile.in index 26ed5b1..f86664f 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in @@ -34,7 +34,7 @@ LIBBB_SRC:= \ human_readable.c inet_common.c inode_hash.c interface.c isdirectory.c \ kernel_version.c last_char_is.c llist_add_to.c login.c loop.c \ make_directory.c mode_string.c module_syscalls.c mtab.c mtab_file.c \ - my_getgrgid.c my_getgrnam.c my_getpwnam.c \ + my_getgrgid.c my_getgrnam.c my_getpwnam.c my_getug.c\ my_getpwuid.c obscure.c parse_mode.c parse_number.c perror_msg.c \ perror_msg_and_die.c print_file.c get_console.c \ 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 8c53096..9ac14a3 100644 --- a/libbb/my_getgrgid.c +++ b/libbb/my_getgrgid.c @@ -22,48 +22,28 @@ /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more * flexible : * - * if bufsize is > 0 char *group cannot be set to NULL - * on success groupname is written on static allocated buffer - * on failure gid as string is written to buffer and NULL is returned - * if bufsize is = 0 char *group can be set to NULL - * on success groupname is returned - * on failure NULL is returned - * if bufsize is < 0 char *group can be set to NULL - * on success groupname is returned - * on failure an error message is printed and the program exits + * if bufsize is > 0 char *group cannot be set to NULL. + * On success groupname is written on static allocated buffer group + * (and a pointer to it is returned). + * On failure gid as string is written to static allocated buffer + * group and NULL is returned. + * if bufsize is = 0 char *group can be set to NULL. + * On success groupname is returned. + * On failure NULL is returned. + * if bufsize is < 0 char *group can be set to NULL. + * On success groupname is returned. + * On failure an error message is printed and the program exits. */ -#include <stdio.h> -#include <string.h> -#include <assert.h> #include "libbb.h" -#include "pwd_.h" #include "grp_.h" - /* gets a groupname given a gid */ char * my_getgrgid(char *group, long gid, int bufsize) { - struct group *mygroup; + struct group *mygroup = getgrgid(gid); - mygroup = getgrgid(gid); - if (mygroup==NULL) { - if(bufsize > 0) { - assert(group != NULL); - snprintf(group, bufsize, "%ld", (long)gid); - } - if( bufsize < 0 ) { - bb_error_msg_and_die("unknown gid %ld", (long)gid); - } - return NULL; - } else { - if(bufsize > 0) - { - assert(group != NULL); - return safe_strncpy(group, mygroup->gr_name, bufsize); - } - return mygroup->gr_name; - } + return my_getug(group, (mygroup) ? mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g'); } diff --git a/libbb/my_getpwuid.c b/libbb/my_getpwuid.c index 1e8b11a..3195a21 100644 --- a/libbb/my_getpwuid.c +++ b/libbb/my_getpwuid.c @@ -22,49 +22,28 @@ /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more * flexible : * - * if bufsize is > 0 char *user can not be set to NULL - * on success username is written on static allocated buffer - * on failure uid as string is written to buffer and NULL is returned - * if bufsize is = 0 char *user can be set to NULL - * on success username is returned - * on failure NULL is returned + * if bufsize is > 0 char *user can not be set to NULL. + * On success username is written on static allocated buffer name + * (and a pointer to it is returned). + * On failure uid as string is written to static allocated buffer name + * and NULL is returned. + * if bufsize is = 0 char *user can be set to NULL. + * On success username is returned. + * On failure NULL is returned. * if bufsize is < 0 char *user can be set to NULL - * on success username is returned - * on failure an error message is printed and the program exits + * On success username is returned. + * On failure an error message is printed and the program exits. */ -#include <stdio.h> -#include <string.h> -#include <assert.h> #include "libbb.h" #include "pwd_.h" -#include "grp_.h" - - /* gets a username given a uid */ char * my_getpwuid(char *name, long uid, int bufsize) { - struct passwd *myuser; + struct passwd *myuser = getpwuid(uid); - myuser = getpwuid(uid); - if (myuser==NULL) { - if(bufsize > 0) { - assert(name != NULL); - snprintf(name, bufsize, "%ld", (long)uid); - } - if (bufsize < 0 ) { - bb_error_msg_and_die("unknown uid %ld", (long)uid); - } - return NULL; - } else { - if(bufsize > 0 ) - { - assert(name != NULL); - return safe_strncpy(name, myuser->pw_name, bufsize); - } - return myuser->pw_name; - } + return my_getug(name, (myuser) ? myuser->pw_name : (char *)myuser , uid, bufsize, 'u'); } /* END CODE */ diff --git a/libbb/my_getug.c b/libbb/my_getug.c new file mode 100644 index 0000000..894dc5f --- /dev/null +++ b/libbb/my_getug.c @@ -0,0 +1,64 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 2004 by Tito Ragusa <farmatito@tiscali.it> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + /* + * + * if bufsize is > 0 char *idname can not be set to NULL. + * On success idname is written on static allocated buffer + * (and a pointer to it is returned). + * On failure uid or gid as string is written to static allocated buffer + * and NULL is returned. + * if bufsize is = 0 char *idname can be set to NULL. + * On success idname is returned. + * On failure NULL is returned. + * if bufsize is < 0 char *idname can be set to NULL. + * On success idname is returned. + * On failure an error message is printed and the program exits. + */ + +#include <stdio.h> +#include <assert.h> +#include "libbb.h" + + +/* internal function for my_getpwuid and my_getgrgid */ +char * my_getug(char *buffer, char *idname, long id, int bufsize, char prefix) +{ + if(bufsize > 0 ) { + assert(buffer!=NULL); + if(idname) { + return safe_strncpy(buffer, idname, bufsize); + } + snprintf(buffer, bufsize, "%ld", id); + } else if(bufsize < 0 && !idname) { + bb_error_msg_and_die("unknown %cid %ld", prefix, id); + } + return idname; +} + +/* END CODE */ +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ |