setfiles.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. setfiles: based on policycoreutils 2.0.19
  3. policycoreutils was released under GPL 2.
  4. Port to BusyBox (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
  5. */
  6. //config:config SETFILES
  7. //config: bool "setfiles"
  8. //config: default n
  9. //config: depends on SELINUX
  10. //config: help
  11. //config: Enable support to modify to relabel files.
  12. //config: Notice: If you built libselinux with -D_FILE_OFFSET_BITS=64,
  13. //config: (It is default in libselinux's Makefile), you _must_ enable
  14. //config: CONFIG_LFS.
  15. //config:
  16. //config:config FEATURE_SETFILES_CHECK_OPTION
  17. //config: bool "Enable check option"
  18. //config: default n
  19. //config: depends on SETFILES
  20. //config: help
  21. //config: Support "-c" option (check the validity of the contexts against
  22. //config: the specified binary policy) for setfiles. Requires libsepol.
  23. //config:
  24. //config:config RESTORECON
  25. //config: bool "restorecon"
  26. //config: default n
  27. //config: depends on SELINUX
  28. //config: help
  29. //config: Enable support to relabel files. The feature is almost
  30. //config: the same as setfiles, but usage is a little different.
  31. //applet:IF_SETFILES(APPLET(setfiles, BB_DIR_SBIN, BB_SUID_DROP))
  32. //applet:IF_RESTORECON(APPLET_ODDNAME(restorecon, setfiles, BB_DIR_SBIN, BB_SUID_DROP, restorecon))
  33. //kbuild:lib-$(CONFIG_SETFILES) += setfiles.o
  34. //kbuild:lib-$(CONFIG_RESTORECON) += setfiles.o
  35. //usage:#define setfiles_trivial_usage
  36. //usage: "[-dnpqsvW] [-e DIR]... [-o FILE] [-r alt_root_path]"
  37. //usage: IF_FEATURE_SETFILES_CHECK_OPTION(
  38. //usage: " [-c policyfile] spec_file"
  39. //usage: )
  40. //usage: " pathname"
  41. //usage:#define setfiles_full_usage "\n\n"
  42. //usage: "Reset file contexts under pathname according to spec_file\n"
  43. //usage: IF_FEATURE_SETFILES_CHECK_OPTION(
  44. //usage: "\n -c FILE Check the validity of the contexts against the specified binary policy"
  45. //usage: )
  46. //usage: "\n -d Show which specification matched each file"
  47. //usage: "\n -l Log changes in file labels to syslog"
  48. //TODO: log to syslog is not yet implemented, it goes to stdout only now
  49. //usage: "\n -n Don't change any file labels"
  50. //usage: "\n -q Suppress warnings"
  51. //usage: "\n -r DIR Use an alternate root path"
  52. //usage: "\n -e DIR Exclude DIR"
  53. //usage: "\n -F Force reset of context to match file_context for customizable files"
  54. //usage: "\n -o FILE Save list of files with incorrect context"
  55. //usage: "\n -s Take a list of files from stdin (instead of command line)"
  56. //usage: "\n -v Show changes in file labels, if type or role are changing"
  57. //usage: "\n -vv Show changes in file labels, if type, role, or user are changing"
  58. //usage: "\n -W Display warnings about entries that had no matching files"
  59. //usage:
  60. //usage:#define restorecon_trivial_usage
  61. //usage: "[-iFnRv] [-e EXCLUDEDIR]... [-o FILE] [-f FILE]"
  62. //usage:#define restorecon_full_usage "\n\n"
  63. //usage: "Reset security contexts of files in pathname\n"
  64. //usage: "\n -i Ignore files that don't exist"
  65. //usage: "\n -f FILE File with list of files to process"
  66. //usage: "\n -e DIR Directory to exclude"
  67. //usage: "\n -R,-r Recurse"
  68. //usage: "\n -n Don't change any file labels"
  69. //usage: "\n -o FILE Save list of files with incorrect context"
  70. //usage: "\n -v Verbose"
  71. //usage: "\n -vv Show changed labels"
  72. //usage: "\n -F Force reset of context to match file_context"
  73. //usage: "\n for customizable files, or the user section,"
  74. //usage: "\n if it has changed"
  75. #include "libbb.h"
  76. #include "common_bufsiz.h"
  77. #if ENABLE_FEATURE_SETFILES_CHECK_OPTION
  78. #include <sepol/sepol.h>
  79. #endif
  80. #define MAX_EXCLUDES 50
  81. struct edir {
  82. char *directory;
  83. size_t size;
  84. };
  85. struct globals {
  86. FILE *outfile;
  87. char *policyfile;
  88. char *rootpath;
  89. int rootpathlen;
  90. unsigned count;
  91. int excludeCtr;
  92. int errors;
  93. int verbose; /* getopt32 uses it, has to be int */
  94. smallint recurse; /* Recursive descent */
  95. smallint follow_mounts;
  96. /* Behavior flags determined based on setfiles vs. restorecon */
  97. smallint expand_realpath; /* Expand paths via realpath */
  98. smallint abort_on_error; /* Abort the file tree walk upon an error */
  99. int add_assoc; /* Track inode associations for conflict detection */
  100. int matchpathcon_flags; /* Flags to matchpathcon */
  101. dev_t dev_id; /* Device id where target file exists */
  102. int nerr;
  103. struct edir excludeArray[MAX_EXCLUDES];
  104. } FIX_ALIASING;
  105. #define G (*(struct globals*)bb_common_bufsiz1)
  106. void BUG_setfiles_globals_too_big(void);
  107. #define INIT_G() do { \
  108. setup_common_bufsiz(); \
  109. if (sizeof(G) > COMMON_BUFSIZE) \
  110. BUG_setfiles_globals_too_big(); \
  111. /* memset(&G, 0, sizeof(G)); - already is */ \
  112. } while (0)
  113. #define outfile (G.outfile )
  114. #define policyfile (G.policyfile )
  115. #define rootpath (G.rootpath )
  116. #define rootpathlen (G.rootpathlen )
  117. #define count (G.count )
  118. #define excludeCtr (G.excludeCtr )
  119. #define errors (G.errors )
  120. #define verbose (G.verbose )
  121. #define recurse (G.recurse )
  122. #define follow_mounts (G.follow_mounts )
  123. #define expand_realpath (G.expand_realpath )
  124. #define abort_on_error (G.abort_on_error )
  125. #define add_assoc (G.add_assoc )
  126. #define matchpathcon_flags (G.matchpathcon_flags)
  127. #define dev_id (G.dev_id )
  128. #define nerr (G.nerr )
  129. #define excludeArray (G.excludeArray )
  130. /* Must match getopt32 string! */
  131. enum {
  132. OPT_d = (1 << 0),
  133. OPT_e = (1 << 1),
  134. OPT_f = (1 << 2),
  135. OPT_i = (1 << 3),
  136. OPT_l = (1 << 4),
  137. OPT_n = (1 << 5),
  138. OPT_p = (1 << 6),
  139. OPT_q = (1 << 7),
  140. OPT_r = (1 << 8),
  141. OPT_s = (1 << 9),
  142. OPT_v = (1 << 10),
  143. OPT_o = (1 << 11),
  144. OPT_F = (1 << 12),
  145. OPT_W = (1 << 13),
  146. OPT_c = (1 << 14), /* c only for setfiles */
  147. OPT_R = (1 << 14), /* R only for restorecon */
  148. };
  149. #define FLAG_d_debug (option_mask32 & OPT_d)
  150. #define FLAG_e (option_mask32 & OPT_e)
  151. #define FLAG_f (option_mask32 & OPT_f)
  152. #define FLAG_i_ignore_enoent (option_mask32 & OPT_i)
  153. #define FLAG_l_take_log (option_mask32 & OPT_l)
  154. #define FLAG_n_dry_run (option_mask32 & OPT_n)
  155. #define FLAG_p_progress (option_mask32 & OPT_p)
  156. #define FLAG_q_quiet (option_mask32 & OPT_q)
  157. #define FLAG_r (option_mask32 & OPT_r)
  158. #define FLAG_s (option_mask32 & OPT_s)
  159. #define FLAG_v (option_mask32 & OPT_v)
  160. #define FLAG_o (option_mask32 & OPT_o)
  161. #define FLAG_F_force (option_mask32 & OPT_F)
  162. #define FLAG_W_warn_no_match (option_mask32 & OPT_W)
  163. #define FLAG_c (option_mask32 & OPT_c)
  164. #define FLAG_R (option_mask32 & OPT_R)
  165. static void qprintf(const char *fmt UNUSED_PARAM, ...)
  166. {
  167. /* quiet, do nothing */
  168. }
  169. static void inc_err(void)
  170. {
  171. nerr++;
  172. if (nerr > 9 && !FLAG_d_debug) {
  173. bb_error_msg_and_die("exiting after 10 errors");
  174. }
  175. }
  176. static void add_exclude(const char *directory)
  177. {
  178. struct stat sb;
  179. size_t len;
  180. if (directory == NULL || directory[0] != '/') {
  181. bb_error_msg_and_die("full path required for exclude: %s", directory);
  182. }
  183. if (lstat(directory, &sb)) {
  184. bb_error_msg("directory \"%s\" not found, ignoring", directory);
  185. return;
  186. }
  187. if ((sb.st_mode & S_IFDIR) == 0) {
  188. bb_error_msg("\"%s\" is not a directory: mode %o, ignoring",
  189. directory, sb.st_mode);
  190. return;
  191. }
  192. if (excludeCtr == MAX_EXCLUDES) {
  193. bb_error_msg_and_die("maximum excludes %d exceeded", MAX_EXCLUDES);
  194. }
  195. len = strlen(directory);
  196. while (len > 1 && directory[len - 1] == '/') {
  197. len--;
  198. }
  199. excludeArray[excludeCtr].directory = xstrndup(directory, len);
  200. excludeArray[excludeCtr++].size = len;
  201. }
  202. static bool exclude(const char *file)
  203. {
  204. int i = 0;
  205. for (i = 0; i < excludeCtr; i++) {
  206. if (strncmp(file, excludeArray[i].directory,
  207. excludeArray[i].size) == 0) {
  208. if (file[excludeArray[i].size] == '\0'
  209. || file[excludeArray[i].size] == '/') {
  210. return 1;
  211. }
  212. }
  213. }
  214. return 0;
  215. }
  216. static int match(const char *name, struct stat *sb, char **con)
  217. {
  218. int ret;
  219. char path[PATH_MAX + 1];
  220. char *tmp_path = xstrdup(name);
  221. if (excludeCtr > 0 && exclude(name)) {
  222. goto err;
  223. }
  224. ret = lstat(name, sb);
  225. if (ret) {
  226. if (FLAG_i_ignore_enoent && errno == ENOENT) {
  227. free(tmp_path);
  228. return 0;
  229. }
  230. bb_error_msg("stat(%s)", name);
  231. goto err;
  232. }
  233. if (expand_realpath) {
  234. if (S_ISLNK(sb->st_mode)) {
  235. char *p = NULL;
  236. char *file_sep;
  237. size_t len = 0;
  238. if (verbose > 1)
  239. bb_error_msg("warning! %s refers to a symbolic link, not following last component", name);
  240. file_sep = strrchr(tmp_path, '/');
  241. if (file_sep == tmp_path) {
  242. file_sep++;
  243. path[0] = '\0';
  244. p = path;
  245. } else if (file_sep) {
  246. *file_sep++ = '\0';
  247. p = realpath(tmp_path, path);
  248. } else {
  249. file_sep = tmp_path;
  250. p = realpath("./", path);
  251. }
  252. if (p)
  253. len = strlen(p);
  254. if (!p || len + strlen(file_sep) + 2 > PATH_MAX) {
  255. bb_perror_msg("realpath(%s) failed", name);
  256. goto err;
  257. }
  258. p += len;
  259. /* ensure trailing slash of directory name */
  260. if (len == 0 || p[-1] != '/') {
  261. *p++ = '/';
  262. }
  263. strcpy(p, file_sep);
  264. name = path;
  265. if (excludeCtr > 0 && exclude(name))
  266. goto err;
  267. } else {
  268. char *p;
  269. p = realpath(name, path);
  270. if (!p) {
  271. bb_perror_msg("realpath(%s)", name);
  272. goto err;
  273. }
  274. name = p;
  275. if (excludeCtr > 0 && exclude(name))
  276. goto err;
  277. }
  278. }
  279. /* name will be what is matched in the policy */
  280. if (NULL != rootpath) {
  281. if (0 != strncmp(rootpath, name, rootpathlen)) {
  282. bb_error_msg("%s is not located in %s",
  283. name, rootpath);
  284. goto err;
  285. }
  286. name += rootpathlen;
  287. }
  288. free(tmp_path);
  289. if (rootpath != NULL && name[0] == '\0')
  290. /* this is actually the root dir of the alt root */
  291. return matchpathcon_index("/", sb->st_mode, con);
  292. return matchpathcon_index(name, sb->st_mode, con);
  293. err:
  294. free(tmp_path);
  295. return -1;
  296. }
  297. /* Compare two contexts to see if their differences are "significant",
  298. * or whether the only difference is in the user. */
  299. static bool only_changed_user(const char *a, const char *b)
  300. {
  301. if (FLAG_F_force)
  302. return 0;
  303. if (!a || !b)
  304. return 0;
  305. a = strchr(a, ':'); /* Rest of the context after the user */
  306. b = strchr(b, ':');
  307. if (!a || !b)
  308. return 0;
  309. return (strcmp(a, b) == 0);
  310. }
  311. static int restore(const char *file)
  312. {
  313. char *my_file;
  314. struct stat my_sb;
  315. int i, j, ret;
  316. char *context = NULL;
  317. char *newcon = NULL;
  318. bool user_only_changed = 0;
  319. int retval = 0;
  320. my_file = bb_simplify_path(file);
  321. i = match(my_file, &my_sb, &newcon);
  322. if (i < 0) /* No matching specification. */
  323. goto out;
  324. if (FLAG_p_progress) {
  325. count++;
  326. if (count % 0x400 == 0) { /* every 1024 times */
  327. count = (count % (80*0x400));
  328. if (count == 0)
  329. bb_putchar('\n');
  330. bb_putchar('*');
  331. fflush_all();
  332. }
  333. }
  334. /*
  335. * Try to add an association between this inode and
  336. * this specification. If there is already an association
  337. * for this inode and it conflicts with this specification,
  338. * then use the last matching specification.
  339. */
  340. if (add_assoc) {
  341. j = matchpathcon_filespec_add(my_sb.st_ino, i, my_file);
  342. if (j < 0)
  343. goto err;
  344. if (j != i) {
  345. /* There was already an association and it took precedence. */
  346. goto out;
  347. }
  348. }
  349. if (FLAG_d_debug)
  350. printf("%s: %s matched by %s\n", applet_name, my_file, newcon);
  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. * 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. if (!FLAG_F_force && context && (is_context_customizable(context) > 0)) {
  373. if (verbose > 1) {
  374. bb_error_msg("skipping %s. %s is customizable_types",
  375. my_file, context);
  376. }
  377. goto out;
  378. }
  379. if (verbose) {
  380. /* If we're just doing "-v", trim out any relabels where
  381. * the user has changed but the role and type are the
  382. * same. For "-vv", emit everything. */
  383. if (verbose > 1 || !user_only_changed) {
  384. printf("%s: reset %s context %s->%s\n",
  385. applet_name, my_file, context ? context : "", newcon);
  386. }
  387. }
  388. if (FLAG_l_take_log && !user_only_changed) {
  389. if (context)
  390. printf("relabeling %s from %s to %s\n", my_file, context, newcon);
  391. else
  392. printf("labeling %s to %s\n", my_file, newcon);
  393. }
  394. if (outfile && !user_only_changed)
  395. fprintf(outfile, "%s\n", my_file);
  396. /*
  397. * Do not relabel the file if -n was used.
  398. */
  399. if (FLAG_n_dry_run || user_only_changed)
  400. goto out;
  401. /*
  402. * Relabel the file to the specified context.
  403. */
  404. ret = lsetfilecon(my_file, newcon);
  405. if (ret) {
  406. bb_perror_msg("lsetfileconon(%s,%s)", my_file, newcon);
  407. goto err;
  408. }
  409. out:
  410. freecon(context);
  411. freecon(newcon);
  412. free(my_file);
  413. return retval;
  414. err:
  415. retval--; /* -1 */
  416. goto out;
  417. }
  418. /*
  419. * Apply the last matching specification to a file.
  420. * This function is called by recursive_action on each file during
  421. * the directory traversal.
  422. */
  423. static int FAST_FUNC apply_spec(
  424. const char *file,
  425. struct stat *sb,
  426. void *userData UNUSED_PARAM,
  427. int depth UNUSED_PARAM)
  428. {
  429. if (!follow_mounts) {
  430. /* setfiles does not process across different mount points */
  431. if (sb->st_dev != dev_id) {
  432. return SKIP;
  433. }
  434. }
  435. errors |= restore(file);
  436. if (abort_on_error && errors)
  437. return FALSE;
  438. return TRUE;
  439. }
  440. static int canoncon(const char *path, unsigned lineno, char **contextp)
  441. {
  442. static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s";
  443. char *tmpcon;
  444. char *context = *contextp;
  445. int invalid = 0;
  446. #if ENABLE_FEATURE_SETFILES_CHECK_OPTION
  447. if (policyfile) {
  448. if (sepol_check_context(context) >= 0)
  449. return 0;
  450. /* Exit immediately if we're in checking mode. */
  451. bb_error_msg_and_die(err_msg, path, lineno, context);
  452. }
  453. #endif
  454. if (security_canonicalize_context_raw(context, &tmpcon) < 0) {
  455. if (errno != ENOENT) {
  456. invalid = 1;
  457. inc_err();
  458. }
  459. } else {
  460. free(context);
  461. *contextp = tmpcon;
  462. }
  463. if (invalid) {
  464. bb_error_msg(err_msg, path, lineno, context);
  465. }
  466. return invalid;
  467. }
  468. static int process_one(char *name)
  469. {
  470. struct stat sb;
  471. int rc;
  472. rc = lstat(name, &sb);
  473. if (rc < 0) {
  474. if (FLAG_i_ignore_enoent && errno == ENOENT)
  475. return 0;
  476. bb_perror_msg("stat(%s)", name);
  477. goto err;
  478. }
  479. dev_id = sb.st_dev;
  480. if (S_ISDIR(sb.st_mode) && recurse) {
  481. if (recursive_action(name,
  482. ACTION_RECURSE,
  483. apply_spec,
  484. apply_spec,
  485. NULL, 0) != TRUE
  486. ) {
  487. bb_error_msg("error while labeling %s", name);
  488. goto err;
  489. }
  490. } else {
  491. rc = restore(name);
  492. if (rc)
  493. goto err;
  494. }
  495. out:
  496. if (add_assoc) {
  497. if (FLAG_q_quiet)
  498. set_matchpathcon_printf(&qprintf);
  499. matchpathcon_filespec_eval();
  500. set_matchpathcon_printf(NULL);
  501. matchpathcon_filespec_destroy();
  502. }
  503. return rc;
  504. err:
  505. rc = -1;
  506. goto out;
  507. }
  508. int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  509. int setfiles_main(int argc UNUSED_PARAM, char **argv)
  510. {
  511. struct stat sb;
  512. int rc, i = 0;
  513. const char *input_filename = NULL;
  514. char *buf = NULL;
  515. size_t buf_len;
  516. int flags;
  517. llist_t *exclude_dir = NULL;
  518. char *out_filename = NULL;
  519. INIT_G();
  520. if (applet_name[0] == 's') { /* "setfiles" */
  521. /*
  522. * setfiles:
  523. * Recursive descent,
  524. * Does not expand paths via realpath,
  525. * Aborts on errors during the file tree walk,
  526. * Try to track inode associations for conflict detection,
  527. * Does not follow mounts,
  528. * Validates all file contexts at init time.
  529. */
  530. recurse = 1;
  531. abort_on_error = 1;
  532. add_assoc = 1;
  533. /* follow_mounts = 0; - already is */
  534. matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS;
  535. } else {
  536. /*
  537. * restorecon:
  538. * No recursive descent unless -r/-R,
  539. * Expands paths via realpath,
  540. * Do not abort on errors during the file tree walk,
  541. * Do not try to track inode associations for conflict detection,
  542. * Follows mounts,
  543. * Does lazy validation of contexts upon use.
  544. */
  545. expand_realpath = 1;
  546. follow_mounts = 1;
  547. matchpathcon_flags = MATCHPATHCON_NOTRANS;
  548. /* restorecon only */
  549. selinux_or_die();
  550. }
  551. set_matchpathcon_flags(matchpathcon_flags);
  552. opt_complementary = "vv:v--p:p--v:v--q:q--v";
  553. /* Option order must match OPT_x definitions! */
  554. if (applet_name[0] == 'r') { /* restorecon */
  555. flags = getopt32(argv, "de:*f:ilnpqrsvo:FWR",
  556. &exclude_dir, &input_filename, &out_filename, &verbose);
  557. } else { /* setfiles */
  558. flags = getopt32(argv, "de:*f:ilnpqr:svo:FW"
  559. IF_FEATURE_SETFILES_CHECK_OPTION("c:"),
  560. &exclude_dir, &input_filename, &rootpath, &out_filename,
  561. IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
  562. &verbose);
  563. }
  564. argv += optind;
  565. #if ENABLE_FEATURE_SETFILES_CHECK_OPTION
  566. if ((applet_name[0] == 's') && (flags & OPT_c)) {
  567. FILE *policystream;
  568. policystream = xfopen_for_read(policyfile);
  569. if (sepol_set_policydb_from_file(policystream) < 0) {
  570. bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
  571. }
  572. fclose(policystream);
  573. /* Only process the specified file_contexts file, not
  574. * any .homedirs or .local files, and do not perform
  575. * context translations. */
  576. set_matchpathcon_flags(MATCHPATHCON_BASEONLY |
  577. MATCHPATHCON_NOTRANS |
  578. MATCHPATHCON_VALIDATE);
  579. }
  580. #endif
  581. while (exclude_dir)
  582. add_exclude(llist_pop(&exclude_dir));
  583. if (flags & OPT_o) {
  584. outfile = stdout;
  585. if (NOT_LONE_CHAR(out_filename, '-')) {
  586. outfile = xfopen_for_write(out_filename);
  587. }
  588. }
  589. if (applet_name[0] == 'r') { /* restorecon */
  590. if (flags & (OPT_r | OPT_R))
  591. recurse = 1;
  592. } else { /* setfiles */
  593. if (flags & OPT_r)
  594. rootpathlen = strlen(rootpath);
  595. }
  596. if (flags & OPT_s) {
  597. input_filename = "-";
  598. add_assoc = 0;
  599. }
  600. if (applet_name[0] == 's') { /* setfiles */
  601. /* Use our own invalid context checking function so that
  602. * we can support either checking against the active policy or
  603. * checking against a binary policy file. */
  604. set_matchpathcon_canoncon(&canoncon);
  605. if (!argv[0])
  606. bb_show_usage();
  607. xstat(argv[0], &sb);
  608. if (!S_ISREG(sb.st_mode)) {
  609. bb_error_msg_and_die("spec file %s is not a regular file", argv[0]);
  610. }
  611. /* Load the file contexts configuration and check it. */
  612. rc = matchpathcon_init(argv[0]);
  613. if (rc < 0) {
  614. bb_simple_perror_msg_and_die(argv[0]);
  615. }
  616. if (nerr)
  617. exit(EXIT_FAILURE);
  618. argv++;
  619. }
  620. if (input_filename) {
  621. ssize_t len;
  622. FILE *f = stdin;
  623. if (NOT_LONE_CHAR(input_filename, '-'))
  624. f = xfopen_for_read(input_filename);
  625. while ((len = getline(&buf, &buf_len, f)) > 0) {
  626. buf[len - 1] = '\0';
  627. errors |= process_one(buf);
  628. }
  629. if (ENABLE_FEATURE_CLEAN_UP)
  630. fclose_if_not_stdin(f);
  631. } else {
  632. if (!argv[0])
  633. bb_show_usage();
  634. for (i = 0; argv[i]; i++) {
  635. errors |= process_one(argv[i]);
  636. }
  637. }
  638. if (FLAG_W_warn_no_match)
  639. matchpathcon_checkmatches(argv[0]);
  640. if (ENABLE_FEATURE_CLEAN_UP && outfile)
  641. fclose(outfile);
  642. return errors;
  643. }