blob: 36007ae55c6b83629191f9893e5698c01a6beffd [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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen94f5e0b2000-05-01 19:10:52 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
Eric Andersen7eb79ff2004-09-02 22:21:41 +000011/* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000012 * be more similar to GNU id.
Eric Andersen7eb79ff2004-09-02 22:21:41 +000013 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000014
Eric Andersen3570a342000-09-25 21:45:58 +000015#include "busybox.h"
Erik Andersen94f5e0b2000-05-01 19:10:52 +000016#include <stdio.h>
17#include <unistd.h>
Erik Andersen94f5e0b2000-05-01 19:10:52 +000018#include <sys/types.h>
Eric Andersen7eb79ff2004-09-02 22:21:41 +000019
Eric Andersen9e480452003-07-03 10:07:04 +000020#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000021#include <selinux/selinux.h> /* for is_selinux_enabled() */
Eric Andersen9e480452003-07-03 10:07:04 +000022#endif
Erik Andersen94f5e0b2000-05-01 19:10:52 +000023
Eric Andersen7eb79ff2004-09-02 22:21:41 +000024#define PRINT_REAL 1
25#define NAME_NOT_NUMBER 2
26#define JUST_USER 4
27#define JUST_GROUP 8
28
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000029static short printf_full(unsigned int id, const char *arg, const char prefix)
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000030{
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000031 const char *fmt = "%cid=%u";
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000032 short status = EXIT_FAILURE;
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000033
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000034 if (arg) {
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000035 fmt = "%cid=%u(%s)";
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000036 status = EXIT_SUCCESS;
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000037 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000038 printf(fmt, prefix, id, arg);
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000039 return status;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000040}
Manuel Novoa III cad53642003-03-19 09:13:01 +000041
Rob Landleydfba7412006-03-06 20:47:33 +000042int id_main(int argc, char **argv)
Erik Andersen94f5e0b2000-05-01 19:10:52 +000043{
Eric Andersen7eb79ff2004-09-02 22:21:41 +000044 struct passwd *p;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000045 uid_t uid;
46 gid_t gid;
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000047 unsigned long flags;
48 short status;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000049
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000050 /* Don't allow -n -r -nr -ug -rug -nug -rnug */
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000051 /* Don't allow more than one username */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000052 opt_complementary = "?1:?:u--g:g--u:r?ug:n?ug";
53 flags = getopt32(argc, argv, "rnug");
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000054
Eric Andersen7eb79ff2004-09-02 22:21:41 +000055 /* This values could be overwritten later */
56 uid = geteuid();
57 gid = getegid();
58 if (flags & PRINT_REAL) {
59 uid = getuid();
60 gid = getgid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +000061 }
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000062
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000063 if (argv[optind]) {
64 p = getpwnam(argv[optind]);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000065 /* xuname2uid is needed because it exits on failure */
66 uid = xuname2uid(argv[optind]);
Eric Andersen7eb79ff2004-09-02 22:21:41 +000067 gid = p->pw_gid;
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000068 /* in this case PRINT_REAL is the same */
Erik Andersen94f5e0b2000-05-01 19:10:52 +000069 }
70
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000071 if (flags & (JUST_GROUP | JUST_USER)) {
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000072 /* JUST_GROUP and JUST_USER are mutually exclusive */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000073 if (flags & NAME_NOT_NUMBER) {
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000074 /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
75 puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000076 } else {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000077 printf("%u\n", (flags & JUST_USER) ? uid : gid);
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000078 }
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000079 /* exit */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000080 fflush_stdout_and_exit(EXIT_SUCCESS);
Eric Andersenc1b8f122001-01-25 05:12:02 +000081 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000082
Eric Andersen7eb79ff2004-09-02 22:21:41 +000083 /* Print full info like GNU id */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000084 /* bb_getpwuid doesn't exit on failure here */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000085 status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000086 putchar(' ');
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000087 /* bb_getgrgid doesn't exit on failure here */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000088 status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
Rob Landley60158cb2005-05-03 06:25:50 +000089
Eric Andersen7eb79ff2004-09-02 22:21:41 +000090#ifdef CONFIG_SELINUX
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000091 if (is_selinux_enabled()) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000092 security_context_t mysid;
93 const char *context;
Rob Landley60158cb2005-05-03 06:25:50 +000094
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +000095 context = "unknown";
96 getcon(&mysid);
97 if (mysid) {
98 context = alloca(strlen(mysid) + 1);
99 strcpy((char*)context, mysid);
100 freecon(mysid);
101 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000102 printf(" context=%s", context);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000103 }
104#endif
Rob Landley60158cb2005-05-03 06:25:50 +0000105
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000106 putchar('\n');
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000107 fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000108}