md5/sha1sum: better fix for small resource leak
[platform/upstream/busybox.git] / coreutils / id.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini id implementation for busybox
4  *
5  * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
6  * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10
11 /* BB_AUDIT SUSv3 compliant. */
12 /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever
13  * length and to be more similar to GNU id.
14  * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
15  * Added -G option Tito Ragusa (C) 2008 for SUSv3.
16  */
17
18 //usage:#define id_trivial_usage
19 //usage:       "[OPTIONS] [USER]"
20 //usage:#define id_full_usage "\n\n"
21 //usage:       "Print information about USER or the current user\n"
22 //usage:     "\nOptions:"
23 //usage:        IF_SELINUX(
24 //usage:     "\n        -Z      Security context"
25 //usage:        )
26 //usage:     "\n        -u      User ID"
27 //usage:     "\n        -g      Group ID"
28 //usage:     "\n        -G      Supplementary group IDs"
29 //usage:     "\n        -n      Print names instead of numbers"
30 //usage:     "\n        -r      Print real ID instead of effective ID"
31 //usage:
32 //usage:#define id_example_usage
33 //usage:       "$ id\n"
34 //usage:       "uid=1000(andersen) gid=1000(andersen)\n"
35
36 #include "libbb.h"
37
38 /* This is a NOEXEC applet. Be very careful! */
39
40 #if !ENABLE_USE_BB_PWD_GRP
41 #if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0)
42 #if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 &&  __UCLIBC_SUBLEVEL__ < 30)
43 #error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
44 #endif
45 #endif
46 #endif
47
48 enum {
49         PRINT_REAL      = (1 << 0),
50         NAME_NOT_NUMBER = (1 << 1),
51         JUST_USER       = (1 << 2),
52         JUST_GROUP      = (1 << 3),
53         JUST_ALL_GROUPS = (1 << 4),
54 #if ENABLE_SELINUX
55         JUST_CONTEXT    = (1 << 5),
56 #endif
57 };
58
59 static int print_common(unsigned id, const char *name, const char *prefix)
60 {
61         if (prefix) {
62                 printf("%s", prefix);
63         }
64         if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
65                 printf("%u", id);
66         }
67         if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
68                 if (name) {
69                         printf(option_mask32 ? "%s" : "(%s)", name);
70                 } else {
71                         /* Don't set error status flag in default mode */
72                         if (option_mask32) {
73                                 if (ENABLE_DESKTOP)
74                                         bb_error_msg("unknown ID %u", id);
75                                 return EXIT_FAILURE;
76                         }
77                 }
78         }
79         return EXIT_SUCCESS;
80 }
81
82 static int print_group(gid_t id, const char *prefix)
83 {
84         return print_common(id, gid2group(id), prefix);
85 }
86
87 static int print_user(uid_t id, const char *prefix)
88 {
89         return print_common(id, uid2uname(id), prefix);
90 }
91
92 /* On error set *n < 0 and return >= 0
93  * If *n is too small, update it and return < 0
94  *  (ok to trash groups[] in both cases)
95  * Otherwise fill in groups[] and return >= 0
96  */
97 static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
98 {
99         int m;
100
101         if (username) {
102                 /* If the user is a member of more than
103                  * *n groups, then -1 is returned. Otherwise >= 0.
104                  * (and no defined way of detecting errors?!) */
105                 m = getgrouplist(username, rgid, groups, n);
106                 /* I guess *n < 0 might indicate error. Anyway,
107                  * malloc'ing -1 bytes won't be good, so: */
108                 //if (*n < 0)
109                 //      return 0;
110                 //return m;
111                 //commented out here, happens below anyway
112         } else {
113                 /* On error -1 is returned, which ends up in *n */
114                 int nn = getgroups(*n, groups);
115                 /* 0: nn <= *n, groups[] was big enough; -1 otherwise */
116                 m = - (nn > *n);
117                 *n = nn;
118         }
119         if (*n < 0)
120                 return 0; /* error, don't return < 0! */
121         return m;
122 }
123
124 int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
125 int id_main(int argc UNUSED_PARAM, char **argv)
126 {
127         uid_t ruid;
128         gid_t rgid;
129         uid_t euid;
130         gid_t egid;
131         unsigned opt;
132         int i;
133         int status = EXIT_SUCCESS;
134         const char *prefix;
135         const char *username;
136 #if ENABLE_SELINUX
137         security_context_t scontext = NULL;
138 #endif
139         /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
140         /* Don't allow more than one username */
141         opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
142                          IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
143         opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
144
145         username = argv[optind];
146         if (username) {
147                 struct passwd *p = xgetpwnam(username);
148                 euid = ruid = p->pw_uid;
149                 egid = rgid = p->pw_gid;
150         } else {
151                 egid = getegid();
152                 rgid = getgid();
153                 euid = geteuid();
154                 ruid = getuid();
155         }
156         /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
157         /* id says: print the real ID instead of the effective ID, with -ugG */
158         /* in fact in this case egid is always printed if egid != rgid */
159         if (!opt || (opt & JUST_ALL_GROUPS)) {
160                 gid_t *groups;
161                 int n;
162
163                 if (!opt) {
164                         /* Default Mode */
165                         status |= print_user(ruid, "uid=");
166                         status |= print_group(rgid, " gid=");
167                         if (euid != ruid)
168                                 status |= print_user(euid, " euid=");
169                         if (egid != rgid)
170                                 status |= print_group(egid, " egid=");
171                 } else {
172                         /* JUST_ALL_GROUPS */
173                         status |= print_group(rgid, NULL);
174                         if (egid != rgid)
175                                 status |= print_group(egid, " ");
176                 }
177                 /* We are supplying largish buffer, trying
178                  * to not run get_groups() twice. That might be slow
179                  * ("user database in remote SQL server" case) */
180                 groups = xmalloc(64 * sizeof(gid_t));
181                 n = 64;
182                 if (get_groups(username, rgid, groups, &n) < 0) {
183                         /* Need bigger buffer after all */
184                         groups = xrealloc(groups, n * sizeof(gid_t));
185                         get_groups(username, rgid, groups, &n);
186                 }
187                 if (n > 0) {
188                         /* Print the list */
189                         prefix = " groups=";
190                         for (i = 0; i < n; i++) {
191                                 if (opt && (groups[i] == rgid || groups[i] == egid))
192                                         continue;
193                                 status |= print_group(groups[i], opt ? " " : prefix);
194                                 prefix = ",";
195                         }
196                 } else if (n < 0) { /* error in get_groups() */
197                         if (ENABLE_DESKTOP)
198                                 bb_error_msg_and_die("can't get groups");
199                         return EXIT_FAILURE;
200                 }
201                 if (ENABLE_FEATURE_CLEAN_UP)
202                         free(groups);
203 #if ENABLE_SELINUX
204                 if (is_selinux_enabled()) {
205                         if (getcon(&scontext) == 0)
206                                 printf(" context=%s", scontext);
207                 }
208 #endif
209         } else if (opt & PRINT_REAL) {
210                 euid = ruid;
211                 egid = rgid;
212         }
213
214         if (opt & JUST_USER)
215                 status |= print_user(euid, NULL);
216         else if (opt & JUST_GROUP)
217                 status |= print_group(egid, NULL);
218 #if ENABLE_SELINUX
219         else if (opt & JUST_CONTEXT) {
220                 selinux_or_die();
221                 if (username || getcon(&scontext)) {
222                         bb_error_msg_and_die("can't get process context%s",
223                                 username ? " for a different user" : "");
224                 }
225                 fputs(scontext, stdout);
226         }
227         /* freecon(NULL) seems to be harmless */
228         if (ENABLE_FEATURE_CLEAN_UP)
229                 freecon(scontext);
230 #endif
231         bb_putchar('\n');
232         fflush_stdout_and_exit(status);
233 }