3
0

sestatus.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * sestatus -- displays the status of SELinux
  3. *
  4. * Ported to busybox: KaiGai Kohei <kaigai@ak.jp.nec.com>
  5. *
  6. * Copyright (C) KaiGai Kohei <kaigai@ak.jp.nec.com>
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  9. */
  10. //usage:#define sestatus_trivial_usage
  11. //usage: "[-vb]"
  12. //usage:#define sestatus_full_usage "\n\n"
  13. //usage: " -v Verbose"
  14. //usage: "\n -b Display current state of booleans"
  15. #include "libbb.h"
  16. extern char *selinux_mnt;
  17. #define OPT_VERBOSE (1 << 0)
  18. #define OPT_BOOLEAN (1 << 1)
  19. #define COL_FMT "%-31s "
  20. static void display_boolean(void)
  21. {
  22. char **bools;
  23. int i, active, pending, nbool;
  24. if (security_get_boolean_names(&bools, &nbool) < 0)
  25. return;
  26. puts("\nPolicy booleans:");
  27. for (i = 0; i < nbool; i++) {
  28. active = security_get_boolean_active(bools[i]);
  29. if (active < 0)
  30. goto skip;
  31. pending = security_get_boolean_pending(bools[i]);
  32. if (pending < 0)
  33. goto skip;
  34. printf(COL_FMT "%s",
  35. bools[i], active == 0 ? "off" : "on");
  36. if (active != pending)
  37. printf(" (%sactivate pending)", pending == 0 ? "in" : "");
  38. bb_putchar('\n');
  39. skip:
  40. if (ENABLE_FEATURE_CLEAN_UP)
  41. free(bools[i]);
  42. }
  43. if (ENABLE_FEATURE_CLEAN_UP)
  44. free(bools);
  45. }
  46. static void read_config(char **pc, int npc, char **fc, int nfc)
  47. {
  48. char *buf;
  49. parser_t *parser;
  50. int pc_ofs = 0, fc_ofs = 0, section = -1;
  51. pc[0] = fc[0] = NULL;
  52. parser = config_open("/etc/sestatus.conf");
  53. while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
  54. if (strcmp(buf, "[process]") == 0) {
  55. section = 1;
  56. } else if (strcmp(buf, "[files]") == 0) {
  57. section = 2;
  58. } else {
  59. if (section == 1 && pc_ofs < npc -1) {
  60. pc[pc_ofs++] = xstrdup(buf);
  61. pc[pc_ofs] = NULL;
  62. } else if (section == 2 && fc_ofs < nfc - 1) {
  63. fc[fc_ofs++] = xstrdup(buf);
  64. fc[fc_ofs] = NULL;
  65. }
  66. }
  67. }
  68. config_close(parser);
  69. }
  70. static void display_verbose(void)
  71. {
  72. security_context_t con, _con;
  73. char *fc[50], *pc[50], *cterm;
  74. pid_t *pidList;
  75. int i;
  76. read_config(pc, ARRAY_SIZE(pc), fc, ARRAY_SIZE(fc));
  77. /* process contexts */
  78. puts("\nProcess contexts:");
  79. /* current context */
  80. if (getcon(&con) == 0) {
  81. printf(COL_FMT "%s\n", "Current context:", con);
  82. if (ENABLE_FEATURE_CLEAN_UP)
  83. freecon(con);
  84. }
  85. /* /sbin/init context */
  86. if (getpidcon(1, &con) == 0) {
  87. printf(COL_FMT "%s\n", "Init context:", con);
  88. if (ENABLE_FEATURE_CLEAN_UP)
  89. freecon(con);
  90. }
  91. /* [process] context */
  92. for (i = 0; pc[i] != NULL; i++) {
  93. pidList = find_pid_by_name(bb_basename(pc[i]));
  94. if (pidList[0] > 0 && getpidcon(pidList[0], &con) == 0) {
  95. printf(COL_FMT "%s\n", pc[i], con);
  96. if (ENABLE_FEATURE_CLEAN_UP)
  97. freecon(con);
  98. }
  99. if (ENABLE_FEATURE_CLEAN_UP)
  100. free(pidList);
  101. }
  102. /* files contexts */
  103. puts("\nFile contexts:");
  104. cterm = xmalloc_ttyname(0);
  105. //FIXME: if cterm == NULL, we segfault!??
  106. puts(cterm);
  107. if (cterm && lgetfilecon(cterm, &con) >= 0) {
  108. printf(COL_FMT "%s\n", "Controlling term:", con);
  109. if (ENABLE_FEATURE_CLEAN_UP)
  110. freecon(con);
  111. }
  112. for (i = 0; fc[i] != NULL; i++) {
  113. struct stat stbuf;
  114. if (lgetfilecon(fc[i], &con) < 0)
  115. continue;
  116. if (lstat(fc[i], &stbuf) == 0) {
  117. if (S_ISLNK(stbuf.st_mode)) {
  118. if (getfilecon(fc[i], &_con) >= 0) {
  119. printf(COL_FMT "%s -> %s\n", fc[i], _con, con);
  120. if (ENABLE_FEATURE_CLEAN_UP)
  121. freecon(_con);
  122. }
  123. } else {
  124. printf(COL_FMT "%s\n", fc[i], con);
  125. }
  126. }
  127. if (ENABLE_FEATURE_CLEAN_UP)
  128. freecon(con);
  129. }
  130. }
  131. int sestatus_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  132. int sestatus_main(int argc UNUSED_PARAM, char **argv)
  133. {
  134. unsigned opts;
  135. const char *pol_path;
  136. int rc;
  137. opt_complementary = "?0"; /* no arguments are required. */
  138. opts = getopt32(argv, "vb");
  139. /* SELinux status: line */
  140. rc = is_selinux_enabled();
  141. if (rc < 0)
  142. goto error;
  143. printf(COL_FMT "%s\n", "SELinux status:",
  144. rc == 1 ? "enabled" : "disabled");
  145. /* SELinuxfs mount: line */
  146. if (!selinux_mnt)
  147. goto error;
  148. printf(COL_FMT "%s\n", "SELinuxfs mount:",
  149. selinux_mnt);
  150. /* Current mode: line */
  151. rc = security_getenforce();
  152. if (rc < 0)
  153. goto error;
  154. printf(COL_FMT "%s\n", "Current mode:",
  155. rc == 0 ? "permissive" : "enforcing");
  156. /* Mode from config file: line */
  157. if (selinux_getenforcemode(&rc) != 0)
  158. goto error;
  159. printf(COL_FMT "%s\n", "Mode from config file:",
  160. rc < 0 ? "disabled" : (rc == 0 ? "permissive" : "enforcing"));
  161. /* Policy version: line */
  162. rc = security_policyvers();
  163. if (rc < 0)
  164. goto error;
  165. printf(COL_FMT "%u\n", "Policy version:", rc);
  166. /* Policy from config file: line */
  167. pol_path = selinux_policy_root();
  168. if (!pol_path)
  169. goto error;
  170. printf(COL_FMT "%s\n", "Policy from config file:",
  171. bb_basename(pol_path));
  172. if (opts & OPT_BOOLEAN)
  173. display_boolean();
  174. if (opts & OPT_VERBOSE)
  175. display_verbose();
  176. return 0;
  177. error:
  178. bb_perror_msg_and_die("libselinux returns unknown state");
  179. }