sestatus.c 5.0 KB

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