restorecon.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* restorecon.c - Restore default security contexts for files
  2. *
  3. * Copyright 2015 The Android Open Source Project
  4. USE_RESTORECON(NEWTOY(restorecon, "<1DFnRrv", TOYFLAG_USR|TOYFLAG_SBIN))
  5. config RESTORECON
  6. bool "restorecon"
  7. depends on TOYBOX_SELINUX
  8. default y
  9. help
  10. usage: restorecon [-D] [-F] [-R] [-n] [-v] FILE...
  11. Restores the default security contexts for the given files.
  12. -D Apply to /data/data too
  13. -F Force reset
  14. -R Recurse into directories
  15. -n Don't make any changes; useful with -v to see what would change
  16. -v Verbose
  17. */
  18. #define FOR_restorecon
  19. #include "toys.h"
  20. #if defined(__ANDROID__)
  21. #include <selinux/android.h>
  22. #endif
  23. void restorecon_main(void)
  24. {
  25. #if defined(__ANDROID__)
  26. char **s;
  27. int flags = 0;
  28. if (toys.optflags & FLAG_D) flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
  29. if (toys.optflags & FLAG_F) flags |= SELINUX_ANDROID_RESTORECON_FORCE;
  30. if (toys.optflags & (FLAG_R|FLAG_r))
  31. flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
  32. if (toys.optflags & FLAG_n) flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
  33. if (toys.optflags & FLAG_v) flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
  34. for (s = toys.optargs; *s; s++)
  35. if (selinux_android_restorecon(*s, flags) < 0)
  36. perror_msg("restorecon failed: %s", *s);
  37. #endif
  38. }