4e3128e28c3c9ff70c8ee300cb86826e8d03e4f7
[sip-router] / cfg.lex
1 /*
2  * $Id$
3  *
4  * scanner for cfg files
5  *
6  * Copyright (C) 2001-2003 Fhg Fokus
7  *
8  * This file is part of ser, a free SIP server.
9  *
10  * ser is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version
14  *
15  * For a license to use the ser software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * ser is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License 
26  * along with this program; if not, write to the Free Software 
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  *
29  * History:
30  * -------
31  *  2003-01-29  src_port added (jiri)
32  *  2003-01-23  mhomed added (jiri)
33  *  2003-03-19  replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei)
34  *  2003-04-01  added dst_port, proto (tcp, udp, tls), af(inet, inet6) (andrei)
35  *  2003-04-05  s/reply_route/failure_route, onreply_route introduced (jiri)
36  *  2003-04-12  added force_rport, chdir and wdir (andrei)
37  */
38
39
40 %{
41         #include "cfg.tab.h"
42         #include "dprint.h"
43         #include "globals.h"
44         #include "mem/mem.h"
45         #include <string.h>
46         #include <stdlib.h>
47         #include "ip_addr.h"
48
49
50         /* states */
51         #define INITIAL_S               0
52         #define COMMENT_S               1
53         #define COMMENT_LN_S    2
54         #define STRING_S                3
55
56         
57         static int comment_nest=0;
58         static int state=0;
59         static char* tstr=0;
60         int line=1;
61         int column=1;
62         int startcolumn=1;
63
64         static char* addstr(char*, char**);
65         static void count();
66
67
68 %}
69
70 /* start conditions */
71 %x STRING1 STRING2 COMMENT COMMENT_LN
72
73 /* action keywords */
74 FORWARD forward
75 FORWARD_TCP     forward_tcp
76 FORWARD_UDP     forward_udp
77 DROP    "drop"|"break"
78 SEND    send
79 SEND_TCP        send_tcp
80 LOG             log
81 ERROR   error
82 ROUTE   route
83 ROUTE_FAILURE failure_route
84 ROUTE_ONREPLY onreply_route
85 EXEC    exec
86 FORCE_RPORT             "force_rport"|"add_rport"
87 SETFLAG         setflag
88 RESETFLAG       resetflag
89 ISFLAGSET       isflagset
90 LEN_GT          len_gt
91 SET_HOST                "rewritehost"|"sethost"|"seth"
92 SET_HOSTPORT    "rewritehostport"|"sethostport"|"sethp"
93 SET_USER                "rewriteuser"|"setuser"|"setu"
94 SET_USERPASS    "rewriteuserpass"|"setuserpass"|"setup"
95 SET_PORT                "rewriteport"|"setport"|"setp"
96 SET_URI                 "rewriteuri"|"seturi"
97 REVERT_URI              "revert_uri"
98 PREFIX                  "prefix"
99 STRIP                   "strip"
100 APPEND_BRANCH   "append_branch"
101 IF                              "if"
102 ELSE                    "else"
103
104 /*ACTION LVALUES*/
105 URIHOST                 "uri:host"
106 URIPORT                 "uri:port"
107
108 MAX_LEN                 "max_len"
109
110
111 /* condition keywords */
112 METHOD  method
113 /* hack -- the second element in first line is referrable
114    as either uri or status; it only would makes sense to
115    call it "uri" from route{} and status from onreply_route{}
116 */
117 URI             "uri"|"status"
118 SRCIP   src_ip
119 SRCPORT src_port
120 DSTIP   dst_ip
121 DSTPORT dst_port
122 PROTO   proto
123 AF              af
124 MYSELF  myself
125 /* operators */
126 EQUAL   =
127 EQUAL_T ==
128 MATCH   =~
129 NOT             !|"not"
130 AND             "and"|"&&"|"&"
131 OR              "or"|"||"|"|"
132
133 /* config vars. */
134 DEBUG   debug
135 FORK    fork
136 LOGSTDERROR     log_stderror
137 LISTEN          listen
138 ALIAS           alias
139 DNS              dns
140 REV_DNS  rev_dns
141 PORT    port
142 STAT    statistics
143 MAXBUFFER maxbuffer
144 CHILDREN children
145 CHECK_VIA       check_via
146 SYN_BRANCH syn_branch
147 MEMLOG  memlog
148 SIP_WARNING sip_warning
149 FIFO fifo
150 FIFO_MODE fifo_mode
151 SERVER_SIGNATURE server_signature
152 REPLY_TO_VIA reply_to_via
153 USER            "user"|"uid"
154 GROUP           "group"|"gid"
155 CHROOT          "chroot"
156 WDIR            "workdir"|"wdir"
157 MHOMED          mhomed
158
159 LOADMODULE      loadmodule
160 MODPARAM        modparam
161
162 /* values */
163 YES                     "yes"|"true"|"on"|"enable"
164 NO                      "no"|"false"|"off"|"disable"
165 UDP                     "udp"
166 TCP                     "tcp"
167 TLS                     "tls"
168 INET            "inet"
169 INET6           "inet6"
170
171 LETTER          [a-zA-Z]
172 DIGIT           [0-9]
173 ALPHANUM        {LETTER}|{DIGIT}|[_]
174 NUMBER          {DIGIT}+
175 ID                      {LETTER}{ALPHANUM}*
176 HEX                     [0-9a-fA-F]
177 HEX4            {HEX}{1,4}
178 IPV6ADDR        ({HEX4}":"){7}{HEX4}|({HEX4}":"){1,7}(":"{HEX4}){1,7}|":"(":"{HEX4}){1,7}|({HEX4}":"){1,7}":"|"::"
179 QUOTES          \"
180 TICK            \'
181 SLASH           "/"
182 SEMICOLON       ;
183 RPAREN          \)
184 LPAREN          \(
185 LBRACE          \{
186 RBRACE          \}
187 LBRACK          \[
188 RBRACK          \]
189 COMMA           ","
190 DOT                     \.
191 CR                      \n
192
193
194
195 COM_LINE        #
196 COM_START       "/\*"
197 COM_END         "\*/"
198
199 EAT_ABLE        [\ \t\b\r]
200
201 %%
202
203
204 <INITIAL>{EAT_ABLE}     { count(); }
205
206 <INITIAL>{FORWARD}      {count(); yylval.strval=yytext; return FORWARD; }
207 <INITIAL>{FORWARD_TCP}  {count(); yylval.strval=yytext; return FORWARD_TCP; }
208 <INITIAL>{FORWARD_UDP}  {count(); yylval.strval=yytext; return FORWARD_UDP; }
209 <INITIAL>{DROP} { count(); yylval.strval=yytext; return DROP; }
210 <INITIAL>{SEND} { count(); yylval.strval=yytext; return SEND; }
211 <INITIAL>{SEND_TCP}     { count(); yylval.strval=yytext; return SEND_TCP; }
212 <INITIAL>{LOG}  { count(); yylval.strval=yytext; return LOG_TOK; }
213 <INITIAL>{ERROR}        { count(); yylval.strval=yytext; return ERROR; }
214 <INITIAL>{SETFLAG}      { count(); yylval.strval=yytext; return SETFLAG; }
215 <INITIAL>{RESETFLAG}    { count(); yylval.strval=yytext; return RESETFLAG; }
216 <INITIAL>{ISFLAGSET}    { count(); yylval.strval=yytext; return ISFLAGSET; }
217 <INITIAL>{LEN_GT}       { count(); yylval.strval=yytext; return LEN_GT; }
218 <INITIAL>{ROUTE}        { count(); yylval.strval=yytext; return ROUTE; }
219 <INITIAL>{ROUTE_ONREPLY}        { count(); yylval.strval=yytext;
220                                                                 return ROUTE_ONREPLY; }
221 <INITIAL>{ROUTE_FAILURE}        { count(); yylval.strval=yytext;
222                                                                 return ROUTE_FAILURE; }
223 <INITIAL>{EXEC} { count(); yylval.strval=yytext; return EXEC; }
224 <INITIAL>{SET_HOST}     { count(); yylval.strval=yytext; return SET_HOST; }
225 <INITIAL>{SET_HOSTPORT} { count(); yylval.strval=yytext; return SET_HOSTPORT; }
226 <INITIAL>{SET_USER}     { count(); yylval.strval=yytext; return SET_USER; }
227 <INITIAL>{SET_USERPASS} { count(); yylval.strval=yytext; return SET_USERPASS; }
228 <INITIAL>{SET_PORT}     { count(); yylval.strval=yytext; return SET_PORT; }
229 <INITIAL>{SET_URI}      { count(); yylval.strval=yytext; return SET_URI; }
230 <INITIAL>{REVERT_URI}   { count(); yylval.strval=yytext; return REVERT_URI; }
231 <INITIAL>{PREFIX}       { count(); yylval.strval=yytext; return PREFIX; }
232 <INITIAL>{STRIP}        { count(); yylval.strval=yytext; return STRIP; }
233 <INITIAL>{APPEND_BRANCH}        { count(); yylval.strval=yytext; 
234                                                                 return APPEND_BRANCH; }
235 <INITIAL>{FORCE_RPORT}  { count(); yylval.strval=yytext; return FORCE_RPORT; }
236         
237 <INITIAL>{IF}   { count(); yylval.strval=yytext; return IF; }
238 <INITIAL>{ELSE} { count(); yylval.strval=yytext; return ELSE; }
239
240 <INITIAL>{URIHOST}      { count(); yylval.strval=yytext; return URIHOST; }
241 <INITIAL>{URIPORT}      { count(); yylval.strval=yytext; return URIPORT; }
242
243 <INITIAL>{MAX_LEN}      { count(); yylval.strval=yytext; return MAX_LEN; }
244
245 <INITIAL>{METHOD}       { count(); yylval.strval=yytext; return METHOD; }
246 <INITIAL>{URI}  { count(); yylval.strval=yytext; return URI; }
247 <INITIAL>{SRCIP}        { count(); yylval.strval=yytext; return SRCIP; }
248 <INITIAL>{SRCPORT}      { count(); yylval.strval=yytext; return SRCPORT; }
249 <INITIAL>{DSTIP}        { count(); yylval.strval=yytext; return DSTIP; }
250 <INITIAL>{DSTPORT}      { count(); yylval.strval=yytext; return DSTPORT; }
251 <INITIAL>{PROTO}        { count(); yylval.strval=yytext; return PROTO; }
252 <INITIAL>{AF}   { count(); yylval.strval=yytext; return AF; }
253 <INITIAL>{MYSELF}       { count(); yylval.strval=yytext; return MYSELF; }
254
255 <INITIAL>{DEBUG}        { count(); yylval.strval=yytext; return DEBUG; }
256 <INITIAL>{FORK}         { count(); yylval.strval=yytext; return FORK; }
257 <INITIAL>{LOGSTDERROR}  { yylval.strval=yytext; return LOGSTDERROR; }
258 <INITIAL>{LISTEN}       { count(); yylval.strval=yytext; return LISTEN; }
259 <INITIAL>{ALIAS}        { count(); yylval.strval=yytext; return ALIAS; }
260 <INITIAL>{DNS}  { count(); yylval.strval=yytext; return DNS; }
261 <INITIAL>{REV_DNS}      { count(); yylval.strval=yytext; return REV_DNS; }
262 <INITIAL>{PORT} { count(); yylval.strval=yytext; return PORT; }
263 <INITIAL>{STAT} { count(); yylval.strval=yytext; return STAT; }
264 <INITIAL>{MAXBUFFER}    { count(); yylval.strval=yytext; return MAXBUFFER; }
265 <INITIAL>{CHILDREN}     { count(); yylval.strval=yytext; return CHILDREN; }
266 <INITIAL>{CHECK_VIA}    { count(); yylval.strval=yytext; return CHECK_VIA; }
267 <INITIAL>{SYN_BRANCH}   { count(); yylval.strval=yytext; return SYN_BRANCH; }
268 <INITIAL>{MEMLOG}       { count(); yylval.strval=yytext; return MEMLOG; }
269 <INITIAL>{SIP_WARNING}  { count(); yylval.strval=yytext; return SIP_WARNING; }
270 <INITIAL>{USER}         { count(); yylval.strval=yytext; return USER; }
271 <INITIAL>{GROUP}        { count(); yylval.strval=yytext; return GROUP; }
272 <INITIAL>{CHROOT}       { count(); yylval.strval=yytext; return CHROOT; }
273 <INITIAL>{WDIR} { count(); yylval.strval=yytext; return WDIR; }
274 <INITIAL>{MHOMED}       { count(); yylval.strval=yytext; return MHOMED; }
275 <INITIAL>{FIFO} { count(); yylval.strval=yytext; return FIFO; }
276 <INITIAL>{FIFO_MODE}    { count(); yylval.strval=yytext; return FIFO_MODE; }
277 <INITIAL>{SERVER_SIGNATURE}     { count(); yylval.strval=yytext; return SERVER_SIGNATURE; }
278 <INITIAL>{REPLY_TO_VIA} { count(); yylval.strval=yytext; return REPLY_TO_VIA; }
279 <INITIAL>{LOADMODULE}   { count(); yylval.strval=yytext; return LOADMODULE; }
280 <INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
281
282 <INITIAL>{EQUAL}        { count(); return EQUAL; }
283 <INITIAL>{EQUAL_T}      { count(); return EQUAL_T; }
284 <INITIAL>{MATCH}        { count(); return MATCH; }
285 <INITIAL>{NOT}          { count(); return NOT; }
286 <INITIAL>{AND}          { count(); return AND; }
287 <INITIAL>{OR}           { count(); return OR;  }
288
289
290
291 <INITIAL>{IPV6ADDR}             { count(); yylval.strval=yytext; return IPV6ADDR; }
292 <INITIAL>{NUMBER}               { count(); yylval.intval=atoi(yytext);return NUMBER; }
293 <INITIAL>{YES}                  { count(); yylval.intval=1; return NUMBER; }
294 <INITIAL>{NO}                   { count(); yylval.intval=0; return NUMBER; }
295 <INITIAL>{TCP}                  { count(); yylval.intval=PROTO_TCP; return NUMBER; }
296 <INITIAL>{UDP}                  { count(); yylval.intval=PROTO_UDP; return NUMBER; }
297 <INITIAL>{TLS}                  { count(); yylval.intval=PROTO_TLS; return NUMBER; }
298 <INITIAL>{INET}                 { count(); yylval.intval=AF_INET; return NUMBER; }
299 <INITIAL>{INET6}                { count();
300                                                 #ifdef USE_IPV6
301                                                   yylval.intval=AF_INET6;
302                                                 #else
303                                                   yylval.intval=-1; /* no match*/
304                                                 #endif
305                                                   return NUMBER; }
306
307 <INITIAL>{COMMA}                { count(); return COMMA; }
308 <INITIAL>{SEMICOLON}    { count(); return SEMICOLON; }
309 <INITIAL>{RPAREN}       { count(); return RPAREN; }
310 <INITIAL>{LPAREN}       { count(); return LPAREN; }
311 <INITIAL>{LBRACE}       { count(); return LBRACE; }
312 <INITIAL>{RBRACE}       { count(); return RBRACE; }
313 <INITIAL>{LBRACK}       { count(); return LBRACK; }
314 <INITIAL>{RBRACK}       { count(); return RBRACK; }
315 <INITIAL>{SLASH}        { count(); return SLASH; }
316 <INITIAL>{DOT}          { count(); return DOT; }
317 <INITIAL>\\{CR}         {count(); } /* eat the escaped CR */
318 <INITIAL>{CR}           { count();/* return CR;*/ }
319
320
321 <INITIAL>{QUOTES} { count(); state=STRING_S; BEGIN(STRING1); }
322 <INITIAL>{TICK} { count(); state=STRING_S; BEGIN(STRING2); }
323
324
325 <STRING1>{QUOTES} { count(); state=INITIAL_S; BEGIN(INITIAL); 
326                                                 yytext[yyleng-1]=0; yyleng--;
327                                                 addstr(yytext, &tstr);
328                                                 yylval.strval=tstr; tstr=0;
329                                                 return STRING;
330                                         }
331 <STRING2>{TICK}  { count(); state=INITIAL_S; BEGIN(INITIAL); 
332                                                 yytext[yyleng-1]=0; yyleng--;
333                                                 addstr(yytext, &tstr);
334                                                 yylval.strval=tstr;
335                                                 tstr=0;
336                                                 return STRING;
337                                         }
338 <STRING2>.|{EAT_ABLE}|{CR}      { yymore(); }
339
340 <STRING1>\\n            { count(); yytext[yyleng-2]='\n';yytext[yyleng-1]=0; 
341                                                 yyleng--; addstr(yytext, &tstr); }
342 <STRING1>\\r            { count(); yytext[yyleng-2]='\r';yytext[yyleng-1]=0; 
343                                                 yyleng--; addstr(yytext, &tstr); }
344 <STRING1>\\a            { count(); yytext[yyleng-2]='\a';yytext[yyleng-1]=0; 
345                                                 yyleng--; addstr(yytext, &tstr); }
346 <STRING1>\\t            { count(); yytext[yyleng-2]='\t';yytext[yyleng-1]=0; 
347                                                 yyleng--; addstr(yytext, &tstr); }
348 <STRING1>\\\\           { count(); yytext[yyleng-2]='\\';yytext[yyleng-1]=0; 
349                                                 yyleng--; addstr(yytext, &tstr); } 
350 <STRING1>.|{EAT_ABLE}|{CR}      { yymore(); }
351
352
353 <INITIAL,COMMENT>{COM_START}    { count(); comment_nest++; state=COMMENT_S;
354                                                                                 BEGIN(COMMENT); }
355 <COMMENT>{COM_END}                              { count(); comment_nest--;
356                                                                                 if (comment_nest==0){
357                                                                                         state=INITIAL_S;
358                                                                                         BEGIN(INITIAL);
359                                                                                 }
360                                                                 }
361 <COMMENT>.|{EAT_ABLE}|{CR}                              { count(); };
362
363 <INITIAL>{COM_LINE}.*{CR}       { count(); } 
364
365 <INITIAL>{ID}                   { count(); addstr(yytext, &tstr);
366                                                   yylval.strval=tstr; tstr=0; return ID; }
367
368
369 <<EOF>>                                                 {
370                                                                         switch(state){
371                                                                                 case STRING_S: 
372                                                                                         LOG(L_CRIT, "ERROR: cfg. parser: unexpected EOF in"
373                                                                                                                 " unclosed string\n");
374                                                                                         if (tstr) {pkg_free(tstr);tstr=0;}
375                                                                                         break;
376                                                                                 case COMMENT_S:
377                                                                                         LOG(L_CRIT, "ERROR: cfg. parser: unexpected EOF:"
378                                                                                                                 " %d comments open\n", comment_nest);
379                                                                                         break;
380                                                                                 case COMMENT_LN_S:
381                                                                                         LOG(L_CRIT, "ERROR: unexpected EOF:"
382                                                                                                                 "comment line open\n");
383                                                                                         break;
384                                                                         }
385                                                                         return 0;
386                                                                 }
387                         
388 %%
389
390 static char* addstr(char * src, char ** dest)
391 {
392         char *tmp;
393         unsigned len1, len2;
394         
395         if (*dest==0){
396                 len1 = strlen(src);
397                 *dest = pkg_malloc(len1 + 1);
398                 if (*dest == 0) goto error;
399                 memcpy(*dest, src, len1 + 1);
400         }else{
401                 len1=strlen(*dest);
402                 len2=strlen(src);
403                 tmp=pkg_malloc(len1+len2+1);
404                 if (tmp==0) goto error;
405                 memcpy(tmp, *dest, len1);
406                 memcpy(tmp+len1, src, len2);
407                 tmp[len1+len2]=0;
408                 pkg_free(*dest);
409                 *dest=tmp;
410         }
411         return *dest;
412 error:
413         LOG(L_CRIT, "ERROR:lex:addstr: memory allocation error\n");
414         return 0;
415 }
416
417
418
419 static void count()
420 {
421         int i;
422         
423         startcolumn=column;
424         for (i=0; i<yyleng;i++){
425                 if (yytext[i]=='\n'){
426                         line++;
427                         column=startcolumn=1;
428                 }else if (yytext[i]=='\t'){
429                         column++;
430                         /*column+=8 -(column%8);*/
431                 }else{
432                         column++;
433                 }
434         }
435 }
436
437