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.
|
2004-09-02 22:21:41 +00:00
|
|
|
*/
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2000-09-25 21:45:58 +00:00
|
|
|
#include "busybox.h"
|
2000-05-01 19:10:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
2004-09-02 22:21:41 +00:00
|
|
|
|
2003-07-03 10:07:04 +00:00
|
|
|
#ifdef CONFIG_SELINUX
|
2005-05-03 06:25:50 +00:00
|
|
|
#include <selinux/selinux.h> /* for is_selinux_enabled() */
|
2003-07-03 10:07:04 +00:00
|
|
|
#endif
|
2000-05-01 19:10:52 +00:00
|
|
|
|
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
|
|
|
|
|
2004-09-15 03:04:08 +00:00
|
|
|
static short 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";
|
|
|
|
short status=EXIT_FAILURE;
|
2005-10-11 14:38:01 +00:00
|
|
|
|
2004-09-15 03:04:08 +00:00
|
|
|
if(arg) {
|
|
|
|
fmt = "%cid=%u(%s)";
|
|
|
|
status=EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
bb_printf(fmt, prefix, id, arg);
|
|
|
|
return status;
|
2004-09-02 22:21:41 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
|
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;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
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 */
|
2006-10-03 21:00:06 +00:00
|
|
|
opt_complementary = "?1:?:u--g:g--u:r?ug:n?ug";
|
|
|
|
flags = getopt32(argc, argv, "rnug");
|
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
|
|
|
|
2004-09-15 03:04:08 +00:00
|
|
|
if(argv[optind]) {
|
2004-09-02 22:21:41 +00:00
|
|
|
p=getpwnam(argv[optind]);
|
2005-09-20 21:06:17 +00:00
|
|
|
/* bb_xgetpwnam is needed because it exits on failure */
|
|
|
|
uid = bb_xgetpwnam(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
|
|
|
}
|
|
|
|
|
2004-09-15 03:04:08 +00:00
|
|
|
if(flags & (JUST_GROUP | JUST_USER)) {
|
|
|
|
/* JUST_GROUP and JUST_USER are mutually exclusive */
|
|
|
|
if(flags & NAME_NOT_NUMBER) {
|
2005-09-20 21:06:17 +00:00
|
|
|
/* 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 03:04:08 +00:00
|
|
|
} else {
|
|
|
|
bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
|
|
|
|
}
|
2005-10-11 14:38:01 +00:00
|
|
|
/* exit */
|
2004-09-02 22:21:41 +00:00
|
|
|
bb_fflush_stdout_and_exit(EXIT_SUCCESS);
|
|
|
|
}
|
2004-09-15 03:04:08 +00:00
|
|
|
|
2004-09-02 22:21:41 +00:00
|
|
|
/* Print full info like GNU id */
|
2005-09-20 21:06:17 +00:00
|
|
|
/* bb_getpwuid doesn't exit on failure here */
|
|
|
|
status=printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
|
2004-09-15 03:04:08 +00:00
|
|
|
putchar(' ');
|
2005-09-20 21:06:17 +00:00
|
|
|
/* bb_getgrgid doesn't exit on failure here */
|
|
|
|
status|=printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
|
2005-05-03 06:25:50 +00:00
|
|
|
|
2004-09-02 22:21:41 +00:00
|
|
|
#ifdef CONFIG_SELINUX
|
2005-05-03 06:25:50 +00:00
|
|
|
if ( is_selinux_enabled() ) {
|
|
|
|
security_context_t mysid;
|
|
|
|
char context[80];
|
|
|
|
int len = sizeof(context);
|
|
|
|
|
|
|
|
getcon(&mysid);
|
|
|
|
context[0] = '\0';
|
|
|
|
if (mysid) {
|
|
|
|
len = strlen(mysid)+1;
|
|
|
|
safe_strncpy(context, mysid, len);
|
|
|
|
freecon(mysid);
|
|
|
|
}else{
|
|
|
|
safe_strncpy(context, "unknown",8);
|
|
|
|
}
|
2004-09-15 03:04:08 +00:00
|
|
|
bb_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
|
|
|
|
2004-09-15 03:04:08 +00:00
|
|
|
putchar('\n');
|
|
|
|
bb_fflush_stdout_and_exit(status);
|
2004-09-02 22:21:41 +00:00
|
|
|
}
|