1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <selinux/selinux.h>
6 #include <selinux/get_context_list.h>
7
main(int argc,char ** argv)8 int main(int argc, char **argv)
9 {
10 char *seuser = NULL, *level = NULL;
11 char **contextlist;
12 int rc, n, i;
13
14 if (argc != 3) {
15 fprintf(stderr, "usage: %s linuxuser fromcon\n", argv[0]);
16 return 1;
17 }
18
19 if (!is_selinux_enabled()) {
20 fprintf(stderr, "%s may be used only on a SELinux enabled kernel.\n", argv[0]);
21 return 4;
22 }
23
24 rc = getseuserbyname(argv[1], &seuser, &level);
25 if (rc) {
26 fprintf(stderr, "getseuserbyname failed: %s\n", strerror(errno));
27 return 2;
28 }
29 printf("seuser: %s, level %s\n", seuser, level);
30
31 rc = security_check_context(argv[2]);
32 if (rc) {
33 fprintf(stderr, "context '%s' is invalid\n", argv[2]);
34 free(seuser);
35 free(level);
36 return 5;
37 }
38
39 n = get_ordered_context_list_with_level(seuser, level, argv[2], &contextlist);
40 if (n < 0) {
41 fprintf(stderr, "get_ordered_context_list_with_level failed: %s\n", strerror(errno));
42 free(seuser);
43 free(level);
44 return 3;
45 }
46
47 free(seuser);
48 free(level);
49
50 if (n == 0)
51 printf("no valid context found\n");
52
53 for (i = 0; i < n; i++)
54 printf("Context %d\t%s\n", i, contextlist[i]);
55
56 freeconary(contextlist);
57
58 return EXIT_SUCCESS;
59 }
60