1 /* vi: set sw=4 ts=4: */
3 * Mini id implementation for busybox
5 * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 /* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
11 /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
12 * be more similar to GNU id.
13 * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
19 #define NAME_NOT_NUMBER 2
23 #define JUST_CONTEXT 16
26 static short printf_full(unsigned int id, const char *arg, const char prefix)
28 const char *fmt = "%cid=%u";
29 short status = EXIT_FAILURE;
33 status = EXIT_SUCCESS;
35 printf(fmt, prefix, id, arg);
39 int id_main(int argc, char **argv);
40 int id_main(int argc, char **argv)
48 security_context_t scontext;
50 /* Don't allow -n -r -nr -ug -rug -nug -rnug */
51 /* Don't allow more than one username */
52 opt_complementary = "?1:u--g:g--u:r?ug:n?ug" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g");
53 flags = getopt32(argc, argv, "rnug" USE_SELINUX("Z"));
55 /* This values could be overwritten later */
58 if (flags & PRINT_REAL) {
64 p = getpwnam(argv[optind]);
65 /* xuname2uid is needed because it exits on failure */
66 uid = xuname2uid(argv[optind]);
68 /* in this case PRINT_REAL is the same */
71 if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) {
72 /* JUST_GROUP and JUST_USER are mutually exclusive */
73 if (flags & NAME_NOT_NUMBER) {
74 /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
75 puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
77 if (flags & JUST_USER) {
80 if (flags & JUST_GROUP) {
86 if (flags & JUST_CONTEXT) {
88 if (argc - optind == 1) {
89 bb_error_msg_and_die("user name can't be passed with -Z");
92 if (getcon(&scontext)) {
93 bb_error_msg_and_die("can't get process context");
95 printf("%s\n", scontext);
99 fflush_stdout_and_exit(EXIT_SUCCESS);
102 /* Print full info like GNU id */
103 /* bb_getpwuid doesn't exit on failure here */
104 status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
106 /* bb_getgrgid doesn't exit on failure here */
107 status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
110 if (is_selinux_enabled()) {
111 security_context_t mysid;
117 context = alloca(strlen(mysid) + 1);
118 strcpy((char*)context, mysid);
121 printf(" context=%s", context);
126 fflush_stdout_and_exit(status);