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
|
|
|
*/
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
|
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>
|
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
|
2007-03-10 16:58:49 +00:00
|
|
|
#if ENABLE_SELINUX
|
2007-07-27 11:20:10 +00:00
|
|
|
#define JUST_CONTEXT 16
|
2007-03-10 16:58:49 +00:00
|
|
|
#endif
|
2004-09-02 22:21:41 +00:00
|
|
|
|
2007-07-27 11:20:10 +00:00
|
|
|
static int printf_full(unsigned int id, const char *arg, const char prefix)
|
2005-10-11 14:38:01 +00:00
|
|
|
{
|
2004-09-15 03:04:08 +00:00
|
|
|
const char *fmt = "%cid=%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) {
|
2004-09-15 03:04:08 +00:00
|
|
|
fmt = "%cid=%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;
|
2006-03-06 20:47:33 +00:00
|
|
|
int id_main(int argc, 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;
|
2004-09-15 03:04:08 +00:00
|
|
|
unsigned long flags;
|
|
|
|
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 */
|
2007-07-21 13:27:44 +00:00
|
|
|
opt_complementary = "?1:u--g:g--u:r?ug:n?ug" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g");
|
2007-08-18 15:32:12 +00:00
|
|
|
flags = getopt32(argv, "rnug" 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
|
|
|
}
|
|
|
|
|
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) {
|
2007-07-27 11:20:10 +00:00
|
|
|
/* bb_getXXXid(-1) exit on failure, puts cannot segfault */
|
|
|
|
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) {
|
2007-03-20 11:30:28 +00:00
|
|
|
printf("%u\n", uid);
|
|
|
|
}
|
|
|
|
if (flags & JUST_GROUP) {
|
|
|
|
printf("%u\n", gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2007-03-20 11:30:28 +00:00
|
|
|
if (argc - optind == 1) {
|
|
|
|
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) */
|
|
|
|
status = printf_full(uid, bb_getpwuid(NULL, 0, uid), 'u');
|
2007-09-27 10:20:47 +00:00
|
|
|
bb_putchar(' ');
|
2007-07-27 11:20:10 +00:00
|
|
|
status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), 'g');
|
2005-05-03 06:25:50 +00:00
|
|
|
|
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;
|
|
|
|
const char *context;
|
2005-05-03 06:25:50 +00:00
|
|
|
|
2006-12-28 21:33:30 +00:00
|
|
|
context = "unknown";
|
|
|
|
getcon(&mysid);
|
|
|
|
if (mysid) {
|
|
|
|
context = alloca(strlen(mysid) + 1);
|
|
|
|
strcpy((char*)context, mysid);
|
|
|
|
freecon(mysid);
|
|
|
|
}
|
2006-10-26 23:21:47 +00:00
|
|
|
printf(" context=%s", context);
|
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
|
|
|
}
|