diff options
Diffstat (limited to 'coreutils/id.c')
-rw-r--r-- | coreutils/id.c | 143 |
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 | |||
47 | void 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 | ||
41 | extern int id_main(int argc, char **argv) | 54 | extern 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) { | 106 | PRINT: |
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 | /* | ||
135 | Local Variables: | ||
136 | c-file-style: "linux" | ||
137 | c-basic-offset: 4 | ||
138 | tab-width: 4 | ||
139 | End: | ||
140 | */ | ||
108 | 141 | ||
109 | bb_fflush_stdout_and_exit(0); | ||
110 | } | ||