blob: 441345ae94b9eba8a423959c1260a1770b249e1b [file] [log] [blame]
Denis Vlasenko397137b2007-07-23 14:03:30 +00001/*
2 setfiles: based on policycoreutils 2.0.19
3 policycoreutils was released under GPL 2.
Denis Vlasenko9fe9c1a2007-07-23 14:05:02 +00004 Port to BusyBox (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
Denis Vlasenko397137b2007-07-23 14:03:30 +00005*/
6
Pere Orga5bc8c002011-04-11 03:29:49 +02007//usage:#define setfiles_trivial_usage
8//usage: "[-dnpqsvW] [-e DIR]... [-o FILE] [-r alt_root_path]"
9//usage: IF_FEATURE_SETFILES_CHECK_OPTION(
10//usage: " [-c policyfile] spec_file"
11//usage: )
12//usage: " pathname"
13//usage:#define setfiles_full_usage "\n\n"
14//usage: "Reset file contexts under pathname according to spec_file\n"
15//usage: IF_FEATURE_SETFILES_CHECK_OPTION(
16//usage: "\n -c FILE Check the validity of the contexts against the specified binary policy"
17//usage: )
18//usage: "\n -d Show which specification matched each file"
19//usage: "\n -l Log changes in file labels to syslog"
Denys Vlasenko80f0f1d2016-03-30 18:17:35 +020020//TODO: log to syslog is not yet implemented, it goes to stdout only now
Pere Orga5bc8c002011-04-11 03:29:49 +020021//usage: "\n -n Don't change any file labels"
22//usage: "\n -q Suppress warnings"
23//usage: "\n -r DIR Use an alternate root path"
24//usage: "\n -e DIR Exclude DIR"
25//usage: "\n -F Force reset of context to match file_context for customizable files"
26//usage: "\n -o FILE Save list of files with incorrect context"
27//usage: "\n -s Take a list of files from stdin (instead of command line)"
28//usage: "\n -v Show changes in file labels, if type or role are changing"
29//usage: "\n -vv Show changes in file labels, if type, role, or user are changing"
30//usage: "\n -W Display warnings about entries that had no matching files"
31//usage:
32//usage:#define restorecon_trivial_usage
33//usage: "[-iFnRv] [-e EXCLUDEDIR]... [-o FILE] [-f FILE]"
34//usage:#define restorecon_full_usage "\n\n"
35//usage: "Reset security contexts of files in pathname\n"
36//usage: "\n -i Ignore files that don't exist"
37//usage: "\n -f FILE File with list of files to process"
38//usage: "\n -e DIR Directory to exclude"
39//usage: "\n -R,-r Recurse"
40//usage: "\n -n Don't change any file labels"
41//usage: "\n -o FILE Save list of files with incorrect context"
42//usage: "\n -v Verbose"
43//usage: "\n -vv Show changed labels"
44//usage: "\n -F Force reset of context to match file_context"
45//usage: "\n for customizable files, or the user section,"
46//usage: "\n if it has changed"
47
Denis Vlasenko397137b2007-07-23 14:03:30 +000048#include "libbb.h"
49#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
50#include <sepol/sepol.h>
51#endif
52
53#define MAX_EXCLUDES 50
54
55struct edir {
56 char *directory;
57 size_t size;
58};
59
60struct globals {
61 FILE *outfile;
62 char *policyfile;
63 char *rootpath;
64 int rootpathlen;
65 unsigned count;
66 int excludeCtr;
67 int errors;
68 int verbose; /* getopt32 uses it, has to be int */
Denis Vlasenko397137b2007-07-23 14:03:30 +000069 smallint recurse; /* Recursive descent */
70 smallint follow_mounts;
71 /* Behavior flags determined based on setfiles vs. restorecon */
72 smallint expand_realpath; /* Expand paths via realpath */
73 smallint abort_on_error; /* Abort the file tree walk upon an error */
74 int add_assoc; /* Track inode associations for conflict detection */
75 int matchpathcon_flags; /* Flags to matchpathcon */
76 dev_t dev_id; /* Device id where target file exists */
77 int nerr;
78 struct edir excludeArray[MAX_EXCLUDES];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010079} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020080#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko397137b2007-07-23 14:03:30 +000081void BUG_setfiles_globals_too_big(void);
82#define INIT_G() do { \
83 if (sizeof(G) > COMMON_BUFSIZE) \
84 BUG_setfiles_globals_too_big(); \
85 /* memset(&G, 0, sizeof(G)); - already is */ \
86} while (0)
87#define outfile (G.outfile )
88#define policyfile (G.policyfile )
89#define rootpath (G.rootpath )
90#define rootpathlen (G.rootpathlen )
91#define count (G.count )
92#define excludeCtr (G.excludeCtr )
93#define errors (G.errors )
94#define verbose (G.verbose )
Denis Vlasenko397137b2007-07-23 14:03:30 +000095#define recurse (G.recurse )
96#define follow_mounts (G.follow_mounts )
97#define expand_realpath (G.expand_realpath )
98#define abort_on_error (G.abort_on_error )
99#define add_assoc (G.add_assoc )
100#define matchpathcon_flags (G.matchpathcon_flags)
101#define dev_id (G.dev_id )
102#define nerr (G.nerr )
103#define excludeArray (G.excludeArray )
104
105/* Must match getopt32 string! */
106enum {
107 OPT_d = (1 << 0),
108 OPT_e = (1 << 1),
109 OPT_f = (1 << 2),
110 OPT_i = (1 << 3),
111 OPT_l = (1 << 4),
112 OPT_n = (1 << 5),
113 OPT_p = (1 << 6),
114 OPT_q = (1 << 7),
115 OPT_r = (1 << 8),
116 OPT_s = (1 << 9),
117 OPT_v = (1 << 10),
118 OPT_o = (1 << 11),
119 OPT_F = (1 << 12),
120 OPT_W = (1 << 13),
121 OPT_c = (1 << 14), /* c only for setfiles */
122 OPT_R = (1 << 14), /* R only for restorecon */
123};
124#define FLAG_d_debug (option_mask32 & OPT_d)
125#define FLAG_e (option_mask32 & OPT_e)
126#define FLAG_f (option_mask32 & OPT_f)
127#define FLAG_i_ignore_enoent (option_mask32 & OPT_i)
128#define FLAG_l_take_log (option_mask32 & OPT_l)
129#define FLAG_n_dry_run (option_mask32 & OPT_n)
130#define FLAG_p_progress (option_mask32 & OPT_p)
131#define FLAG_q_quiet (option_mask32 & OPT_q)
132#define FLAG_r (option_mask32 & OPT_r)
133#define FLAG_s (option_mask32 & OPT_s)
134#define FLAG_v (option_mask32 & OPT_v)
135#define FLAG_o (option_mask32 & OPT_o)
136#define FLAG_F_force (option_mask32 & OPT_F)
137#define FLAG_W_warn_no_match (option_mask32 & OPT_W)
138#define FLAG_c (option_mask32 & OPT_c)
139#define FLAG_R (option_mask32 & OPT_R)
140
141
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000142static void qprintf(const char *fmt UNUSED_PARAM, ...)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000143{
144 /* quiet, do nothing */
145}
146
147static void inc_err(void)
148{
149 nerr++;
150 if (nerr > 9 && !FLAG_d_debug) {
151 bb_error_msg_and_die("exiting after 10 errors");
152 }
153}
154
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000155static void add_exclude(const char *directory)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000156{
157 struct stat sb;
Denis Vlasenko1da77722007-07-24 06:17:43 +0000158 size_t len;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000159
160 if (directory == NULL || directory[0] != '/') {
Denis Vlasenko972fa092007-07-25 17:39:46 +0000161 bb_error_msg_and_die("full path required for exclude: %s", directory);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000162 }
163 if (lstat(directory, &sb)) {
164 bb_error_msg("directory \"%s\" not found, ignoring", directory);
Denis Vlasenko972fa092007-07-25 17:39:46 +0000165 return;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000166 }
167 if ((sb.st_mode & S_IFDIR) == 0) {
168 bb_error_msg("\"%s\" is not a directory: mode %o, ignoring",
169 directory, sb.st_mode);
Denis Vlasenko972fa092007-07-25 17:39:46 +0000170 return;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000171 }
172 if (excludeCtr == MAX_EXCLUDES) {
Denis Vlasenko972fa092007-07-25 17:39:46 +0000173 bb_error_msg_and_die("maximum excludes %d exceeded", MAX_EXCLUDES);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000174 }
175
176 len = strlen(directory);
177 while (len > 1 && directory[len - 1] == '/') {
178 len--;
179 }
180 excludeArray[excludeCtr].directory = xstrndup(directory, len);
181 excludeArray[excludeCtr++].size = len;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000182}
183
Denis Vlasenko1da77722007-07-24 06:17:43 +0000184static bool exclude(const char *file)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000185{
186 int i = 0;
187 for (i = 0; i < excludeCtr; i++) {
188 if (strncmp(file, excludeArray[i].directory,
189 excludeArray[i].size) == 0) {
190 if (file[excludeArray[i].size] == '\0'
191 || file[excludeArray[i].size] == '/') {
192 return 1;
193 }
194 }
195 }
196 return 0;
197}
198
199static int match(const char *name, struct stat *sb, char **con)
200{
201 int ret;
202 char path[PATH_MAX + 1];
203 char *tmp_path = xstrdup(name);
204
Denis Vlasenko9fe9c1a2007-07-23 14:05:02 +0000205 if (excludeCtr > 0 && exclude(name)) {
206 goto err;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000207 }
208 ret = lstat(name, sb);
209 if (ret) {
210 if (FLAG_i_ignore_enoent && errno == ENOENT) {
211 free(tmp_path);
212 return 0;
213 }
214 bb_error_msg("stat(%s)", name);
215 goto err;
216 }
217
218 if (expand_realpath) {
219 if (S_ISLNK(sb->st_mode)) {
220 char *p = NULL;
221 char *file_sep;
222
223 size_t len = 0;
224
225 if (verbose > 1)
226 bb_error_msg("warning! %s refers to a symbolic link, not following last component", name);
227
228 file_sep = strrchr(tmp_path, '/');
229 if (file_sep == tmp_path) {
230 file_sep++;
Denis Vlasenkoa0ad43b2007-09-28 11:37:38 +0000231 path[0] = '\0';
Denis Vlasenko397137b2007-07-23 14:03:30 +0000232 p = path;
233 } else if (file_sep) {
234 *file_sep++ = '\0';
235 p = realpath(tmp_path, path);
236 } else {
237 file_sep = tmp_path;
238 p = realpath("./", path);
239 }
240 if (p)
241 len = strlen(p);
242 if (!p || len + strlen(file_sep) + 2 > PATH_MAX) {
243 bb_perror_msg("realpath(%s) failed", name);
244 goto err;
245 }
246 p += len;
247 /* ensure trailing slash of directory name */
248 if (len == 0 || p[-1] != '/') {
249 *p++ = '/';
250 }
251 strcpy(p, file_sep);
252 name = path;
253 if (excludeCtr > 0 && exclude(name))
254 goto err;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000255 } else {
256 char *p;
257 p = realpath(name, path);
258 if (!p) {
259 bb_perror_msg("realpath(%s)", name);
260 goto err;
261 }
262 name = p;
263 if (excludeCtr > 0 && exclude(name))
264 goto err;
265 }
266 }
267
268 /* name will be what is matched in the policy */
269 if (NULL != rootpath) {
270 if (0 != strncmp(rootpath, name, rootpathlen)) {
271 bb_error_msg("%s is not located in %s",
272 name, rootpath);
273 goto err;
274 }
275 name += rootpathlen;
276 }
277
278 free(tmp_path);
279 if (rootpath != NULL && name[0] == '\0')
280 /* this is actually the root dir of the alt root */
281 return matchpathcon_index("/", sb->st_mode, con);
282 return matchpathcon_index(name, sb->st_mode, con);
283 err:
284 free(tmp_path);
285 return -1;
286}
287
288/* Compare two contexts to see if their differences are "significant",
289 * or whether the only difference is in the user. */
Denis Vlasenko1da77722007-07-24 06:17:43 +0000290static bool only_changed_user(const char *a, const char *b)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000291{
292 if (FLAG_F_force)
293 return 0;
294 if (!a || !b)
295 return 0;
296 a = strchr(a, ':'); /* Rest of the context after the user */
297 b = strchr(b, ':');
298 if (!a || !b)
299 return 0;
300 return (strcmp(a, b) == 0);
301}
302
303static int restore(const char *file)
304{
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000305 char *my_file;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000306 struct stat my_sb;
307 int i, j, ret;
308 char *context = NULL;
309 char *newcon = NULL;
Denis Vlasenko1da77722007-07-24 06:17:43 +0000310 bool user_only_changed = 0;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000311 int retval = 0;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000312
313 my_file = bb_simplify_path(file);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000314
315 i = match(my_file, &my_sb, &newcon);
316
317 if (i < 0) /* No matching specification. */
318 goto out;
319
Denis Vlasenko397137b2007-07-23 14:03:30 +0000320 if (FLAG_p_progress) {
321 count++;
322 if (count % 0x400 == 0) { /* every 1024 times */
323 count = (count % (80*0x400));
324 if (count == 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000325 bb_putchar('\n');
326 bb_putchar('*');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100327 fflush_all();
Denis Vlasenko397137b2007-07-23 14:03:30 +0000328 }
329 }
330
331 /*
332 * Try to add an association between this inode and
333 * this specification. If there is already an association
334 * for this inode and it conflicts with this specification,
335 * then use the last matching specification.
336 */
337 if (add_assoc) {
338 j = matchpathcon_filespec_add(my_sb.st_ino, i, my_file);
339 if (j < 0)
340 goto err;
341
342 if (j != i) {
343 /* There was already an association and it took precedence. */
344 goto out;
345 }
346 }
347
348 if (FLAG_d_debug)
349 printf("%s: %s matched by %s\n", applet_name, my_file, newcon);
350
351 /* Get the current context of the file. */
352 ret = lgetfilecon_raw(my_file, &context);
353 if (ret < 0) {
354 if (errno == ENODATA) {
355 context = NULL; /* paranoia */
356 } else {
357 bb_perror_msg("lgetfilecon_raw on %s", my_file);
358 goto err;
359 }
360 user_only_changed = 0;
361 } else
362 user_only_changed = only_changed_user(context, newcon);
363
364 /*
365 * Do not relabel the file if the matching specification is
366 * <<none>> or the file is already labeled according to the
367 * specification.
368 */
369 if ((strcmp(newcon, "<<none>>") == 0)
370 || (context && (strcmp(context, newcon) == 0) && !FLAG_F_force)) {
371 goto out;
372 }
373
374 if (!FLAG_F_force && context && (is_context_customizable(context) > 0)) {
375 if (verbose > 1) {
376 bb_error_msg("skipping %s. %s is customizable_types",
377 my_file, context);
378 }
379 goto out;
380 }
381
382 if (verbose) {
383 /* If we're just doing "-v", trim out any relabels where
384 * the user has changed but the role and type are the
385 * same. For "-vv", emit everything. */
386 if (verbose > 1 || !user_only_changed) {
Denys Vlasenko80f0f1d2016-03-30 18:17:35 +0200387 printf("%s: reset %s context %s->%s\n",
Denys Vlasenko90a99042009-09-06 02:36:23 +0200388 applet_name, my_file, context ? context : "", newcon);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000389 }
390 }
391
392 if (FLAG_l_take_log && !user_only_changed) {
393 if (context)
Denys Vlasenko80f0f1d2016-03-30 18:17:35 +0200394 printf("relabeling %s from %s to %s\n", my_file, context, newcon);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000395 else
Denys Vlasenko80f0f1d2016-03-30 18:17:35 +0200396 printf("labeling %s to %s\n", my_file, newcon);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000397 }
398
399 if (outfile && !user_only_changed)
400 fprintf(outfile, "%s\n", my_file);
401
402 /*
403 * Do not relabel the file if -n was used.
404 */
405 if (FLAG_n_dry_run || user_only_changed)
406 goto out;
407
408 /*
409 * Relabel the file to the specified context.
410 */
411 ret = lsetfilecon(my_file, newcon);
412 if (ret) {
413 bb_perror_msg("lsetfileconon(%s,%s)", my_file, newcon);
414 goto err;
415 }
416
417 out:
418 freecon(context);
419 freecon(newcon);
Denis Vlasenko84d71f32007-08-06 02:36:35 +0000420 free(my_file);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000421 return retval;
422 err:
423 retval--; /* -1 */
424 goto out;
425}
426
427/*
428 * Apply the last matching specification to a file.
429 * This function is called by recursive_action on each file during
430 * the directory traversal.
431 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000432static int FAST_FUNC apply_spec(
Denis Vlasenko592d4fe2008-03-17 09:19:26 +0000433 const char *file,
434 struct stat *sb,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000435 void *userData UNUSED_PARAM,
436 int depth UNUSED_PARAM)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000437{
438 if (!follow_mounts) {
439 /* setfiles does not process across different mount points */
440 if (sb->st_dev != dev_id) {
441 return SKIP;
442 }
443 }
444 errors |= restore(file);
445 if (abort_on_error && errors)
446 return FALSE;
447 return TRUE;
448}
449
450
451static int canoncon(const char *path, unsigned lineno, char **contextp)
452{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000453 static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s";
Denis Vlasenko397137b2007-07-23 14:03:30 +0000454
455 char *tmpcon;
456 char *context = *contextp;
457 int invalid = 0;
458
459#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
460 if (policyfile) {
461 if (sepol_check_context(context) >= 0)
462 return 0;
463 /* Exit immediately if we're in checking mode. */
464 bb_error_msg_and_die(err_msg, path, lineno, context);
465 }
466#endif
467
468 if (security_canonicalize_context_raw(context, &tmpcon) < 0) {
469 if (errno != ENOENT) {
470 invalid = 1;
471 inc_err();
472 }
473 } else {
474 free(context);
475 *contextp = tmpcon;
476 }
477
478 if (invalid) {
479 bb_error_msg(err_msg, path, lineno, context);
480 }
481
482 return invalid;
483}
484
485static int process_one(char *name)
486{
487 struct stat sb;
488 int rc;
489
490 rc = lstat(name, &sb);
491 if (rc < 0) {
492 if (FLAG_i_ignore_enoent && errno == ENOENT)
493 return 0;
494 bb_perror_msg("stat(%s)", name);
495 goto err;
496 }
497 dev_id = sb.st_dev;
498
499 if (S_ISDIR(sb.st_mode) && recurse) {
500 if (recursive_action(name,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100501 ACTION_RECURSE,
502 apply_spec,
503 apply_spec,
504 NULL, 0) != TRUE
505 ) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000506 bb_error_msg("error while labeling %s", name);
507 goto err;
508 }
509 } else {
510 rc = restore(name);
511 if (rc)
512 goto err;
513 }
514
515 out:
516 if (add_assoc) {
517 if (FLAG_q_quiet)
518 set_matchpathcon_printf(&qprintf);
519 matchpathcon_filespec_eval();
520 set_matchpathcon_printf(NULL);
521 matchpathcon_filespec_destroy();
522 }
523
524 return rc;
525
526 err:
527 rc = -1;
528 goto out;
529}
530
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000531int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100532int setfiles_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000533{
534 struct stat sb;
535 int rc, i = 0;
536 const char *input_filename = NULL;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000537 char *buf = NULL;
538 size_t buf_len;
539 int flags;
540 llist_t *exclude_dir = NULL;
541 char *out_filename = NULL;
542
543 INIT_G();
544
545 if (applet_name[0] == 's') { /* "setfiles" */
546 /*
547 * setfiles:
548 * Recursive descent,
549 * Does not expand paths via realpath,
550 * Aborts on errors during the file tree walk,
551 * Try to track inode associations for conflict detection,
552 * Does not follow mounts,
553 * Validates all file contexts at init time.
554 */
555 recurse = 1;
556 abort_on_error = 1;
557 add_assoc = 1;
558 /* follow_mounts = 0; - already is */
559 matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS;
560 } else {
561 /*
562 * restorecon:
563 * No recursive descent unless -r/-R,
564 * Expands paths via realpath,
565 * Do not abort on errors during the file tree walk,
566 * Do not try to track inode associations for conflict detection,
567 * Follows mounts,
568 * Does lazy validation of contexts upon use.
569 */
570 expand_realpath = 1;
571 follow_mounts = 1;
572 matchpathcon_flags = MATCHPATHCON_NOTRANS;
573 /* restorecon only */
574 selinux_or_die();
575 }
576
577 set_matchpathcon_flags(matchpathcon_flags);
578
Denis Vlasenko1da77722007-07-24 06:17:43 +0000579 opt_complementary = "e::vv:v--p:p--v:v--q:q--v";
Denis Vlasenko397137b2007-07-23 14:03:30 +0000580 /* Option order must match OPT_x definitions! */
581 if (applet_name[0] == 'r') { /* restorecon */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000582 flags = getopt32(argv, "de:f:ilnpqrsvo:FWR",
Denis Vlasenko397137b2007-07-23 14:03:30 +0000583 &exclude_dir, &input_filename, &out_filename, &verbose);
584 } else { /* setfiles */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000585 flags = getopt32(argv, "de:f:ilnpqr:svo:FW"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000586 IF_FEATURE_SETFILES_CHECK_OPTION("c:"),
Denis Vlasenko397137b2007-07-23 14:03:30 +0000587 &exclude_dir, &input_filename, &rootpath, &out_filename,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100588 IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000589 &verbose);
590 }
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100591 argv += optind;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000592
593#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
594 if ((applet_name[0] == 's') && (flags & OPT_c)) {
595 FILE *policystream;
596
Denis Vlasenko5415c852008-07-21 23:05:26 +0000597 policystream = xfopen_for_read(policyfile);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000598 if (sepol_set_policydb_from_file(policystream) < 0) {
599 bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
600 }
601 fclose(policystream);
602
603 /* Only process the specified file_contexts file, not
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100604 * any .homedirs or .local files, and do not perform
605 * context translations. */
Denis Vlasenko397137b2007-07-23 14:03:30 +0000606 set_matchpathcon_flags(MATCHPATHCON_BASEONLY |
607 MATCHPATHCON_NOTRANS |
608 MATCHPATHCON_VALIDATE);
609 }
610#endif
611
Denis Vlasenko972fa092007-07-25 17:39:46 +0000612 while (exclude_dir)
613 add_exclude(llist_pop(&exclude_dir));
614
Denis Vlasenko397137b2007-07-23 14:03:30 +0000615 if (flags & OPT_o) {
616 outfile = stdout;
617 if (NOT_LONE_CHAR(out_filename, '-')) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000618 outfile = xfopen_for_write(out_filename);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000619 }
620 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000621 if (applet_name[0] == 'r') { /* restorecon */
622 if (flags & (OPT_r | OPT_R))
623 recurse = 1;
624 } else { /* setfiles */
625 if (flags & OPT_r)
626 rootpathlen = strlen(rootpath);
627 }
628 if (flags & OPT_s) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000629 input_filename = "-";
630 add_assoc = 0;
631 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000632
633 if (applet_name[0] == 's') { /* setfiles */
634 /* Use our own invalid context checking function so that
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100635 * we can support either checking against the active policy or
636 * checking against a binary policy file. */
Denis Vlasenko397137b2007-07-23 14:03:30 +0000637 set_matchpathcon_canoncon(&canoncon);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100638 if (!argv[0])
Denis Vlasenko397137b2007-07-23 14:03:30 +0000639 bb_show_usage();
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100640 xstat(argv[0], &sb);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000641 if (!S_ISREG(sb.st_mode)) {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100642 bb_error_msg_and_die("spec file %s is not a regular file", argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000643 }
644 /* Load the file contexts configuration and check it. */
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100645 rc = matchpathcon_init(argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000646 if (rc < 0) {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100647 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000648 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000649 if (nerr)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000650 exit(EXIT_FAILURE);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100651 argv++;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000652 }
653
Denis Vlasenko84d71f32007-08-06 02:36:35 +0000654 if (input_filename) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000655 ssize_t len;
656 FILE *f = stdin;
657
658 if (NOT_LONE_CHAR(input_filename, '-'))
Denis Vlasenko5415c852008-07-21 23:05:26 +0000659 f = xfopen_for_read(input_filename);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000660 while ((len = getline(&buf, &buf_len, f)) > 0) {
661 buf[len - 1] = '\0';
662 errors |= process_one(buf);
663 }
664 if (ENABLE_FEATURE_CLEAN_UP)
665 fclose_if_not_stdin(f);
666 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100667 if (!argv[0])
Denis Vlasenko397137b2007-07-23 14:03:30 +0000668 bb_show_usage();
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100669 for (i = 0; argv[i]; i++) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000670 errors |= process_one(argv[i]);
671 }
672 }
673
674 if (FLAG_W_warn_no_match)
675 matchpathcon_checkmatches(argv[0]);
676
677 if (ENABLE_FEATURE_CLEAN_UP && outfile)
678 fclose(outfile);
679
680 return errors;
681}