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