matchpathcon.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* matchpathcon - get the default security context for the specified
  2. * path from the file contexts configuration.
  3. * based on libselinux-1.32
  4. * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
  5. *
  6. */
  7. #include "libbb.h"
  8. static int print_matchpathcon(char *path, int noprint)
  9. {
  10. char *buf;
  11. int rc = matchpathcon(path, 0, &buf);
  12. if (rc < 0) {
  13. bb_perror_msg("matchpathcon(%s) failed", path);
  14. return 1;
  15. }
  16. if (!noprint)
  17. printf("%s\t%s\n", path, buf);
  18. else
  19. puts(buf);
  20. freecon(buf);
  21. return 0;
  22. }
  23. #define OPT_NOT_PRINT (1<<0) /* -n */
  24. #define OPT_NOT_TRANS (1<<1) /* -N */
  25. #define OPT_FCONTEXT (1<<2) /* -f */
  26. #define OPT_PREFIX (1<<3) /* -p */
  27. #define OPT_VERIFY (1<<4) /* -V */
  28. int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  29. int matchpathcon_main(int argc UNUSED_PARAM, char **argv)
  30. {
  31. int error = 0;
  32. unsigned opts;
  33. char *fcontext, *prefix, *path;
  34. opt_complementary = "-1" /* at least one param reqd */
  35. ":?:f--p:p--f"; /* mutually exclusive */
  36. opts = getopt32(argv, "nNf:p:V", &fcontext, &prefix);
  37. argv += optind;
  38. if (opts & OPT_NOT_TRANS) {
  39. set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
  40. }
  41. if (opts & OPT_FCONTEXT) {
  42. if (matchpathcon_init(fcontext))
  43. bb_perror_msg_and_die("error while processing %s", fcontext);
  44. }
  45. if (opts & OPT_PREFIX) {
  46. if (matchpathcon_init_prefix(NULL, prefix))
  47. bb_perror_msg_and_die("error while processing %s", prefix);
  48. }
  49. while ((path = *argv++) != NULL) {
  50. security_context_t con;
  51. int rc;
  52. if (!(opts & OPT_VERIFY)) {
  53. error += print_matchpathcon(path, opts & OPT_NOT_PRINT);
  54. continue;
  55. }
  56. if (selinux_file_context_verify(path, 0)) {
  57. printf("%s verified\n", path);
  58. continue;
  59. }
  60. if (opts & OPT_NOT_TRANS)
  61. rc = lgetfilecon_raw(path, &con);
  62. else
  63. rc = lgetfilecon(path, &con);
  64. if (rc >= 0) {
  65. printf("%s has context %s, should be ", path, con);
  66. error += print_matchpathcon(path, 1);
  67. freecon(con);
  68. continue;
  69. }
  70. printf("actual context unknown: %s, should be ", strerror(errno));
  71. error += print_matchpathcon(path, 1);
  72. }
  73. matchpathcon_fini();
  74. return error;
  75. }