3
0

setfiles.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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(
  425. const char *file,
  426. struct stat *sb,
  427. void *userData UNUSED_PARAM,
  428. int depth UNUSED_PARAM)
  429. {
  430. if (!follow_mounts) {
  431. /* setfiles does not process across different mount points */
  432. if (sb->st_dev != dev_id) {
  433. return SKIP;
  434. }
  435. }
  436. errors |= restore(file);
  437. if (abort_on_error && errors)
  438. return FALSE;
  439. return TRUE;
  440. }
  441. static int canoncon(const char *path, unsigned lineno, char **contextp)
  442. {
  443. static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s";
  444. char *tmpcon;
  445. char *context = *contextp;
  446. int invalid = 0;
  447. #if ENABLE_FEATURE_SETFILES_CHECK_OPTION
  448. if (policyfile) {
  449. if (sepol_check_context(context) >= 0)
  450. return 0;
  451. /* Exit immediately if we're in checking mode. */
  452. bb_error_msg_and_die(err_msg, path, lineno, context);
  453. }
  454. #endif
  455. if (security_canonicalize_context_raw(context, &tmpcon) < 0) {
  456. if (errno != ENOENT) {
  457. invalid = 1;
  458. inc_err();
  459. }
  460. } else {
  461. free(context);
  462. *contextp = tmpcon;
  463. }
  464. if (invalid) {
  465. bb_error_msg(err_msg, path, lineno, context);
  466. }
  467. return invalid;
  468. }
  469. static int process_one(char *name)
  470. {
  471. struct stat sb;
  472. int rc;
  473. rc = lstat(name, &sb);
  474. if (rc < 0) {
  475. if (FLAG_i_ignore_enoent && errno == ENOENT)
  476. return 0;
  477. bb_perror_msg("stat(%s)", name);
  478. goto err;
  479. }
  480. dev_id = sb.st_dev;
  481. if (S_ISDIR(sb.st_mode) && recurse) {
  482. if (recursive_action(name,
  483. ACTION_RECURSE,
  484. apply_spec,
  485. apply_spec,
  486. NULL, 0) != TRUE
  487. ) {
  488. bb_error_msg("error while labeling %s", name);
  489. goto err;
  490. }
  491. } else {
  492. rc = restore(name);
  493. if (rc)
  494. goto err;
  495. }
  496. out:
  497. if (add_assoc) {
  498. if (FLAG_q_quiet)
  499. set_matchpathcon_printf(&qprintf);
  500. matchpathcon_filespec_eval();
  501. set_matchpathcon_printf(NULL);
  502. matchpathcon_filespec_destroy();
  503. }
  504. return rc;
  505. err:
  506. rc = -1;
  507. goto out;
  508. }
  509. int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  510. int setfiles_main(int argc UNUSED_PARAM, char **argv)
  511. {
  512. struct stat sb;
  513. int rc, i = 0;
  514. const char *input_filename = NULL;
  515. char *buf = NULL;
  516. size_t buf_len;
  517. int flags;
  518. llist_t *exclude_dir = NULL;
  519. char *out_filename = NULL;
  520. INIT_G();
  521. if (applet_name[0] == 's') { /* "setfiles" */
  522. /*
  523. * setfiles:
  524. * Recursive descent,
  525. * Does not expand paths via realpath,
  526. * Aborts on errors during the file tree walk,
  527. * Try to track inode associations for conflict detection,
  528. * Does not follow mounts,
  529. * Validates all file contexts at init time.
  530. */
  531. recurse = 1;
  532. abort_on_error = 1;
  533. add_assoc = 1;
  534. /* follow_mounts = 0; - already is */
  535. matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS;
  536. } else {
  537. /*
  538. * restorecon:
  539. * No recursive descent unless -r/-R,
  540. * Expands paths via realpath,
  541. * Do not abort on errors during the file tree walk,
  542. * Do not try to track inode associations for conflict detection,
  543. * Follows mounts,
  544. * Does lazy validation of contexts upon use.
  545. */
  546. expand_realpath = 1;
  547. follow_mounts = 1;
  548. matchpathcon_flags = MATCHPATHCON_NOTRANS;
  549. /* restorecon only */
  550. selinux_or_die();
  551. }
  552. set_matchpathcon_flags(matchpathcon_flags);
  553. /* Option order must match OPT_x definitions! */
  554. if (applet_name[0] == 'r') { /* restorecon */
  555. flags = getopt32(argv, "^"
  556. "de:*f:ilnpqrsvo:FWR"
  557. "\0"
  558. "vv:v--p:p--v:v--q:q--v",
  559. &exclude_dir, &input_filename, &out_filename,
  560. &verbose
  561. );
  562. } else { /* setfiles */
  563. flags = getopt32(argv, "^"
  564. "de:*f:ilnpqr:svo:FW"
  565. IF_FEATURE_SETFILES_CHECK_OPTION("c:")
  566. "\0"
  567. "vv:v--p:p--v:v--q:q--v",
  568. &exclude_dir, &input_filename, &rootpath, &out_filename,
  569. IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
  570. &verbose
  571. );
  572. }
  573. argv += optind;
  574. #if ENABLE_FEATURE_SETFILES_CHECK_OPTION
  575. if ((applet_name[0] == 's') && (flags & OPT_c)) {
  576. FILE *policystream;
  577. policystream = xfopen_for_read(policyfile);
  578. if (sepol_set_policydb_from_file(policystream) < 0) {
  579. bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
  580. }
  581. fclose(policystream);
  582. /* Only process the specified file_contexts file, not
  583. * any .homedirs or .local files, and do not perform
  584. * context translations. */
  585. set_matchpathcon_flags(MATCHPATHCON_BASEONLY |
  586. MATCHPATHCON_NOTRANS |
  587. MATCHPATHCON_VALIDATE);
  588. }
  589. #endif
  590. while (exclude_dir)
  591. add_exclude(llist_pop(&exclude_dir));
  592. if (flags & OPT_o) {
  593. outfile = stdout;
  594. if (NOT_LONE_CHAR(out_filename, '-')) {
  595. outfile = xfopen_for_write(out_filename);
  596. }
  597. }
  598. if (applet_name[0] == 'r') { /* restorecon */
  599. if (flags & (OPT_r | OPT_R))
  600. recurse = 1;
  601. } else { /* setfiles */
  602. if (flags & OPT_r)
  603. rootpathlen = strlen(rootpath);
  604. }
  605. if (flags & OPT_s) {
  606. input_filename = "-";
  607. add_assoc = 0;
  608. }
  609. if (applet_name[0] == 's') { /* setfiles */
  610. /* Use our own invalid context checking function so that
  611. * we can support either checking against the active policy or
  612. * checking against a binary policy file. */
  613. set_matchpathcon_canoncon(&canoncon);
  614. if (!argv[0])
  615. bb_show_usage();
  616. xstat(argv[0], &sb);
  617. if (!S_ISREG(sb.st_mode)) {
  618. bb_error_msg_and_die("'%s' is not a regular file", argv[0]);
  619. }
  620. /* Load the file contexts configuration and check it. */
  621. rc = matchpathcon_init(argv[0]);
  622. if (rc < 0) {
  623. bb_simple_perror_msg_and_die(argv[0]);
  624. }
  625. if (nerr)
  626. exit(EXIT_FAILURE);
  627. argv++;
  628. }
  629. if (input_filename) {
  630. ssize_t len;
  631. FILE *f = stdin;
  632. if (NOT_LONE_CHAR(input_filename, '-'))
  633. f = xfopen_for_read(input_filename);
  634. while ((len = getline(&buf, &buf_len, f)) > 0) {
  635. buf[len - 1] = '\0';
  636. errors |= process_one(buf);
  637. }
  638. if (ENABLE_FEATURE_CLEAN_UP)
  639. fclose_if_not_stdin(f);
  640. } else {
  641. if (!argv[0])
  642. bb_show_usage();
  643. for (i = 0; argv[i]; i++) {
  644. errors |= process_one(argv[i]);
  645. }
  646. }
  647. if (FLAG_W_warn_no_match)
  648. matchpathcon_checkmatches(argv[0]);
  649. if (ENABLE_FEATURE_CLEAN_UP && outfile)
  650. fclose(outfile);
  651. return errors;
  652. }