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