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