blob: e77e9ded5f5a60cd667d9e2a70487fcf66cbf053 [file] [log] [blame]
Denis Vlasenko1203c9b2007-03-11 22:16:02 +00001/*
2 * chcon -- change security context, based on coreutils-5.97-13
3 *
4 * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
Denis Vlasenkoc86e0522007-03-20 11:30:28 +00005 *
Denis Vlasenko1203c9b2007-03-11 22:16:02 +00006 * Copyright (C) 2006 - 2007 KaiGai Kohei <kaigai@kaigai.gr.jp>
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko1203c9b2007-03-11 22:16:02 +00009 */
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +010010//config:config CHCON
Denys Vlasenkoae178ce2017-07-19 14:32:54 +020011//config: bool "chcon (8.9 kb)"
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +010012//config: default n
13//config: depends on SELINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: Enable support to change the security context of file.
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +010016
17//applet:IF_CHCON(APPLET(chcon, BB_DIR_USR_BIN, BB_SUID_DROP))
18
19//kbuild:lib-$(CONFIG_CHCON) += chcon.o
Pere Orga5bc8c002011-04-11 03:29:49 +020020
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..."
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage: "\n chcon [OPTIONS] --reference=RFILE FILE..."
Denys Vlasenko036585a2017-08-08 16:38:18 +020025//usage:
Pere Orga5bc8c002011-04-11 03:29:49 +020026//usage:#define chcon_full_usage "\n\n"
27//usage: "Change the security context of each FILE to CONTEXT\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage: "\n -v Verbose"
29//usage: "\n -c Report changes made"
30//usage: "\n -h Affect symlinks instead of their targets"
31//usage: "\n -f Suppress most error messages"
Denys Vlasenko036585a2017-08-08 16:38:18 +020032//usage: IF_LONG_OPTS(
33//usage: "\n --reference RFILE Use RFILE's group instead of using a CONTEXT value"
34//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +020035//usage: "\n -u USER Set user/role/type/range in the target security context"
36//usage: "\n -r ROLE"
37//usage: "\n -t TYPE"
38//usage: "\n -l RNG"
39//usage: "\n -R Recurse"
Pere Orga5bc8c002011-04-11 03:29:49 +020040
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000041#include <selinux/context.h>
42
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000043#include "libbb.h"
44
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000045#define OPT_RECURSIVE (1<<0) /* 'R' */
46#define OPT_CHANHES (1<<1) /* 'c' */
47#define OPT_NODEREFERENCE (1<<2) /* 'h' */
48#define OPT_QUIET (1<<3) /* 'f' */
49#define OPT_USER (1<<4) /* 'u' */
50#define OPT_ROLE (1<<5) /* 'r' */
51#define OPT_TYPE (1<<6) /* 't' */
52#define OPT_RANGE (1<<7) /* 'l' */
53#define OPT_VERBOSE (1<<8) /* 'v' */
Denys Vlasenko036585a2017-08-08 16:38:18 +020054#define OPT_REFERENCE ((1<<9) * ENABLE_LONG_OPTS)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000055#define OPT_COMPONENT_SPECIFIED (OPT_USER | OPT_ROLE | OPT_TYPE | OPT_RANGE)
56
57static char *user = NULL;
58static char *role = NULL;
59static char *type = NULL;
60static char *range = NULL;
61static char *specified_context = NULL;
62
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000063static int FAST_FUNC change_filedir_context(
Denis Vlasenko592d4fe2008-03-17 09:19:26 +000064 const char *fname,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000065 struct stat *stbuf UNUSED_PARAM,
66 void *userData UNUSED_PARAM,
67 int depth UNUSED_PARAM)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000068{
69 context_t context = NULL;
70 security_context_t file_context = NULL;
71 security_context_t context_string;
72 int rc = FALSE;
73 int status = 0;
74
75 if (option_mask32 & OPT_NODEREFERENCE) {
76 status = lgetfilecon(fname, &file_context);
77 } else {
78 status = getfilecon(fname, &file_context);
79 }
80 if (status < 0 && errno != ENODATA) {
81 if ((option_mask32 & OPT_QUIET) == 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +010082 bb_error_msg("can't obtain security context: %s", fname);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000083 goto skip;
84 }
85
86 if (file_context == NULL && specified_context == NULL) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010087 bb_error_msg("can't apply partial context to unlabeled file %s", fname);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000088 goto skip;
89 }
90
91 if (specified_context == NULL) {
92 context = set_security_context_component(file_context,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010093 user, role, type, range);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000094 if (!context) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010095 bb_error_msg("can't compute security context from %s", file_context);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000096 goto skip;
97 }
98 } else {
99 context = context_new(specified_context);
100 if (!context) {
101 bb_error_msg("invalid context: %s", specified_context);
102 goto skip;
103 }
104 }
105
106 context_string = context_str(context);
107 if (!context_string) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100108 bb_error_msg("can't obtain security context in text expression");
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000109 goto skip;
110 }
111
112 if (file_context == NULL || strcmp(context_string, file_context) != 0) {
113 int fail;
114
115 if (option_mask32 & OPT_NODEREFERENCE) {
116 fail = lsetfilecon(fname, context_string);
117 } else {
118 fail = setfilecon(fname, context_string);
119 }
120 if ((option_mask32 & OPT_VERBOSE) || ((option_mask32 & OPT_CHANHES) && !fail)) {
121 printf(!fail
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100122 ? "context of %s changed to %s\n"
123 : "can't change context of %s to %s\n",
124 fname, context_string);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000125 }
126 if (!fail) {
127 rc = TRUE;
128 } else if ((option_mask32 & OPT_QUIET) == 0) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100129 bb_error_msg("can't change context of %s to %s",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100130 fname, context_string);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000131 }
132 } else if (option_mask32 & OPT_VERBOSE) {
133 printf("context of %s retained as %s\n", fname, context_string);
134 rc = TRUE;
135 }
136skip:
Denis Vlasenkob5c33b12007-03-12 19:49:07 +0000137 context_free(context);
138 freecon(file_context);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000139
140 return rc;
141}
142
Denys Vlasenko036585a2017-08-08 16:38:18 +0200143#if ENABLE_LONG_OPTS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000144static const char chcon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000145 "recursive\0" No_argument "R"
146 "changes\0" No_argument "c"
147 "no-dereference\0" No_argument "h"
148 "silent\0" No_argument "f"
149 "quiet\0" No_argument "f"
150 "user\0" Required_argument "u"
151 "role\0" Required_argument "r"
152 "type\0" Required_argument "t"
153 "range\0" Required_argument "l"
154 "verbose\0" No_argument "v"
155 "reference\0" Required_argument "\xff" /* no short option */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000156 ;
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000157#endif
158
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000159int chcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000160int chcon_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000161{
162 char *reference_file;
163 char *fname;
164 int i, errors = 0;
165
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200166 getopt32long(argv, "^"
167 "Rchfu:r:t:l:v"
168 "\0"
169 "-1" /* at least 1 arg */
170 ":?" /* error if exclusivity constraints are violated */
Denys Vlasenko036585a2017-08-08 16:38:18 +0200171#if ENABLE_LONG_OPTS
Denis Vlasenko955bccc2007-03-12 10:41:23 +0000172 ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff"
173#endif
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200174 ":f--v:v--f" /* 'verbose' and 'quiet' are exclusive */
175 , chcon_longopts,
Denys Vlasenko036585a2017-08-08 16:38:18 +0200176 &user, &role, &type, &range, &reference_file
177 );
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000178 argv += optind;
179
Denys Vlasenko036585a2017-08-08 16:38:18 +0200180#if ENABLE_LONG_OPTS
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000181 if (option_mask32 & OPT_REFERENCE) {
182 /* FIXME: lgetfilecon() should be used when '-h' is specified.
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100183 * But current implementation follows the original one. */
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000184 if (getfilecon(reference_file, &specified_context) < 0)
185 bb_perror_msg_and_die("getfilecon('%s') failed", reference_file);
186 } else
187#endif
188 if ((option_mask32 & OPT_COMPONENT_SPECIFIED) == 0) {
189 specified_context = *argv++;
190 /* specified_context is never NULL -
191 * "-1" in opt_complementary prevents this. */
192 if (!argv[0])
193 bb_error_msg_and_die("too few arguments");
194 }
195
196 for (i = 0; (fname = argv[i]) != NULL; i++) {
197 int fname_len = strlen(fname);
198 while (fname_len > 1 && fname[fname_len - 1] == '/')
199 fname_len--;
200 fname[fname_len] = '\0';
201
202 if (recursive_action(fname,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100203 1<<option_mask32 & OPT_RECURSIVE,
204 change_filedir_context,
205 change_filedir_context,
206 NULL, 0) != TRUE)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000207 errors = 1;
208 }
209 return errors;
210}