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