setfiles.c 19 KB

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