matchpathcon.c 2.2 KB

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