1 /*
2 * Author: Karl MacMillan <kmacmillan@tresys.com>
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 queue_t id_queue;
30 extern unsigned int policydb_errors;
31 extern policydb_t *policydbp;
32 extern int mlspol;
33 extern void set_source_file(const char *name);
34
read_source_policy(policydb_t * p,const char * file,const char * progname)35 int read_source_policy(policydb_t * p, const char *file, const char *progname)
36 {
37 yyin = fopen(file, "r");
38 if (!yyin) {
39 fprintf(stderr, "%s: unable to open %s: %s\n", progname, file, strerror(errno));
40 return -1;
41 }
42 set_source_file(file);
43
44 if ((id_queue = queue_create()) == NULL) {
45 fprintf(stderr, "%s: out of memory!\n", progname);
46 return -1;
47 }
48
49 policydbp = p;
50 mlspol = p->mls;
51
52 init_parser(1);
53 if (yyparse() || policydb_errors) {
54 fprintf(stderr,
55 "%s: error(s) encountered while parsing configuration\n",
56 progname);
57 return -1;
58 }
59 rewind(yyin);
60 init_parser(2);
61 set_source_file(file);
62 yyrestart(yyin);
63 if (yyparse() || policydb_errors) {
64 fprintf(stderr,
65 "%s: error(s) encountered while parsing configuration\n",
66 progname);
67 return -1;
68 }
69 queue_destroy(id_queue);
70
71 fclose(yyin);
72
73 return 0;
74 }
75