blob: a0364675ff43f1e0ddb27abd7f380fd5d8921521 [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.
Denis Vlasenko49622d72007-03-10 16:58:49 +000013 * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
Eric Andersen7eb79ff2004-09-02 22:21:41 +000014 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000015
Eric Andersen3570a342000-09-25 21:45:58 +000016#include "busybox.h"
Erik Andersen94f5e0b2000-05-01 19:10:52 +000017#include <stdio.h>
18#include <unistd.h>
Erik Andersen94f5e0b2000-05-01 19:10:52 +000019#include <sys/types.h>
Eric Andersen7eb79ff2004-09-02 22:21:41 +000020
Eric Andersen7eb79ff2004-09-02 22:21:41 +000021#define PRINT_REAL 1
22#define NAME_NOT_NUMBER 2
23#define JUST_USER 4
24#define JUST_GROUP 8
Denis Vlasenko49622d72007-03-10 16:58:49 +000025#if ENABLE_SELINUX
26#define JUST_CONTEXT 16
27#endif
Eric Andersen7eb79ff2004-09-02 22:21:41 +000028
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
Denis Vlasenko06af2162007-02-03 17:28:39 +000042int id_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000043int id_main(int argc, char **argv)
Erik Andersen94f5e0b2000-05-01 19:10:52 +000044{
Eric Andersen7eb79ff2004-09-02 22:21:41 +000045 struct passwd *p;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000046 uid_t uid;
47 gid_t gid;
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000048 unsigned long flags;
49 short status;
Denis Vlasenko49622d72007-03-10 16:58:49 +000050#if ENABLE_SELINUX
51 security_context_t scontext;
52#endif
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000053 /* Don't allow -n -r -nr -ug -rug -nug -rnug */
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000054 /* Don't allow more than one username */
Denis Vlasenko49622d72007-03-10 16:58:49 +000055 opt_complementary = "?1:?:u--g:g--u:r?ug:n?ug" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g");
56 flags = getopt32(argc, argv, "rnug" USE_SELINUX("Z"));
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000057
Eric Andersen7eb79ff2004-09-02 22:21:41 +000058 /* This values could be overwritten later */
59 uid = geteuid();
60 gid = getegid();
61 if (flags & PRINT_REAL) {
62 uid = getuid();
63 gid = getgid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +000064 }
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000065
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000066 if (argv[optind]) {
67 p = getpwnam(argv[optind]);
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000068 /* xuname2uid is needed because it exits on failure */
69 uid = xuname2uid(argv[optind]);
Eric Andersen7eb79ff2004-09-02 22:21:41 +000070 gid = p->pw_gid;
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000071 /* in this case PRINT_REAL is the same */
Erik Andersen94f5e0b2000-05-01 19:10:52 +000072 }
73
Denis Vlasenko49622d72007-03-10 16:58:49 +000074 if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) {
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000075 /* JUST_GROUP and JUST_USER are mutually exclusive */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +000076 if (flags & NAME_NOT_NUMBER) {
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000077 /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
78 puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000079 } else {
Denis Vlasenko49622d72007-03-10 16:58:49 +000080 if (flags & JUST_USER) {
81 printf("%u\n", uid);
82 }
83 if (flags & JUST_GROUP) {
84 printf("%u\n", gid);
85 }
86 }
87
88#if ENABLE_SELINUX
89 if (flags & JUST_CONTEXT) {
90 selinux_or_die();
91 if (argc - optind == 1) {
92 bb_error_msg_and_die("can't print security context when user specified");
93 }
94
95 if (getcon(&scontext)) {
96 bb_error_msg_and_die("can't get process context");
97 }
98 printf("%s\n", scontext);
99 }
100#endif
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000101 /* exit */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000102 fflush_stdout_and_exit(EXIT_SUCCESS);
Eric Andersenc1b8f122001-01-25 05:12:02 +0000103 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000104
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000105 /* Print full info like GNU id */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000106 /* bb_getpwuid doesn't exit on failure here */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000107 status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000108 putchar(' ');
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +0000109 /* bb_getgrgid doesn't exit on failure here */
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000110 status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
Rob Landley60158cb2005-05-03 06:25:50 +0000111
Denis Vlasenko49622d72007-03-10 16:58:49 +0000112#if ENABLE_SELINUX
Denis Vlasenkode59c0f2006-10-05 22:50:22 +0000113 if (is_selinux_enabled()) {
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000114 security_context_t mysid;
115 const char *context;
Rob Landley60158cb2005-05-03 06:25:50 +0000116
Denis Vlasenko7fa0fca2006-12-28 21:33:30 +0000117 context = "unknown";
118 getcon(&mysid);
119 if (mysid) {
120 context = alloca(strlen(mysid) + 1);
121 strcpy((char*)context, mysid);
122 freecon(mysid);
123 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000124 printf(" context=%s", context);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000125 }
126#endif
Rob Landley60158cb2005-05-03 06:25:50 +0000127
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000128 putchar('\n');
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000129 fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000130}