2 * chcon -- change security context, based on coreutils-5.97-13
4 * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
6 * Copyright (C) 2006 - 2007 KaiGai Kohei <kaigai@kaigai.gr.jp>
8 * Licensed under GPLv2, see file LICENSE in this source tree.
11 //config: bool "chcon (8.9 kb)"
13 //config: depends on SELINUX
15 //config: Enable support to change the security context of file.
17 //applet:IF_CHCON(APPLET(chcon, BB_DIR_USR_BIN, BB_SUID_DROP))
19 //kbuild:lib-$(CONFIG_CHCON) += chcon.o
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..."
24 //usage: "\n chcon [OPTIONS] --reference=RFILE FILE..."
26 //usage:#define chcon_full_usage "\n\n"
27 //usage: "Change the security context of each FILE to CONTEXT\n"
28 //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"
32 //usage: IF_LONG_OPTS(
33 //usage: "\n --reference RFILE Use RFILE's group instead of using a CONTEXT value"
35 //usage: "\n -u USER Set user/role/type/range in the target security context"
39 //usage: "\n -R Recurse"
41 #include <selinux/context.h>
45 #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' */
54 #define OPT_REFERENCE ((1<<9) * ENABLE_LONG_OPTS)
55 #define OPT_COMPONENT_SPECIFIED (OPT_USER | OPT_ROLE | OPT_TYPE | OPT_RANGE)
57 static char *user = NULL;
58 static char *role = NULL;
59 static char *type = NULL;
60 static char *range = NULL;
61 static char *specified_context = NULL;
63 static int FAST_FUNC change_filedir_context(
65 struct stat *stbuf UNUSED_PARAM,
66 void *userData UNUSED_PARAM,
67 int depth UNUSED_PARAM)
69 context_t context = NULL;
70 security_context_t file_context = NULL;
71 security_context_t context_string;
75 if (option_mask32 & OPT_NODEREFERENCE) {
76 status = lgetfilecon(fname, &file_context);
78 status = getfilecon(fname, &file_context);
80 if (status < 0 && errno != ENODATA) {
81 if ((option_mask32 & OPT_QUIET) == 0)
82 bb_error_msg("can't obtain security context: %s", fname);
86 if (file_context == NULL && specified_context == NULL) {
87 bb_error_msg("can't apply partial context to unlabeled file %s", fname);
91 if (specified_context == NULL) {
92 context = set_security_context_component(file_context,
93 user, role, type, range);
95 bb_error_msg("can't compute security context from %s", file_context);
99 context = context_new(specified_context);
101 bb_error_msg("invalid context: %s", specified_context);
106 context_string = context_str(context);
107 if (!context_string) {
108 bb_error_msg("can't obtain security context in text expression");
112 if (file_context == NULL || strcmp(context_string, file_context) != 0) {
115 if (option_mask32 & OPT_NODEREFERENCE) {
116 fail = lsetfilecon(fname, context_string);
118 fail = setfilecon(fname, context_string);
120 if ((option_mask32 & OPT_VERBOSE) || ((option_mask32 & OPT_CHANHES) && !fail)) {
122 ? "context of %s changed to %s\n"
123 : "can't change context of %s to %s\n",
124 fname, context_string);
128 } else if ((option_mask32 & OPT_QUIET) == 0) {
129 bb_error_msg("can't change context of %s to %s",
130 fname, context_string);
132 } else if (option_mask32 & OPT_VERBOSE) {
133 printf("context of %s retained as %s\n", fname, context_string);
137 context_free(context);
138 freecon(file_context);
144 static const char chcon_longopts[] ALIGN1 =
145 "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 */
159 int chcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
160 int chcon_main(int argc UNUSED_PARAM, char **argv)
162 char *reference_file;
166 opt_complementary = "-1" /* at least 1 param */
167 ":?" /* error if exclusivity constraints are violated */
169 ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff"
171 ":f--v:v--f"; /* 'verbose' and 'quiet' are exclusive */
172 getopt32long(argv, "Rchfu:r:t:l:v", chcon_longopts,
173 &user, &role, &type, &range, &reference_file
178 if (option_mask32 & OPT_REFERENCE) {
179 /* FIXME: lgetfilecon() should be used when '-h' is specified.
180 * But current implementation follows the original one. */
181 if (getfilecon(reference_file, &specified_context) < 0)
182 bb_perror_msg_and_die("getfilecon('%s') failed", reference_file);
185 if ((option_mask32 & OPT_COMPONENT_SPECIFIED) == 0) {
186 specified_context = *argv++;
187 /* specified_context is never NULL -
188 * "-1" in opt_complementary prevents this. */
190 bb_error_msg_and_die("too few arguments");
193 for (i = 0; (fname = argv[i]) != NULL; i++) {
194 int fname_len = strlen(fname);
195 while (fname_len > 1 && fname[fname_len - 1] == '/')
197 fname[fname_len] = '\0';
199 if (recursive_action(fname,
200 1<<option_mask32 & OPT_RECURSIVE,
201 change_filedir_context,
202 change_filedir_context,