aboutsummaryrefslogtreecommitdiff
path: root/coreutils/id.c
diff options
context:
space:
mode:
authorandersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277>2004-09-02 22:21:41 +0000
committerandersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277>2004-09-02 22:21:41 +0000
commit55c6ad8eccb1a025f75ad5f5c6b597419a1680e7 (patch)
treededb5229f5b92441ba10aa00667463ef100ccd6f /coreutils/id.c
parentb35759bc8c090a0a6c2f7006fdf4b77cdec9c3ae (diff)
downloadbusybox-w32-55c6ad8eccb1a025f75ad5f5c6b597419a1680e7.tar.gz
busybox-w32-55c6ad8eccb1a025f75ad5f5c6b597419a1680e7.tar.bz2
busybox-w32-55c6ad8eccb1a025f75ad5f5c6b597419a1680e7.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.......). git-svn-id: svn://busybox.net/trunk/busybox@9194 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils/id.c')
-rw-r--r--coreutils/id.c143
1 files changed, 87 insertions, 56 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}