blob: 2267be4511201d7b59b71222e97e7690f908bab2 [file] [log] [blame]
Denis Vlasenkod46d3c22007-02-06 19:28:50 +00001/*
2 * setenforce
3 *
4 * Based on libselinux 1.33.1
5 * Port to BusyBox Hiroshi Shinji <shiroshi@my.email.ne.jp>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkod46d3c22007-02-06 19:28:50 +00008 */
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +01009//config:config SETENFORCE
Denys Vlasenkoae178ce2017-07-19 14:32:54 +020010//config: bool "setenforce (2.1 kb)"
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +010011//config: default n
12//config: depends on SELINUX
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: Enable support to modify the mode SELinux is running in.
Denys Vlasenkoa8e52da2016-11-23 18:46:40 +010015
16//applet:IF_SETENFORCE(APPLET(setenforce, BB_DIR_USR_SBIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_SETENFORCE) += setenforce.o
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000019
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage:#define setenforce_trivial_usage
21//usage: "[Enforcing | Permissive | 1 | 0]"
22//usage:#define setenforce_full_usage ""
23
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000025
Denis Vlasenko8c6c6e92007-02-07 22:08:42 +000026/* These strings are arranged so that odd ones
27 * result in security_setenforce(1) being done,
28 * the rest will do security_setenforce(0) */
Denys Vlasenko987be932022-02-06 20:07:12 +010029static const char *const setenforce_cmd[] ALIGN_PTR = {
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000030 "0",
31 "1",
32 "permissive",
33 "enforcing",
34 NULL,
35};
36
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000037int setenforce_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010038int setenforce_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000039{
40 int i, rc;
41
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010042 if (!argv[1] || argv[2])
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000043 bb_show_usage();
44
45 selinux_or_die();
46
47 for (i = 0; setenforce_cmd[i]; i++) {
48 if (strcasecmp(argv[1], setenforce_cmd[i]) != 0)
49 continue;
Denis Vlasenko8c6c6e92007-02-07 22:08:42 +000050 rc = security_setenforce(i & 1);
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000051 if (rc < 0)
James Byrne69374872019-07-02 11:35:03 +020052 bb_simple_perror_msg_and_die("setenforce() failed");
Denis Vlasenkod46d3c22007-02-06 19:28:50 +000053 return 0;
54 }
55
56 bb_show_usage();
57}