blob: c974c4a9d4e00aa344dbe0f9d61c423c98635cad [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"
20//usage: "\n -n Don't change any file labels"
21//usage: "\n -q Suppress warnings"
22//usage: "\n -r DIR Use an alternate root path"
23//usage: "\n -e DIR Exclude DIR"
24//usage: "\n -F Force reset of context to match file_context for customizable files"
25//usage: "\n -o FILE Save list of files with incorrect context"
26//usage: "\n -s Take a list of files from stdin (instead of command line)"
27//usage: "\n -v Show changes in file labels, if type or role are changing"
28//usage: "\n -vv Show changes in file labels, if type, role, or user are changing"
29//usage: "\n -W Display warnings about entries that had no matching files"
30//usage:
31//usage:#define restorecon_trivial_usage
32//usage: "[-iFnRv] [-e EXCLUDEDIR]... [-o FILE] [-f FILE]"
33//usage:#define restorecon_full_usage "\n\n"
34//usage: "Reset security contexts of files in pathname\n"
35//usage: "\n -i Ignore files that don't exist"
36//usage: "\n -f FILE File with list of files to process"
37//usage: "\n -e DIR Directory to exclude"
38//usage: "\n -R,-r Recurse"
39//usage: "\n -n Don't change any file labels"
40//usage: "\n -o FILE Save list of files with incorrect context"
41//usage: "\n -v Verbose"
42//usage: "\n -vv Show changed labels"
43//usage: "\n -F Force reset of context to match file_context"
44//usage: "\n for customizable files, or the user section,"
45//usage: "\n if it has changed"
46
Denis Vlasenko397137b2007-07-23 14:03:30 +000047#include "libbb.h"
48#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
49#include <sepol/sepol.h>
50#endif
51
52#define MAX_EXCLUDES 50
53
54struct edir {
55 char *directory;
56 size_t size;
57};
58
59struct globals {
60 FILE *outfile;
61 char *policyfile;
62 char *rootpath;
63 int rootpathlen;
64 unsigned count;
65 int excludeCtr;
66 int errors;
67 int verbose; /* getopt32 uses it, has to be int */
Denis Vlasenko397137b2007-07-23 14:03:30 +000068 smallint recurse; /* Recursive descent */
69 smallint follow_mounts;
70 /* Behavior flags determined based on setfiles vs. restorecon */
71 smallint expand_realpath; /* Expand paths via realpath */
72 smallint abort_on_error; /* Abort the file tree walk upon an error */
73 int add_assoc; /* Track inode associations for conflict detection */
74 int matchpathcon_flags; /* Flags to matchpathcon */
75 dev_t dev_id; /* Device id where target file exists */
76 int nerr;
77 struct edir excludeArray[MAX_EXCLUDES];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010078} FIX_ALIASING;
Denis Vlasenko397137b2007-07-23 14:03:30 +000079#define G (*(struct globals*)&bb_common_bufsiz1)
80void BUG_setfiles_globals_too_big(void);
81#define INIT_G() do { \
82 if (sizeof(G) > COMMON_BUFSIZE) \
83 BUG_setfiles_globals_too_big(); \
84 /* memset(&G, 0, sizeof(G)); - already is */ \
85} while (0)
86#define outfile (G.outfile )
87#define policyfile (G.policyfile )
88#define rootpath (G.rootpath )
89#define rootpathlen (G.rootpathlen )
90#define count (G.count )
91#define excludeCtr (G.excludeCtr )
92#define errors (G.errors )
93#define verbose (G.verbose )
Denis Vlasenko397137b2007-07-23 14:03:30 +000094#define recurse (G.recurse )
95#define follow_mounts (G.follow_mounts )
96#define expand_realpath (G.expand_realpath )
97#define abort_on_error (G.abort_on_error )
98#define add_assoc (G.add_assoc )
99#define matchpathcon_flags (G.matchpathcon_flags)
100#define dev_id (G.dev_id )
101#define nerr (G.nerr )
102#define excludeArray (G.excludeArray )
103
104/* Must match getopt32 string! */
105enum {
106 OPT_d = (1 << 0),
107 OPT_e = (1 << 1),
108 OPT_f = (1 << 2),
109 OPT_i = (1 << 3),
110 OPT_l = (1 << 4),
111 OPT_n = (1 << 5),
112 OPT_p = (1 << 6),
113 OPT_q = (1 << 7),
114 OPT_r = (1 << 8),
115 OPT_s = (1 << 9),
116 OPT_v = (1 << 10),
117 OPT_o = (1 << 11),
118 OPT_F = (1 << 12),
119 OPT_W = (1 << 13),
120 OPT_c = (1 << 14), /* c only for setfiles */
121 OPT_R = (1 << 14), /* R only for restorecon */
122};
123#define FLAG_d_debug (option_mask32 & OPT_d)
124#define FLAG_e (option_mask32 & OPT_e)
125#define FLAG_f (option_mask32 & OPT_f)
126#define FLAG_i_ignore_enoent (option_mask32 & OPT_i)
127#define FLAG_l_take_log (option_mask32 & OPT_l)
128#define FLAG_n_dry_run (option_mask32 & OPT_n)
129#define FLAG_p_progress (option_mask32 & OPT_p)
130#define FLAG_q_quiet (option_mask32 & OPT_q)
131#define FLAG_r (option_mask32 & OPT_r)
132#define FLAG_s (option_mask32 & OPT_s)
133#define FLAG_v (option_mask32 & OPT_v)
134#define FLAG_o (option_mask32 & OPT_o)
135#define FLAG_F_force (option_mask32 & OPT_F)
136#define FLAG_W_warn_no_match (option_mask32 & OPT_W)
137#define FLAG_c (option_mask32 & OPT_c)
138#define FLAG_R (option_mask32 & OPT_R)
139
140
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000141static void qprintf(const char *fmt UNUSED_PARAM, ...)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000142{
143 /* quiet, do nothing */
144}
145
146static void inc_err(void)
147{
148 nerr++;
149 if (nerr > 9 && !FLAG_d_debug) {
150 bb_error_msg_and_die("exiting after 10 errors");
151 }
152}
153
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000154static void add_exclude(const char *directory)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000155{
156 struct stat sb;
Denis Vlasenko1da77722007-07-24 06:17:43 +0000157 size_t len;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000158
159 if (directory == NULL || directory[0] != '/') {
Denis Vlasenko972fa092007-07-25 17:39:46 +0000160 bb_error_msg_and_die("full path required for exclude: %s", directory);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000161 }
162 if (lstat(directory, &sb)) {
163 bb_error_msg("directory \"%s\" not found, ignoring", directory);
Denis Vlasenko972fa092007-07-25 17:39:46 +0000164 return;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000165 }
166 if ((sb.st_mode & S_IFDIR) == 0) {
167 bb_error_msg("\"%s\" is not a directory: mode %o, ignoring",
168 directory, sb.st_mode);
Denis Vlasenko972fa092007-07-25 17:39:46 +0000169 return;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000170 }
171 if (excludeCtr == MAX_EXCLUDES) {
Denis Vlasenko972fa092007-07-25 17:39:46 +0000172 bb_error_msg_and_die("maximum excludes %d exceeded", MAX_EXCLUDES);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000173 }
174
175 len = strlen(directory);
176 while (len > 1 && directory[len - 1] == '/') {
177 len--;
178 }
179 excludeArray[excludeCtr].directory = xstrndup(directory, len);
180 excludeArray[excludeCtr++].size = len;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000181}
182
Denis Vlasenko1da77722007-07-24 06:17:43 +0000183static bool exclude(const char *file)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000184{
185 int i = 0;
186 for (i = 0; i < excludeCtr; i++) {
187 if (strncmp(file, excludeArray[i].directory,
188 excludeArray[i].size) == 0) {
189 if (file[excludeArray[i].size] == '\0'
190 || file[excludeArray[i].size] == '/') {
191 return 1;
192 }
193 }
194 }
195 return 0;
196}
197
198static int match(const char *name, struct stat *sb, char **con)
199{
200 int ret;
201 char path[PATH_MAX + 1];
202 char *tmp_path = xstrdup(name);
203
Denis Vlasenko9fe9c1a2007-07-23 14:05:02 +0000204 if (excludeCtr > 0 && exclude(name)) {
205 goto err;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000206 }
207 ret = lstat(name, sb);
208 if (ret) {
209 if (FLAG_i_ignore_enoent && errno == ENOENT) {
210 free(tmp_path);
211 return 0;
212 }
213 bb_error_msg("stat(%s)", name);
214 goto err;
215 }
216
217 if (expand_realpath) {
218 if (S_ISLNK(sb->st_mode)) {
219 char *p = NULL;
220 char *file_sep;
221
222 size_t len = 0;
223
224 if (verbose > 1)
225 bb_error_msg("warning! %s refers to a symbolic link, not following last component", name);
226
227 file_sep = strrchr(tmp_path, '/');
228 if (file_sep == tmp_path) {
229 file_sep++;
Denis Vlasenkoa0ad43b2007-09-28 11:37:38 +0000230 path[0] = '\0';
Denis Vlasenko397137b2007-07-23 14:03:30 +0000231 p = path;
232 } else if (file_sep) {
233 *file_sep++ = '\0';
234 p = realpath(tmp_path, path);
235 } else {
236 file_sep = tmp_path;
237 p = realpath("./", path);
238 }
239 if (p)
240 len = strlen(p);
241 if (!p || len + strlen(file_sep) + 2 > PATH_MAX) {
242 bb_perror_msg("realpath(%s) failed", name);
243 goto err;
244 }
245 p += len;
246 /* ensure trailing slash of directory name */
247 if (len == 0 || p[-1] != '/') {
248 *p++ = '/';
249 }
250 strcpy(p, file_sep);
251 name = path;
252 if (excludeCtr > 0 && exclude(name))
253 goto err;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000254 } else {
255 char *p;
256 p = realpath(name, path);
257 if (!p) {
258 bb_perror_msg("realpath(%s)", name);
259 goto err;
260 }
261 name = p;
262 if (excludeCtr > 0 && exclude(name))
263 goto err;
264 }
265 }
266
267 /* name will be what is matched in the policy */
268 if (NULL != rootpath) {
269 if (0 != strncmp(rootpath, name, rootpathlen)) {
270 bb_error_msg("%s is not located in %s",
271 name, rootpath);
272 goto err;
273 }
274 name += rootpathlen;
275 }
276
277 free(tmp_path);
278 if (rootpath != NULL && name[0] == '\0')
279 /* this is actually the root dir of the alt root */
280 return matchpathcon_index("/", sb->st_mode, con);
281 return matchpathcon_index(name, sb->st_mode, con);
282 err:
283 free(tmp_path);
284 return -1;
285}
286
287/* Compare two contexts to see if their differences are "significant",
288 * or whether the only difference is in the user. */
Denis Vlasenko1da77722007-07-24 06:17:43 +0000289static bool only_changed_user(const char *a, const char *b)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000290{
291 if (FLAG_F_force)
292 return 0;
293 if (!a || !b)
294 return 0;
295 a = strchr(a, ':'); /* Rest of the context after the user */
296 b = strchr(b, ':');
297 if (!a || !b)
298 return 0;
299 return (strcmp(a, b) == 0);
300}
301
302static int restore(const char *file)
303{
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000304 char *my_file;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000305 struct stat my_sb;
306 int i, j, ret;
307 char *context = NULL;
308 char *newcon = NULL;
Denis Vlasenko1da77722007-07-24 06:17:43 +0000309 bool user_only_changed = 0;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000310 int retval = 0;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000311
312 my_file = bb_simplify_path(file);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000313
314 i = match(my_file, &my_sb, &newcon);
315
316 if (i < 0) /* No matching specification. */
317 goto out;
318
Denis Vlasenko397137b2007-07-23 14:03:30 +0000319 if (FLAG_p_progress) {
320 count++;
321 if (count % 0x400 == 0) { /* every 1024 times */
322 count = (count % (80*0x400));
323 if (count == 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000324 bb_putchar('\n');
325 bb_putchar('*');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100326 fflush_all();
Denis Vlasenko397137b2007-07-23 14:03:30 +0000327 }
328 }
329
330 /*
331 * Try to add an association between this inode and
332 * this specification. If there is already an association
333 * for this inode and it conflicts with this specification,
334 * then use the last matching specification.
335 */
336 if (add_assoc) {
337 j = matchpathcon_filespec_add(my_sb.st_ino, i, my_file);
338 if (j < 0)
339 goto err;
340
341 if (j != i) {
342 /* There was already an association and it took precedence. */
343 goto out;
344 }
345 }
346
347 if (FLAG_d_debug)
348 printf("%s: %s matched by %s\n", applet_name, my_file, newcon);
349
350 /* Get the current context of the file. */
351 ret = lgetfilecon_raw(my_file, &context);
352 if (ret < 0) {
353 if (errno == ENODATA) {
354 context = NULL; /* paranoia */
355 } else {
356 bb_perror_msg("lgetfilecon_raw on %s", my_file);
357 goto err;
358 }
359 user_only_changed = 0;
360 } else
361 user_only_changed = only_changed_user(context, newcon);
362
363 /*
364 * Do not relabel the file if the matching specification is
365 * <<none>> or the file is already labeled according to the
366 * specification.
367 */
368 if ((strcmp(newcon, "<<none>>") == 0)
369 || (context && (strcmp(context, newcon) == 0) && !FLAG_F_force)) {
370 goto out;
371 }
372
373 if (!FLAG_F_force && context && (is_context_customizable(context) > 0)) {
374 if (verbose > 1) {
375 bb_error_msg("skipping %s. %s is customizable_types",
376 my_file, context);
377 }
378 goto out;
379 }
380
381 if (verbose) {
382 /* If we're just doing "-v", trim out any relabels where
383 * the user has changed but the role and type are the
384 * same. For "-vv", emit everything. */
385 if (verbose > 1 || !user_only_changed) {
386 bb_info_msg("%s: reset %s context %s->%s",
Denys Vlasenko90a99042009-09-06 02:36:23 +0200387 applet_name, my_file, context ? context : "", newcon);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000388 }
389 }
390
391 if (FLAG_l_take_log && !user_only_changed) {
392 if (context)
393 bb_info_msg("relabeling %s from %s to %s", my_file, context, newcon);
394 else
395 bb_info_msg("labeling %s to %s", my_file, newcon);
396 }
397
398 if (outfile && !user_only_changed)
399 fprintf(outfile, "%s\n", my_file);
400
401 /*
402 * Do not relabel the file if -n was used.
403 */
404 if (FLAG_n_dry_run || user_only_changed)
405 goto out;
406
407 /*
408 * Relabel the file to the specified context.
409 */
410 ret = lsetfilecon(my_file, newcon);
411 if (ret) {
412 bb_perror_msg("lsetfileconon(%s,%s)", my_file, newcon);
413 goto err;
414 }
415
416 out:
417 freecon(context);
418 freecon(newcon);
Denis Vlasenko84d71f32007-08-06 02:36:35 +0000419 free(my_file);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000420 return retval;
421 err:
422 retval--; /* -1 */
423 goto out;
424}
425
426/*
427 * Apply the last matching specification to a file.
428 * This function is called by recursive_action on each file during
429 * the directory traversal.
430 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000431static int FAST_FUNC apply_spec(
Denis Vlasenko592d4fe2008-03-17 09:19:26 +0000432 const char *file,
433 struct stat *sb,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000434 void *userData UNUSED_PARAM,
435 int depth UNUSED_PARAM)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000436{
437 if (!follow_mounts) {
438 /* setfiles does not process across different mount points */
439 if (sb->st_dev != dev_id) {
440 return SKIP;
441 }
442 }
443 errors |= restore(file);
444 if (abort_on_error && errors)
445 return FALSE;
446 return TRUE;
447}
448
449
450static int canoncon(const char *path, unsigned lineno, char **contextp)
451{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000452 static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s";
Denis Vlasenko397137b2007-07-23 14:03:30 +0000453
454 char *tmpcon;
455 char *context = *contextp;
456 int invalid = 0;
457
458#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
459 if (policyfile) {
460 if (sepol_check_context(context) >= 0)
461 return 0;
462 /* Exit immediately if we're in checking mode. */
463 bb_error_msg_and_die(err_msg, path, lineno, context);
464 }
465#endif
466
467 if (security_canonicalize_context_raw(context, &tmpcon) < 0) {
468 if (errno != ENOENT) {
469 invalid = 1;
470 inc_err();
471 }
472 } else {
473 free(context);
474 *contextp = tmpcon;
475 }
476
477 if (invalid) {
478 bb_error_msg(err_msg, path, lineno, context);
479 }
480
481 return invalid;
482}
483
484static int process_one(char *name)
485{
486 struct stat sb;
487 int rc;
488
489 rc = lstat(name, &sb);
490 if (rc < 0) {
491 if (FLAG_i_ignore_enoent && errno == ENOENT)
492 return 0;
493 bb_perror_msg("stat(%s)", name);
494 goto err;
495 }
496 dev_id = sb.st_dev;
497
498 if (S_ISDIR(sb.st_mode) && recurse) {
499 if (recursive_action(name,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100500 ACTION_RECURSE,
501 apply_spec,
502 apply_spec,
503 NULL, 0) != TRUE
504 ) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000505 bb_error_msg("error while labeling %s", name);
506 goto err;
507 }
508 } else {
509 rc = restore(name);
510 if (rc)
511 goto err;
512 }
513
514 out:
515 if (add_assoc) {
516 if (FLAG_q_quiet)
517 set_matchpathcon_printf(&qprintf);
518 matchpathcon_filespec_eval();
519 set_matchpathcon_printf(NULL);
520 matchpathcon_filespec_destroy();
521 }
522
523 return rc;
524
525 err:
526 rc = -1;
527 goto out;
528}
529
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000530int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100531int setfiles_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000532{
533 struct stat sb;
534 int rc, i = 0;
535 const char *input_filename = NULL;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000536 char *buf = NULL;
537 size_t buf_len;
538 int flags;
539 llist_t *exclude_dir = NULL;
540 char *out_filename = NULL;
541
542 INIT_G();
543
544 if (applet_name[0] == 's') { /* "setfiles" */
545 /*
546 * setfiles:
547 * Recursive descent,
548 * Does not expand paths via realpath,
549 * Aborts on errors during the file tree walk,
550 * Try to track inode associations for conflict detection,
551 * Does not follow mounts,
552 * Validates all file contexts at init time.
553 */
554 recurse = 1;
555 abort_on_error = 1;
556 add_assoc = 1;
557 /* follow_mounts = 0; - already is */
558 matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS;
559 } else {
560 /*
561 * restorecon:
562 * No recursive descent unless -r/-R,
563 * Expands paths via realpath,
564 * Do not abort on errors during the file tree walk,
565 * Do not try to track inode associations for conflict detection,
566 * Follows mounts,
567 * Does lazy validation of contexts upon use.
568 */
569 expand_realpath = 1;
570 follow_mounts = 1;
571 matchpathcon_flags = MATCHPATHCON_NOTRANS;
572 /* restorecon only */
573 selinux_or_die();
574 }
575
576 set_matchpathcon_flags(matchpathcon_flags);
577
Denis Vlasenko1da77722007-07-24 06:17:43 +0000578 opt_complementary = "e::vv:v--p:p--v:v--q:q--v";
Denis Vlasenko397137b2007-07-23 14:03:30 +0000579 /* Option order must match OPT_x definitions! */
580 if (applet_name[0] == 'r') { /* restorecon */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000581 flags = getopt32(argv, "de:f:ilnpqrsvo:FWR",
Denis Vlasenko397137b2007-07-23 14:03:30 +0000582 &exclude_dir, &input_filename, &out_filename, &verbose);
583 } else { /* setfiles */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000584 flags = getopt32(argv, "de:f:ilnpqr:svo:FW"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000585 IF_FEATURE_SETFILES_CHECK_OPTION("c:"),
Denis Vlasenko397137b2007-07-23 14:03:30 +0000586 &exclude_dir, &input_filename, &rootpath, &out_filename,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100587 IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000588 &verbose);
589 }
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100590 argv += optind;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000591
592#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
593 if ((applet_name[0] == 's') && (flags & OPT_c)) {
594 FILE *policystream;
595
Denis Vlasenko5415c852008-07-21 23:05:26 +0000596 policystream = xfopen_for_read(policyfile);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000597 if (sepol_set_policydb_from_file(policystream) < 0) {
598 bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
599 }
600 fclose(policystream);
601
602 /* Only process the specified file_contexts file, not
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100603 * any .homedirs or .local files, and do not perform
604 * context translations. */
Denis Vlasenko397137b2007-07-23 14:03:30 +0000605 set_matchpathcon_flags(MATCHPATHCON_BASEONLY |
606 MATCHPATHCON_NOTRANS |
607 MATCHPATHCON_VALIDATE);
608 }
609#endif
610
Denis Vlasenko972fa092007-07-25 17:39:46 +0000611 while (exclude_dir)
612 add_exclude(llist_pop(&exclude_dir));
613
Denis Vlasenko397137b2007-07-23 14:03:30 +0000614 if (flags & OPT_o) {
615 outfile = stdout;
616 if (NOT_LONE_CHAR(out_filename, '-')) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000617 outfile = xfopen_for_write(out_filename);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000618 }
619 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000620 if (applet_name[0] == 'r') { /* restorecon */
621 if (flags & (OPT_r | OPT_R))
622 recurse = 1;
623 } else { /* setfiles */
624 if (flags & OPT_r)
625 rootpathlen = strlen(rootpath);
626 }
627 if (flags & OPT_s) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000628 input_filename = "-";
629 add_assoc = 0;
630 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000631
632 if (applet_name[0] == 's') { /* setfiles */
633 /* Use our own invalid context checking function so that
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100634 * we can support either checking against the active policy or
635 * checking against a binary policy file. */
Denis Vlasenko397137b2007-07-23 14:03:30 +0000636 set_matchpathcon_canoncon(&canoncon);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100637 if (!argv[0])
Denis Vlasenko397137b2007-07-23 14:03:30 +0000638 bb_show_usage();
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100639 xstat(argv[0], &sb);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000640 if (!S_ISREG(sb.st_mode)) {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100641 bb_error_msg_and_die("spec file %s is not a regular file", argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000642 }
643 /* Load the file contexts configuration and check it. */
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100644 rc = matchpathcon_init(argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000645 if (rc < 0) {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100646 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000647 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000648 if (nerr)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000649 exit(EXIT_FAILURE);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100650 argv++;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000651 }
652
Denis Vlasenko84d71f32007-08-06 02:36:35 +0000653 if (input_filename) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000654 ssize_t len;
655 FILE *f = stdin;
656
657 if (NOT_LONE_CHAR(input_filename, '-'))
Denis Vlasenko5415c852008-07-21 23:05:26 +0000658 f = xfopen_for_read(input_filename);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000659 while ((len = getline(&buf, &buf_len, f)) > 0) {
660 buf[len - 1] = '\0';
661 errors |= process_one(buf);
662 }
663 if (ENABLE_FEATURE_CLEAN_UP)
664 fclose_if_not_stdin(f);
665 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100666 if (!argv[0])
Denis Vlasenko397137b2007-07-23 14:03:30 +0000667 bb_show_usage();
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100668 for (i = 0; argv[i]; i++) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000669 errors |= process_one(argv[i]);
670 }
671 }
672
673 if (FLAG_W_warn_no_match)
674 matchpathcon_checkmatches(argv[0]);
675
676 if (ENABLE_FEATURE_CLEAN_UP && outfile)
677 fclose(outfile);
678
679 return errors;
680}