3
0

matchpathcon.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. //config:config MATCHPATHCON
  9. //config: bool "matchpathcon (6.1 kb)"
  10. //config: default n
  11. //config: depends on SELINUX
  12. //config: help
  13. //config: Enable support to get default security context of the
  14. //config: specified path from the file contexts configuration.
  15. //applet:IF_MATCHPATHCON(APPLET(matchpathcon, BB_DIR_USR_SBIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_MATCHPATHCON) += matchpathcon.o
  17. //usage:#define matchpathcon_trivial_usage
  18. //usage: "[-n] [-N] [-f file_contexts_file] [-p prefix] [-V]"
  19. //usage:#define matchpathcon_full_usage "\n\n"
  20. //usage: " -n Don't display path"
  21. //usage: "\n -N Don't use translations"
  22. //usage: "\n -f Use alternate file_context file"
  23. //usage: "\n -p Use prefix to speed translations"
  24. //usage: "\n -V Verify file context on disk matches defaults"
  25. #include "libbb.h"
  26. static int print_matchpathcon(char *path, int noprint)
  27. {
  28. char *buf;
  29. int rc = matchpathcon(path, 0, &buf);
  30. if (rc < 0) {
  31. bb_perror_msg("matchpathcon(%s) failed", path);
  32. return 1;
  33. }
  34. if (!noprint)
  35. printf("%s\t%s\n", path, buf);
  36. else
  37. puts(buf);
  38. freecon(buf);
  39. return 0;
  40. }
  41. #define OPT_NOT_PRINT (1<<0) /* -n */
  42. #define OPT_NOT_TRANS (1<<1) /* -N */
  43. #define OPT_FCONTEXT (1<<2) /* -f */
  44. #define OPT_PREFIX (1<<3) /* -p */
  45. #define OPT_VERIFY (1<<4) /* -V */
  46. int matchpathcon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  47. int matchpathcon_main(int argc UNUSED_PARAM, char **argv)
  48. {
  49. int error = 0;
  50. unsigned opts;
  51. char *fcontext, *prefix, *path;
  52. opts = getopt32(argv, "^"
  53. "nNf:p:V"
  54. "\0"
  55. "-1" /* at least one param reqd */
  56. ":?:f--p:p--f" /* mutually exclusive */
  57. , &fcontext, &prefix
  58. );
  59. argv += optind;
  60. if (opts & OPT_NOT_TRANS) {
  61. set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
  62. }
  63. if (opts & OPT_FCONTEXT) {
  64. if (matchpathcon_init(fcontext))
  65. bb_perror_msg_and_die("error while processing %s", fcontext);
  66. }
  67. if (opts & OPT_PREFIX) {
  68. if (matchpathcon_init_prefix(NULL, prefix))
  69. bb_perror_msg_and_die("error while processing %s", prefix);
  70. }
  71. while ((path = *argv++) != NULL) {
  72. security_context_t con;
  73. int rc;
  74. if (!(opts & OPT_VERIFY)) {
  75. error += print_matchpathcon(path, opts & OPT_NOT_PRINT);
  76. continue;
  77. }
  78. if (selinux_file_context_verify(path, 0)) {
  79. printf("%s verified\n", path);
  80. continue;
  81. }
  82. if (opts & OPT_NOT_TRANS)
  83. rc = lgetfilecon_raw(path, &con);
  84. else
  85. rc = lgetfilecon(path, &con);
  86. if (rc >= 0) {
  87. printf("%s has context %s, should be ", path, con);
  88. error += print_matchpathcon(path, 1);
  89. freecon(con);
  90. continue;
  91. }
  92. printf("actual context unknown: "STRERROR_FMT", should be " STRERROR_ERRNO);
  93. error += print_matchpathcon(path, 1);
  94. }
  95. matchpathcon_fini();
  96. return error;
  97. }