• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice,
8  *       this list of conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice,
11  *       this list of conditions and the following disclaimer in the documentation
12  *       and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17  * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * The views and conclusions contained in the software and documentation are those
26  * of the authors and should not be interpreted as representing official policies,
27  * either expressed or implied, of Tresys Technology, LLC.
28  */
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <getopt.h>
35 #include <sys/stat.h>
36 
37 #ifdef ANDROID
38 #include <cil/cil.h>
39 #else
40 #include <sepol/cil/cil.h>
41 #endif
42 #include <sepol/policydb.h>
43 
44 enum write_ast_phase {
45 	WRITE_AST_PHASE_PARSE = 0,
46 	WRITE_AST_PHASE_BUILD,
47 	WRITE_AST_PHASE_RESOLVE,
48 };
49 
usage(const char * prog)50 static __attribute__((__noreturn__)) void usage(const char *prog)
51 {
52 	printf("Usage: %s [OPTION]... FILE...\n", prog);
53 	printf("\n");
54 	printf("Options:\n");
55 	printf("  -o, --output=<file>      write AST to <file>. (default: stdout)\n");
56 	printf("  -P, --preserve-tunables  treat tunables as booleans\n");
57 	printf("  -Q, --qualified-names    Allow names containing dots (qualified names).\n");
58 	printf("                           Blocks, blockinherits, blockabstracts, and\n");
59 	printf("                           in-statements will not be allowed.\n");
60 	printf("  -A, --ast-phase=<phase>  write AST of phase <phase>. Phase must be parse, \n");
61 	printf("                           build, or resolve. (default: resolve)\n");
62 	printf("  -v, --verbose            increment verbosity level\n");
63 	printf("  -h, --help               display usage information\n");
64 	exit(1);
65 }
66 
main(int argc,char * argv[])67 int main(int argc, char *argv[])
68 {
69 	int rc = SEPOL_ERR;
70 	FILE *file = NULL;
71 	char *buffer = NULL;
72 	struct stat filedata;
73 	uint32_t file_size;
74 	char *output = NULL;
75 	struct cil_db *db = NULL;
76 	int preserve_tunables = 0;
77 	int qualified_names = 0;
78 	enum write_ast_phase write_ast = WRITE_AST_PHASE_RESOLVE;
79 	int opt_char;
80 	int opt_index = 0;
81 	enum cil_log_level log_level = CIL_ERR;
82 	static struct option long_opts[] = {
83 		{"help", no_argument, 0, 'h'},
84 		{"verbose", no_argument, 0, 'v'},
85 		{"preserve-tunables", no_argument, 0, 'P'},
86 		{"qualified-names", no_argument, 0, 'Q'},
87 		{"output", required_argument, 0, 'o'},
88 		{"ast-phase", required_argument, 0, 'A'},
89 		{0, 0, 0, 0}
90 	};
91 	int i;
92 
93 	while (1) {
94 		opt_char = getopt_long(argc, argv, "o:hvPQA:", long_opts, &opt_index);
95 		if (opt_char == -1) {
96 			break;
97 		}
98 		switch (opt_char) {
99 			case 'v':
100 				log_level++;
101 				break;
102 			case 'P':
103 				preserve_tunables = 1;
104 				break;
105 			case 'Q':
106 				qualified_names = 1;
107 				break;
108 			case 'o':
109 				output = strdup(optarg);
110 				break;
111 			case 'A':
112 				if (!strcasecmp(optarg, "parse")) {
113 					write_ast = WRITE_AST_PHASE_PARSE;
114 				} else if (!strcasecmp(optarg, "build")) {
115 					write_ast = WRITE_AST_PHASE_BUILD;
116 				} else if (!strcasecmp(optarg, "resolve")) {
117 					write_ast = WRITE_AST_PHASE_RESOLVE;
118 				} else {
119 					fprintf(stderr, "Invalid AST phase: %s\n", optarg);
120 					usage(argv[0]);
121 				}
122 				break;
123 			case 'h':
124 				usage(argv[0]);
125 			case '?':
126 				break;
127 			default:
128 				fprintf(stderr, "Unsupported option: %s\n", optarg);
129 				usage(argv[0]);
130 		}
131 	}
132 
133 	if (optind >= argc) {
134 		fprintf(stderr, "No cil files specified\n");
135 		usage(argv[0]);
136 	}
137 
138 	cil_set_log_level(log_level);
139 
140 	cil_db_init(&db);
141 	cil_set_preserve_tunables(db, preserve_tunables);
142 	cil_set_qualified_names(db, qualified_names);
143 	cil_set_attrs_expand_generated(db, 0);
144 	cil_set_attrs_expand_size(db, 0);
145 
146 	for (i = optind; i < argc; i++) {
147 		file = fopen(argv[i], "r");
148 		if (!file) {
149 			fprintf(stderr, "Could not open file: %s\n", argv[i]);
150 			rc = SEPOL_ERR;
151 			goto exit;
152 		}
153 		rc = stat(argv[i], &filedata);
154 		if (rc == -1) {
155 			fprintf(stderr, "Could not stat file: %s\n", argv[i]);
156 			goto exit;
157 		}
158 		file_size = filedata.st_size;
159 
160 		buffer = malloc(file_size);
161 		rc = fread(buffer, file_size, 1, file);
162 		if (rc != 1) {
163 			fprintf(stderr, "Failure reading file: %s\n", argv[i]);
164 			goto exit;
165 		}
166 		fclose(file);
167 		file = NULL;
168 
169 		rc = cil_add_file(db, argv[i], buffer, file_size);
170 		if (rc != SEPOL_OK) {
171 			fprintf(stderr, "Failure adding %s\n", argv[i]);
172 			goto exit;
173 		}
174 
175 		free(buffer);
176 		buffer = NULL;
177 	}
178 
179 	if (output == NULL) {
180 		file = stdout;
181 	} else {
182 		file = fopen(output, "w");
183 		if (file == NULL) {
184 			fprintf(stderr, "Failure opening file %s for writing\n", output);
185 			rc = SEPOL_ERR;
186 			goto exit;
187 		}
188 	}
189 
190 	switch (write_ast) {
191 	case WRITE_AST_PHASE_PARSE:
192 		rc = cil_write_parse_ast(file, db);
193 		break;
194 	case WRITE_AST_PHASE_BUILD:
195 		rc = cil_write_build_ast(file, db);
196 		break;
197 	case WRITE_AST_PHASE_RESOLVE:
198 		rc = cil_write_resolve_ast(file, db);
199 		break;
200 	}
201 
202 	if (rc != SEPOL_OK) {
203 		fprintf(stderr, "Failed to write AST\n");
204 		goto exit;
205 	}
206 
207 exit:
208 	if (file != NULL && file != stdin) {
209 		fclose(file);
210 	}
211 	free(buffer);
212 	free(output);
213 	cil_db_destroy(&db);
214 	return rc;
215 }
216