Tito writes,
[oweals/busybox.git] / coreutils / id.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini id implementation for busybox
4  *
5  * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
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. 
26  */
27
28 #include "busybox.h"
29 #include "pwd_.h"
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33
34 #ifdef CONFIG_SELINUX
35 #include <proc_secure.h>
36 #include <flask_util.h>
37 #endif
38
39 #define PRINT_REAL        1
40 #define NAME_NOT_NUMBER   2
41 #define JUST_USER         4
42 #define JUST_GROUP        8
43
44 static short printf_full(unsigned int id, const char *arg, const char prefix)
45 {       
46         const char *fmt = "%cid=%u";
47         short status=EXIT_FAILURE;
48         
49         if(arg) {
50                 fmt = "%cid=%u(%s)";
51                 status=EXIT_SUCCESS;
52         }
53         bb_printf(fmt, prefix, id, arg);
54         return status;
55 }
56
57 extern int id_main(int argc, char **argv)
58 {
59         struct passwd *p;
60         uid_t uid;
61         gid_t gid;
62         unsigned long flags;
63         short status;
64 #ifdef CONFIG_SELINUX
65         int is_flask_enabled_flag = is_flask_enabled();
66 #endif
67
68         bb_opt_complementaly = "u~g:g~u";
69         flags = bb_getopt_ulflags(argc, argv, "rnug");
70
71         if ((flags & 0x80000000UL)
72         /* Don't allow -n -r -nr */
73         || (flags <= 3 && flags > 0) 
74         /* Don't allow more than one username */
75         || (argc > optind + 1))
76                 bb_show_usage();
77         
78         /* This values could be overwritten later */
79         uid = geteuid();
80         gid = getegid();
81         if (flags & PRINT_REAL) {
82                 uid = getuid();
83                 gid = getgid();
84         }
85         
86         if(argv[optind]) {
87                 p=getpwnam(argv[optind]);
88                 /* my_getpwnam is needed because it exits on failure */
89                 uid = my_getpwnam(argv[optind]);
90                 gid = p->pw_gid;
91                 /* in this case PRINT_REAL is the same */ 
92         }
93
94         if(flags & (JUST_GROUP | JUST_USER)) {
95                 /* JUST_GROUP and JUST_USER are mutually exclusive */
96                 if(flags & NAME_NOT_NUMBER) {
97                         /* my_getpwuid and my_getgrgid exit on failure so puts cannot segfault */
98                         puts((flags & JUST_USER) ? my_getpwuid(NULL, uid, -1 ) : my_getgrgid(NULL, gid, -1 ));
99                 } else {
100                         bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
101                 }
102                 /* exit */ 
103                 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
104         }
105
106         /* Print full info like GNU id */
107         /* my_getpwuid doesn't exit on failure here */
108         status=printf_full(uid, my_getpwuid(NULL, uid, 0), 'u');
109         putchar(' ');
110         /* my_getgrgid doesn't exit on failure here */
111         status|=printf_full(gid, my_getgrgid(NULL, gid, 0), 'g');
112 #ifdef CONFIG_SELINUX
113         if(is_flask_enabled_flag) {
114                 security_id_t mysid = getsecsid();
115                 char context[80];
116                 int len = sizeof(context);
117                 context[0] = '\0';
118                 if(security_sid_to_context(mysid, context, &len))
119                         strcpy(context, "unknown");
120                 bb_printf(" context=%s", context);
121         }
122 #endif
123         putchar('\n');
124         bb_fflush_stdout_and_exit(status);
125 }
126
127 /* END CODE */
128 /*
129 Local Variables:
130 c-file-style: "linux"
131 c-basic-offset: 4
132 tab-width: 4
133 End:
134 */
135