blob: f79f1fa8bb430835be5c02fb23d8a5bad9281408 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2015, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* Yacc extension's configuration parser.
37 * See doc/app_acct.conf.sample for configuration file format
38 */
39
40/* For development only : */
41%debug
42%error-verbose
43
44/* The parser receives the configuration file filename as parameter */
45%parse-param {char * conffile}
46
47/* Keep track of location */
48%locations
49%pure-parser
50
51%{
52#include "app_acct.h"
53#include "acct_conf.tab.h"
54
55#include <string.h>
56#include <errno.h>
57
58/* Forward declaration */
59int yyparse(char * conffile);
60
61/* The Lex parser prototype */
62int acct_conflex(YYSTYPE *lvalp, YYLTYPE *llocp);
63
64/* the global configuration */
65struct acct_conf * acct_config = NULL;
66
67/* Initialize the blank configuration structure */
68int acct_conf_init(void)
69{
70 TRACE_ENTRY();
71
72 CHECK_MALLOC( acct_config = malloc(sizeof(struct acct_conf)) );
73 memset(acct_config, 0, sizeof(struct acct_conf) );
74 fd_list_init(&acct_config->avps, NULL);
75
76 return 0;
77}
78
79/* Validate and eventually display the content of the configuration file for debug */
80int acct_conf_check(char * conffile)
81{
82 CHECK_PARAMS(acct_config);
83
84 if ( ! acct_config->conninfo) {
85 fd_log_debug("[app_acct] ERROR: 'ConnInfo' is missing in file '%s'."
86 " You can specify 'ConnInfo=\"\";' to use default parameters.", conffile);
87 return EINVAL;
88 }
89 if ( ! acct_config->tablename) {
90 fd_log_debug("[app_acct] ERROR: 'Table' value is missing in file '%s'.", conffile);
91 return EINVAL;
92 }
93
94 if (!TRACE_BOOL(FULL))
95 return 0;
96
97 struct fd_list * li;
98
99 fd_log_debug("[app_acct] Configuration dump:");
100 fd_log_debug(" Database:");
101 fd_log_debug(" ConnInfo ...... : '%s'", acct_config->conninfo ?: "<null>");
102 fd_log_debug(" Table name .... : '%s'", acct_config->tablename ?: "<null>");
103 fd_log_debug(" Timestamp field : '%s'", acct_config->tsfield ?: "<null>");
104 fd_log_debug(" Server name fld : '%s'", acct_config->srvnfield ?: "<null>");
105 fd_log_debug(" AVPs that will be saved to the database:");
106 for (li = acct_config->avps.next; li != &acct_config->avps; li = li->next) {
107 struct acct_conf_avp * a = (struct acct_conf_avp *)li;
108 fd_log_debug(" %-*s AVP%s saved in ", 30, a->avpname, a->required ? " [required]":"" );
109 if (a->multi) {
110 fd_log_debug("fields '%s[1..%d]' ", a->field?:a->avpname, a->multi);
111 } else {
112 fd_log_debug("field '%s' ", a->field?:a->avpname);
113 }
114 fd_log_debug("as ::%s", diam2db_types_mapping[a->avptype]);
115 }
116 fd_log_debug("[app_acct] Complete.");
117 return 0;
118}
119
120void acct_conf_free(void)
121{
122 TRACE_ENTRY();
123
124 if (!acct_config)
125 return;
126
127 /* Destroy the list */
128 while (!FD_IS_LIST_EMPTY(&acct_config->avps)) {
129 struct acct_conf_avp * a = (struct acct_conf_avp *)(acct_config->avps.next);
130 fd_list_unlink(&a->chain);
131 free(a->avpname);
132 free(a->field);
133 free(a);
134 }
135
136 /* destroy other data */
137 free(acct_config->conninfo);
138 free(acct_config->tablename);
139 free(acct_config->tsfield);
140 free(acct_config->srvnfield);
141
142 /* Done */
143 free(acct_config);
144 acct_config = NULL;
145}
146
147/* Parse the configuration file */
148int acct_conf_parse(char * conffile)
149{
150 extern FILE * acct_confin;
151 int ret;
152
153 TRACE_ENTRY("%p", conffile);
154
155 TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile);
156
157 acct_confin = fopen(conffile, "r");
158 if (acct_confin == NULL) {
159 ret = errno;
160 fd_log_debug("Unable to open extension configuration file %s for reading: %s", conffile, strerror(ret));
161 return ret;
162 }
163
164 ret = yyparse(conffile);
165
166 fclose(acct_confin);
167
168 if (ret != 0) {
169 TRACE_DEBUG (INFO, "Unable to parse the configuration file.");
170 return EINVAL;
171 }
172
173 return 0;
174}
175
176/* Function to report the errors */
177void yyerror (YYLTYPE *ploc, char * conffile, char const *s)
178{
179 LOG_E( "Error in configuration parsing");
180
181 if (ploc->first_line != ploc->last_line)
182 LOG_E("%s:%d.%d-%d.%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s);
183 else if (ploc->first_column != ploc->last_column)
184 LOG_E("%s:%d.%d-%d : %s", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s);
185 else
186 LOG_E("%s:%d.%d : %s", conffile, ploc->first_line, ploc->first_column, s);
187}
188
189static struct acct_conf_avp avpdata;
190
191%}
192
193/* Values returned by lex for token */
194%union {
195 char *string; /* The string is allocated by strdup in lex.*/
196 int integer; /* Store integer values */
197}
198
199/* In case of error in the lexical analysis */
200%token LEX_ERROR
201
202/* Keywords */
203%token FIELD
204%token REQUIRED
205%token MULTI
206%token CONNINFO
207%token TABLE
208%token TSFIELD
209%token SRVNFIELD
210
211/* Tokens and types */
212/* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */
213%token <string> QSTRING
214
215/* An integer value */
216%token <integer> INTEGER
217
218
219
220/* -------------------------------------- */
221%%
222
223 /* The grammar definition */
224conffile: /* empty grammar is OK for the parser (will be validated afterwards) */
225 | conffile avpline
226 | conffile conninfoline
227 | conffile tableline
228 | conffile tsfieldline
229 | conffile srvnfieldline
230 | conffile errors
231 {
232 yyerror(&yylloc, conffile, "An error occurred while parsing the configuration file.");
233 return EINVAL;
234 }
235 ;
236
237 /* Catch lexical and syntax errors */
238errors: LEX_ERROR
239 | error
240 ;
241
242 /* The tokens */
243avpline: {
244 memset(&avpdata, 0, sizeof(struct acct_conf_avp));
245 }
246 QSTRING avpcontents ';'
247 {
248 struct acct_conf_avp *new;
249 struct dict_object * dict;
250 struct dict_avp_data dictdata;
251
252 /* Validate the avp name first */
253 CHECK_FCT_DO( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME_ALL_VENDORS, $2, &dict, ENOENT),
254 { yyerror (&yylloc, conffile, "AVP definition not found in the dictionary. Was the appropriate dict_*.fdx extension loaded?"); YYERROR; } );
255 CHECK_FCT( fd_dict_getval( dict, &dictdata ));
256
257 /* Create a new entry */
258 CHECK_MALLOC_DO( new = malloc(sizeof(struct acct_conf_avp)),
259 { yyerror (&yylloc, conffile, "Out of memory"); YYERROR; } );
260
261 /* Retrieve all the data from avpcontents parsing (field, required, multi) */
262 memcpy(new, &avpdata, sizeof(struct acct_conf_avp));
263
264 /* Initialize the other data */
265 fd_list_init(&new->chain, NULL);
266 new->avpname = $2;
267 new->avpobj = dict;
268 new->avptype = dictdata.avp_basetype;
269
270 /* Add this new entry at the end of the list */
271 fd_list_insert_before( &acct_config->avps, &new->chain );
272 }
273 ;
274
275avpcontents: /* Empty content is fine */
276 | '=' '{' avpflagline '}'
277 ;
278
279avpflagline: /* Empty flags is also fine */
280 | avpflagline FIELD '=' QSTRING ';'
281 {
282 if (avpdata.field) {
283 yyerror (&yylloc, conffile, "Duplicate entry");
284 YYERROR;
285 }
286 avpdata.field = $4;
287 }
288 | avpflagline REQUIRED ';'
289 {
290 avpdata.required = 1;
291 }
292 | avpflagline MULTI '=' INTEGER ';'
293 {
294 avpdata.multi = (unsigned) $4;
295 }
296 ;
297
298conninfoline: CONNINFO '=' QSTRING ';'
299 {
300 if (acct_config->conninfo) {
301 yyerror (&yylloc, conffile, "Duplicate entry");
302 YYERROR;
303 }
304 acct_config->conninfo = $3;
305 }
306 ;
307
308tableline: TABLE '=' QSTRING ';'
309 {
310 if (acct_config->tablename) {
311 yyerror (&yylloc, conffile, "Duplicate entry");
312 YYERROR;
313 }
314 acct_config->tablename = $3;
315 }
316 ;
317
318tsfieldline: TSFIELD '=' QSTRING ';'
319 {
320 if (acct_config->tsfield) {
321 yyerror (&yylloc, conffile, "Duplicate entry");
322 YYERROR;
323 }
324 acct_config->tsfield = $3;
325 }
326 ;
327
328srvnfieldline: SRVNFIELD '=' QSTRING ';'
329 {
330 if (acct_config->srvnfield) {
331 yyerror (&yylloc, conffile, "Duplicate entry");
332 YYERROR;
333 }
334 acct_config->srvnfield = $3;
335 }
336 ;