source: MondoRescue/branches/2.2.5/mindi-busybox/coreutils/id.c@ 2142

Last change on this file since 2142 was 1765, checked in by Bruno Cornec, 17 years ago

Update to busybox 1.7.2

File size: 3.0 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * Mini id implementation for busybox
4 *
5 * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
11/* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
12 * be more similar to GNU id.
[1765]13 * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
[821]14 */
15
[1765]16#include "libbb.h"
[821]17
18#define PRINT_REAL 1
19#define NAME_NOT_NUMBER 2
20#define JUST_USER 4
21#define JUST_GROUP 8
[1765]22#if ENABLE_SELINUX
23#define JUST_CONTEXT 16
24#endif
[821]25
[1765]26static int printf_full(unsigned int id, const char *arg, const char prefix)
[821]27{
28 const char *fmt = "%cid=%u";
[1765]29 int status = EXIT_FAILURE;
[821]30
[1765]31 if (arg) {
[821]32 fmt = "%cid=%u(%s)";
[1765]33 status = EXIT_SUCCESS;
[821]34 }
[1765]35 printf(fmt, prefix, id, arg);
[821]36 return status;
37}
38
[1765]39int id_main(int argc, char **argv);
[821]40int id_main(int argc, char **argv)
41{
42 struct passwd *p;
43 uid_t uid;
44 gid_t gid;
45 unsigned long flags;
46 short status;
[1765]47#if ENABLE_SELINUX
48 security_context_t scontext;
49#endif
[821]50 /* Don't allow -n -r -nr -ug -rug -nug -rnug */
51 /* Don't allow more than one username */
[1765]52 opt_complementary = "?1:u--g:g--u:r?ug:n?ug" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g");
53 flags = getopt32(argv, "rnug" USE_SELINUX("Z"));
[821]54
55 /* This values could be overwritten later */
56 uid = geteuid();
57 gid = getegid();
58 if (flags & PRINT_REAL) {
59 uid = getuid();
60 gid = getgid();
61 }
62
[1765]63 if (argv[optind]) {
64 p = getpwnam(argv[optind]);
65 /* xuname2uid is needed because it exits on failure */
66 uid = xuname2uid(argv[optind]);
[821]67 gid = p->pw_gid;
68 /* in this case PRINT_REAL is the same */
69 }
70
[1765]71 if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) {
[821]72 /* JUST_GROUP and JUST_USER are mutually exclusive */
[1765]73 if (flags & NAME_NOT_NUMBER) {
74 /* bb_getXXXid(-1) exit on failure, puts cannot segfault */
75 puts((flags & JUST_USER) ? bb_getpwuid(NULL, -1, uid) : bb_getgrgid(NULL, -1, gid));
[821]76 } else {
[1765]77 if (flags & JUST_USER) {
78 printf("%u\n", uid);
79 }
80 if (flags & JUST_GROUP) {
81 printf("%u\n", gid);
82 }
[821]83 }
[1765]84
85#if ENABLE_SELINUX
86 if (flags & JUST_CONTEXT) {
87 selinux_or_die();
88 if (argc - optind == 1) {
89 bb_error_msg_and_die("user name can't be passed with -Z");
90 }
91
92 if (getcon(&scontext)) {
93 bb_error_msg_and_die("can't get process context");
94 }
95 printf("%s\n", scontext);
96 }
97#endif
[821]98 /* exit */
[1765]99 fflush_stdout_and_exit(EXIT_SUCCESS);
[821]100 }
101
102 /* Print full info like GNU id */
[1765]103 /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */
104 status = printf_full(uid, bb_getpwuid(NULL, 0, uid), 'u');
[821]105 putchar(' ');
[1765]106 status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), 'g');
[821]107
[1765]108#if ENABLE_SELINUX
109 if (is_selinux_enabled()) {
110 security_context_t mysid;
111 const char *context;
[821]112
[1765]113 context = "unknown";
114 getcon(&mysid);
115 if (mysid) {
116 context = alloca(strlen(mysid) + 1);
117 strcpy((char*)context, mysid);
118 freecon(mysid);
119 }
120 printf(" context=%s", context);
[821]121 }
122#endif
123
124 putchar('\n');
[1765]125 fflush_stdout_and_exit(status);
[821]126}
Note: See TracBrowser for help on using the repository browser.