Undo all of the ugliness and some of the bloat from 15412.
[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 _NOT_ compliant -- option -G is not currently supported. */
11 /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
12  * be more similar to GNU id.
13  */
14
15 #include "busybox.h"
16 #include "pwd_.h"
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20
21 #ifdef CONFIG_SELINUX
22 #include <selinux/selinux.h>          /* for is_selinux_enabled() */
23 #endif
24
25 #define PRINT_REAL        1
26 #define NAME_NOT_NUMBER   2
27 #define JUST_USER         4
28 #define JUST_GROUP        8
29
30 static short printf_full(unsigned int id, const char *arg, const char prefix)
31 {
32         const char *fmt = "%cid=%u";
33         short status=EXIT_FAILURE;
34
35         if(arg) {
36                 fmt = "%cid=%u(%s)";
37                 status=EXIT_SUCCESS;
38         }
39         bb_printf(fmt, prefix, id, arg);
40         return status;
41 }
42
43 int id_main(int argc, char **argv)
44 {
45         struct passwd *p;
46         uid_t uid;
47         gid_t gid;
48         unsigned long flags;
49         short status;
50
51         /* Don't allow -n -r -nr -ug -rug -nug -rnug */
52         /* Don't allow more than one username */
53         bb_opt_complementally = "?1:?:u--g:g--u:r?ug:n?ug";
54         flags = bb_getopt_ulflags(argc, argv, "rnug");
55
56         /* This values could be overwritten later */
57         uid = geteuid();
58         gid = getegid();
59         if (flags & PRINT_REAL) {
60                 uid = getuid();
61                 gid = getgid();
62         }
63
64         if(argv[optind]) {
65                 p=getpwnam(argv[optind]);
66                 /* bb_xgetpwnam is needed because it exits on failure */
67                 uid = bb_xgetpwnam(argv[optind]);
68                 gid = p->pw_gid;
69                 /* in this case PRINT_REAL is the same */
70         }
71
72         if(flags & (JUST_GROUP | JUST_USER)) {
73                 /* JUST_GROUP and JUST_USER are mutually exclusive */
74                 if(flags & NAME_NOT_NUMBER) {
75                         /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
76                         puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
77                 } else {
78                         bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
79                 }
80                 /* exit */
81                 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
82         }
83
84         /* Print full info like GNU id */
85         /* bb_getpwuid doesn't exit on failure here */
86         status=printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
87         putchar(' ');
88         /* bb_getgrgid doesn't exit on failure here */
89         status|=printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
90
91 #ifdef CONFIG_SELINUX
92         if ( is_selinux_enabled() ) {
93                         security_context_t mysid;
94                         char context[80];
95                         int len = sizeof(context);
96
97                         getcon(&mysid);
98                         context[0] = '\0';
99                         if (mysid) {
100                                         len = strlen(mysid)+1;
101                                         safe_strncpy(context, mysid, len);
102                                         freecon(mysid);
103                         }else{
104                                         safe_strncpy(context, "unknown",8);
105                         }
106                 bb_printf(" context=%s", context);
107         }
108 #endif
109
110         putchar('\n');
111         bb_fflush_stdout_and_exit(status);
112 }