Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <sys/types.h>
Chris PeBenito 473ea7
#include <fcntl.h>
Chris PeBenito 473ea7
#include <stdio.h>
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include <ctype.h>
Chris PeBenito 473ea7
#include <selinux/selinux.h>
Chris PeBenito 473ea7
#include <selinux/get_context_list.h>
Chris PeBenito 473ea7
Chris PeBenito 473ea7
void usage(char *name, char *detail, int rc) 
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	fprintf(stderr, "usage:  %s [-l level] user [context]\n", name);
Chris PeBenito 473ea7
	if (detail)
Chris PeBenito 473ea7
		fprintf(stderr, "%s:  %s\n", name, detail);
Chris PeBenito 473ea7
	exit(rc);
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int main(int argc, char **argv) 
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	security_context_t *list, usercon = NULL, cur_context = NULL;
Chris PeBenito 473ea7
	char *user = NULL, *level = NULL;
Chris PeBenito 473ea7
	int ret, i, opt;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	while ((opt = getopt(argc, argv, "l:")) > 0) {
Chris PeBenito 473ea7
		switch (opt) {
Chris PeBenito 473ea7
		case 'l':
Chris PeBenito 473ea7
			level = strdup(optarg);
Chris PeBenito 473ea7
			break;
Chris PeBenito 473ea7
		default:
Chris PeBenito 473ea7
			usage(argv[0], "invalid option", 1);
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (((argc - optind) < 1) || ((argc - optind) > 2))
Chris PeBenito 473ea7
		usage(argv[0], "invalid number of arguments", 2);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	/* If selinux isn't available, bail out. */
Chris PeBenito 473ea7
	if( !is_selinux_enabled() ) {
Chris PeBenito 473ea7
	    fprintf( stderr,
Chris PeBenito 473ea7
        	"getconlist may be used only on a SELinux kernel.\n" );
Chris PeBenito 473ea7
	    return 1;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	user = argv[optind];
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	/* If a context wasn't passed, use the current context. */
Chris PeBenito 473ea7
	if (((argc - optind) < 2)) {
Chris PeBenito 473ea7
		if (getcon(&cur_context) < 0) {
Chris PeBenito 473ea7
			fprintf( stderr, "Couldn't get current context.\n" );
Chris PeBenito 473ea7
			return 2;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
	else
Chris PeBenito 473ea7
		cur_context = argv[optind+1];
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	/* Get the list and print it */
Chris PeBenito 473ea7
	if (level)
Chris PeBenito 473ea7
		ret = get_ordered_context_list_with_level(user, level, cur_context, &list);
Chris PeBenito 473ea7
	else
Chris PeBenito 473ea7
		ret = get_ordered_context_list(user, cur_context, &list);
Chris PeBenito 473ea7
	if(ret != -1) {
Chris PeBenito 473ea7
		for (i = 0; list[i]; i++)
Chris PeBenito 473ea7
			puts (list[i]);
Chris PeBenito 473ea7
		freeconary(list);
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	free(usercon);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	return 0;
Chris PeBenito 473ea7
}