aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2004-09-02 22:21:41 +0000
committerEric Andersen <andersen@codepoet.org>2004-09-02 22:21:41 +0000
commit7eb79fff10915afc4d561a65e54851efa869db89 (patch)
treededb5229f5b92441ba10aa00667463ef100ccd6f
parentb225e2a76bcd2b1f3f919a09dba1e186c0d4fa65 (diff)
downloadbusybox-w32-7eb79fff10915afc4d561a65e54851efa869db89.tar.gz
busybox-w32-7eb79fff10915afc4d561a65e54851efa869db89.tar.bz2
busybox-w32-7eb79fff10915afc4d561a65e54851efa869db89.zip
Tito writes:
Hi Erik, Hi to all, This is part five of the my_get*id story. I've tweaked a bit this two functions to make them more flexible, but this changes will not affect existing code. Now they work so: 1) my_getpwuid( char *user, uid_t uid, int bufsize) if bufsize is > 0 char *user cannot 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 be set to NULL on success username is returned on failure an error message is printed and the program exits 2) 1) my_getgrgid( char *group, uid_t uid, int bufsize) 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 This changes were needed mainly for my new id applet. It is somewhat bigger then the previous but matches the behaviour of GNU id and is capable to handle usernames of whatever length. BTW: at a first look it seems to me that it will integrate well (with just a few changes) with the pending patch in patches/id_groups_alias.patch. The increase in size is balanced by the removal of my_getpwnamegid.c from libbb as this was used only in previous id applet and by size optimizations made possible in whoami.c and in passwd.c. I know that we are in feature freeze but I think that i've tested it enough (at least I hope so.......).
-rw-r--r--coreutils/id.c143
-rw-r--r--coreutils/whoami.c14
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/Makefile.in2
-rw-r--r--libbb/my_getgrgid.c30
-rw-r--r--libbb/my_getpwuid.c30
-rw-r--r--loginutils/passwd.c9
7 files changed, 149 insertions, 80 deletions
diff --git a/coreutils/id.c b/coreutils/id.c
index db8afc585..76331e48f 100644
--- a/coreutils/id.c
+++ b/coreutils/id.c
@@ -21,90 +21,121 @@
21 */ 21 */
22 22
23/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */ 23/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
24/* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
25 * be more similar to GNU id.
26 */
24 27
25#include "busybox.h" 28#include "busybox.h"
29#include "grp_.h"
30#include "pwd_.h"
26#include <stdio.h> 31#include <stdio.h>
27#include <unistd.h> 32#include <unistd.h>
28#include <getopt.h> 33#include <getopt.h>
29#include <string.h> 34#include <string.h>
30#include <sys/types.h> 35#include <sys/types.h>
36
31#ifdef CONFIG_SELINUX 37#ifdef CONFIG_SELINUX
32#include <proc_secure.h> 38#include <proc_secure.h>
33#include <flask_util.h> 39#include <flask_util.h>
34#endif 40#endif
35 41
36#define JUST_USER 1 42#define PRINT_REAL 1
37#define JUST_GROUP 2 43#define NAME_NOT_NUMBER 2
38#define PRINT_REAL 4 44#define JUST_USER 4
39#define NAME_NOT_NUMBER 8 45#define JUST_GROUP 8
46
47void printf_full(unsigned int id, char *arg, char prefix)
48{
49 printf("%cid=%u",prefix, id);
50 if(arg)
51 printf("(%s) ", arg);
52}
40 53
41extern int id_main(int argc, char **argv) 54extern int id_main(int argc, char **argv)
42{ 55{
43 char user[32], group[32]; 56 struct passwd *p;
44 long pwnam, grnam; 57 char *user;
45 int uid, gid; 58 char *group;
59 uid_t uid;
60 gid_t gid;
46 int flags; 61 int flags;
47#ifdef CONFIG_SELINUX 62#ifdef CONFIG_SELINUX
48 int is_flask_enabled_flag = is_flask_enabled(); 63 int is_flask_enabled_flag = is_flask_enabled();
49#endif 64#endif
50 65
51 flags = bb_getopt_ulflags(argc, argv, "ugrn"); 66 bb_opt_complementaly = "u~g:g~u";
67 flags = bb_getopt_ulflags(argc, argv, "rnug");
52 68
53 if (((flags & (JUST_USER | JUST_GROUP)) == (JUST_USER | JUST_GROUP)) 69 if ((flags & 0x80000000UL)
54 || (argc > optind + 1) 70 /* Don't allow -n -r -nr */
55 ) { 71 || (flags <= 3 && flags > 0)
72 || (argc > optind + 1))
56 bb_show_usage(); 73 bb_show_usage();
74
75 /* This values could be overwritten later */
76 uid = geteuid();
77 gid = getegid();
78 if (flags & PRINT_REAL) {
79 uid = getuid();
80 gid = getgid();
57 } 81 }
82
83 if(argv[optind])
84 {
58 85
59 if (argv[optind] == NULL) { 86 p=getpwnam(argv[optind]);
60 if (flags & PRINT_REAL) { 87 /* this is needed because it exits on failure */
61 uid = getuid(); 88 uid = my_getpwnam(argv[optind]);
62 gid = getgid(); 89 gid = p->pw_gid;
63 } else { 90 /* in this case PRINT_REAL is the same */
64 uid = geteuid();
65 gid = getegid();
66 }
67 my_getpwuid(user, uid, sizeof(user));
68 } else {
69 safe_strncpy(user, argv[optind], sizeof(user));
70 gid = my_getpwnamegid(user);
71 } 91 }
72 my_getgrgid(group, gid, sizeof(group)); 92
93 user=my_getpwuid(NULL, uid, (flags & JUST_USER) ? -1 : 0);
73 94
74 pwnam=my_getpwnam(user); 95 if(flags & JUST_USER)
75 grnam=my_getgrnam(group); 96 {
97 gid=uid;
98 group=user;
99 goto PRINT;
100 }
101
102 group=my_getgrgid(NULL, gid, (flags & JUST_GROUP) ? -1 : 0);
76 103
77 if (flags & (JUST_GROUP | JUST_USER)) { 104 if(flags & JUST_GROUP)
78 char *s = group; 105 {
79 if (flags & JUST_USER) { 106PRINT:
80 s = user; 107 if(flags & NAME_NOT_NUMBER)
81 grnam = pwnam; 108 puts(group);
82 }
83 if (flags & NAME_NOT_NUMBER) {
84 puts(s);
85 } else {
86 printf("%ld\n", grnam);
87 }
88 } else {
89#ifdef CONFIG_SELINUX
90 printf("uid=%ld(%s) gid=%ld(%s)", pwnam, user, grnam, group);
91 if(is_flask_enabled_flag)
92 {
93 security_id_t mysid = getsecsid();
94 char context[80];
95 int len = sizeof(context);
96 context[0] = '\0';
97 if(security_sid_to_context(mysid, context, &len))
98 strcpy(context, "unknown");
99 printf(" context=%s\n", context);
100 }
101 else 109 else
102 printf("\n"); 110 printf ("%u\n", gid);
103#else 111 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
104 printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group); 112 }
113
114 /* Print full info like GNU id */
115 printf_full(uid, user, 'u');
116 printf_full(gid, group, 'g');
117#ifdef CONFIG_SELINUX
118 if(is_flask_enabled_flag)
119 {
120 security_id_t mysid = getsecsid();
121 char context[80];
122 int len = sizeof(context);
123 context[0] = '\0';
124 if(security_sid_to_context(mysid, context, &len))
125 strcpy(context, "unknown");
126 printf("context=%s", context);
127 }
105#endif 128#endif
129 puts("");
130 bb_fflush_stdout_and_exit((user && group) ? EXIT_SUCCESS : EXIT_FAILURE);
131}
106 132
107 } 133/* END CODE */
134/*
135Local Variables:
136c-file-style: "linux"
137c-basic-offset: 4
138tab-width: 4
139End:
140*/
108 141
109 bb_fflush_stdout_and_exit(0);
110}
diff --git a/coreutils/whoami.c b/coreutils/whoami.c
index c979b0dd9..6a6e2eec9 100644
--- a/coreutils/whoami.c
+++ b/coreutils/whoami.c
@@ -26,21 +26,13 @@
26#include <stdlib.h> 26#include <stdlib.h>
27#include <unistd.h> 27#include <unistd.h>
28#include "busybox.h" 28#include "busybox.h"
29#include "pwd_.h"
30#include "grp_.h"
31 29
32extern int whoami_main(int argc, char **argv) 30extern int whoami_main(int argc, char **argv)
33{ 31{
34 struct passwd *p;
35 uid_t uid;
36
37 if (argc > 1) 32 if (argc > 1)
38 bb_show_usage(); 33 bb_show_usage();
39 34
40 uid = geteuid(); 35 puts(my_getpwuid(NULL, geteuid(), -1));
41 if((p = getpwuid(uid))!=NULL) { 36 /* exits on error */
42 puts(p->pw_name); 37 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
43 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
44 }
45 bb_error_msg_and_die("cannot find username for UID %u", (unsigned) uid);
46} 38}
diff --git a/include/libbb.h b/include/libbb.h
index 78b9711e8..51afd1e9d 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -232,7 +232,6 @@ extern long my_getpwnam(const char *name);
232extern long my_getgrnam(const char *name); 232extern long my_getgrnam(const char *name);
233extern char * my_getpwuid(char *name, long uid, int bufsize); 233extern char * my_getpwuid(char *name, long uid, int bufsize);
234extern char * my_getgrgid(char *group, long gid, int bufsize); 234extern char * my_getgrgid(char *group, long gid, int bufsize);
235extern long my_getpwnamegid(const char *name);
236extern char *bb_askpass(int timeout, const char * prompt); 235extern char *bb_askpass(int timeout, const char * prompt);
237 236
238extern int device_open(const char *device, int mode); 237extern int device_open(const char *device, int mode);
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index f993b21ea..26ed5b132 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 my_getpwnamegid.c \ 37 my_getgrgid.c my_getgrnam.c my_getpwnam.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 e6b877687..8c530964c 100644
--- a/libbb/my_getgrgid.c
+++ b/libbb/my_getgrgid.c
@@ -19,8 +19,23 @@
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 20 */
21 21
22 /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
23 * flexible :
24 *
25 * if bufsize is > 0 char *group cannot be set to NULL
26 * on success groupname is written on static allocated buffer
27 * on failure gid as string is written to buffer and NULL is returned
28 * if bufsize is = 0 char *group can be set to NULL
29 * on success groupname is returned
30 * on failure NULL is returned
31 * if bufsize is < 0 char *group can be set to NULL
32 * on success groupname is returned
33 * on failure an error message is printed and the program exits
34 */
35
22#include <stdio.h> 36#include <stdio.h>
23#include <string.h> 37#include <string.h>
38#include <assert.h>
24#include "libbb.h" 39#include "libbb.h"
25#include "pwd_.h" 40#include "pwd_.h"
26#include "grp_.h" 41#include "grp_.h"
@@ -33,10 +48,21 @@ char * my_getgrgid(char *group, long gid, int bufsize)
33 48
34 mygroup = getgrgid(gid); 49 mygroup = getgrgid(gid);
35 if (mygroup==NULL) { 50 if (mygroup==NULL) {
36 snprintf(group, bufsize, "%ld", gid); 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 }
37 return NULL; 58 return NULL;
38 } else { 59 } else {
39 return safe_strncpy(group, mygroup->gr_name, bufsize); 60 if(bufsize > 0)
61 {
62 assert(group != NULL);
63 return safe_strncpy(group, mygroup->gr_name, bufsize);
64 }
65 return mygroup->gr_name;
40 } 66 }
41} 67}
42 68
diff --git a/libbb/my_getpwuid.c b/libbb/my_getpwuid.c
index 53f6c77ee..1e8b11a09 100644
--- a/libbb/my_getpwuid.c
+++ b/libbb/my_getpwuid.c
@@ -19,8 +19,23 @@
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 20 */
21 21
22 /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
23 * flexible :
24 *
25 * if bufsize is > 0 char *user can not be set to NULL
26 * on success username is written on static allocated buffer
27 * on failure uid as string is written to buffer and NULL is returned
28 * if bufsize is = 0 char *user can be set to NULL
29 * on success username is returned
30 * on failure NULL is returned
31 * if bufsize is < 0 char *user can be set to NULL
32 * on success username is returned
33 * on failure an error message is printed and the program exits
34 */
35
22#include <stdio.h> 36#include <stdio.h>
23#include <string.h> 37#include <string.h>
38#include <assert.h>
24#include "libbb.h" 39#include "libbb.h"
25#include "pwd_.h" 40#include "pwd_.h"
26#include "grp_.h" 41#include "grp_.h"
@@ -34,10 +49,21 @@ char * my_getpwuid(char *name, long uid, int bufsize)
34 49
35 myuser = getpwuid(uid); 50 myuser = getpwuid(uid);
36 if (myuser==NULL) { 51 if (myuser==NULL) {
37 snprintf(name, bufsize, "%ld", (long)uid); 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 }
38 return NULL; 59 return NULL;
39 } else { 60 } else {
40 return safe_strncpy(name, myuser->pw_name, bufsize); 61 if(bufsize > 0 )
62 {
63 assert(name != NULL);
64 return safe_strncpy(name, myuser->pw_name, bufsize);
65 }
66 return myuser->pw_name;
41 } 67 }
42} 68}
43 69
diff --git a/loginutils/passwd.c b/loginutils/passwd.c
index d0b2afc19..400ddb9a5 100644
--- a/loginutils/passwd.c
+++ b/loginutils/passwd.c
@@ -145,7 +145,6 @@ extern int passwd_main(int argc, char **argv)
145 int uflg = 0; /* -u - unlock account */ 145 int uflg = 0; /* -u - unlock account */
146 int dflg = 0; /* -d - delete password */ 146 int dflg = 0; /* -d - delete password */
147 const struct passwd *pw; 147 const struct passwd *pw;
148 unsigned short ruid;
149 148
150#ifdef CONFIG_FEATURE_SHADOWPASSWDS 149#ifdef CONFIG_FEATURE_SHADOWPASSWDS
151 const struct spwd *sp; 150 const struct spwd *sp;
@@ -170,12 +169,8 @@ extern int passwd_main(int argc, char **argv)
170 bb_show_usage(); 169 bb_show_usage();
171 } 170 }
172 } 171 }
173 ruid = getuid(); 172 myname = (char *) bb_xstrdup(my_getpwuid(NULL, getuid(), -1));
174 pw = (struct passwd *) getpwuid(ruid); 173 /* exits on error */
175 if (!pw) {
176 bb_error_msg_and_die("Cannot determine your user name.");
177 }
178 myname = (char *) bb_xstrdup(pw->pw_name);
179 if (optind < argc) { 174 if (optind < argc) {
180 name = argv[optind]; 175 name = argv[optind];
181 } else { 176 } else {