3
0

sestatus.c 4.5 KB

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