3
0

sestatus.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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[256];
  42. FILE *fp;
  43. int pc_ofs = 0, fc_ofs = 0, section = -1;
  44. pc[0] = fc[0] = NULL;
  45. fp = fopen("/etc/sestatus.conf", "rb");
  46. if (fp == NULL)
  47. return;
  48. while (fgets(buf, sizeof(buf), fp) != NULL) {
  49. int i, c;
  50. /* kills comments */
  51. for (i = 0; (c = buf[i]) != '\0'; i++) {
  52. if (c == '#') {
  53. buf[i] = '\0';
  54. break;
  55. }
  56. }
  57. trim(buf);
  58. if (buf[0] == '\0')
  59. continue;
  60. if (strcmp(buf, "[process]") == 0) {
  61. section = 1;
  62. } else if (strcmp(buf, "[files]") == 0) {
  63. section = 2;
  64. } else {
  65. if (section == 1 && pc_ofs < npc -1) {
  66. pc[pc_ofs++] = xstrdup(buf);
  67. pc[pc_ofs] = NULL;
  68. } else if (section == 2 && fc_ofs < nfc - 1) {
  69. fc[fc_ofs++] = xstrdup(buf);
  70. fc[fc_ofs] = NULL;
  71. }
  72. }
  73. }
  74. fclose(fp);
  75. }
  76. static void display_verbose(void)
  77. {
  78. security_context_t con, _con;
  79. char *fc[50], *pc[50], *cterm;
  80. pid_t *pidList;
  81. int i;
  82. read_config(pc, ARRAY_SIZE(pc), fc, ARRAY_SIZE(fc));
  83. /* process contexts */
  84. puts("\nProcess contexts:");
  85. /* current context */
  86. if (getcon(&con) == 0) {
  87. printf(COL_FMT "%s\n", "Current context:", con);
  88. if (ENABLE_FEATURE_CLEAN_UP)
  89. freecon(con);
  90. }
  91. /* /sbin/init context */
  92. if (getpidcon(1, &con) == 0) {
  93. printf(COL_FMT "%s\n", "Init context:", con);
  94. if (ENABLE_FEATURE_CLEAN_UP)
  95. freecon(con);
  96. }
  97. /* [process] context */
  98. for (i = 0; pc[i] != NULL; i++) {
  99. pidList = find_pid_by_name(bb_basename(pc[i]));
  100. if (pidList[0] > 0 && getpidcon(pidList[0], &con) == 0) {
  101. printf(COL_FMT "%s\n", pc[i], con);
  102. if (ENABLE_FEATURE_CLEAN_UP)
  103. freecon(con);
  104. }
  105. if (ENABLE_FEATURE_CLEAN_UP)
  106. free(pidList);
  107. }
  108. /* files contexts */
  109. puts("\nFile contexts:");
  110. cterm = ttyname(0);
  111. puts(cterm);
  112. if (cterm && lgetfilecon(cterm, &con) >= 0) {
  113. printf(COL_FMT "%s\n", "Controlling term:", con);
  114. if (ENABLE_FEATURE_CLEAN_UP)
  115. freecon(con);
  116. }
  117. for (i=0; fc[i] != NULL; i++) {
  118. struct stat stbuf;
  119. if (lgetfilecon(fc[i], &con) < 0)
  120. continue;
  121. if (lstat(fc[i], &stbuf) == 0) {
  122. if (S_ISLNK(stbuf.st_mode)) {
  123. if (getfilecon(fc[i], &_con) >= 0) {
  124. printf(COL_FMT "%s -> %s\n", fc[i], _con, con);
  125. if (ENABLE_FEATURE_CLEAN_UP)
  126. freecon(_con);
  127. }
  128. } else {
  129. printf(COL_FMT "%s\n", fc[i], con);
  130. }
  131. }
  132. if (ENABLE_FEATURE_CLEAN_UP)
  133. freecon(con);
  134. }
  135. }
  136. int sestatus_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  137. int sestatus_main(int argc ATTRIBUTE_UNUSED, char **argv)
  138. {
  139. unsigned opts;
  140. const char *pol_path;
  141. int rc;
  142. opt_complementary = "?0"; /* no arguments are required. */
  143. opts = getopt32(argv, "vb");
  144. /* SELinux status: line */
  145. rc = is_selinux_enabled();
  146. if (rc < 0)
  147. goto error;
  148. printf(COL_FMT "%s\n", "SELinux status:",
  149. rc == 1 ? "enabled" : "disabled");
  150. /* SELinuxfs mount: line */
  151. if (!selinux_mnt)
  152. goto error;
  153. printf(COL_FMT "%s\n", "SELinuxfs mount:",
  154. selinux_mnt);
  155. /* Current mode: line */
  156. rc = security_getenforce();
  157. if (rc < 0)
  158. goto error;
  159. printf(COL_FMT "%s\n", "Current mode:",
  160. rc == 0 ? "permissive" : "enforcing");
  161. /* Mode from config file: line */
  162. if (selinux_getenforcemode(&rc) != 0)
  163. goto error;
  164. printf(COL_FMT "%s\n", "Mode from config file:",
  165. rc < 0 ? "disabled" : (rc == 0 ? "permissive" : "enforcing"));
  166. /* Policy version: line */
  167. rc = security_policyvers();
  168. if (rc < 0)
  169. goto error;
  170. printf(COL_FMT "%u\n", "Policy version:", rc);
  171. /* Policy from config file: line */
  172. pol_path = selinux_policy_root();
  173. if (!pol_path)
  174. goto error;
  175. printf(COL_FMT "%s\n", "Policy from config file:",
  176. bb_basename(pol_path));
  177. if (opts & OPT_BOOLEAN)
  178. display_boolean();
  179. if (opts & OPT_VERBOSE)
  180. display_verbose();
  181. return 0;
  182. error:
  183. bb_perror_msg_and_die("libselinux returns unknown state");
  184. }