Debian Codegraph @

cil/src/cil_build_ast.c

raw
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 <string.h>
33 #include <ctype.h>
34
35 #include <sepol/policydb/conditional.h>
36
37 #include "cil_internal.h"
38 #include "cil_flavor.h"
39 #include "cil_log.h"
40 #include "cil_mem.h"
41 #include "cil_tree.h"
42 #include "cil_list.h"
43 #include "cil_parser.h"
44 #include "cil_build_ast.h"
45 #include "cil_copy_ast.h"
46 #include "cil_verify.h"
47 #include "cil_strpool.h"
48
49 struct cil_args_build {
50 struct cil_tree_node *ast;
51 struct cil_db *db;
52 struct cil_tree_node *tunif;
53 struct cil_tree_node *in;
54 struct cil_tree_node *macro;
55 struct cil_tree_node *optional;
56 struct cil_tree_node *boolif;
57 };
58
59 static int cil_fill_list(struct cil_tree_node *current, enum cil_flavor flavor, struct cil_list **list)
60 {
61 int rc = SEPOL_ERR;
62 struct cil_tree_node *curr;
63 enum cil_syntax syntax[] = {
64 CIL_SYN_N_STRINGS,
65 CIL_SYN_END
66 };
67 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
68
69 rc = __cil_verify_syntax(current, syntax, syntax_len);
70 if (rc != SEPOL_OK) {
71 goto exit;
72 }
73
74 cil_list_init(list, flavor);
75
76 for (curr = current; curr != NULL; curr = curr->next) {
77 cil_list_append(*list, CIL_STRING, curr->data);
78 }
79
80 return SEPOL_OK;
81
82 exit:
83 return rc;
84 }
85
86 struct cil_symtab_datum *cil_gen_declared_string(struct cil_db *db, hashtab_key_t key, struct cil_tree_node *ast_node)
87 {
88 struct cil_tree_node *parent = ast_node->parent;
89 struct cil_macro *macro = NULL;
90 symtab_t *symtab;
91 struct cil_symtab_datum *datum;
92
93 while (parent) {
94 if (parent->flavor == CIL_MACRO) {
95 /* This condition is only reached in the build phase */
96 macro = parent->data;
97 break;
98 } else if (parent->flavor == CIL_CALL) {
99 /* This condition is only reached in the resolve phase */
100 struct cil_call *call = parent->data;
101 macro = call->macro;
102 break;
103 }
104 parent = parent->parent;
105 }
106
107 if (macro && macro->params) {
108 struct cil_list_item *item;
109 cil_list_for_each(item, macro->params) {
110 struct cil_param *param = item->data;
111 if (param->flavor == CIL_DECLARED_STRING && param->str == key) {
112 return NULL;
113 }
114 }
115 }
116
117 symtab = &((struct cil_root *)db->ast->root->data)->symtab[CIL_SYM_STRINGS];
118 cil_symtab_get_datum(symtab, key, &datum);
119 if (datum != NULL) {
120 return datum;
121 }
122
123 datum = cil_malloc(sizeof(*datum));
124 cil_symtab_datum_init(datum);
125 cil_symtab_insert(symtab, key, datum, ast_node);
126 cil_list_append(db->declared_strings, CIL_DATUM, datum);
127 return datum;
128 }
129
130
131 static int cil_allow_multiple_decls(struct cil_db *db, enum cil_flavor f_new, enum cil_flavor f_old)
132 {
133 if (f_new != f_old) {
134 return CIL_FALSE;
135 }
136
137 switch (f_new) {
138 case CIL_TYPE:
139 case CIL_TYPEATTRIBUTE:
140 case CIL_ROLE:
141 if (db->multiple_decls) {
142 return CIL_TRUE;
143 }
144 break;
145 case CIL_OPTIONAL:
146 return CIL_TRUE;
147 break;
148 case CIL_POLICYCAP:
149 return CIL_TRUE;
150 break;
151 default:
152 break;
153 }
154
155 return CIL_FALSE;
156 }
157
158 int cil_add_decl_to_symtab(struct cil_db *db, symtab_t *symtab, hashtab_key_t key, struct cil_symtab_datum *datum, struct cil_tree_node *node)
159 {
160 int rc;
161
162 if (symtab == NULL || datum == NULL || node == NULL) {
163 return SEPOL_ERR;
164 }
165
166 rc = cil_symtab_insert(symtab, key, datum, node);
167 if (rc == SEPOL_EEXIST) {
168 struct cil_symtab_datum *prev;
169 rc = cil_symtab_get_datum(symtab, key, &prev);
170 if (rc != SEPOL_OK) {
171 cil_log(CIL_ERR, "Re-declaration of %s %s, but previous declaration could not be found\n",cil_node_to_string(node), key);
172 return SEPOL_ERR;
173 }
174 if (!cil_allow_multiple_decls(db, node->flavor, FLAVOR(prev))) {
175 /* multiple_decls not ok, ret error */
176 struct cil_tree_node *n = NODE(prev);
177 cil_log(CIL_ERR, "Re-declaration of %s %s\n",
178 cil_node_to_string(node), key);
179 cil_tree_log(node, CIL_ERR, "Previous declaration of %s",
180 cil_node_to_string(n));
181 return SEPOL_ERR;
182 }
183 /* multiple_decls is enabled and works for this datum type, add node */
184 cil_list_append(prev->nodes, CIL_NODE, node);
185 node->data = prev;
186 return SEPOL_EEXIST;
187 }
188
189 return SEPOL_OK;
190 }
191
192 int cil_gen_node(struct cil_db *db, struct cil_tree_node *ast_node, struct cil_symtab_datum *datum, hashtab_key_t key, enum cil_sym_index sflavor, enum cil_flavor nflavor)
193 {
194 int rc = SEPOL_ERR;
195 symtab_t *symtab = NULL;
196
197 rc = cil_verify_name(db, (const char*)key, nflavor);
198 if (rc != SEPOL_OK) {
199 goto exit;
200 }
201
202 rc = cil_get_symtab(ast_node->parent, &symtab, sflavor);
203 if (rc != SEPOL_OK) {
204 goto exit;
205 }
206
207 ast_node->data = datum;
208 ast_node->flavor = nflavor;
209
210 rc = cil_add_decl_to_symtab(db, symtab, key, datum, ast_node);
211 if (rc != SEPOL_OK) {
212 goto exit;
213 }
214
215 if (ast_node->parent->flavor == CIL_MACRO) {
216 rc = cil_verify_decl_does_not_shadow_macro_parameter(ast_node->parent->data, ast_node, key);
217 if (rc != SEPOL_OK) {
218 goto exit;
219 }
220 }
221
222 return SEPOL_OK;
223
224 exit:
225 return rc;
226 }
227
228 static void cil_clear_node(struct cil_tree_node *ast_node)
229 {
230 if (ast_node == NULL) {
231 return;
232 }
233
234 ast_node->data = NULL;
235 ast_node->flavor = CIL_NONE;
236 }
237
238 int cil_gen_ordered(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor)
239 {
240 enum cil_syntax syntax[] = {
241 CIL_SYN_STRING,
242 CIL_SYN_LIST,
243 CIL_SYN_END
244 };
245 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
246 struct cil_ordered *ordered = NULL;
247 struct cil_list_item *curr = NULL;
248
249 int rc = SEPOL_ERR;
250
251 if (db == NULL || parse_current == NULL || ast_node == NULL) {
252 goto exit;
253 }
254
255 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
256 if (rc != SEPOL_OK) {
257 goto exit;
258 }
259
260 cil_ordered_init(&ordered);
261
262 rc = cil_fill_list(parse_current->next->cl_head, flavor, &ordered->strs);
263 if (rc != SEPOL_OK) {
264 goto exit;
265 }
266
267 cil_list_for_each(curr, ordered->strs) {
268 if (curr->data == CIL_KEY_UNORDERED) {
269 if (flavor == CIL_CLASSORDER) {
270 if (curr == ordered->strs->head && curr->next == NULL) {
271 cil_log(CIL_ERR, "classorder 'unordered' keyword must be followed by one or more class.\n");
272 rc = SEPOL_ERR;
273 goto exit;
274 } else if (curr != ordered->strs->head) {
275 cil_log(CIL_ERR, "classorder can only use 'unordered' keyword as the first item in the list.\n");
276 rc = SEPOL_ERR;
277 goto exit;
278 }
279 } else {
280 cil_log(CIL_ERR, "The 'unordered' keyword can only be used with classorder rules.\n");
281 rc = SEPOL_ERR;
282 goto exit;
283 }
284 }
285 }
286
287 ast_node->data = ordered;
288 ast_node->flavor = flavor;
289
290 return SEPOL_OK;
291
292 exit:
293 cil_tree_log(parse_current, CIL_ERR, "Bad ordered declaration");
294 cil_destroy_ordered(ordered);
295 return rc;
296 }
297
298 void cil_destroy_ordered(struct cil_ordered *ordered)
299 {
300 if (ordered == NULL) {
301 return;
302 }
303
304 if (ordered->strs != NULL) {
305 cil_list_destroy(&ordered->strs, CIL_FALSE);
306 }
307 if (ordered->datums != NULL) {
308 cil_list_destroy(&ordered->datums, CIL_FALSE);
309 }
310
311 free(ordered);
312 }
313
314
315 int cil_gen_block(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, uint16_t is_abstract)
316 {
317 enum cil_syntax syntax[] = {
318 CIL_SYN_STRING,
319 CIL_SYN_STRING,
320 CIL_SYN_N_LISTS | CIL_SYN_END,
321 CIL_SYN_END
322 };
323 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
324 char *key = NULL;
325 struct cil_block *block = NULL;
326 int rc = SEPOL_ERR;
327
328 if (db == NULL || parse_current == NULL || ast_node == NULL) {
329 goto exit;
330 }
331
332 if (db->qualified_names) {
333 cil_log(CIL_ERR, "Blocks are not allowed when the option for qualified names is used\n");
334 goto exit;
335 }
336
337 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
338 if (rc != SEPOL_OK) {
339 goto exit;
340 }
341
342 cil_block_init(&block);
343
344 block->is_abstract = is_abstract;
345
346 key = parse_current->next->data;
347
348 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)block, (hashtab_key_t)key, CIL_SYM_BLOCKS, CIL_BLOCK);
349 if (rc != SEPOL_OK) {
350 goto exit;
351 }
352
353 return SEPOL_OK;
354
355 exit:
356 cil_tree_log(parse_current, CIL_ERR, "Bad block declaration");
357 cil_destroy_block(block);
358 cil_clear_node(ast_node);
359 return rc;
360 }
361
362 void cil_destroy_block(struct cil_block *block)
363 {
364 struct cil_list_item *item;
365 struct cil_tree_node *bi_node;
366 struct cil_blockinherit *inherit;
367
368 if (block == NULL) {
369 return;
370 }
371
372 cil_symtab_datum_destroy(&block->datum);
373 cil_symtab_array_destroy(block->symtab);
374 if (block->bi_nodes != NULL) {
375 /* unlink blockinherit->block */
376 cil_list_for_each(item, block->bi_nodes) {
377 bi_node = item->data;
378 /* the conditions should always be true, but better be sure */
379 if (bi_node->flavor == CIL_BLOCKINHERIT) {
380 inherit = bi_node->data;
381 if (inherit->block == block) {
382 inherit->block = NULL;
383 }
384 }
385 }
386 cil_list_destroy(&block->bi_nodes, CIL_FALSE);
387 }
388
389 free(block);
390 }
391
392 int cil_gen_blockinherit(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
393 {
394 enum cil_syntax syntax[] = {
395 CIL_SYN_STRING,
396 CIL_SYN_STRING,
397 CIL_SYN_END
398 };
399 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
400 struct cil_blockinherit *inherit = NULL;
401 int rc = SEPOL_ERR;
402
403 if (db == NULL || parse_current == NULL || ast_node == NULL) {
404 goto exit;
405 }
406
407 if (db->qualified_names) {
408 cil_log(CIL_ERR, "Block inherit rules are not allowed when the option for qualified names is used\n");
409 goto exit;
410 }
411
412 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
413 if (rc != SEPOL_OK) {
414 goto exit;
415 }
416
417 cil_blockinherit_init(&inherit);
418
419 inherit->block_str = parse_current->next->data;
420
421 ast_node->data = inherit;
422 ast_node->flavor = CIL_BLOCKINHERIT;
423
424 return SEPOL_OK;
425
426 exit:
427 cil_tree_log(parse_current, CIL_ERR, "Bad blockinherit declaration");
428 cil_destroy_blockinherit(inherit);
429 return rc;
430 }
431
432 void cil_destroy_blockinherit(struct cil_blockinherit *inherit)
433 {
434 if (inherit == NULL) {
435 return;
436 }
437
438 if (inherit->block != NULL && inherit->block->bi_nodes != NULL) {
439 struct cil_tree_node *node;
440 struct cil_list_item *item;
441
442 cil_list_for_each(item, inherit->block->bi_nodes) {
443 node = item->data;
444 if (node->data == inherit) {
445 cil_list_remove(inherit->block->bi_nodes, CIL_NODE, node, CIL_FALSE);
446 break;
447 }
448 }
449 }
450
451 free(inherit);
452 }
453
454 int cil_gen_blockabstract(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
455 {
456 enum cil_syntax syntax[] = {
457 CIL_SYN_STRING,
458 CIL_SYN_STRING,
459 CIL_SYN_END
460 };
461 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
462 struct cil_blockabstract *abstract = NULL;
463 int rc = SEPOL_ERR;
464
465 if (db == NULL || parse_current == NULL || ast_node == NULL) {
466 goto exit;
467 }
468
469 if (db->qualified_names) {
470 cil_log(CIL_ERR, "Block abstract rules are not allowed when the option for qualified names is used\n");
471 goto exit;
472 }
473
474 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
475 if (rc != SEPOL_OK) {
476 goto exit;
477 }
478
479 cil_blockabstract_init(&abstract);
480
481 abstract->block_str = parse_current->next->data;
482
483 ast_node->data = abstract;
484 ast_node->flavor = CIL_BLOCKABSTRACT;
485
486 return SEPOL_OK;
487
488 exit:
489 cil_tree_log(parse_current, CIL_ERR, "Bad blockabstract declaration");
490 cil_destroy_blockabstract(abstract);
491 return rc;
492 }
493
494 void cil_destroy_blockabstract(struct cil_blockabstract *abstract)
495 {
496 if (abstract == NULL) {
497 return;
498 }
499
500 free(abstract);
501 }
502
503 int cil_gen_in(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
504 {
505 enum cil_syntax syntax[] = {
506 CIL_SYN_STRING,
507 CIL_SYN_STRING,
508 CIL_SYN_STRING | CIL_SYN_N_LISTS,
509 CIL_SYN_N_LISTS | CIL_SYN_END,
510 CIL_SYN_END
511 };
512 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
513 int rc = SEPOL_ERR;
514 struct cil_in *in = NULL;
515
516 if (db == NULL || parse_current == NULL || ast_node == NULL) {
517 goto exit;
518 }
519
520 if (db->qualified_names) {
521 cil_log(CIL_ERR, "In-statements are not allowed when the option for qualified names is used\n");
522 goto exit;
523 }
524
525 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
526 if (rc != SEPOL_OK) {
527 goto exit;
528 }
529
530 cil_in_init(&in);
531
532 if (parse_current->next->next->data) {
533 char *is_after_str = parse_current->next->data;
534 if (is_after_str == CIL_KEY_IN_BEFORE) {
535 in->is_after = CIL_FALSE;
536 } else if (is_after_str == CIL_KEY_IN_AFTER) {
537 in->is_after = CIL_TRUE;
538 } else {
539 cil_log(CIL_ERR, "Value must be either \'before\' or \'after\'\n");
540 rc = SEPOL_ERR;
541 goto exit;
542 }
543 in->block_str = parse_current->next->next->data;
544 } else {
545 in->is_after = CIL_FALSE;
546 in->block_str = parse_current->next->data;
547 }
548
549 ast_node->data = in;
550 ast_node->flavor = CIL_IN;
551
552 return SEPOL_OK;
553 exit:
554 cil_tree_log(parse_current, CIL_ERR, "Bad in-statement");
555 cil_destroy_in(in);
556 return rc;
557 }
558
559 void cil_destroy_in(struct cil_in *in)
560 {
561 if (in == NULL) {
562 return;
563 }
564
565 cil_symtab_array_destroy(in->symtab);
566
567 free(in);
568 }
569
570 int cil_gen_class(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
571 {
572 enum cil_syntax syntax[] = {
573 CIL_SYN_STRING,
574 CIL_SYN_STRING,
575 CIL_SYN_LIST | CIL_SYN_EMPTY_LIST,
576 CIL_SYN_END
577 };
578 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
579 char *key = NULL;
580 struct cil_class *class = NULL;
581 struct cil_tree_node *perms = NULL;
582 int rc = SEPOL_ERR;
583
584 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
585 if (rc != SEPOL_OK) {
586 goto exit;
587 }
588
589 cil_class_init(&class);
590
591 key = parse_current->next->data;
592 if (key == CIL_KEY_UNORDERED) {
593 cil_log(CIL_ERR, "'unordered' keyword is reserved and not a valid class name.\n");
594 rc = SEPOL_ERR;
595 goto exit;
596 }
597
598 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)class, (hashtab_key_t)key, CIL_SYM_CLASSES, CIL_CLASS);
599 if (rc != SEPOL_OK) {
600 goto exit;
601 }
602
603 if (parse_current->next->next != NULL) {
604 perms = parse_current->next->next->cl_head;
605 rc = cil_gen_perm_nodes(db, perms, ast_node, CIL_PERM, &class->num_perms);
606 if (rc != SEPOL_OK) {
607 goto exit;
608 }
609 if (class->num_perms > CIL_PERMS_PER_CLASS) {
610 cil_tree_log(parse_current, CIL_ERR, "Too many permissions in class '%s'", class->datum.name);
611 cil_tree_children_destroy(ast_node);
612 rc = SEPOL_ERR;
613 goto exit;
614 }
615
616 }
617
618 return SEPOL_OK;
619
620 exit:
621 cil_tree_log(parse_current, CIL_ERR, "Bad class declaration");
622 cil_destroy_class(class);
623 cil_clear_node(ast_node);
624 return rc;
625 }
626
627 void cil_destroy_class(struct cil_class *class)
628 {
629 if (class == NULL) {
630 return;
631 }
632
633 cil_symtab_datum_destroy(&class->datum);
634 cil_symtab_destroy(&class->perms);
635
636 free(class);
637 }
638
639 int cil_gen_perm(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor, unsigned int *num_perms)
640 {
641 char *key = NULL;
642 struct cil_perm *perm = NULL;
643 int rc = SEPOL_ERR;
644
645 cil_perm_init(&perm);
646
647 key = parse_current->data;
648 if (key == NULL) {
649 cil_log(CIL_ERR, "Bad permission\n");
650 goto exit;
651 }
652
653 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)perm, (hashtab_key_t)key, CIL_SYM_PERMS, flavor);
654 if (rc != SEPOL_OK) {
655 goto exit;
656 }
657
658 perm->value = *num_perms;
659 (*num_perms)++;
660
661 return SEPOL_OK;
662
663 exit:
664 cil_destroy_perm(perm);
665 cil_clear_node(ast_node);
666 return rc;
667 }
668
669 void cil_destroy_perm(struct cil_perm *perm)
670 {
671 if (perm == NULL) {
672 return;
673 }
674
675 cil_symtab_datum_destroy(&perm->datum);
676 cil_list_destroy(&perm->classperms, CIL_FALSE);
677
678 free(perm);
679 }
680
681 int cil_gen_perm_nodes(struct cil_db *db, struct cil_tree_node *current_perm, struct cil_tree_node *ast_node, enum cil_flavor flavor, unsigned int *num_perms)
682 {
683 int rc = SEPOL_ERR;
684 struct cil_tree_node *new_ast = NULL;
685
686 while(current_perm != NULL) {
687 if (current_perm->cl_head != NULL) {
688
689 rc = SEPOL_ERR;
690 goto exit;
691 }
692 cil_tree_node_init(&new_ast);
693 new_ast->parent = ast_node;
694 new_ast->line = current_perm->line;
695 new_ast->hll_offset = current_perm->hll_offset;
696
697 rc = cil_gen_perm(db, current_perm, new_ast, flavor, num_perms);
698 if (rc != SEPOL_OK) {
699 cil_tree_node_destroy(&new_ast);
700 goto exit;
701 }
702
703 if (ast_node->cl_head == NULL) {
704 ast_node->cl_head = new_ast;
705 } else {
706 ast_node->cl_tail->next = new_ast;
707 }
708 ast_node->cl_tail = new_ast;
709
710 current_perm = current_perm->next;
711 }
712
713 return SEPOL_OK;
714
715 exit:
716 cil_log(CIL_ERR, "Bad permissions\n");
717 cil_tree_children_destroy(ast_node);
718 cil_clear_node(ast_node);
719 return rc;
720 }
721
722 int cil_fill_perms(struct cil_tree_node *start_perm, struct cil_list **perms)
723 {
724 int rc = SEPOL_ERR;
725 enum cil_syntax syntax[] = {
726 CIL_SYN_N_STRINGS | CIL_SYN_N_LISTS,
727 CIL_SYN_END
728 };
729 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
730
731 rc = __cil_verify_syntax(start_perm->cl_head, syntax, syntax_len);
732 if (rc != SEPOL_OK) {
733 goto exit;
734 }
735
736 rc = cil_gen_expr(start_perm, CIL_PERM, perms);
737 if (rc != SEPOL_OK) {
738 goto exit;
739 }
740
741 return SEPOL_OK;
742
743 exit:
744 cil_log(CIL_ERR, "Bad permission list or expression\n");
745 return rc;
746 }
747
748 int cil_fill_classperms(struct cil_tree_node *parse_current, struct cil_classperms **cp)
749 {
750 int rc = SEPOL_ERR;
751 enum cil_syntax syntax[] = {
752 CIL_SYN_STRING,
753 CIL_SYN_LIST,
754 CIL_SYN_END
755 };
756 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
757
758 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
759 if (rc != SEPOL_OK) {
760 goto exit;
761 }
762
763 cil_classperms_init(cp);
764
765 (*cp)->class_str = parse_current->data;
766
767 rc = cil_fill_perms(parse_current->next, &(*cp)->perm_strs);
768 if (rc != SEPOL_OK) {
769 cil_destroy_classperms(*cp);
770 goto exit;
771 }
772
773 return SEPOL_OK;
774
775 exit:
776 cil_log(CIL_ERR, "Bad class-permissions\n");
777 *cp = NULL;
778 return rc;
779 }
780
781 void cil_destroy_classperms(struct cil_classperms *cp)
782 {
783 if (cp == NULL) {
784 return;
785 }
786
787 cil_list_destroy(&cp->perm_strs, CIL_TRUE);
788 cil_list_destroy(&cp->perms, CIL_FALSE);
789
790 free(cp);
791 }
792
793 void cil_fill_classperms_set(struct cil_tree_node *parse_current, struct cil_classperms_set **cp_set)
794 {
795 cil_classperms_set_init(cp_set);
796 (*cp_set)->set_str = parse_current->data;
797 }
798
799 void cil_destroy_classperms_set(struct cil_classperms_set *cp_set)
800 {
801 if (cp_set == NULL) {
802 return;
803 }
804
805 free(cp_set);
806 }
807
808 int cil_fill_classperms_list(struct cil_tree_node *parse_current, struct cil_list **cp_list)
809 {
810 int rc = SEPOL_ERR;
811 struct cil_tree_node *curr;
812
813 if (parse_current == NULL || cp_list == NULL) {
814 goto exit;
815 }
816
817 cil_list_init(cp_list, CIL_CLASSPERMS);
818
819 curr = parse_current->cl_head;
820
821 if (curr == NULL) {
822 /* Class-perms form: SET1 */
823 struct cil_classperms_set *new_cp_set;
824 cil_fill_classperms_set(parse_current, &new_cp_set);
825 cil_list_append(*cp_list, CIL_CLASSPERMS_SET, new_cp_set);
826 } else if (curr->cl_head == NULL) {
827 /* Class-perms form: (CLASS1 (PERM1 ...)) */
828 struct cil_classperms *new_cp;
829 rc = cil_fill_classperms(curr, &new_cp);
830 if (rc != SEPOL_OK) {
831 goto exit;
832 }
833 cil_list_append(*cp_list, CIL_CLASSPERMS, new_cp);
834 } else {
835 cil_log(CIL_ERR, "Bad class-permissions list syntax\n");
836 rc = SEPOL_ERR;
837 goto exit;
838 }
839
840 return SEPOL_OK;
841
842 exit:
843 cil_log(CIL_ERR, "Problem filling class-permissions list\n");
844 cil_list_destroy(cp_list, CIL_TRUE);
845 return rc;
846 }
847
848 void cil_destroy_classperms_list(struct cil_list **cp_list)
849 {
850 struct cil_list_item *curr;
851
852 if (cp_list == NULL || *cp_list == NULL) {
853 return;
854 }
855
856 cil_list_for_each(curr, *cp_list) {
857 if (curr->flavor == CIL_CLASSPERMS) {
858 cil_destroy_classperms(curr->data);
859 } else {
860 cil_destroy_classperms_set(curr->data);
861 }
862 }
863
864 cil_list_destroy(cp_list, CIL_FALSE);
865 }
866
867 int cil_gen_classpermission(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
868 {
869 int rc = SEPOL_ERR;
870 char *key = NULL;
871 struct cil_classpermission *cp = NULL;
872 enum cil_syntax syntax[] = {
873 CIL_SYN_STRING,
874 CIL_SYN_STRING,
875 CIL_SYN_END
876 };
877 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
878
879 if (db == NULL || parse_current == NULL || ast_node == NULL) {
880 goto exit;
881 }
882
883 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
884 if (rc != SEPOL_OK) {
885 goto exit;
886 }
887
888 cil_classpermission_init(&cp);
889
890 key = parse_current->next->data;
891
892 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)cp, (hashtab_key_t)key, CIL_SYM_CLASSPERMSETS, CIL_CLASSPERMISSION);
893 if (rc != SEPOL_OK) {
894 goto exit;
895 }
896
897 return SEPOL_OK;
898
899 exit:
900 cil_tree_log(parse_current, CIL_ERR, "Bad classpermission declaration");
901 cil_destroy_classpermission(cp);
902 cil_clear_node(ast_node);
903 return rc;
904 }
905
906 void cil_destroy_classpermission(struct cil_classpermission *cp)
907 {
908 if (cp == NULL) {
909 return;
910 }
911
912 if (cp->datum.name != NULL) {
913 cil_list_destroy(&cp->classperms, CIL_FALSE);
914 } else {
915 /* anonymous classpermission from call */
916 cil_destroy_classperms_list(&cp->classperms);
917 }
918
919 cil_symtab_datum_destroy(&cp->datum);
920
921
922 free(cp);
923 }
924
925 int cil_gen_classpermissionset(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
926 {
927 int rc = SEPOL_ERR;
928 struct cil_classpermissionset *cps = NULL;
929 enum cil_syntax syntax[] = {
930 CIL_SYN_STRING,
931 CIL_SYN_STRING,
932 CIL_SYN_STRING | CIL_SYN_LIST,
933 CIL_SYN_END
934 };
935 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
936
937 if (db == NULL || parse_current == NULL || ast_node == NULL) {
938 goto exit;
939 }
940
941 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
942 if (rc != SEPOL_OK) {
943 goto exit;
944 }
945
946 cil_classpermissionset_init(&cps);
947
948 cps->set_str = parse_current->next->data;
949
950 rc = cil_fill_classperms_list(parse_current->next->next, &cps->classperms);
951 if (rc != SEPOL_OK) {
952 goto exit;
953 }
954
955 ast_node->data = cps;
956 ast_node->flavor = CIL_CLASSPERMISSIONSET;
957
958 return SEPOL_OK;
959
960 exit:
961 cil_tree_log(parse_current, CIL_ERR, "Bad classpermissionset");
962 cil_destroy_classpermissionset(cps);
963 return rc;
964 }
965
966 void cil_destroy_classpermissionset(struct cil_classpermissionset *cps)
967 {
968 if (cps == NULL) {
969 return;
970 }
971
972 cil_destroy_classperms_list(&cps->classperms);
973
974 free(cps);
975 }
976
977 int cil_gen_map_class(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
978 {
979 enum cil_syntax syntax[] = {
980 CIL_SYN_STRING,
981 CIL_SYN_STRING,
982 CIL_SYN_LIST,
983 CIL_SYN_END
984 };
985 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
986 char *key = NULL;
987 struct cil_class *map = NULL;
988 int rc = SEPOL_ERR;
989
990 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
991 if (rc != SEPOL_OK) {
992 goto exit;
993 }
994
995 cil_class_init(&map);
996
997 key = parse_current->next->data;
998
999 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)map, (hashtab_key_t)key, CIL_SYM_CLASSES, CIL_MAP_CLASS);
1000 if (rc != SEPOL_OK) {
1001 goto exit;
1002 }
1003
1004 rc = cil_gen_perm_nodes(db, parse_current->next->next->cl_head, ast_node, CIL_MAP_PERM, &map->num_perms);
1005 if (rc != SEPOL_OK) {
1006 goto exit;
1007 }
1008
1009 return SEPOL_OK;
1010
1011 exit:
1012 cil_tree_log(parse_current, CIL_ERR, "Bad map class declaration");
1013 cil_destroy_class(map);
1014 cil_clear_node(ast_node);
1015 return rc;
1016 }
1017
1018 int cil_gen_classmapping(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1019 {
1020 int rc = SEPOL_ERR;
1021 struct cil_classmapping *mapping = NULL;
1022 enum cil_syntax syntax[] = {
1023 CIL_SYN_STRING,
1024 CIL_SYN_STRING,
1025 CIL_SYN_STRING,
1026 CIL_SYN_STRING | CIL_SYN_LIST,
1027 CIL_SYN_END
1028 };
1029 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1030
1031 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1032 goto exit;
1033 }
1034
1035 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1036 if (rc != SEPOL_OK) {
1037 goto exit;
1038 }
1039
1040 cil_classmapping_init(&mapping);
1041
1042 mapping->map_class_str = parse_current->next->data;
1043 mapping->map_perm_str = parse_current->next->next->data;
1044
1045 rc = cil_fill_classperms_list(parse_current->next->next->next, &mapping->classperms);
1046 if (rc != SEPOL_OK) {
1047 goto exit;
1048 }
1049
1050 ast_node->data = mapping;
1051 ast_node->flavor = CIL_CLASSMAPPING;
1052
1053 return SEPOL_OK;
1054
1055 exit:
1056 cil_tree_log(parse_current, CIL_ERR, "Bad classmapping declaration");
1057 cil_destroy_classmapping(mapping);
1058 return rc;
1059 }
1060
1061 void cil_destroy_classmapping(struct cil_classmapping *mapping)
1062 {
1063 if (mapping == NULL) {
1064 return;
1065 }
1066
1067 cil_destroy_classperms_list(&mapping->classperms);
1068
1069 free(mapping);
1070 }
1071
1072 // TODO try to merge some of this with cil_gen_class (helper function for both)
1073 int cil_gen_common(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1074 {
1075 enum cil_syntax syntax[] = {
1076 CIL_SYN_STRING,
1077 CIL_SYN_STRING,
1078 CIL_SYN_LIST,
1079 CIL_SYN_END
1080 };
1081 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1082 char *key = NULL;
1083 struct cil_class *common = NULL;
1084 int rc = SEPOL_ERR;
1085
1086 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1087 goto exit;
1088 }
1089
1090 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1091 if (rc != SEPOL_OK) {
1092 goto exit;
1093 }
1094
1095 cil_class_init(&common);
1096
1097 key = parse_current->next->data;
1098
1099 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)common, (hashtab_key_t)key, CIL_SYM_COMMONS, CIL_COMMON);
1100 if (rc != SEPOL_OK) {
1101 goto exit;
1102 }
1103
1104 rc = cil_gen_perm_nodes(db, parse_current->next->next->cl_head, ast_node, CIL_PERM, &common->num_perms);
1105 if (rc != SEPOL_OK) {
1106 goto exit;
1107 }
1108 if (common->num_perms > CIL_PERMS_PER_CLASS) {
1109 cil_tree_log(parse_current, CIL_ERR, "Too many permissions in common '%s'", common->datum.name);
1110 cil_tree_children_destroy(ast_node);
1111 rc = SEPOL_ERR;
1112 goto exit;
1113 }
1114
1115 return SEPOL_OK;
1116
1117 exit:
1118 cil_tree_log(parse_current, CIL_ERR, "Bad common declaration");
1119 cil_destroy_class(common);
1120 cil_clear_node(ast_node);
1121 return rc;
1122
1123 }
1124
1125 int cil_gen_classcommon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1126 {
1127 enum cil_syntax syntax[] = {
1128 CIL_SYN_STRING,
1129 CIL_SYN_STRING,
1130 CIL_SYN_STRING,
1131 CIL_SYN_END
1132 };
1133 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1134 struct cil_classcommon *clscom = NULL;
1135 int rc = SEPOL_ERR;
1136
1137 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1138 goto exit;
1139 }
1140
1141 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1142 if (rc != SEPOL_OK) {
1143 goto exit;
1144 }
1145
1146 cil_classcommon_init(&clscom);
1147
1148 clscom->class_str = parse_current->next->data;
1149 clscom->common_str = parse_current->next->next->data;
1150
1151 ast_node->data = clscom;
1152 ast_node->flavor = CIL_CLASSCOMMON;
1153
1154 return SEPOL_OK;
1155
1156 exit:
1157 cil_tree_log(parse_current, CIL_ERR, "Bad classcommon declaration");
1158 cil_destroy_classcommon(clscom);
1159 return rc;
1160
1161 }
1162
1163 void cil_destroy_classcommon(struct cil_classcommon *clscom)
1164 {
1165 if (clscom == NULL) {
1166 return;
1167 }
1168
1169 free(clscom);
1170 }
1171
1172 int cil_gen_sid(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1173 {
1174 enum cil_syntax syntax[] = {
1175 CIL_SYN_STRING,
1176 CIL_SYN_STRING,
1177 CIL_SYN_END
1178 };
1179 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1180 char *key = NULL;
1181 struct cil_sid *sid = NULL;
1182 int rc = SEPOL_ERR;
1183
1184 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1185 goto exit;
1186 }
1187
1188 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1189 if (rc != SEPOL_OK) {
1190 goto exit;
1191 }
1192
1193 cil_sid_init(&sid);
1194
1195 key = parse_current->next->data;
1196
1197 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)sid, (hashtab_key_t)key, CIL_SYM_SIDS, CIL_SID);
1198 if (rc != SEPOL_OK) {
1199 goto exit;
1200 }
1201
1202 return SEPOL_OK;
1203
1204 exit:
1205 cil_tree_log(parse_current, CIL_ERR, "Bad sid declaration");
1206 cil_destroy_sid(sid);
1207 cil_clear_node(ast_node);
1208 return rc;
1209 }
1210
1211 void cil_destroy_sid(struct cil_sid *sid)
1212 {
1213 if (sid == NULL) {
1214 return;
1215 }
1216
1217 cil_symtab_datum_destroy(&sid->datum);
1218 free(sid);
1219 }
1220
1221 int cil_gen_sidcontext(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1222 {
1223 enum cil_syntax syntax[] = {
1224 CIL_SYN_STRING,
1225 CIL_SYN_STRING,
1226 CIL_SYN_STRING | CIL_SYN_LIST,
1227 CIL_SYN_END
1228 };
1229 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1230 struct cil_sidcontext *sidcon = NULL;
1231 int rc = SEPOL_ERR;
1232
1233 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1234 goto exit;
1235 }
1236
1237 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1238 if (rc != SEPOL_OK) {
1239 goto exit;
1240 }
1241
1242 cil_sidcontext_init(&sidcon);
1243
1244 sidcon->sid_str = parse_current->next->data;
1245
1246 if (parse_current->next->next->cl_head == NULL) {
1247 sidcon->context_str = parse_current->next->next->data;
1248 } else {
1249 cil_context_init(&sidcon->context);
1250
1251 rc = cil_fill_context(parse_current->next->next->cl_head, sidcon->context);
1252 if (rc != SEPOL_OK) {
1253 goto exit;
1254 }
1255 }
1256
1257 ast_node->data = sidcon;
1258 ast_node->flavor = CIL_SIDCONTEXT;
1259
1260 return SEPOL_OK;
1261
1262 exit:
1263 cil_tree_log(parse_current, CIL_ERR, "Bad sidcontext declaration");
1264 cil_destroy_sidcontext(sidcon);
1265 return rc;
1266 }
1267
1268 void cil_destroy_sidcontext(struct cil_sidcontext *sidcon)
1269 {
1270 if (sidcon == NULL) {
1271 return;
1272 }
1273
1274 if (sidcon->context_str == NULL && sidcon->context != NULL) {
1275 cil_destroy_context(sidcon->context);
1276 }
1277
1278 free(sidcon);
1279 }
1280
1281 int cil_gen_user(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1282 {
1283 enum cil_syntax syntax[] = {
1284 CIL_SYN_STRING,
1285 CIL_SYN_STRING,
1286 CIL_SYN_END
1287 };
1288 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1289 char *key = NULL;
1290 struct cil_user *user = NULL;
1291 int rc = SEPOL_ERR;
1292
1293 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1294 goto exit;
1295 }
1296
1297 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1298 if (rc != SEPOL_OK) {
1299 goto exit;
1300 }
1301
1302 cil_user_init(&user);
1303
1304 key = parse_current->next->data;
1305
1306 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)user, (hashtab_key_t)key, CIL_SYM_USERS, CIL_USER);
1307 if (rc != SEPOL_OK) {
1308 goto exit;
1309 }
1310
1311 return SEPOL_OK;
1312
1313 exit:
1314 cil_tree_log(parse_current, CIL_ERR, "Bad user declaration");
1315 cil_destroy_user(user);
1316 cil_clear_node(ast_node);
1317 return rc;
1318 }
1319
1320 void cil_destroy_user(struct cil_user *user)
1321 {
1322 if (user == NULL) {
1323 return;
1324 }
1325
1326 cil_symtab_datum_destroy(&user->datum);
1327 ebitmap_destroy(user->roles);
1328 free(user->roles);
1329 free(user);
1330 }
1331
1332 int cil_gen_userattribute(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1333 {
1334 enum cil_syntax syntax[] = {
1335 CIL_SYN_STRING,
1336 CIL_SYN_STRING,
1337 CIL_SYN_END
1338 };
1339 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1340 char *key = NULL;
1341 struct cil_userattribute *attr = NULL;
1342 int rc = SEPOL_ERR;
1343
1344 if (parse_current == NULL || ast_node == NULL) {
1345 goto exit;
1346 }
1347
1348 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1349 if (rc != SEPOL_OK) {
1350 goto exit;
1351 }
1352
1353 cil_userattribute_init(&attr);
1354
1355 key = parse_current->next->data;
1356 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)attr, (hashtab_key_t)key, CIL_SYM_USERS, CIL_USERATTRIBUTE);
1357 if (rc != SEPOL_OK) {
1358 goto exit;
1359 }
1360
1361 return SEPOL_OK;
1362 exit:
1363 cil_tree_log(parse_current, CIL_ERR, "Bad userattribute declaration");
1364 cil_destroy_userattribute(attr);
1365 cil_clear_node(ast_node);
1366 return rc;
1367 }
1368
1369 void cil_destroy_userattribute(struct cil_userattribute *attr)
1370 {
1371 struct cil_list_item *expr = NULL;
1372 struct cil_list_item *next = NULL;
1373
1374 if (attr == NULL) {
1375 return;
1376 }
1377
1378 if (attr->expr_list != NULL) {
1379 /* we don't want to destroy the expression stacks (cil_list) inside
1380 * this list cil_list_destroy destroys sublists, so we need to do it
1381 * manually */
1382 expr = attr->expr_list->head;
1383 while (expr != NULL) {
1384 next = expr->next;
1385 cil_list_item_destroy(&expr, CIL_FALSE);
1386 expr = next;
1387 }
1388 free(attr->expr_list);
1389 attr->expr_list = NULL;
1390 }
1391
1392 cil_symtab_datum_destroy(&attr->datum);
1393 ebitmap_destroy(attr->users);
1394 free(attr->users);
1395 free(attr);
1396 }
1397
1398 int cil_gen_userattributeset(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1399 {
1400 enum cil_syntax syntax[] = {
1401 CIL_SYN_STRING,
1402 CIL_SYN_STRING,
1403 CIL_SYN_STRING | CIL_SYN_LIST,
1404 CIL_SYN_END
1405 };
1406 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1407 struct cil_userattributeset *attrset = NULL;
1408 int rc = SEPOL_ERR;
1409
1410 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1411 goto exit;
1412 }
1413
1414 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1415 if (rc != SEPOL_OK) {
1416 goto exit;
1417 }
1418
1419 cil_userattributeset_init(&attrset);
1420
1421 attrset->attr_str = parse_current->next->data;
1422
1423 rc = cil_gen_expr(parse_current->next->next, CIL_USER, &attrset->str_expr);
1424 if (rc != SEPOL_OK) {
1425 goto exit;
1426 }
1427 ast_node->data = attrset;
1428 ast_node->flavor = CIL_USERATTRIBUTESET;
1429
1430 return SEPOL_OK;
1431
1432 exit:
1433 cil_tree_log(parse_current, CIL_ERR, "Bad userattributeset declaration");
1434 cil_destroy_userattributeset(attrset);
1435
1436 return rc;
1437 }
1438
1439 void cil_destroy_userattributeset(struct cil_userattributeset *attrset)
1440 {
1441 if (attrset == NULL) {
1442 return;
1443 }
1444
1445 cil_list_destroy(&attrset->str_expr, CIL_TRUE);
1446 cil_list_destroy(&attrset->datum_expr, CIL_FALSE);
1447
1448 free(attrset);
1449 }
1450
1451 int cil_gen_userlevel(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1452 {
1453 enum cil_syntax syntax[] = {
1454 CIL_SYN_STRING,
1455 CIL_SYN_STRING,
1456 CIL_SYN_STRING | CIL_SYN_LIST,
1457 CIL_SYN_END
1458 };
1459 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1460 struct cil_userlevel *usrlvl = NULL;
1461 int rc = SEPOL_ERR;
1462
1463 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1464 goto exit;
1465 }
1466
1467 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1468 if (rc != SEPOL_OK) {
1469 goto exit;
1470 }
1471
1472 cil_userlevel_init(&usrlvl);
1473
1474 usrlvl->user_str = parse_current->next->data;
1475
1476 if (parse_current->next->next->cl_head == NULL) {
1477 usrlvl->level_str = parse_current->next->next->data;
1478 } else {
1479 cil_level_init(&usrlvl->level);
1480
1481 rc = cil_fill_level(parse_current->next->next->cl_head, usrlvl->level);
1482 if (rc != SEPOL_OK) {
1483 goto exit;
1484 }
1485 }
1486
1487 ast_node->data = usrlvl;
1488 ast_node->flavor = CIL_USERLEVEL;
1489
1490 return SEPOL_OK;
1491
1492 exit:
1493 cil_tree_log(parse_current, CIL_ERR, "Bad userlevel declaration");
1494 cil_destroy_userlevel(usrlvl);
1495 return rc;
1496 }
1497
1498 void cil_destroy_userlevel(struct cil_userlevel *usrlvl)
1499 {
1500 if (usrlvl == NULL) {
1501 return;
1502 }
1503
1504 if (usrlvl->level_str == NULL && usrlvl->level != NULL) {
1505 cil_destroy_level(usrlvl->level);
1506 }
1507
1508 free(usrlvl);
1509 }
1510
1511 int cil_gen_userrange(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1512 {
1513 enum cil_syntax syntax[] = {
1514 CIL_SYN_STRING,
1515 CIL_SYN_STRING,
1516 CIL_SYN_STRING | CIL_SYN_LIST,
1517 CIL_SYN_END
1518 };
1519 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1520 struct cil_userrange *userrange = NULL;
1521 int rc = SEPOL_ERR;
1522
1523 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1524 goto exit;
1525 }
1526
1527 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1528 if (rc != SEPOL_OK) {
1529 goto exit;
1530 }
1531
1532 cil_userrange_init(&userrange);
1533
1534 userrange->user_str = parse_current->next->data;
1535
1536 if (parse_current->next->next->cl_head == NULL) {
1537 userrange->range_str = parse_current->next->next->data;
1538 } else {
1539 cil_levelrange_init(&userrange->range);
1540
1541 rc = cil_fill_levelrange(parse_current->next->next->cl_head, userrange->range);
1542 if (rc != SEPOL_OK) {
1543 goto exit;
1544 }
1545 }
1546
1547 ast_node->data = userrange;
1548 ast_node->flavor = CIL_USERRANGE;
1549
1550 return SEPOL_OK;
1551
1552 exit:
1553 cil_tree_log(parse_current, CIL_ERR, "Bad userrange declaration");
1554 cil_destroy_userrange(userrange);
1555 return rc;
1556 }
1557
1558 void cil_destroy_userrange(struct cil_userrange *userrange)
1559 {
1560 if (userrange == NULL) {
1561 return;
1562 }
1563
1564 if (userrange->range_str == NULL && userrange->range != NULL) {
1565 cil_destroy_levelrange(userrange->range);
1566 }
1567
1568 free(userrange);
1569 }
1570
1571 int cil_gen_userprefix(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1572 {
1573 enum cil_syntax syntax[] = {
1574 CIL_SYN_STRING,
1575 CIL_SYN_STRING,
1576 CIL_SYN_STRING,
1577 CIL_SYN_END
1578 };
1579 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1580 struct cil_userprefix *userprefix = NULL;
1581 int rc = SEPOL_ERR;
1582
1583 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1584 goto exit;
1585 }
1586
1587 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1588 if (rc != SEPOL_OK) {
1589 goto exit;
1590 }
1591
1592 cil_userprefix_init(&userprefix);
1593
1594 userprefix->user_str = parse_current->next->data;
1595 userprefix->prefix_str = parse_current->next->next->data;
1596
1597 ast_node->data = userprefix;
1598 ast_node->flavor = CIL_USERPREFIX;
1599
1600 return SEPOL_OK;
1601 exit:
1602 cil_tree_log(parse_current, CIL_ERR, "Bad userprefix declaration");
1603 cil_destroy_userprefix(userprefix);
1604 return rc;
1605 }
1606
1607 void cil_destroy_userprefix(struct cil_userprefix *userprefix)
1608 {
1609 if (userprefix == NULL) {
1610 return;
1611 }
1612
1613 free(userprefix);
1614 }
1615
1616 int cil_gen_selinuxuser(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1617 {
1618 enum cil_syntax syntax[] = {
1619 CIL_SYN_STRING,
1620 CIL_SYN_STRING,
1621 CIL_SYN_STRING,
1622 CIL_SYN_STRING | CIL_SYN_LIST,
1623 CIL_SYN_END
1624 };
1625 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1626 struct cil_selinuxuser *selinuxuser = NULL;
1627 int rc = SEPOL_ERR;
1628
1629 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1630 goto exit;
1631 }
1632
1633 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1634 if (rc != SEPOL_OK) {
1635 goto exit;
1636 }
1637
1638 cil_selinuxuser_init(&selinuxuser);
1639
1640 selinuxuser->name_str = parse_current->next->data;
1641 selinuxuser->user_str = parse_current->next->next->data;
1642
1643 if (parse_current->next->next->next->cl_head == NULL) {
1644 selinuxuser->range_str = parse_current->next->next->next->data;
1645 } else {
1646 cil_levelrange_init(&selinuxuser->range);
1647
1648 rc = cil_fill_levelrange(parse_current->next->next->next->cl_head, selinuxuser->range);
1649 if (rc != SEPOL_OK) {
1650 goto exit;
1651 }
1652 }
1653
1654 ast_node->data = selinuxuser;
1655 ast_node->flavor = CIL_SELINUXUSER;
1656
1657 return SEPOL_OK;
1658 exit:
1659 cil_tree_log(parse_current, CIL_ERR, "Bad selinuxuser declaration");
1660 cil_destroy_selinuxuser(selinuxuser);
1661 return rc;
1662 }
1663
1664 int cil_gen_selinuxuserdefault(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1665 {
1666 enum cil_syntax syntax[] = {
1667 CIL_SYN_STRING,
1668 CIL_SYN_STRING,
1669 CIL_SYN_STRING | CIL_SYN_LIST,
1670 CIL_SYN_END
1671 };
1672 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1673 struct cil_selinuxuser *selinuxuser = NULL;
1674 int rc = SEPOL_ERR;
1675
1676 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1677 goto exit;
1678 }
1679
1680 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1681 if (rc != SEPOL_OK) {
1682 goto exit;
1683 }
1684
1685 cil_selinuxuser_init(&selinuxuser);
1686
1687 selinuxuser->name_str = cil_strpool_add("__default__");
1688 selinuxuser->user_str = parse_current->next->data;
1689
1690 if (parse_current->next->next->cl_head == NULL) {
1691 selinuxuser->range_str = parse_current->next->next->data;
1692 } else {
1693 cil_levelrange_init(&selinuxuser->range);
1694
1695 rc = cil_fill_levelrange(parse_current->next->next->cl_head, selinuxuser->range);
1696 if (rc != SEPOL_OK) {
1697 goto exit;
1698 }
1699 }
1700
1701 ast_node->data = selinuxuser;
1702 ast_node->flavor = CIL_SELINUXUSERDEFAULT;
1703
1704 return SEPOL_OK;
1705 exit:
1706 cil_tree_log(parse_current, CIL_ERR, "Bad selinuxuserdefault declaration");
1707 cil_destroy_selinuxuser(selinuxuser);
1708 return rc;
1709 }
1710
1711 void cil_destroy_selinuxuser(struct cil_selinuxuser *selinuxuser)
1712 {
1713 if (selinuxuser == NULL) {
1714 return;
1715 }
1716
1717 if (selinuxuser->range_str == NULL && selinuxuser->range != NULL) {
1718 cil_destroy_levelrange(selinuxuser->range);
1719 }
1720
1721 free(selinuxuser);
1722 }
1723
1724 int cil_gen_role(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1725 {
1726 enum cil_syntax syntax[] = {
1727 CIL_SYN_STRING,
1728 CIL_SYN_STRING,
1729 CIL_SYN_END
1730 };
1731 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1732 char *key = NULL;
1733 struct cil_role *role = NULL;
1734 int rc = SEPOL_ERR;
1735
1736 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1737 goto exit;
1738 }
1739
1740 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1741 if (rc != SEPOL_OK) {
1742 goto exit;
1743 }
1744
1745 cil_role_init(&role);
1746
1747 key = parse_current->next->data;
1748
1749 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)role, (hashtab_key_t)key, CIL_SYM_ROLES, CIL_ROLE);
1750 if (rc != SEPOL_OK) {
1751 if (rc == SEPOL_EEXIST) {
1752 cil_destroy_role(role);
1753 role = NULL;
1754 } else {
1755 goto exit;
1756 }
1757 }
1758
1759 return SEPOL_OK;
1760
1761 exit:
1762 cil_tree_log(parse_current, CIL_ERR, "Bad role declaration");
1763 cil_destroy_role(role);
1764 cil_clear_node(ast_node);
1765 return rc;
1766 }
1767
1768 void cil_destroy_role(struct cil_role *role)
1769 {
1770 if (role == NULL) {
1771 return;
1772 }
1773
1774 cil_symtab_datum_destroy(&role->datum);
1775 ebitmap_destroy(role->types);
1776 free(role->types);
1777 free(role);
1778 }
1779
1780 int cil_gen_roletype(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1781 {
1782 enum cil_syntax syntax[] = {
1783 CIL_SYN_STRING,
1784 CIL_SYN_STRING,
1785 CIL_SYN_STRING,
1786 CIL_SYN_END
1787 };
1788 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1789 struct cil_roletype *roletype = NULL;
1790 int rc = SEPOL_ERR;
1791
1792 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1793 goto exit;
1794 }
1795
1796 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1797 if (rc != SEPOL_OK) {
1798 goto exit;
1799 }
1800
1801 cil_roletype_init(&roletype);
1802
1803 roletype->role_str = parse_current->next->data;
1804 roletype->type_str = parse_current->next->next->data;
1805
1806 ast_node->data = roletype;
1807 ast_node->flavor = CIL_ROLETYPE;
1808
1809 return SEPOL_OK;
1810
1811 exit:
1812 cil_tree_log(parse_current, CIL_ERR, "Bad roletype declaration");
1813 cil_destroy_roletype(roletype);
1814 return rc;
1815 }
1816
1817 void cil_destroy_roletype(struct cil_roletype *roletype)
1818 {
1819 if (roletype == NULL) {
1820 return;
1821 }
1822
1823 free(roletype);
1824 }
1825
1826 int cil_gen_userrole(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1827 {
1828 enum cil_syntax syntax[] = {
1829 CIL_SYN_STRING,
1830 CIL_SYN_STRING,
1831 CIL_SYN_STRING,
1832 CIL_SYN_END
1833 };
1834 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1835 struct cil_userrole *userrole = NULL;
1836 int rc = SEPOL_ERR;
1837
1838 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1839 goto exit;
1840 }
1841
1842 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1843 if (rc != SEPOL_OK) {
1844 goto exit;
1845 }
1846
1847 cil_userrole_init(&userrole);
1848
1849 userrole->user_str = parse_current->next->data;
1850 userrole->role_str = parse_current->next->next->data;
1851
1852 ast_node->data = userrole;
1853 ast_node->flavor = CIL_USERROLE;
1854
1855 return SEPOL_OK;
1856
1857 exit:
1858 cil_tree_log(parse_current, CIL_ERR, "Bad userrole declaration");
1859 cil_destroy_userrole(userrole);
1860 return rc;
1861 }
1862
1863 void cil_destroy_userrole(struct cil_userrole *userrole)
1864 {
1865 if (userrole == NULL) {
1866 return;
1867 }
1868
1869 free(userrole);
1870 }
1871
1872 int cil_gen_roletransition(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1873 {
1874 enum cil_syntax syntax[] = {
1875 CIL_SYN_STRING,
1876 CIL_SYN_STRING,
1877 CIL_SYN_STRING,
1878 CIL_SYN_STRING,
1879 CIL_SYN_STRING,
1880 CIL_SYN_END
1881 };
1882 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1883 struct cil_roletransition *roletrans = NULL;
1884 int rc = SEPOL_ERR;
1885
1886 if (parse_current == NULL || ast_node == NULL) {
1887 goto exit;
1888 }
1889
1890 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1891 if (rc != SEPOL_OK) {
1892 goto exit;
1893 }
1894
1895 cil_roletransition_init(&roletrans);
1896
1897 roletrans->src_str = parse_current->next->data;
1898 roletrans->tgt_str = parse_current->next->next->data;
1899 roletrans->obj_str = parse_current->next->next->next->data;
1900 roletrans->result_str = parse_current->next->next->next->next->data;
1901
1902 ast_node->data = roletrans;
1903 ast_node->flavor = CIL_ROLETRANSITION;
1904
1905 return SEPOL_OK;
1906
1907 exit:
1908 cil_tree_log(parse_current, CIL_ERR, "Bad roletransition rule");
1909 cil_destroy_roletransition(roletrans);
1910 return rc;
1911 }
1912
1913 void cil_destroy_roletransition(struct cil_roletransition *roletrans)
1914 {
1915 if (roletrans == NULL) {
1916 return;
1917 }
1918
1919 free(roletrans);
1920 }
1921
1922 int cil_gen_roleallow(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1923 {
1924 enum cil_syntax syntax[] = {
1925 CIL_SYN_STRING,
1926 CIL_SYN_STRING,
1927 CIL_SYN_STRING,
1928 CIL_SYN_END
1929 };
1930 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1931 struct cil_roleallow *roleallow = NULL;
1932 int rc = SEPOL_ERR;
1933
1934 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1935 goto exit;
1936 }
1937
1938 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1939 if (rc != SEPOL_OK) {
1940 goto exit;
1941 }
1942
1943 cil_roleallow_init(&roleallow);
1944
1945 roleallow->src_str = parse_current->next->data;
1946 roleallow->tgt_str = parse_current->next->next->data;
1947
1948 ast_node->data = roleallow;
1949 ast_node->flavor = CIL_ROLEALLOW;
1950
1951 return SEPOL_OK;
1952
1953 exit:
1954 cil_tree_log(parse_current, CIL_ERR, "Bad roleallow rule");
1955 cil_destroy_roleallow(roleallow);
1956 return rc;
1957 }
1958
1959 void cil_destroy_roleallow(struct cil_roleallow *roleallow)
1960 {
1961 if (roleallow == NULL) {
1962 return;
1963 }
1964
1965 free(roleallow);
1966 }
1967
1968 int cil_gen_roleattribute(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
1969 {
1970 enum cil_syntax syntax[] = {
1971 CIL_SYN_STRING,
1972 CIL_SYN_STRING,
1973 CIL_SYN_END
1974 };
1975 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
1976 char *key = NULL;
1977 struct cil_roleattribute *attr = NULL;
1978 int rc = SEPOL_ERR;
1979
1980 if (db == NULL || parse_current == NULL || ast_node == NULL) {
1981 goto exit;
1982 }
1983
1984 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
1985 if (rc != SEPOL_OK) {
1986 goto exit;
1987 }
1988
1989 cil_roleattribute_init(&attr);
1990
1991 key = parse_current->next->data;
1992 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)attr, (hashtab_key_t)key, CIL_SYM_ROLES, CIL_ROLEATTRIBUTE);
1993 if (rc != SEPOL_OK) {
1994 goto exit;
1995 }
1996
1997 return SEPOL_OK;
1998 exit:
1999 cil_tree_log(parse_current, CIL_ERR, "Bad roleattribute declaration");
2000 cil_destroy_roleattribute(attr);
2001 cil_clear_node(ast_node);
2002 return rc;
2003 }
2004
2005 void cil_destroy_roleattribute(struct cil_roleattribute *attr)
2006 {
2007 if (attr == NULL) {
2008 return;
2009 }
2010
2011 if (attr->expr_list != NULL) {
2012 /* we don't want to destroy the expression stacks (cil_list) inside
2013 * this list cil_list_destroy destroys sublists, so we need to do it
2014 * manually */
2015 struct cil_list_item *expr = attr->expr_list->head;
2016 while (expr != NULL) {
2017 struct cil_list_item *next = expr->next;
2018 cil_list_item_destroy(&expr, CIL_FALSE);
2019 expr = next;
2020 }
2021 free(attr->expr_list);
2022 attr->expr_list = NULL;
2023 }
2024
2025 cil_symtab_datum_destroy(&attr->datum);
2026 ebitmap_destroy(attr->roles);
2027 free(attr->roles);
2028 free(attr);
2029 }
2030
2031 int cil_gen_roleattributeset(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
2032 {
2033 enum cil_syntax syntax[] = {
2034 CIL_SYN_STRING,
2035 CIL_SYN_STRING,
2036 CIL_SYN_STRING | CIL_SYN_LIST,
2037 CIL_SYN_END
2038 };
2039 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2040 struct cil_roleattributeset *attrset = NULL;
2041 int rc = SEPOL_ERR;
2042
2043 if (db == NULL || parse_current == NULL || ast_node == NULL) {
2044 goto exit;
2045 }
2046
2047 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2048 if (rc != SEPOL_OK) {
2049 goto exit;
2050 }
2051
2052 cil_roleattributeset_init(&attrset);
2053
2054 attrset->attr_str = parse_current->next->data;
2055
2056 rc = cil_gen_expr(parse_current->next->next, CIL_ROLE, &attrset->str_expr);
2057 if (rc != SEPOL_OK) {
2058 goto exit;
2059 }
2060 ast_node->data = attrset;
2061 ast_node->flavor = CIL_ROLEATTRIBUTESET;
2062
2063 return SEPOL_OK;
2064
2065 exit:
2066 cil_tree_log(parse_current, CIL_ERR, "Bad roleattributeset declaration");
2067 cil_destroy_roleattributeset(attrset);
2068
2069 return rc;
2070 }
2071
2072 void cil_destroy_roleattributeset(struct cil_roleattributeset *attrset)
2073 {
2074 if (attrset == NULL) {
2075 return;
2076 }
2077
2078 cil_list_destroy(&attrset->str_expr, CIL_TRUE);
2079 cil_list_destroy(&attrset->datum_expr, CIL_FALSE);
2080
2081 free(attrset);
2082 }
2083
2084 int cil_gen_avrule(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, uint32_t rule_kind)
2085 {
2086 enum cil_syntax syntax[] = {
2087 CIL_SYN_STRING,
2088 CIL_SYN_STRING,
2089 CIL_SYN_STRING,
2090 CIL_SYN_STRING | CIL_SYN_LIST,
2091 CIL_SYN_END
2092 };
2093 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2094 struct cil_avrule *rule = NULL;
2095 int rc = SEPOL_ERR;
2096
2097 if (parse_current == NULL || ast_node == NULL) {
2098 goto exit;
2099 }
2100
2101 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2102 if (rc != SEPOL_OK) {
2103 goto exit;
2104 }
2105
2106 cil_avrule_init(&rule);
2107
2108 rule->is_extended = 0;
2109 rule->rule_kind = rule_kind;
2110
2111 rule->src_str = parse_current->next->data;
2112 rule->tgt_str = parse_current->next->next->data;
2113
2114 rc = cil_fill_classperms_list(parse_current->next->next->next, &rule->perms.classperms);
2115 if (rc != SEPOL_OK) {
2116 goto exit;
2117 }
2118
2119 ast_node->data = rule;
2120 ast_node->flavor = CIL_AVRULE;
2121
2122 return SEPOL_OK;
2123
2124 exit:
2125 cil_tree_log(parse_current, CIL_ERR, "Bad allow rule");
2126 cil_destroy_avrule(rule);
2127 return rc;
2128 }
2129
2130 void cil_destroy_avrule(struct cil_avrule *rule)
2131 {
2132 if (rule == NULL) {
2133 return;
2134 }
2135
2136 if (!rule->is_extended) {
2137 cil_destroy_classperms_list(&rule->perms.classperms);
2138 } else {
2139 if (rule->perms.x.permx_str == NULL && rule->perms.x.permx != NULL) {
2140 cil_destroy_permissionx(rule->perms.x.permx);
2141 }
2142 }
2143
2144 free(rule);
2145 }
2146
2147 static int cil_fill_permissionx(struct cil_tree_node *parse_current, struct cil_permissionx *permx)
2148 {
2149 enum cil_syntax syntax[] = {
2150 CIL_SYN_STRING,
2151 CIL_SYN_STRING,
2152 CIL_SYN_LIST,
2153 CIL_SYN_END
2154 };
2155 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2156 int rc = SEPOL_ERR;
2157
2158 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2159 if (rc != SEPOL_OK) {
2160 goto exit;
2161 }
2162
2163 if (parse_current->data == CIL_KEY_IOCTL) {
2164 permx->kind = CIL_PERMX_KIND_IOCTL;
2165 } else if (parse_current->data == CIL_KEY_NLMSG) {
2166 permx->kind = CIL_PERMX_KIND_NLMSG;
2167 } else {
2168 cil_log(CIL_ERR, "Unknown permissionx kind, %s. Must be \"ioctl\" or \"nlmsg\"\n", (char *)parse_current->data);
2169 rc = SEPOL_ERR;
2170 goto exit;
2171 }
2172
2173 permx->obj_str = parse_current->next->data;
2174
2175 rc = cil_gen_expr(parse_current->next->next, CIL_PERMISSIONX, &permx->expr_str);
2176 if (rc != SEPOL_OK) {
2177 goto exit;
2178 }
2179
2180 return SEPOL_OK;
2181
2182 exit:
2183 cil_tree_log(parse_current, CIL_ERR, "Bad permissionx content");
2184 return rc;
2185 }
2186
2187 int cil_gen_permissionx(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
2188 {
2189 enum cil_syntax syntax[] = {
2190 CIL_SYN_STRING,
2191 CIL_SYN_STRING,
2192 CIL_SYN_LIST,
2193 CIL_SYN_END
2194 };
2195 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2196 char *key = NULL;
2197 struct cil_permissionx *permx = NULL;
2198 int rc = SEPOL_ERR;
2199
2200 if (parse_current == NULL || ast_node == NULL) {
2201 goto exit;
2202 }
2203
2204 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2205 if (rc != SEPOL_OK) {
2206 goto exit;
2207 }
2208
2209 cil_permissionx_init(&permx);
2210
2211 key = parse_current->next->data;
2212
2213 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)permx, (hashtab_key_t)key, CIL_SYM_PERMX, CIL_PERMISSIONX);
2214 if (rc != SEPOL_OK) {
2215 goto exit;
2216 }
2217
2218 rc = cil_fill_permissionx(parse_current->next->next->cl_head, permx);
2219 if (rc != SEPOL_OK) {
2220 goto exit;
2221 }
2222
2223 return SEPOL_OK;
2224
2225 exit:
2226 cil_tree_log(parse_current, CIL_ERR, "Bad permissionx statement");
2227 cil_destroy_permissionx(permx);
2228 cil_clear_node(ast_node);
2229 return rc;
2230 }
2231
2232 void cil_destroy_permissionx(struct cil_permissionx *permx)
2233 {
2234 if (permx == NULL) {
2235 return;
2236 }
2237
2238 cil_symtab_datum_destroy(&permx->datum);
2239
2240 cil_list_destroy(&permx->expr_str, CIL_TRUE);
2241 ebitmap_destroy(permx->perms);
2242 free(permx->perms);
2243 free(permx);
2244 }
2245
2246 int cil_gen_avrulex(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, uint32_t rule_kind)
2247 {
2248 enum cil_syntax syntax[] = {
2249 CIL_SYN_STRING,
2250 CIL_SYN_STRING,
2251 CIL_SYN_STRING,
2252 CIL_SYN_STRING | CIL_SYN_LIST,
2253 CIL_SYN_END
2254 };
2255 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2256 struct cil_avrule *rule = NULL;
2257 int rc = SEPOL_ERR;
2258
2259 if (parse_current == NULL || ast_node == NULL) {
2260 goto exit;
2261 }
2262
2263 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2264 if (rc != SEPOL_OK) {
2265 goto exit;
2266 }
2267
2268 cil_avrule_init(&rule);
2269
2270 rule->is_extended = 1;
2271 rule->rule_kind = rule_kind;
2272 rule->src_str = parse_current->next->data;
2273 rule->tgt_str = parse_current->next->next->data;
2274
2275 if (parse_current->next->next->next->cl_head == NULL) {
2276 rule->perms.x.permx_str = parse_current->next->next->next->data;
2277 } else {
2278 cil_permissionx_init(&rule->perms.x.permx);
2279
2280 rc = cil_fill_permissionx(parse_current->next->next->next->cl_head, rule->perms.x.permx);
2281 if (rc != SEPOL_OK) {
2282 goto exit;
2283 }
2284 }
2285
2286 ast_node->data = rule;
2287 ast_node->flavor = CIL_AVRULEX;
2288
2289 return SEPOL_OK;
2290
2291 exit:
2292 cil_tree_log(parse_current, CIL_ERR, "Bad allowx rule");
2293 cil_destroy_avrule(rule);
2294 return rc;
2295 }
2296
2297 int cil_gen_deny_rule(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
2298 {
2299 enum cil_syntax syntax[] = {
2300 CIL_SYN_STRING,
2301 CIL_SYN_STRING,
2302 CIL_SYN_STRING,
2303 CIL_SYN_STRING | CIL_SYN_LIST,
2304 CIL_SYN_END
2305 };
2306 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2307 struct cil_deny_rule *rule = NULL;
2308 int rc = SEPOL_ERR;
2309
2310 if (parse_current == NULL || ast_node == NULL) {
2311 goto exit;
2312 }
2313
2314 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2315 if (rc != SEPOL_OK) {
2316 goto exit;
2317 }
2318
2319 cil_deny_rule_init(&rule);
2320
2321 rule->src_str = parse_current->next->data;
2322 rule->tgt_str = parse_current->next->next->data;
2323
2324 rc = cil_fill_classperms_list(parse_current->next->next->next, &rule->classperms);
2325 if (rc != SEPOL_OK) {
2326 goto exit;
2327 }
2328
2329 ast_node->data = rule;
2330 ast_node->flavor = CIL_DENY_RULE;
2331
2332 return SEPOL_OK;
2333
2334 exit:
2335 cil_tree_log(parse_current, CIL_ERR, "Bad deny rule");
2336 cil_destroy_deny_rule(rule);
2337 return rc;
2338 }
2339
2340 void cil_destroy_deny_rule(struct cil_deny_rule *rule)
2341 {
2342 if (rule == NULL) {
2343 return;
2344 }
2345
2346 cil_destroy_classperms_list(&rule->classperms);
2347
2348 free(rule);
2349 }
2350
2351 int cil_gen_type_rule(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, uint32_t rule_kind)
2352 {
2353 enum cil_syntax syntax[] = {
2354 CIL_SYN_STRING,
2355 CIL_SYN_STRING,
2356 CIL_SYN_STRING,
2357 CIL_SYN_STRING,
2358 CIL_SYN_STRING,
2359 CIL_SYN_END
2360 };
2361 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2362 struct cil_type_rule *rule = NULL;
2363 int rc = SEPOL_ERR;
2364
2365 if (parse_current == NULL || ast_node == NULL) {
2366 goto exit;
2367 }
2368
2369 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2370 if (rc != SEPOL_OK) {
2371 goto exit;
2372 }
2373
2374 cil_type_rule_init(&rule);
2375
2376 rule->rule_kind = rule_kind;
2377 rule->src_str = parse_current->next->data;
2378 rule->tgt_str = parse_current->next->next->data;
2379 rule->obj_str = parse_current->next->next->next->data;
2380 rule->result_str = parse_current->next->next->next->next->data;
2381
2382 ast_node->data = rule;
2383 ast_node->flavor = CIL_TYPE_RULE;
2384
2385 return SEPOL_OK;
2386
2387 exit:
2388 cil_tree_log(parse_current, CIL_ERR, "Bad type rule");
2389 cil_destroy_type_rule(rule);
2390 return rc;
2391 }
2392
2393 void cil_destroy_type_rule(struct cil_type_rule *rule)
2394 {
2395 if (rule == NULL) {
2396 return;
2397 }
2398
2399 free(rule);
2400 }
2401
2402 int cil_gen_type(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
2403 {
2404 enum cil_syntax syntax[] = {
2405 CIL_SYN_STRING,
2406 CIL_SYN_STRING,
2407 CIL_SYN_END
2408 };
2409 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2410 char *key = NULL;
2411 struct cil_type *type = NULL;
2412 int rc = SEPOL_ERR;
2413
2414 if (db == NULL || parse_current == NULL || ast_node == NULL) {
2415 goto exit;
2416 }
2417
2418 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2419 if (rc != SEPOL_OK) {
2420 goto exit;
2421 }
2422
2423 cil_type_init(&type);
2424
2425 key = parse_current->next->data;
2426 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)type, (hashtab_key_t)key, CIL_SYM_TYPES, CIL_TYPE);
2427 if (rc != SEPOL_OK) {
2428 if (rc == SEPOL_EEXIST) {
2429 cil_destroy_type(type);
2430 type = NULL;
2431 } else {
2432 goto exit;
2433 }
2434 }
2435
2436 return SEPOL_OK;
2437
2438 exit:
2439 cil_tree_log(parse_current, CIL_ERR, "Bad type declaration");
2440 cil_destroy_type(type);
2441 cil_clear_node(ast_node);
2442 return rc;
2443 }
2444
2445 void cil_destroy_type(struct cil_type *type)
2446 {
2447 if (type == NULL) {
2448 return;
2449 }
2450
2451 cil_symtab_datum_destroy(&type->datum);
2452 free(type);
2453 }
2454
2455 int cil_gen_typeattribute(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
2456 {
2457 enum cil_syntax syntax[] = {
2458 CIL_SYN_STRING,
2459 CIL_SYN_STRING,
2460 CIL_SYN_END
2461 };
2462 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2463 char *key = NULL;
2464 struct cil_typeattribute *attr = NULL;
2465 int rc = SEPOL_ERR;
2466
2467 if (db == NULL || parse_current == NULL || ast_node == NULL) {
2468 goto exit;
2469 }
2470
2471 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2472 if (rc != SEPOL_OK) {
2473 goto exit;
2474 }
2475
2476 cil_typeattribute_init(&attr);
2477
2478 key = parse_current->next->data;
2479 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)attr, (hashtab_key_t)key, CIL_SYM_TYPES, CIL_TYPEATTRIBUTE);
2480 if (rc != SEPOL_OK) {
2481 if (rc == SEPOL_EEXIST) {
2482 cil_destroy_typeattribute(attr);
2483 attr = NULL;
2484 } else {
2485 goto exit;
2486 }
2487 }
2488
2489 return SEPOL_OK;
2490
2491 exit:
2492 cil_tree_log(parse_current, CIL_ERR, "Bad typeattribute declaration");
2493 cil_destroy_typeattribute(attr);
2494 cil_clear_node(ast_node);
2495 return rc;
2496 }
2497
2498 void cil_destroy_typeattribute(struct cil_typeattribute *attr)
2499 {
2500 if (attr == NULL) {
2501 return;
2502 }
2503
2504 cil_symtab_datum_destroy(&attr->datum);
2505
2506 if (attr->expr_list != NULL) {
2507 /* we don't want to destroy the expression stacks (cil_list) inside
2508 * this list cil_list_destroy destroys sublists, so we need to do it
2509 * manually */
2510 struct cil_list_item *expr = attr->expr_list->head;
2511 while (expr != NULL) {
2512 struct cil_list_item *next = expr->next;
2513 cil_list_item_destroy(&expr, CIL_FALSE);
2514 expr = next;
2515 }
2516 free(attr->expr_list);
2517 attr->expr_list = NULL;
2518 }
2519 ebitmap_destroy(attr->types);
2520 free(attr->types);
2521 free(attr);
2522 }
2523
2524 int cil_gen_bool(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, int tunableif)
2525 {
2526 enum cil_syntax syntax[] = {
2527 CIL_SYN_STRING,
2528 CIL_SYN_STRING,
2529 CIL_SYN_STRING,
2530 CIL_SYN_END
2531 };
2532 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2533 char *key = NULL;
2534 struct cil_bool *boolean = NULL;
2535 int rc = SEPOL_ERR;
2536
2537 if (db == NULL || parse_current == NULL || ast_node == NULL) {
2538 goto exit;
2539 }
2540
2541 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2542 if (rc != SEPOL_OK) {
2543 goto exit;
2544 }
2545
2546 cil_bool_init(&boolean);
2547
2548 key = parse_current->next->data;
2549
2550 if (parse_current->next->next->data == CIL_KEY_CONDTRUE) {
2551 boolean->value = CIL_TRUE;
2552 } else if (parse_current->next->next->data == CIL_KEY_CONDFALSE) {
2553 boolean->value = CIL_FALSE;
2554 } else {
2555 cil_log(CIL_ERR, "Value must be either \'true\' or \'false\'");
2556 rc = SEPOL_ERR;
2557 goto exit;
2558 }
2559
2560 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)boolean, (hashtab_key_t)key, CIL_SYM_BOOLS, CIL_BOOL);
2561 if (rc != SEPOL_OK) {
2562 goto exit;
2563 }
2564
2565 return SEPOL_OK;
2566
2567 exit:
2568 if (tunableif) {
2569 cil_tree_log(parse_current, CIL_ERR, "Bad tunable (treated as a boolean due to preserve-tunables) declaration");
2570 } else {
2571 cil_tree_log(parse_current, CIL_ERR, "Bad boolean declaration");
2572 }
2573 cil_destroy_bool(boolean);
2574 cil_clear_node(ast_node);
2575 return rc;
2576 }
2577
2578 void cil_destroy_bool(struct cil_bool *boolean)
2579 {
2580 if (boolean == NULL) {
2581 return;
2582 }
2583
2584 cil_symtab_datum_destroy(&boolean->datum);
2585 free(boolean);
2586 }
2587
2588 int cil_gen_tunable(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
2589 {
2590 enum cil_syntax syntax[] = {
2591 CIL_SYN_STRING,
2592 CIL_SYN_STRING,
2593 CIL_SYN_STRING,
2594 CIL_SYN_END
2595 };
2596 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2597 char *key = NULL;
2598 struct cil_tunable *tunable = NULL;
2599 int rc = SEPOL_ERR;
2600
2601 if (db == NULL || parse_current == NULL || ast_node == NULL) {
2602 goto exit;
2603 }
2604
2605 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2606 if (rc != SEPOL_OK) {
2607 goto exit;
2608 }
2609
2610 cil_tunable_init(&tunable);
2611
2612 key = parse_current->next->data;
2613
2614 if (parse_current->next->next->data == CIL_KEY_CONDTRUE) {
2615 tunable->value = CIL_TRUE;
2616 } else if (parse_current->next->next->data == CIL_KEY_CONDFALSE) {
2617 tunable->value = CIL_FALSE;
2618 } else {
2619 cil_log(CIL_ERR, "Value must be either \'true\' or \'false\'");
2620 rc = SEPOL_ERR;
2621 goto exit;
2622 }
2623
2624 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)tunable, (hashtab_key_t)key, CIL_SYM_TUNABLES, CIL_TUNABLE);
2625 if (rc != SEPOL_OK) {
2626 goto exit;
2627 }
2628
2629 return SEPOL_OK;
2630
2631 exit:
2632 cil_tree_log(parse_current, CIL_ERR, "Bad tunable declaration");
2633 cil_destroy_tunable(tunable);
2634 cil_clear_node(ast_node);
2635 return rc;
2636 }
2637
2638 void cil_destroy_tunable(struct cil_tunable *tunable)
2639 {
2640 if (tunable == NULL) {
2641 return;
2642 }
2643
2644 cil_symtab_datum_destroy(&tunable->datum);
2645 free(tunable);
2646 }
2647
2648 static enum cil_flavor __cil_get_expr_operator_flavor(const char *op)
2649 {
2650 if (op == NULL) return CIL_NONE;
2651 else if (op == CIL_KEY_AND) return CIL_AND;
2652 else if (op == CIL_KEY_OR) return CIL_OR;
2653 else if (op == CIL_KEY_NOT) return CIL_NOT;
2654 else if (op == CIL_KEY_EQ) return CIL_EQ; /* Only conditional */
2655 else if (op == CIL_KEY_NEQ) return CIL_NEQ; /* Only conditional */
2656 else if (op == CIL_KEY_XOR) return CIL_XOR;
2657 else if (op == CIL_KEY_ALL) return CIL_ALL; /* Only set and permissionx */
2658 else if (op == CIL_KEY_RANGE) return CIL_RANGE; /* Only catset and permissionx */
2659 else return CIL_NONE;
2660 }
2661
2662 static int __cil_fill_expr(struct cil_tree_node *current, enum cil_flavor flavor, struct cil_list *expr);
2663
2664 static int __cil_fill_expr_helper(struct cil_tree_node *current, enum cil_flavor flavor, struct cil_list *expr)
2665 {
2666 int rc = SEPOL_ERR;
2667 enum cil_flavor op;
2668
2669 op = __cil_get_expr_operator_flavor(current->data);
2670
2671 rc = cil_verify_expr_syntax(current, op, flavor);
2672 if (rc != SEPOL_OK) {
2673 goto exit;
2674 }
2675
2676 if (op != CIL_NONE) {
2677 cil_list_append(expr, CIL_OP, (void *)op);
2678 current = current->next;
2679 }
2680
2681 for (;current != NULL; current = current->next) {
2682 rc = __cil_fill_expr(current, flavor, expr);
2683 if (rc != SEPOL_OK) {
2684 goto exit;
2685 }
2686 }
2687
2688 return SEPOL_OK;
2689
2690 exit:
2691 return rc;
2692 }
2693
2694 static int __cil_fill_expr(struct cil_tree_node *current, enum cil_flavor flavor, struct cil_list *expr)
2695 {
2696 int rc = SEPOL_ERR;
2697
2698 if (current->cl_head == NULL) {
2699 enum cil_flavor op = __cil_get_expr_operator_flavor(current->data);
2700 if (op != CIL_NONE) {
2701 cil_log(CIL_ERR, "Operator (%s) not in an expression\n", (char*)current->data);
2702 goto exit;
2703 }
2704 cil_list_append(expr, CIL_STRING, current->data);
2705 } else {
2706 struct cil_list *sub_expr;
2707 cil_list_init(&sub_expr, flavor);
2708 rc = __cil_fill_expr_helper(current->cl_head, flavor, sub_expr);
2709 if (rc != SEPOL_OK) {
2710 cil_list_destroy(&sub_expr, CIL_TRUE);
2711 goto exit;
2712 }
2713 cil_list_append(expr, CIL_LIST, sub_expr);
2714 }
2715
2716 return SEPOL_OK;
2717
2718 exit:
2719 return rc;
2720 }
2721
2722
2723 int cil_gen_expr(struct cil_tree_node *current, enum cil_flavor flavor, struct cil_list **expr)
2724 {
2725 int rc = SEPOL_ERR;
2726
2727 cil_list_init(expr, flavor);
2728
2729 if (current->cl_head == NULL) {
2730 rc = __cil_fill_expr(current, flavor, *expr);
2731 } else {
2732 rc = __cil_fill_expr_helper(current->cl_head, flavor, *expr);
2733 }
2734
2735 if (rc != SEPOL_OK) {
2736 cil_list_destroy(expr, CIL_TRUE);
2737 cil_log(CIL_ERR, "Bad expression\n");
2738 }
2739
2740 return rc;
2741 }
2742
2743 static enum cil_flavor __cil_get_constraint_operator_flavor(const char *op)
2744 {
2745 if (op == CIL_KEY_AND) return CIL_AND;
2746 else if (op == CIL_KEY_OR) return CIL_OR;
2747 else if (op == CIL_KEY_NOT) return CIL_NOT;
2748 else if (op == CIL_KEY_EQ) return CIL_EQ;
2749 else if (op == CIL_KEY_NEQ) return CIL_NEQ;
2750 else if (op == CIL_KEY_CONS_DOM) return CIL_CONS_DOM;
2751 else if (op == CIL_KEY_CONS_DOMBY) return CIL_CONS_DOMBY;
2752 else if (op == CIL_KEY_CONS_INCOMP) return CIL_CONS_INCOMP;
2753 else return CIL_NONE;
2754 }
2755
2756 static enum cil_flavor __cil_get_constraint_operand_flavor(const char *operand)
2757 {
2758 if (operand == NULL) return CIL_LIST;
2759 else if (operand == CIL_KEY_CONS_T1) return CIL_CONS_T1;
2760 else if (operand == CIL_KEY_CONS_T2) return CIL_CONS_T2;
2761 else if (operand == CIL_KEY_CONS_T3) return CIL_CONS_T3;
2762 else if (operand == CIL_KEY_CONS_R1) return CIL_CONS_R1;
2763 else if (operand == CIL_KEY_CONS_R2) return CIL_CONS_R2;
2764 else if (operand == CIL_KEY_CONS_R3) return CIL_CONS_R3;
2765 else if (operand == CIL_KEY_CONS_U1) return CIL_CONS_U1;
2766 else if (operand == CIL_KEY_CONS_U2) return CIL_CONS_U2;
2767 else if (operand == CIL_KEY_CONS_U3) return CIL_CONS_U3;
2768 else if (operand == CIL_KEY_CONS_L1) return CIL_CONS_L1;
2769 else if (operand == CIL_KEY_CONS_L2) return CIL_CONS_L2;
2770 else if (operand == CIL_KEY_CONS_H1) return CIL_CONS_H1;
2771 else if (operand == CIL_KEY_CONS_H2) return CIL_CONS_H2;
2772 else return CIL_STRING;
2773 }
2774
2775 static int __cil_fill_constraint_leaf_expr(struct cil_tree_node *current, enum cil_flavor expr_flavor, enum cil_flavor op, struct cil_list **leaf_expr)
2776 {
2777 int rc = SEPOL_ERR;
2778 enum cil_flavor leaf_expr_flavor = CIL_NONE;
2779 enum cil_flavor l_flavor = CIL_NONE;
2780 enum cil_flavor r_flavor = CIL_NONE;
2781
2782 l_flavor = __cil_get_constraint_operand_flavor(current->next->data);
2783 r_flavor = __cil_get_constraint_operand_flavor(current->next->next->data);
2784
2785 switch (l_flavor) {
2786 case CIL_CONS_U1:
2787 case CIL_CONS_U2:
2788 case CIL_CONS_U3:
2789 leaf_expr_flavor = CIL_USER;
2790 break;
2791 case CIL_CONS_R1:
2792 case CIL_CONS_R2:
2793 case CIL_CONS_R3:
2794 leaf_expr_flavor = CIL_ROLE;
2795 break;
2796 case CIL_CONS_T1:
2797 case CIL_CONS_T2:
2798 case CIL_CONS_T3:
2799 leaf_expr_flavor = CIL_TYPE;
2800 break;
2801 case CIL_CONS_L1:
2802 case CIL_CONS_L2:
2803 case CIL_CONS_H1:
2804 case CIL_CONS_H2:
2805 leaf_expr_flavor = CIL_LEVEL;
2806 break;
2807 default:
2808 cil_log(CIL_ERR, "Invalid left operand (%s)\n", (char*)current->next->data);
2809 goto exit;
2810 }
2811
2812 rc = cil_verify_constraint_leaf_expr_syntax(l_flavor, r_flavor, op, expr_flavor);
2813 if (rc != SEPOL_OK) {
2814 goto exit;
2815 }
2816
2817 cil_list_init(leaf_expr, leaf_expr_flavor);
2818
2819 cil_list_append(*leaf_expr, CIL_OP, (void *)op);
2820
2821 cil_list_append(*leaf_expr, CIL_CONS_OPERAND, (void *)l_flavor);
2822
2823 if (r_flavor == CIL_STRING) {
2824 cil_list_append(*leaf_expr, CIL_STRING, current->next->next->data);
2825 } else if (r_flavor == CIL_LIST) {
2826 struct cil_list *sub_list;
2827 rc = cil_fill_list(current->next->next->cl_head, leaf_expr_flavor, &sub_list);
2828 if (rc != SEPOL_OK) {
2829 cil_list_destroy(leaf_expr, CIL_TRUE);
2830 goto exit;
2831 }
2832 cil_list_append(*leaf_expr, CIL_LIST, sub_list);
2833 } else {
2834 cil_list_append(*leaf_expr, CIL_CONS_OPERAND, (void *)r_flavor);
2835 }
2836
2837 return SEPOL_OK;
2838
2839 exit:
2840
2841 return SEPOL_ERR;
2842 }
2843
2844 static int __cil_fill_constraint_expr(struct cil_tree_node *current, enum cil_flavor flavor, struct cil_list **expr)
2845 {
2846 int rc = SEPOL_ERR;
2847 enum cil_flavor op;
2848 struct cil_list *lexpr;
2849 struct cil_list *rexpr;
2850
2851 if (current->data == NULL || current->cl_head != NULL) {
2852 cil_log(CIL_ERR, "Expected a string at the start of the constraint expression\n");
2853 goto exit;
2854 }
2855
2856 op = __cil_get_constraint_operator_flavor(current->data);
2857
2858 rc = cil_verify_constraint_expr_syntax(current, op);
2859 if (rc != SEPOL_OK) {
2860 goto exit;
2861 }
2862
2863 switch (op) {
2864 case CIL_EQ:
2865 case CIL_NEQ:
2866 case CIL_CONS_DOM:
2867 case CIL_CONS_DOMBY:
2868 case CIL_CONS_INCOMP:
2869 rc = __cil_fill_constraint_leaf_expr(current, flavor, op, expr);
2870 if (rc != SEPOL_OK) {
2871 goto exit;
2872 }
2873 break;
2874 case CIL_NOT:
2875 rc = __cil_fill_constraint_expr(current->next->cl_head, flavor, &lexpr);
2876 if (rc != SEPOL_OK) {
2877 goto exit;
2878 }
2879 cil_list_init(expr, flavor);
2880 cil_list_append(*expr, CIL_OP, (void *)op);
2881 cil_list_append(*expr, CIL_LIST, lexpr);
2882 break;
2883 default:
2884 rc = __cil_fill_constraint_expr(current->next->cl_head, flavor, &lexpr);
2885 if (rc != SEPOL_OK) {
2886 goto exit;
2887 }
2888 rc = __cil_fill_constraint_expr(current->next->next->cl_head, flavor, &rexpr);
2889 if (rc != SEPOL_OK) {
2890 cil_list_destroy(&lexpr, CIL_TRUE);
2891 goto exit;
2892 }
2893 cil_list_init(expr, flavor);
2894 cil_list_append(*expr, CIL_OP, (void *)op);
2895 cil_list_append(*expr, CIL_LIST, lexpr);
2896 cil_list_append(*expr, CIL_LIST, rexpr);
2897 break;
2898 }
2899
2900 return SEPOL_OK;
2901 exit:
2902
2903 return rc;
2904 }
2905
2906 static int cil_gen_constraint_expr(struct cil_tree_node *current, enum cil_flavor flavor, struct cil_list **expr)
2907 {
2908 int rc = SEPOL_ERR;
2909
2910 if (current->cl_head == NULL) {
2911 goto exit;
2912 }
2913
2914 rc = __cil_fill_constraint_expr(current->cl_head, flavor, expr);
2915 if (rc != SEPOL_OK) {
2916 goto exit;
2917 }
2918
2919 return SEPOL_OK;
2920
2921 exit:
2922
2923 cil_log(CIL_ERR, "Bad expression tree for constraint\n");
2924 return rc;
2925 }
2926
2927 int cil_gen_boolif(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, int tunableif)
2928 {
2929 enum cil_syntax syntax[] = {
2930 CIL_SYN_STRING,
2931 CIL_SYN_STRING | CIL_SYN_LIST,
2932 CIL_SYN_LIST,
2933 CIL_SYN_LIST | CIL_SYN_END,
2934 CIL_SYN_END
2935 };
2936 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
2937 struct cil_booleanif *bif = NULL;
2938 struct cil_tree_node *next = NULL;
2939 int rc = SEPOL_ERR;
2940
2941 if (db == NULL || parse_current == NULL || ast_node == NULL) {
2942 goto exit;
2943 }
2944
2945 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
2946 if (rc != SEPOL_OK) {
2947 goto exit;
2948 }
2949
2950 cil_boolif_init(&bif);
2951 bif->preserved_tunable = tunableif;
2952
2953 rc = cil_gen_expr(parse_current->next, CIL_BOOL, &bif->str_expr);
2954 if (rc != SEPOL_OK) {
2955 goto exit;
2956 }
2957
2958 rc = cil_verify_conditional_blocks(parse_current->next->next);
2959 if (rc != SEPOL_OK) {
2960 goto exit;
2961 }
2962
2963 /* Destroying expr tree */
2964 next = parse_current->next->next;
2965 cil_tree_subtree_destroy(parse_current->next);
2966 parse_current->next = next;
2967
2968 ast_node->flavor = CIL_BOOLEANIF;
2969 ast_node->data = bif;
2970
2971 return SEPOL_OK;
2972
2973 exit:
2974 if (tunableif) {
2975 cil_tree_log(parse_current, CIL_ERR, "Bad tunableif (treated as a booleanif due to preserve-tunables) declaration");
2976 } else {
2977 cil_tree_log(parse_current, CIL_ERR, "Bad booleanif declaration");
2978 }
2979 cil_destroy_boolif(bif);
2980 return rc;
2981 }
2982
2983 void cil_destroy_boolif(struct cil_booleanif *bif)
2984 {
2985 if (bif == NULL) {
2986 return;
2987 }
2988
2989 cil_list_destroy(&bif->str_expr, CIL_TRUE);
2990 cil_list_destroy(&bif->datum_expr, CIL_FALSE);
2991
2992 free(bif);
2993 }
2994
2995 int cil_gen_tunif(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
2996 {
2997 enum cil_syntax syntax[] = {
2998 CIL_SYN_STRING,
2999 CIL_SYN_STRING | CIL_SYN_LIST,
3000 CIL_SYN_LIST,
3001 CIL_SYN_LIST | CIL_SYN_END,
3002 CIL_SYN_END
3003 };
3004 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3005 struct cil_tunableif *tif = NULL;
3006 struct cil_tree_node *next = NULL;
3007 int rc = SEPOL_ERR;
3008
3009 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3010 goto exit;
3011 }
3012
3013 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3014 if (rc != SEPOL_OK) {
3015 goto exit;
3016 }
3017
3018 cil_tunif_init(&tif);
3019
3020 rc = cil_gen_expr(parse_current->next, CIL_TUNABLE, &tif->str_expr);
3021 if (rc != SEPOL_OK) {
3022 goto exit;
3023 }
3024
3025 rc = cil_verify_conditional_blocks(parse_current->next->next);
3026 if (rc != SEPOL_OK) {
3027 goto exit;
3028 }
3029
3030 /* Destroying expr tree */
3031 next = parse_current->next->next;
3032 cil_tree_subtree_destroy(parse_current->next);
3033 parse_current->next = next;
3034
3035 ast_node->flavor = CIL_TUNABLEIF;
3036 ast_node->data = tif;
3037
3038 return SEPOL_OK;
3039
3040 exit:
3041 cil_tree_log(parse_current, CIL_ERR, "Bad tunableif declaration");
3042 cil_destroy_tunif(tif);
3043 return rc;
3044 }
3045
3046 void cil_destroy_tunif(struct cil_tunableif *tif)
3047 {
3048 if (tif == NULL) {
3049 return;
3050 }
3051
3052 cil_list_destroy(&tif->str_expr, CIL_TRUE);
3053 cil_list_destroy(&tif->datum_expr, CIL_FALSE);
3054
3055 free(tif);
3056 }
3057
3058 int cil_gen_condblock(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor)
3059 {
3060 enum cil_syntax syntax[] = {
3061 CIL_SYN_STRING,
3062 CIL_SYN_N_LISTS,
3063 CIL_SYN_END
3064 };
3065 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3066 int rc = SEPOL_ERR;
3067 struct cil_condblock *cb = NULL;
3068
3069 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3070 goto exit;
3071 }
3072
3073 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3074 if (rc != SEPOL_OK) {
3075 goto exit;
3076 }
3077
3078 if (ast_node->parent->flavor != CIL_BOOLEANIF && ast_node->parent->flavor != CIL_TUNABLEIF) {
3079 rc = SEPOL_ERR;
3080 cil_log(CIL_ERR, "Conditional statements must be a direct child of a tunableif or booleanif statement.\n");
3081 goto exit;
3082 }
3083
3084 ast_node->flavor = CIL_CONDBLOCK;
3085
3086 cil_condblock_init(&cb);
3087 cb->flavor = flavor;
3088
3089 ast_node->data = cb;
3090
3091 return SEPOL_OK;
3092
3093 exit:
3094 cil_tree_log(parse_current, CIL_ERR, "Bad %s condition declaration",
3095 (char*)parse_current->data);
3096 cil_destroy_condblock(cb);
3097 return rc;
3098 }
3099
3100 void cil_destroy_condblock(struct cil_condblock *cb)
3101 {
3102 if (cb == NULL) {
3103 return;
3104 }
3105
3106 cil_symtab_array_destroy(cb->symtab);
3107 free(cb);
3108 }
3109
3110 int cil_gen_alias(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor)
3111 {
3112 enum cil_syntax syntax[] = {
3113 CIL_SYN_STRING,
3114 CIL_SYN_STRING,
3115 CIL_SYN_END
3116 };
3117 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3118 char *key = NULL;
3119 struct cil_alias *alias = NULL;
3120 enum cil_sym_index sym_index;
3121 int rc = SEPOL_ERR;
3122
3123 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3124 goto exit;
3125 }
3126
3127 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3128 if (rc != SEPOL_OK) {
3129 goto exit;
3130 }
3131
3132 cil_alias_init(&alias);
3133
3134 key = parse_current->next->data;
3135
3136 rc = cil_flavor_to_symtab_index(flavor, &sym_index);
3137 if (rc != SEPOL_OK) {
3138 goto exit;
3139 }
3140
3141 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)alias, (hashtab_key_t)key, sym_index, flavor);
3142 if (rc != SEPOL_OK) {
3143 goto exit;
3144 }
3145
3146 return SEPOL_OK;
3147
3148 exit:
3149 cil_tree_log(parse_current, CIL_ERR, "Bad %s declaration", (char*)parse_current->data);
3150 cil_destroy_alias(alias);
3151 cil_clear_node(ast_node);
3152 return rc;
3153 }
3154
3155 void cil_destroy_alias(struct cil_alias *alias)
3156 {
3157 if (alias == NULL) {
3158 return;
3159 }
3160
3161 cil_symtab_datum_destroy(&alias->datum);
3162 alias->actual = NULL;
3163
3164 free(alias);
3165 }
3166
3167 int cil_gen_aliasactual(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor)
3168 {
3169 int rc = SEPOL_ERR;
3170 enum cil_syntax syntax[] = {
3171 CIL_SYN_STRING,
3172 CIL_SYN_STRING,
3173 CIL_SYN_STRING,
3174 CIL_SYN_END
3175 };
3176 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3177 struct cil_aliasactual *aliasactual = NULL;
3178
3179 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3180 goto exit;
3181 }
3182
3183 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3184 if (rc != SEPOL_OK) {
3185 goto exit;
3186 }
3187
3188 cil_aliasactual_init(&aliasactual);
3189
3190 aliasactual->alias_str = parse_current->next->data;
3191
3192 aliasactual->actual_str = parse_current->next->next->data;
3193
3194 ast_node->data = aliasactual;
3195 ast_node->flavor = flavor;
3196
3197 return SEPOL_OK;
3198
3199 exit:
3200 cil_tree_log(parse_current, CIL_ERR, "Bad %s association", cil_node_to_string(parse_current));
3201 cil_clear_node(ast_node);
3202 return rc;
3203 }
3204
3205 void cil_destroy_aliasactual(struct cil_aliasactual *aliasactual)
3206 {
3207 if (aliasactual == NULL) {
3208 return;
3209 }
3210
3211 free(aliasactual);
3212 }
3213
3214 int cil_gen_typeattributeset(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3215 {
3216 enum cil_syntax syntax[] = {
3217 CIL_SYN_STRING,
3218 CIL_SYN_STRING,
3219 CIL_SYN_STRING | CIL_SYN_LIST,
3220 CIL_SYN_END
3221 };
3222 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3223 struct cil_typeattributeset *attrset = NULL;
3224 int rc = SEPOL_ERR;
3225
3226 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3227 goto exit;
3228 }
3229
3230 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3231 if (rc != SEPOL_OK) {
3232 goto exit;
3233 }
3234
3235 cil_typeattributeset_init(&attrset);
3236
3237 attrset->attr_str = parse_current->next->data;
3238
3239 rc = cil_gen_expr(parse_current->next->next, CIL_TYPE, &attrset->str_expr);
3240 if (rc != SEPOL_OK) {
3241 goto exit;
3242 }
3243 ast_node->data = attrset;
3244 ast_node->flavor = CIL_TYPEATTRIBUTESET;
3245
3246 return SEPOL_OK;
3247
3248 exit:
3249 cil_tree_log(parse_current, CIL_ERR, "Bad typeattributeset statement");
3250 cil_destroy_typeattributeset(attrset);
3251 return rc;
3252 }
3253
3254 void cil_destroy_typeattributeset(struct cil_typeattributeset *attrset)
3255 {
3256 if (attrset == NULL) {
3257 return;
3258 }
3259
3260 cil_list_destroy(&attrset->str_expr, CIL_TRUE);
3261 cil_list_destroy(&attrset->datum_expr, CIL_FALSE);
3262
3263 free(attrset);
3264 }
3265
3266 int cil_gen_expandtypeattribute(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3267 {
3268 enum cil_syntax syntax[] = {
3269 CIL_SYN_STRING,
3270 CIL_SYN_STRING | CIL_SYN_LIST,
3271 CIL_SYN_STRING,
3272 CIL_SYN_END
3273 };
3274 char *expand_str;
3275 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3276 struct cil_expandtypeattribute *expandattr = NULL;
3277 int rc = SEPOL_ERR;
3278
3279 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3280 goto exit;
3281 }
3282
3283 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3284 if (rc != SEPOL_OK) {
3285 goto exit;
3286 }
3287
3288 cil_expandtypeattribute_init(&expandattr);
3289
3290 if (parse_current->next->cl_head == NULL) {
3291 cil_list_init(&expandattr->attr_strs, CIL_TYPE);
3292 cil_list_append(expandattr->attr_strs, CIL_STRING, parse_current->next->data);
3293 } else {
3294 rc = cil_fill_list(parse_current->next->cl_head, CIL_TYPE, &expandattr->attr_strs);
3295 if (rc != SEPOL_OK) {
3296 goto exit;
3297 }
3298 }
3299
3300 expand_str = parse_current->next->next->data;
3301
3302 if (expand_str == CIL_KEY_CONDTRUE) {
3303 expandattr->expand = CIL_TRUE;
3304 } else if (expand_str == CIL_KEY_CONDFALSE) {
3305 expandattr->expand = CIL_FALSE;
3306 } else {
3307 cil_log(CIL_ERR, "Value must be either \'true\' or \'false\'");
3308 rc = SEPOL_ERR;
3309 goto exit;
3310 }
3311
3312 ast_node->data = expandattr;
3313 ast_node->flavor = CIL_EXPANDTYPEATTRIBUTE;
3314
3315 return SEPOL_OK;
3316
3317 exit:
3318 cil_tree_log(parse_current, CIL_ERR, "Bad expandtypeattribute statement");
3319 cil_destroy_expandtypeattribute(expandattr);
3320 return rc;
3321 }
3322
3323 void cil_destroy_expandtypeattribute(struct cil_expandtypeattribute *expandattr)
3324 {
3325 if (expandattr == NULL) {
3326 return;
3327 }
3328
3329 cil_list_destroy(&expandattr->attr_strs, CIL_TRUE);
3330
3331 cil_list_destroy(&expandattr->attr_datums, CIL_FALSE);
3332
3333 free(expandattr);
3334 }
3335
3336 int cil_gen_typepermissive(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3337 {
3338 enum cil_syntax syntax[] = {
3339 CIL_SYN_STRING,
3340 CIL_SYN_STRING,
3341 CIL_SYN_END
3342 };
3343 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3344 struct cil_typepermissive *typeperm = NULL;
3345 int rc = SEPOL_ERR;
3346
3347 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3348 goto exit;
3349 }
3350
3351 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3352 if (rc != SEPOL_OK) {
3353 goto exit;
3354 }
3355
3356 cil_typepermissive_init(&typeperm);
3357
3358 typeperm->type_str = parse_current->next->data;
3359
3360 ast_node->data = typeperm;
3361 ast_node->flavor = CIL_TYPEPERMISSIVE;
3362
3363 return SEPOL_OK;
3364
3365 exit:
3366 cil_tree_log(parse_current, CIL_ERR, "Bad typepermissive declaration");
3367 cil_destroy_typepermissive(typeperm);
3368 return rc;
3369 }
3370
3371 void cil_destroy_typepermissive(struct cil_typepermissive *typeperm)
3372 {
3373 if (typeperm == NULL) {
3374 return;
3375 }
3376
3377 free(typeperm);
3378 }
3379
3380 int cil_gen_typeneveraudit(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3381 {
3382 enum cil_syntax syntax[] = {
3383 CIL_SYN_STRING,
3384 CIL_SYN_STRING,
3385 CIL_SYN_END
3386 };
3387 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3388 struct cil_typeneveraudit *typeperm = NULL;
3389 int rc = SEPOL_ERR;
3390
3391 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3392 goto exit;
3393 }
3394
3395 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3396 if (rc != SEPOL_OK) {
3397 goto exit;
3398 }
3399
3400 cil_typeneveraudit_init(&typeperm);
3401
3402 typeperm->type_str = parse_current->next->data;
3403
3404 ast_node->data = typeperm;
3405 ast_node->flavor = CIL_TYPENEVERAUDIT;
3406
3407 return SEPOL_OK;
3408
3409 exit:
3410 cil_tree_log(parse_current, CIL_ERR, "Bad typeneveraudit declaration");
3411 cil_destroy_typeneveraudit(typeperm);
3412 return rc;
3413 }
3414
3415 void cil_destroy_typeneveraudit(struct cil_typeneveraudit *typeperm)
3416 {
3417 if (typeperm == NULL) {
3418 return;
3419 }
3420
3421 free(typeperm);
3422 }
3423
3424 int cil_gen_typetransition(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3425 {
3426 int rc = SEPOL_ERR;
3427 enum cil_syntax syntax[] = {
3428 CIL_SYN_STRING,
3429 CIL_SYN_STRING,
3430 CIL_SYN_STRING,
3431 CIL_SYN_STRING,
3432 CIL_SYN_STRING,
3433 CIL_SYN_STRING | CIL_SYN_END,
3434 CIL_SYN_END
3435 };
3436 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3437 char *s1, *s2, *s3, *s4, *s5;
3438
3439 if (db == NULL || parse_current == NULL || ast_node == NULL ) {
3440 goto exit;
3441 }
3442
3443 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3444 if (rc != SEPOL_OK) {
3445 goto exit;
3446 }
3447
3448 s1 = parse_current->next->data;
3449 s2 = parse_current->next->next->data;
3450 s3 = parse_current->next->next->next->data;
3451 s4 = parse_current->next->next->next->next->data;
3452 s5 = NULL;
3453
3454 if (parse_current->next->next->next->next->next) {
3455 if (s4 == CIL_KEY_STAR) {
3456 s4 = parse_current->next->next->next->next->next->data;
3457 } else {
3458 s5 = parse_current->next->next->next->next->next->data;
3459 }
3460 }
3461
3462 if (s5) {
3463 struct cil_nametypetransition *nametypetrans = NULL;
3464 cil_nametypetransition_init(&nametypetrans);
3465
3466 ast_node->data = nametypetrans;
3467 ast_node->flavor = CIL_NAMETYPETRANSITION;
3468
3469 nametypetrans->src_str = s1;
3470 nametypetrans->tgt_str = s2;
3471 nametypetrans->obj_str = s3;
3472 nametypetrans->name_str = s4;
3473 nametypetrans->name = cil_gen_declared_string(db, s4, ast_node);
3474 nametypetrans->result_str = s5;
3475 } else {
3476 struct cil_type_rule *rule = NULL;
3477 cil_type_rule_init(&rule);
3478
3479 ast_node->data = rule;
3480 ast_node->flavor = CIL_TYPE_RULE;
3481
3482 rule->rule_kind = CIL_TYPE_TRANSITION;
3483 rule->src_str = s1;
3484 rule->tgt_str = s2;
3485 rule->obj_str = s3;
3486 rule->result_str = s4;
3487 }
3488
3489 return SEPOL_OK;
3490
3491 exit:
3492 cil_tree_log(parse_current, CIL_ERR, "Bad typetransition declaration");
3493 return rc;
3494 }
3495
3496 void cil_destroy_typetransition(struct cil_nametypetransition *nametypetrans)
3497 {
3498 if (nametypetrans == NULL) {
3499 return;
3500 }
3501
3502 free(nametypetrans);
3503 }
3504
3505 int cil_gen_rangetransition(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3506 {
3507 enum cil_syntax syntax[] = {
3508 CIL_SYN_STRING,
3509 CIL_SYN_STRING,
3510 CIL_SYN_STRING,
3511 CIL_SYN_STRING,
3512 CIL_SYN_STRING | CIL_SYN_LIST,
3513 CIL_SYN_END
3514 };
3515 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3516 struct cil_rangetransition *rangetrans = NULL;
3517 int rc = SEPOL_ERR;
3518
3519 if (db == NULL || parse_current == NULL || ast_node == NULL ) {
3520 goto exit;
3521 }
3522
3523 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3524 if (rc != SEPOL_OK) {
3525 goto exit;
3526 }
3527
3528 cil_rangetransition_init(&rangetrans);
3529
3530 rangetrans->src_str = parse_current->next->data;
3531 rangetrans->exec_str = parse_current->next->next->data;
3532 rangetrans->obj_str = parse_current->next->next->next->data;
3533
3534 rangetrans->range_str = NULL;
3535
3536 if (parse_current->next->next->next->next->cl_head == NULL) {
3537 rangetrans->range_str = parse_current->next->next->next->next->data;
3538 } else {
3539 cil_levelrange_init(&rangetrans->range);
3540
3541 rc = cil_fill_levelrange(parse_current->next->next->next->next->cl_head, rangetrans->range);
3542 if (rc != SEPOL_OK) {
3543 goto exit;
3544 }
3545 }
3546
3547 ast_node->data = rangetrans;
3548 ast_node->flavor = CIL_RANGETRANSITION;
3549
3550 return SEPOL_OK;
3551
3552 exit:
3553 cil_tree_log(parse_current, CIL_ERR, "Bad rangetransition declaration");
3554 cil_destroy_rangetransition(rangetrans);
3555 return rc;
3556 }
3557
3558 void cil_destroy_rangetransition(struct cil_rangetransition *rangetrans)
3559 {
3560 if (rangetrans == NULL) {
3561 return;
3562 }
3563
3564 if (rangetrans->range_str == NULL && rangetrans->range != NULL) {
3565 cil_destroy_levelrange(rangetrans->range);
3566 }
3567
3568 free(rangetrans);
3569 }
3570
3571 int cil_gen_sensitivity(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3572 {
3573 enum cil_syntax syntax[] = {
3574 CIL_SYN_STRING,
3575 CIL_SYN_STRING,
3576 CIL_SYN_END
3577 };
3578 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3579 char *key = NULL;
3580 struct cil_sens *sens = NULL;
3581 int rc = SEPOL_ERR;
3582
3583 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3584 goto exit;
3585 }
3586
3587 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3588 if (rc != SEPOL_OK) {
3589 goto exit;
3590 }
3591
3592 cil_sens_init(&sens);
3593
3594 key = parse_current->next->data;
3595
3596 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)sens, (hashtab_key_t)key, CIL_SYM_SENS, CIL_SENS);
3597 if (rc != SEPOL_OK) {
3598 goto exit;
3599 }
3600
3601 return SEPOL_OK;
3602
3603 exit:
3604 cil_tree_log(parse_current, CIL_ERR, "Bad sensitivity declaration");
3605 cil_destroy_sensitivity(sens);
3606 cil_clear_node(ast_node);
3607 return rc;
3608 }
3609
3610 void cil_destroy_sensitivity(struct cil_sens *sens)
3611 {
3612 if (sens == NULL) {
3613 return;
3614 }
3615
3616 cil_symtab_datum_destroy(&sens->datum);
3617
3618 cil_list_destroy(&sens->cats_list, CIL_FALSE);
3619
3620 free(sens);
3621 }
3622
3623 int cil_gen_category(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3624 {
3625 enum cil_syntax syntax[] = {
3626 CIL_SYN_STRING,
3627 CIL_SYN_STRING,
3628 CIL_SYN_END
3629 };
3630 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3631 char *key = NULL;
3632 struct cil_cat *cat = NULL;
3633 int rc = SEPOL_ERR;
3634
3635 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3636 goto exit;
3637 }
3638
3639 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3640 if (rc != SEPOL_OK) {
3641 goto exit;
3642 }
3643
3644 cil_cat_init(&cat);
3645
3646 key = parse_current->next->data;
3647
3648 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)cat, (hashtab_key_t)key, CIL_SYM_CATS, CIL_CAT);
3649 if (rc != SEPOL_OK) {
3650 goto exit;
3651 }
3652
3653 return SEPOL_OK;
3654
3655 exit:
3656 cil_tree_log(parse_current, CIL_ERR, "Bad category declaration");
3657 cil_destroy_category(cat);
3658 cil_clear_node(ast_node);
3659 return rc;
3660 }
3661
3662 void cil_destroy_category(struct cil_cat *cat)
3663 {
3664 if (cat == NULL) {
3665 return;
3666 }
3667
3668 cil_symtab_datum_destroy(&cat->datum);
3669 free(cat);
3670 }
3671
3672 static int cil_gen_catset(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3673 {
3674 enum cil_syntax syntax[] = {
3675 CIL_SYN_STRING,
3676 CIL_SYN_STRING,
3677 CIL_SYN_LIST,
3678 CIL_SYN_END
3679 };
3680 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3681 char *key = NULL;
3682 struct cil_catset *catset = NULL;
3683 int rc = SEPOL_ERR;
3684
3685 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3686 goto exit;
3687 }
3688
3689 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3690 if (rc != SEPOL_OK) {
3691 goto exit;
3692 }
3693
3694 cil_catset_init(&catset);
3695
3696 key = parse_current->next->data;
3697
3698 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)catset, (hashtab_key_t)key, CIL_SYM_CATS, CIL_CATSET);
3699 if (rc != SEPOL_OK) {
3700 goto exit;
3701 }
3702
3703 rc = cil_fill_cats(parse_current->next->next, &catset->cats);
3704 if (rc != SEPOL_OK) {
3705 goto exit;
3706 }
3707
3708 return SEPOL_OK;
3709
3710 exit:
3711 cil_tree_log(parse_current, CIL_ERR, "Bad categoryset declaration");
3712 cil_destroy_catset(catset);
3713 cil_clear_node(ast_node);
3714 return rc;
3715 }
3716
3717 void cil_destroy_catset(struct cil_catset *catset)
3718 {
3719 if (catset == NULL) {
3720 return;
3721 }
3722
3723 cil_symtab_datum_destroy(&catset->datum);
3724
3725 cil_destroy_cats(catset->cats);
3726
3727 free(catset);
3728 }
3729
3730 int cil_gen_senscat(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3731 {
3732 enum cil_syntax syntax[] = {
3733 CIL_SYN_STRING,
3734 CIL_SYN_STRING,
3735 CIL_SYN_STRING | CIL_SYN_LIST,
3736 CIL_SYN_END
3737 };
3738 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3739 struct cil_senscat *senscat = NULL;
3740 int rc = SEPOL_ERR;
3741
3742 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3743 goto exit;
3744 }
3745
3746 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3747 if (rc != SEPOL_OK) {
3748 goto exit;
3749 }
3750
3751 cil_senscat_init(&senscat);
3752
3753 senscat->sens_str = parse_current->next->data;
3754
3755 rc = cil_fill_cats(parse_current->next->next, &senscat->cats);
3756 if (rc != SEPOL_OK) {
3757 goto exit;
3758 }
3759
3760 ast_node->data = senscat;
3761 ast_node->flavor = CIL_SENSCAT;
3762
3763 return SEPOL_OK;
3764
3765 exit:
3766 cil_tree_log(parse_current, CIL_ERR, "Bad sensitivitycategory declaration");
3767 cil_destroy_senscat(senscat);
3768 return rc;
3769 }
3770
3771 void cil_destroy_senscat(struct cil_senscat *senscat)
3772 {
3773 if (senscat == NULL) {
3774 return;
3775 }
3776
3777 cil_destroy_cats(senscat->cats);
3778
3779 free(senscat);
3780 }
3781
3782 int cil_gen_level(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3783 {
3784 enum cil_syntax syntax[] = {
3785 CIL_SYN_STRING,
3786 CIL_SYN_STRING,
3787 CIL_SYN_LIST,
3788 CIL_SYN_END
3789 };
3790 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3791 char *key = NULL;
3792 struct cil_level *level = NULL;
3793 int rc = SEPOL_ERR;
3794
3795 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3796 goto exit;
3797 }
3798
3799 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3800 if (rc != SEPOL_OK) {
3801 goto exit;
3802 }
3803
3804 cil_level_init(&level);
3805
3806 key = parse_current->next->data;
3807
3808 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)level, (hashtab_key_t)key, CIL_SYM_LEVELS, CIL_LEVEL);
3809 if (rc != SEPOL_OK) {
3810 goto exit;
3811 }
3812
3813 rc = cil_fill_level(parse_current->next->next->cl_head, level);
3814 if (rc != SEPOL_OK) {
3815 goto exit;
3816 }
3817
3818 return SEPOL_OK;
3819
3820 exit:
3821 cil_tree_log(parse_current, CIL_ERR, "Bad level declaration");
3822 cil_destroy_level(level);
3823 cil_clear_node(ast_node);
3824 return rc;
3825 }
3826
3827 void cil_destroy_level(struct cil_level *level)
3828 {
3829 if (level == NULL) {
3830 return;
3831 }
3832
3833 cil_symtab_datum_destroy(&level->datum);
3834
3835 cil_destroy_cats(level->cats);
3836
3837 free(level);
3838 }
3839
3840 /* low should be pointing to either the name of the low level or to an open paren for an anonymous low level */
3841 int cil_fill_levelrange(struct cil_tree_node *low, struct cil_levelrange *lvlrange)
3842 {
3843 enum cil_syntax syntax[] = {
3844 CIL_SYN_STRING | CIL_SYN_LIST,
3845 CIL_SYN_STRING | CIL_SYN_LIST,
3846 CIL_SYN_END
3847 };
3848 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3849 int rc = SEPOL_ERR;
3850
3851 if (low == NULL || lvlrange == NULL) {
3852 goto exit;
3853 }
3854
3855 rc = __cil_verify_syntax(low, syntax, syntax_len);
3856 if (rc != SEPOL_OK) {
3857
3858 goto exit;
3859 }
3860
3861 if (low->cl_head == NULL) {
3862 lvlrange->low_str = low->data;
3863 } else {
3864 cil_level_init(&lvlrange->low);
3865 rc = cil_fill_level(low->cl_head, lvlrange->low);
3866 if (rc != SEPOL_OK) {
3867 goto exit;
3868 }
3869 }
3870
3871 if (low->next->cl_head == NULL) {
3872 lvlrange->high_str = low->next->data;
3873 } else {
3874 cil_level_init(&lvlrange->high);
3875 rc = cil_fill_level(low->next->cl_head, lvlrange->high);
3876 if (rc != SEPOL_OK) {
3877 goto exit;
3878 }
3879 }
3880
3881 return SEPOL_OK;
3882
3883 exit:
3884 cil_log(CIL_ERR, "Bad levelrange\n");
3885 return rc;
3886 }
3887
3888 int cil_gen_levelrange(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
3889 {
3890 enum cil_syntax syntax[] = {
3891 CIL_SYN_STRING,
3892 CIL_SYN_STRING,
3893 CIL_SYN_LIST,
3894 CIL_SYN_END
3895 };
3896 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3897 char *key = NULL;
3898 struct cil_levelrange *lvlrange = NULL;
3899 int rc = SEPOL_ERR;
3900
3901 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3902 goto exit;
3903 }
3904
3905 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3906 if (rc != SEPOL_OK) {
3907 goto exit;
3908 }
3909
3910 cil_levelrange_init(&lvlrange);
3911
3912 key = parse_current->next->data;
3913
3914 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)lvlrange, (hashtab_key_t)key, CIL_SYM_LEVELRANGES, CIL_LEVELRANGE);
3915 if (rc != SEPOL_OK) {
3916 goto exit;
3917 }
3918
3919 rc = cil_fill_levelrange(parse_current->next->next->cl_head, lvlrange);
3920 if (rc != SEPOL_OK) {
3921 goto exit;
3922 }
3923
3924 return SEPOL_OK;
3925
3926 exit:
3927 cil_tree_log(parse_current, CIL_ERR, "Bad levelrange declaration");
3928 cil_destroy_levelrange(lvlrange);
3929 cil_clear_node(ast_node);
3930 return rc;
3931 }
3932
3933 void cil_destroy_levelrange(struct cil_levelrange *lvlrange)
3934 {
3935 if (lvlrange == NULL) {
3936 return;
3937 }
3938
3939 cil_symtab_datum_destroy(&lvlrange->datum);
3940
3941 if (lvlrange->low_str == NULL) {
3942 cil_destroy_level(lvlrange->low);
3943 }
3944
3945 if (lvlrange->high_str == NULL) {
3946 cil_destroy_level(lvlrange->high);
3947 }
3948
3949 free(lvlrange);
3950 }
3951
3952 int cil_gen_constrain(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor)
3953 {
3954 enum cil_syntax syntax[] = {
3955 CIL_SYN_STRING,
3956 CIL_SYN_STRING | CIL_SYN_LIST,
3957 CIL_SYN_LIST,
3958 CIL_SYN_END
3959 };
3960 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
3961 struct cil_constrain *cons = NULL;
3962 int rc = SEPOL_ERR;
3963
3964 if (db == NULL || parse_current == NULL || ast_node == NULL) {
3965 goto exit;
3966 }
3967
3968 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
3969 if (rc != SEPOL_OK) {
3970 goto exit;
3971 }
3972
3973 cil_constrain_init(&cons);
3974
3975 rc = cil_fill_classperms_list(parse_current->next, &cons->classperms);
3976 if (rc != SEPOL_OK) {
3977 goto exit;
3978 }
3979
3980 rc = cil_gen_constraint_expr(parse_current->next->next, flavor, &cons->str_expr);
3981 if (rc != SEPOL_OK) {
3982 goto exit;
3983 }
3984
3985 ast_node->data = cons;
3986 ast_node->flavor = flavor;
3987
3988 return SEPOL_OK;
3989
3990 exit:
3991 cil_tree_log(parse_current, CIL_ERR, "Bad constrain declaration");
3992 cil_destroy_constrain(cons);
3993 return rc;
3994 }
3995
3996 void cil_destroy_constrain(struct cil_constrain *cons)
3997 {
3998 if (cons == NULL) {
3999 return;
4000 }
4001
4002 cil_destroy_classperms_list(&cons->classperms);
4003 cil_list_destroy(&cons->str_expr, CIL_TRUE);
4004 cil_list_destroy(&cons->datum_expr, CIL_FALSE);
4005
4006 free(cons);
4007 }
4008
4009 int cil_gen_validatetrans(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor)
4010 {
4011 enum cil_syntax syntax[] = {
4012 CIL_SYN_STRING,
4013 CIL_SYN_STRING,
4014 CIL_SYN_LIST,
4015 CIL_SYN_END
4016 };
4017 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4018 struct cil_validatetrans *validtrans = NULL;
4019 int rc = SEPOL_ERR;
4020
4021 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4022 goto exit;
4023 }
4024
4025 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4026 if (rc != SEPOL_OK) {
4027 goto exit;
4028 }
4029
4030 cil_validatetrans_init(&validtrans);
4031
4032 validtrans->class_str = parse_current->next->data;
4033
4034 rc = cil_gen_constraint_expr(parse_current->next->next, flavor, &validtrans->str_expr);
4035 if (rc != SEPOL_OK) {
4036 goto exit;
4037 }
4038
4039 ast_node->data = validtrans;
4040 ast_node->flavor = flavor;
4041
4042 return SEPOL_OK;
4043
4044 exit:
4045 cil_tree_log(parse_current, CIL_ERR, "Bad validatetrans declaration");
4046 cil_destroy_validatetrans(validtrans);
4047 return rc;
4048
4049
4050 }
4051
4052 void cil_destroy_validatetrans(struct cil_validatetrans *validtrans)
4053 {
4054 if (validtrans == NULL) {
4055 return;
4056 }
4057
4058 cil_list_destroy(&validtrans->str_expr, CIL_TRUE);
4059 cil_list_destroy(&validtrans->datum_expr, CIL_FALSE);
4060
4061 free(validtrans);
4062 }
4063
4064 /* Fills in context starting from user */
4065 int cil_fill_context(struct cil_tree_node *user_node, struct cil_context *context)
4066 {
4067 enum cil_syntax syntax[] = {
4068 CIL_SYN_STRING,
4069 CIL_SYN_STRING,
4070 CIL_SYN_STRING,
4071 CIL_SYN_STRING | CIL_SYN_LIST,
4072 CIL_SYN_END
4073 };
4074 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4075 int rc = SEPOL_ERR;
4076
4077 if (user_node == NULL || context == NULL) {
4078 goto exit;
4079 }
4080
4081 rc = __cil_verify_syntax(user_node, syntax, syntax_len);
4082 if (rc != SEPOL_OK) {
4083 goto exit;
4084 }
4085
4086 context->user_str = user_node->data;
4087 context->role_str = user_node->next->data;
4088 context->type_str = user_node->next->next->data;
4089
4090 context->range_str = NULL;
4091
4092 if (user_node->next->next->next->cl_head == NULL) {
4093 context->range_str = user_node->next->next->next->data;
4094 } else {
4095 cil_levelrange_init(&context->range);
4096
4097 rc = cil_fill_levelrange(user_node->next->next->next->cl_head, context->range);
4098 if (rc != SEPOL_OK) {
4099 goto exit;
4100 }
4101 }
4102
4103 return SEPOL_OK;
4104
4105 exit:
4106 cil_log(CIL_ERR, "Bad context\n");
4107 return rc;
4108 }
4109
4110 int cil_gen_context(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4111 {
4112 enum cil_syntax syntax[] = {
4113 CIL_SYN_STRING,
4114 CIL_SYN_STRING,
4115 CIL_SYN_LIST,
4116 CIL_SYN_END
4117 };
4118 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4119 char *key = NULL;
4120 struct cil_context *context = NULL;
4121 int rc = SEPOL_ERR;
4122
4123 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4124 goto exit;
4125 }
4126
4127 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4128 if (rc != SEPOL_OK) {
4129 goto exit;
4130 }
4131
4132 cil_context_init(&context);
4133
4134 key = parse_current->next->data;
4135
4136 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)context, (hashtab_key_t)key, CIL_SYM_CONTEXTS, CIL_CONTEXT);
4137 if (rc != SEPOL_OK) {
4138 goto exit;
4139 }
4140
4141 rc = cil_fill_context(parse_current->next->next->cl_head, context);
4142 if (rc != SEPOL_OK) {
4143 goto exit;
4144 }
4145
4146 return SEPOL_OK;
4147
4148 exit:
4149 cil_tree_log(parse_current, CIL_ERR, "Bad context declaration");
4150 cil_destroy_context(context);
4151 cil_clear_node(ast_node);
4152 return SEPOL_ERR;
4153 }
4154
4155 void cil_destroy_context(struct cil_context *context)
4156 {
4157 if (context == NULL) {
4158 return;
4159 }
4160
4161 cil_symtab_datum_destroy(&context->datum);
4162
4163 if (context->range_str == NULL && context->range != NULL) {
4164 cil_destroy_levelrange(context->range);
4165 }
4166
4167 free(context);
4168 }
4169
4170 int cil_gen_filecon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4171 {
4172 enum cil_syntax syntax[] = {
4173 CIL_SYN_STRING,
4174 CIL_SYN_STRING,
4175 CIL_SYN_STRING,
4176 CIL_SYN_STRING | CIL_SYN_LIST | CIL_SYN_EMPTY_LIST,
4177 CIL_SYN_END
4178 };
4179 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4180 int rc = SEPOL_ERR;
4181 struct cil_filecon *filecon = NULL;
4182 char *type = NULL;
4183
4184 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4185 goto exit;
4186 }
4187
4188 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4189 if (rc != SEPOL_OK) {
4190 goto exit;
4191 }
4192
4193 type = parse_current->next->next->data;
4194 cil_filecon_init(&filecon);
4195
4196 ast_node->data = filecon;
4197 ast_node->flavor = CIL_FILECON;
4198
4199 filecon->path_str = parse_current->next->data;
4200 /* filecon->path will be NULL if in a macro and the path is an argument */
4201 filecon->path = cil_gen_declared_string(db, filecon->path_str, ast_node);
4202
4203 if (type == CIL_KEY_ANY) {
4204 filecon->type = CIL_FILECON_ANY;
4205 } else if (type == CIL_KEY_FILE) {
4206 filecon->type = CIL_FILECON_FILE;
4207 } else if (type == CIL_KEY_DIR) {
4208 filecon->type = CIL_FILECON_DIR;
4209 } else if (type == CIL_KEY_CHAR) {
4210 filecon->type = CIL_FILECON_CHAR;
4211 } else if (type == CIL_KEY_BLOCK) {
4212 filecon->type = CIL_FILECON_BLOCK;
4213 } else if (type == CIL_KEY_SOCKET) {
4214 filecon->type = CIL_FILECON_SOCKET;
4215 } else if (type == CIL_KEY_PIPE) {
4216 filecon->type = CIL_FILECON_PIPE;
4217 } else if (type == CIL_KEY_SYMLINK) {
4218 filecon->type = CIL_FILECON_SYMLINK;
4219 } else {
4220 cil_log(CIL_ERR, "Invalid file type\n");
4221 rc = SEPOL_ERR;
4222 goto exit;
4223 }
4224
4225 if (parse_current->next->next->next->cl_head == NULL) {
4226 filecon->context_str = parse_current->next->next->next->data;
4227 } else {
4228 if (parse_current->next->next->next->cl_head->next == NULL) {
4229 filecon->context = NULL;
4230 } else {
4231 cil_context_init(&filecon->context);
4232
4233 rc = cil_fill_context(parse_current->next->next->next->cl_head, filecon->context);
4234 if (rc != SEPOL_OK) {
4235 goto exit;
4236 }
4237 }
4238 }
4239
4240 return SEPOL_OK;
4241
4242 exit:
4243 cil_tree_log(parse_current, CIL_ERR, "Bad filecon declaration");
4244 cil_destroy_filecon(filecon);
4245 cil_clear_node(ast_node);
4246 return rc;
4247 }
4248
4249 //TODO: Should we be checking if the pointer is NULL when passed in?
4250 void cil_destroy_filecon(struct cil_filecon *filecon)
4251 {
4252 if (filecon == NULL) {
4253 return;
4254 }
4255
4256 if (filecon->context_str == NULL && filecon->context != NULL) {
4257 cil_destroy_context(filecon->context);
4258 }
4259
4260 free(filecon);
4261 }
4262
4263 int cil_gen_ibpkeycon(__attribute__((unused)) struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4264 {
4265 enum cil_syntax syntax[] = {
4266 CIL_SYN_STRING,
4267 CIL_SYN_STRING,
4268 CIL_SYN_STRING | CIL_SYN_LIST,
4269 CIL_SYN_STRING | CIL_SYN_LIST,
4270 CIL_SYN_END
4271 };
4272 size_t syntax_len = sizeof(syntax) / sizeof(*syntax);
4273 int rc = SEPOL_ERR;
4274 struct cil_ibpkeycon *ibpkeycon = NULL;
4275
4276 if (!parse_current || !ast_node)
4277 goto exit;
4278
4279 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4280 if (rc != SEPOL_OK)
4281 goto exit;
4282
4283 cil_ibpkeycon_init(&ibpkeycon);
4284
4285 ibpkeycon->subnet_prefix_str = parse_current->next->data;
4286
4287 if (parse_current->next->next->cl_head) {
4288 if (parse_current->next->next->cl_head->next &&
4289 !parse_current->next->next->cl_head->next->next) {
4290 rc = cil_fill_integer(parse_current->next->next->cl_head, &ibpkeycon->pkey_low, 0);
4291 if (rc != SEPOL_OK) {
4292 cil_log(CIL_ERR, "Improper ibpkey specified\n");
4293 goto exit;
4294 }
4295 rc = cil_fill_integer(parse_current->next->next->cl_head->next, &ibpkeycon->pkey_high, 0);
4296 if (rc != SEPOL_OK) {
4297 cil_log(CIL_ERR, "Improper ibpkey specified\n");
4298 goto exit;
4299 }
4300 } else {
4301 cil_log(CIL_ERR, "Improper ibpkey range specified\n");
4302 rc = SEPOL_ERR;
4303 goto exit;
4304 }
4305 } else {
4306 rc = cil_fill_integer(parse_current->next->next, &ibpkeycon->pkey_low, 0);
4307 if (rc != SEPOL_OK) {
4308 cil_log(CIL_ERR, "Improper ibpkey specified\n");
4309 goto exit;
4310 }
4311 ibpkeycon->pkey_high = ibpkeycon->pkey_low;
4312 }
4313
4314 if (!parse_current->next->next->next->cl_head) {
4315 ibpkeycon->context_str = parse_current->next->next->next->data;
4316 } else {
4317 cil_context_init(&ibpkeycon->context);
4318
4319 rc = cil_fill_context(parse_current->next->next->next->cl_head, ibpkeycon->context);
4320 if (rc != SEPOL_OK)
4321 goto exit;
4322 }
4323
4324 ast_node->data = ibpkeycon;
4325 ast_node->flavor = CIL_IBPKEYCON;
4326 return SEPOL_OK;
4327
4328 exit:
4329 cil_tree_log(parse_current, CIL_ERR, "Bad ibpkeycon declaration");
4330 cil_destroy_ibpkeycon(ibpkeycon);
4331
4332 return rc;
4333 }
4334
4335 void cil_destroy_ibpkeycon(struct cil_ibpkeycon *ibpkeycon)
4336 {
4337 if (!ibpkeycon)
4338 return;
4339
4340 if (!ibpkeycon->context_str && ibpkeycon->context)
4341 cil_destroy_context(ibpkeycon->context);
4342
4343 free(ibpkeycon);
4344 }
4345
4346 int cil_gen_portcon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4347 {
4348 enum cil_syntax syntax[] = {
4349 CIL_SYN_STRING,
4350 CIL_SYN_STRING,
4351 CIL_SYN_STRING | CIL_SYN_LIST,
4352 CIL_SYN_STRING | CIL_SYN_LIST,
4353 CIL_SYN_END
4354 };
4355 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4356 int rc = SEPOL_ERR;
4357 struct cil_portcon *portcon = NULL;
4358 char *proto;
4359
4360 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4361 goto exit;
4362 }
4363
4364 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4365 if (rc != SEPOL_OK) {
4366 goto exit;
4367 }
4368
4369 cil_portcon_init(&portcon);
4370
4371 proto = parse_current->next->data;
4372 if (proto == CIL_KEY_UDP) {
4373 portcon->proto = CIL_PROTOCOL_UDP;
4374 } else if (proto == CIL_KEY_TCP) {
4375 portcon->proto = CIL_PROTOCOL_TCP;
4376 } else if (proto == CIL_KEY_DCCP) {
4377 portcon->proto = CIL_PROTOCOL_DCCP;
4378 } else if (proto == CIL_KEY_SCTP) {
4379 portcon->proto = CIL_PROTOCOL_SCTP;
4380 } else {
4381 cil_log(CIL_ERR, "Invalid protocol\n");
4382 rc = SEPOL_ERR;
4383 goto exit;
4384 }
4385
4386 if (parse_current->next->next->cl_head != NULL) {
4387 if (parse_current->next->next->cl_head->next != NULL
4388 && parse_current->next->next->cl_head->next->next == NULL) {
4389 rc = cil_fill_integer(parse_current->next->next->cl_head, &portcon->port_low, 10);
4390 if (rc != SEPOL_OK) {
4391 cil_log(CIL_ERR, "Improper port specified\n");
4392 goto exit;
4393 }
4394 rc = cil_fill_integer(parse_current->next->next->cl_head->next, &portcon->port_high, 10);
4395 if (rc != SEPOL_OK) {
4396 cil_log(CIL_ERR, "Improper port specified\n");
4397 goto exit;
4398 }
4399 } else {
4400 cil_log(CIL_ERR, "Improper port range specified\n");
4401 rc = SEPOL_ERR;
4402 goto exit;
4403 }
4404 } else {
4405 rc = cil_fill_integer(parse_current->next->next, &portcon->port_low, 10);
4406 if (rc != SEPOL_OK) {
4407 cil_log(CIL_ERR, "Improper port specified\n");
4408 goto exit;
4409 }
4410 portcon->port_high = portcon->port_low;
4411 }
4412
4413 if (parse_current->next->next->next->cl_head == NULL ) {
4414 portcon->context_str = parse_current->next->next->next->data;
4415 } else {
4416 cil_context_init(&portcon->context);
4417
4418 rc = cil_fill_context(parse_current->next->next->next->cl_head, portcon->context);
4419 if (rc != SEPOL_OK) {
4420 goto exit;
4421 }
4422 }
4423
4424 ast_node->data = portcon;
4425 ast_node->flavor = CIL_PORTCON;
4426
4427 return SEPOL_OK;
4428
4429 exit:
4430 cil_tree_log(parse_current, CIL_ERR, "Bad portcon declaration");
4431 cil_destroy_portcon(portcon);
4432 return rc;
4433 }
4434
4435 void cil_destroy_portcon(struct cil_portcon *portcon)
4436 {
4437 if (portcon == NULL) {
4438 return;
4439 }
4440
4441 if (portcon->context_str == NULL && portcon->context != NULL) {
4442 cil_destroy_context(portcon->context);
4443 }
4444
4445 free(portcon);
4446 }
4447
4448 int cil_gen_nodecon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4449 {
4450 enum cil_syntax syntax[] = {
4451 CIL_SYN_STRING,
4452 CIL_SYN_STRING | CIL_SYN_LIST,
4453 CIL_SYN_STRING | CIL_SYN_LIST,
4454 CIL_SYN_STRING | CIL_SYN_LIST,
4455 CIL_SYN_END
4456 };
4457 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4458 int rc = SEPOL_ERR;
4459 struct cil_nodecon *nodecon = NULL;
4460
4461 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4462 goto exit;
4463 }
4464
4465 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4466 if (rc != SEPOL_OK) {
4467 goto exit;
4468 }
4469
4470 cil_nodecon_init(&nodecon);
4471
4472 if (parse_current->next->cl_head) {
4473 cil_ipaddr_init(&nodecon->addr);
4474 rc = cil_fill_ipaddr(parse_current->next->cl_head, nodecon->addr);
4475 if (rc != SEPOL_OK) {
4476 goto exit;
4477 }
4478 } else {
4479 char *addr = parse_current->next->data;
4480 if (strchr(addr, ':') || (strchr(addr, '.') && isdigit(addr[0]))) {
4481 cil_ipaddr_init(&nodecon->addr);
4482 rc = cil_fill_ipaddr(parse_current->next, nodecon->addr);
4483 if (rc != SEPOL_OK) {
4484 goto exit;
4485 }
4486 } else {
4487 nodecon->addr_str = addr;
4488 }
4489 }
4490
4491 if (parse_current->next->next->cl_head) {
4492 cil_ipaddr_init(&nodecon->mask);
4493 rc = cil_fill_ipaddr(parse_current->next->next->cl_head, nodecon->mask);
4494 if (rc != SEPOL_OK) {
4495 goto exit;
4496 }
4497 } else {
4498 char *mask = parse_current->next->next->data;
4499 if (strchr(mask, ':') || (strchr(mask, '.') && isdigit(mask[0]))) {
4500 cil_ipaddr_init(&nodecon->mask);
4501 rc = cil_fill_ipaddr(parse_current->next->next, nodecon->mask);
4502 if (rc != SEPOL_OK) {
4503 goto exit;
4504 }
4505 } else {
4506 nodecon->mask_str = mask;
4507 }
4508 }
4509
4510 if (parse_current->next->next->next->cl_head == NULL ) {
4511 nodecon->context_str = parse_current->next->next->next->data;
4512 } else {
4513 cil_context_init(&nodecon->context);
4514
4515 rc = cil_fill_context(parse_current->next->next->next->cl_head, nodecon->context);
4516 if (rc != SEPOL_OK) {
4517 goto exit;
4518 }
4519 }
4520
4521 ast_node->data = nodecon;
4522 ast_node->flavor = CIL_NODECON;
4523
4524 return SEPOL_OK;
4525
4526 exit:
4527 cil_tree_log(parse_current, CIL_ERR, "Bad nodecon declaration");
4528 cil_destroy_nodecon(nodecon);
4529 return rc;
4530 }
4531
4532 void cil_destroy_nodecon(struct cil_nodecon *nodecon)
4533 {
4534 if (nodecon == NULL) {
4535 return;
4536 }
4537
4538 if (nodecon->addr_str == NULL && nodecon->addr != NULL) {
4539 cil_destroy_ipaddr(nodecon->addr);
4540 }
4541
4542 if (nodecon->mask_str == NULL && nodecon->mask != NULL) {
4543 cil_destroy_ipaddr(nodecon->mask);
4544 }
4545
4546 if (nodecon->context_str == NULL && nodecon->context != NULL) {
4547 cil_destroy_context(nodecon->context);
4548 }
4549
4550 free(nodecon);
4551 }
4552
4553 int cil_gen_genfscon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4554 {
4555 enum cil_syntax syntax[] = {
4556 CIL_SYN_STRING,
4557 CIL_SYN_STRING,
4558 CIL_SYN_STRING,
4559 CIL_SYN_STRING | CIL_SYN_LIST,
4560 CIL_SYN_STRING | CIL_SYN_LIST | CIL_SYN_END,
4561 CIL_SYN_END
4562 };
4563 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4564 struct cil_tree_node *context_node;
4565 int rc = SEPOL_ERR;
4566 struct cil_genfscon *genfscon = NULL;
4567
4568 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4569 goto exit;
4570 }
4571
4572 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4573 if (rc != SEPOL_OK) {
4574 goto exit;
4575 }
4576
4577 cil_genfscon_init(&genfscon);
4578
4579 genfscon->fs_str = parse_current->next->data;
4580 genfscon->path_str = parse_current->next->next->data;
4581
4582 if (parse_current->next->next->next->next) {
4583 /* (genfscon <FS_STR> <PATH_STR> <FILE_TYPE> ... */
4584 char *file_type = parse_current->next->next->next->data;
4585 if (file_type == CIL_KEY_ANY) {
4586 genfscon->file_type = CIL_FILECON_ANY;
4587 } else if (file_type == CIL_KEY_FILE) {
4588 genfscon->file_type = CIL_FILECON_FILE;
4589 } else if (file_type == CIL_KEY_DIR) {
4590 genfscon->file_type = CIL_FILECON_DIR;
4591 } else if (file_type == CIL_KEY_CHAR) {
4592 genfscon->file_type = CIL_FILECON_CHAR;
4593 } else if (file_type == CIL_KEY_BLOCK) {
4594 genfscon->file_type = CIL_FILECON_BLOCK;
4595 } else if (file_type == CIL_KEY_SOCKET) {
4596 genfscon->file_type = CIL_FILECON_SOCKET;
4597 } else if (file_type == CIL_KEY_PIPE) {
4598 genfscon->file_type = CIL_FILECON_PIPE;
4599 } else if (file_type == CIL_KEY_SYMLINK) {
4600 genfscon->file_type = CIL_FILECON_SYMLINK;
4601 } else {
4602 if (parse_current->next->next->next->cl_head) {
4603 cil_log(CIL_ERR, "Expecting file type, but found a list\n");
4604 } else {
4605 cil_log(CIL_ERR, "Invalid file type \"%s\"\n", file_type);
4606 }
4607 goto exit;
4608 }
4609 context_node = parse_current->next->next->next->next;
4610 } else {
4611 /* (genfscon <FS_STR> <PATH_STR> ... */
4612 context_node = parse_current->next->next->next;
4613 }
4614
4615 if (context_node->cl_head) {
4616 cil_context_init(&genfscon->context);
4617 rc = cil_fill_context(context_node->cl_head, genfscon->context);
4618 if (rc != SEPOL_OK) {
4619 goto exit;
4620 }
4621 } else {
4622 genfscon->context_str = context_node->data;
4623 }
4624
4625 ast_node->data = genfscon;
4626 ast_node->flavor = CIL_GENFSCON;
4627
4628 return SEPOL_OK;
4629
4630 exit:
4631 cil_tree_log(parse_current, CIL_ERR, "Bad genfscon declaration");
4632 cil_destroy_genfscon(genfscon);
4633 return SEPOL_ERR;
4634 }
4635
4636 void cil_destroy_genfscon(struct cil_genfscon *genfscon)
4637 {
4638 if (genfscon == NULL) {
4639 return;
4640 }
4641
4642 if (genfscon->context_str == NULL && genfscon->context != NULL) {
4643 cil_destroy_context(genfscon->context);
4644 }
4645
4646 free(genfscon);
4647 }
4648
4649
4650 int cil_gen_netifcon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4651 {
4652 enum cil_syntax syntax[] = {
4653 CIL_SYN_STRING,
4654 CIL_SYN_STRING,
4655 CIL_SYN_STRING | CIL_SYN_LIST,
4656 CIL_SYN_STRING | CIL_SYN_LIST,
4657 CIL_SYN_END
4658 };
4659 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4660 int rc = SEPOL_ERR;
4661 struct cil_netifcon *netifcon = NULL;
4662
4663 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4664 goto exit;
4665 }
4666
4667 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4668 if (rc != SEPOL_OK) {
4669 goto exit;
4670 }
4671
4672 cil_netifcon_init(&netifcon);
4673
4674 netifcon->interface_str = parse_current->next->data;
4675
4676 if (parse_current->next->next->cl_head == NULL) {
4677 netifcon->if_context_str = parse_current->next->next->data;
4678 } else {
4679 cil_context_init(&netifcon->if_context);
4680
4681 rc = cil_fill_context(parse_current->next->next->cl_head, netifcon->if_context);
4682 if (rc != SEPOL_OK) {
4683 goto exit;
4684 }
4685 }
4686
4687 if (parse_current->next->next->next->cl_head == NULL) {
4688 netifcon->packet_context_str = parse_current->next->next->next->data;
4689 } else {
4690 cil_context_init(&netifcon->packet_context);
4691
4692 rc = cil_fill_context(parse_current->next->next->next->cl_head, netifcon->packet_context);
4693 if (rc != SEPOL_OK) {
4694 goto exit;
4695 }
4696 }
4697
4698 ast_node->data = netifcon;
4699 ast_node->flavor = CIL_NETIFCON;
4700
4701 return SEPOL_OK;
4702
4703 exit:
4704 cil_tree_log(parse_current, CIL_ERR, "Bad netifcon declaration");
4705 cil_destroy_netifcon(netifcon);
4706 return SEPOL_ERR;
4707 }
4708
4709 void cil_destroy_netifcon(struct cil_netifcon *netifcon)
4710 {
4711 if (netifcon == NULL) {
4712 return;
4713 }
4714
4715 if (netifcon->if_context_str == NULL && netifcon->if_context != NULL) {
4716 cil_destroy_context(netifcon->if_context);
4717 }
4718
4719 if (netifcon->packet_context_str == NULL && netifcon->packet_context != NULL) {
4720 cil_destroy_context(netifcon->packet_context);
4721 }
4722
4723 free(netifcon);
4724 }
4725
4726 int cil_gen_ibendportcon(__attribute__((unused)) struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4727 {
4728 enum cil_syntax syntax[] = {
4729 CIL_SYN_STRING,
4730 CIL_SYN_STRING,
4731 CIL_SYN_STRING,
4732 CIL_SYN_STRING | CIL_SYN_LIST,
4733 CIL_SYN_END
4734 };
4735 size_t syntax_len = sizeof(syntax) / sizeof(*syntax);
4736 int rc = SEPOL_ERR;
4737 struct cil_ibendportcon *ibendportcon = NULL;
4738
4739 if (!parse_current || !ast_node)
4740 goto exit;
4741
4742 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4743 if (rc != SEPOL_OK)
4744 goto exit;
4745
4746 cil_ibendportcon_init(&ibendportcon);
4747
4748 ibendportcon->dev_name_str = parse_current->next->data;
4749
4750 rc = cil_fill_integer(parse_current->next->next, &ibendportcon->port, 10);
4751 if (rc != SEPOL_OK) {
4752 cil_log(CIL_ERR, "Improper ibendport port specified\n");
4753 goto exit;
4754 }
4755
4756 if (!parse_current->next->next->next->cl_head) {
4757 ibendportcon->context_str = parse_current->next->next->next->data;
4758 } else {
4759 cil_context_init(&ibendportcon->context);
4760
4761 rc = cil_fill_context(parse_current->next->next->next->cl_head, ibendportcon->context);
4762 if (rc != SEPOL_OK)
4763 goto exit;
4764 }
4765
4766 ast_node->data = ibendportcon;
4767 ast_node->flavor = CIL_IBENDPORTCON;
4768
4769 return SEPOL_OK;
4770
4771 exit:
4772 cil_tree_log(parse_current, CIL_ERR, "Bad ibendportcon declaration");
4773 cil_destroy_ibendportcon(ibendportcon);
4774 return SEPOL_ERR;
4775 }
4776
4777 void cil_destroy_ibendportcon(struct cil_ibendportcon *ibendportcon)
4778 {
4779 if (!ibendportcon)
4780 return;
4781
4782 if (!ibendportcon->context_str && ibendportcon->context)
4783 cil_destroy_context(ibendportcon->context);
4784
4785 free(ibendportcon);
4786 }
4787
4788 int cil_gen_pirqcon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4789 {
4790 enum cil_syntax syntax[] = {
4791 CIL_SYN_STRING,
4792 CIL_SYN_STRING,
4793 CIL_SYN_STRING | CIL_SYN_LIST,
4794 CIL_SYN_END
4795 };
4796 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4797 int rc = SEPOL_ERR;
4798 struct cil_pirqcon *pirqcon = NULL;
4799
4800 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4801 goto exit;
4802 }
4803
4804 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4805 if (rc != SEPOL_OK) {
4806 goto exit;
4807 }
4808
4809 cil_pirqcon_init(&pirqcon);
4810
4811 rc = cil_fill_integer(parse_current->next, &pirqcon->pirq, 10);
4812 if (rc != SEPOL_OK) {
4813 goto exit;
4814 }
4815
4816 if (parse_current->next->next->cl_head == NULL) {
4817 pirqcon->context_str = parse_current->next->next->data;
4818 } else {
4819 cil_context_init(&pirqcon->context);
4820
4821 rc = cil_fill_context(parse_current->next->next->cl_head, pirqcon->context);
4822 if (rc != SEPOL_OK) {
4823 goto exit;
4824 }
4825 }
4826
4827 ast_node->data = pirqcon;
4828 ast_node->flavor = CIL_PIRQCON;
4829
4830 return SEPOL_OK;
4831
4832 exit:
4833 cil_tree_log(parse_current, CIL_ERR, "Bad pirqcon declaration");
4834 cil_destroy_pirqcon(pirqcon);
4835 return rc;
4836 }
4837
4838 void cil_destroy_pirqcon(struct cil_pirqcon *pirqcon)
4839 {
4840 if (pirqcon == NULL) {
4841 return;
4842 }
4843
4844 if (pirqcon->context_str == NULL && pirqcon->context != NULL) {
4845 cil_destroy_context(pirqcon->context);
4846 }
4847
4848 free(pirqcon);
4849 }
4850
4851 int cil_gen_iomemcon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4852 {
4853 enum cil_syntax syntax[] = {
4854 CIL_SYN_STRING,
4855 CIL_SYN_STRING | CIL_SYN_LIST,
4856 CIL_SYN_STRING | CIL_SYN_LIST,
4857 CIL_SYN_END
4858 };
4859 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4860 int rc = SEPOL_ERR;
4861 struct cil_iomemcon *iomemcon = NULL;
4862
4863 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4864 goto exit;
4865 }
4866
4867 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4868 if (rc != SEPOL_OK) {
4869 goto exit;
4870 }
4871
4872 cil_iomemcon_init(&iomemcon);
4873
4874 if (parse_current->next->cl_head != NULL) {
4875 if (parse_current->next->cl_head->next != NULL &&
4876 parse_current->next->cl_head->next->next == NULL) {
4877 rc = cil_fill_integer64(parse_current->next->cl_head, &iomemcon->iomem_low, 0);
4878 if (rc != SEPOL_OK) {
4879 cil_log(CIL_ERR, "Improper iomem specified\n");
4880 goto exit;
4881 }
4882 rc = cil_fill_integer64(parse_current->next->cl_head->next, &iomemcon->iomem_high, 0);
4883 if (rc != SEPOL_OK) {
4884 cil_log(CIL_ERR, "Improper iomem specified\n");
4885 goto exit;
4886 }
4887 } else {
4888 cil_log(CIL_ERR, "Improper iomem range specified\n");
4889 rc = SEPOL_ERR;
4890 goto exit;
4891 }
4892 } else {
4893 rc = cil_fill_integer64(parse_current->next, &iomemcon->iomem_low, 0);
4894 if (rc != SEPOL_OK) {
4895 cil_log(CIL_ERR, "Improper iomem specified\n");
4896 goto exit;
4897 }
4898 iomemcon->iomem_high = iomemcon->iomem_low;
4899 }
4900
4901 if (parse_current->next->next->cl_head == NULL ) {
4902 iomemcon->context_str = parse_current->next->next->data;
4903 } else {
4904 cil_context_init(&iomemcon->context);
4905
4906 rc = cil_fill_context(parse_current->next->next->cl_head, iomemcon->context);
4907 if (rc != SEPOL_OK) {
4908 goto exit;
4909 }
4910 }
4911
4912 ast_node->data = iomemcon;
4913 ast_node->flavor = CIL_IOMEMCON;
4914
4915 return SEPOL_OK;
4916
4917 exit:
4918 cil_tree_log(parse_current, CIL_ERR, "Bad iomemcon declaration");
4919 cil_destroy_iomemcon(iomemcon);
4920 return rc;
4921 }
4922
4923 void cil_destroy_iomemcon(struct cil_iomemcon *iomemcon)
4924 {
4925 if (iomemcon == NULL) {
4926 return;
4927 }
4928
4929 if (iomemcon->context_str == NULL && iomemcon->context != NULL) {
4930 cil_destroy_context(iomemcon->context);
4931 }
4932
4933 free(iomemcon);
4934 }
4935
4936 int cil_gen_ioportcon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
4937 {
4938 enum cil_syntax syntax[] = {
4939 CIL_SYN_STRING,
4940 CIL_SYN_STRING | CIL_SYN_LIST,
4941 CIL_SYN_STRING | CIL_SYN_LIST,
4942 CIL_SYN_END
4943 };
4944 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
4945 int rc = SEPOL_ERR;
4946 struct cil_ioportcon *ioportcon = NULL;
4947
4948 if (db == NULL || parse_current == NULL || ast_node == NULL) {
4949 goto exit;
4950 }
4951
4952 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
4953 if (rc != SEPOL_OK) {
4954 goto exit;
4955 }
4956
4957 cil_ioportcon_init(&ioportcon);
4958
4959 if (parse_current->next->cl_head != NULL) {
4960 if (parse_current->next->cl_head->next != NULL &&
4961 parse_current->next->cl_head->next->next == NULL) {
4962 rc = cil_fill_integer(parse_current->next->cl_head, &ioportcon->ioport_low, 0);
4963 if (rc != SEPOL_OK) {
4964 cil_log(CIL_ERR, "Improper ioport specified\n");
4965 goto exit;
4966 }
4967 rc = cil_fill_integer(parse_current->next->cl_head->next, &ioportcon->ioport_high, 0);
4968 if (rc != SEPOL_OK) {
4969 cil_log(CIL_ERR, "Improper ioport specified\n");
4970 goto exit;
4971 }
4972 } else {
4973 cil_log(CIL_ERR, "Improper ioport range specified\n");
4974 rc = SEPOL_ERR;
4975 goto exit;
4976 }
4977 } else {
4978 rc = cil_fill_integer(parse_current->next, &ioportcon->ioport_low, 0);
4979 if (rc != SEPOL_OK) {
4980 cil_log(CIL_ERR, "Improper ioport specified\n");
4981 goto exit;
4982 }
4983 ioportcon->ioport_high = ioportcon->ioport_low;
4984 }
4985
4986 if (parse_current->next->next->cl_head == NULL ) {
4987 ioportcon->context_str = parse_current->next->next->data;
4988 } else {
4989 cil_context_init(&ioportcon->context);
4990
4991 rc = cil_fill_context(parse_current->next->next->cl_head, ioportcon->context);
4992 if (rc != SEPOL_OK) {
4993 goto exit;
4994 }
4995 }
4996
4997 ast_node->data = ioportcon;
4998 ast_node->flavor = CIL_IOPORTCON;
4999
5000 return SEPOL_OK;
5001
5002 exit:
5003 cil_tree_log(parse_current, CIL_ERR, "Bad ioportcon declaration");
5004 cil_destroy_ioportcon(ioportcon);
5005 return rc;
5006 }
5007
5008 void cil_destroy_ioportcon(struct cil_ioportcon *ioportcon)
5009 {
5010 if (ioportcon == NULL) {
5011 return;
5012 }
5013
5014 if (ioportcon->context_str == NULL && ioportcon->context != NULL) {
5015 cil_destroy_context(ioportcon->context);
5016 }
5017
5018 free(ioportcon);
5019 }
5020
5021 int cil_gen_pcidevicecon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5022 {
5023 enum cil_syntax syntax[] = {
5024 CIL_SYN_STRING,
5025 CIL_SYN_STRING,
5026 CIL_SYN_STRING | CIL_SYN_LIST,
5027 CIL_SYN_END
5028 };
5029 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5030 int rc = SEPOL_ERR;
5031 struct cil_pcidevicecon *pcidevicecon = NULL;
5032
5033 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5034 goto exit;
5035 }
5036
5037 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5038 if (rc != SEPOL_OK) {
5039 goto exit;
5040 }
5041
5042 cil_pcidevicecon_init(&pcidevicecon);
5043
5044 rc = cil_fill_integer(parse_current->next, &pcidevicecon->dev, 0);
5045 if (rc != SEPOL_OK) {
5046 goto exit;
5047 }
5048
5049 if (parse_current->next->next->cl_head == NULL) {
5050 pcidevicecon->context_str = parse_current->next->next->data;
5051 } else {
5052 cil_context_init(&pcidevicecon->context);
5053
5054 rc = cil_fill_context(parse_current->next->next->cl_head, pcidevicecon->context);
5055 if (rc != SEPOL_OK) {
5056 goto exit;
5057 }
5058 }
5059
5060 ast_node->data = pcidevicecon;
5061 ast_node->flavor = CIL_PCIDEVICECON;
5062
5063 return SEPOL_OK;
5064
5065 exit:
5066 cil_tree_log(parse_current, CIL_ERR, "Bad pcidevicecon declaration");
5067 cil_destroy_pcidevicecon(pcidevicecon);
5068 return rc;
5069 }
5070
5071 void cil_destroy_pcidevicecon(struct cil_pcidevicecon *pcidevicecon)
5072 {
5073 if (pcidevicecon == NULL) {
5074 return;
5075 }
5076
5077 if (pcidevicecon->context_str == NULL && pcidevicecon->context != NULL) {
5078 cil_destroy_context(pcidevicecon->context);
5079 }
5080
5081 free(pcidevicecon);
5082 }
5083
5084 int cil_gen_devicetreecon(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5085 {
5086 enum cil_syntax syntax[] = {
5087 CIL_SYN_STRING,
5088 CIL_SYN_STRING,
5089 CIL_SYN_STRING | CIL_SYN_LIST,
5090 CIL_SYN_END
5091 };
5092 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5093 int rc = SEPOL_ERR;
5094 struct cil_devicetreecon *devicetreecon = NULL;
5095
5096 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5097 goto exit;
5098 }
5099
5100 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5101 if (rc != SEPOL_OK) {
5102 goto exit;
5103 }
5104
5105 cil_devicetreecon_init(&devicetreecon);
5106
5107 devicetreecon->path = parse_current->next->data;
5108
5109 if (parse_current->next->next->cl_head == NULL) {
5110 devicetreecon->context_str = parse_current->next->next->data;
5111 } else {
5112 cil_context_init(&devicetreecon->context);
5113
5114 rc = cil_fill_context(parse_current->next->next->cl_head, devicetreecon->context);
5115 if (rc != SEPOL_OK) {
5116 goto exit;
5117 }
5118 }
5119
5120 ast_node->data = devicetreecon;
5121 ast_node->flavor = CIL_DEVICETREECON;
5122
5123 return SEPOL_OK;
5124
5125 exit:
5126 cil_tree_log(parse_current, CIL_ERR, "Bad devicetreecon declaration");
5127 cil_destroy_devicetreecon(devicetreecon);
5128 return rc;
5129 }
5130
5131 void cil_destroy_devicetreecon(struct cil_devicetreecon *devicetreecon)
5132 {
5133 if (devicetreecon == NULL) {
5134 return;
5135 }
5136
5137 if (devicetreecon->context_str == NULL && devicetreecon->context != NULL) {
5138 cil_destroy_context(devicetreecon->context);
5139 }
5140
5141 free(devicetreecon);
5142 }
5143
5144 int cil_gen_fsuse(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5145 {
5146 enum cil_syntax syntax[] = {
5147 CIL_SYN_STRING,
5148 CIL_SYN_STRING,
5149 CIL_SYN_STRING,
5150 CIL_SYN_STRING | CIL_SYN_LIST,
5151 CIL_SYN_END
5152 };
5153 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5154 char *type = NULL;
5155 struct cil_fsuse *fsuse = NULL;
5156 int rc = SEPOL_ERR;
5157
5158 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5159 goto exit;
5160 }
5161
5162 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5163 if (rc != SEPOL_OK) {
5164 goto exit;
5165 }
5166
5167 type = parse_current->next->data;
5168
5169 cil_fsuse_init(&fsuse);
5170
5171 if (type == CIL_KEY_XATTR) {
5172 fsuse->type = CIL_FSUSE_XATTR;
5173 } else if (type == CIL_KEY_TASK) {
5174 fsuse->type = CIL_FSUSE_TASK;
5175 } else if (type == CIL_KEY_TRANS) {
5176 fsuse->type = CIL_FSUSE_TRANS;
5177 } else {
5178 cil_log(CIL_ERR, "Invalid fsuse type\n");
5179 goto exit;
5180 }
5181
5182 fsuse->fs_str = parse_current->next->next->data;
5183
5184 if (parse_current->next->next->next->cl_head == NULL) {
5185 fsuse->context_str = parse_current->next->next->next->data;
5186 } else {
5187 cil_context_init(&fsuse->context);
5188
5189 rc = cil_fill_context(parse_current->next->next->next->cl_head, fsuse->context);
5190 if (rc != SEPOL_OK) {
5191 goto exit;
5192 }
5193 }
5194
5195 ast_node->data = fsuse;
5196 ast_node->flavor = CIL_FSUSE;
5197
5198 return SEPOL_OK;
5199
5200 exit:
5201 cil_tree_log(parse_current, CIL_ERR, "Bad fsuse declaration");
5202 cil_destroy_fsuse(fsuse);
5203 return SEPOL_ERR;
5204 }
5205
5206 void cil_destroy_fsuse(struct cil_fsuse *fsuse)
5207 {
5208 if (fsuse == NULL) {
5209 return;
5210 }
5211
5212 if (fsuse->context_str == NULL && fsuse->context != NULL) {
5213 cil_destroy_context(fsuse->context);
5214 }
5215
5216 free(fsuse);
5217 }
5218
5219 void cil_destroy_param(struct cil_param *param)
5220 {
5221 if (param == NULL) {
5222 return;
5223 }
5224
5225 free(param);
5226 }
5227
5228 int cil_gen_macro(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5229 {
5230 int rc = SEPOL_ERR;
5231 char *key = NULL;
5232 struct cil_macro *macro = NULL;
5233 struct cil_tree_node *macro_content = NULL;
5234 struct cil_tree_node *current_item;
5235 enum cil_syntax syntax[] = {
5236 CIL_SYN_STRING,
5237 CIL_SYN_STRING,
5238 CIL_SYN_LIST | CIL_SYN_EMPTY_LIST,
5239 CIL_SYN_N_LISTS | CIL_SYN_END,
5240 CIL_SYN_END
5241 };
5242 size_t syntax_len = sizeof(syntax)/ sizeof(*syntax);
5243
5244 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5245 goto exit;
5246 }
5247
5248 rc =__cil_verify_syntax(parse_current, syntax, syntax_len);
5249 if (rc != SEPOL_OK) {
5250 goto exit;
5251 }
5252
5253 cil_macro_init(&macro);
5254
5255 key = parse_current->next->data;
5256
5257 current_item = parse_current->next->next->cl_head;
5258 while (current_item != NULL) {
5259 enum cil_syntax param_syntax[] = {
5260 CIL_SYN_STRING,
5261 CIL_SYN_STRING,
5262 CIL_SYN_END
5263 };
5264 int param_syntax_len = sizeof(param_syntax)/sizeof(*param_syntax);
5265 char *kind = NULL;
5266 struct cil_param *param = NULL;
5267 struct cil_list_item *curr_param;
5268
5269 rc =__cil_verify_syntax(current_item->cl_head, param_syntax, param_syntax_len);
5270 if (rc != SEPOL_OK) {
5271 goto exit;
5272 }
5273
5274 if (macro->params == NULL) {
5275 cil_list_init(&macro->params, CIL_LIST_ITEM);
5276 }
5277
5278 kind = current_item->cl_head->data;
5279 cil_param_init(&param);
5280
5281 if (kind == CIL_KEY_TYPE) {
5282 param->flavor = CIL_TYPE;
5283 } else if (kind == CIL_KEY_ROLE) {
5284 param->flavor = CIL_ROLE;
5285 } else if (kind == CIL_KEY_USER) {
5286 param->flavor = CIL_USER;
5287 } else if (kind == CIL_KEY_SENSITIVITY) {
5288 param->flavor = CIL_SENS;
5289 } else if (kind == CIL_KEY_CATEGORY) {
5290 param->flavor = CIL_CAT;
5291 } else if (kind == CIL_KEY_CATSET) {
5292 param->flavor = CIL_CATSET;
5293 } else if (kind == CIL_KEY_LEVEL) {
5294 param->flavor = CIL_LEVEL;
5295 } else if (kind == CIL_KEY_LEVELRANGE) {
5296 param->flavor = CIL_LEVELRANGE;
5297 } else if (kind == CIL_KEY_CLASS) {
5298 param->flavor = CIL_CLASS;
5299 } else if (kind == CIL_KEY_IPADDR) {
5300 param->flavor = CIL_IPADDR;
5301 } else if (kind == CIL_KEY_MAP_CLASS) {
5302 param->flavor = CIL_MAP_CLASS;
5303 } else if (kind == CIL_KEY_CLASSPERMISSION) {
5304 param->flavor = CIL_CLASSPERMISSION;
5305 } else if (kind == CIL_KEY_BOOL) {
5306 param->flavor = CIL_BOOL;
5307 } else if (kind == CIL_KEY_STRING) {
5308 param->flavor = CIL_DECLARED_STRING;
5309 } else if (kind == CIL_KEY_NAME) {
5310 param->flavor = CIL_DECLARED_STRING;
5311 } else {
5312 cil_log(CIL_ERR, "The kind %s is not allowed as a parameter\n",kind);
5313 cil_destroy_param(param);
5314 goto exit;
5315 }
5316
5317 param->str = current_item->cl_head->next->data;
5318
5319 rc = cil_verify_name(db, param->str, param->flavor);
5320 if (rc != SEPOL_OK) {
5321 cil_destroy_param(param);
5322 goto exit;
5323 }
5324
5325 //walk current list and check for duplicate parameters
5326 cil_list_for_each(curr_param, macro->params) {
5327 if (param->str == ((struct cil_param*)curr_param->data)->str) {
5328 cil_log(CIL_ERR, "Duplicate parameter\n");
5329 cil_destroy_param(param);
5330 goto exit;
5331 }
5332 }
5333
5334 cil_list_append(macro->params, CIL_PARAM, param);
5335
5336 current_item = current_item->next;
5337 }
5338
5339 /* we don't want the tree walker to walk the macro parameters (they were just handled above), so the subtree is deleted, and the next pointer of the
5340 node containing the macro name is updated to point to the start of the macro content */
5341 macro_content = parse_current->next->next->next;
5342 cil_tree_subtree_destroy(parse_current->next->next);
5343 parse_current->next->next = macro_content;
5344 if (macro_content == NULL) {
5345 /* No statements in macro and macro parameter list was last node */
5346 parse_current->parent->cl_tail = parse_current->next;
5347 }
5348
5349 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)macro, (hashtab_key_t)key, CIL_SYM_BLOCKS, CIL_MACRO);
5350 if (rc != SEPOL_OK) {
5351 goto exit;
5352 }
5353
5354 return SEPOL_OK;
5355
5356 exit:
5357 cil_tree_log(parse_current, CIL_ERR, "Bad macro declaration");
5358 cil_destroy_macro(macro);
5359 cil_clear_node(ast_node);
5360 return SEPOL_ERR;
5361 }
5362
5363 void cil_destroy_macro(struct cil_macro *macro)
5364 {
5365 if (macro == NULL) {
5366 return;
5367 }
5368
5369 cil_symtab_datum_destroy(&macro->datum);
5370 cil_symtab_array_destroy(macro->symtab);
5371
5372 if (macro->params != NULL) {
5373 cil_list_destroy(&macro->params, 1);
5374 }
5375
5376 free(macro);
5377 }
5378
5379 int cil_gen_call(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5380 {
5381 enum cil_syntax syntax[] = {
5382 CIL_SYN_STRING,
5383 CIL_SYN_STRING,
5384 CIL_SYN_LIST | CIL_SYN_EMPTY_LIST | CIL_SYN_END,
5385 CIL_SYN_END
5386 };
5387 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5388 struct cil_call *call = NULL;
5389 int rc = SEPOL_ERR;
5390
5391 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5392 goto exit;
5393 }
5394
5395 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5396 if (rc != SEPOL_OK) {
5397 goto exit;
5398 }
5399
5400 cil_call_init(&call);
5401
5402 call->macro_str = parse_current->next->data;
5403
5404 if (parse_current->next->next != NULL) {
5405 cil_tree_init(&call->args_tree);
5406 cil_copy_ast(db, parse_current->next->next, call->args_tree->root);
5407 }
5408
5409 ast_node->data = call;
5410 ast_node->flavor = CIL_CALL;
5411
5412 return SEPOL_OK;
5413
5414 exit:
5415 cil_tree_log(parse_current, CIL_ERR, "Bad macro call");
5416 cil_destroy_call(call);
5417 return rc;
5418 }
5419
5420 void cil_destroy_call(struct cil_call *call)
5421 {
5422 if (call == NULL) {
5423 return;
5424 }
5425
5426 call->macro = NULL;
5427
5428 if (call->args_tree != NULL) {
5429 cil_tree_destroy(&call->args_tree);
5430 }
5431
5432 if (call->args != NULL) {
5433 cil_list_destroy(&call->args, 1);
5434 }
5435
5436 free(call);
5437 }
5438
5439 void cil_destroy_args(struct cil_args *args)
5440 {
5441 if (args == NULL) {
5442 return;
5443 }
5444
5445 if (args->arg_str != NULL) {
5446 args->arg_str = NULL;
5447 } else if (args->arg != NULL) {
5448 struct cil_tree_node *node = args->arg->nodes->head->data;
5449 switch (args->flavor) {
5450 case CIL_DECLARED_STRING:
5451 break;
5452 case CIL_CATSET:
5453 cil_destroy_catset((struct cil_catset *)args->arg);
5454 free(node);
5455 break;
5456 case CIL_LEVEL:
5457 cil_destroy_level((struct cil_level *)args->arg);
5458 free(node);
5459 break;
5460 case CIL_LEVELRANGE:
5461 cil_destroy_levelrange((struct cil_levelrange *)args->arg);
5462 free(node);
5463 break;
5464 case CIL_IPADDR:
5465 cil_destroy_ipaddr((struct cil_ipaddr *)args->arg);
5466 free(node);
5467 break;
5468 case CIL_CLASSPERMISSION:
5469 cil_destroy_classpermission((struct cil_classpermission *)args->arg);
5470 free(node);
5471 break;
5472 default:
5473 cil_log(CIL_ERR, "Destroying arg with the unexpected flavor=%d\n",args->flavor);
5474 break;
5475 }
5476 }
5477
5478 args->param_str = NULL;
5479 args->arg = NULL;
5480
5481 free(args);
5482 }
5483
5484 int cil_gen_optional(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5485 {
5486 enum cil_syntax syntax[] = {
5487 CIL_SYN_STRING,
5488 CIL_SYN_STRING,
5489 CIL_SYN_N_LISTS | CIL_SYN_END,
5490 CIL_SYN_END
5491 };
5492 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5493 char *key = NULL;
5494 struct cil_optional *optional = NULL;
5495 int rc = SEPOL_ERR;
5496
5497 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5498 goto exit;
5499 }
5500
5501 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5502 if (rc != SEPOL_OK) {
5503 goto exit;
5504 }
5505
5506 cil_optional_init(&optional);
5507
5508 key = parse_current->next->data;
5509
5510 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)optional, (hashtab_key_t)key, CIL_SYM_BLOCKS, CIL_OPTIONAL);
5511 if (rc != SEPOL_OK) {
5512 if (rc == SEPOL_EEXIST) {
5513 cil_destroy_optional(optional);
5514 optional = NULL;
5515 } else {
5516 goto exit;
5517 }
5518 }
5519
5520 return SEPOL_OK;
5521
5522 exit:
5523 cil_tree_log(parse_current, CIL_ERR, "Bad optional");
5524 cil_destroy_optional(optional);
5525 cil_clear_node(ast_node);
5526 return rc;
5527 }
5528
5529 void cil_destroy_optional(struct cil_optional *optional)
5530 {
5531 if (optional == NULL) {
5532 return;
5533 }
5534
5535 cil_symtab_datum_destroy(&optional->datum);
5536 free(optional);
5537 }
5538
5539 int cil_gen_policycap(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5540 {
5541 enum cil_syntax syntax[] = {
5542 CIL_SYN_STRING,
5543 CIL_SYN_STRING,
5544 CIL_SYN_END
5545 };
5546 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5547 char *key = NULL;
5548 struct cil_policycap *polcap = NULL;
5549 int rc = SEPOL_ERR;
5550
5551 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5552 goto exit;
5553 }
5554
5555 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5556 if (rc != SEPOL_OK) {
5557 goto exit;
5558 }
5559
5560 cil_policycap_init(&polcap);
5561
5562 key = parse_current->next->data;
5563
5564 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)polcap, (hashtab_key_t)key, CIL_SYM_POLICYCAPS, CIL_POLICYCAP);
5565 if (rc != SEPOL_OK) {
5566 if (rc == SEPOL_EEXIST) {
5567 cil_destroy_policycap(polcap);
5568 polcap = NULL;
5569 } else {
5570 goto exit;
5571 }
5572 }
5573
5574 return SEPOL_OK;
5575
5576 exit:
5577 cil_tree_log(parse_current, CIL_ERR, "Bad policycap statement");
5578 cil_destroy_policycap(polcap);
5579 cil_clear_node(ast_node);
5580 return rc;
5581 }
5582
5583 void cil_destroy_policycap(struct cil_policycap *polcap)
5584 {
5585 if (polcap == NULL) {
5586 return;
5587 }
5588
5589 cil_symtab_datum_destroy(&polcap->datum);
5590 free(polcap);
5591 }
5592
5593 int cil_gen_ipaddr(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5594 {
5595 enum cil_syntax syntax[] = {
5596 CIL_SYN_STRING,
5597 CIL_SYN_STRING,
5598 CIL_SYN_STRING,
5599 CIL_SYN_END
5600 };
5601 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5602 char *key = NULL;
5603 struct cil_ipaddr *ipaddr = NULL;
5604 int rc = SEPOL_ERR;
5605
5606 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5607 goto exit;
5608 }
5609
5610 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5611 if (rc != SEPOL_OK) {
5612 goto exit;
5613 }
5614
5615 cil_ipaddr_init(&ipaddr);
5616
5617 key = parse_current->next->data;
5618
5619 rc = cil_fill_ipaddr(parse_current->next->next, ipaddr);
5620 if (rc != SEPOL_OK) {
5621 goto exit;
5622 }
5623
5624 rc = cil_gen_node(db, ast_node, (struct cil_symtab_datum*)ipaddr, (hashtab_key_t)key, CIL_SYM_IPADDRS, CIL_IPADDR);
5625 if (rc != SEPOL_OK) {
5626 goto exit;
5627 }
5628
5629 return SEPOL_OK;
5630
5631 exit:
5632 cil_tree_log(parse_current, CIL_ERR, "Bad ipaddr statement");
5633 cil_destroy_ipaddr(ipaddr);
5634 cil_clear_node(ast_node);
5635 return rc;
5636 }
5637
5638 void cil_destroy_ipaddr(struct cil_ipaddr *ipaddr)
5639 {
5640 if (ipaddr == NULL) {
5641 return;
5642 }
5643
5644 cil_symtab_datum_destroy(&ipaddr->datum);
5645 free(ipaddr);
5646 }
5647
5648 int cil_fill_integer(struct cil_tree_node *int_node, uint32_t *integer, int base)
5649 {
5650 int rc = SEPOL_ERR;
5651
5652 if (int_node == NULL || int_node->data == NULL || integer == NULL) {
5653 goto exit;
5654 }
5655
5656 rc = cil_string_to_uint32(int_node->data, integer, base);
5657 if (rc != SEPOL_OK) {
5658 goto exit;
5659 }
5660
5661 return SEPOL_OK;
5662
5663 exit:
5664 cil_log(CIL_ERR, "Failed to fill 32-bit integer\n");
5665 return rc;
5666 }
5667
5668 int cil_fill_integer64(struct cil_tree_node *int_node, uint64_t *integer, int base)
5669 {
5670 int rc = SEPOL_ERR;
5671
5672 if (int_node == NULL || int_node->data == NULL || integer == NULL) {
5673 goto exit;
5674 }
5675
5676 rc = cil_string_to_uint64(int_node->data, integer, base);
5677 if (rc != SEPOL_OK) {
5678 goto exit;
5679 }
5680
5681 return SEPOL_OK;
5682
5683 exit:
5684 cil_log(CIL_ERR, "Failed to fill 64-bit integer\n");
5685 return rc;
5686 }
5687
5688 int cil_fill_ipaddr(struct cil_tree_node *addr_node, struct cil_ipaddr *addr)
5689 {
5690 int rc = SEPOL_ERR;
5691 char *addr_str;
5692
5693 if (addr_node == NULL || addr_node->data == NULL || addr == NULL) {
5694 goto exit;
5695 }
5696
5697 addr_str = addr_node->data;
5698 if (strchr(addr_str, ':')) {
5699 addr->family = AF_INET6;
5700 } else if (strchr(addr_str, '.') && isdigit(addr_str[0])) {
5701 addr->family = AF_INET;
5702 } else {
5703 goto exit;
5704 }
5705
5706 rc = inet_pton(addr->family, addr_node->data, &addr->ip);
5707 if (rc != 1) {
5708 rc = SEPOL_ERR;
5709 goto exit;
5710 }
5711
5712 return SEPOL_OK;
5713
5714 exit:
5715 cil_log(CIL_ERR, "Bad ip address or netmask: %s\n", (addr_node && addr_node->data) ? (const char *)addr_node->data : "NULL");
5716 return rc;
5717 }
5718
5719 int cil_fill_level(struct cil_tree_node *curr, struct cil_level *level)
5720 {
5721 int rc = SEPOL_ERR;
5722 enum cil_syntax syntax[] = {
5723 CIL_SYN_STRING,
5724 CIL_SYN_STRING | CIL_SYN_LIST | CIL_SYN_END,
5725 CIL_SYN_END
5726 };
5727 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5728
5729 if (curr == NULL) {
5730 goto exit;
5731 }
5732
5733 rc = __cil_verify_syntax(curr, syntax, syntax_len);
5734 if (rc != SEPOL_OK) {
5735 goto exit;
5736 }
5737
5738 level->sens_str = curr->data;
5739 if (curr->next != NULL) {
5740 rc = cil_fill_cats(curr->next, &level->cats);
5741 if (rc != SEPOL_OK) {
5742 goto exit;
5743 }
5744 }
5745
5746 return SEPOL_OK;
5747
5748 exit:
5749 cil_log(CIL_ERR, "Bad level\n");
5750 return rc;
5751 }
5752
5753 int cil_fill_cats(struct cil_tree_node *curr, struct cil_cats **cats)
5754 {
5755 int rc = SEPOL_ERR;
5756
5757 cil_cats_init(cats);
5758
5759 rc = cil_gen_expr(curr, CIL_CAT, &(*cats)->str_expr);
5760 if (rc != SEPOL_OK) {
5761 cil_destroy_cats(*cats);
5762 *cats = NULL;
5763 }
5764
5765 return rc;
5766 }
5767
5768 void cil_destroy_cats(struct cil_cats *cats)
5769 {
5770 if (cats == NULL) {
5771 return;
5772 }
5773
5774 cil_list_destroy(&cats->str_expr, CIL_TRUE);
5775
5776 cil_list_destroy(&cats->datum_expr, CIL_FALSE);
5777
5778 free(cats);
5779 }
5780 int cil_gen_bounds(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor)
5781 {
5782 enum cil_syntax syntax[] = {
5783 CIL_SYN_STRING,
5784 CIL_SYN_STRING,
5785 CIL_SYN_STRING,
5786 CIL_SYN_END
5787 };
5788 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5789 struct cil_bounds *bounds = NULL;
5790 int rc = SEPOL_ERR;
5791
5792 if (db == NULL || parse_current == NULL || ast_node == NULL) {
5793 goto exit;
5794 }
5795
5796 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5797 if (rc != SEPOL_OK) {
5798 goto exit;
5799 }
5800
5801 cil_bounds_init(&bounds);
5802
5803 bounds->parent_str = parse_current->next->data;
5804 bounds->child_str = parse_current->next->next->data;
5805
5806 ast_node->data = bounds;
5807
5808 switch (flavor) {
5809 case CIL_USER:
5810 ast_node->flavor = CIL_USERBOUNDS;
5811 break;
5812 case CIL_ROLE:
5813 ast_node->flavor = CIL_ROLEBOUNDS;
5814 break;
5815 case CIL_TYPE:
5816 ast_node->flavor = CIL_TYPEBOUNDS;
5817 break;
5818 default:
5819 break;
5820 }
5821
5822 return SEPOL_OK;
5823
5824 exit:
5825 cil_tree_log(parse_current, CIL_ERR, "Bad bounds declaration");
5826 cil_destroy_bounds(bounds);
5827 return rc;
5828 }
5829
5830 void cil_destroy_bounds(struct cil_bounds *bounds)
5831 {
5832 if (bounds == NULL) {
5833 return;
5834 }
5835
5836 free(bounds);
5837 }
5838
5839 int cil_gen_default(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node, enum cil_flavor flavor)
5840 {
5841 int rc = SEPOL_ERR;
5842 struct cil_default *def = NULL;
5843 char *object;
5844 enum cil_syntax syntax[] = {
5845 CIL_SYN_STRING,
5846 CIL_SYN_STRING | CIL_SYN_LIST,
5847 CIL_SYN_STRING,
5848 CIL_SYN_END
5849 };
5850 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5851
5852 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5853 if (rc != SEPOL_OK) {
5854 goto exit;
5855 }
5856
5857 cil_default_init(&def);
5858
5859 def->flavor = flavor;
5860
5861 if (parse_current->next->cl_head == NULL) {
5862 cil_list_init(&def->class_strs, CIL_CLASS);
5863 cil_list_append(def->class_strs, CIL_STRING, parse_current->next->data);
5864 } else {
5865 rc = cil_fill_list(parse_current->next->cl_head, CIL_CLASS, &def->class_strs);
5866 if (rc != SEPOL_OK) {
5867 goto exit;
5868 }
5869 }
5870
5871 object = parse_current->next->next->data;
5872 if (object == CIL_KEY_SOURCE) {
5873 def->object = CIL_DEFAULT_SOURCE;
5874 } else if (object == CIL_KEY_TARGET) {
5875 def->object = CIL_DEFAULT_TARGET;
5876 } else {
5877 cil_log(CIL_ERR,"Expected either 'source' or 'target'\n");
5878 rc = SEPOL_ERR;
5879 goto exit;
5880 }
5881
5882 ast_node->data = def;
5883 ast_node->flavor = flavor;
5884
5885 return SEPOL_OK;
5886
5887 exit:
5888 cil_tree_log(parse_current, CIL_ERR, "Bad %s declaration", cil_node_to_string(parse_current));
5889 cil_destroy_default(def);
5890 return rc;
5891 }
5892
5893 void cil_destroy_default(struct cil_default *def)
5894 {
5895 if (def == NULL) {
5896 return;
5897 }
5898
5899 cil_list_destroy(&def->class_strs, CIL_TRUE);
5900
5901 cil_list_destroy(&def->class_datums, CIL_FALSE);
5902
5903 free(def);
5904 }
5905
5906 int cil_gen_defaultrange(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
5907 {
5908 int rc = SEPOL_ERR;
5909 struct cil_defaultrange *def = NULL;
5910 char *object;
5911 char *range;
5912 enum cil_syntax syntax[] = {
5913 CIL_SYN_STRING,
5914 CIL_SYN_STRING | CIL_SYN_LIST,
5915 CIL_SYN_STRING,
5916 CIL_SYN_STRING | CIL_SYN_END,
5917 CIL_SYN_END
5918 };
5919 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
5920
5921 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
5922 if (rc != SEPOL_OK) {
5923 goto exit;
5924 }
5925
5926 cil_defaultrange_init(&def);
5927
5928 if (parse_current->next->cl_head == NULL) {
5929 cil_list_init(&def->class_strs, CIL_CLASS);
5930 cil_list_append(def->class_strs, CIL_STRING, parse_current->next->data);
5931 } else {
5932 rc = cil_fill_list(parse_current->next->cl_head, CIL_CLASS, &def->class_strs);
5933 if (rc != SEPOL_OK) {
5934 goto exit;
5935 }
5936 }
5937
5938 object = parse_current->next->next->data;
5939 if (object == CIL_KEY_SOURCE) {
5940 if (!parse_current->next->next->next) {
5941 cil_log(CIL_ERR, "Missing 'low', 'high', or 'low-high'\n");
5942 rc = SEPOL_ERR;
5943 goto exit;
5944 }
5945 range = parse_current->next->next->next->data;
5946 if (range == CIL_KEY_LOW) {
5947 def->object_range = CIL_DEFAULT_SOURCE_LOW;
5948 } else if (range == CIL_KEY_HIGH) {
5949 def->object_range = CIL_DEFAULT_SOURCE_HIGH;
5950 } else if (range == CIL_KEY_LOW_HIGH) {
5951 def->object_range = CIL_DEFAULT_SOURCE_LOW_HIGH;
5952 } else {
5953 cil_log(CIL_ERR,"Expected 'low', 'high', or 'low-high'\n");
5954 rc = SEPOL_ERR;
5955 goto exit;
5956 }
5957 } else if (object == CIL_KEY_TARGET) {
5958 if (!parse_current->next->next->next) {
5959 cil_log(CIL_ERR, "Missing 'low', 'high', or 'low-high'\n");
5960 rc = SEPOL_ERR;
5961 goto exit;
5962 }
5963 range = parse_current->next->next->next->data;
5964 if (range == CIL_KEY_LOW) {
5965 def->object_range = CIL_DEFAULT_TARGET_LOW;
5966 } else if (range == CIL_KEY_HIGH) {
5967 def->object_range = CIL_DEFAULT_TARGET_HIGH;
5968 } else if (range == CIL_KEY_LOW_HIGH) {
5969 def->object_range = CIL_DEFAULT_TARGET_LOW_HIGH;
5970 } else {
5971 cil_log(CIL_ERR,"Expected 'low', 'high', or 'low-high'\n");
5972 rc = SEPOL_ERR;
5973 goto exit;
5974 }
5975 } else if (object == CIL_KEY_GLBLUB) {
5976 def->object_range = CIL_DEFAULT_GLBLUB;
5977 } else {
5978 cil_log(CIL_ERR,"Expected \'source\', \'target\', or \'glblub\'\n");
5979 rc = SEPOL_ERR;
5980 goto exit;
5981 }
5982
5983 ast_node->data = def;
5984 ast_node->flavor = CIL_DEFAULTRANGE;
5985
5986 return SEPOL_OK;
5987
5988 exit:
5989 cil_tree_log(parse_current, CIL_ERR, "Bad defaultrange declaration");
5990 cil_destroy_defaultrange(def);
5991 return rc;
5992 }
5993
5994 void cil_destroy_defaultrange(struct cil_defaultrange *def)
5995 {
5996 if (def == NULL) {
5997 return;
5998 }
5999
6000 cil_list_destroy(&def->class_strs, CIL_TRUE);
6001
6002 cil_list_destroy(&def->class_datums, CIL_FALSE);
6003
6004 free(def);
6005 }
6006
6007 int cil_gen_handleunknown(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
6008 {
6009 int rc = SEPOL_ERR;
6010 enum cil_syntax syntax[] = {
6011 CIL_SYN_STRING,
6012 CIL_SYN_STRING,
6013 CIL_SYN_END
6014 };
6015 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
6016 struct cil_handleunknown *unknown = NULL;
6017 char *unknown_key;
6018
6019 if (parse_current == NULL || ast_node == NULL) {
6020 goto exit;
6021 }
6022
6023 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
6024 if (rc != SEPOL_OK) {
6025 goto exit;
6026 }
6027
6028 cil_handleunknown_init(&unknown);
6029
6030 unknown_key = parse_current->next->data;
6031 if (unknown_key == CIL_KEY_HANDLEUNKNOWN_ALLOW) {
6032 unknown->handle_unknown = SEPOL_ALLOW_UNKNOWN;
6033 } else if (unknown_key == CIL_KEY_HANDLEUNKNOWN_DENY) {
6034 unknown->handle_unknown = SEPOL_DENY_UNKNOWN;
6035 } else if (unknown_key == CIL_KEY_HANDLEUNKNOWN_REJECT) {
6036 unknown->handle_unknown = SEPOL_REJECT_UNKNOWN;
6037 } else {
6038 cil_log(CIL_ERR, "Expected either \'%s\', \'%s\', or \'%s\'\n", CIL_KEY_HANDLEUNKNOWN_ALLOW, CIL_KEY_HANDLEUNKNOWN_DENY, CIL_KEY_HANDLEUNKNOWN_REJECT);
6039 rc = SEPOL_ERR;
6040 goto exit;
6041 }
6042
6043 ast_node->data = unknown;
6044 ast_node->flavor = CIL_HANDLEUNKNOWN;
6045
6046 return SEPOL_OK;
6047
6048 exit:
6049 cil_tree_log(parse_current, CIL_ERR, "Bad handleunknown");
6050 cil_destroy_handleunknown(unknown);
6051 return rc;
6052 }
6053
6054 void cil_destroy_handleunknown(struct cil_handleunknown *unk)
6055 {
6056 free(unk);
6057 }
6058
6059 int cil_gen_mls(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
6060 {
6061 int rc = SEPOL_ERR;
6062 enum cil_syntax syntax[] = {
6063 CIL_SYN_STRING,
6064 CIL_SYN_STRING,
6065 CIL_SYN_END
6066 };
6067 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
6068 struct cil_mls *mls = NULL;
6069
6070 if (parse_current == NULL || ast_node == NULL) {
6071 goto exit;
6072 }
6073
6074 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
6075 if (rc != SEPOL_OK) {
6076 goto exit;
6077 }
6078
6079 cil_mls_init(&mls);
6080
6081 if (parse_current->next->data == CIL_KEY_CONDTRUE) {
6082 mls->value = CIL_TRUE;
6083 } else if (parse_current->next->data == CIL_KEY_CONDFALSE) {
6084 mls->value = CIL_FALSE;
6085 } else {
6086 cil_log(CIL_ERR, "Value must be either \'true\' or \'false\'");
6087 rc = SEPOL_ERR;
6088 goto exit;
6089 }
6090
6091 ast_node->data = mls;
6092 ast_node->flavor = CIL_MLS;
6093
6094 return SEPOL_OK;
6095
6096 exit:
6097 cil_tree_log(parse_current, CIL_ERR, "Bad mls");
6098 cil_destroy_mls(mls);
6099 return rc;
6100 }
6101
6102 void cil_destroy_mls(struct cil_mls *mls)
6103 {
6104 free(mls);
6105 }
6106
6107 int cil_gen_src_info(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
6108 {
6109 int rc = SEPOL_ERR;
6110 enum cil_syntax syntax[] = {
6111 CIL_SYN_STRING,
6112 CIL_SYN_STRING,
6113 CIL_SYN_STRING,
6114 CIL_SYN_STRING,
6115 CIL_SYN_N_LISTS | CIL_SYN_END,
6116 CIL_SYN_END
6117 };
6118 size_t syntax_len = sizeof(syntax)/sizeof(*syntax);
6119 struct cil_src_info *info = NULL;
6120
6121 if (parse_current == NULL || ast_node == NULL) {
6122 goto exit;
6123 }
6124
6125 rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
6126 if (rc != SEPOL_OK) {
6127 goto exit;
6128 }
6129
6130 cil_src_info_init(&info);
6131
6132 info->kind = parse_current->next->data;
6133 if (info->kind != CIL_KEY_SRC_CIL && info->kind != CIL_KEY_SRC_HLL_LMS && info->kind != CIL_KEY_SRC_HLL_LMX) {
6134 cil_log(CIL_ERR, "Invalid src info kind\n");
6135 rc = SEPOL_ERR;
6136 goto exit;
6137 }
6138
6139 rc = cil_string_to_uint32(parse_current->next->next->data, &info->hll_line, 10);
6140 if (rc != SEPOL_OK) {
6141 goto exit;
6142 }
6143
6144 info->path = parse_current->next->next->next->data;
6145
6146 ast_node->data = info;
6147 ast_node->flavor = CIL_SRC_INFO;
6148
6149 return SEPOL_OK;
6150
6151 exit:
6152 cil_tree_log(parse_current, CIL_ERR, "Bad src info");
6153 cil_destroy_src_info(info);
6154 return rc;
6155 }
6156
6157 void cil_destroy_src_info(struct cil_src_info *info)
6158 {
6159 free(info);
6160 }
6161
6162 static int check_for_illegal_statement(struct cil_tree_node *parse_current, struct cil_args_build *args)
6163 {
6164 if (args->tunif != NULL) {
6165 if (parse_current->data == CIL_KEY_TUNABLE) {
6166 cil_tree_log(parse_current, CIL_ERR, "%s is not allowed in tunableif", (char *)parse_current->data);
6167 return SEPOL_ERR;
6168 }
6169 }
6170
6171 if (args->in != NULL) {
6172 struct cil_in *in_block = args->in->data;
6173 if (parse_current->data == CIL_KEY_TUNABLE ||
6174 parse_current->data == CIL_KEY_IN) {
6175 cil_tree_log(parse_current, CIL_ERR, "%s is not allowed in in-statement", (char *)parse_current->data);
6176 return SEPOL_ERR;
6177 }
6178 if (in_block->is_after == CIL_TRUE) {
6179 if (parse_current->data == CIL_KEY_BLOCKINHERIT ||
6180 parse_current->data == CIL_KEY_BLOCKABSTRACT) {
6181 cil_tree_log(parse_current, CIL_ERR, "%s is not allowed in an after in-statement", (char *)parse_current->data);
6182 return SEPOL_ERR;
6183 }
6184 }
6185 }
6186
6187 if (args->macro != NULL) {
6188 if (parse_current->data == CIL_KEY_TUNABLE ||
6189 parse_current->data == CIL_KEY_IN ||
6190 parse_current->data == CIL_KEY_BLOCK ||
6191 parse_current->data == CIL_KEY_BLOCKINHERIT ||
6192 parse_current->data == CIL_KEY_BLOCKABSTRACT ||
6193 parse_current->data == CIL_KEY_MACRO) {
6194 cil_tree_log(parse_current, CIL_ERR, "%s is not allowed in macro", (char *)parse_current->data);
6195 return SEPOL_ERR;
6196 }
6197 }
6198
6199 if (args->optional != NULL) {
6200 if (parse_current->data == CIL_KEY_TUNABLE ||
6201 parse_current->data == CIL_KEY_IN ||
6202 parse_current->data == CIL_KEY_BLOCK ||
6203 parse_current->data == CIL_KEY_BLOCKABSTRACT ||
6204 parse_current->data == CIL_KEY_MACRO) {
6205 cil_tree_log(parse_current, CIL_ERR, "%s is not allowed in optional", (char *)parse_current->data);
6206 return SEPOL_ERR;
6207 }
6208 }
6209
6210 if (args->boolif != NULL) {
6211 if (parse_current->data != CIL_KEY_TUNABLEIF &&
6212 parse_current->data != CIL_KEY_CALL &&
6213 parse_current->data != CIL_KEY_CONDTRUE &&
6214 parse_current->data != CIL_KEY_CONDFALSE &&
6215 parse_current->data != CIL_KEY_ALLOW &&
6216 parse_current->data != CIL_KEY_DONTAUDIT &&
6217 parse_current->data != CIL_KEY_AUDITALLOW &&
6218 parse_current->data != CIL_KEY_TYPETRANSITION &&
6219 parse_current->data != CIL_KEY_TYPECHANGE &&
6220 parse_current->data != CIL_KEY_SRC_INFO &&
6221 parse_current->data != CIL_KEY_TYPEMEMBER &&
6222 ((args->db->policy_version < POLICYDB_VERSION_COND_XPERMS) ||
6223 (parse_current->data != CIL_KEY_ALLOWX &&
6224 parse_current->data != CIL_KEY_DONTAUDITX &&
6225 parse_current->data != CIL_KEY_AUDITALLOWX))) {
6226 if (((struct cil_booleanif*)args->boolif->data)->preserved_tunable) {
6227 cil_tree_log(parse_current, CIL_ERR, "%s is not allowed in tunableif being treated as a booleanif", (char *)parse_current->data);
6228 } else {
6229 cil_tree_log(parse_current, CIL_ERR, "%s is not allowed in booleanif", (char *)parse_current->data);
6230 }
6231 return SEPOL_ERR;
6232 }
6233 }
6234
6235 return SEPOL_OK;
6236 }
6237
6238 static struct cil_tree_node * parse_statement(struct cil_db *db, struct cil_tree_node *parse_current, struct cil_tree_node *ast_parent)
6239 {
6240 struct cil_tree_node *new_ast_node = NULL;
6241 int rc = SEPOL_ERR;
6242
6243 cil_tree_node_init(&new_ast_node);
6244 new_ast_node->parent = ast_parent;
6245 new_ast_node->line = parse_current->line;
6246 new_ast_node->hll_offset = parse_current->hll_offset;
6247
6248 if (parse_current->data == CIL_KEY_BLOCK) {
6249 rc = cil_gen_block(db, parse_current, new_ast_node, 0);
6250 } else if (parse_current->data == CIL_KEY_BLOCKINHERIT) {
6251 rc = cil_gen_blockinherit(db, parse_current, new_ast_node);
6252 } else if (parse_current->data == CIL_KEY_BLOCKABSTRACT) {
6253 rc = cil_gen_blockabstract(db, parse_current, new_ast_node);
6254 } else if (parse_current->data == CIL_KEY_IN) {
6255 rc = cil_gen_in(db, parse_current, new_ast_node);
6256 } else if (parse_current->data == CIL_KEY_CLASS) {
6257 rc = cil_gen_class(db, parse_current, new_ast_node);
6258 } else if (parse_current->data == CIL_KEY_CLASSORDER) {
6259 rc = cil_gen_ordered(db, parse_current, new_ast_node, CIL_CLASSORDER);
6260 } else if (parse_current->data == CIL_KEY_MAP_CLASS) {
6261 rc = cil_gen_map_class(db, parse_current, new_ast_node);
6262 } else if (parse_current->data == CIL_KEY_CLASSMAPPING) {
6263 rc = cil_gen_classmapping(db, parse_current, new_ast_node);
6264 } else if (parse_current->data == CIL_KEY_CLASSPERMISSION) {
6265 rc = cil_gen_classpermission(db, parse_current, new_ast_node);
6266 } else if (parse_current->data == CIL_KEY_CLASSPERMISSIONSET) {
6267 rc = cil_gen_classpermissionset(db, parse_current, new_ast_node);
6268 } else if (parse_current->data == CIL_KEY_COMMON) {
6269 rc = cil_gen_common(db, parse_current, new_ast_node);
6270 } else if (parse_current->data == CIL_KEY_CLASSCOMMON) {
6271 rc = cil_gen_classcommon(db, parse_current, new_ast_node);
6272 } else if (parse_current->data == CIL_KEY_SID) {
6273 rc = cil_gen_sid(db, parse_current, new_ast_node);
6274 } else if (parse_current->data == CIL_KEY_SIDCONTEXT) {
6275 rc = cil_gen_sidcontext(db, parse_current, new_ast_node);
6276 } else if (parse_current->data == CIL_KEY_SIDORDER) {
6277 rc = cil_gen_ordered(db, parse_current, new_ast_node, CIL_SIDORDER);
6278 } else if (parse_current->data == CIL_KEY_USER) {
6279 rc = cil_gen_user(db, parse_current, new_ast_node);
6280 } else if (parse_current->data == CIL_KEY_USERATTRIBUTE) {
6281 rc = cil_gen_userattribute(db, parse_current, new_ast_node);
6282 } else if (parse_current->data == CIL_KEY_USERATTRIBUTESET) {
6283 rc = cil_gen_userattributeset(db, parse_current, new_ast_node);
6284 } else if (parse_current->data == CIL_KEY_USERLEVEL) {
6285 rc = cil_gen_userlevel(db, parse_current, new_ast_node);
6286 } else if (parse_current->data == CIL_KEY_USERRANGE) {
6287 rc = cil_gen_userrange(db, parse_current, new_ast_node);
6288 } else if (parse_current->data == CIL_KEY_USERBOUNDS) {
6289 rc = cil_gen_bounds(db, parse_current, new_ast_node, CIL_USER);
6290 } else if (parse_current->data == CIL_KEY_USERPREFIX) {
6291 rc = cil_gen_userprefix(db, parse_current, new_ast_node);
6292 } else if (parse_current->data == CIL_KEY_SELINUXUSER) {
6293 rc = cil_gen_selinuxuser(db, parse_current, new_ast_node);
6294 } else if (parse_current->data == CIL_KEY_SELINUXUSERDEFAULT) {
6295 rc = cil_gen_selinuxuserdefault(db, parse_current, new_ast_node);
6296 } else if (parse_current->data == CIL_KEY_TYPE) {
6297 rc = cil_gen_type(db, parse_current, new_ast_node);
6298 } else if (parse_current->data == CIL_KEY_TYPEATTRIBUTE) {
6299 rc = cil_gen_typeattribute(db, parse_current, new_ast_node);
6300 } else if (parse_current->data == CIL_KEY_TYPEATTRIBUTESET) {
6301 rc = cil_gen_typeattributeset(db, parse_current, new_ast_node);
6302 } else if (parse_current->data == CIL_KEY_EXPANDTYPEATTRIBUTE) {
6303 rc = cil_gen_expandtypeattribute(db, parse_current, new_ast_node);
6304 } else if (parse_current->data == CIL_KEY_TYPEALIAS) {
6305 rc = cil_gen_alias(db, parse_current, new_ast_node, CIL_TYPEALIAS);
6306 } else if (parse_current->data == CIL_KEY_TYPEALIASACTUAL) {
6307 rc = cil_gen_aliasactual(db, parse_current, new_ast_node, CIL_TYPEALIASACTUAL);
6308 } else if (parse_current->data == CIL_KEY_TYPEBOUNDS) {
6309 rc = cil_gen_bounds(db, parse_current, new_ast_node, CIL_TYPE);
6310 } else if (parse_current->data == CIL_KEY_TYPEPERMISSIVE) {
6311 rc = cil_gen_typepermissive(db, parse_current, new_ast_node);
6312 } else if (parse_current->data == CIL_KEY_TYPENEVERAUDIT) {
6313 rc = cil_gen_typeneveraudit(db, parse_current, new_ast_node);
6314 } else if (parse_current->data == CIL_KEY_RANGETRANSITION) {
6315 rc = cil_gen_rangetransition(db, parse_current, new_ast_node);
6316 } else if (parse_current->data == CIL_KEY_ROLE) {
6317 rc = cil_gen_role(db, parse_current, new_ast_node);
6318 } else if (parse_current->data == CIL_KEY_USERROLE) {
6319 rc = cil_gen_userrole(db, parse_current, new_ast_node);
6320 } else if (parse_current->data == CIL_KEY_ROLETYPE) {
6321 rc = cil_gen_roletype(db, parse_current, new_ast_node);
6322 } else if (parse_current->data == CIL_KEY_ROLETRANSITION) {
6323 rc = cil_gen_roletransition(parse_current, new_ast_node);
6324 } else if (parse_current->data == CIL_KEY_ROLEALLOW) {
6325 rc = cil_gen_roleallow(db, parse_current, new_ast_node);
6326 } else if (parse_current->data == CIL_KEY_ROLEATTRIBUTE) {
6327 rc = cil_gen_roleattribute(db, parse_current, new_ast_node);
6328 } else if (parse_current->data == CIL_KEY_ROLEATTRIBUTESET) {
6329 rc = cil_gen_roleattributeset(db, parse_current, new_ast_node);
6330 } else if (parse_current->data == CIL_KEY_ROLEBOUNDS) {
6331 rc = cil_gen_bounds(db, parse_current, new_ast_node, CIL_ROLE);
6332 } else if (parse_current->data == CIL_KEY_BOOL) {
6333 rc = cil_gen_bool(db, parse_current, new_ast_node, CIL_FALSE);
6334 } else if (parse_current->data == CIL_KEY_BOOLEANIF) {
6335 rc = cil_gen_boolif(db, parse_current, new_ast_node, CIL_FALSE);
6336 } else if(parse_current->data == CIL_KEY_TUNABLE) {
6337 if (db->preserve_tunables) {
6338 rc = cil_gen_bool(db, parse_current, new_ast_node, CIL_TRUE);
6339 } else {
6340 rc = cil_gen_tunable(db, parse_current, new_ast_node);
6341 }
6342 } else if (parse_current->data == CIL_KEY_TUNABLEIF) {
6343 if (db->preserve_tunables) {
6344 rc = cil_gen_boolif(db, parse_current, new_ast_node, CIL_TRUE);
6345 } else {
6346 rc = cil_gen_tunif(db, parse_current, new_ast_node);
6347 }
6348 } else if (parse_current->data == CIL_KEY_CONDTRUE) {
6349 rc = cil_gen_condblock(db, parse_current, new_ast_node, CIL_CONDTRUE);
6350 } else if (parse_current->data == CIL_KEY_CONDFALSE) {
6351 rc = cil_gen_condblock(db, parse_current, new_ast_node, CIL_CONDFALSE);
6352 } else if (parse_current->data == CIL_KEY_ALLOW) {
6353 rc = cil_gen_avrule(parse_current, new_ast_node, CIL_AVRULE_ALLOWED);
6354 } else if (parse_current->data == CIL_KEY_AUDITALLOW) {
6355 rc = cil_gen_avrule(parse_current, new_ast_node, CIL_AVRULE_AUDITALLOW);
6356 } else if (parse_current->data == CIL_KEY_DONTAUDIT) {
6357 rc = cil_gen_avrule(parse_current, new_ast_node, CIL_AVRULE_DONTAUDIT);
6358 } else if (parse_current->data == CIL_KEY_NEVERALLOW) {
6359 rc = cil_gen_avrule(parse_current, new_ast_node, CIL_AVRULE_NEVERALLOW);
6360 } else if (parse_current->data == CIL_KEY_ALLOWX) {
6361 rc = cil_gen_avrulex(parse_current, new_ast_node, CIL_AVRULE_ALLOWED);
6362 } else if (parse_current->data == CIL_KEY_AUDITALLOWX) {
6363 rc = cil_gen_avrulex(parse_current, new_ast_node, CIL_AVRULE_AUDITALLOW);
6364 } else if (parse_current->data == CIL_KEY_DONTAUDITX) {
6365 rc = cil_gen_avrulex(parse_current, new_ast_node, CIL_AVRULE_DONTAUDIT);
6366 } else if (parse_current->data == CIL_KEY_NEVERALLOWX) {
6367 rc = cil_gen_avrulex(parse_current, new_ast_node, CIL_AVRULE_NEVERALLOW);
6368 } else if (parse_current->data == CIL_KEY_PERMISSIONX) {
6369 rc = cil_gen_permissionx(db, parse_current, new_ast_node);
6370 } else if (parse_current->data == CIL_KEY_DENY_RULE) {
6371 rc = cil_gen_deny_rule(parse_current, new_ast_node);
6372 } else if (parse_current->data == CIL_KEY_TYPETRANSITION) {
6373 rc = cil_gen_typetransition(db, parse_current, new_ast_node);
6374 } else if (parse_current->data == CIL_KEY_TYPECHANGE) {
6375 rc = cil_gen_type_rule(parse_current, new_ast_node, CIL_TYPE_CHANGE);
6376 } else if (parse_current->data == CIL_KEY_TYPEMEMBER) {
6377 rc = cil_gen_type_rule(parse_current, new_ast_node, CIL_TYPE_MEMBER);
6378 } else if (parse_current->data == CIL_KEY_SENSITIVITY) {
6379 rc = cil_gen_sensitivity(db, parse_current, new_ast_node);
6380 } else if (parse_current->data == CIL_KEY_SENSALIAS) {
6381 rc = cil_gen_alias(db, parse_current, new_ast_node, CIL_SENSALIAS);
6382 } else if (parse_current->data == CIL_KEY_SENSALIASACTUAL) {
6383 rc = cil_gen_aliasactual(db, parse_current, new_ast_node, CIL_SENSALIASACTUAL);
6384 } else if (parse_current->data == CIL_KEY_CATEGORY) {
6385 rc = cil_gen_category(db, parse_current, new_ast_node);
6386 } else if (parse_current->data == CIL_KEY_CATALIAS) {
6387 rc = cil_gen_alias(db, parse_current, new_ast_node, CIL_CATALIAS);
6388 } else if (parse_current->data == CIL_KEY_CATALIASACTUAL) {
6389 rc = cil_gen_aliasactual(db, parse_current, new_ast_node, CIL_CATALIASACTUAL);
6390 } else if (parse_current->data == CIL_KEY_CATSET) {
6391 rc = cil_gen_catset(db, parse_current, new_ast_node);
6392 } else if (parse_current->data == CIL_KEY_CATORDER) {
6393 rc = cil_gen_ordered(db, parse_current, new_ast_node, CIL_CATORDER);
6394 } else if (parse_current->data == CIL_KEY_SENSITIVITYORDER) {
6395 rc = cil_gen_ordered(db, parse_current, new_ast_node, CIL_SENSITIVITYORDER);
6396 } else if (parse_current->data == CIL_KEY_SENSCAT) {
6397 rc = cil_gen_senscat(db, parse_current, new_ast_node);
6398 } else if (parse_current->data == CIL_KEY_LEVEL) {
6399 rc = cil_gen_level(db, parse_current, new_ast_node);
6400 } else if (parse_current->data == CIL_KEY_LEVELRANGE) {
6401 rc = cil_gen_levelrange(db, parse_current, new_ast_node);
6402 } else if (parse_current->data == CIL_KEY_CONSTRAIN) {
6403 rc = cil_gen_constrain(db, parse_current, new_ast_node, CIL_CONSTRAIN);
6404 } else if (parse_current->data == CIL_KEY_MLSCONSTRAIN) {
6405 rc = cil_gen_constrain(db, parse_current, new_ast_node, CIL_MLSCONSTRAIN);
6406 } else if (parse_current->data == CIL_KEY_VALIDATETRANS) {
6407 rc = cil_gen_validatetrans(db, parse_current, new_ast_node, CIL_VALIDATETRANS);
6408 } else if (parse_current->data == CIL_KEY_MLSVALIDATETRANS) {
6409 rc = cil_gen_validatetrans(db, parse_current, new_ast_node, CIL_MLSVALIDATETRANS);
6410 } else if (parse_current->data == CIL_KEY_CONTEXT) {
6411 rc = cil_gen_context(db, parse_current, new_ast_node);
6412 } else if (parse_current->data == CIL_KEY_FILECON) {
6413 rc = cil_gen_filecon(db, parse_current, new_ast_node);
6414 } else if (parse_current->data == CIL_KEY_IBPKEYCON) {
6415 rc = cil_gen_ibpkeycon(db, parse_current, new_ast_node);
6416 } else if (parse_current->data == CIL_KEY_IBENDPORTCON) {
6417 rc = cil_gen_ibendportcon(db, parse_current, new_ast_node);
6418 } else if (parse_current->data == CIL_KEY_PORTCON) {
6419 rc = cil_gen_portcon(db, parse_current, new_ast_node);
6420 } else if (parse_current->data == CIL_KEY_NODECON) {
6421 rc = cil_gen_nodecon(db, parse_current, new_ast_node);
6422 } else if (parse_current->data == CIL_KEY_GENFSCON) {
6423 rc = cil_gen_genfscon(db, parse_current, new_ast_node);
6424 } else if (parse_current->data == CIL_KEY_NETIFCON) {
6425 rc = cil_gen_netifcon(db, parse_current, new_ast_node);
6426 } else if (parse_current->data == CIL_KEY_PIRQCON) {
6427 rc = cil_gen_pirqcon(db, parse_current, new_ast_node);
6428 } else if (parse_current->data == CIL_KEY_IOMEMCON) {
6429 rc = cil_gen_iomemcon(db, parse_current, new_ast_node);
6430 } else if (parse_current->data == CIL_KEY_IOPORTCON) {
6431 rc = cil_gen_ioportcon(db, parse_current, new_ast_node);
6432 } else if (parse_current->data == CIL_KEY_PCIDEVICECON) {
6433 rc = cil_gen_pcidevicecon(db, parse_current, new_ast_node);
6434 } else if (parse_current->data == CIL_KEY_DEVICETREECON) {
6435 rc = cil_gen_devicetreecon(db, parse_current, new_ast_node);
6436 } else if (parse_current->data == CIL_KEY_FSUSE) {
6437 rc = cil_gen_fsuse(db, parse_current, new_ast_node);
6438 } else if (parse_current->data == CIL_KEY_MACRO) {
6439 rc = cil_gen_macro(db, parse_current, new_ast_node);
6440 } else if (parse_current->data == CIL_KEY_CALL) {
6441 rc = cil_gen_call(db, parse_current, new_ast_node);
6442 } else if (parse_current->data == CIL_KEY_POLICYCAP) {
6443 rc = cil_gen_policycap(db, parse_current, new_ast_node);
6444 } else if (parse_current->data == CIL_KEY_OPTIONAL) {
6445 rc = cil_gen_optional(db, parse_current, new_ast_node);
6446 } else if (parse_current->data == CIL_KEY_IPADDR) {
6447 rc = cil_gen_ipaddr(db, parse_current, new_ast_node);
6448 } else if (parse_current->data == CIL_KEY_DEFAULTUSER) {
6449 rc = cil_gen_default(parse_current, new_ast_node, CIL_DEFAULTUSER);
6450 } else if (parse_current->data == CIL_KEY_DEFAULTROLE) {
6451 rc = cil_gen_default(parse_current, new_ast_node, CIL_DEFAULTROLE);
6452 } else if (parse_current->data == CIL_KEY_DEFAULTTYPE) {
6453 rc = cil_gen_default(parse_current, new_ast_node, CIL_DEFAULTTYPE);
6454 } else if (parse_current->data == CIL_KEY_DEFAULTRANGE) {
6455 rc = cil_gen_defaultrange(parse_current, new_ast_node);
6456 } else if (parse_current->data == CIL_KEY_HANDLEUNKNOWN) {
6457 rc = cil_gen_handleunknown(parse_current, new_ast_node);
6458 } else if (parse_current->data == CIL_KEY_MLS) {
6459 rc = cil_gen_mls(parse_current, new_ast_node);
6460 } else if (parse_current->data == CIL_KEY_SRC_INFO) {
6461 rc = cil_gen_src_info(parse_current, new_ast_node);
6462 } else {
6463 cil_log(CIL_ERR, "Error: Unknown keyword %s\n", (char *)parse_current->data);
6464 rc = SEPOL_ERR;
6465 }
6466
6467 if (rc == SEPOL_OK) {
6468 if (ast_parent->cl_head == NULL) {
6469 ast_parent->cl_head = new_ast_node;
6470 } else {
6471 ast_parent->cl_tail->next = new_ast_node;
6472 }
6473 ast_parent->cl_tail = new_ast_node;
6474 } else {
6475 cil_tree_node_destroy(&new_ast_node);
6476 new_ast_node = NULL;
6477 }
6478
6479 return new_ast_node;
6480 }
6481
6482 static int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *finished, void *extra_args)
6483 {
6484 struct cil_args_build *args = extra_args;
6485 struct cil_tree_node *new_ast_node = NULL;
6486 int rc = SEPOL_ERR;
6487
6488 if (parse_current->parent->cl_head != parse_current) {
6489 /* ignore anything that isn't following a parenthesis */
6490 return SEPOL_OK;
6491 } else if (parse_current->data == NULL) {
6492 /* the only time parenthesis can immediately following parenthesis is if
6493 * the parent is the root node */
6494 if (parse_current->parent->parent == NULL) {
6495 return SEPOL_OK;
6496 } else {
6497 cil_tree_log(parse_current, CIL_ERR, "Keyword expected after open parenthesis");
6498 return SEPOL_ERR;
6499 }
6500 }
6501
6502 rc = check_for_illegal_statement(parse_current, args);
6503 if (rc != SEPOL_OK) {
6504 return SEPOL_ERR;
6505 }
6506
6507 new_ast_node = parse_statement(args->db, parse_current, args->ast);
6508 if (!new_ast_node) {
6509 return SEPOL_ERR;
6510 }
6511
6512 args->ast = new_ast_node;
6513
6514 if (parse_current->data != CIL_KEY_BLOCK &&
6515 parse_current->data != CIL_KEY_IN &&
6516 parse_current->data != CIL_KEY_TUNABLEIF &&
6517 parse_current->data != CIL_KEY_BOOLEANIF &&
6518 parse_current->data != CIL_KEY_CONDTRUE &&
6519 parse_current->data != CIL_KEY_CONDFALSE &&
6520 parse_current->data != CIL_KEY_MACRO &&
6521 parse_current->data != CIL_KEY_OPTIONAL &&
6522 parse_current->data != CIL_KEY_SRC_INFO) {
6523 /* Skip anything that does not contain a list of policy statements */
6524 *finished = CIL_TREE_SKIP_NEXT;
6525 }
6526
6527 return SEPOL_OK;
6528 }
6529
6530 static int __cil_build_ast_first_child_helper(__attribute__((unused)) struct cil_tree_node *parse_current, void *extra_args)
6531 {
6532 struct cil_args_build *args = extra_args;
6533 struct cil_tree_node *ast = args->ast;
6534
6535 if (ast->flavor == CIL_TUNABLEIF) {
6536 args->tunif = ast;
6537 } else if (ast->flavor == CIL_IN) {
6538 args->in = ast;
6539 } else if (ast->flavor == CIL_MACRO) {
6540 args->macro = ast;
6541 } else if (ast->flavor == CIL_OPTIONAL) {
6542 args->optional = ast;
6543 } else if (ast->flavor == CIL_BOOLEANIF) {
6544 args->boolif = ast;
6545 }
6546
6547 return SEPOL_OK;
6548 }
6549
6550 static int __cil_build_ast_last_child_helper(struct cil_tree_node *parse_current, void *extra_args)
6551 {
6552 struct cil_args_build *args = extra_args;
6553 struct cil_tree_node *ast = args->ast;
6554
6555 if (ast->flavor == CIL_ROOT) {
6556 return SEPOL_OK;
6557 }
6558
6559 args->ast = ast->parent;
6560
6561 if (ast->flavor == CIL_TUNABLEIF) {
6562 args->tunif = NULL;
6563 }
6564
6565 if (ast->flavor == CIL_IN) {
6566 args->in = NULL;
6567 }
6568
6569 if (ast->flavor == CIL_MACRO) {
6570 args->macro = NULL;
6571 }
6572
6573 if (ast->flavor == CIL_OPTIONAL) {
6574 struct cil_tree_node *n = ast->parent;
6575 args->optional = NULL;
6576 /* Optionals can be nested */
6577 while (n && n->flavor != CIL_ROOT) {
6578 if (n->flavor == CIL_OPTIONAL) {
6579 args->optional = n;
6580 break;
6581 }
6582 n = n->parent;
6583 }
6584 }
6585
6586 if (ast->flavor == CIL_BOOLEANIF) {
6587 args->boolif = NULL;
6588 }
6589
6590 // At this point we no longer have any need for parse_current or any of its
6591 // siblings; they have all been converted to the appropriate AST node. The
6592 // full parse tree will get deleted elsewhere, but in an attempt to
6593 // minimize memory usage (of which the parse tree uses a lot), start
6594 // deleting the parts we don't need now.
6595 cil_tree_children_destroy(parse_current->parent);
6596
6597 return SEPOL_OK;
6598 }
6599
6600 int cil_build_ast(struct cil_db *db, struct cil_tree_node *parse_tree, struct cil_tree_node *ast)
6601 {
6602 int rc = SEPOL_ERR;
6603 struct cil_args_build extra_args;
6604
6605 if (db == NULL || parse_tree == NULL || ast == NULL) {
6606 goto exit;
6607 }
6608
6609 extra_args.ast = ast;
6610 extra_args.db = db;
6611 extra_args.tunif = NULL;
6612 extra_args.in = NULL;
6613 extra_args.macro = NULL;
6614 extra_args.optional = NULL;
6615 extra_args.boolif = NULL;
6616
6617 rc = cil_tree_walk(parse_tree, __cil_build_ast_node_helper, __cil_build_ast_first_child_helper, __cil_build_ast_last_child_helper, &extra_args);
6618 if (rc != SEPOL_OK) {
6619 goto exit;
6620 }
6621
6622 return SEPOL_OK;
6623
6624 exit:
6625 return rc;
6626 }