1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <getopt.h>
5 #include <errno.h>
6 #include <selinux/selinux.h>
7 #include <selinux/label.h>
8 
usage(const char * progname)9 static __attribute__ ((__noreturn__)) void usage(const char *progname)
10 {
11 	fprintf(stderr,
12 		"usage: %s -b backend [-v] [-r] -k key [-t type] [-f file]\n\n"
13 		"Where:\n\t"
14 		"-b  The backend - \"file\", \"media\", \"x\", \"db\" or "
15 			"\"prop\"\n\t"
16 		"-v  Validate entries against loaded policy.\n\t"
17 		"-r  Use \"raw\" function.\n\t"
18 		"-k  Lookup key - Depends on backend.\n\t"
19 		"-t  Lookup type - Optional as depends on backend.\n\t"
20 		"-f  Optional file containing the specs (defaults to\n\t"
21 		"    those used by loaded policy).\n\n"
22 		"Examples:\n\t"
23 		"%s -v -b file -k /run -t 0\n\t"
24 		"   lookup with validation against the loaded policy, the\n\t"
25 		"   \"file\" backend for path \"/run\" with mode = 0\n\t"
26 		"%s -r -b x -t 4 -k X11:ButtonPress\n\t"
27 		"   lookup_raw the \"X\" backend for type SELABEL_X_EVENT\n\t"
28 		"   using key \"X11:ButtonPress\"\n\n",
29 		progname, progname, progname);
30 	exit(1);
31 }
32 
main(int argc,char ** argv)33 int main(int argc, char **argv)
34 {
35 	int raw = 0, type = 0, backend = 0, rc, opt;
36 	char *validate = NULL, *key = NULL, *context = NULL, *file = NULL;
37 
38 	struct selabel_handle *hnd;
39 	struct selinux_opt selabel_option[] = {
40 		{ SELABEL_OPT_PATH, file },
41 		{ SELABEL_OPT_VALIDATE, validate }
42 	};
43 
44 	if (argc < 3)
45 		usage(argv[0]);
46 
47 	while ((opt = getopt(argc, argv, "b:f:vrk:t:")) > 0) {
48 		switch (opt) {
49 		case 'b':
50 			if (!strcasecmp(optarg, "file")) {
51 				backend = SELABEL_CTX_FILE;
52 			} else if (!strcmp(optarg, "media")) {
53 				backend = SELABEL_CTX_MEDIA;
54 			} else if (!strcmp(optarg, "x")) {
55 				backend = SELABEL_CTX_X;
56 			} else if (!strcmp(optarg, "db")) {
57 				backend = SELABEL_CTX_DB;
58 			} else if (!strcmp(optarg, "prop")) {
59 				backend = SELABEL_CTX_ANDROID_PROP;
60 			} else if (!strcmp(optarg, "service")) {
61 				backend = SELABEL_CTX_ANDROID_SERVICE;
62 			} else if (!strcmp(optarg, "keystore2_key")) {
63 				backend = SELABEL_CTX_ANDROID_KEYSTORE2_KEY;
64 			} else {
65 				fprintf(stderr, "Unknown backend: %s\n",
66 								    optarg);
67 				usage(argv[0]);
68 			}
69 			break;
70 		case 'f':
71 			file = optarg;
72 			break;
73 		case 'v':
74 			validate = (char *)1;
75 			break;
76 		case 'r':
77 			raw = 1;
78 			break;
79 		case 'k':
80 			key = optarg;
81 			break;
82 		case 't':
83 			type = atoi(optarg);
84 			break;
85 		default:
86 			usage(argv[0]);
87 		}
88 	}
89 
90 	selabel_option[0].value = file;
91 	selabel_option[1].value = validate;
92 
93 	hnd = selabel_open(backend, selabel_option, 2);
94 	if (!hnd) {
95 		fprintf(stderr, "ERROR: selabel_open - Could not obtain "
96 							     "handle.\n");
97 		return -1;
98 	}
99 
100 	switch (raw) {
101 	case 1:
102 		rc = selabel_lookup_raw(hnd, &context, key, type);
103 		break;
104 	default:
105 		rc = selabel_lookup(hnd, &context, key, type);
106 	}
107 	selabel_close(hnd);
108 
109 	if (rc) {
110 		switch (errno) {
111 		case ENOENT:
112 			fprintf(stderr, "ERROR: selabel_lookup failed to "
113 					    "find a valid context.\n");
114 			break;
115 		case EINVAL:
116 			fprintf(stderr, "ERROR: selabel_lookup failed to "
117 				    "validate context, or key / type are "
118 				    "invalid.\n");
119 			break;
120 		default:
121 			fprintf(stderr, "selabel_lookup ERROR: %s\n",
122 						    strerror(errno));
123 		}
124 	} else {
125 		printf("Default context: %s\n", context);
126 		freecon(context);
127 	}
128 
129 	return rc;
130 }
131