blob: 989510e3d908a99533729182604075ed5422a18c [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
7#include "libbb.h"
8#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
9#include <sepol/sepol.h>
10#endif
11
12#define MAX_EXCLUDES 50
13
14struct edir {
15 char *directory;
16 size_t size;
17};
18
19struct globals {
20 FILE *outfile;
21 char *policyfile;
22 char *rootpath;
23 int rootpathlen;
24 unsigned count;
25 int excludeCtr;
26 int errors;
27 int verbose; /* getopt32 uses it, has to be int */
Denis Vlasenko397137b2007-07-23 14:03:30 +000028 smallint recurse; /* Recursive descent */
29 smallint follow_mounts;
30 /* Behavior flags determined based on setfiles vs. restorecon */
31 smallint expand_realpath; /* Expand paths via realpath */
32 smallint abort_on_error; /* Abort the file tree walk upon an error */
33 int add_assoc; /* Track inode associations for conflict detection */
34 int matchpathcon_flags; /* Flags to matchpathcon */
35 dev_t dev_id; /* Device id where target file exists */
36 int nerr;
37 struct edir excludeArray[MAX_EXCLUDES];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010038} FIX_ALIASING;
Denis Vlasenko397137b2007-07-23 14:03:30 +000039#define G (*(struct globals*)&bb_common_bufsiz1)
40void BUG_setfiles_globals_too_big(void);
41#define INIT_G() do { \
42 if (sizeof(G) > COMMON_BUFSIZE) \
43 BUG_setfiles_globals_too_big(); \
44 /* memset(&G, 0, sizeof(G)); - already is */ \
45} while (0)
46#define outfile (G.outfile )
47#define policyfile (G.policyfile )
48#define rootpath (G.rootpath )
49#define rootpathlen (G.rootpathlen )
50#define count (G.count )
51#define excludeCtr (G.excludeCtr )
52#define errors (G.errors )
53#define verbose (G.verbose )
Denis Vlasenko397137b2007-07-23 14:03:30 +000054#define recurse (G.recurse )
55#define follow_mounts (G.follow_mounts )
56#define expand_realpath (G.expand_realpath )
57#define abort_on_error (G.abort_on_error )
58#define add_assoc (G.add_assoc )
59#define matchpathcon_flags (G.matchpathcon_flags)
60#define dev_id (G.dev_id )
61#define nerr (G.nerr )
62#define excludeArray (G.excludeArray )
63
64/* Must match getopt32 string! */
65enum {
66 OPT_d = (1 << 0),
67 OPT_e = (1 << 1),
68 OPT_f = (1 << 2),
69 OPT_i = (1 << 3),
70 OPT_l = (1 << 4),
71 OPT_n = (1 << 5),
72 OPT_p = (1 << 6),
73 OPT_q = (1 << 7),
74 OPT_r = (1 << 8),
75 OPT_s = (1 << 9),
76 OPT_v = (1 << 10),
77 OPT_o = (1 << 11),
78 OPT_F = (1 << 12),
79 OPT_W = (1 << 13),
80 OPT_c = (1 << 14), /* c only for setfiles */
81 OPT_R = (1 << 14), /* R only for restorecon */
82};
83#define FLAG_d_debug (option_mask32 & OPT_d)
84#define FLAG_e (option_mask32 & OPT_e)
85#define FLAG_f (option_mask32 & OPT_f)
86#define FLAG_i_ignore_enoent (option_mask32 & OPT_i)
87#define FLAG_l_take_log (option_mask32 & OPT_l)
88#define FLAG_n_dry_run (option_mask32 & OPT_n)
89#define FLAG_p_progress (option_mask32 & OPT_p)
90#define FLAG_q_quiet (option_mask32 & OPT_q)
91#define FLAG_r (option_mask32 & OPT_r)
92#define FLAG_s (option_mask32 & OPT_s)
93#define FLAG_v (option_mask32 & OPT_v)
94#define FLAG_o (option_mask32 & OPT_o)
95#define FLAG_F_force (option_mask32 & OPT_F)
96#define FLAG_W_warn_no_match (option_mask32 & OPT_W)
97#define FLAG_c (option_mask32 & OPT_c)
98#define FLAG_R (option_mask32 & OPT_R)
99
100
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000101static void qprintf(const char *fmt UNUSED_PARAM, ...)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000102{
103 /* quiet, do nothing */
104}
105
106static void inc_err(void)
107{
108 nerr++;
109 if (nerr > 9 && !FLAG_d_debug) {
110 bb_error_msg_and_die("exiting after 10 errors");
111 }
112}
113
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000114static void add_exclude(const char *directory)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000115{
116 struct stat sb;
Denis Vlasenko1da77722007-07-24 06:17:43 +0000117 size_t len;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000118
119 if (directory == NULL || directory[0] != '/') {
Denis Vlasenko972fa092007-07-25 17:39:46 +0000120 bb_error_msg_and_die("full path required for exclude: %s", directory);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000121 }
122 if (lstat(directory, &sb)) {
123 bb_error_msg("directory \"%s\" not found, ignoring", directory);
Denis Vlasenko972fa092007-07-25 17:39:46 +0000124 return;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000125 }
126 if ((sb.st_mode & S_IFDIR) == 0) {
127 bb_error_msg("\"%s\" is not a directory: mode %o, ignoring",
128 directory, sb.st_mode);
Denis Vlasenko972fa092007-07-25 17:39:46 +0000129 return;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000130 }
131 if (excludeCtr == MAX_EXCLUDES) {
Denis Vlasenko972fa092007-07-25 17:39:46 +0000132 bb_error_msg_and_die("maximum excludes %d exceeded", MAX_EXCLUDES);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000133 }
134
135 len = strlen(directory);
136 while (len > 1 && directory[len - 1] == '/') {
137 len--;
138 }
139 excludeArray[excludeCtr].directory = xstrndup(directory, len);
140 excludeArray[excludeCtr++].size = len;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000141}
142
Denis Vlasenko1da77722007-07-24 06:17:43 +0000143static bool exclude(const char *file)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000144{
145 int i = 0;
146 for (i = 0; i < excludeCtr; i++) {
147 if (strncmp(file, excludeArray[i].directory,
148 excludeArray[i].size) == 0) {
149 if (file[excludeArray[i].size] == '\0'
150 || file[excludeArray[i].size] == '/') {
151 return 1;
152 }
153 }
154 }
155 return 0;
156}
157
158static int match(const char *name, struct stat *sb, char **con)
159{
160 int ret;
161 char path[PATH_MAX + 1];
162 char *tmp_path = xstrdup(name);
163
Denis Vlasenko9fe9c1a2007-07-23 14:05:02 +0000164 if (excludeCtr > 0 && exclude(name)) {
165 goto err;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000166 }
167 ret = lstat(name, sb);
168 if (ret) {
169 if (FLAG_i_ignore_enoent && errno == ENOENT) {
170 free(tmp_path);
171 return 0;
172 }
173 bb_error_msg("stat(%s)", name);
174 goto err;
175 }
176
177 if (expand_realpath) {
178 if (S_ISLNK(sb->st_mode)) {
179 char *p = NULL;
180 char *file_sep;
181
182 size_t len = 0;
183
184 if (verbose > 1)
185 bb_error_msg("warning! %s refers to a symbolic link, not following last component", name);
186
187 file_sep = strrchr(tmp_path, '/');
188 if (file_sep == tmp_path) {
189 file_sep++;
Denis Vlasenkoa0ad43b2007-09-28 11:37:38 +0000190 path[0] = '\0';
Denis Vlasenko397137b2007-07-23 14:03:30 +0000191 p = path;
192 } else if (file_sep) {
193 *file_sep++ = '\0';
194 p = realpath(tmp_path, path);
195 } else {
196 file_sep = tmp_path;
197 p = realpath("./", path);
198 }
199 if (p)
200 len = strlen(p);
201 if (!p || len + strlen(file_sep) + 2 > PATH_MAX) {
202 bb_perror_msg("realpath(%s) failed", name);
203 goto err;
204 }
205 p += len;
206 /* ensure trailing slash of directory name */
207 if (len == 0 || p[-1] != '/') {
208 *p++ = '/';
209 }
210 strcpy(p, file_sep);
211 name = path;
212 if (excludeCtr > 0 && exclude(name))
213 goto err;
214
215 } else {
216 char *p;
217 p = realpath(name, path);
218 if (!p) {
219 bb_perror_msg("realpath(%s)", name);
220 goto err;
221 }
222 name = p;
223 if (excludeCtr > 0 && exclude(name))
224 goto err;
225 }
226 }
227
228 /* name will be what is matched in the policy */
229 if (NULL != rootpath) {
230 if (0 != strncmp(rootpath, name, rootpathlen)) {
231 bb_error_msg("%s is not located in %s",
232 name, rootpath);
233 goto err;
234 }
235 name += rootpathlen;
236 }
237
238 free(tmp_path);
239 if (rootpath != NULL && name[0] == '\0')
240 /* this is actually the root dir of the alt root */
241 return matchpathcon_index("/", sb->st_mode, con);
242 return matchpathcon_index(name, sb->st_mode, con);
243 err:
244 free(tmp_path);
245 return -1;
246}
247
248/* Compare two contexts to see if their differences are "significant",
249 * or whether the only difference is in the user. */
Denis Vlasenko1da77722007-07-24 06:17:43 +0000250static bool only_changed_user(const char *a, const char *b)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000251{
252 if (FLAG_F_force)
253 return 0;
254 if (!a || !b)
255 return 0;
256 a = strchr(a, ':'); /* Rest of the context after the user */
257 b = strchr(b, ':');
258 if (!a || !b)
259 return 0;
260 return (strcmp(a, b) == 0);
261}
262
263static int restore(const char *file)
264{
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000265 char *my_file;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000266 struct stat my_sb;
267 int i, j, ret;
268 char *context = NULL;
269 char *newcon = NULL;
Denis Vlasenko1da77722007-07-24 06:17:43 +0000270 bool user_only_changed = 0;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000271 int retval = 0;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000272
273 my_file = bb_simplify_path(file);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000274
275 i = match(my_file, &my_sb, &newcon);
276
277 if (i < 0) /* No matching specification. */
278 goto out;
279
Denis Vlasenko397137b2007-07-23 14:03:30 +0000280 if (FLAG_p_progress) {
281 count++;
282 if (count % 0x400 == 0) { /* every 1024 times */
283 count = (count % (80*0x400));
284 if (count == 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000285 bb_putchar('\n');
286 bb_putchar('*');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100287 fflush_all();
Denis Vlasenko397137b2007-07-23 14:03:30 +0000288 }
289 }
290
291 /*
292 * Try to add an association between this inode and
293 * this specification. If there is already an association
294 * for this inode and it conflicts with this specification,
295 * then use the last matching specification.
296 */
297 if (add_assoc) {
298 j = matchpathcon_filespec_add(my_sb.st_ino, i, my_file);
299 if (j < 0)
300 goto err;
301
302 if (j != i) {
303 /* There was already an association and it took precedence. */
304 goto out;
305 }
306 }
307
308 if (FLAG_d_debug)
309 printf("%s: %s matched by %s\n", applet_name, my_file, newcon);
310
311 /* Get the current context of the file. */
312 ret = lgetfilecon_raw(my_file, &context);
313 if (ret < 0) {
314 if (errno == ENODATA) {
315 context = NULL; /* paranoia */
316 } else {
317 bb_perror_msg("lgetfilecon_raw on %s", my_file);
318 goto err;
319 }
320 user_only_changed = 0;
321 } else
322 user_only_changed = only_changed_user(context, newcon);
323
324 /*
325 * Do not relabel the file if the matching specification is
326 * <<none>> or the file is already labeled according to the
327 * specification.
328 */
329 if ((strcmp(newcon, "<<none>>") == 0)
330 || (context && (strcmp(context, newcon) == 0) && !FLAG_F_force)) {
331 goto out;
332 }
333
334 if (!FLAG_F_force && context && (is_context_customizable(context) > 0)) {
335 if (verbose > 1) {
336 bb_error_msg("skipping %s. %s is customizable_types",
337 my_file, context);
338 }
339 goto out;
340 }
341
342 if (verbose) {
343 /* If we're just doing "-v", trim out any relabels where
344 * the user has changed but the role and type are the
345 * same. For "-vv", emit everything. */
346 if (verbose > 1 || !user_only_changed) {
347 bb_info_msg("%s: reset %s context %s->%s",
Denys Vlasenko90a99042009-09-06 02:36:23 +0200348 applet_name, my_file, context ? context : "", newcon);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000349 }
350 }
351
352 if (FLAG_l_take_log && !user_only_changed) {
353 if (context)
354 bb_info_msg("relabeling %s from %s to %s", my_file, context, newcon);
355 else
356 bb_info_msg("labeling %s to %s", my_file, newcon);
357 }
358
359 if (outfile && !user_only_changed)
360 fprintf(outfile, "%s\n", my_file);
361
362 /*
363 * Do not relabel the file if -n was used.
364 */
365 if (FLAG_n_dry_run || user_only_changed)
366 goto out;
367
368 /*
369 * Relabel the file to the specified context.
370 */
371 ret = lsetfilecon(my_file, newcon);
372 if (ret) {
373 bb_perror_msg("lsetfileconon(%s,%s)", my_file, newcon);
374 goto err;
375 }
376
377 out:
378 freecon(context);
379 freecon(newcon);
Denis Vlasenko84d71f32007-08-06 02:36:35 +0000380 free(my_file);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000381 return retval;
382 err:
383 retval--; /* -1 */
384 goto out;
385}
386
387/*
388 * Apply the last matching specification to a file.
389 * This function is called by recursive_action on each file during
390 * the directory traversal.
391 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000392static int FAST_FUNC apply_spec(
Denis Vlasenko592d4fe2008-03-17 09:19:26 +0000393 const char *file,
394 struct stat *sb,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000395 void *userData UNUSED_PARAM,
396 int depth UNUSED_PARAM)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000397{
398 if (!follow_mounts) {
399 /* setfiles does not process across different mount points */
400 if (sb->st_dev != dev_id) {
401 return SKIP;
402 }
403 }
404 errors |= restore(file);
405 if (abort_on_error && errors)
406 return FALSE;
407 return TRUE;
408}
409
410
411static int canoncon(const char *path, unsigned lineno, char **contextp)
412{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000413 static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s";
Denis Vlasenko397137b2007-07-23 14:03:30 +0000414
415 char *tmpcon;
416 char *context = *contextp;
417 int invalid = 0;
418
419#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
420 if (policyfile) {
421 if (sepol_check_context(context) >= 0)
422 return 0;
423 /* Exit immediately if we're in checking mode. */
424 bb_error_msg_and_die(err_msg, path, lineno, context);
425 }
426#endif
427
428 if (security_canonicalize_context_raw(context, &tmpcon) < 0) {
429 if (errno != ENOENT) {
430 invalid = 1;
431 inc_err();
432 }
433 } else {
434 free(context);
435 *contextp = tmpcon;
436 }
437
438 if (invalid) {
439 bb_error_msg(err_msg, path, lineno, context);
440 }
441
442 return invalid;
443}
444
445static int process_one(char *name)
446{
447 struct stat sb;
448 int rc;
449
450 rc = lstat(name, &sb);
451 if (rc < 0) {
452 if (FLAG_i_ignore_enoent && errno == ENOENT)
453 return 0;
454 bb_perror_msg("stat(%s)", name);
455 goto err;
456 }
457 dev_id = sb.st_dev;
458
459 if (S_ISDIR(sb.st_mode) && recurse) {
460 if (recursive_action(name,
461 ACTION_RECURSE,
462 apply_spec,
463 apply_spec,
464 NULL, 0) != TRUE) {
465 bb_error_msg("error while labeling %s", name);
466 goto err;
467 }
468 } else {
469 rc = restore(name);
470 if (rc)
471 goto err;
472 }
473
474 out:
475 if (add_assoc) {
476 if (FLAG_q_quiet)
477 set_matchpathcon_printf(&qprintf);
478 matchpathcon_filespec_eval();
479 set_matchpathcon_printf(NULL);
480 matchpathcon_filespec_destroy();
481 }
482
483 return rc;
484
485 err:
486 rc = -1;
487 goto out;
488}
489
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000490int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100491int setfiles_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000492{
493 struct stat sb;
494 int rc, i = 0;
495 const char *input_filename = NULL;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000496 char *buf = NULL;
497 size_t buf_len;
498 int flags;
499 llist_t *exclude_dir = NULL;
500 char *out_filename = NULL;
501
502 INIT_G();
503
504 if (applet_name[0] == 's') { /* "setfiles" */
505 /*
506 * setfiles:
507 * Recursive descent,
508 * Does not expand paths via realpath,
509 * Aborts on errors during the file tree walk,
510 * Try to track inode associations for conflict detection,
511 * Does not follow mounts,
512 * Validates all file contexts at init time.
513 */
514 recurse = 1;
515 abort_on_error = 1;
516 add_assoc = 1;
517 /* follow_mounts = 0; - already is */
518 matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS;
519 } else {
520 /*
521 * restorecon:
522 * No recursive descent unless -r/-R,
523 * Expands paths via realpath,
524 * Do not abort on errors during the file tree walk,
525 * Do not try to track inode associations for conflict detection,
526 * Follows mounts,
527 * Does lazy validation of contexts upon use.
528 */
529 expand_realpath = 1;
530 follow_mounts = 1;
531 matchpathcon_flags = MATCHPATHCON_NOTRANS;
532 /* restorecon only */
533 selinux_or_die();
534 }
535
536 set_matchpathcon_flags(matchpathcon_flags);
537
Denis Vlasenko1da77722007-07-24 06:17:43 +0000538 opt_complementary = "e::vv:v--p:p--v:v--q:q--v";
Denis Vlasenko397137b2007-07-23 14:03:30 +0000539 /* Option order must match OPT_x definitions! */
540 if (applet_name[0] == 'r') { /* restorecon */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000541 flags = getopt32(argv, "de:f:ilnpqrsvo:FWR",
Denis Vlasenko397137b2007-07-23 14:03:30 +0000542 &exclude_dir, &input_filename, &out_filename, &verbose);
543 } else { /* setfiles */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000544 flags = getopt32(argv, "de:f:ilnpqr:svo:FW"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000545 IF_FEATURE_SETFILES_CHECK_OPTION("c:"),
Denis Vlasenko397137b2007-07-23 14:03:30 +0000546 &exclude_dir, &input_filename, &rootpath, &out_filename,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000547 IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000548 &verbose);
549 }
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100550 argv += optind;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000551
552#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
553 if ((applet_name[0] == 's') && (flags & OPT_c)) {
554 FILE *policystream;
555
Denis Vlasenko5415c852008-07-21 23:05:26 +0000556 policystream = xfopen_for_read(policyfile);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000557 if (sepol_set_policydb_from_file(policystream) < 0) {
558 bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
559 }
560 fclose(policystream);
561
562 /* Only process the specified file_contexts file, not
563 any .homedirs or .local files, and do not perform
564 context translations. */
565 set_matchpathcon_flags(MATCHPATHCON_BASEONLY |
566 MATCHPATHCON_NOTRANS |
567 MATCHPATHCON_VALIDATE);
568 }
569#endif
570
Denis Vlasenko972fa092007-07-25 17:39:46 +0000571 while (exclude_dir)
572 add_exclude(llist_pop(&exclude_dir));
573
Denis Vlasenko397137b2007-07-23 14:03:30 +0000574 if (flags & OPT_o) {
575 outfile = stdout;
576 if (NOT_LONE_CHAR(out_filename, '-')) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000577 outfile = xfopen_for_write(out_filename);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000578 }
579 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000580 if (applet_name[0] == 'r') { /* restorecon */
581 if (flags & (OPT_r | OPT_R))
582 recurse = 1;
583 } else { /* setfiles */
584 if (flags & OPT_r)
585 rootpathlen = strlen(rootpath);
586 }
587 if (flags & OPT_s) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000588 input_filename = "-";
589 add_assoc = 0;
590 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000591
592 if (applet_name[0] == 's') { /* setfiles */
593 /* Use our own invalid context checking function so that
594 we can support either checking against the active policy or
595 checking against a binary policy file. */
596 set_matchpathcon_canoncon(&canoncon);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100597 if (!argv[0])
Denis Vlasenko397137b2007-07-23 14:03:30 +0000598 bb_show_usage();
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100599 xstat(argv[0], &sb);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000600 if (!S_ISREG(sb.st_mode)) {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100601 bb_error_msg_and_die("spec file %s is not a regular file", argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000602 }
603 /* Load the file contexts configuration and check it. */
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100604 rc = matchpathcon_init(argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000605 if (rc < 0) {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100606 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000607 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000608 if (nerr)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000609 exit(EXIT_FAILURE);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100610 argv++;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000611 }
612
Denis Vlasenko84d71f32007-08-06 02:36:35 +0000613 if (input_filename) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000614 ssize_t len;
615 FILE *f = stdin;
616
617 if (NOT_LONE_CHAR(input_filename, '-'))
Denis Vlasenko5415c852008-07-21 23:05:26 +0000618 f = xfopen_for_read(input_filename);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000619 while ((len = getline(&buf, &buf_len, f)) > 0) {
620 buf[len - 1] = '\0';
621 errors |= process_one(buf);
622 }
623 if (ENABLE_FEATURE_CLEAN_UP)
624 fclose_if_not_stdin(f);
625 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100626 if (!argv[0])
Denis Vlasenko397137b2007-07-23 14:03:30 +0000627 bb_show_usage();
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100628 for (i = 0; argv[i]; i++) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000629 errors |= process_one(argv[i]);
630 }
631 }
632
633 if (FLAG_W_warn_no_match)
634 matchpathcon_checkmatches(argv[0]);
635
636 if (ENABLE_FEATURE_CLEAN_UP && outfile)
637 fclose(outfile);
638
639 return errors;
640}