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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | /* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */ |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 24 | /* 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^] | 25 | * be more similar to GNU id. |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 26 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 28 | #include "busybox.h" |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 29 | #include "pwd_.h" |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <unistd.h> |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 32 | #include <sys/types.h> |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 33 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 34 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 35 | #include <selinux/selinux.h> /* for is_selinux_enabled() */ |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 36 | #endif |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 37 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 38 | #define PRINT_REAL 1 |
| 39 | #define NAME_NOT_NUMBER 2 |
| 40 | #define JUST_USER 4 |
| 41 | #define JUST_GROUP 8 |
| 42 | |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 43 | 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^] | 44 | { |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 45 | const char *fmt = "%cid=%u"; |
| 46 | short status=EXIT_FAILURE; |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame^] | 47 | |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 48 | if(arg) { |
| 49 | fmt = "%cid=%u(%s)"; |
| 50 | status=EXIT_SUCCESS; |
| 51 | } |
| 52 | bb_printf(fmt, prefix, id, arg); |
| 53 | return status; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 54 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 56 | extern int id_main(int argc, char **argv) |
| 57 | { |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 58 | struct passwd *p; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 59 | uid_t uid; |
| 60 | gid_t gid; |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 61 | unsigned long flags; |
| 62 | short status; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 63 | |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame^] | 64 | /* Don't allow -n -r -nr -ug -rug -nug -rnug */ |
| 65 | bb_opt_complementally = "?u~g:g~u:r?ug:n?ug"; |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 66 | flags = bb_getopt_ulflags(argc, argv, "rnug"); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 67 | |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 68 | /* Don't allow more than one username */ |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame^] | 69 | if (argc > (optind + 1)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | bb_show_usage(); |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame^] | 71 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 72 | /* This values could be overwritten later */ |
| 73 | uid = geteuid(); |
| 74 | gid = getegid(); |
| 75 | if (flags & PRINT_REAL) { |
| 76 | uid = getuid(); |
| 77 | gid = getgid(); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 78 | } |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame^] | 79 | |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 80 | if(argv[optind]) { |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 81 | p=getpwnam(argv[optind]); |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 82 | /* bb_xgetpwnam is needed because it exits on failure */ |
| 83 | uid = bb_xgetpwnam(argv[optind]); |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 84 | gid = p->pw_gid; |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame^] | 85 | /* in this case PRINT_REAL is the same */ |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 88 | if(flags & (JUST_GROUP | JUST_USER)) { |
| 89 | /* JUST_GROUP and JUST_USER are mutually exclusive */ |
| 90 | if(flags & NAME_NOT_NUMBER) { |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 91 | /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */ |
| 92 | 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] | 93 | } else { |
| 94 | bb_printf("%u\n",(flags & JUST_USER) ? uid : gid); |
| 95 | } |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame^] | 96 | /* exit */ |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 97 | bb_fflush_stdout_and_exit(EXIT_SUCCESS); |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 98 | } |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 99 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 100 | /* Print full info like GNU id */ |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 101 | /* bb_getpwuid doesn't exit on failure here */ |
| 102 | status=printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u'); |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 103 | putchar(' '); |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 104 | /* bb_getgrgid doesn't exit on failure here */ |
| 105 | status|=printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g'); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 106 | |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 107 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 108 | if ( is_selinux_enabled() ) { |
| 109 | security_context_t mysid; |
| 110 | char context[80]; |
| 111 | int len = sizeof(context); |
| 112 | |
| 113 | getcon(&mysid); |
| 114 | context[0] = '\0'; |
| 115 | if (mysid) { |
| 116 | len = strlen(mysid)+1; |
| 117 | safe_strncpy(context, mysid, len); |
| 118 | freecon(mysid); |
| 119 | }else{ |
| 120 | safe_strncpy(context, "unknown",8); |
| 121 | } |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 122 | bb_printf(" context=%s", context); |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 123 | } |
| 124 | #endif |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 125 | |
Glenn L McGrath | f15dfc5 | 2004-09-15 03:04:08 +0000 | [diff] [blame] | 126 | putchar('\n'); |
| 127 | bb_fflush_stdout_and_exit(status); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 128 | } |
Eric Andersen | 7eb79ff | 2004-09-02 22:21:41 +0000 | [diff] [blame] | 129 | |
| 130 | /* END CODE */ |
| 131 | /* |
| 132 | Local Variables: |
| 133 | c-file-style: "linux" |
| 134 | c-basic-offset: 4 |
| 135 | tab-width: 4 |
| 136 | End: |
| 137 | */ |
| 138 | |