setfiles.c 18 KB

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