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.
18 #include <sys/types.h>
21 #include <selinux/selinux.h> /* for is_selinux_enabled() */
25 #define NAME_NOT_NUMBER 2
29 static short printf_full(unsigned int id, const char *arg, const char prefix)
31 const char *fmt = "%cid=%u";
32 short status = EXIT_FAILURE;
36 status = EXIT_SUCCESS;
38 printf(fmt, prefix, id, arg);
42 int id_main(int argc, char **argv);
43 int id_main(int argc, char **argv)
51 /* Don't allow -n -r -nr -ug -rug -nug -rnug */
52 /* Don't allow more than one username */
53 opt_complementary = "?1:?:u--g:g--u:r?ug:n?ug";
54 flags = getopt32(argc, argv, "rnug");
56 /* This values could be overwritten later */
59 if (flags & PRINT_REAL) {
65 p = getpwnam(argv[optind]);
66 /* xuname2uid is needed because it exits on failure */
67 uid = xuname2uid(argv[optind]);
69 /* in this case PRINT_REAL is the same */
72 if (flags & (JUST_GROUP | JUST_USER)) {
73 /* JUST_GROUP and JUST_USER are mutually exclusive */
74 if (flags & NAME_NOT_NUMBER) {
75 /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
76 puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
78 printf("%u\n", (flags & JUST_USER) ? uid : gid);
81 fflush_stdout_and_exit(EXIT_SUCCESS);
84 /* Print full info like GNU id */
85 /* bb_getpwuid doesn't exit on failure here */
86 status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
88 /* bb_getgrgid doesn't exit on failure here */
89 status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
92 if (is_selinux_enabled()) {
93 security_context_t mysid;
99 context = alloca(strlen(mysid) + 1);
100 strcpy((char*)context, mysid);
103 printf(" context=%s", context);
108 fflush_stdout_and_exit(status);