Home | History | Annotate | Download | only in utils
      1 #include <unistd.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <ctype.h>
      5 #include <string.h>
      6 #include <strings.h>
      7 #include <selinux/selinux.h>
      8 
      9 static void usage(const char *progname)
     10 {
     11 	fprintf(stderr, "usage:  %s [ Enforcing | Permissive | 1 | 0 ]\n",
     12 		progname);
     13 	exit(1);
     14 }
     15 
     16 int main(int argc, char **argv)
     17 {
     18 	int rc = 0;
     19 	if (argc != 2) {
     20 		usage(argv[0]);
     21 	}
     22 
     23 	if (is_selinux_enabled() <= 0) {
     24 		fprintf(stderr, "%s: SELinux is disabled\n", argv[0]);
     25 		return 1;
     26 	}
     27 	if (strlen(argv[1]) == 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
     28 		rc = security_setenforce(atoi(argv[1]));
     29 	} else {
     30 		if (strcasecmp(argv[1], "enforcing") == 0) {
     31 			rc = security_setenforce(1);
     32 		} else if (strcasecmp(argv[1], "permissive") == 0) {
     33 			rc = security_setenforce(0);
     34 		} else
     35 			usage(argv[0]);
     36 	}
     37 	if (rc < 0) {
     38 		fprintf(stderr, "%s:  setenforce() failed\n", argv[0]);
     39 		return 2;
     40 	}
     41 	return 0;
     42 }
     43