/* * $Id$ * * scanner for cfg files * * Copyright (C) 2001-2003 Fhg Fokus * * This file is part of ser, a free SIP server. * * ser is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version * * For a license to use the ser software under conditions * other than those described here, or to purchase support for this * software, please contact iptel.org by e-mail at the following addresses: * info@iptel.org * * ser is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * History: * ------- * 2003-01-29 src_port added (jiri) * 2003-01-23 mhomed added (jiri) * 2003-03-19 replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei) * 2003-04-01 added dst_port, proto (tcp, udp, tls), af(inet, inet6) (andrei) * 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri) * 2003-04-12 added force_rport, chdir and wdir (andrei) * 2003-04-22 strip_tail added (jiri) * 2003-07-03 tls* (disable, certificate, private_key, ca_list, verify, * require_certificate added (andrei) * 2003-07-06 more tls config. vars added: tls_method, tls_port_no (andrei) * 2003-10-02 added {,set_}advertised_{address,port} (andrei) * 2003-10-07 added hex and octal numbers support (andrei) * 2003-10-10 replaced len_gt w/ msg:len (andrei) * 2003-11-29 added {tcp_send, tcp_connect, tls_*}_timeout (andrei) */ %{ #include "cfg.tab.h" #include "dprint.h" #include "globals.h" #include "mem/mem.h" #include #include #include "ip_addr.h" /* states */ #define INITIAL_S 0 #define COMMENT_S 1 #define COMMENT_LN_S 2 #define STRING_S 3 static int comment_nest=0; static int state=0; static char* tstr=0; int line=1; int column=1; int startcolumn=1; static char* addstr(char*, char**); static void count(); %} /* start conditions */ %x STRING1 STRING2 COMMENT COMMENT_LN /* action keywords */ FORWARD forward FORWARD_TCP forward_tcp FORWARD_UDP forward_udp FORWARD_TLS forward_tls DROP "drop"|"break" SEND send SEND_TCP send_tcp LOG log ERROR error ROUTE route ROUTE_FAILURE failure_route ROUTE_ONREPLY onreply_route EXEC exec FORCE_RPORT "force_rport"|"add_rport" SETFLAG setflag RESETFLAG resetflag ISFLAGSET isflagset SET_HOST "rewritehost"|"sethost"|"seth" SET_HOSTPORT "rewritehostport"|"sethostport"|"sethp" SET_USER "rewriteuser"|"setuser"|"setu" SET_USERPASS "rewriteuserpass"|"setuserpass"|"setup" SET_PORT "rewriteport"|"setport"|"setp" SET_URI "rewriteuri"|"seturi" REVERT_URI "revert_uri" PREFIX "prefix" STRIP "strip" STRIP_TAIL "strip_tail" APPEND_BRANCH "append_branch" IF "if" ELSE "else" SET_ADV_ADDRESS "set_advertised_address" SET_ADV_PORT "set_advertised_port" /*ACTION LVALUES*/ URIHOST "uri:host" URIPORT "uri:port" MAX_LEN "max_len" /* condition keywords */ METHOD method /* hack -- the second element in first line is referrable as either uri or status; it only would makes sense to call it "uri" from route{} and status from onreply_route{} */ URI "uri"|"status" SRCIP src_ip SRCPORT src_port DSTIP dst_ip DSTPORT dst_port PROTO proto AF af MYSELF myself MSGLEN "msg:len" /* operators */ EQUAL = EQUAL_T == GT > LT < GTE >= LTE <= DIFF != MATCH =~ NOT !|"not" AND "and"|"&&"|"&" OR "or"|"||"|"|" /* config vars. */ DEBUG debug FORK fork LOGSTDERROR log_stderror LOGFACILITY log_facility LISTEN listen ALIAS alias DNS dns REV_DNS rev_dns PORT port STAT statistics MAXBUFFER maxbuffer CHILDREN children CHECK_VIA check_via SYN_BRANCH syn_branch MEMLOG "memlog"|"mem_log" SIP_WARNING sip_warning FIFO fifo FIFO_MODE fifo_mode SERVER_SIGNATURE server_signature REPLY_TO_VIA reply_to_via USER "user"|"uid" GROUP "group"|"gid" CHROOT "chroot" WDIR "workdir"|"wdir" MHOMED mhomed DISABLE_TCP "disable_tcp" TCP_CHILDREN "tcp_children" TCP_SEND_TIMEOUT "tcp_send_timeout" TCP_CONNECT_TIMEOUT "tcp_connect_timeout" DISABLE_TLS "disable_tls" TLSLOG "tlslog"|"tls_log" TLS_PORT_NO "tls_port_no" TLS_METHOD "tls_method" TLS_VERIFY "tls_verify" TLS_REQUIRE_CERTIFICATE "tls_require_certificate" TLS_CERTIFICATE "tls_certificate" TLS_PRIVATE_KEY "tls_private_key" TLS_CA_LIST "tls_ca_list" TLS_HANDSHAKE_TIMEOUT "tls_handshake_timeout" TLS_SEND_TIMEOUT "tls_send_timeout" ADVERTISED_ADDRESS "advertised_address" ADVERTISED_PORT "advertised_port" LOADMODULE loadmodule MODPARAM modparam /* values */ YES "yes"|"true"|"on"|"enable" NO "no"|"false"|"off"|"disable" UDP "udp"|"UDP" TCP "tcp"|"TCP" TLS "tls"|"TLS" INET "inet"|"INET" INET6 "inet6"|"INET6" SSLv23 "sslv23"|"SSLv23"|"SSLV23" SSLv2 "sslv2"|"SSLv2"|"SSLV2" SSLv3 "sslv3"|"SSLv3"|"SSLV3" TLSv1 "tlsv1"|"TLSv1"|"TLSV1" LETTER [a-zA-Z] DIGIT [0-9] ALPHANUM {LETTER}|{DIGIT}|[_] NUMBER 0|([1-9]{DIGIT}*) ID {LETTER}{ALPHANUM}* HEX [0-9a-fA-F] HEXNUMBER 0x{HEX}+ OCTNUMBER 0[0-7]+ HEX4 {HEX}{1,4} IPV6ADDR ({HEX4}":"){7}{HEX4}|({HEX4}":"){1,7}(":"{HEX4}){1,7}|":"(":"{HEX4}){1,7}|({HEX4}":"){1,7}":"|"::" QUOTES \" TICK \' SLASH "/" SEMICOLON ; RPAREN \) LPAREN \( LBRACE \{ RBRACE \} LBRACK \[ RBRACK \] COMMA "," DOT \. CR \n COM_LINE # COM_START "/\*" COM_END "\*/" EAT_ABLE [\ \t\b\r] %% {EAT_ABLE} { count(); } {FORWARD} {count(); yylval.strval=yytext; return FORWARD; } {FORWARD_TCP} {count(); yylval.strval=yytext; return FORWARD_TCP; } {FORWARD_TLS} {count(); yylval.strval=yytext; return FORWARD_TLS; } {FORWARD_UDP} {count(); yylval.strval=yytext; return FORWARD_UDP; } {DROP} { count(); yylval.strval=yytext; return DROP; } {SEND} { count(); yylval.strval=yytext; return SEND; } {SEND_TCP} { count(); yylval.strval=yytext; return SEND_TCP; } {LOG} { count(); yylval.strval=yytext; return LOG_TOK; } {ERROR} { count(); yylval.strval=yytext; return ERROR; } {SETFLAG} { count(); yylval.strval=yytext; return SETFLAG; } {RESETFLAG} { count(); yylval.strval=yytext; return RESETFLAG; } {ISFLAGSET} { count(); yylval.strval=yytext; return ISFLAGSET; } {MSGLEN} { count(); yylval.strval=yytext; return MSGLEN; } {ROUTE} { count(); yylval.strval=yytext; return ROUTE; } {ROUTE_ONREPLY} { count(); yylval.strval=yytext; return ROUTE_ONREPLY; } {ROUTE_FAILURE} { count(); yylval.strval=yytext; return ROUTE_FAILURE; } {EXEC} { count(); yylval.strval=yytext; return EXEC; } {SET_HOST} { count(); yylval.strval=yytext; return SET_HOST; } {SET_HOSTPORT} { count(); yylval.strval=yytext; return SET_HOSTPORT; } {SET_USER} { count(); yylval.strval=yytext; return SET_USER; } {SET_USERPASS} { count(); yylval.strval=yytext; return SET_USERPASS; } {SET_PORT} { count(); yylval.strval=yytext; return SET_PORT; } {SET_URI} { count(); yylval.strval=yytext; return SET_URI; } {REVERT_URI} { count(); yylval.strval=yytext; return REVERT_URI; } {PREFIX} { count(); yylval.strval=yytext; return PREFIX; } {STRIP} { count(); yylval.strval=yytext; return STRIP; } {STRIP_TAIL} { count(); yylval.strval=yytext; return STRIP_TAIL; } {APPEND_BRANCH} { count(); yylval.strval=yytext; return APPEND_BRANCH; } {FORCE_RPORT} { count(); yylval.strval=yytext; return FORCE_RPORT; } {IF} { count(); yylval.strval=yytext; return IF; } {ELSE} { count(); yylval.strval=yytext; return ELSE; } {SET_ADV_ADDRESS} { count(); yylval.strval=yytext; return SET_ADV_ADDRESS; } {SET_ADV_PORT} { count(); yylval.strval=yytext; return SET_ADV_PORT; } {URIHOST} { count(); yylval.strval=yytext; return URIHOST; } {URIPORT} { count(); yylval.strval=yytext; return URIPORT; } {MAX_LEN} { count(); yylval.strval=yytext; return MAX_LEN; } {METHOD} { count(); yylval.strval=yytext; return METHOD; } {URI} { count(); yylval.strval=yytext; return URI; } {SRCIP} { count(); yylval.strval=yytext; return SRCIP; } {SRCPORT} { count(); yylval.strval=yytext; return SRCPORT; } {DSTIP} { count(); yylval.strval=yytext; return DSTIP; } {DSTPORT} { count(); yylval.strval=yytext; return DSTPORT; } {PROTO} { count(); yylval.strval=yytext; return PROTO; } {AF} { count(); yylval.strval=yytext; return AF; } {MYSELF} { count(); yylval.strval=yytext; return MYSELF; } {DEBUG} { count(); yylval.strval=yytext; return DEBUG; } {FORK} { count(); yylval.strval=yytext; return FORK; } {LOGSTDERROR} { yylval.strval=yytext; return LOGSTDERROR; } {LOGFACILITY} { yylval.strval=yytext; return LOGFACILITY; } {LISTEN} { count(); yylval.strval=yytext; return LISTEN; } {ALIAS} { count(); yylval.strval=yytext; return ALIAS; } {DNS} { count(); yylval.strval=yytext; return DNS; } {REV_DNS} { count(); yylval.strval=yytext; return REV_DNS; } {PORT} { count(); yylval.strval=yytext; return PORT; } {STAT} { count(); yylval.strval=yytext; return STAT; } {MAXBUFFER} { count(); yylval.strval=yytext; return MAXBUFFER; } {CHILDREN} { count(); yylval.strval=yytext; return CHILDREN; } {CHECK_VIA} { count(); yylval.strval=yytext; return CHECK_VIA; } {SYN_BRANCH} { count(); yylval.strval=yytext; return SYN_BRANCH; } {MEMLOG} { count(); yylval.strval=yytext; return MEMLOG; } {SIP_WARNING} { count(); yylval.strval=yytext; return SIP_WARNING; } {USER} { count(); yylval.strval=yytext; return USER; } {GROUP} { count(); yylval.strval=yytext; return GROUP; } {CHROOT} { count(); yylval.strval=yytext; return CHROOT; } {WDIR} { count(); yylval.strval=yytext; return WDIR; } {MHOMED} { count(); yylval.strval=yytext; return MHOMED; } {DISABLE_TCP} { count(); yylval.strval=yytext; return DISABLE_TCP; } {TCP_CHILDREN} { count(); yylval.strval=yytext; return TCP_CHILDREN; } {TCP_SEND_TIMEOUT} { count(); yylval.strval=yytext; return TCP_SEND_TIMEOUT; } {TCP_CONNECT_TIMEOUT} { count(); yylval.strval=yytext; return TCP_CONNECT_TIMEOUT; } {DISABLE_TLS} { count(); yylval.strval=yytext; return DISABLE_TLS; } {TLSLOG} { count(); yylval.strval=yytext; return TLS_PORT_NO; } {TLS_PORT_NO} { count(); yylval.strval=yytext; return TLS_PORT_NO; } {TLS_METHOD} { count(); yylval.strval=yytext; return TLS_METHOD; } {TLS_VERIFY} { count(); yylval.strval=yytext; return TLS_VERIFY; } {TLS_REQUIRE_CERTIFICATE} { count(); yylval.strval=yytext; return TLS_REQUIRE_CERTIFICATE; } {TLS_CERTIFICATE} { count(); yylval.strval=yytext; return TLS_CERTIFICATE; } {TLS_PRIVATE_KEY} { count(); yylval.strval=yytext; return TLS_PRIVATE_KEY; } {TLS_CA_LIST} { count(); yylval.strval=yytext; return TLS_CA_LIST; } {TLS_HANDSHAKE_TIMEOUT} { count(); yylval.strval=yytext; return TLS_HANDSHAKE_TIMEOUT; } {TLS_SEND_TIMEOUT} { count(); yylval.strval=yytext; return TLS_SEND_TIMEOUT; } {FIFO} { count(); yylval.strval=yytext; return FIFO; } {FIFO_MODE} { count(); yylval.strval=yytext; return FIFO_MODE; } {SERVER_SIGNATURE} { count(); yylval.strval=yytext; return SERVER_SIGNATURE; } {REPLY_TO_VIA} { count(); yylval.strval=yytext; return REPLY_TO_VIA; } {ADVERTISED_ADDRESS} { count(); yylval.strval=yytext; return ADVERTISED_ADDRESS; } {ADVERTISED_PORT} { count(); yylval.strval=yytext; return ADVERTISED_PORT; } {LOADMODULE} { count(); yylval.strval=yytext; return LOADMODULE; } {MODPARAM} { count(); yylval.strval=yytext; return MODPARAM; } {EQUAL} { count(); return EQUAL; } {EQUAL_T} { count(); return EQUAL_T; } {GT} { count(); return GT; } {LT} { count(); return LT; } {GTE} { count(); return GTE; } {LTE} { count(); return LTE; } {DIFF} { count(); return DIFF; } {MATCH} { count(); return MATCH; } {NOT} { count(); return NOT; } {AND} { count(); return AND; } {OR} { count(); return OR; } {IPV6ADDR} { count(); yylval.strval=yytext; return IPV6ADDR; } {NUMBER} { count(); yylval.intval=atoi(yytext);return NUMBER; } {HEXNUMBER} { count(); yylval.intval=(int)strtol(yytext, 0, 16); return NUMBER; } {OCTNUMBER} { count(); yylval.intval=(int)strtol(yytext, 0, 8); return NUMBER; } {YES} { count(); yylval.intval=1; return NUMBER; } {NO} { count(); yylval.intval=0; return NUMBER; } {TCP} { count(); yylval.intval=PROTO_TCP; return NUMBER; } {UDP} { count(); yylval.intval=PROTO_UDP; return NUMBER; } {TLS} { count(); yylval.intval=PROTO_TLS; return NUMBER; } {INET} { count(); yylval.intval=AF_INET; return NUMBER; } {INET6} { count(); #ifdef USE_IPV6 yylval.intval=AF_INET6; #else yylval.intval=-1; /* no match*/ #endif return NUMBER; } {SSLv23} { count(); yylval.strval=yytext; return SSLv23; } {SSLv2} { count(); yylval.strval=yytext; return SSLv2; } {SSLv3} { count(); yylval.strval=yytext; return SSLv3; } {TLSv1} { count(); yylval.strval=yytext; return TLSv1; } {COMMA} { count(); return COMMA; } {SEMICOLON} { count(); return SEMICOLON; } {RPAREN} { count(); return RPAREN; } {LPAREN} { count(); return LPAREN; } {LBRACE} { count(); return LBRACE; } {RBRACE} { count(); return RBRACE; } {LBRACK} { count(); return LBRACK; } {RBRACK} { count(); return RBRACK; } {SLASH} { count(); return SLASH; } {DOT} { count(); return DOT; } \\{CR} {count(); } /* eat the escaped CR */ {CR} { count();/* return CR;*/ } {QUOTES} { count(); state=STRING_S; BEGIN(STRING1); } {TICK} { count(); state=STRING_S; BEGIN(STRING2); } {QUOTES} { count(); state=INITIAL_S; BEGIN(INITIAL); yytext[yyleng-1]=0; yyleng--; addstr(yytext, &tstr); yylval.strval=tstr; tstr=0; return STRING; } {TICK} { count(); state=INITIAL_S; BEGIN(INITIAL); yytext[yyleng-1]=0; yyleng--; addstr(yytext, &tstr); yylval.strval=tstr; tstr=0; return STRING; } .|{EAT_ABLE}|{CR} { yymore(); } \\n { count(); yytext[yyleng-2]='\n';yytext[yyleng-1]=0; yyleng--; addstr(yytext, &tstr); } \\r { count(); yytext[yyleng-2]='\r';yytext[yyleng-1]=0; yyleng--; addstr(yytext, &tstr); } \\a { count(); yytext[yyleng-2]='\a';yytext[yyleng-1]=0; yyleng--; addstr(yytext, &tstr); } \\t { count(); yytext[yyleng-2]='\t';yytext[yyleng-1]=0; yyleng--; addstr(yytext, &tstr); } \\\\ { count(); yytext[yyleng-2]='\\';yytext[yyleng-1]=0; yyleng--; addstr(yytext, &tstr); } .|{EAT_ABLE}|{CR} { yymore(); } {COM_START} { count(); comment_nest++; state=COMMENT_S; BEGIN(COMMENT); } {COM_END} { count(); comment_nest--; if (comment_nest==0){ state=INITIAL_S; BEGIN(INITIAL); } } .|{EAT_ABLE}|{CR} { count(); }; {COM_LINE}.*{CR} { count(); } {ID} { count(); addstr(yytext, &tstr); yylval.strval=tstr; tstr=0; return ID; } <> { switch(state){ case STRING_S: LOG(L_CRIT, "ERROR: cfg. parser: unexpected EOF in" " unclosed string\n"); if (tstr) {pkg_free(tstr);tstr=0;} break; case COMMENT_S: LOG(L_CRIT, "ERROR: cfg. parser: unexpected EOF:" " %d comments open\n", comment_nest); break; case COMMENT_LN_S: LOG(L_CRIT, "ERROR: unexpected EOF:" "comment line open\n"); break; } return 0; } %% static char* addstr(char * src, char ** dest) { char *tmp; unsigned len1, len2; if (*dest==0){ len1 = strlen(src); *dest = pkg_malloc(len1 + 1); if (*dest == 0) goto error; memcpy(*dest, src, len1 + 1); }else{ len1=strlen(*dest); len2=strlen(src); tmp=pkg_malloc(len1+len2+1); if (tmp==0) goto error; memcpy(tmp, *dest, len1); memcpy(tmp+len1, src, len2); tmp[len1+len2]=0; pkg_free(*dest); *dest=tmp; } return *dest; error: LOG(L_CRIT, "ERROR:lex:addstr: memory allocation error\n"); return 0; } static void count() { int i; startcolumn=column; for (i=0; i