1 /* vi: set sw=4 ts=4: */
3 * Mini id implementation for busybox
5 * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
24 /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
25 * be more similar to GNU id.
32 #include <sys/types.h>
35 #include <selinux/selinux.h> /* for is_selinux_enabled() */
39 #define NAME_NOT_NUMBER 2
43 static short printf_full(unsigned int id, const char *arg, const char prefix)
45 const char *fmt = "%cid=%u";
46 short status=EXIT_FAILURE;
52 bb_printf(fmt, prefix, id, arg);
56 extern int id_main(int argc, char **argv)
64 bb_opt_complementaly = "u~g:g~u";
65 flags = bb_getopt_ulflags(argc, argv, "rnug");
67 if ((flags & BB_GETOPT_ERROR)
68 /* Don't allow -n -r -nr */
69 || (flags <= 3 && flags > 0)
70 /* Don't allow more than one username */
71 || (argc > optind + 1))
74 /* This values could be overwritten later */
77 if (flags & PRINT_REAL) {
83 p=getpwnam(argv[optind]);
84 /* my_getpwnam is needed because it exits on failure */
85 uid = my_getpwnam(argv[optind]);
87 /* in this case PRINT_REAL is the same */
90 if(flags & (JUST_GROUP | JUST_USER)) {
91 /* JUST_GROUP and JUST_USER are mutually exclusive */
92 if(flags & NAME_NOT_NUMBER) {
93 /* my_getpwuid and my_getgrgid exit on failure so puts cannot segfault */
94 puts((flags & JUST_USER) ? my_getpwuid(NULL, uid, -1 ) : my_getgrgid(NULL, gid, -1 ));
96 bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
99 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
102 /* Print full info like GNU id */
103 /* my_getpwuid doesn't exit on failure here */
104 status=printf_full(uid, my_getpwuid(NULL, uid, 0), 'u');
106 /* my_getgrgid doesn't exit on failure here */
107 status|=printf_full(gid, my_getgrgid(NULL, gid, 0), 'g');
109 #ifdef CONFIG_SELINUX
110 if ( is_selinux_enabled() ) {
111 security_context_t mysid;
113 int len = sizeof(context);
118 len = strlen(mysid)+1;
119 safe_strncpy(context, mysid, len);
122 safe_strncpy(context, "unknown",8);
124 bb_printf(" context=%s", context);
129 bb_fflush_stdout_and_exit(status);
135 c-file-style: "linux"