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
21 #include <utils/debug.h>
23 #include "ipsec-parser.h"
25 #define YYERROR_VERBOSE
26 #define ERRSTRING_LEN 256
31 static char parser_errstring[ERRSTRING_LEN+1];
33 extern void yyerror(const char *s);
34 extern int yylex (void);
35 extern void _parser_y_error(char *b, int size, const char *s);
40 static int _save_errors_;
41 static config_parsed_t *_parser_cfg;
42 static kw_list_t **_parser_kw, *_parser_kw_last;
43 static char errbuf[ERRSTRING_LEN+1];
48 extern kw_entry_t *in_word_set (char *str, unsigned int len);
53 %token EQUAL FIRST_SPACES EOL CONFIG SETUP CONN CA INCLUDE FILE_VERSION
63 config_file section_or_include
68 FILE_VERSION STRING EOL
74 _parser_kw = &(_parser_cfg->config_setup);
75 _parser_kw_last = NULL;
79 section_list_t *section = malloc_thing(section_list_t);
81 section->name = strdupnull($2);
84 _parser_kw = &(section->kw);
85 if (!_parser_cfg->conn_first)
86 _parser_cfg->conn_first = section;
87 if (_parser_cfg->conn_last)
88 _parser_cfg->conn_last->next = section;
89 _parser_cfg->conn_last = section;
90 _parser_kw_last = NULL;
95 section_list_t *section = malloc_thing(section_list_t);
96 section->name = strdupnull($2);
99 _parser_kw = &(section->kw);
100 if (!_parser_cfg->ca_first)
101 _parser_cfg->ca_first = section;
102 if (_parser_cfg->ca_last)
103 _parser_cfg->ca_last->next = section;
104 _parser_cfg->ca_last = section;
105 _parser_kw_last = NULL;
110 extern void _parser_y_include (const char *f);
111 _parser_y_include($2);
118 FIRST_SPACES statement_kw EOL kw_section
126 kw_entry_t *entry = in_word_set($1, strlen($1));
130 snprintf(errbuf, ERRSTRING_LEN, "unknown keyword '%s'", $1);
135 new = (kw_list_t *)malloc_thing(kw_list_t);
137 new->value = strdupnull($3);
140 _parser_kw_last->next = new;
141 _parser_kw_last = new;
157 void yyerror(const char *s)
160 _parser_y_error(parser_errstring, ERRSTRING_LEN, s);
163 config_parsed_t *parser_load_conf(const char *file)
165 config_parsed_t *cfg = NULL;
169 extern void _parser_y_init(const char *f);
170 extern void _parser_y_fini(void);
173 memset(parser_errstring, 0, ERRSTRING_LEN+1);
175 cfg = (config_parsed_t *)malloc_thing(config_parsed_t);
178 memset(cfg, 0, sizeof(config_parsed_t));
179 f = fopen(file, "r");
183 _parser_y_init(file);
189 if (parser_errstring[0] == '\0')
191 snprintf(parser_errstring, ERRSTRING_LEN, "Unknown error...");
194 while (yyparse() != 0);
197 else if (parser_errstring[0] != '\0')
212 snprintf(parser_errstring, ERRSTRING_LEN, "can't load file '%s'", file);
218 snprintf(parser_errstring, ERRSTRING_LEN, "can't allocate memory");
224 DBG1(DBG_APP, "%s", parser_errstring);
227 parser_free_conf(cfg);
235 static void parser_free_kwlist(kw_list_t *list)
248 void parser_free_conf(config_parsed_t *cfg)
253 parser_free_kwlist(cfg->config_setup);
254 while (cfg->conn_first)
256 sec = cfg->conn_first;
257 cfg->conn_first = cfg->conn_first->next;
259 parser_free_kwlist(sec->kw);
262 while (cfg->ca_first)
265 cfg->ca_first = cfg->ca_first->next;
267 parser_free_kwlist(sec->kw);