Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini id implementation for busybox |
| 4 | * |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 by Randolph Chung <tausq@debian.org> |
| 6 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */ |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 11 | /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 12 | * be more similar to GNU id. |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 13 | * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp> |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 14 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 17 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 18 | #define PRINT_REAL 1 |
| 19 | #define NAME_NOT_NUMBER 2 |
| 20 | #define JUST_USER 4 |
| 21 | #define JUST_GROUP 8 |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 22 | #if ENABLE_SELINUX |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 23 | #define JUST_CONTEXT 16 |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 24 | #endif |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 26 | static int printf_full(unsigned int id, const char *arg, const char prefix) |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 27 | { |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 28 | const char *fmt = "%cid=%u"; |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 29 | int status = EXIT_FAILURE; |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 31 | if (arg) { |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 32 | fmt = "%cid=%u(%s)"; |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 33 | status = EXIT_SUCCESS; |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 34 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 35 | printf(fmt, prefix, id, arg); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 36 | return status; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 37 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 39 | int id_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 40 | int id_main(int argc, char **argv) |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 41 | { |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 42 | struct passwd *p; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 43 | uid_t uid; |
| 44 | gid_t gid; |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 45 | unsigned long flags; |
| 46 | short status; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 47 | #if ENABLE_SELINUX |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 48 | security_context_t scontext; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 49 | #endif |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 50 | /* Don't allow -n -r -nr -ug -rug -nug -rnug */ |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 51 | /* Don't allow more than one username */ |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 52 | opt_complementary = "?1:u--g:g--u:r?ug:n?ug" USE_SELINUX(":u--Z:Z--u:g--Z:Z--g"); |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 53 | flags = getopt32(argv, "rnug" USE_SELINUX("Z")); |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 54 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 55 | /* This values could be overwritten later */ |
| 56 | uid = geteuid(); |
| 57 | gid = getegid(); |
| 58 | if (flags & PRINT_REAL) { |
| 59 | uid = getuid(); |
| 60 | gid = getgid(); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 61 | } |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 63 | if (argv[optind]) { |
| 64 | p = getpwnam(argv[optind]); |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 65 | /* xuname2uid is needed because it exits on failure */ |
| 66 | uid = xuname2uid(argv[optind]); |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 67 | gid = p->pw_gid; |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 68 | /* in this case PRINT_REAL is the same */ |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 71 | if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) { |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 72 | /* JUST_GROUP and JUST_USER are mutually exclusive */ |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 73 | if (flags & NAME_NOT_NUMBER) { |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 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)); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 76 | } else { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 77 | if (flags & JUST_USER) { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 78 | printf("%u\n", uid); |
| 79 | } |
| 80 | if (flags & JUST_GROUP) { |
| 81 | printf("%u\n", gid); |
| 82 | } |
| 83 | } |
| 84 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 85 | #if ENABLE_SELINUX |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 86 | if (flags & JUST_CONTEXT) { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 87 | selinux_or_die(); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 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 | } |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame^] | 95 | puts(scontext); |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 96 | } |
| 97 | #endif |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 98 | /* exit */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 99 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 100 | } |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 101 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 102 | /* Print full info like GNU id */ |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 103 | /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */ |
| 104 | status = printf_full(uid, bb_getpwuid(NULL, 0, uid), 'u'); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 105 | bb_putchar(' '); |
Denis Vlasenko | 3734b94 | 2007-07-27 11:20:10 +0000 | [diff] [blame] | 106 | status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), 'g'); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 108 | #if ENABLE_SELINUX |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 109 | if (is_selinux_enabled()) { |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 110 | security_context_t mysid; |
| 111 | const char *context; |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 113 | context = "unknown"; |
| 114 | getcon(&mysid); |
| 115 | if (mysid) { |
| 116 | context = alloca(strlen(mysid) + 1); |
| 117 | strcpy((char*)context, mysid); |
| 118 | freecon(mysid); |
| 119 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 120 | printf(" context=%s", context); |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 121 | } |
| 122 | #endif |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 124 | bb_putchar('\n'); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 125 | fflush_stdout_and_exit(status); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 126 | } |