2000-05-02 00:40:52 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini id implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
|
|
|
|
*
|
2006-05-20 00:59:19 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2000-05-02 00:40:52 +05:30
|
|
|
*/
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
|
2004-09-03 03:51:41 +05:30
|
|
|
/* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
|
2005-10-11 20:08:01 +05:30
|
|
|
* be more similar to GNU id.
|
2007-03-10 22:28:49 +05:30
|
|
|
* -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
|
2004-09-03 03:51:41 +05:30
|
|
|
*/
|
2003-03-19 14:43:01 +05:30
|
|
|
|
2000-09-26 03:15:58 +05:30
|
|
|
#include "busybox.h"
|
2000-05-02 00:40:52 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
2004-09-03 03:51:41 +05:30
|
|
|
|
|
|
|
#define PRINT_REAL 1
|
|
|
|
#define NAME_NOT_NUMBER 2
|
|
|
|
#define JUST_USER 4
|
|
|
|
#define JUST_GROUP 8
|
2007-03-10 22:28:49 +05:30
|
|
|
#if ENABLE_SELINUX
|
|
|
|
#define JUST_CONTEXT 16
|
|
|
|
#endif
|
2004-09-03 03:51:41 +05:30
|
|
|
|
2004-09-15 08:34:08 +05:30
|
|
|
static short printf_full(unsigned int id, const char *arg, const char prefix)
|
2005-10-11 20:08:01 +05:30
|
|
|
{
|
2004-09-15 08:34:08 +05:30
|
|
|
const char *fmt = "%cid=%u";
|
2006-10-06 04:20:22 +05:30
|
|
|
short status = EXIT_FAILURE;
|
2005-10-11 20:08:01 +05:30
|
|
|
|
2006-10-06 04:20:22 +05:30
|
|
|
if (arg) {
|
2004-09-15 08:34:08 +05:30
|
|
|
fmt = "%cid=%u(%s)";
|
2006-10-06 04:20:22 +05:30
|
|
|
status = EXIT_SUCCESS;
|
2004-09-15 08:34:08 +05:30
|
|
|
}
|
2006-10-27 04:51:47 +05:30
|
|
|
printf(fmt, prefix, id, arg);
|
2004-09-15 08:34:08 +05:30
|
|
|
return status;
|
2004-09-03 03:51:41 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
|
2007-02-03 22:58:39 +05:30
|
|
|
int id_main(int argc, char **argv);
|
2006-03-07 02:17:33 +05:30
|
|
|
int id_main(int argc, char **argv)
|
2000-05-02 00:40:52 +05:30
|
|
|
{
|
2004-09-03 03:51:41 +05:30
|
|
|
struct passwd *p;
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
2004-09-15 08:34:08 +05:30
|
|
|
unsigned long flags;
|
|
|
|
short status;
|
2007-03-10 22:28:49 +05:30
|
|
|
#if ENABLE_SELINUX
|
2007-03-20 17:00:28 +05:30
|
|
|
security_context_t scontext;
|
2007-03-10 22:28:49 +05:30
|
|
|
#endif
|
2005-10-11 20:08:01 +05:30
|
|
|
/* Don't allow -n -r -nr -ug -rug -nug -rnug */
|
2004-09-15 08:34:08 +05:30
|
|
|
/* Don't allow more than one username */
|
2007-03-10 22:28:49 +05:30
|
|
|
opt_complementary = "?1:?:u--g:g--u:r?ug:n?ug" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g");
|
|
|
|
flags = getopt32(argc, argv, "rnug" USE_SELINUX("Z"));
|
2005-10-11 20:08:01 +05:30
|
|
|
|
2004-09-03 03:51:41 +05:30
|
|
|
/* This values could be overwritten later */
|
|
|
|
uid = geteuid();
|
|
|
|
gid = getegid();
|
|
|
|
if (flags & PRINT_REAL) {
|
|
|
|
uid = getuid();
|
|
|
|
gid = getgid();
|
2000-05-02 00:40:52 +05:30
|
|
|
}
|
2005-10-11 20:08:01 +05:30
|
|
|
|
2006-10-06 04:20:22 +05:30
|
|
|
if (argv[optind]) {
|
|
|
|
p = getpwnam(argv[optind]);
|
2006-12-28 11:14:47 +05:30
|
|
|
/* xuname2uid is needed because it exits on failure */
|
|
|
|
uid = xuname2uid(argv[optind]);
|
2004-09-03 03:51:41 +05:30
|
|
|
gid = p->pw_gid;
|
2005-10-11 20:08:01 +05:30
|
|
|
/* in this case PRINT_REAL is the same */
|
2000-05-02 00:40:52 +05:30
|
|
|
}
|
|
|
|
|
2007-03-10 22:28:49 +05:30
|
|
|
if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) {
|
2004-09-15 08:34:08 +05:30
|
|
|
/* JUST_GROUP and JUST_USER are mutually exclusive */
|
2006-10-06 04:20:22 +05:30
|
|
|
if (flags & NAME_NOT_NUMBER) {
|
2005-09-21 02:36:17 +05:30
|
|
|
/* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
|
|
|
|
puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
|
2004-09-15 08:34:08 +05:30
|
|
|
} else {
|
2007-03-10 22:28:49 +05:30
|
|
|
if (flags & JUST_USER) {
|
2007-03-20 17:00:28 +05:30
|
|
|
printf("%u\n", uid);
|
|
|
|
}
|
|
|
|
if (flags & JUST_GROUP) {
|
|
|
|
printf("%u\n", gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 22:28:49 +05:30
|
|
|
#if ENABLE_SELINUX
|
2007-03-20 17:00:28 +05:30
|
|
|
if (flags & JUST_CONTEXT) {
|
2007-03-10 22:28:49 +05:30
|
|
|
selinux_or_die();
|
2007-03-20 17:00:28 +05:30
|
|
|
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");
|
|
|
|
}
|
|
|
|
printf("%s\n", scontext);
|
|
|
|
}
|
|
|
|
#endif
|
2005-10-11 20:08:01 +05:30
|
|
|
/* exit */
|
2006-10-27 04:51:47 +05:30
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
2004-09-03 03:51:41 +05:30
|
|
|
}
|
2004-09-15 08:34:08 +05:30
|
|
|
|
2004-09-03 03:51:41 +05:30
|
|
|
/* Print full info like GNU id */
|
2005-09-21 02:36:17 +05:30
|
|
|
/* bb_getpwuid doesn't exit on failure here */
|
2006-10-06 04:20:22 +05:30
|
|
|
status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
|
2004-09-15 08:34:08 +05:30
|
|
|
putchar(' ');
|
2005-09-21 02:36:17 +05:30
|
|
|
/* bb_getgrgid doesn't exit on failure here */
|
2006-10-06 04:20:22 +05:30
|
|
|
status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
|
2005-05-03 11:55:50 +05:30
|
|
|
|
2007-03-10 22:28:49 +05:30
|
|
|
#if ENABLE_SELINUX
|
2006-10-06 04:20:22 +05:30
|
|
|
if (is_selinux_enabled()) {
|
2006-12-29 03:03:30 +05:30
|
|
|
security_context_t mysid;
|
|
|
|
const char *context;
|
2005-05-03 11:55:50 +05:30
|
|
|
|
2006-12-29 03:03:30 +05:30
|
|
|
context = "unknown";
|
|
|
|
getcon(&mysid);
|
|
|
|
if (mysid) {
|
|
|
|
context = alloca(strlen(mysid) + 1);
|
|
|
|
strcpy((char*)context, mysid);
|
|
|
|
freecon(mysid);
|
|
|
|
}
|
2006-10-27 04:51:47 +05:30
|
|
|
printf(" context=%s", context);
|
2004-09-03 03:51:41 +05:30
|
|
|
}
|
2003-07-03 15:37:04 +05:30
|
|
|
#endif
|
2005-05-03 11:55:50 +05:30
|
|
|
|
2004-09-15 08:34:08 +05:30
|
|
|
putchar('\n');
|
2006-10-27 04:51:47 +05:30
|
|
|
fflush_stdout_and_exit(status);
|
2004-09-03 03:51:41 +05:30
|
|
|
}
|