blob: 1bedff37aad5754ab8ac7e62d42928521649b412 [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"
Eric Andersen7eb79ff2004-09-02 22:21:41 +000016#include "pwd_.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 Andersen9e480452003-07-03 10:07:04 +000021#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000022#include <selinux/selinux.h> /* for is_selinux_enabled() */
Eric Andersen9e480452003-07-03 10:07:04 +000023#endif
Erik Andersen94f5e0b2000-05-01 19:10:52 +000024
Eric Andersen7eb79ff2004-09-02 22:21:41 +000025#define PRINT_REAL 1
26#define NAME_NOT_NUMBER 2
27#define JUST_USER 4
28#define JUST_GROUP 8
29
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000030static short printf_full(unsigned int id, const char *arg, const char prefix)
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000031{
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000032 const char *fmt = "%cid=%u";
33 short status=EXIT_FAILURE;
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000034
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000035 if(arg) {
36 fmt = "%cid=%u(%s)";
37 status=EXIT_SUCCESS;
38 }
39 bb_printf(fmt, prefix, id, arg);
40 return status;
Eric Andersen7eb79ff2004-09-02 22:21:41 +000041}
Manuel Novoa III cad53642003-03-19 09:13:01 +000042
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;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000051 /* Don't allow -n -r -nr -ug -rug -nug -rnug */
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000052 /* Don't allow more than one username */
"Vladimir N. Oleynik"f704b272005-10-14 09:56:52 +000053 bb_opt_complementally = "?1:?:u--g:g--u:r?ug:n?ug";
54 flags = bb_getopt_ulflags(argc, argv, "rnug");
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000055
Eric Andersen7eb79ff2004-09-02 22:21:41 +000056 /* This values could be overwritten later */
57 uid = geteuid();
58 gid = getegid();
59 if (flags & PRINT_REAL) {
60 uid = getuid();
61 gid = getgid();
Erik Andersen94f5e0b2000-05-01 19:10:52 +000062 }
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000063
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000064 if(argv[optind]) {
Eric Andersen7eb79ff2004-09-02 22:21:41 +000065 p=getpwnam(argv[optind]);
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000066 /* bb_xgetpwnam is needed because it exits on failure */
67 uid = bb_xgetpwnam(argv[optind]);
Eric Andersen7eb79ff2004-09-02 22:21:41 +000068 gid = p->pw_gid;
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000069 /* in this case PRINT_REAL is the same */
Erik Andersen94f5e0b2000-05-01 19:10:52 +000070 }
71
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000072 if(flags & (JUST_GROUP | JUST_USER)) {
73 /* JUST_GROUP and JUST_USER are mutually exclusive */
74 if(flags & NAME_NOT_NUMBER) {
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000075 /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */
76 puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 ));
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000077 } else {
78 bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
79 }
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +000080 /* exit */
Eric Andersen7eb79ff2004-09-02 22:21:41 +000081 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
Eric Andersenc1b8f122001-01-25 05:12:02 +000082 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000083
Eric Andersen7eb79ff2004-09-02 22:21:41 +000084 /* Print full info like GNU id */
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000085 /* bb_getpwuid doesn't exit on failure here */
86 status=printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u');
Glenn L McGrathf15dfc52004-09-15 03:04:08 +000087 putchar(' ');
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000088 /* bb_getgrgid doesn't exit on failure here */
89 status|=printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
Rob Landley60158cb2005-05-03 06:25:50 +000090
Eric Andersen7eb79ff2004-09-02 22:21:41 +000091#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000092 if ( is_selinux_enabled() ) {
93 security_context_t mysid;
94 char context[80];
95 int len = sizeof(context);
96
97 getcon(&mysid);
98 context[0] = '\0';
99 if (mysid) {
100 len = strlen(mysid)+1;
101 safe_strncpy(context, mysid, len);
102 freecon(mysid);
103 }else{
104 safe_strncpy(context, "unknown",8);
105 }
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000106 bb_printf(" context=%s", context);
Eric Andersen7eb79ff2004-09-02 22:21:41 +0000107 }
108#endif
Rob Landley60158cb2005-05-03 06:25:50 +0000109
Glenn L McGrathf15dfc52004-09-15 03:04:08 +0000110 putchar('\n');
111 bb_fflush_stdout_and_exit(status);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000112}