525
|
1 /********************************************************************************************************* |
|
2 * Software License Agreement (BSD License) * |
|
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> * |
|
4 * * |
|
5 * Copyright (c) 2010, 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 */ |
|
38 |
|
39 /* For development only : */ |
|
40 %debug |
|
41 %error-verbose |
|
42 |
|
43 /* The parser receives the configuration file filename as parameter */ |
|
44 %parse-param {char * conffile} |
|
45 |
|
46 /* Keep track of location */ |
|
47 %locations |
|
48 %pure-parser |
|
49 |
|
50 %{ |
|
51 #include "rtereg.h" |
|
52 #include "rtereg_conf.tab.h" /* bison is not smart enough to define the YYLTYPE before including this code, so... */ |
|
53 |
|
54 /* Forward declaration */ |
|
55 int yyparse(char * conffile); |
|
56 |
|
57 /* Parse the configuration file */ |
|
58 int rtereg_conf_handle(char * conffile) |
|
59 { |
|
60 extern FILE * rtereg_confin; |
|
61 int ret; |
|
62 |
|
63 TRACE_ENTRY("%p", conffile); |
|
64 |
|
65 TRACE_DEBUG (FULL, "Parsing configuration file: %s...", conffile); |
|
66 |
|
67 rtereg_confin = fopen(conffile, "r"); |
|
68 if (rtereg_confin == NULL) { |
|
69 ret = errno; |
|
70 fd_log_debug("Unable to open extension configuration file %s for reading: %s\n", conffile, strerror(ret)); |
|
71 TRACE_DEBUG (INFO, "Error occurred, message logged -- configuration file."); |
|
72 return ret; |
|
73 } |
|
74 |
|
75 ret = yyparse(conffile); |
|
76 |
|
77 fclose(rtereg_confin); |
|
78 |
|
79 if (ret != 0) { |
|
80 TRACE_DEBUG (INFO, "Unable to parse the configuration file."); |
|
81 return EINVAL; |
|
82 } else { |
|
83 TRACE_DEBUG(FULL, "[rt-ereg] Added %d rules successfully.", rtereg_conf.rules_nb); |
|
84 } |
|
85 |
|
86 return 0; |
|
87 } |
|
88 |
|
89 /* The Lex parser prototype */ |
|
90 int rtereg_conflex(YYSTYPE *lvalp, YYLTYPE *llocp); |
|
91 |
|
92 /* Function to report the errors */ |
|
93 void yyerror (YYLTYPE *ploc, char * conffile, char const *s) |
|
94 { |
|
95 TRACE_DEBUG(INFO, "Error in configuration parsing"); |
|
96 |
|
97 if (ploc->first_line != ploc->last_line) |
|
98 fd_log_debug("%s:%d.%d-%d.%d : %s\n", conffile, ploc->first_line, ploc->first_column, ploc->last_line, ploc->last_column, s); |
|
99 else if (ploc->first_column != ploc->last_column) |
|
100 fd_log_debug("%s:%d.%d-%d : %s\n", conffile, ploc->first_line, ploc->first_column, ploc->last_column, s); |
|
101 else |
|
102 fd_log_debug("%s:%d.%d : %s\n", conffile, ploc->first_line, ploc->first_column, s); |
|
103 } |
|
104 |
|
105 %} |
|
106 |
|
107 /* Values returned by lex for token */ |
|
108 %union { |
|
109 char *string; /* The string is allocated by strdup in lex.*/ |
|
110 int integer; |
|
111 } |
|
112 |
|
113 /* In case of error in the lexical analysis */ |
|
114 %token LEX_ERROR |
|
115 |
|
116 /* A (de)quoted string (malloc'd in lex parser; it must be freed after use) */ |
|
117 %token <string> QSTRING |
|
118 %token <integer> INTEGER |
|
119 |
|
120 /* Tokens */ |
|
121 %token AVP |
|
122 |
|
123 |
|
124 /* -------------------------------------- */ |
|
125 %% |
|
126 |
|
127 /* The grammar definition */ |
|
128 conffile: rules avp rules |
|
129 ; |
|
130 |
|
131 /* a server entry */ |
|
132 avp: AVP '=' QSTRING ';' |
|
133 { |
|
134 if (rtereg_conf.avp != NULL) { |
|
135 yyerror(&yylloc, conffile, "Only one AVP can be specified"); |
|
136 YYERROR; |
|
137 } |
|
138 |
|
139 CHECK_FCT_DO( fd_dict_search ( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, $3, &rtereg_conf.avp, ENOENT ), |
|
140 { |
|
141 TRACE_DEBUG(INFO, "Unable to find '%s' AVP in the loaded dictionaries.", $3); |
|
142 yyerror (&yylloc, conffile, "Invalid AVP value."); |
|
143 YYERROR; |
|
144 } ); |
|
145 |
|
146 /* Now check the type */ |
|
147 { |
|
148 struct dict_avp_data data; |
|
149 CHECK_FCT( fd_dict_getval( rtereg_conf.avp, &data) ); |
|
150 CHECK_PARAMS_DO (data.avp_basetype == AVP_TYPE_OCTETSTRING, |
|
151 { |
|
152 TRACE_DEBUG(INFO, "'%s' AVP in not an OCTETSTRING AVP (%d).", $3, data.avp_basetype); |
|
153 yyerror (&yylloc, conffile, "AVP in not an OCTETSTRING type."); |
|
154 YYERROR; |
|
155 } ); |
|
156 } |
|
157 } |
|
158 ; |
|
159 |
|
160 rules: /* empty OK */ |
|
161 | rules rule |
|
162 ; |
|
163 |
|
164 rule: QSTRING ':' QSTRING '+' '=' INTEGER ';' |
|
165 { |
|
166 struct rtereg_rule * new; |
|
167 int err; |
|
168 |
|
169 /* Add new rule in the array */ |
|
170 rtereg_conf.rules_nb += 1; |
|
171 CHECK_MALLOC_DO(rtereg_conf.rules = realloc(rtereg_conf.rules, rtereg_conf.rules_nb * sizeof(struct rtereg_rule)), |
|
172 { |
|
173 yyerror (&yylloc, conffile, "Not enough memory to store the configuration..."); |
|
174 YYERROR; |
|
175 } ); |
|
176 |
|
177 new = &rtereg_conf.rules[rtereg_conf.rules_nb - 1]; |
|
178 |
|
179 new->pattern = $1; |
|
180 new->server = $3; |
|
181 new->score = $6; |
|
182 |
|
183 /* Attempt to compile the regex */ |
|
184 CHECK_FCT_DO( err=regcomp(&new->preg, new->pattern, REG_EXTENDED | REG_NOSUB), |
|
185 { |
|
186 char * buf; |
|
187 size_t bl; |
|
188 |
|
189 /* Error while compiling the regex */ |
|
190 TRACE_DEBUG(INFO, "Error while compiling the regular expression '%s':", new->pattern); |
|
191 |
|
192 /* Get the error message size */ |
|
193 bl = regerror(err, &new->preg, NULL, 0); |
|
194 |
|
195 /* Alloc the buffer for error message */ |
|
196 CHECK_MALLOC( buf = malloc(bl) ); |
|
197 |
|
198 /* Get the error message content */ |
|
199 regerror(err, &new->preg, buf, bl); |
|
200 TRACE_DEBUG(INFO, "\t%s", buf); |
|
201 |
|
202 /* Free the buffer, return the error */ |
|
203 free(buf); |
|
204 |
|
205 yyerror (&yylloc, conffile, "Invalid regular expression."); |
|
206 YYERROR; |
|
207 } ); |
|
208 } |
|
209 ; |