blob: 8644502b588115fe9c230698e0b67e5dfa644f6f [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 */
Pere Orga5bc8c002011-04-11 03:29:49 +020010
11//usage:#define chcon_trivial_usage
12//usage: "[OPTIONS] CONTEXT FILE..."
13//usage: "\n chcon [OPTIONS] [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE..."
14//usage: IF_FEATURE_CHCON_LONG_OPTIONS(
15//usage: "\n chcon [OPTIONS] --reference=RFILE FILE..."
16//usage: )
17//usage:#define chcon_full_usage "\n\n"
18//usage: "Change the security context of each FILE to CONTEXT\n"
19//usage: IF_FEATURE_CHCON_LONG_OPTIONS(
20//usage: "\n -v,--verbose Verbose"
21//usage: "\n -c,--changes Report changes made"
22//usage: "\n -h,--no-dereference Affect symlinks instead of their targets"
23//usage: "\n -f,--silent,--quiet Suppress most error messages"
24//usage: "\n --reference=RFILE Use RFILE's group instead of using a CONTEXT value"
25//usage: "\n -u,--user=USER Set user/role/type/range in the target"
26//usage: "\n -r,--role=ROLE security context"
27//usage: "\n -t,--type=TYPE"
28//usage: "\n -l,--range=RANGE"
29//usage: "\n -R,--recursive Recurse"
30//usage: )
31//usage: IF_NOT_FEATURE_CHCON_LONG_OPTIONS(
32//usage: "\n -v Verbose"
33//usage: "\n -c Report changes made"
34//usage: "\n -h Affect symlinks instead of their targets"
35//usage: "\n -f Suppress most error messages"
36//usage: "\n -u USER Set user/role/type/range in the target security context"
37//usage: "\n -r ROLE"
38//usage: "\n -t TYPE"
39//usage: "\n -l RNG"
40//usage: "\n -R Recurse"
41//usage: )
42
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000043#include <getopt.h>
44#include <selinux/context.h>
45
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000046#include "libbb.h"
47
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000048#define OPT_RECURSIVE (1<<0) /* 'R' */
49#define OPT_CHANHES (1<<1) /* 'c' */
50#define OPT_NODEREFERENCE (1<<2) /* 'h' */
51#define OPT_QUIET (1<<3) /* 'f' */
52#define OPT_USER (1<<4) /* 'u' */
53#define OPT_ROLE (1<<5) /* 'r' */
54#define OPT_TYPE (1<<6) /* 't' */
55#define OPT_RANGE (1<<7) /* 'l' */
56#define OPT_VERBOSE (1<<8) /* 'v' */
57#define OPT_REFERENCE ((1<<9) * ENABLE_FEATURE_CHCON_LONG_OPTIONS)
58#define OPT_COMPONENT_SPECIFIED (OPT_USER | OPT_ROLE | OPT_TYPE | OPT_RANGE)
59
60static char *user = NULL;
61static char *role = NULL;
62static char *type = NULL;
63static char *range = NULL;
64static char *specified_context = NULL;
65
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000066static int FAST_FUNC change_filedir_context(
Denis Vlasenko592d4fe2008-03-17 09:19:26 +000067 const char *fname,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000068 struct stat *stbuf UNUSED_PARAM,
69 void *userData UNUSED_PARAM,
70 int depth UNUSED_PARAM)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000071{
72 context_t context = NULL;
73 security_context_t file_context = NULL;
74 security_context_t context_string;
75 int rc = FALSE;
76 int status = 0;
77
78 if (option_mask32 & OPT_NODEREFERENCE) {
79 status = lgetfilecon(fname, &file_context);
80 } else {
81 status = getfilecon(fname, &file_context);
82 }
83 if (status < 0 && errno != ENODATA) {
84 if ((option_mask32 & OPT_QUIET) == 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +010085 bb_error_msg("can't obtain security context: %s", fname);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000086 goto skip;
87 }
88
89 if (file_context == NULL && specified_context == NULL) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010090 bb_error_msg("can't apply partial context to unlabeled file %s", fname);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000091 goto skip;
92 }
93
94 if (specified_context == NULL) {
95 context = set_security_context_component(file_context,
96 user, role, type, range);
97 if (!context) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010098 bb_error_msg("can't compute security context from %s", file_context);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000099 goto skip;
100 }
101 } else {
102 context = context_new(specified_context);
103 if (!context) {
104 bb_error_msg("invalid context: %s", specified_context);
105 goto skip;
106 }
107 }
108
109 context_string = context_str(context);
110 if (!context_string) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100111 bb_error_msg("can't obtain security context in text expression");
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000112 goto skip;
113 }
114
115 if (file_context == NULL || strcmp(context_string, file_context) != 0) {
116 int fail;
117
118 if (option_mask32 & OPT_NODEREFERENCE) {
119 fail = lsetfilecon(fname, context_string);
120 } else {
121 fail = setfilecon(fname, context_string);
122 }
123 if ((option_mask32 & OPT_VERBOSE) || ((option_mask32 & OPT_CHANHES) && !fail)) {
124 printf(!fail
125 ? "context of %s changed to %s\n"
Denys Vlasenko651a2692010-03-23 16:25:17 +0100126 : "can't change context of %s to %s\n",
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000127 fname, context_string);
128 }
129 if (!fail) {
130 rc = TRUE;
131 } else if ((option_mask32 & OPT_QUIET) == 0) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100132 bb_error_msg("can't change context of %s to %s",
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000133 fname, context_string);
134 }
135 } else if (option_mask32 & OPT_VERBOSE) {
136 printf("context of %s retained as %s\n", fname, context_string);
137 rc = TRUE;
138 }
139skip:
Denis Vlasenkob5c33b12007-03-12 19:49:07 +0000140 context_free(context);
141 freecon(file_context);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000142
143 return rc;
144}
145
146#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000147static const char chcon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000148 "recursive\0" No_argument "R"
149 "changes\0" No_argument "c"
150 "no-dereference\0" No_argument "h"
151 "silent\0" No_argument "f"
152 "quiet\0" No_argument "f"
153 "user\0" Required_argument "u"
154 "role\0" Required_argument "r"
155 "type\0" Required_argument "t"
156 "range\0" Required_argument "l"
157 "verbose\0" No_argument "v"
158 "reference\0" Required_argument "\xff" /* no short option */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000159 ;
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000160#endif
161
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000162int chcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000163int chcon_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000164{
165 char *reference_file;
166 char *fname;
167 int i, errors = 0;
168
169#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000170 applet_long_options = chcon_longopts;
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000171#endif
Denis Vlasenko955bccc2007-03-12 10:41:23 +0000172 opt_complementary = "-1" /* at least 1 param */
173 ":?" /* error if exclusivity constraints are violated */
174#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
175 ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff"
176#endif
177 ":f--v:v--f"; /* 'verbose' and 'quiet' are exclusive */
Denis Vlasenkodd5e11b2007-09-26 17:55:55 +0000178 getopt32(argv, "Rchfu:r:t:l:v",
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000179 &user, &role, &type, &range, &reference_file);
180 argv += optind;
181
182#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
183 if (option_mask32 & OPT_REFERENCE) {
184 /* FIXME: lgetfilecon() should be used when '-h' is specified.
185 But current implementation follows the original one. */
186 if (getfilecon(reference_file, &specified_context) < 0)
187 bb_perror_msg_and_die("getfilecon('%s') failed", reference_file);
188 } else
189#endif
190 if ((option_mask32 & OPT_COMPONENT_SPECIFIED) == 0) {
191 specified_context = *argv++;
192 /* specified_context is never NULL -
193 * "-1" in opt_complementary prevents this. */
194 if (!argv[0])
195 bb_error_msg_and_die("too few arguments");
196 }
197
198 for (i = 0; (fname = argv[i]) != NULL; i++) {
199 int fname_len = strlen(fname);
200 while (fname_len > 1 && fname[fname_len - 1] == '/')
201 fname_len--;
202 fname[fname_len] = '\0';
203
204 if (recursive_action(fname,
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000205 1<<option_mask32 & OPT_RECURSIVE,
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000206 change_filedir_context,
207 change_filedir_context,
208 NULL, 0) != TRUE)
209 errors = 1;
210 }
211 return errors;
212}