matchpathcon.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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"
  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. opt_complementary = "-1" /* at least one param reqd */
  53. ":?:f--p:p--f"; /* mutually exclusive */
  54. opts = getopt32(argv, "nNf:p:V", &fcontext, &prefix);
  55. argv += optind;
  56. if (opts & OPT_NOT_TRANS) {
  57. set_matchpathcon_flags(MATCHPATHCON_NOTRANS);
  58. }
  59. if (opts & OPT_FCONTEXT) {
  60. if (matchpathcon_init(fcontext))
  61. bb_perror_msg_and_die("error while processing %s", fcontext);
  62. }
  63. if (opts & OPT_PREFIX) {
  64. if (matchpathcon_init_prefix(NULL, prefix))
  65. bb_perror_msg_and_die("error while processing %s", prefix);
  66. }
  67. while ((path = *argv++) != NULL) {
  68. security_context_t con;
  69. int rc;
  70. if (!(opts & OPT_VERIFY)) {
  71. error += print_matchpathcon(path, opts & OPT_NOT_PRINT);
  72. continue;
  73. }
  74. if (selinux_file_context_verify(path, 0)) {
  75. printf("%s verified\n", path);
  76. continue;
  77. }
  78. if (opts & OPT_NOT_TRANS)
  79. rc = lgetfilecon_raw(path, &con);
  80. else
  81. rc = lgetfilecon(path, &con);
  82. if (rc >= 0) {
  83. printf("%s has context %s, should be ", path, con);
  84. error += print_matchpathcon(path, 1);
  85. freecon(con);
  86. continue;
  87. }
  88. printf("actual context unknown: %s, should be ", strerror(errno));
  89. error += print_matchpathcon(path, 1);
  90. }
  91. matchpathcon_fini();
  92. return error;
  93. }