2 /* strongSwan config file parser (parser.y)
3 * Copyright (C) 2001 Mathieu Lafon - Arkoon Network Security
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include "../pluto/constants.h"
23 #include "../pluto/defs.h"
24 #include "../pluto/log.h"
25 #include "ipsec-parser.h"
27 #define YYERROR_VERBOSE
28 #define ERRSTRING_LEN 256
33 static char parser_errstring[ERRSTRING_LEN+1];
35 extern void yyerror(const char *s);
36 extern int yylex (void);
37 extern void _parser_y_error(char *b, int size, const char *s);
42 static int _save_errors_;
43 static config_parsed_t *_parser_cfg;
44 static kw_list_t **_parser_kw, *_parser_kw_last;
45 static char errbuf[ERRSTRING_LEN+1];
50 extern kw_entry_t *in_word_set (char *str, unsigned int len);
55 %token EQUAL FIRST_SPACES EOL CONFIG SETUP CONN CA INCLUDE FILE_VERSION
65 config_file section_or_include
70 FILE_VERSION STRING EOL
76 _parser_kw = &(_parser_cfg->config_setup);
77 _parser_kw_last = NULL;
81 section_list_t *section = malloc_thing(section_list_t);
83 section->name = clone_str($2);
86 _parser_kw = &(section->kw);
87 if (!_parser_cfg->conn_first)
88 _parser_cfg->conn_first = section;
89 if (_parser_cfg->conn_last)
90 _parser_cfg->conn_last->next = section;
91 _parser_cfg->conn_last = section;
92 _parser_kw_last = NULL;
97 section_list_t *section = malloc_thing(section_list_t);
98 section->name = clone_str($2);
100 section->next = NULL;
101 _parser_kw = &(section->kw);
102 if (!_parser_cfg->ca_first)
103 _parser_cfg->ca_first = section;
104 if (_parser_cfg->ca_last)
105 _parser_cfg->ca_last->next = section;
106 _parser_cfg->ca_last = section;
107 _parser_kw_last = NULL;
112 extern void _parser_y_include (const char *f);
113 _parser_y_include($2);
120 FIRST_SPACES statement_kw EOL kw_section
128 kw_entry_t *entry = in_word_set($1, strlen($1));
132 snprintf(errbuf, ERRSTRING_LEN, "unknown keyword '%s'", $1);
137 new = (kw_list_t *)malloc_thing(kw_list_t);
139 new->value = clone_str($3);
142 _parser_kw_last->next = new;
143 _parser_kw_last = new;
159 void yyerror(const char *s)
162 _parser_y_error(parser_errstring, ERRSTRING_LEN, s);
165 config_parsed_t *parser_load_conf(const char *file)
167 config_parsed_t *cfg = NULL;
171 extern void _parser_y_init(const char *f);
172 extern void _parser_y_fini(void);
175 memset(parser_errstring, 0, ERRSTRING_LEN+1);
177 cfg = (config_parsed_t *)malloc_thing(config_parsed_t);
180 memset(cfg, 0, sizeof(config_parsed_t));
181 f = fopen(file, "r");
185 _parser_y_init(file);
191 if (parser_errstring[0] == '\0')
193 snprintf(parser_errstring, ERRSTRING_LEN, "Unknown error...");
196 while (yyparse() != 0);
199 else if (parser_errstring[0] != '\0')
214 snprintf(parser_errstring, ERRSTRING_LEN, "can't load file '%s'", file);
220 snprintf(parser_errstring, ERRSTRING_LEN, "can't allocate memory");
226 plog("%s", parser_errstring);
229 parser_free_conf(cfg);
237 static void parser_free_kwlist(kw_list_t *list)
250 void parser_free_conf(config_parsed_t *cfg)
255 parser_free_kwlist(cfg->config_setup);
256 while (cfg->conn_first)
258 sec = cfg->conn_first;
259 cfg->conn_first = cfg->conn_first->next;
261 parser_free_kwlist(sec->kw);
264 while (cfg->ca_first)
267 cfg->ca_first = cfg->ca_first->next;
269 parser_free_kwlist(sec->kw);