blob: 39a57c196e8ddc9c35ec6e43b419b0d8f7f1f723 [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. */
Eric Andersen7eb79ff2004-09-02 22:21:41 +000024/* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000025 * be more similar to GNU id.
Eric Andersen7eb79ff2004-09-02 22:21:41 +000026 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000027
Eric Andersen3570a342000-09-25 21:45:58 +000028#include "busybox.h"
Eric Andersen7eb79ff2004-09-02 22:21:41 +000029#include "pwd_.h"
Erik Andersen94f5e0b2000-05-01 19:10:52 +000030#include <stdio.h>
31#include <unistd.h>
Erik Andersen94f5e0b2000-05-01 19:10:52 +000032#include <sys/types.h>
Eric Andersen7eb79ff2004-09-02 22:21:41 +000033
Eric Andersen9e480452003-07-03 10:07:04 +000034#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000035#include <selinux/selinux.h> /* for is_selinux_enabled() */
Eric Andersen9e480452003-07-03 10:07:04 +000036#endif
Erik Andersen94f5e0b2000-05-01 19:10:52 +000037
Eric Andersen7eb79ff2004-09-02 22:21:41 +000038#define PRINT_REAL 1
39#define NAME_NOT_NUMBER 2
40#define JUST_USER 4
41#define JUST_GROUP 8
42
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000043static short printf_full(unsigned int id, const char *arg, const char prefix)
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000044{
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000045 const char *fmt = "%cid=%u";
46 short status=EXIT_FAILURE;
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000047
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000048 if(arg) {
49 fmt = "%cid=%u(%s)";
50 status=EXIT_SUCCESS;
51 }
52 bb_printf(fmt, prefix, id, arg);
53 return status;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000054}
Manuel Novoa III cad53642003-03-19 09:13:01 +000055
Erik Andersen94f5e0b2000-05-01 19:10:52 +000056extern int id_main(int argc, char **argv)
57{
Eric Andersen7eb79ff2004-09-02 22:21:41 +000058 struct passwd *p;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000059 uid_t uid;
60 gid_t gid;
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000061 unsigned long flags;
62 short status;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000063
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000064 /* Don't allow -n -r -nr -ug -rug -nug -rnug */
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000065 /* Don't allow more than one username */
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +000066 bb_opt_complementally = "?1:?:u--g:g--u:r?ug:n?ug";
67 flags = bb_getopt_ulflags(argc, argv, "rnug");
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000068
Eric Andersen7eb79ff2004-09-02 22:21:41 +000069 /* This values could be overwritten later */
70 uid = geteuid();
71 gid = getegid();
72 if (flags & PRINT_REAL) {
73 uid = getuid();
74 gid = getgid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +000075 }
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000076
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000077 if(argv[optind]) {
Eric Andersen7eb79ff2004-09-02 22:21:41 +000078 p=getpwnam(argv[optind]);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000079 /* bb_xgetpwnam is needed because it exits on failure */
80 uid = bb_xgetpwnam(argv[optind]);
Eric Andersen7eb79ff2004-09-02 22:21:41 +000081 gid = p->pw_gid;
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000082 /* in this case PRINT_REAL is the same */
Erik Andersen94f5e0b2000-05-01 19:10:52 +000083 }
84
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000085 if(flags & (JUST_GROUP | JUST_USER)) {
86 /* JUST_GROUP and JUST_USER are mutually exclusive */
87 if(flags & NAME_NOT_NUMBER) {
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000088 /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
89 puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000090 } else {
91 bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
92 }
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000093 /* exit */
Eric Andersen7eb79ff2004-09-02 22:21:41 +000094 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
Eric Andersenc1b8f122001-01-25 05:12:02 +000095 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000096
Eric Andersen7eb79ff2004-09-02 22:21:41 +000097 /* Print full info like GNU id */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000098 /* bb_getpwuid doesn't exit on failure here */
99 status=printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000100 putchar(' ');
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000101 /* bb_getgrgid doesn't exit on failure here */
102 status|=printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
Rob Landley60158cb2005-05-03 06:25:50 +0000103
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000104#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +0000105 if ( is_selinux_enabled() ) {
106 security_context_t mysid;
107 char context[80];
108 int len = sizeof(context);
109
110 getcon(&mysid);
111 context[0] = '\0';
112 if (mysid) {
113 len = strlen(mysid)+1;
114 safe_strncpy(context, mysid, len);
115 freecon(mysid);
116 }else{
117 safe_strncpy(context, "unknown",8);
118 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000119 bb_printf(" context=%s", context);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000120 }
121#endif
Rob Landley60158cb2005-05-03 06:25:50 +0000122
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000123 putchar('\n');
124 bb_fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000125}
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000126
127/* END CODE */
128/*
129Local Variables:
130c-file-style: "linux"
131c-basic-offset: 4
132tab-width: 4
133End:
134*/
135