blob: 971e7cdad6949e00bca908830992fedd414ddf25 [file] [log] [blame]
Erik Andersen94f5e0b2000-05-01 19:10:52 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini id implementation for busybox
4 *
Erik Andersen94f5e0b2000-05-01 19:10:52 +00005 * 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
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
24
Eric Andersen3570a342000-09-25 21:45:58 +000025#include "busybox.h"
Erik Andersen94f5e0b2000-05-01 19:10:52 +000026#include <stdio.h>
27#include <unistd.h>
Mark Whitley99e370f2000-07-20 00:03:38 +000028#include <getopt.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <string.h>
Erik Andersen94f5e0b2000-05-01 19:10:52 +000030#include <sys/types.h>
Eric Andersen9e480452003-07-03 10:07:04 +000031#ifdef CONFIG_SELINUX
32#include <proc_secure.h>
33#include <flask_util.h>
34#endif
Erik Andersen94f5e0b2000-05-01 19:10:52 +000035
Eric Andersen9e480452003-07-03 10:07:04 +000036#define JUST_USER 1
37#define JUST_GROUP 2
Manuel Novoa III cad53642003-03-19 09:13:01 +000038#define PRINT_REAL 4
39#define NAME_NOT_NUMBER 8
40
Erik Andersen94f5e0b2000-05-01 19:10:52 +000041extern int id_main(int argc, char **argv)
42{
Eric Andersenbd193a42000-12-13 01:52:39 +000043 char user[9], group[9];
Eric Andersen6150df32000-07-28 16:22:51 +000044 long pwnam, grnam;
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 int uid, gid;
46 int flags;
Eric Andersen9e480452003-07-03 10:07:04 +000047#ifdef CONFIG_SELINUX
48 int is_flask_enabled_flag = is_flask_enabled();
49#endif
Erik Andersen94f5e0b2000-05-01 19:10:52 +000050
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 flags = bb_getopt_ulflags(argc, argv, "ugrn");
Erik Andersen94f5e0b2000-05-01 19:10:52 +000052
Eric Andersen9e480452003-07-03 10:07:04 +000053 if (((flags & (JUST_USER | JUST_GROUP)) == (JUST_USER | JUST_GROUP))
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 || (argc > optind + 1)
55 ) {
56 bb_show_usage();
Erik Andersen94f5e0b2000-05-01 19:10:52 +000057 }
58
Eric Andersenbd193a42000-12-13 01:52:39 +000059 if (argv[optind] == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 if (flags & PRINT_REAL) {
61 uid = getuid();
62 gid = getgid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +000063 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 uid = geteuid();
65 gid = getegid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +000066 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 my_getpwuid(user, uid);
Erik Andersen94f5e0b2000-05-01 19:10:52 +000068 } else {
Eric Andersen02e6ba92002-09-30 20:39:56 +000069 safe_strncpy(user, argv[optind], sizeof(user));
Erik Andersen94f5e0b2000-05-01 19:10:52 +000070 gid = my_getpwnamegid(user);
Erik Andersen94f5e0b2000-05-01 19:10:52 +000071 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 my_getgrgid(group, gid);
Erik Andersen94f5e0b2000-05-01 19:10:52 +000073
Eric Andersen6150df32000-07-28 16:22:51 +000074 pwnam=my_getpwnam(user);
75 grnam=my_getgrnam(group);
Eric Andersenc1b8f122001-01-25 05:12:02 +000076
Eric Andersen9e480452003-07-03 10:07:04 +000077 if (flags & (JUST_GROUP | JUST_USER)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 char *s = group;
Eric Andersen9e480452003-07-03 10:07:04 +000079 if (flags & JUST_USER) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 s = user;
81 grnam = pwnam;
82 }
83 if (flags & NAME_NOT_NUMBER) {
84 puts(s);
85 } else {
Eric Andersenc1b8f122001-01-25 05:12:02 +000086 printf("%ld\n", grnam);
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 }
Eric Andersenc1b8f122001-01-25 05:12:02 +000088 } else {
Eric Andersen9e480452003-07-03 10:07:04 +000089#ifdef CONFIG_SELINUX
90 printf("uid=%ld(%s) gid=%ld(%s)", pwnam, user, grnam, group);
91 if(is_flask_enabled_flag)
92 {
93 security_id_t mysid = getsecsid();
94 char context[80];
95 int len = sizeof(context);
96 context[0] = '\0';
97 if(security_sid_to_context(mysid, context, &len))
98 strcpy(context, "unknown");
99 printf(" context=%s\n", context);
100 }
101 else
102 printf("\n");
103#else
Eric Andersen6150df32000-07-28 16:22:51 +0000104 printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group);
Eric Andersen9e480452003-07-03 10:07:04 +0000105#endif
106
Eric Andersenc1b8f122001-01-25 05:12:02 +0000107 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108
109 bb_fflush_stdout_and_exit(0);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000110}