1 /* vi: set sw=4 ts=4: */
3 * Mini id implementation for busybox
5 * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
6 * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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.
20 /* This is a NOEXEC applet. Be very careful! */
22 #if !ENABLE_USE_BB_PWD_GRP
23 #if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0)
24 #if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 30)
25 #error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
31 PRINT_REAL
= (1 << 0),
32 NAME_NOT_NUMBER
= (1 << 1),
34 JUST_GROUP
= (1 << 3),
35 JUST_ALL_GROUPS
= (1 << 4),
37 JUST_CONTEXT
= (1 << 5),
41 static int print_common(unsigned id
, const char *name
, const char *prefix
)
46 if (!(option_mask32
& NAME_NOT_NUMBER
) || !name
) {
49 if (!option_mask32
|| (option_mask32
& NAME_NOT_NUMBER
)) {
51 printf(option_mask32
? "%s" : "(%s)", name
);
53 /* Don't set error status flag in default mode */
56 bb_error_msg("unknown ID %u", id
);
64 static int print_group(gid_t id
, const char *prefix
)
66 return print_common(id
, gid2group(id
), prefix
);
69 static int print_user(uid_t id
, const char *prefix
)
71 return print_common(id
, uid2uname(id
), prefix
);
74 /* On error set *n < 0 and return >= 0
75 * If *n is too small, update it and return < 0
76 * (ok to trash groups[] in both cases)
77 * Otherwise fill in groups[] and return >= 0
79 static int get_groups(const char *username
, gid_t rgid
, gid_t
*groups
, int *n
)
84 /* If the user is a member of more than
85 * *n groups, then -1 is returned. Otherwise >= 0.
86 * (and no defined way of detecting errors?!) */
87 m
= getgrouplist(username
, rgid
, groups
, n
);
88 /* I guess *n < 0 might indicate error. Anyway,
89 * malloc'ing -1 bytes won't be good, so: */
93 //commented out here, happens below anyway
95 /* On error -1 is returned, which ends up in *n */
96 int nn
= getgroups(*n
, groups
);
97 /* 0: nn <= *n, groups[] was big enough; -1 otherwise */
102 return 0; /* error, don't return < 0! */
106 int id_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
107 int id_main(int argc UNUSED_PARAM
, char **argv
)
115 int status
= EXIT_SUCCESS
;
117 const char *username
;
119 security_context_t scontext
= NULL
;
121 /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
122 /* Don't allow more than one username */
123 opt_complementary
= "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
124 IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
125 opt
= getopt32(argv
, "rnugG" IF_SELINUX("Z"));
127 username
= argv
[optind
];
129 struct passwd
*p
= xgetpwnam(username
);
130 euid
= ruid
= p
->pw_uid
;
131 egid
= rgid
= p
->pw_gid
;
138 /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
139 /* id says: print the real ID instead of the effective ID, with -ugG */
140 /* in fact in this case egid is always printed if egid != rgid */
141 if (!opt
|| (opt
& JUST_ALL_GROUPS
)) {
147 status
|= print_user(ruid
, "uid=");
148 status
|= print_group(rgid
, " gid=");
150 status
|= print_user(euid
, " euid=");
152 status
|= print_group(egid
, " egid=");
154 /* JUST_ALL_GROUPS */
155 status
|= print_group(rgid
, NULL
);
157 status
|= print_group(egid
, " ");
159 /* We are supplying largish buffer, trying
160 * to not run get_groups() twice. That might be slow
161 * ("user database in remote SQL server" case) */
162 groups
= xmalloc(64 * sizeof(gid_t
));
164 if (get_groups(username
, rgid
, groups
, &n
) < 0) {
165 /* Need bigger buffer after all */
166 groups
= xrealloc(groups
, n
* sizeof(gid_t
));
167 get_groups(username
, rgid
, groups
, &n
);
172 for (i
= 0; i
< n
; i
++) {
173 if (opt
&& (groups
[i
] == rgid
|| groups
[i
] == egid
))
175 status
|= print_group(groups
[i
], opt
? " " : prefix
);
178 } else if (n
< 0) { /* error in get_groups() */
180 bb_error_msg_and_die("can't get groups");
184 if (ENABLE_FEATURE_CLEAN_UP
)
187 if (is_selinux_enabled()) {
188 if (getcon(&scontext
) == 0)
189 printf(" context=%s", scontext
);
192 } else if (opt
& PRINT_REAL
) {
198 status
|= print_user(euid
, NULL
);
199 else if (opt
& JUST_GROUP
)
200 status
|= print_group(egid
, NULL
);
202 else if (opt
& JUST_CONTEXT
) {
204 if (username
|| getcon(&scontext
)) {
205 bb_error_msg_and_die("can't get process context%s",
206 username
? " for a different user" : "");
208 fputs(scontext
, stdout
);
210 /* freecon(NULL) seems to be harmless */
211 if (ENABLE_FEATURE_CLEAN_UP
)
215 fflush_stdout_and_exit(status
);