id: code shrink
[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  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 compliant. */
11 /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
12  * be more similar to GNU id.
13  * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
14  * Added -G option Tito Ragusa (C) 2008 for SUSv3.
15  */
16
17 #include "libbb.h"
18
19 #define PRINT_REAL        1
20 #define NAME_NOT_NUMBER   2
21 #define JUST_USER         4
22 #define JUST_GROUP        8
23 #define JUST_ALL_GROUPS  16
24 #if ENABLE_SELINUX
25 #define JUST_CONTEXT     32
26 #endif
27
28 static int printf_full(unsigned int id, const char *arg, const char *prefix)
29 {
30         const char *fmt = "%s%u";
31         int status = EXIT_FAILURE;
32
33         if (arg) {
34                 fmt = "%s%u(%s)";
35                 status = EXIT_SUCCESS;
36         }
37         printf(fmt, prefix, id, arg);
38         return status;
39 }
40
41 int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int id_main(int argc UNUSED_PARAM, char **argv)
43 {
44         struct passwd *p;
45         uid_t uid;
46         gid_t gid;
47         gid_t *groups;
48         int n;
49         unsigned long flags;
50         short status;
51 #if ENABLE_SELINUX
52         security_context_t scontext;
53 #endif
54         /* Don't allow -n -r -nr -ug -rug -nug -rnug */
55         /* Don't allow more than one username */
56         opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g");
57         flags = getopt32(argv, "rnugG" USE_SELINUX("Z"));
58
59         /* This values could be overwritten later */
60         uid = geteuid();
61         gid = getegid();
62         if (flags & PRINT_REAL) {
63                 uid = getuid();
64                 gid = getgid();
65         }
66
67         if (argv[optind]) {
68                 p = getpwnam(argv[optind]);
69                 /* xuname2uid is needed because it exits on failure */
70                 uid = xuname2uid(argv[optind]);
71                 gid = p->pw_gid;
72                 /* in this case PRINT_REAL is the same */
73         }
74
75         n = getgroups(0, NULL);
76         groups = xmalloc(sizeof(groups[0]) * n);
77         getgroups(n, groups);
78
79         if (flags & JUST_ALL_GROUPS) {
80                 while (n--) {
81                         if (flags & NAME_NOT_NUMBER)
82                                 printf("%s", bb_getgrgid(NULL, 0, *groups++));
83                         else
84                                 printf("%d", (int) *groups++);
85                         bb_putchar((n > 0) ? ' ' : '\n');
86                 }
87                 /* exit */
88                 fflush_stdout_and_exit(EXIT_SUCCESS);
89         }
90
91         if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) {
92                 /* JUST_GROUP and JUST_USER are mutually exclusive */
93                 if (flags & NAME_NOT_NUMBER) {
94                         /* bb_getXXXid(-1) exit on failure, puts cannot segfault */
95                         puts((flags & JUST_USER) ? bb_getpwuid(NULL, -1, uid) : bb_getgrgid(NULL, -1, gid));
96                 } else {
97                         if (flags & JUST_USER) {
98                                 printf("%u\n", uid);
99                         }
100                         if (flags & JUST_GROUP) {
101                                 printf("%u\n", gid);
102                         }
103                 }
104
105 #if ENABLE_SELINUX
106                 if (flags & JUST_CONTEXT) {
107                         selinux_or_die();
108                         if (argv[optind]) {
109                                 bb_error_msg_and_die("user name can't be passed with -Z");
110                         }
111
112                         if (getcon(&scontext)) {
113                                 bb_error_msg_and_die("can't get process context");
114                         }
115                         puts(scontext);
116                 }
117 #endif
118                 /* exit */
119                 fflush_stdout_and_exit(EXIT_SUCCESS);
120         }
121
122         /* Print full info like GNU id */
123         /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */
124         status = printf_full(uid, bb_getpwuid(NULL, 0, uid), "uid=");
125         status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), " gid=");
126         {
127                 const char *msg = " groups=";
128                 while (n--) {
129                         status |= printf_full(*groups, bb_getgrgid(NULL, 0, *groups), msg);
130                         msg = ",";
131                         groups++;
132                 }
133         }
134         /* we leak groups vector... */
135
136 #if ENABLE_SELINUX
137         if (is_selinux_enabled()) {
138                 security_context_t mysid;
139                 const char *context;
140
141                 context = "unknown";
142                 getcon(&mysid);
143                 if (mysid) {
144                         context = alloca(strlen(mysid) + 1);
145                         strcpy((char*)context, mysid);
146                         freecon(mysid);
147                 }
148                 printf(" context=%s", context);
149         }
150 #endif
151
152         bb_putchar('\n');
153         fflush_stdout_and_exit(status);
154 }