blob: e00cdda1bb6c36d21ef071135ce4d58945fcf2d3 [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 */
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000010#include <getopt.h>
11#include <selinux/context.h>
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
14
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000015#define OPT_RECURSIVE (1<<0) /* 'R' */
16#define OPT_CHANHES (1<<1) /* 'c' */
17#define OPT_NODEREFERENCE (1<<2) /* 'h' */
18#define OPT_QUIET (1<<3) /* 'f' */
19#define OPT_USER (1<<4) /* 'u' */
20#define OPT_ROLE (1<<5) /* 'r' */
21#define OPT_TYPE (1<<6) /* 't' */
22#define OPT_RANGE (1<<7) /* 'l' */
23#define OPT_VERBOSE (1<<8) /* 'v' */
24#define OPT_REFERENCE ((1<<9) * ENABLE_FEATURE_CHCON_LONG_OPTIONS)
25#define OPT_COMPONENT_SPECIFIED (OPT_USER | OPT_ROLE | OPT_TYPE | OPT_RANGE)
26
27static char *user = NULL;
28static char *role = NULL;
29static char *type = NULL;
30static char *range = NULL;
31static char *specified_context = NULL;
32
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000033static int FAST_FUNC change_filedir_context(
Denis Vlasenko592d4fe2008-03-17 09:19:26 +000034 const char *fname,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000035 struct stat *stbuf UNUSED_PARAM,
36 void *userData UNUSED_PARAM,
37 int depth UNUSED_PARAM)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000038{
39 context_t context = NULL;
40 security_context_t file_context = NULL;
41 security_context_t context_string;
42 int rc = FALSE;
43 int status = 0;
44
45 if (option_mask32 & OPT_NODEREFERENCE) {
46 status = lgetfilecon(fname, &file_context);
47 } else {
48 status = getfilecon(fname, &file_context);
49 }
50 if (status < 0 && errno != ENODATA) {
51 if ((option_mask32 & OPT_QUIET) == 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +010052 bb_error_msg("can't obtain security context: %s", fname);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000053 goto skip;
54 }
55
56 if (file_context == NULL && specified_context == NULL) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010057 bb_error_msg("can't apply partial context to unlabeled file %s", fname);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000058 goto skip;
59 }
60
61 if (specified_context == NULL) {
62 context = set_security_context_component(file_context,
63 user, role, type, range);
64 if (!context) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010065 bb_error_msg("can't compute security context from %s", file_context);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000066 goto skip;
67 }
68 } else {
69 context = context_new(specified_context);
70 if (!context) {
71 bb_error_msg("invalid context: %s", specified_context);
72 goto skip;
73 }
74 }
75
76 context_string = context_str(context);
77 if (!context_string) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010078 bb_error_msg("can't obtain security context in text expression");
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000079 goto skip;
80 }
81
82 if (file_context == NULL || strcmp(context_string, file_context) != 0) {
83 int fail;
84
85 if (option_mask32 & OPT_NODEREFERENCE) {
86 fail = lsetfilecon(fname, context_string);
87 } else {
88 fail = setfilecon(fname, context_string);
89 }
90 if ((option_mask32 & OPT_VERBOSE) || ((option_mask32 & OPT_CHANHES) && !fail)) {
91 printf(!fail
92 ? "context of %s changed to %s\n"
Denys Vlasenko651a2692010-03-23 16:25:17 +010093 : "can't change context of %s to %s\n",
Denis Vlasenko1203c9b2007-03-11 22:16:02 +000094 fname, context_string);
95 }
96 if (!fail) {
97 rc = TRUE;
98 } else if ((option_mask32 & OPT_QUIET) == 0) {
Denys Vlasenko651a2692010-03-23 16:25:17 +010099 bb_error_msg("can't change context of %s to %s",
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000100 fname, context_string);
101 }
102 } else if (option_mask32 & OPT_VERBOSE) {
103 printf("context of %s retained as %s\n", fname, context_string);
104 rc = TRUE;
105 }
106skip:
Denis Vlasenkob5c33b12007-03-12 19:49:07 +0000107 context_free(context);
108 freecon(file_context);
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000109
110 return rc;
111}
112
113#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000114static const char chcon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000115 "recursive\0" No_argument "R"
116 "changes\0" No_argument "c"
117 "no-dereference\0" No_argument "h"
118 "silent\0" No_argument "f"
119 "quiet\0" No_argument "f"
120 "user\0" Required_argument "u"
121 "role\0" Required_argument "r"
122 "type\0" Required_argument "t"
123 "range\0" Required_argument "l"
124 "verbose\0" No_argument "v"
125 "reference\0" Required_argument "\xff" /* no short option */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000126 ;
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000127#endif
128
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000129int chcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000130int chcon_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000131{
132 char *reference_file;
133 char *fname;
134 int i, errors = 0;
135
136#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000137 applet_long_options = chcon_longopts;
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000138#endif
Denis Vlasenko955bccc2007-03-12 10:41:23 +0000139 opt_complementary = "-1" /* at least 1 param */
140 ":?" /* error if exclusivity constraints are violated */
141#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
142 ":\xff--urtl:u--\xff:r--\xff:t--\xff:l--\xff"
143#endif
144 ":f--v:v--f"; /* 'verbose' and 'quiet' are exclusive */
Denis Vlasenkodd5e11b2007-09-26 17:55:55 +0000145 getopt32(argv, "Rchfu:r:t:l:v",
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000146 &user, &role, &type, &range, &reference_file);
147 argv += optind;
148
149#if ENABLE_FEATURE_CHCON_LONG_OPTIONS
150 if (option_mask32 & OPT_REFERENCE) {
151 /* FIXME: lgetfilecon() should be used when '-h' is specified.
152 But current implementation follows the original one. */
153 if (getfilecon(reference_file, &specified_context) < 0)
154 bb_perror_msg_and_die("getfilecon('%s') failed", reference_file);
155 } else
156#endif
157 if ((option_mask32 & OPT_COMPONENT_SPECIFIED) == 0) {
158 specified_context = *argv++;
159 /* specified_context is never NULL -
160 * "-1" in opt_complementary prevents this. */
161 if (!argv[0])
162 bb_error_msg_and_die("too few arguments");
163 }
164
165 for (i = 0; (fname = argv[i]) != NULL; i++) {
166 int fname_len = strlen(fname);
167 while (fname_len > 1 && fname[fname_len - 1] == '/')
168 fname_len--;
169 fname[fname_len] = '\0';
170
171 if (recursive_action(fname,
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000172 1<<option_mask32 & OPT_RECURSIVE,
Denis Vlasenko1203c9b2007-03-11 22:16:02 +0000173 change_filedir_context,
174 change_filedir_context,
175 NULL, 0) != TRUE)
176 errors = 1;
177 }
178 return errors;
179}