2 * sestatus -- displays the status of SELinux
4 * Ported to busybox: KaiGai Kohei <kaigai@ak.jp.nec.com>
6 * Copyright (C) KaiGai Kohei <kaigai@ak.jp.nec.com>
8 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //config:config SESTATUS
11 //config: bool "sestatus (12 kb)"
13 //config: depends on SELINUX
15 //config: Displays the status of SELinux.
17 //applet:IF_SESTATUS(APPLET(sestatus, BB_DIR_USR_SBIN, BB_SUID_DROP))
19 //kbuild:lib-$(CONFIG_SESTATUS) += sestatus.o
21 //usage:#define sestatus_trivial_usage
23 //usage:#define sestatus_full_usage "\n\n"
24 //usage: " -v Verbose"
25 //usage: "\n -b Display current state of booleans"
29 extern char *selinux_mnt;
31 #define OPT_VERBOSE (1 << 0)
32 #define OPT_BOOLEAN (1 << 1)
34 #define COL_FMT "%-31s "
36 static void display_boolean(void)
39 int i, active, pending, nbool;
41 if (security_get_boolean_names(&bools, &nbool) < 0)
44 puts("\nPolicy booleans:");
46 for (i = 0; i < nbool; i++) {
47 active = security_get_boolean_active(bools[i]);
50 pending = security_get_boolean_pending(bools[i]);
54 bools[i], active == 0 ? "off" : "on");
55 if (active != pending)
56 printf(" (%sactivate pending)", pending == 0 ? "in" : "");
59 if (ENABLE_FEATURE_CLEAN_UP)
62 if (ENABLE_FEATURE_CLEAN_UP)
66 static void read_config(char **pc, int npc, char **fc, int nfc)
70 int pc_ofs = 0, fc_ofs = 0, section = -1;
74 parser = config_open("/etc/sestatus.conf");
75 while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
76 if (strcmp(buf, "[process]") == 0) {
78 } else if (strcmp(buf, "[files]") == 0) {
81 if (section == 1 && pc_ofs < npc -1) {
82 pc[pc_ofs++] = xstrdup(buf);
84 } else if (section == 2 && fc_ofs < nfc - 1) {
85 fc[fc_ofs++] = xstrdup(buf);
93 static void display_verbose(void)
95 security_context_t con, _con;
96 char *fc[50], *pc[50], *cterm;
100 read_config(pc, ARRAY_SIZE(pc), fc, ARRAY_SIZE(fc));
102 /* process contexts */
103 puts("\nProcess contexts:");
105 /* current context */
106 if (getcon(&con) == 0) {
107 printf(COL_FMT "%s\n", "Current context:", con);
108 if (ENABLE_FEATURE_CLEAN_UP)
111 /* /sbin/init context */
112 if (getpidcon(1, &con) == 0) {
113 printf(COL_FMT "%s\n", "Init context:", con);
114 if (ENABLE_FEATURE_CLEAN_UP)
118 /* [process] context */
119 for (i = 0; pc[i] != NULL; i++) {
120 pidList = find_pid_by_name(bb_basename(pc[i]));
121 if (pidList[0] > 0 && getpidcon(pidList[0], &con) == 0) {
122 printf(COL_FMT "%s\n", pc[i], con);
123 if (ENABLE_FEATURE_CLEAN_UP)
126 if (ENABLE_FEATURE_CLEAN_UP)
131 puts("\nFile contexts:");
133 cterm = xmalloc_ttyname(0);
134 //FIXME: if cterm == NULL, we segfault!??
136 if (cterm && lgetfilecon(cterm, &con) >= 0) {
137 printf(COL_FMT "%s\n", "Controlling term:", con);
138 if (ENABLE_FEATURE_CLEAN_UP)
142 for (i = 0; fc[i] != NULL; i++) {
145 if (lgetfilecon(fc[i], &con) < 0)
147 if (lstat(fc[i], &stbuf) == 0) {
148 if (S_ISLNK(stbuf.st_mode)) {
149 if (getfilecon(fc[i], &_con) >= 0) {
150 printf(COL_FMT "%s -> %s\n", fc[i], _con, con);
151 if (ENABLE_FEATURE_CLEAN_UP)
155 printf(COL_FMT "%s\n", fc[i], con);
158 if (ENABLE_FEATURE_CLEAN_UP)
163 int sestatus_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
164 int sestatus_main(int argc UNUSED_PARAM, char **argv)
167 const char *pol_path;
170 opts = getopt32(argv, "^" "vb" "\0" "=0"/*no arguments*/);
172 /* SELinux status: line */
173 rc = is_selinux_enabled();
176 printf(COL_FMT "%s\n", "SELinux status:",
177 rc == 1 ? "enabled" : "disabled");
179 /* SELinuxfs mount: line */
182 printf(COL_FMT "%s\n", "SELinuxfs mount:",
185 /* Current mode: line */
186 rc = security_getenforce();
189 printf(COL_FMT "%s\n", "Current mode:",
190 rc == 0 ? "permissive" : "enforcing");
192 /* Mode from config file: line */
193 if (selinux_getenforcemode(&rc) != 0)
195 printf(COL_FMT "%s\n", "Mode from config file:",
196 rc < 0 ? "disabled" : (rc == 0 ? "permissive" : "enforcing"));
198 /* Policy version: line */
199 rc = security_policyvers();
202 printf(COL_FMT "%u\n", "Policy version:", rc);
204 /* Policy from config file: line */
205 pol_path = selinux_policy_root();
208 printf(COL_FMT "%s\n", "Policy from config file:",
209 bb_basename(pol_path));
211 if (opts & OPT_BOOLEAN)
213 if (opts & OPT_VERBOSE)
219 bb_perror_msg_and_die("libselinux returns unknown state");