1#include <unistd.h>
2#include <sys/types.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <errno.h>
7#include <selinux/selinux.h>
8
9int main(int argc, char **argv)
10{
11	char *buf;
12	security_class_t tclass;
13	int ret;
14
15	if (argc != 4) {
16		fprintf(stderr, "usage:  %s scontext tcontext tclass\n",
17			argv[0]);
18		exit(1);
19	}
20
21	if (security_check_context(argv[1])) {
22		fprintf(stderr, "%s:  invalid source context '%s'\n", argv[0], argv[1]);
23		exit(4);
24	}
25
26	if (security_check_context(argv[2])) {
27		fprintf(stderr, "%s:  invalid target context '%s'\n", argv[0], argv[2]);
28		exit(5);
29	}
30
31	tclass = string_to_security_class(argv[3]);
32	if (!tclass) {
33		fprintf(stderr, "%s:  invalid class '%s'\n", argv[0], argv[3]);
34		exit(2);
35	}
36
37	ret = security_compute_member(argv[1], argv[2], tclass, &buf);
38	if (ret < 0) {
39		fprintf(stderr, "%s:  security_compute_member failed:  %s\n",
40			argv[0], strerror(errno));
41		exit(3);
42	}
43
44	printf("%s\n", buf);
45	freecon(buf);
46	exit(EXIT_SUCCESS);
47}
48