setfiles.c 18 KB

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