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. |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 13 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 14 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <unistd.h> |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 18 | #include <sys/types.h> |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 19 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 20 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 21 | #include <selinux/selinux.h> /* for is_selinux_enabled() */ |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 22 | #endif |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 23 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 24 | #define PRINT_REAL 1 |
| 25 | #define NAME_NOT_NUMBER 2 |
| 26 | #define JUST_USER 4 |
| 27 | #define JUST_GROUP 8 |
| 28 | |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 29 | static short printf_full(unsigned int id, const char *arg, const char prefix) |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 30 | { |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 31 | const char *fmt = "%cid=%u"; |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 32 | short status = EXIT_FAILURE; |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 34 | if (arg) { |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 35 | fmt = "%cid=%u(%s)"; |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 36 | status = EXIT_SUCCESS; |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 37 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 38 | printf(fmt, prefix, id, arg); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 39 | return status; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 40 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 42 | int id_main(int argc, char **argv) |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 43 | { |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 44 | struct passwd *p; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 45 | uid_t uid; |
| 46 | gid_t gid; |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 47 | unsigned long flags; |
| 48 | short status; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 49 | |
"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 | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 52 | opt_complementary = "?1:?:u--g:g--u:r?ug:n?ug"; |
| 53 | flags = getopt32(argc, argv, "rnug"); |
"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 | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 71 | if (flags & (JUST_GROUP | JUST_USER)) { |
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) { |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 74 | /* 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 McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 76 | } else { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 77 | printf("%u\n", (flags & JUST_USER) ? uid : gid); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 78 | } |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 79 | /* exit */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 80 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 81 | } |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 82 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 83 | /* Print full info like GNU id */ |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 84 | /* bb_getpwuid doesn't exit on failure here */ |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 85 | status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u'); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 86 | putchar(' '); |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 87 | /* bb_getgrgid doesn't exit on failure here */ |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 88 | status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g'); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 89 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 90 | #ifdef CONFIG_SELINUX |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 91 | if (is_selinux_enabled()) { |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame^] | 92 | security_context_t mysid; |
| 93 | const char *context; |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame^] | 95 | context = "unknown"; |
| 96 | getcon(&mysid); |
| 97 | if (mysid) { |
| 98 | context = alloca(strlen(mysid) + 1); |
| 99 | strcpy((char*)context, mysid); |
| 100 | freecon(mysid); |
| 101 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 102 | printf(" context=%s", context); |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 103 | } |
| 104 | #endif |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 105 | |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 106 | putchar('\n'); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 107 | fflush_stdout_and_exit(status); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 108 | } |