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. */ |
| 24 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 25 | #include "busybox.h" |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <unistd.h> |
Mark Whitley | 99e370f | 2000-07-20 00:03:38 +0000 | [diff] [blame] | 28 | #include <getopt.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 29 | #include <string.h> |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 31 | #ifdef CONFIG_SELINUX |
| 32 | #include <proc_secure.h> |
| 33 | #include <flask_util.h> |
| 34 | #endif |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 36 | #define JUST_USER 1 |
| 37 | #define JUST_GROUP 2 |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | #define PRINT_REAL 4 |
| 39 | #define NAME_NOT_NUMBER 8 |
| 40 | |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 41 | extern int id_main(int argc, char **argv) |
| 42 | { |
Eric Andersen | bd193a4 | 2000-12-13 01:52:39 +0000 | [diff] [blame] | 43 | char user[9], group[9]; |
Eric Andersen | 6150df3 | 2000-07-28 16:22:51 +0000 | [diff] [blame] | 44 | long pwnam, grnam; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | int uid, gid; |
| 46 | int flags; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 47 | #ifdef CONFIG_SELINUX |
| 48 | int is_flask_enabled_flag = is_flask_enabled(); |
| 49 | #endif |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 50 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | flags = bb_getopt_ulflags(argc, argv, "ugrn"); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 52 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 53 | if (((flags & (JUST_USER | JUST_GROUP)) == (JUST_USER | JUST_GROUP)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | || (argc > optind + 1) |
| 55 | ) { |
| 56 | bb_show_usage(); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Eric Andersen | bd193a4 | 2000-12-13 01:52:39 +0000 | [diff] [blame] | 59 | if (argv[optind] == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | if (flags & PRINT_REAL) { |
| 61 | uid = getuid(); |
| 62 | gid = getgid(); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 63 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | uid = geteuid(); |
| 65 | gid = getegid(); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 66 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | my_getpwuid(user, uid); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 68 | } else { |
Eric Andersen | 02e6ba9 | 2002-09-30 20:39:56 +0000 | [diff] [blame] | 69 | safe_strncpy(user, argv[optind], sizeof(user)); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 70 | gid = my_getpwnamegid(user); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 71 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | my_getgrgid(group, gid); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 73 | |
Eric Andersen | 6150df3 | 2000-07-28 16:22:51 +0000 | [diff] [blame] | 74 | pwnam=my_getpwnam(user); |
| 75 | grnam=my_getgrnam(group); |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 76 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 77 | if (flags & (JUST_GROUP | JUST_USER)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | char *s = group; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 79 | if (flags & JUST_USER) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | s = user; |
| 81 | grnam = pwnam; |
| 82 | } |
| 83 | if (flags & NAME_NOT_NUMBER) { |
| 84 | puts(s); |
| 85 | } else { |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 86 | printf("%ld\n", grnam); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | } |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 88 | } else { |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 89 | #ifdef CONFIG_SELINUX |
| 90 | printf("uid=%ld(%s) gid=%ld(%s)", pwnam, user, grnam, group); |
| 91 | if(is_flask_enabled_flag) |
| 92 | { |
| 93 | security_id_t mysid = getsecsid(); |
| 94 | char context[80]; |
| 95 | int len = sizeof(context); |
| 96 | context[0] = '\0'; |
| 97 | if(security_sid_to_context(mysid, context, &len)) |
| 98 | strcpy(context, "unknown"); |
| 99 | printf(" context=%s\n", context); |
| 100 | } |
| 101 | else |
| 102 | printf("\n"); |
| 103 | #else |
Eric Andersen | 6150df3 | 2000-07-28 16:22:51 +0000 | [diff] [blame] | 104 | printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
Eric Andersen | c1b8f12 | 2001-01-25 05:12:02 +0000 | [diff] [blame] | 107 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 108 | |
| 109 | bb_fflush_stdout_and_exit(0); |
Erik Andersen | 94f5e0b | 2000-05-01 19:10:52 +0000 | [diff] [blame] | 110 | } |