2000-05-01 19:10:52 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini id implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
|
|
|
|
*
|
2006-05-19 19:29:19 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2000-05-01 19:10:52 +00:00
|
|
|
*/
|
|
|
|
|
2008-09-12 09:58:11 +00:00
|
|
|
/* BB_AUDIT SUSv3 compliant. */
|
2004-09-02 22:21:41 +00:00
|
|
|
/* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
|
2005-10-11 14:38:01 +00:00
|
|
|
* be more similar to GNU id.
|
2007-03-10 16:58:49 +00:00
|
|
|
* -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
|
2008-09-12 09:58:11 +00:00
|
|
|
* Added -G option Tito Ragusa (C) 2008 for SUSv3.
|
2004-09-02 22:21:41 +00:00
|
|
|
*/
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2004-09-02 22:21:41 +00:00
|
|
|
|
|
|
|
#define PRINT_REAL 1
|
|
|
|
#define NAME_NOT_NUMBER 2
|
|
|
|
#define JUST_USER 4
|
|
|
|
#define JUST_GROUP 8
|
2008-09-12 09:58:11 +00:00
|
|
|
#define JUST_ALL_GROUPS 16
|
2007-03-10 16:58:49 +00:00
|
|
|
#if ENABLE_SELINUX
|
2008-09-12 09:58:11 +00:00
|
|
|
#define JUST_CONTEXT 32
|
2007-03-10 16:58:49 +00:00
|
|
|
#endif
|
2004-09-02 22:21:41 +00:00
|
|
|
|
2008-09-14 19:31:36 +00:00
|
|
|
static int printf_full(unsigned id, const char *arg, const char *prefix)
|
2005-10-11 14:38:01 +00:00
|
|
|
{
|
2008-09-12 09:58:11 +00:00
|
|
|
const char *fmt = "%s%u";
|
2007-07-27 11:20:10 +00:00
|
|
|
int status = EXIT_FAILURE;
|
2005-10-11 14:38:01 +00:00
|
|
|
|
2006-10-05 22:50:22 +00:00
|
|
|
if (arg) {
|
2008-09-12 09:58:11 +00:00
|
|
|
fmt = "%s%u(%s)";
|
2006-10-05 22:50:22 +00:00
|
|
|
status = EXIT_SUCCESS;
|
2004-09-15 03:04:08 +00:00
|
|
|
}
|
2006-10-26 23:21:47 +00:00
|
|
|
printf(fmt, prefix, id, arg);
|
2004-09-15 03:04:08 +00:00
|
|
|
return status;
|
2004-09-02 22:21:41 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int id_main(int argc UNUSED_PARAM, char **argv)
|
2000-05-01 19:10:52 +00:00
|
|
|
{
|
2004-09-02 22:21:41 +00:00
|
|
|
struct passwd *p;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
2008-09-12 09:58:11 +00:00
|
|
|
gid_t *groups;
|
2008-09-13 12:51:10 +00:00
|
|
|
int n;
|
2008-09-14 19:31:36 +00:00
|
|
|
unsigned flags;
|
2004-09-15 03:04:08 +00:00
|
|
|
short status;
|
2007-03-10 16:58:49 +00:00
|
|
|
#if ENABLE_SELINUX
|
2007-03-20 11:30:28 +00:00
|
|
|
security_context_t scontext;
|
2007-03-10 16:58:49 +00:00
|
|
|
#endif
|
2005-10-11 14:38:01 +00:00
|
|
|
/* Don't allow -n -r -nr -ug -rug -nug -rnug */
|
2004-09-15 03:04:08 +00:00
|
|
|
/* Don't allow more than one username */
|
2008-09-12 09:58:11 +00:00
|
|
|
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");
|
|
|
|
flags = getopt32(argv, "rnugG" USE_SELINUX("Z"));
|
2005-10-11 14:38:01 +00:00
|
|
|
|
2004-09-02 22:21:41 +00:00
|
|
|
/* This values could be overwritten later */
|
|
|
|
uid = geteuid();
|
|
|
|
gid = getegid();
|
|
|
|
if (flags & PRINT_REAL) {
|
|
|
|
uid = getuid();
|
|
|
|
gid = getgid();
|
2000-05-01 19:10:52 +00:00
|
|
|
}
|
2005-10-11 14:38:01 +00:00
|
|
|
|
2006-10-05 22:50:22 +00:00
|
|
|
if (argv[optind]) {
|
|
|
|
p = getpwnam(argv[optind]);
|
2006-12-28 05:44:47 +00:00
|
|
|
/* xuname2uid is needed because it exits on failure */
|
|
|
|
uid = xuname2uid(argv[optind]);
|
2004-09-02 22:21:41 +00:00
|
|
|
gid = p->pw_gid;
|
2005-10-11 14:38:01 +00:00
|
|
|
/* in this case PRINT_REAL is the same */
|
2000-05-01 19:10:52 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 12:51:10 +00:00
|
|
|
n = getgroups(0, NULL);
|
|
|
|
groups = xmalloc(sizeof(groups[0]) * n);
|
|
|
|
getgroups(n, groups);
|
2008-09-12 09:58:11 +00:00
|
|
|
|
2008-09-13 12:51:10 +00:00
|
|
|
if (flags & JUST_ALL_GROUPS) {
|
|
|
|
while (n--) {
|
2008-09-12 09:58:11 +00:00
|
|
|
if (flags & NAME_NOT_NUMBER)
|
|
|
|
printf("%s", bb_getgrgid(NULL, 0, *groups++));
|
|
|
|
else
|
2008-09-14 19:31:36 +00:00
|
|
|
printf("%u", (unsigned) *groups++);
|
2008-09-13 12:51:10 +00:00
|
|
|
bb_putchar((n > 0) ? ' ' : '\n');
|
2008-09-12 09:58:11 +00:00
|
|
|
}
|
|
|
|
/* exit */
|
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2007-03-10 16:58:49 +00:00
|
|
|
if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) {
|
2004-09-15 03:04:08 +00:00
|
|
|
/* JUST_GROUP and JUST_USER are mutually exclusive */
|
2006-10-05 22:50:22 +00:00
|
|
|
if (flags & NAME_NOT_NUMBER) {
|
2008-09-14 19:31:36 +00:00
|
|
|
/* bb_getXXXid(-1) exits on failure, puts cannot segfault */
|
2007-07-27 11:20:10 +00:00
|
|
|
puts((flags & JUST_USER) ? bb_getpwuid(NULL, -1, uid) : bb_getgrgid(NULL, -1, gid));
|
2004-09-15 03:04:08 +00:00
|
|
|
} else {
|
2007-03-10 16:58:49 +00:00
|
|
|
if (flags & JUST_USER) {
|
2008-09-14 19:31:36 +00:00
|
|
|
printf("%u\n", (unsigned)uid);
|
2007-03-20 11:30:28 +00:00
|
|
|
}
|
|
|
|
if (flags & JUST_GROUP) {
|
2008-09-14 19:31:36 +00:00
|
|
|
printf("%u\n", (unsigned)gid);
|
2007-03-20 11:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 16:58:49 +00:00
|
|
|
#if ENABLE_SELINUX
|
2007-03-20 11:30:28 +00:00
|
|
|
if (flags & JUST_CONTEXT) {
|
2007-03-10 16:58:49 +00:00
|
|
|
selinux_or_die();
|
2008-09-13 12:51:10 +00:00
|
|
|
if (argv[optind]) {
|
2007-03-20 11:30:28 +00:00
|
|
|
bb_error_msg_and_die("user name can't be passed with -Z");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getcon(&scontext)) {
|
|
|
|
bb_error_msg_and_die("can't get process context");
|
|
|
|
}
|
2007-10-01 12:05:12 +00:00
|
|
|
puts(scontext);
|
2007-03-20 11:30:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-10-11 14:38:01 +00:00
|
|
|
/* exit */
|
2006-10-26 23:21:47 +00:00
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
2004-09-02 22:21:41 +00:00
|
|
|
}
|
2004-09-15 03:04:08 +00:00
|
|
|
|
2004-09-02 22:21:41 +00:00
|
|
|
/* Print full info like GNU id */
|
2007-07-27 11:20:10 +00:00
|
|
|
/* bb_getpwuid(0) doesn't exit on failure (returns NULL) */
|
2008-09-12 09:58:11 +00:00
|
|
|
status = printf_full(uid, bb_getpwuid(NULL, 0, uid), "uid=");
|
2008-09-13 12:51:10 +00:00
|
|
|
status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), " gid=");
|
|
|
|
{
|
|
|
|
const char *msg = " groups=";
|
|
|
|
while (n--) {
|
|
|
|
status |= printf_full(*groups, bb_getgrgid(NULL, 0, *groups), msg);
|
|
|
|
msg = ",";
|
|
|
|
groups++;
|
|
|
|
}
|
2008-09-12 09:58:11 +00:00
|
|
|
}
|
2008-09-13 12:51:10 +00:00
|
|
|
/* we leak groups vector... */
|
|
|
|
|
2007-03-10 16:58:49 +00:00
|
|
|
#if ENABLE_SELINUX
|
2006-10-05 22:50:22 +00:00
|
|
|
if (is_selinux_enabled()) {
|
2006-12-28 21:33:30 +00:00
|
|
|
security_context_t mysid;
|
|
|
|
getcon(&mysid);
|
2008-09-14 19:31:36 +00:00
|
|
|
printf(" context=%s", mysid ? mysid : "unknown");
|
|
|
|
if (mysid) /* TODO: maybe freecon(NULL) is harmless? */
|
2006-12-28 21:33:30 +00:00
|
|
|
freecon(mysid);
|
2004-09-02 22:21:41 +00:00
|
|
|
}
|
2003-07-03 10:07:04 +00:00
|
|
|
#endif
|
2005-05-03 06:25:50 +00:00
|
|
|
|
2007-09-27 10:20:47 +00:00
|
|
|
bb_putchar('\n');
|
2006-10-26 23:21:47 +00:00
|
|
|
fflush_stdout_and_exit(status);
|
2004-09-02 22:21:41 +00:00
|
|
|
}
|