xref: /aosp_15_r20/external/selinux/checkpolicy/parse_util.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 /*
2  * Author: Karl MacMillan <[email protected]>
3  *
4  * Copyright (C) 2006 Tresys Technology, LLC
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2.1 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #include "parse_util.h"
22 #include "queue.h"
23 
24 /* these are defined in policy_parse.y and are needed for read_source_policy */
25 extern FILE *yyin;
26 extern void init_parser(int);
27 extern int yyparse(void);
28 extern void yyrestart(FILE *);
29 extern int yylex_destroy(void);
30 extern queue_t id_queue;
31 extern unsigned int policydb_errors;
32 extern policydb_t *policydbp;
33 extern int mlspol;
34 extern void set_source_file(const char *name);
35 
read_source_policy(policydb_t * p,const char * file,const char * progname)36 int read_source_policy(policydb_t * p, const char *file, const char *progname)
37 {
38 	int rc = -1;
39 
40 	yyin = fopen(file, "r");
41 	if (!yyin) {
42 		fprintf(stderr, "%s:  unable to open %s:  %s\n", progname, file, strerror(errno));
43 		return -1;
44 	}
45 	set_source_file(file);
46 
47 	id_queue = queue_create();
48 	if (id_queue == NULL) {
49 		fprintf(stderr, "%s: out of memory!\n", progname);
50 		goto cleanup;
51 	}
52 
53 	mlspol = p->mls;
54 	policydbp = p;
55 	policydbp->name = strdup(file);
56 	if (!policydbp->name) {
57 		fprintf(stderr, "%s: out of memory!\n", progname);
58 		goto cleanup;
59 	}
60 
61 	init_parser(1);
62 	if (yyparse() || policydb_errors) {
63 		fprintf(stderr,
64 			"%s:  error(s) encountered while parsing configuration\n",
65 			progname);
66 		goto cleanup;
67 	}
68 	rewind(yyin);
69 	init_parser(2);
70 	set_source_file(file);
71 	yyrestart(yyin);
72 	if (yyparse() || policydb_errors) {
73 		fprintf(stderr,
74 			"%s:  error(s) encountered while parsing configuration\n",
75 			progname);
76 		goto cleanup;
77 	}
78 
79 	rc = 0;
80 
81 cleanup:
82 	queue_destroy(id_queue);
83 	fclose(yyin);
84 	yylex_destroy();
85 
86 	return rc;
87 }
88