matchpathcon.c 2.6 KB

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