blob: f45e41b2b465144f364d47c2f0612c58c3843aa7 [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);
121
Denis Vlasenko397137b2007-07-23 14:03:30 +0000122 }
123 if (lstat(directory, &sb)) {
124 bb_error_msg("directory \"%s\" not found, ignoring", directory);
Denis Vlasenko972fa092007-07-25 17:39:46 +0000125 return;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000126 }
127 if ((sb.st_mode & S_IFDIR) == 0) {
128 bb_error_msg("\"%s\" is not a directory: mode %o, ignoring",
129 directory, sb.st_mode);
Denis Vlasenko972fa092007-07-25 17:39:46 +0000130 return;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000131 }
132 if (excludeCtr == MAX_EXCLUDES) {
Denis Vlasenko972fa092007-07-25 17:39:46 +0000133 bb_error_msg_and_die("maximum excludes %d exceeded", MAX_EXCLUDES);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000134 }
135
136 len = strlen(directory);
137 while (len > 1 && directory[len - 1] == '/') {
138 len--;
139 }
140 excludeArray[excludeCtr].directory = xstrndup(directory, len);
141 excludeArray[excludeCtr++].size = len;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000142}
143
Denis Vlasenko1da77722007-07-24 06:17:43 +0000144static bool exclude(const char *file)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000145{
146 int i = 0;
147 for (i = 0; i < excludeCtr; i++) {
148 if (strncmp(file, excludeArray[i].directory,
149 excludeArray[i].size) == 0) {
150 if (file[excludeArray[i].size] == '\0'
151 || file[excludeArray[i].size] == '/') {
152 return 1;
153 }
154 }
155 }
156 return 0;
157}
158
159static int match(const char *name, struct stat *sb, char **con)
160{
161 int ret;
162 char path[PATH_MAX + 1];
163 char *tmp_path = xstrdup(name);
164
Denis Vlasenko9fe9c1a2007-07-23 14:05:02 +0000165 if (excludeCtr > 0 && exclude(name)) {
166 goto err;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000167 }
168 ret = lstat(name, sb);
169 if (ret) {
170 if (FLAG_i_ignore_enoent && errno == ENOENT) {
171 free(tmp_path);
172 return 0;
173 }
174 bb_error_msg("stat(%s)", name);
175 goto err;
176 }
177
178 if (expand_realpath) {
179 if (S_ISLNK(sb->st_mode)) {
180 char *p = NULL;
181 char *file_sep;
182
183 size_t len = 0;
184
185 if (verbose > 1)
186 bb_error_msg("warning! %s refers to a symbolic link, not following last component", name);
187
188 file_sep = strrchr(tmp_path, '/');
189 if (file_sep == tmp_path) {
190 file_sep++;
Denis Vlasenkoa0ad43b2007-09-28 11:37:38 +0000191 path[0] = '\0';
Denis Vlasenko397137b2007-07-23 14:03:30 +0000192 p = path;
193 } else if (file_sep) {
194 *file_sep++ = '\0';
195 p = realpath(tmp_path, path);
196 } else {
197 file_sep = tmp_path;
198 p = realpath("./", path);
199 }
200 if (p)
201 len = strlen(p);
202 if (!p || len + strlen(file_sep) + 2 > PATH_MAX) {
203 bb_perror_msg("realpath(%s) failed", name);
204 goto err;
205 }
206 p += len;
207 /* ensure trailing slash of directory name */
208 if (len == 0 || p[-1] != '/') {
209 *p++ = '/';
210 }
211 strcpy(p, file_sep);
212 name = path;
213 if (excludeCtr > 0 && exclude(name))
214 goto err;
215
216 } else {
217 char *p;
218 p = realpath(name, path);
219 if (!p) {
220 bb_perror_msg("realpath(%s)", name);
221 goto err;
222 }
223 name = p;
224 if (excludeCtr > 0 && exclude(name))
225 goto err;
226 }
227 }
228
229 /* name will be what is matched in the policy */
230 if (NULL != rootpath) {
231 if (0 != strncmp(rootpath, name, rootpathlen)) {
232 bb_error_msg("%s is not located in %s",
233 name, rootpath);
234 goto err;
235 }
236 name += rootpathlen;
237 }
238
239 free(tmp_path);
240 if (rootpath != NULL && name[0] == '\0')
241 /* this is actually the root dir of the alt root */
242 return matchpathcon_index("/", sb->st_mode, con);
243 return matchpathcon_index(name, sb->st_mode, con);
244 err:
245 free(tmp_path);
246 return -1;
247}
248
249/* Compare two contexts to see if their differences are "significant",
250 * or whether the only difference is in the user. */
Denis Vlasenko1da77722007-07-24 06:17:43 +0000251static bool only_changed_user(const char *a, const char *b)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000252{
253 if (FLAG_F_force)
254 return 0;
255 if (!a || !b)
256 return 0;
257 a = strchr(a, ':'); /* Rest of the context after the user */
258 b = strchr(b, ':');
259 if (!a || !b)
260 return 0;
261 return (strcmp(a, b) == 0);
262}
263
264static int restore(const char *file)
265{
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000266 char *my_file;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000267 struct stat my_sb;
268 int i, j, ret;
269 char *context = NULL;
270 char *newcon = NULL;
Denis Vlasenko1da77722007-07-24 06:17:43 +0000271 bool user_only_changed = 0;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000272 int retval = 0;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000273
274 my_file = bb_simplify_path(file);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000275
276 i = match(my_file, &my_sb, &newcon);
277
278 if (i < 0) /* No matching specification. */
279 goto out;
280
Denis Vlasenko397137b2007-07-23 14:03:30 +0000281 if (FLAG_p_progress) {
282 count++;
283 if (count % 0x400 == 0) { /* every 1024 times */
284 count = (count % (80*0x400));
285 if (count == 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000286 bb_putchar('\n');
287 bb_putchar('*');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100288 fflush_all();
Denis Vlasenko397137b2007-07-23 14:03:30 +0000289 }
290 }
291
292 /*
293 * Try to add an association between this inode and
294 * this specification. If there is already an association
295 * for this inode and it conflicts with this specification,
296 * then use the last matching specification.
297 */
298 if (add_assoc) {
299 j = matchpathcon_filespec_add(my_sb.st_ino, i, my_file);
300 if (j < 0)
301 goto err;
302
303 if (j != i) {
304 /* There was already an association and it took precedence. */
305 goto out;
306 }
307 }
308
309 if (FLAG_d_debug)
310 printf("%s: %s matched by %s\n", applet_name, my_file, newcon);
311
312 /* Get the current context of the file. */
313 ret = lgetfilecon_raw(my_file, &context);
314 if (ret < 0) {
315 if (errno == ENODATA) {
316 context = NULL; /* paranoia */
317 } else {
318 bb_perror_msg("lgetfilecon_raw on %s", my_file);
319 goto err;
320 }
321 user_only_changed = 0;
322 } else
323 user_only_changed = only_changed_user(context, newcon);
324
325 /*
326 * Do not relabel the file if the matching specification is
327 * <<none>> or the file is already labeled according to the
328 * specification.
329 */
330 if ((strcmp(newcon, "<<none>>") == 0)
331 || (context && (strcmp(context, newcon) == 0) && !FLAG_F_force)) {
332 goto out;
333 }
334
335 if (!FLAG_F_force && context && (is_context_customizable(context) > 0)) {
336 if (verbose > 1) {
337 bb_error_msg("skipping %s. %s is customizable_types",
338 my_file, context);
339 }
340 goto out;
341 }
342
343 if (verbose) {
344 /* If we're just doing "-v", trim out any relabels where
345 * the user has changed but the role and type are the
346 * same. For "-vv", emit everything. */
347 if (verbose > 1 || !user_only_changed) {
348 bb_info_msg("%s: reset %s context %s->%s",
Denys Vlasenko90a99042009-09-06 02:36:23 +0200349 applet_name, my_file, context ? context : "", newcon);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000350 }
351 }
352
353 if (FLAG_l_take_log && !user_only_changed) {
354 if (context)
355 bb_info_msg("relabeling %s from %s to %s", my_file, context, newcon);
356 else
357 bb_info_msg("labeling %s to %s", my_file, newcon);
358 }
359
360 if (outfile && !user_only_changed)
361 fprintf(outfile, "%s\n", my_file);
362
363 /*
364 * Do not relabel the file if -n was used.
365 */
366 if (FLAG_n_dry_run || user_only_changed)
367 goto out;
368
369 /*
370 * Relabel the file to the specified context.
371 */
372 ret = lsetfilecon(my_file, newcon);
373 if (ret) {
374 bb_perror_msg("lsetfileconon(%s,%s)", my_file, newcon);
375 goto err;
376 }
377
378 out:
379 freecon(context);
380 freecon(newcon);
Denis Vlasenko84d71f32007-08-06 02:36:35 +0000381 free(my_file);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000382 return retval;
383 err:
384 retval--; /* -1 */
385 goto out;
386}
387
388/*
389 * Apply the last matching specification to a file.
390 * This function is called by recursive_action on each file during
391 * the directory traversal.
392 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000393static int FAST_FUNC apply_spec(
Denis Vlasenko592d4fe2008-03-17 09:19:26 +0000394 const char *file,
395 struct stat *sb,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000396 void *userData UNUSED_PARAM,
397 int depth UNUSED_PARAM)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000398{
399 if (!follow_mounts) {
400 /* setfiles does not process across different mount points */
401 if (sb->st_dev != dev_id) {
402 return SKIP;
403 }
404 }
405 errors |= restore(file);
406 if (abort_on_error && errors)
407 return FALSE;
408 return TRUE;
409}
410
411
412static int canoncon(const char *path, unsigned lineno, char **contextp)
413{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000414 static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s";
Denis Vlasenko397137b2007-07-23 14:03:30 +0000415
416 char *tmpcon;
417 char *context = *contextp;
418 int invalid = 0;
419
420#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
421 if (policyfile) {
422 if (sepol_check_context(context) >= 0)
423 return 0;
424 /* Exit immediately if we're in checking mode. */
425 bb_error_msg_and_die(err_msg, path, lineno, context);
426 }
427#endif
428
429 if (security_canonicalize_context_raw(context, &tmpcon) < 0) {
430 if (errno != ENOENT) {
431 invalid = 1;
432 inc_err();
433 }
434 } else {
435 free(context);
436 *contextp = tmpcon;
437 }
438
439 if (invalid) {
440 bb_error_msg(err_msg, path, lineno, context);
441 }
442
443 return invalid;
444}
445
446static int process_one(char *name)
447{
448 struct stat sb;
449 int rc;
450
451 rc = lstat(name, &sb);
452 if (rc < 0) {
453 if (FLAG_i_ignore_enoent && errno == ENOENT)
454 return 0;
455 bb_perror_msg("stat(%s)", name);
456 goto err;
457 }
458 dev_id = sb.st_dev;
459
460 if (S_ISDIR(sb.st_mode) && recurse) {
461 if (recursive_action(name,
462 ACTION_RECURSE,
463 apply_spec,
464 apply_spec,
465 NULL, 0) != TRUE) {
466 bb_error_msg("error while labeling %s", name);
467 goto err;
468 }
469 } else {
470 rc = restore(name);
471 if (rc)
472 goto err;
473 }
474
475 out:
476 if (add_assoc) {
477 if (FLAG_q_quiet)
478 set_matchpathcon_printf(&qprintf);
479 matchpathcon_filespec_eval();
480 set_matchpathcon_printf(NULL);
481 matchpathcon_filespec_destroy();
482 }
483
484 return rc;
485
486 err:
487 rc = -1;
488 goto out;
489}
490
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000491int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100492int setfiles_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000493{
494 struct stat sb;
495 int rc, i = 0;
496 const char *input_filename = NULL;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000497 char *buf = NULL;
498 size_t buf_len;
499 int flags;
500 llist_t *exclude_dir = NULL;
501 char *out_filename = NULL;
502
503 INIT_G();
504
505 if (applet_name[0] == 's') { /* "setfiles" */
506 /*
507 * setfiles:
508 * Recursive descent,
509 * Does not expand paths via realpath,
510 * Aborts on errors during the file tree walk,
511 * Try to track inode associations for conflict detection,
512 * Does not follow mounts,
513 * Validates all file contexts at init time.
514 */
515 recurse = 1;
516 abort_on_error = 1;
517 add_assoc = 1;
518 /* follow_mounts = 0; - already is */
519 matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS;
520 } else {
521 /*
522 * restorecon:
523 * No recursive descent unless -r/-R,
524 * Expands paths via realpath,
525 * Do not abort on errors during the file tree walk,
526 * Do not try to track inode associations for conflict detection,
527 * Follows mounts,
528 * Does lazy validation of contexts upon use.
529 */
530 expand_realpath = 1;
531 follow_mounts = 1;
532 matchpathcon_flags = MATCHPATHCON_NOTRANS;
533 /* restorecon only */
534 selinux_or_die();
535 }
536
537 set_matchpathcon_flags(matchpathcon_flags);
538
Denis Vlasenko1da77722007-07-24 06:17:43 +0000539 opt_complementary = "e::vv:v--p:p--v:v--q:q--v";
Denis Vlasenko397137b2007-07-23 14:03:30 +0000540 /* Option order must match OPT_x definitions! */
541 if (applet_name[0] == 'r') { /* restorecon */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000542 flags = getopt32(argv, "de:f:ilnpqrsvo:FWR",
Denis Vlasenko397137b2007-07-23 14:03:30 +0000543 &exclude_dir, &input_filename, &out_filename, &verbose);
544 } else { /* setfiles */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000545 flags = getopt32(argv, "de:f:ilnpqr:svo:FW"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000546 IF_FEATURE_SETFILES_CHECK_OPTION("c:"),
Denis Vlasenko397137b2007-07-23 14:03:30 +0000547 &exclude_dir, &input_filename, &rootpath, &out_filename,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000548 IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
Denis Vlasenko397137b2007-07-23 14:03:30 +0000549 &verbose);
550 }
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100551 argv += optind;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000552
553#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
554 if ((applet_name[0] == 's') && (flags & OPT_c)) {
555 FILE *policystream;
556
Denis Vlasenko5415c852008-07-21 23:05:26 +0000557 policystream = xfopen_for_read(policyfile);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000558 if (sepol_set_policydb_from_file(policystream) < 0) {
559 bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
560 }
561 fclose(policystream);
562
563 /* Only process the specified file_contexts file, not
564 any .homedirs or .local files, and do not perform
565 context translations. */
566 set_matchpathcon_flags(MATCHPATHCON_BASEONLY |
567 MATCHPATHCON_NOTRANS |
568 MATCHPATHCON_VALIDATE);
569 }
570#endif
571
Denis Vlasenko972fa092007-07-25 17:39:46 +0000572 while (exclude_dir)
573 add_exclude(llist_pop(&exclude_dir));
574
Denis Vlasenko397137b2007-07-23 14:03:30 +0000575 if (flags & OPT_o) {
576 outfile = stdout;
577 if (NOT_LONE_CHAR(out_filename, '-')) {
Denis Vlasenko5415c852008-07-21 23:05:26 +0000578 outfile = xfopen_for_write(out_filename);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000579 }
580 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000581 if (applet_name[0] == 'r') { /* restorecon */
582 if (flags & (OPT_r | OPT_R))
583 recurse = 1;
584 } else { /* setfiles */
585 if (flags & OPT_r)
586 rootpathlen = strlen(rootpath);
587 }
588 if (flags & OPT_s) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000589 input_filename = "-";
590 add_assoc = 0;
591 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000592
593 if (applet_name[0] == 's') { /* setfiles */
594 /* Use our own invalid context checking function so that
595 we can support either checking against the active policy or
596 checking against a binary policy file. */
597 set_matchpathcon_canoncon(&canoncon);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100598 if (!argv[0])
Denis Vlasenko397137b2007-07-23 14:03:30 +0000599 bb_show_usage();
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100600 xstat(argv[0], &sb);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000601 if (!S_ISREG(sb.st_mode)) {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100602 bb_error_msg_and_die("spec file %s is not a regular file", argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000603 }
604 /* Load the file contexts configuration and check it. */
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100605 rc = matchpathcon_init(argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000606 if (rc < 0) {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100607 bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000608 }
Denis Vlasenko397137b2007-07-23 14:03:30 +0000609 if (nerr)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000610 exit(EXIT_FAILURE);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100611 argv++;
Denis Vlasenko397137b2007-07-23 14:03:30 +0000612 }
613
Denis Vlasenko84d71f32007-08-06 02:36:35 +0000614 if (input_filename) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000615 ssize_t len;
616 FILE *f = stdin;
617
618 if (NOT_LONE_CHAR(input_filename, '-'))
Denis Vlasenko5415c852008-07-21 23:05:26 +0000619 f = xfopen_for_read(input_filename);
Denis Vlasenko397137b2007-07-23 14:03:30 +0000620 while ((len = getline(&buf, &buf_len, f)) > 0) {
621 buf[len - 1] = '\0';
622 errors |= process_one(buf);
623 }
624 if (ENABLE_FEATURE_CLEAN_UP)
625 fclose_if_not_stdin(f);
626 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100627 if (!argv[0])
Denis Vlasenko397137b2007-07-23 14:03:30 +0000628 bb_show_usage();
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100629 for (i = 0; argv[i]; i++) {
Denis Vlasenko397137b2007-07-23 14:03:30 +0000630 errors |= process_one(argv[i]);
631 }
632 }
633
634 if (FLAG_W_warn_no_match)
635 matchpathcon_checkmatches(argv[0]);
636
637 if (ENABLE_FEATURE_CLEAN_UP && outfile)
638 fclose(outfile);
639
640 return errors;
641}