File: setenforce.c

package info (click to toggle)
libselinux 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, experimental, sid
  • size: 2,080 kB
  • sloc: ansic: 16,057; makefile: 379; sh: 23
file content (42 lines) | stat: -rw-r--r-- 933 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <selinux/selinux.h>

static __attribute__ ((__noreturn__)) void usage(const char *progname)
{
	fprintf(stderr, "usage:  %s [ Enforcing | Permissive | 1 | 0 ]\n",
		progname);
	exit(1);
}

int main(int argc, char **argv)
{
	int rc = 0;
	if (argc != 2) {
		usage(argv[0]);
	}

	if (is_selinux_enabled() <= 0) {
		fprintf(stderr, "%s: SELinux is disabled\n", argv[0]);
		return 1;
	}
	if (strlen(argv[1]) == 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
		rc = security_setenforce(atoi(argv[1]));
	} else {
		if (strcasecmp(argv[1], "enforcing") == 0) {
			rc = security_setenforce(1);
		} else if (strcasecmp(argv[1], "permissive") == 0) {
			rc = security_setenforce(0);
		} else
			usage(argv[0]);
	}
	if (rc < 0) {
		fprintf(stderr, "%s:  setenforce() failed\n", argv[0]);
		return 2;
	}
	return 0;
}