Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <stdio.h>
Chris PeBenito 473ea7
#include <getopt.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include <selinux/selinux.h>
Chris PeBenito 473ea7
Chris PeBenito 473ea7
void usage(const char *progname) 
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	fprintf(stderr, "usage:  %s -a or %s boolean...\n", progname, progname);
Chris PeBenito 473ea7
	exit(1);
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int main(int argc, char **argv)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	int i, rc = 0, active, pending, len = 0, opt;
Chris PeBenito 473ea7
	char **names;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	while ((opt = getopt(argc, argv, "a")) > 0) {
Chris PeBenito 473ea7
		switch (opt) {
Chris PeBenito 473ea7
		case 'a':
Chris PeBenito 473ea7
			if (argc > 2)
Chris PeBenito 473ea7
				usage(argv[0]);
Chris PeBenito 473ea7
			if (is_selinux_enabled() <= 0) {
Chris PeBenito 473ea7
				fprintf(stderr, "%s:  SELinux is disabled\n", 
Chris PeBenito 473ea7
					argv[0]);
Chris PeBenito 473ea7
				return 1;
Chris PeBenito 473ea7
			}
Chris PeBenito 473ea7
			errno = 0;
Chris PeBenito 473ea7
			rc = security_get_boolean_names(&names, &len;;
Chris PeBenito 473ea7
			if (rc) {
Chris PeBenito 473ea7
				fprintf(stderr, 
Chris PeBenito 473ea7
				    "%s:  Unable to get boolean names:  %s\n", 
Chris PeBenito 473ea7
				    argv[0], strerror(errno));
Chris PeBenito 473ea7
				return 1;
Chris PeBenito 473ea7
			}
Chris PeBenito 473ea7
			if (!len) {
Chris PeBenito 473ea7
				printf("No booleans\n");
Chris PeBenito 473ea7
				return 0;
Chris PeBenito 473ea7
			}
Chris PeBenito 473ea7
			break;
Chris PeBenito 473ea7
		default:
Chris PeBenito 473ea7
			usage(argv[0]);
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (is_selinux_enabled() <= 0) {
Chris PeBenito 473ea7
		fprintf(stderr, "%s:  SELinux is disabled\n", argv[0]);
Chris PeBenito 473ea7
		return 1;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (!len) {
Chris PeBenito 473ea7
		if (argc < 2) 
Chris PeBenito 473ea7
			usage(argv[0]);
Chris PeBenito 473ea7
		len = argc - 1;
Chris PeBenito 473ea7
		names = malloc(sizeof(char *)*len);
Chris PeBenito 473ea7
		if (!names) {
Chris PeBenito 473ea7
			fprintf(stderr, "%s:  out of memory\n", argv[0]);
Chris PeBenito 473ea7
			return 2;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
		for (i = 0; i < len; i++) {
Chris PeBenito 473ea7
			names[i] = strdup(argv[i+1]);
Chris PeBenito 473ea7
			if (!names[i]) {
Chris PeBenito 473ea7
				fprintf(stderr, "%s:  out of memory\n", argv[0]);
Chris PeBenito 473ea7
				return 2;
Chris PeBenito 473ea7
			}
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	for (i = 0; i < len; i++) {
Chris PeBenito 473ea7
		active = security_get_boolean_active(names[i]);
Chris PeBenito 473ea7
		if (active < 0) {
Chris PeBenito 473ea7
			fprintf(stderr, "Error getting active value for %s\n",
Chris PeBenito 473ea7
				names[i]);
Chris PeBenito 473ea7
			rc = -1;
Chris PeBenito 473ea7
			goto out;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
		pending = security_get_boolean_pending(names[i]);
Chris PeBenito 473ea7
		if (pending < 0) {
Chris PeBenito 473ea7
			fprintf(stderr, "Error getting pending value for %s\n",
Chris PeBenito 473ea7
				names[i]);
Chris PeBenito 473ea7
			rc = -1;
Chris PeBenito 473ea7
			goto out;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
		if (pending != active) {
Chris PeBenito 473ea7
			printf("%s --> %s pending: %s\n", names[i],
Chris PeBenito 473ea7
			       ( active ? "active" : "inactive"),
Chris PeBenito 473ea7
			       ( pending ? "active" : "inactive"));
Chris PeBenito 473ea7
		} else {
Chris PeBenito 473ea7
			printf("%s --> %s\n", names[i],
Chris PeBenito 473ea7
			       ( active ? "active" : "inactive"));
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
out:
Chris PeBenito 473ea7
	for (i = 0; i < len; i++)
Chris PeBenito 473ea7
		free(names[i]);
Chris PeBenito 473ea7
	free(names);
Chris PeBenito 473ea7
	return rc;
Chris PeBenito 473ea7
}