Debian Codegraph @

cil/src/cil_write_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 <stdio.h>
31 #include <stdint.h>
32 #include <inttypes.h>
33 #include <ctype.h>
34
35 #include "cil_internal.h"
36 #include "cil_flavor.h"
37 #include "cil_list.h"
38 #include "cil_log.h"
39 #include "cil_symtab.h"
40 #include "cil_tree.h"
41 #include "cil_write_ast.h"
42
43
44 static inline const char *datum_or_str(struct cil_symtab_datum *datum, const char *str)
45 {
46 return datum && datum->fqn ? datum->fqn : str;
47 }
48
49 static inline const char *datum_to_str(struct cil_symtab_datum *datum)
50 {
51 return datum ? datum->fqn : "<?DATUM>";
52 }
53
54 static void write_expr(FILE *out, struct cil_list *expr)
55 {
56 struct cil_list_item *curr;
57 int notfirst = 0;
58
59 fprintf(out, "(");
60 cil_list_for_each(curr, expr) {
61 if (notfirst)
62 fprintf(out, " ");
63 else
64 notfirst = 1;
65 switch (curr->flavor) {
66 case CIL_LIST:
67 write_expr(out, curr->data);
68 break;
69 case CIL_STRING:
70 fprintf(out, "%s", (char *)curr->data);
71 break;
72 case CIL_DATUM:
73 case CIL_TYPE:
74 case CIL_ROLE:
75 case CIL_USER:
76 case CIL_SENS:
77 case CIL_CAT:
78 case CIL_BOOL:
79 case CIL_CLASS:
80 case CIL_MAP_CLASS:
81 case CIL_DECLARED_STRING:
82 fprintf(out, "%s", datum_to_str(curr->data));
83 break;
84 case CIL_OP: {
85 const char *op_str;
86 enum cil_flavor op_flavor = (enum cil_flavor)(uintptr_t)curr->data;
87 switch (op_flavor) {
88 case CIL_AND:
89 op_str = CIL_KEY_AND;
90 break;
91 case CIL_OR:
92 op_str = CIL_KEY_OR;
93 break;
94 case CIL_NOT:
95 op_str = CIL_KEY_NOT;
96 break;
97 case CIL_ALL:
98 op_str = CIL_KEY_ALL;
99 break;
100 case CIL_EQ:
101 op_str = CIL_KEY_EQ;
102 break;
103 case CIL_NEQ:
104 op_str = CIL_KEY_NEQ;
105 break;
106 case CIL_XOR:
107 op_str = CIL_KEY_XOR;
108 break;
109 case CIL_RANGE:
110 op_str = CIL_KEY_RANGE;
111 break;
112 case CIL_CONS_DOM:
113 op_str = CIL_KEY_CONS_DOM;
114 break;
115 case CIL_CONS_DOMBY:
116 op_str = CIL_KEY_CONS_DOMBY;
117 break;
118 case CIL_CONS_INCOMP:
119 op_str = CIL_KEY_CONS_INCOMP;
120 break;
121 default:
122 op_str = "<?OP>";
123 break;
124 }
125 fprintf(out, "%s", op_str);
126 break;
127 }
128 case CIL_CONS_OPERAND: {
129 const char *operand_str;
130 enum cil_flavor operand_flavor = (enum cil_flavor)(uintptr_t)curr->data;
131 switch (operand_flavor) {
132 case CIL_CONS_U1:
133 operand_str = CIL_KEY_CONS_U1;
134 break;
135 case CIL_CONS_U2:
136 operand_str = CIL_KEY_CONS_U2;
137 break;
138 case CIL_CONS_U3:
139 operand_str = CIL_KEY_CONS_U3;
140 break;
141 case CIL_CONS_T1:
142 operand_str = CIL_KEY_CONS_T1;
143 break;
144 case CIL_CONS_T2:
145 operand_str = CIL_KEY_CONS_T2;
146 break;
147 case CIL_CONS_T3:
148 operand_str = CIL_KEY_CONS_T3;
149 break;
150 case CIL_CONS_R1:
151 operand_str = CIL_KEY_CONS_R1;
152 break;
153 case CIL_CONS_R2:
154 operand_str = CIL_KEY_CONS_R2;
155 break;
156 case CIL_CONS_R3:
157 operand_str = CIL_KEY_CONS_R3;
158 break;
159 case CIL_CONS_L1:
160 operand_str = CIL_KEY_CONS_L1;
161 break;
162 case CIL_CONS_L2:
163 operand_str = CIL_KEY_CONS_L2;
164 break;
165 case CIL_CONS_H1:
166 operand_str = CIL_KEY_CONS_H1;
167 break;
168 case CIL_CONS_H2:
169 operand_str = CIL_KEY_CONS_H2;
170 break;
171 default:
172 operand_str = "<?OPERAND>";
173 break;
174 }
175 fprintf(out, "%s", operand_str);
176 break;
177 }
178 default:
179 fprintf(out, "<?FLAVOR>");
180 break;
181 }
182 }
183 fprintf(out, ")");
184 }
185
186 static void write_node_list(FILE *out, struct cil_tree_node *current)
187 {
188 int notfirst = 0;
189
190 fprintf(out, "(");
191 while (current) {
192 if (notfirst)
193 fprintf(out, " ");
194 else
195 notfirst = 1;
196
197 fprintf(out, "%s", datum_to_str(current->data));
198 current = current->next;
199 }
200 fprintf(out, ")");
201 }
202
203 static void write_string_list(FILE *out, struct cil_list *list)
204 {
205 struct cil_list_item *curr;
206 int notfirst = 0;
207
208 if (!list) {
209 fprintf(out, "()");
210 return;
211 }
212
213 fprintf(out, "(");
214 cil_list_for_each(curr, list) {
215 if (notfirst)
216 fprintf(out, " ");
217 else
218 notfirst = 1;
219 fprintf(out, "%s", (char*)curr->data);
220 }
221 fprintf(out, ")");
222 }
223
224 static void write_datum_list(FILE *out, struct cil_list *list)
225 {
226 struct cil_list_item *curr;
227 int notfirst = 0;
228
229 if (!list) {
230 fprintf(out, "()");
231 return;
232 }
233
234 fprintf(out, "(");
235 cil_list_for_each(curr, list) {
236 if (notfirst)
237 fprintf(out, " ");
238 else
239 notfirst = 1;
240 fprintf(out, "%s", datum_to_str(curr->data));
241 }
242 fprintf(out, ")");
243 }
244
245 static void write_classperms(FILE *out, struct cil_classperms *cp)
246 {
247 if (!cp) {
248 fprintf(out, "()");
249 return;
250 }
251
252 fprintf(out, "(%s ", datum_or_str(DATUM(cp->class), cp->class_str));
253 if (cp->perms)
254 write_expr(out, cp->perms);
255 else
256 write_expr(out, cp->perm_strs);
257 fprintf(out, ")");
258 }
259
260 static void write_classperms_list(FILE *out, struct cil_list *cp_list)
261 {
262 struct cil_list_item *curr;
263 int notfirst = 0;
264 int num = 0;
265
266 if (!cp_list) {
267 fprintf(out, "()");
268 return;
269 }
270
271 cil_list_for_each(curr, cp_list) {
272 num++;
273 }
274 if (num > 1)
275 fprintf(out, "(");
276 cil_list_for_each(curr, cp_list) {
277 if (notfirst)
278 fprintf(out, " ");
279 else
280 notfirst = 1;
281 if (curr->flavor == CIL_CLASSPERMS) {
282 write_classperms(out, curr->data);
283 } else {
284 struct cil_classperms_set *cp_set = curr->data;
285 struct cil_classpermission *cp = cp_set->set;
286 if (cp) {
287 if (cp->datum.name)
288 fprintf(out, "%s", datum_to_str(DATUM(cp)));
289 else
290 write_classperms_list(out,cp->classperms);
291 } else {
292 fprintf(out, "%s", cp_set->set_str);
293 }
294 }
295 }
296 if (num > 1)
297 fprintf(out, ")");
298 }
299
300 static void write_permx(FILE *out, struct cil_permissionx *permx)
301 {
302 if (permx->datum.name) {
303 fprintf(out, "%s", datum_to_str(DATUM(permx)));
304 } else {
305 fprintf(out, "(");
306 if (permx->kind == CIL_PERMX_KIND_IOCTL) {
307 fprintf(out, "ioctl ");
308 } else if (permx->kind == CIL_PERMX_KIND_NLMSG) {
309 fprintf(out, "nlmsg ");
310 } else {
311 fprintf(out, "<?KIND> ");
312 }
313 fprintf(out, "%s ", datum_or_str(DATUM(permx->obj), permx->obj_str));
314 write_expr(out, permx->expr_str);
315 fprintf(out, ")");
316 }
317 }
318
319 static void write_cats(FILE *out, struct cil_cats *cats)
320 {
321 if (cats->datum_expr) {
322 write_expr(out, cats->datum_expr);
323 } else {
324 write_expr(out, cats->str_expr);
325 }
326 }
327
328 static void write_level(FILE *out, struct cil_level *level, int print_name)
329 {
330 if (print_name && level->datum.name) {
331 fprintf(out, "%s", datum_to_str(DATUM(level)));
332 } else {
333 fprintf(out, "(");
334 fprintf(out, "%s", datum_or_str(DATUM(level->sens), level->sens_str));
335 if (level->cats) {
336 fprintf(out, " ");
337 write_cats(out, level->cats);
338 }
339 fprintf(out, ")");
340 }
341 }
342
343 static void write_range(FILE *out, struct cil_levelrange *range, int print_name)
344 {
345 if (print_name && range->datum.name) {
346 fprintf(out, "%s", datum_to_str(DATUM(range)));
347 } else {
348 fprintf(out, "(");
349 if (range->low)
350 write_level(out, range->low, CIL_TRUE);
351 else
352 fprintf(out, "%s", range->low_str);
353 fprintf(out, " ");
354 if (range->high)
355 write_level(out, range->high, CIL_TRUE);
356 else
357 fprintf(out, "%s", range->high_str);
358 fprintf(out, ")");
359 }
360 }
361
362 static void write_context(FILE *out, struct cil_context *context, int print_name)
363 {
364 if (print_name && context->datum.name) {
365 fprintf(out, "%s", datum_to_str(DATUM(context)));
366 } else {
367 fprintf(out, "(");
368 fprintf(out, "%s ", datum_or_str(DATUM(context->user), context->user_str));
369 fprintf(out, "%s ", datum_or_str(DATUM(context->role), context->role_str));
370 fprintf(out, "%s ", datum_or_str(DATUM(context->type), context->type_str));
371 if (context->range)
372 write_range(out, context->range, CIL_TRUE);
373 else
374 fprintf(out, "%s", context->range_str);
375 fprintf(out, ")");
376 }
377 }
378
379 static void write_ipaddr(FILE *out, struct cil_ipaddr *ipaddr)
380 {
381 if (ipaddr->datum.name) {
382 fprintf(out, "%s", datum_to_str(DATUM(ipaddr)));
383 } else {
384 char buf[256];
385 if (inet_ntop(ipaddr->family, &ipaddr->ip, buf, 256) == NULL)
386 strcpy(buf, "<?IPADDR>");
387 fprintf(out, "(%s)", buf);
388 }
389 }
390
391 static void write_constrain(FILE *out, struct cil_constrain *cons)
392 {
393 write_classperms_list(out, cons->classperms);
394 fprintf(out, " ");
395 if (cons->datum_expr)
396 write_expr(out, cons->datum_expr);
397 else
398 write_expr(out, cons->str_expr);
399 }
400
401 static void write_call_args(FILE *out, struct cil_list *args)
402 {
403 struct cil_list_item *item;
404 int notfirst = 0;
405
406 fprintf(out, "(");
407 cil_list_for_each(item, args) {
408 struct cil_args* arg = item->data;
409 enum cil_flavor arg_flavor = arg->flavor;
410 if (notfirst)
411 fprintf(out, " ");
412 else
413 notfirst = 1;
414 switch (arg_flavor) {
415 case CIL_TYPE:
416 case CIL_ROLE:
417 case CIL_USER:
418 case CIL_SENS:
419 case CIL_CAT:
420 case CIL_BOOL:
421 case CIL_CLASS:
422 case CIL_MAP_CLASS: {
423 fprintf(out, "%s", datum_or_str(DATUM(arg->arg), arg->arg_str));
424 break;
425 }
426 case CIL_DECLARED_STRING: {
427 if (arg->arg) {
428 fprintf(out, "\"%s\" ", DATUM(arg->arg)->fqn);
429 } else {
430 fprintf(out, "%s ", arg->arg_str);
431 }
432 break;
433 }
434 case CIL_CATSET: {
435 if (arg->arg) {
436 struct cil_catset *catset = (struct cil_catset *)arg->arg;
437 write_cats(out, catset->cats);
438 } else {
439 fprintf(out, "%s", arg->arg_str);
440 }
441 break;
442 }
443 case CIL_LEVEL: {
444 if (arg->arg) {
445 struct cil_level *level = (struct cil_level *)arg->arg;
446 write_level(out, level, CIL_TRUE);
447 } else {
448 fprintf(out, "%s", arg->arg_str);
449 }
450 break;
451 }
452 case CIL_LEVELRANGE: {
453 if (arg->arg) {
454 struct cil_levelrange *range = (struct cil_levelrange *)arg->arg;
455 write_range(out, range, CIL_TRUE);
456 } else {
457 fprintf(out, "%s", arg->arg_str);
458 }
459 break;
460 }
461 case CIL_IPADDR: {
462 if (arg->arg) {
463 struct cil_ipaddr *addr = (struct cil_ipaddr *)arg->arg;
464 write_ipaddr(out, addr);
465 } else {
466 fprintf(out, "%s", arg->arg_str);
467 }
468 break;
469 }
470 case CIL_CLASSPERMISSION: {
471 if (arg->arg) {
472 struct cil_classpermission *cp = (struct cil_classpermission *)arg->arg;
473 if (cp->datum.name)
474 fprintf(out, "%s", datum_to_str(DATUM(cp)));
475 else
476 write_classperms_list(out, cp->classperms);
477 } else {
478 fprintf(out, "%s", arg->arg_str);
479 }
480 break;
481 }
482 default:
483 fprintf(out, "<?ARG:%s>", datum_or_str(DATUM(arg->arg), arg->arg_str));
484 break;
485 }
486 }
487 fprintf(out, ")");
488 }
489
490 static void write_call_args_tree(FILE *out, struct cil_tree_node *arg_node)
491 {
492 while (arg_node) {
493 if (arg_node->data) {
494 fprintf(out, "%s", (char *)arg_node->data);
495 } else if (arg_node->cl_head) {
496 fprintf(out, "(");
497 write_call_args_tree(out, arg_node->cl_head);
498 fprintf(out, ")");
499 }
500 if (arg_node->next)
501 fprintf(out, " ");
502 arg_node = arg_node->next;
503 }
504 }
505
506 static const char *macro_param_flavor_to_string(enum cil_flavor flavor)
507 {
508 const char *str;
509 switch(flavor) {
510 case CIL_TYPE:
511 str = CIL_KEY_TYPE;
512 break;
513 case CIL_ROLE:
514 str = CIL_KEY_ROLE;
515 break;
516 case CIL_USER:
517 str = CIL_KEY_USER;
518 break;
519 case CIL_SENS:
520 str = CIL_KEY_SENSITIVITY;
521 break;
522 case CIL_CAT:
523 str = CIL_KEY_CATEGORY;
524 break;
525 case CIL_CATSET:
526 str = CIL_KEY_CATSET;
527 break;
528 case CIL_LEVEL:
529 str = CIL_KEY_LEVEL;
530 break;
531 case CIL_LEVELRANGE:
532 str = CIL_KEY_LEVELRANGE;
533 break;
534 case CIL_CLASS:
535 str = CIL_KEY_CLASS;
536 break;
537 case CIL_IPADDR:
538 str = CIL_KEY_IPADDR;
539 break;
540 case CIL_MAP_CLASS:
541 str = CIL_KEY_MAP_CLASS;
542 break;
543 case CIL_CLASSPERMISSION:
544 str = CIL_KEY_CLASSPERMISSION;
545 break;
546 case CIL_BOOL:
547 str = CIL_KEY_BOOL;
548 break;
549 case CIL_DECLARED_STRING:
550 str = CIL_KEY_STRING;
551 break;
552 default:
553 str = "<?FLAVOR>";
554 break;
555 }
556 return str;
557 }
558
559 static void cil_write_src_info_node(FILE *out, struct cil_tree_node *node)
560 {
561 struct cil_src_info *info = node->data;
562 if (info->kind == CIL_KEY_SRC_CIL || info->kind == CIL_KEY_SRC_HLL_LMS) {
563 fprintf(out, ";;* lms %u %s\n", info->hll_line, info->path);
564 } else if (info->kind == CIL_KEY_SRC_HLL_LMX) {
565 fprintf(out, ";;* lmx %u %s\n", info->hll_line, info->path);
566 } else {
567 fprintf(out, ";;* <?SRC_INFO_KIND> %u %s\n", info->hll_line, info->path);
568 }
569 }
570
571 void cil_write_ast_node(FILE *out, struct cil_tree_node *node)
572 {
573 if (!node->data) {
574 return;
575 }
576
577 switch(node->flavor) {
578 case CIL_NODE: {
579 fprintf(out, "%s\n", (char *)node->data);
580 break;
581 }
582 case CIL_BLOCK: {
583 struct cil_block *block = node->data;
584 fprintf(out, "(block %s", datum_to_str(DATUM(block)));
585 if (!node->cl_head)
586 fprintf(out, ")");
587 fprintf(out, "\n");
588 break;
589 }
590 case CIL_BLOCKINHERIT: {
591 struct cil_blockinherit *inherit = node->data;
592 fprintf(out, "(blockinherit %s)\n", datum_or_str(DATUM(inherit->block), inherit->block_str));
593 break;
594 }
595 case CIL_IN: {
596 struct cil_in *in = node->data;
597 fprintf(out, "(in %s", datum_or_str(DATUM(in->block), in->block_str));
598 if (!node->cl_head)
599 fprintf(out, ")");
600 fprintf(out, "\n");
601 break;
602 }
603 case CIL_OPTIONAL: {
604 struct cil_optional *optional = node->data;
605 fprintf(out, "(optional %s", datum_to_str(DATUM(optional)));
606 if (!node->cl_head)
607 fprintf(out, ")");
608 fprintf(out, "\n");
609 break;
610 }
611 case CIL_BOOLEANIF: {
612 struct cil_booleanif *bif = node->data;
613 fprintf(out, "(booleanif ");
614 if (bif->datum_expr)
615 write_expr(out, bif->datum_expr);
616 else
617 write_expr(out, bif->str_expr);
618 if (!node->cl_head)
619 fprintf(out, ")");
620 fprintf(out, "\n");
621 break;
622 }
623 case CIL_TUNABLEIF: {
624 struct cil_tunableif *tif = node->data;
625 fprintf(out, "(tunableif ");
626 if (tif->datum_expr)
627 write_expr(out, tif->datum_expr);
628 else
629 write_expr(out, tif->str_expr);
630 if (!node->cl_head)
631 fprintf(out, ")");
632 fprintf(out, "\n");
633 break;
634 }
635 case CIL_CONDBLOCK: {
636 struct cil_condblock *cb = node->data;
637 fprintf(out, "(%s", cb->flavor == CIL_CONDTRUE ? "true" : "false");
638 if (!node->cl_head)
639 fprintf(out, ")");
640 fprintf(out, "\n");
641 break;
642 }
643 case CIL_MACRO: {
644 struct cil_macro *macro = node->data;
645 struct cil_list_item *curr;
646 fprintf(out, "(macro %s (", datum_to_str(DATUM(macro)));
647 if (macro->params) {
648 cil_list_for_each(curr, macro->params) {
649 struct cil_param *param = curr->data;
650 fprintf(out, "(%s %s)", macro_param_flavor_to_string(param->flavor), param->str);
651 }
652 }
653 fprintf(out, ")");
654 if (!node->cl_head)
655 fprintf(out, ")");
656 fprintf(out, "\n");
657 break;
658 }
659 case CIL_CALL: {
660 struct cil_call *call = node->data;
661 fprintf(out, "(call %s", datum_or_str(DATUM(call->macro), call->macro_str));
662 if (call->args) {
663 fprintf(out, " ");
664 write_call_args(out, call->args);
665 } else if (call->args_tree) {
666 fprintf(out, " ");
667 write_call_args_tree(out, call->args_tree->root);
668 }
669 if (!node->cl_head)
670 fprintf(out, ")");
671 fprintf(out, "\n");
672 break;
673 }
674 case CIL_BLOCKABSTRACT: {
675 struct cil_blockabstract *abstract = node->data;
676 fprintf(out, "(blockabstract %s)\n", datum_or_str(DATUM(abstract->block), abstract->block_str));
677 break;
678 }
679 case CIL_MLS: {
680 struct cil_mls *mls = node->data;
681 fprintf(out, "(mls %s)\n", mls->value ? "true" : "false");
682 break;
683 }
684 case CIL_HANDLEUNKNOWN: {
685 struct cil_handleunknown *unknown = node->data;
686 fprintf(out, "(handleunknown ");
687 if (unknown->handle_unknown == SEPOL_ALLOW_UNKNOWN)
688 fprintf(out, "%s", CIL_KEY_HANDLEUNKNOWN_ALLOW);
689 else if (unknown->handle_unknown == SEPOL_DENY_UNKNOWN)
690 fprintf(out, "%s", CIL_KEY_HANDLEUNKNOWN_DENY);
691 else if (unknown->handle_unknown == SEPOL_REJECT_UNKNOWN)
692 fprintf(out, "%s", CIL_KEY_HANDLEUNKNOWN_REJECT);
693 else
694 fprintf(out, "<?UNKNOWN>");
695 fprintf(out, ")\n");
696 break;
697 }
698 case CIL_DEFAULTUSER: {
699 struct cil_default *def = node->data;
700 fprintf(out, "(defaultuser ");
701 if (def->class_datums)
702 write_datum_list(out, def->class_datums);
703 else
704 write_string_list(out, def->class_strs);
705 if (def->object == CIL_DEFAULT_SOURCE)
706 fprintf(out, " source");
707 else if (def->object == CIL_DEFAULT_TARGET)
708 fprintf(out, " target");
709 else
710 fprintf(out, " <?DEFAULT>");
711 fprintf(out, ")\n");
712 break;
713 }
714 case CIL_DEFAULTROLE: {
715 struct cil_default *def = node->data;
716 fprintf(out, "(defaultrole ");
717 if (def->class_datums)
718 write_datum_list(out, def->class_datums);
719 else
720 write_string_list(out, def->class_strs);
721 if (def->object == CIL_DEFAULT_SOURCE)
722 fprintf(out, " source");
723 else if (def->object == CIL_DEFAULT_TARGET)
724 fprintf(out, " target");
725 else
726 fprintf(out, " <?DEFAULT>");
727 fprintf(out, ")\n");
728 break;
729 }
730 case CIL_DEFAULTTYPE: {
731 struct cil_default *def = node->data;
732 fprintf(out, "(defaulttype ");
733 if (def->class_datums)
734 write_datum_list(out, def->class_datums);
735 else
736 write_string_list(out, def->class_strs);
737 if (def->object == CIL_DEFAULT_SOURCE)
738 fprintf(out, " source");
739 else if (def->object == CIL_DEFAULT_TARGET)
740 fprintf(out, " target");
741 else
742 fprintf(out, " <?DEFAULT>");
743 fprintf(out, ")\n");
744 break;
745 }
746 case CIL_DEFAULTRANGE: {
747 struct cil_defaultrange *def = node->data;
748 fprintf(out, "(defaultrange ");
749 if (def->class_datums)
750 write_datum_list(out, def->class_datums);
751 else
752 write_string_list(out, def->class_strs);
753 if (def->object_range == CIL_DEFAULT_SOURCE_LOW)
754 fprintf(out, " source low");
755 else if (def->object_range == CIL_DEFAULT_SOURCE_HIGH)
756 fprintf(out, " source high");
757 else if (def->object_range == CIL_DEFAULT_SOURCE_LOW_HIGH)
758 fprintf(out, " source low-high");
759 else if (def->object_range == CIL_DEFAULT_TARGET_LOW)
760 fprintf(out, " target low");
761 else if (def->object_range == CIL_DEFAULT_TARGET_HIGH)
762 fprintf(out, " target high");
763 else if (def->object_range == CIL_DEFAULT_TARGET_LOW_HIGH)
764 fprintf(out, " target low-high");
765 else
766 fprintf(out, " <?DEFAULT>");
767 fprintf(out, ")\n");
768 break;
769 }
770 case CIL_CLASS: {
771 struct cil_class *class = node->data;
772 fprintf(out, "(class %s ", datum_to_str(DATUM(class)));
773 write_node_list(out, node->cl_head);
774 fprintf(out, ")\n");
775 break;
776 }
777 case CIL_CLASSORDER: {
778 struct cil_ordered *ordered = node->data;
779 fprintf(out, "(classorder ");
780 if (ordered->datums) {
781 write_datum_list(out, ordered->datums);
782 } else {
783 write_string_list(out, ordered->strs);
784 }
785 fprintf(out, ")\n");
786 break;
787 }
788 case CIL_COMMON: {
789 struct cil_class *common = node->data;
790 fprintf(out, "(common %s ", datum_to_str(DATUM(common)));
791 write_node_list(out, node->cl_head);
792 fprintf(out, ")\n");
793 break;
794 }
795 case CIL_CLASSCOMMON: {
796 struct cil_classcommon *cc = node->data;
797 fprintf(out, "(classcommon %s ", datum_or_str(DATUM(cc->class), cc->class_str));
798 fprintf(out, "%s", datum_or_str(DATUM(cc->common), cc->common_str));
799 fprintf(out, ")\n");
800 break;
801 }
802 case CIL_CLASSPERMISSION: {
803 struct cil_classpermission *cp = node->data;
804 fprintf(out, "(classpermission %s)\n", datum_to_str(DATUM(cp)));
805 break;
806 }
807 case CIL_CLASSPERMISSIONSET: {
808 struct cil_classpermissionset *cps = node->data;
809 fprintf(out, "(classpermissionset %s ", datum_or_str(DATUM(cps->set), cps->set_str));
810 write_classperms_list(out, cps->classperms);
811 fprintf(out, ")\n");
812 break;
813 }
814 case CIL_MAP_CLASS: {
815 struct cil_class *map = node->data;
816 fprintf(out, "(classmap %s ", datum_to_str(DATUM(map)));
817 write_node_list(out, node->cl_head);
818 fprintf(out, ")\n");
819 break;
820 }
821 case CIL_CLASSMAPPING: {
822 struct cil_classmapping *mapping = node->data;
823 fprintf(out, "(classmapping %s ", datum_or_str(DATUM(mapping->map_class), mapping->map_class_str));
824 fprintf(out, "%s ", datum_or_str(DATUM(mapping->map_perm), mapping->map_perm_str));
825 write_classperms_list(out, mapping->classperms);
826 fprintf(out, ")\n");
827 break;
828 }
829 case CIL_PERMISSIONX: {
830 struct cil_permissionx *permx = node->data;
831 fprintf(out, "(permissionx %s (", datum_to_str(DATUM(permx)));
832 if (permx->kind == CIL_PERMX_KIND_IOCTL) {
833 fprintf(out, "ioctl ");
834 } else if (permx->kind == CIL_PERMX_KIND_NLMSG) {
835 fprintf(out, "nlmsg ");
836 } else {
837 fprintf(out, "<?KIND> ");
838 }
839 fprintf(out, "%s ", datum_or_str(DATUM(permx->obj), permx->obj_str));
840 write_expr(out, permx->expr_str);
841 fprintf(out, "))\n");
842 break;
843 }
844 case CIL_SID: {
845 struct cil_sid *sid = node->data;
846 fprintf(out, "(sid %s)\n", datum_to_str(DATUM(sid)));
847 break;
848 }
849 case CIL_SIDCONTEXT: {
850 struct cil_sidcontext *sidcon = node->data;
851 fprintf(out, "(sidcontext %s ", datum_or_str(DATUM(sidcon->sid), sidcon->sid_str));
852 if (sidcon->context)
853 write_context(out, sidcon->context, CIL_TRUE);
854 else
855 fprintf(out, "%s", sidcon->context_str);
856 fprintf(out, ")\n");
857 break;
858 }
859 case CIL_SIDORDER: {
860 struct cil_ordered *ordered = node->data;
861 fprintf(out, "(sidorder ");
862 if (ordered->datums) {
863 write_datum_list(out, ordered->datums);
864 } else {
865 write_string_list(out, ordered->strs);
866 }
867 fprintf(out, ")\n");
868 break;
869 }
870 case CIL_BOOL: {
871 struct cil_bool *boolean = node->data;
872 fprintf(out, "(boolean %s %s)\n", datum_to_str(DATUM(boolean)), boolean->value ? "true" : "false");
873 break;
874 }
875 case CIL_TUNABLE: {
876 struct cil_tunable *tunable = node->data;
877 fprintf(out, "(tunable %s %s)\n", datum_to_str(DATUM(tunable)), tunable->value ? "true" : "false");
878 break;
879 }
880 case CIL_SENS: {
881 struct cil_sens *sens = node->data;
882 fprintf(out, "(sensitivity %s)\n", datum_to_str(DATUM(sens)));
883 break;
884 }
885 case CIL_SENSALIAS: {
886 struct cil_alias *alias = node->data;
887 fprintf(out, "(sensitivityalias %s)\n", datum_to_str(DATUM(alias)));
888 break;
889 }
890 case CIL_SENSALIASACTUAL: {
891 struct cil_aliasactual *aliasactual = node->data;
892 fprintf(out, "(sensitivityaliasactual %s ", datum_or_str(DATUM(aliasactual->alias), aliasactual->alias_str));
893 fprintf(out, "%s", datum_or_str(DATUM(aliasactual->actual), aliasactual->actual_str));
894 fprintf(out, ")\n");
895 break;
896 }
897 case CIL_CAT: {
898 struct cil_cat *cat = node->data;
899 fprintf(out, "(category %s)\n", datum_to_str(DATUM(cat)));
900 break;
901 }
902 case CIL_CATALIAS: {
903 struct cil_alias *alias = node->data;
904 fprintf(out, "(categoryalias %s)\n", datum_to_str(DATUM(alias)));
905 break;
906 }
907 case CIL_CATALIASACTUAL: {
908 struct cil_aliasactual *aliasactual = node->data;
909 fprintf(out, "(categoryaliasactual %s ", datum_or_str(DATUM(aliasactual->alias), aliasactual->alias_str));
910 fprintf(out, "%s", datum_or_str(DATUM(aliasactual->actual), aliasactual->actual_str));
911 fprintf(out, ")\n");
912 break;
913 }
914 case CIL_CATSET: {
915 struct cil_catset *catset = node->data;
916 fprintf(out, "(categoryset %s ", datum_to_str(DATUM(catset)));
917 write_cats(out, catset->cats);
918 fprintf(out, ")\n");
919 break;
920 }
921 case CIL_CATORDER: {
922 struct cil_ordered *ordered = node->data;
923 fprintf(out, "(categoryorder ");
924 if (ordered->datums) {
925 write_datum_list(out, ordered->datums);
926 } else {
927 write_string_list(out, ordered->strs);
928 }
929 fprintf(out, ")\n");
930 break;
931 }
932 case CIL_SENSCAT: {
933 struct cil_senscat *senscat = node->data;
934 fprintf(out, "(sensitivitycategory ");
935 fprintf(out, "%s ", datum_or_str(DATUM(senscat->sens), senscat->sens_str));
936 write_cats(out, senscat->cats);
937 fprintf(out, ")\n");
938 break;
939 }
940 case CIL_SENSITIVITYORDER: {
941 struct cil_ordered *ordered = node->data;
942 fprintf(out, "(sensitivityorder ");
943 if (ordered->datums) {
944 write_datum_list(out, ordered->datums);
945 } else {
946 write_string_list(out, ordered->strs);
947 }
948 fprintf(out, ")\n");
949 break;
950 }
951 case CIL_LEVEL: {
952 struct cil_level *level = node->data;
953 fprintf(out, "(level %s ", datum_to_str(&level->datum));
954 write_level(out, level, CIL_FALSE);
955 fprintf(out, ")\n");
956 break;
957 }
958 case CIL_LEVELRANGE: {
959 struct cil_levelrange *lvlrange = node->data;
960 fprintf(out, "(levelrange %s ", datum_to_str(DATUM(lvlrange)));
961 write_range(out, lvlrange, CIL_FALSE);
962 fprintf(out, ")\n");
963 break;
964 }
965 case CIL_USER: {
966 struct cil_user *user = node->data;
967 fprintf(out, "(user %s)\n", datum_to_str(DATUM(user)));
968 break;
969 }
970 case CIL_USERATTRIBUTE: {
971 struct cil_userattribute *attr = node->data;
972 fprintf(out, "(userattribute %s)\n", datum_to_str(DATUM(attr)));
973 break;
974 }
975 case CIL_USERATTRIBUTESET: {
976 struct cil_userattributeset *attr = node->data;
977 fprintf(out, "(userattributeset %s ", datum_or_str(DATUM(attr->attr), attr->attr_str));
978 if (attr->datum_expr)
979 write_expr(out, attr->datum_expr);
980 else
981 write_expr(out, attr->str_expr);
982 fprintf(out, ")\n");
983 break;
984 }
985 case CIL_USERROLE: {
986 struct cil_userrole *userrole = node->data;
987 fprintf(out, "(userrole ");
988 fprintf(out, "%s ", datum_or_str(DATUM(userrole->user), userrole->user_str));
989 fprintf(out, "%s", datum_or_str(DATUM(userrole->role), userrole->role_str));
990 fprintf(out, ")\n");
991 break;
992 }
993 case CIL_USERLEVEL: {
994 struct cil_userlevel *userlevel = node->data;
995 fprintf(out, "(userlevel %s ", datum_or_str(DATUM(userlevel->user), userlevel->user_str));
996 if (userlevel->level)
997 write_level(out, userlevel->level, CIL_TRUE);
998 else
999 fprintf(out, "%s", userlevel->level_str);
1000 fprintf(out, ")\n");
1001 break;
1002 }
1003 case CIL_USERRANGE: {
1004 struct cil_userrange *userrange = node->data;
1005 fprintf(out, "(userrange %s ", datum_or_str(DATUM(userrange->user), userrange->user_str));
1006 if (userrange->range)
1007 write_range(out, userrange->range, CIL_TRUE);
1008 else
1009 fprintf(out, "%s", userrange->range_str);
1010 fprintf(out, ")\n");
1011 break;
1012 }
1013 case CIL_USERBOUNDS: {
1014 struct cil_bounds *bounds = node->data;
1015 fprintf(out, "(userbounds ");
1016 fprintf(out, "%s ", datum_or_str(DATUM(bounds->parent), bounds->parent_str));
1017 fprintf(out, "%s)\n", datum_or_str(DATUM(bounds->child), bounds->child_str));
1018 break;
1019 }
1020 case CIL_USERPREFIX: {
1021 struct cil_userprefix *prefix = node->data;
1022 fprintf(out, "(userprefix ");
1023 fprintf(out, "%s ", datum_or_str(DATUM(prefix->user), prefix->user_str));
1024 fprintf(out, "%s)\n", prefix->prefix_str);
1025 break;
1026 }
1027 case CIL_SELINUXUSER: {
1028 struct cil_selinuxuser *selinuxuser = node->data;
1029 fprintf(out, "(selinuxuser %s ", selinuxuser->name_str);
1030 fprintf(out, "%s ", datum_or_str(DATUM(selinuxuser->user), selinuxuser->user_str));
1031 if (selinuxuser->range)
1032 write_range(out, selinuxuser->range, CIL_TRUE);
1033 else
1034 fprintf(out, "%s", selinuxuser->range_str);
1035 fprintf(out, ")\n");
1036 break;
1037 }
1038 case CIL_SELINUXUSERDEFAULT: {
1039 struct cil_selinuxuser *selinuxuser = node->data;
1040 fprintf(out, "(selinuxuserdefault ");
1041 fprintf(out, "%s ", datum_or_str(DATUM(selinuxuser->user), selinuxuser->user_str));
1042 if (selinuxuser->range)
1043 write_range(out, selinuxuser->range, CIL_TRUE);
1044 else
1045 fprintf(out, "%s", selinuxuser->range_str);
1046 fprintf(out, ")\n");
1047 break;
1048 }
1049 case CIL_ROLE: {
1050 fprintf(out, "(role %s)\n", datum_to_str(node->data));
1051 break;
1052 }
1053 case CIL_ROLEATTRIBUTE: {
1054 fprintf(out, "(roleattribute %s)\n", datum_to_str(node->data));
1055 break;
1056 }
1057 case CIL_ROLEATTRIBUTESET: {
1058 struct cil_roleattributeset *attr = node->data;
1059 fprintf(out, "(roleattributeset %s ", datum_or_str(DATUM(attr->attr), attr->attr_str));
1060 if (attr->datum_expr)
1061 write_expr(out, attr->datum_expr);
1062 else
1063 write_expr(out, attr->str_expr);
1064 fprintf(out, ")\n");
1065 break;
1066 }
1067 case CIL_ROLETYPE: {
1068 struct cil_roletype *roletype = node->data;
1069 fprintf(out, "(roletype ");
1070 fprintf(out, "%s ", datum_or_str(DATUM(roletype->role), roletype->role_str));
1071 fprintf(out, "%s", datum_or_str(DATUM(roletype->type), roletype->type_str));
1072 fprintf(out, ")\n");
1073 break;
1074 }
1075 case CIL_ROLEBOUNDS: {
1076 struct cil_bounds *bounds = node->data;
1077 fprintf(out, "(rolebounds ");
1078 fprintf(out, "%s ", datum_or_str(DATUM(bounds->parent), bounds->parent_str));
1079 fprintf(out, "%s)\n", datum_or_str(DATUM(bounds->child), bounds->child_str));
1080 break;
1081 }
1082 case CIL_TYPE: {
1083 fprintf(out, "(type %s)\n", datum_to_str(node->data));
1084 break;
1085 }
1086 case CIL_TYPEALIAS: {
1087 fprintf(out, "(typealias %s)\n", datum_to_str(node->data));
1088 break;
1089 }
1090 case CIL_TYPEALIASACTUAL: {
1091 struct cil_aliasactual *aliasactual = node->data;
1092 fprintf(out, "(typealiasactual %s ", datum_or_str(DATUM(aliasactual->alias), aliasactual->alias_str));
1093 fprintf(out, "%s", datum_or_str(DATUM(aliasactual->actual), aliasactual->actual_str));
1094 fprintf(out, ")\n");
1095 break;
1096 }
1097 case CIL_TYPEATTRIBUTE: {
1098 fprintf(out, "(typeattribute %s)\n", datum_to_str(node->data));
1099 break;
1100 }
1101 case CIL_TYPEATTRIBUTESET: {
1102 struct cil_typeattributeset *attr = node->data;
1103 fprintf(out, "(typeattributeset %s ", datum_or_str(DATUM(attr->attr), attr->attr_str));
1104 if (attr->datum_expr)
1105 write_expr(out, attr->datum_expr);
1106 else
1107 write_expr(out, attr->str_expr);
1108 fprintf(out, ")\n");
1109 break;
1110 }
1111 case CIL_EXPANDTYPEATTRIBUTE: {
1112 struct cil_expandtypeattribute *attr = node->data;
1113 fprintf(out, "(expandtypeattribute ");
1114 if (attr->attr_datums)
1115 write_expr(out, attr->attr_datums);
1116 else
1117 write_expr(out, attr->attr_strs);
1118 fprintf(out, " %s)\n", attr->expand ? "true" : "false");
1119 break;
1120 }
1121 case CIL_TYPEPERMISSIVE: {
1122 struct cil_typepermissive *tp = node->data;
1123 fprintf(out, "(typepermissive ");
1124 fprintf(out, "%s", datum_or_str(DATUM(tp->type), tp->type_str));
1125 fprintf(out, ")\n");
1126 break;
1127 }
1128 case CIL_TYPENEVERAUDIT: {
1129 struct cil_typeneveraudit *tp = node->data;
1130 fprintf(out, "(typeneveraudit ");
1131 fprintf(out, "%s", datum_or_str(DATUM(tp->type), tp->type_str));
1132 fprintf(out, ")\n");
1133 break;
1134 }
1135 case CIL_TYPEBOUNDS: {
1136 struct cil_bounds *bounds = node->data;
1137 fprintf(out, "(typebounds ");
1138 fprintf(out, "%s ", datum_or_str(DATUM(bounds->parent), bounds->parent_str));
1139 fprintf(out, "%s)\n", datum_or_str(DATUM(bounds->child), bounds->child_str));
1140 break;
1141 }
1142 case CIL_ROLEALLOW: {
1143 struct cil_roleallow *roleallow = node->data;
1144 fprintf(out, "(roleallow ");
1145 fprintf(out, "%s ", datum_or_str(DATUM(roleallow->src), roleallow->src_str));
1146 fprintf(out, "%s", datum_or_str(DATUM(roleallow->tgt), roleallow->tgt_str));
1147 fprintf(out, ")\n");
1148 break;
1149 }
1150 case CIL_ROLETRANSITION: {
1151 struct cil_roletransition *roletrans = node->data;
1152 fprintf(out, "(roletransition ");
1153 fprintf(out, "%s ", datum_or_str(DATUM(roletrans->src), roletrans->src_str));
1154 fprintf(out, "%s ", datum_or_str(DATUM(roletrans->tgt), roletrans->tgt_str));
1155 fprintf(out, "%s ", datum_or_str(DATUM(roletrans->obj), roletrans->obj_str));
1156 fprintf(out, "%s", datum_or_str(DATUM(roletrans->result), roletrans->result_str));
1157 fprintf(out, ")\n");
1158 break;
1159 }
1160 case CIL_AVRULE: {
1161 struct cil_avrule *rule = node->data;
1162 if (rule->rule_kind == AVRULE_ALLOWED)
1163 fprintf(out, "(allow ");
1164 else if (rule->rule_kind == AVRULE_AUDITALLOW)
1165 fprintf(out, "(auditallow ");
1166 else if (rule->rule_kind == AVRULE_DONTAUDIT)
1167 fprintf(out, "(dontaudit ");
1168 else if (rule->rule_kind == AVRULE_NEVERALLOW)
1169 fprintf(out, "(neverallow ");
1170 else
1171 fprintf(out, "(<?AVRULE> ");
1172
1173 fprintf(out, "%s ", datum_or_str(DATUM(rule->src), rule->src_str));
1174 fprintf(out, "%s ", datum_or_str(DATUM(rule->tgt), rule->tgt_str));
1175 write_classperms_list(out, rule->perms.classperms);
1176 fprintf(out, ")\n");
1177 break;
1178 }
1179 case CIL_AVRULEX: {
1180 struct cil_avrule *rule = node->data;
1181 if (rule->rule_kind == AVRULE_ALLOWED)
1182 fprintf(out, "(allowx ");
1183 else if (rule->rule_kind == AVRULE_AUDITALLOW)
1184 fprintf(out, "(auditallowx ");
1185 else if (rule->rule_kind == AVRULE_DONTAUDIT)
1186 fprintf(out, "(dontauditx ");
1187 else if (rule->rule_kind == AVRULE_NEVERALLOW)
1188 fprintf(out, "(neverallowx ");
1189 else
1190 fprintf(out, "(<?AVRULEX> ");
1191 fprintf(out, "%s ", datum_or_str(DATUM(rule->src), rule->src_str));
1192 fprintf(out, "%s ", datum_or_str(DATUM(rule->tgt), rule->tgt_str));
1193 if (rule->perms.x.permx_str) {
1194 fprintf(out, "%s",rule->perms.x.permx_str);
1195 } else {
1196 write_permx(out, rule->perms.x.permx);
1197 }
1198 fprintf(out, ")\n");
1199 break;
1200 }
1201 case CIL_DENY_RULE: {
1202 struct cil_deny_rule *rule = node->data;
1203 fprintf(out, "(deny ");
1204
1205 fprintf(out, "%s ", datum_or_str(DATUM(rule->src), rule->src_str));
1206 fprintf(out, "%s ", datum_or_str(DATUM(rule->tgt), rule->tgt_str));
1207 write_classperms_list(out, rule->classperms);
1208 fprintf(out, ")\n");
1209 break;
1210 }
1211 case CIL_TYPE_RULE: {
1212 struct cil_type_rule *rule = node->data;
1213 if (rule->rule_kind == AVRULE_TRANSITION)
1214 fprintf(out, "(typetransition ");
1215 else if (rule->rule_kind == AVRULE_MEMBER)
1216 fprintf(out, "(typemember ");
1217 else if (rule->rule_kind == AVRULE_CHANGE)
1218 fprintf(out, "(typechange ");
1219 else
1220 fprintf(out, "(<?TYPERULE> ");
1221 fprintf(out, "%s ", datum_or_str(DATUM(rule->src), rule->src_str));
1222 fprintf(out, "%s ", datum_or_str(DATUM(rule->tgt), rule->tgt_str));
1223 fprintf(out, "%s ", datum_or_str(DATUM(rule->obj), rule->obj_str));
1224 fprintf(out, "%s", datum_or_str(DATUM(rule->result), rule->result_str));
1225 fprintf(out, ")\n");
1226 break;
1227 }
1228 case CIL_NAMETYPETRANSITION: {
1229 struct cil_nametypetransition *rule = node->data;
1230 fprintf(out, "(typetransition ");
1231 fprintf(out, "%s ", datum_or_str(DATUM(rule->src), rule->src_str));
1232 fprintf(out, "%s ", datum_or_str(DATUM(rule->tgt), rule->tgt_str));
1233 fprintf(out, "%s ", datum_or_str(DATUM(rule->obj), rule->obj_str));
1234 if (rule->name) {
1235 fprintf(out, "\"%s\" ", DATUM(rule->name)->fqn);
1236 } else {
1237 fprintf(out, "%s ", rule->name_str);
1238 }
1239 fprintf(out, "%s", datum_or_str(DATUM(rule->result), rule->result_str));
1240 fprintf(out, ")\n");
1241 break;
1242 }
1243 case CIL_RANGETRANSITION: {
1244 struct cil_rangetransition *rule = node->data;
1245 fprintf(out, "(rangetransition ");
1246 fprintf(out, "%s ", datum_or_str(DATUM(rule->src), rule->src_str));
1247 fprintf(out, "%s ", datum_or_str(DATUM(rule->exec), rule->exec_str));
1248 fprintf(out, "%s ", datum_or_str(DATUM(rule->obj), rule->obj_str));
1249 if (rule->range)
1250 write_range(out, rule->range, CIL_TRUE);
1251 else
1252 fprintf(out, "%s", rule->range_str);
1253 fprintf(out, ")\n");
1254 break;
1255 }
1256 case CIL_CONSTRAIN: {
1257 struct cil_constrain *cons = node->data;
1258 fprintf(out, "(constrain ");
1259 write_constrain(out, cons);
1260 fprintf(out, ")\n");
1261 break;
1262 }
1263 case CIL_MLSCONSTRAIN: {
1264 struct cil_constrain *cons = node->data;
1265 fprintf(out, "(mlsconstrain ");
1266 write_constrain(out, cons);
1267 fprintf(out, ")\n");
1268 break;
1269 }
1270 case CIL_VALIDATETRANS: {
1271 struct cil_validatetrans *vt = node->data;
1272 fprintf(out, "(validatetrans ");
1273 fprintf(out, "%s ", datum_or_str(DATUM(vt->class), vt->class_str));
1274 if (vt->datum_expr)
1275 write_expr(out, vt->datum_expr);
1276 else
1277 write_expr(out, vt->str_expr);
1278 fprintf(out, ")\n");
1279 break;
1280 }
1281 case CIL_MLSVALIDATETRANS: {
1282 struct cil_validatetrans *vt = node->data;
1283 fprintf(out, "(mlsvalidatetrans ");
1284 fprintf(out, "%s ", datum_or_str(DATUM(vt->class), vt->class_str));
1285 if (vt->datum_expr)
1286 write_expr(out, vt->datum_expr);
1287 else
1288 write_expr(out, vt->str_expr);
1289 fprintf(out, ")\n");
1290 break;
1291 }
1292 case CIL_CONTEXT: {
1293 struct cil_context *context = node->data;
1294 fprintf(out, "(context %s ", datum_to_str(DATUM(context)));
1295 write_context(out, context, CIL_FALSE);
1296 fprintf(out, ")\n");
1297 break;
1298 }
1299 case CIL_FILECON: {
1300 struct cil_filecon *filecon = node->data;
1301 fprintf(out, "(filecon ");
1302 if (filecon->path) {
1303 fprintf(out, "\"%s\" ", DATUM(filecon->path)->fqn);
1304 } else {
1305 fprintf(out, "%s ", filecon->path_str);
1306 }
1307 switch (filecon->type) {
1308 case CIL_FILECON_ANY:
1309 fprintf(out, "%s ", CIL_KEY_ANY);
1310 break;
1311 case CIL_FILECON_FILE:
1312 fprintf(out, "%s ", CIL_KEY_FILE);
1313 break;
1314 case CIL_FILECON_DIR:
1315 fprintf(out, "%s ", CIL_KEY_DIR);
1316 break;
1317 case CIL_FILECON_CHAR:
1318 fprintf(out, "%s ", CIL_KEY_CHAR);
1319 break;
1320 case CIL_FILECON_BLOCK:
1321 fprintf(out, "%s ", CIL_KEY_BLOCK);
1322 break;
1323 case CIL_FILECON_SOCKET:
1324 fprintf(out, "%s ", CIL_KEY_SOCKET);
1325 break;
1326 case CIL_FILECON_PIPE:
1327 fprintf(out, "%s ", CIL_KEY_PIPE);
1328 break;
1329 case CIL_FILECON_SYMLINK:
1330 fprintf(out, "%s ", CIL_KEY_SYMLINK);
1331 break;
1332 default:
1333 fprintf(out, "<?FILETYPE> ");
1334 }
1335 if (filecon->context)
1336 write_context(out, filecon->context, CIL_TRUE);
1337 else if (filecon->context_str)
1338 fprintf(out, "%s", filecon->context_str);
1339 else
1340 fprintf(out, "()");
1341 fprintf(out, ")\n");
1342 break;
1343 }
1344 case CIL_IBPKEYCON: {
1345 struct cil_ibpkeycon *ibpkeycon = node->data;
1346 fprintf(out, "(ibpkeycon %s ", ibpkeycon->subnet_prefix_str);
1347 fprintf(out, "(%d %d) ", ibpkeycon->pkey_low, ibpkeycon->pkey_high);
1348 if (ibpkeycon->context)
1349 write_context(out, ibpkeycon->context, CIL_TRUE);
1350 else if (ibpkeycon->context_str)
1351 fprintf(out, "%s", ibpkeycon->context_str);
1352 fprintf(out, ")\n");
1353 break;
1354 }
1355 case CIL_PORTCON: {
1356 struct cil_portcon *portcon = node->data;
1357 fprintf(out, "(portcon ");
1358 if (portcon->proto == CIL_PROTOCOL_UDP)
1359 fprintf(out, " udp ");
1360 else if (portcon->proto == CIL_PROTOCOL_TCP)
1361 fprintf(out, " tcp ");
1362 else if (portcon->proto == CIL_PROTOCOL_DCCP)
1363 fprintf(out, "dccp ");
1364 else if (portcon->proto == CIL_PROTOCOL_SCTP)
1365 fprintf(out, "sctp ");
1366 else
1367 fprintf(out, "<?PROTOCOL> ");
1368 if (portcon->port_low == portcon->port_high)
1369 fprintf(out, "%d ", portcon->port_low);
1370 else
1371 fprintf(out, "(%d %d) ", portcon->port_low, portcon->port_high);
1372 if (portcon->context)
1373 write_context(out, portcon->context, CIL_TRUE);
1374 else
1375 fprintf(out, "%s", portcon->context_str);
1376 fprintf(out, ")\n");
1377 break;
1378 }
1379 case CIL_NODECON: {
1380 struct cil_nodecon *nodecon = node->data;
1381 fprintf(out, "(nodecon ");
1382 if (nodecon->addr)
1383 write_ipaddr(out, nodecon->addr);
1384 else
1385 fprintf(out, "%s ", nodecon->addr_str);
1386 fprintf(out, " ");
1387 if (nodecon->mask)
1388 write_ipaddr(out, nodecon->mask);
1389 else
1390 fprintf(out, "%s ", nodecon->mask_str);
1391 fprintf(out, " ");
1392 if (nodecon->context)
1393 write_context(out, nodecon->context, CIL_TRUE);
1394 else
1395 fprintf(out, "%s", nodecon->context_str);
1396 fprintf(out, ")\n");
1397 break;
1398 }
1399 case CIL_GENFSCON: {
1400 struct cil_genfscon *genfscon = node->data;
1401 fprintf(out, "(genfscon ");
1402 fprintf(out, "%s \"%s\" ", genfscon->fs_str, genfscon->path_str);
1403 if (genfscon->file_type != CIL_FILECON_ANY) {
1404 switch (genfscon->file_type) {
1405 case CIL_FILECON_FILE:
1406 fprintf(out, "%s ", CIL_KEY_FILE);
1407 break;
1408 case CIL_FILECON_DIR:
1409 fprintf(out, "%s ", CIL_KEY_DIR);
1410 break;
1411 case CIL_FILECON_CHAR:
1412 fprintf(out, "%s ", CIL_KEY_CHAR);
1413 break;
1414 case CIL_FILECON_BLOCK:
1415 fprintf(out, "%s ", CIL_KEY_BLOCK);
1416 break;
1417 case CIL_FILECON_SOCKET:
1418 fprintf(out, "%s ", CIL_KEY_SOCKET);
1419 break;
1420 case CIL_FILECON_PIPE:
1421 fprintf(out, "%s ", CIL_KEY_PIPE);
1422 break;
1423 case CIL_FILECON_SYMLINK:
1424 fprintf(out, "%s ", CIL_KEY_SYMLINK);
1425 break;
1426 default:
1427 fprintf(out, "<?FILETYPE> ");
1428 }
1429 }
1430 if (genfscon->context)
1431 write_context(out, genfscon->context, CIL_TRUE);
1432 else
1433 fprintf(out, "%s", genfscon->context_str);
1434 fprintf(out, ")\n");
1435 break;
1436 }
1437 case CIL_NETIFCON: {
1438 struct cil_netifcon *netifcon = node->data;
1439 fprintf(out, "(netifcon %s ", netifcon->interface_str);
1440 if (netifcon->if_context)
1441 write_context(out, netifcon->if_context, CIL_TRUE);
1442 else
1443 fprintf(out, "%s", netifcon->if_context_str);
1444 fprintf(out, " ");
1445 if (netifcon->packet_context)
1446 write_context(out, netifcon->packet_context, CIL_TRUE);
1447 else
1448 fprintf(out, "%s", netifcon->packet_context_str);
1449 fprintf(out, ")\n");
1450 break;
1451 }
1452 case CIL_IBENDPORTCON: {
1453 struct cil_ibendportcon *ibendportcon = node->data;
1454 fprintf(out, "(ibendportcon %s %u ", ibendportcon->dev_name_str, ibendportcon->port);
1455 if (ibendportcon->context)
1456 write_context(out, ibendportcon->context, CIL_TRUE);
1457 else
1458 fprintf(out, "%s", ibendportcon->context_str);
1459 fprintf(out, ")\n");
1460 break;
1461 }
1462 case CIL_PIRQCON: {
1463 struct cil_pirqcon *pirqcon = node->data;
1464 fprintf(out, "(pirqcon %d ", pirqcon->pirq);
1465 if (pirqcon->context)
1466 write_context(out, pirqcon->context, CIL_TRUE);
1467 else
1468 fprintf(out, "%s", pirqcon->context_str);
1469 fprintf(out, ")\n");
1470 break;
1471 }
1472 case CIL_IOMEMCON: {
1473 struct cil_iomemcon *iomemcon = node->data;
1474 fprintf(out, "(iomemcon (%"PRId64" %"PRId64") ", iomemcon->iomem_low, iomemcon->iomem_high);
1475 if (iomemcon->context)
1476 write_context(out, iomemcon->context, CIL_TRUE);
1477 else
1478 fprintf(out, "%s", iomemcon->context_str);
1479 fprintf(out, ")\n");
1480 break;
1481 }
1482 case CIL_IOPORTCON: {
1483 struct cil_ioportcon *ioportcon = node->data;
1484 fprintf(out, "(ioportcon ");
1485 if (ioportcon->ioport_low == ioportcon->ioport_high)
1486 fprintf(out, "%d ", ioportcon->ioport_low);
1487 else
1488 fprintf(out, "(%d %d) ", ioportcon->ioport_low, ioportcon->ioport_high);
1489
1490 if (ioportcon->context)
1491 write_context(out, ioportcon->context, CIL_TRUE);
1492 else
1493 fprintf(out, "%s", ioportcon->context_str);
1494 fprintf(out, ")\n");
1495 break;
1496 }
1497 case CIL_PCIDEVICECON: {
1498 struct cil_pcidevicecon *pcidevicecon = node->data;
1499 fprintf(out, "(pcidevicecon %d ", pcidevicecon->dev);
1500 if (pcidevicecon->context)
1501 write_context(out, pcidevicecon->context, CIL_TRUE);
1502 else
1503 fprintf(out, "%s", pcidevicecon->context_str);
1504 fprintf(out, ")\n");
1505 break;
1506 }
1507 case CIL_DEVICETREECON: {
1508 struct cil_devicetreecon *devicetreecon = node->data;
1509 fprintf(out, "(devicetreecon \"%s\" ", devicetreecon->path);
1510 if (devicetreecon->context)
1511 write_context(out, devicetreecon->context, CIL_TRUE);
1512 else
1513 fprintf(out, "%s", devicetreecon->context_str);
1514 fprintf(out, ")\n");
1515 break;
1516 }
1517 case CIL_FSUSE: {
1518 struct cil_fsuse *fsuse = node->data;
1519 fprintf(out, "(fsuse ");
1520 if (fsuse->type == CIL_FSUSE_XATTR)
1521 fprintf(out, "xattr ");
1522 else if (fsuse->type == CIL_FSUSE_TASK)
1523 fprintf(out, "task ");
1524 else if (fsuse->type == CIL_FSUSE_TRANS)
1525 fprintf(out, "trans ");
1526 else
1527 fprintf(out, "<?TYPE> ");
1528 fprintf(out, "%s ", fsuse->fs_str);
1529 if (fsuse->context)
1530 write_context(out, fsuse->context, CIL_TRUE);
1531 else
1532 fprintf(out, "%s", fsuse->context_str);
1533 fprintf(out, ")\n");
1534 break;
1535 }
1536 case CIL_POLICYCAP: {
1537 struct cil_policycap *polcap = node->data;
1538 fprintf(out, "(policycap %s)\n", polcap->datum.name);
1539 break;
1540 }
1541 case CIL_IPADDR: {
1542 struct cil_ipaddr *ipaddr = node->data;
1543 char buf[256];
1544 if (inet_ntop(ipaddr->family, &ipaddr->ip, buf, 256) == NULL)
1545 strcpy(buf, "<?IPADDR>");
1546 fprintf(out, "(ipaddr %s %s)\n", datum_to_str(&ipaddr->datum), buf);
1547 break;
1548 }
1549 default :
1550 fprintf(out, "(<?RULE:%s>)\n", cil_node_to_string(node));
1551 break;
1552 }
1553 }
1554
1555 /*
1556 * Tree walk data and helper functions for writing the AST of the various phases
1557 */
1558
1559 struct cil_write_ast_args {
1560 FILE *out;
1561 int depth;
1562 };
1563
1564 /*
1565 * Helper functions for writing the parse AST
1566 */
1567
1568 static int __write_parse_ast_node_helper(struct cil_tree_node *node, __attribute__((unused)) uint32_t *finished, void *extra_args)
1569 {
1570 struct cil_write_ast_args *args = extra_args;
1571
1572 fprintf(args->out, "%*s", args->depth*4, "");
1573 if (!node->data) {
1574 if (node->cl_head)
1575 fprintf(args->out, "(\n");
1576 else
1577 fprintf(args->out, "()\n");
1578 } else {
1579 char *str = (char *)node->data;
1580 size_t len = strlen(str);
1581 size_t i;
1582
1583 for (i = 0; i < len; i++) {
1584 if (isspace(str[i])) {
1585 fprintf(args->out, "\"%s\"\n", str);
1586 return SEPOL_OK;
1587 }
1588 }
1589
1590 fprintf(args->out, "%s\n", (char *)node->data);
1591 }
1592
1593 return SEPOL_OK;
1594 }
1595
1596 static int __write_parse_ast_first_child_helper(struct cil_tree_node *node, void *extra_args)
1597 {
1598 struct cil_write_ast_args *args = extra_args;
1599 struct cil_tree_node *parent = node->parent;
1600
1601 if (parent->flavor != CIL_ROOT) {
1602 args->depth++;
1603 }
1604
1605 return SEPOL_OK;
1606 }
1607
1608 static int __write_parse_ast_last_child_helper(struct cil_tree_node *node, void *extra_args)
1609 {
1610 struct cil_write_ast_args *args = extra_args;
1611 struct cil_tree_node *parent = node->parent;
1612
1613 if (parent->flavor == CIL_ROOT) {
1614 return SEPOL_OK;
1615 }
1616
1617 args->depth--;
1618 fprintf(args->out, "%*s", args->depth*4, "");
1619 fprintf(args->out, ")\n");
1620
1621 return SEPOL_OK;
1622 }
1623
1624 /*
1625 * Helper functions for writing the CIL AST for the build and resolve phases
1626 */
1627
1628 static int __write_cil_ast_node_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
1629 {
1630 struct cil_write_ast_args *args = extra_args;
1631
1632 if (node->flavor == CIL_SRC_INFO) {
1633 cil_write_src_info_node(args->out, node);
1634
1635 if (node->cl_head == NULL) {
1636 fprintf(args->out, ";;* lme\n");
1637 }
1638 return SEPOL_OK;
1639 }
1640
1641 fprintf(args->out, "%*s", args->depth*4, "");
1642
1643 cil_write_ast_node(args->out, node);
1644
1645 if (node->flavor == CIL_CLASS || node->flavor == CIL_COMMON || node->flavor == CIL_MAP_CLASS) {
1646 *finished = CIL_TREE_SKIP_HEAD;
1647 }
1648
1649 return SEPOL_OK;
1650 }
1651
1652 static int __write_cil_ast_first_child_helper(struct cil_tree_node *node, void *extra_args)
1653 {
1654 struct cil_write_ast_args *args = extra_args;
1655 struct cil_tree_node *parent = node->parent;
1656
1657 if (parent->flavor != CIL_SRC_INFO && parent->flavor != CIL_ROOT) {
1658 args->depth++;
1659 }
1660
1661 return SEPOL_OK;
1662 }
1663
1664 static int __write_cil_ast_last_child_helper(struct cil_tree_node *node, void *extra_args)
1665 {
1666 struct cil_write_ast_args *args = extra_args;
1667 struct cil_tree_node *parent = node->parent;
1668
1669 if (parent->flavor == CIL_ROOT) {
1670 return SEPOL_OK;
1671 } else if (parent->flavor == CIL_SRC_INFO) {
1672 fprintf(args->out, ";;* lme\n");
1673 return SEPOL_OK;
1674 }
1675
1676 args->depth--;
1677 fprintf(args->out, "%*s", args->depth*4, "");
1678 fprintf(args->out, ")\n");
1679
1680 return SEPOL_OK;
1681 }
1682
1683 int cil_write_ast(FILE *out, enum cil_write_ast_phase phase, struct cil_tree_node *node)
1684 {
1685 struct cil_write_ast_args extra_args;
1686 int rc;
1687
1688 extra_args.out = out;
1689 extra_args.depth = 0;
1690
1691 if (phase == CIL_WRITE_AST_PHASE_PARSE) {
1692 rc = cil_tree_walk(node, __write_parse_ast_node_helper, __write_parse_ast_first_child_helper, __write_parse_ast_last_child_helper, &extra_args);
1693 } else {
1694 rc = cil_tree_walk(node, __write_cil_ast_node_helper, __write_cil_ast_first_child_helper, __write_cil_ast_last_child_helper, &extra_args);
1695 }
1696
1697 if (rc != SEPOL_OK) {
1698 cil_log(CIL_ERR, "Failed to write AST\n");
1699 return SEPOL_ERR;
1700 }
1701
1702 return SEPOL_OK;
1703 }