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 | * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
|
---|
14 | */
|
---|
15 |
|
---|
16 | #include "libbb.h"
|
---|
17 |
|
---|
18 | #define PRINT_REAL 1
|
---|
19 | #define NAME_NOT_NUMBER 2
|
---|
20 | #define JUST_USER 4
|
---|
21 | #define JUST_GROUP 8
|
---|
22 | #if ENABLE_SELINUX
|
---|
23 | #define JUST_CONTEXT 16
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | static int printf_full(unsigned int id, const char *arg, const char prefix)
|
---|
27 | {
|
---|
28 | const char *fmt = "%cid=%u";
|
---|
29 | int status = EXIT_FAILURE;
|
---|
30 |
|
---|
31 | if (arg) {
|
---|
32 | fmt = "%cid=%u(%s)";
|
---|
33 | status = EXIT_SUCCESS;
|
---|
34 | }
|
---|
35 | printf(fmt, prefix, id, arg);
|
---|
36 | return status;
|
---|
37 | }
|
---|
38 |
|
---|
39 | int id_main(int argc, char **argv);
|
---|
40 | int id_main(int argc, char **argv)
|
---|
41 | {
|
---|
42 | struct passwd *p;
|
---|
43 | uid_t uid;
|
---|
44 | gid_t gid;
|
---|
45 | unsigned long flags;
|
---|
46 | short status;
|
---|
47 | #if ENABLE_SELINUX
|
---|
48 | security_context_t scontext;
|
---|
49 | #endif
|
---|
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" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g");
|
---|
53 | flags = getopt32(argv, "rnug" USE_SELINUX("Z"));
|
---|
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 | /* xuname2uid is needed because it exits on failure */
|
---|
66 | uid = xuname2uid(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 USE_SELINUX(| JUST_CONTEXT))) {
|
---|
72 | /* JUST_GROUP and JUST_USER are mutually exclusive */
|
---|
73 | if (flags & NAME_NOT_NUMBER) {
|
---|
74 | /* bb_getXXXid(-1) exit on failure, puts cannot segfault */
|
---|
75 | puts((flags & JUST_USER) ? bb_getpwuid(NULL, -1, uid) : bb_getgrgid(NULL, -1, gid));
|
---|
76 | } else {
|
---|
77 | if (flags & JUST_USER) {
|
---|
78 | printf("%u\n", uid);
|
---|
79 | }
|
---|
80 | if (flags & JUST_GROUP) {
|
---|
81 | printf("%u\n", gid);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | #if ENABLE_SELINUX
|
---|
86 | if (flags & JUST_CONTEXT) {
|
---|
87 | selinux_or_die();
|
---|
88 | if (argc - optind == 1) {
|
---|
89 | bb_error_msg_and_die("user name can't be passed with -Z");
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (getcon(&scontext)) {
|
---|
93 | bb_error_msg_and_die("can't get process context");
|
---|
94 | }
|
---|
95 | printf("%s\n", scontext);
|
---|
96 | }
|
---|
97 | #endif
|
---|
98 | /* exit */
|
---|
99 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* Print full info like GNU id */
|
---|
103 | /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */
|
---|
104 | status = printf_full(uid, bb_getpwuid(NULL, 0, uid), 'u');
|
---|
105 | putchar(' ');
|
---|
106 | status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), 'g');
|
---|
107 |
|
---|
108 | #if ENABLE_SELINUX
|
---|
109 | if (is_selinux_enabled()) {
|
---|
110 | security_context_t mysid;
|
---|
111 | const char *context;
|
---|
112 |
|
---|
113 | context = "unknown";
|
---|
114 | getcon(&mysid);
|
---|
115 | if (mysid) {
|
---|
116 | context = alloca(strlen(mysid) + 1);
|
---|
117 | strcpy((char*)context, mysid);
|
---|
118 | freecon(mysid);
|
---|
119 | }
|
---|
120 | printf(" context=%s", context);
|
---|
121 | }
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | putchar('\n');
|
---|
125 | fflush_stdout_and_exit(status);
|
---|
126 | }
|
---|