Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * chcon -- change security context, based on coreutils-5.97-13 |
| 3 | * |
| 4 | * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp> |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 5 | * |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006 - 2007 KaiGai Kohei <kaigai@kaigai.gr.jp> |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 10 | //config:config CHCON |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 11 | //config: bool "chcon (8.9 kb)" |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 12 | //config: default n |
| 13 | //config: depends on SELINUX |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: Enable support to change the security context of file. |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 16 | |
| 17 | //applet:IF_CHCON(APPLET(chcon, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 18 | |
| 19 | //kbuild:lib-$(CONFIG_CHCON) += chcon.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 20 | |
| 21 | //usage:#define chcon_trivial_usage |
| 22 | //usage: "[OPTIONS] CONTEXT FILE..." |
| 23 | //usage: "\n chcon [OPTIONS] [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE..." |
Xabier Oneca | 5856dc7 | 2017-08-09 12:18:36 +0200 | [diff] [blame] | 24 | //usage: IF_LONG_OPTS( |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage: "\n chcon [OPTIONS] --reference=RFILE FILE..." |
Xabier Oneca | 5856dc7 | 2017-08-09 12:18:36 +0200 | [diff] [blame] | 26 | //usage: ) |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 27 | //usage: |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage:#define chcon_full_usage "\n\n" |
| 29 | //usage: "Change the security context of each FILE to CONTEXT\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 30 | //usage: "\n -v Verbose" |
| 31 | //usage: "\n -c Report changes made" |
| 32 | //usage: "\n -h Affect symlinks instead of their targets" |
| 33 | //usage: "\n -f Suppress most error messages" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 34 | //usage: IF_LONG_OPTS( |
| 35 | //usage: "\n --reference RFILE Use RFILE's group instead of using a CONTEXT value" |
| 36 | //usage: ) |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 37 | //usage: "\n -u USER Set user/role/type/range in the target security context" |
| 38 | //usage: "\n -r ROLE" |
| 39 | //usage: "\n -t TYPE" |
| 40 | //usage: "\n -l RNG" |
| 41 | //usage: "\n -R Recurse" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 42 | |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 43 | #include <selinux/context.h> |
| 44 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 45 | #include "libbb.h" |
| 46 | |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 47 | #define OPT_RECURSIVE (1<<0) /* 'R' */ |
| 48 | #define OPT_CHANHES (1<<1) /* 'c' */ |
| 49 | #define OPT_NODEREFERENCE (1<<2) /* 'h' */ |
| 50 | #define OPT_QUIET (1<<3) /* 'f' */ |
| 51 | #define OPT_USER (1<<4) /* 'u' */ |
| 52 | #define OPT_ROLE (1<<5) /* 'r' */ |
| 53 | #define OPT_TYPE (1<<6) /* 't' */ |
| 54 | #define OPT_RANGE (1<<7) /* 'l' */ |
| 55 | #define OPT_VERBOSE (1<<8) /* 'v' */ |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 56 | #define OPT_REFERENCE ((1<<9) * ENABLE_LONG_OPTS) |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 57 | #define OPT_COMPONENT_SPECIFIED (OPT_USER | OPT_ROLE | OPT_TYPE | OPT_RANGE) |
| 58 | |
| 59 | static char *user = NULL; |
| 60 | static char *role = NULL; |
| 61 | static char *type = NULL; |
| 62 | static char *range = NULL; |
| 63 | static char *specified_context = NULL; |
| 64 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 65 | static int FAST_FUNC change_filedir_context( |
Denis Vlasenko | 592d4fe | 2008-03-17 09:19:26 +0000 | [diff] [blame] | 66 | const char *fname, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 67 | struct stat *stbuf UNUSED_PARAM, |
| 68 | void *userData UNUSED_PARAM, |
| 69 | int depth UNUSED_PARAM) |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 70 | { |
| 71 | context_t context = NULL; |
| 72 | security_context_t file_context = NULL; |
| 73 | security_context_t context_string; |
| 74 | int rc = FALSE; |
| 75 | int status = 0; |
| 76 | |
| 77 | if (option_mask32 & OPT_NODEREFERENCE) { |
| 78 | status = lgetfilecon(fname, &file_context); |
| 79 | } else { |
| 80 | status = getfilecon(fname, &file_context); |
| 81 | } |
| 82 | if (status < 0 && errno != ENODATA) { |
| 83 | if ((option_mask32 & OPT_QUIET) == 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 84 | bb_error_msg("can't obtain security context: %s", fname); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 85 | goto skip; |
| 86 | } |
| 87 | |
| 88 | if (file_context == NULL && specified_context == NULL) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 89 | bb_error_msg("can't apply partial context to unlabeled file %s", fname); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 90 | goto skip; |
| 91 | } |
| 92 | |
| 93 | if (specified_context == NULL) { |
| 94 | context = set_security_context_component(file_context, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 95 | user, role, type, range); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 96 | if (!context) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 97 | bb_error_msg("can't compute security context from %s", file_context); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 98 | goto skip; |
| 99 | } |
| 100 | } else { |
| 101 | context = context_new(specified_context); |
| 102 | if (!context) { |
| 103 | bb_error_msg("invalid context: %s", specified_context); |
| 104 | goto skip; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | context_string = context_str(context); |
| 109 | if (!context_string) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 110 | bb_error_msg("can't obtain security context in text expression"); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 111 | goto skip; |
| 112 | } |
| 113 | |
| 114 | if (file_context == NULL || strcmp(context_string, file_context) != 0) { |
| 115 | int fail; |
| 116 | |
| 117 | if (option_mask32 & OPT_NODEREFERENCE) { |
| 118 | fail = lsetfilecon(fname, context_string); |
| 119 | } else { |
| 120 | fail = setfilecon(fname, context_string); |
| 121 | } |
| 122 | if ((option_mask32 & OPT_VERBOSE) || ((option_mask32 & OPT_CHANHES) && !fail)) { |
| 123 | printf(!fail |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 124 | ? "context of %s changed to %s\n" |
| 125 | : "can't change context of %s to %s\n", |
| 126 | fname, context_string); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 127 | } |
| 128 | if (!fail) { |
| 129 | rc = TRUE; |
| 130 | } else if ((option_mask32 & OPT_QUIET) == 0) { |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 131 | bb_error_msg("can't change context of %s to %s", |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 132 | fname, context_string); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 133 | } |
sagivd | 8f1ae25 | 2018-08-25 13:49:24 +0300 | [diff] [blame] | 134 | } else { |
| 135 | if (option_mask32 & OPT_VERBOSE) { |
| 136 | printf("context of %s retained as %s\n", fname, context_string); |
| 137 | } |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 138 | rc = TRUE; |
| 139 | } |
| 140 | skip: |
Denis Vlasenko | b5c33b1 | 2007-03-12 19:49:07 +0000 | [diff] [blame] | 141 | context_free(context); |
| 142 | freecon(file_context); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 143 | |
| 144 | return rc; |
| 145 | } |
| 146 | |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 147 | #if ENABLE_LONG_OPTS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 148 | static const char chcon_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 149 | "recursive\0" No_argument "R" |
| 150 | "changes\0" No_argument "c" |
| 151 | "no-dereference\0" No_argument "h" |
| 152 | "silent\0" No_argument "f" |
| 153 | "quiet\0" No_argument "f" |
| 154 | "user\0" Required_argument "u" |
| 155 | "role\0" Required_argument "r" |
| 156 | "type\0" Required_argument "t" |
| 157 | "range\0" Required_argument "l" |
| 158 | "verbose\0" No_argument "v" |
| 159 | "reference\0" Required_argument "\xff" /* no short option */ |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 160 | ; |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 161 | #endif |
| 162 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 163 | int chcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 164 | int chcon_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 165 | { |
| 166 | char *reference_file; |
| 167 | char *fname; |
| 168 | int i, errors = 0; |
| 169 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 170 | getopt32long(argv, "^" |
| 171 | "Rchfu:r:t:l:v" |
| 172 | "\0" |
| 173 | "-1" /* at least 1 arg */ |
| 174 | ":?" /* error if exclusivity constraints are violated */ |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 175 | #if ENABLE_LONG_OPTS |
Denis Vlasenko | 955bccc | 2007-03-12 10:41:23 +0000 | [diff] [blame] | 176 | ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff" |
| 177 | #endif |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 178 | ":f--v:v--f" /* 'verbose' and 'quiet' are exclusive */ |
| 179 | , chcon_longopts, |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 180 | &user, &role, &type, &range, &reference_file |
| 181 | ); |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 182 | argv += optind; |
| 183 | |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 184 | #if ENABLE_LONG_OPTS |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 185 | if (option_mask32 & OPT_REFERENCE) { |
| 186 | /* FIXME: lgetfilecon() should be used when '-h' is specified. |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 187 | * But current implementation follows the original one. */ |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 188 | if (getfilecon(reference_file, &specified_context) < 0) |
| 189 | bb_perror_msg_and_die("getfilecon('%s') failed", reference_file); |
| 190 | } else |
| 191 | #endif |
| 192 | if ((option_mask32 & OPT_COMPONENT_SPECIFIED) == 0) { |
| 193 | specified_context = *argv++; |
| 194 | /* specified_context is never NULL - |
| 195 | * "-1" in opt_complementary prevents this. */ |
| 196 | if (!argv[0]) |
| 197 | bb_error_msg_and_die("too few arguments"); |
| 198 | } |
| 199 | |
| 200 | for (i = 0; (fname = argv[i]) != NULL; i++) { |
| 201 | int fname_len = strlen(fname); |
| 202 | while (fname_len > 1 && fname[fname_len - 1] == '/') |
| 203 | fname_len--; |
| 204 | fname[fname_len] = '\0'; |
| 205 | |
| 206 | if (recursive_action(fname, |
sagivd | 8f1ae25 | 2018-08-25 13:49:24 +0300 | [diff] [blame] | 207 | ((option_mask32 & OPT_RECURSIVE) ? ACTION_RECURSIVE : 0), |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 208 | change_filedir_context, |
| 209 | change_filedir_context, |
| 210 | NULL, 0) != TRUE) |
Denis Vlasenko | 1203c9b | 2007-03-11 22:16:02 +0000 | [diff] [blame] | 211 | errors = 1; |
| 212 | } |
| 213 | return errors; |
| 214 | } |