- warning and fixes (cfg.y $$=0 on error) merged from testing-0.8.12-r1
[sip-router] / cfg.y
1 /*
2  * $Id$
3  *
4  *  cfg grammar
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  /*
30  * History:
31  * ---------
32  * 2003-01-29  src_port added (jiri)
33  * 2003-01-23  mhomed added (jiri)
34  * 2003-03-19  replaced all mallocs/frees with pkg_malloc/pkg_free (andrei)
35  * 2003-03-19  Added support for route type in find_export (janakj)
36  * 2003-03-20  Regex support in modparam (janakj)
37  * 2003-04-01  added dst_port, proto , af (andrei)
38  * 2003-04-05  s/reply_route/failure_route, onreply_route introduced (jiri)
39  * 2003-04-12  added force_rport, chroot and wdir (andrei)
40  * 2003-04-15  added tcp_children, disable_tcp (andrei)
41  * 2003-04-22  strip_tail added (jiri)
42  * 2003-07-03  tls* (disable, certificate, private_key, ca_list, verify, 
43  *              require_certificate added (andrei)
44  * 2003-07-06  more tls config. vars added: tls_method, tls_port_no (andrei)
45  * 2003-10-02  added {,set_}advertised_{address,port} (andrei)
46  * 2003-10-10  added <,>,<=,>=, != operators support
47  *             added msg:len (andrei)
48  * 2003-10-11  if(){} doesn't require a ';' after it anymore (andrei)
49  * 2003-10-13  added FIFO_DIR & proto:host:port listen/alias support (andrei)
50  * 2003-10-24  converted to the new socket_info lists (andrei)
51  * 2003-10-28  added tcp_accept_aliases (andrei)
52  */
53
54
55 %{
56
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <string.h>
64 #include <errno.h>
65 #include "route_struct.h"
66 #include "globals.h"
67 #include "route.h"
68 #include "dprint.h"
69 #include "sr_module.h"
70 #include "modparam.h"
71 #include "ip_addr.h"
72 #include "socket_info.h"
73 #include "name_alias.h"
74 #include "ut.h"
75
76
77 #include "config.h"
78 #ifdef USE_TLS
79 #include "tls/tls_config.h"
80 #endif
81
82 #ifdef DEBUG_DMALLOC
83 #include <dmalloc.h>
84 #endif
85
86 /* hack to avoid alloca usage in the generated C file (needed for compiler
87  with no built in alloca, like icc*/
88 #undef _ALLOCA_H
89
90 struct id_list{
91         char* name;
92         int proto;
93         int port;
94         struct id_list* next;
95 };
96
97 extern int yylex();
98 void yyerror(char* s);
99 static char* tmp;
100 static int i_tmp;
101 static void* f_tmp;
102 static struct id_list* lst_tmp;
103 static int rt;  /* Type of route block for find_export */
104 static str* str_tmp;
105
106 void warn(char* s);
107 static struct id_list* mk_listen_id(char*, int, int);
108  
109
110 %}
111
112 %union {
113         long intval;
114         unsigned long uval;
115         char* strval;
116         struct expr* expr;
117         struct action* action;
118         struct net* ipnet;
119         struct ip_addr* ipaddr;
120         struct id_list* idlst;
121 }
122
123 /* terminals */
124
125
126 /* keywords */
127 %token FORWARD
128 %token FORWARD_TCP
129 %token FORWARD_TLS
130 %token FORWARD_UDP
131 %token SEND
132 %token SEND_TCP
133 %token DROP
134 %token LOG_TOK
135 %token ERROR
136 %token ROUTE
137 %token ROUTE_FAILURE
138 %token ROUTE_ONREPLY
139 %token EXEC
140 %token SET_HOST
141 %token SET_HOSTPORT
142 %token PREFIX
143 %token STRIP
144 %token STRIP_TAIL
145 %token APPEND_BRANCH
146 %token SET_USER
147 %token SET_USERPASS
148 %token SET_PORT
149 %token SET_URI
150 %token REVERT_URI
151 %token FORCE_RPORT
152 %token FORCE_TCP_ALIAS
153 %token IF
154 %token ELSE
155 %token SET_ADV_ADDRESS
156 %token SET_ADV_PORT
157 %token URIHOST
158 %token URIPORT
159 %token MAX_LEN
160 %token SETFLAG
161 %token RESETFLAG
162 %token ISFLAGSET
163 %token METHOD
164 %token URI
165 %token SRCIP
166 %token SRCPORT
167 %token DSTIP
168 %token DSTPORT
169 %token PROTO
170 %token AF
171 %token MYSELF
172 %token MSGLEN 
173 %token UDP
174 %token TCP
175 %token TLS
176
177 /* config vars. */
178 %token DEBUG
179 %token FORK
180 %token LOGSTDERROR
181 %token LISTEN
182 %token ALIAS
183 %token DNS
184 %token REV_DNS
185 %token PORT
186 %token STAT
187 %token CHILDREN
188 %token CHECK_VIA
189 %token SYN_BRANCH
190 %token MEMLOG
191 %token SIP_WARNING
192 %token FIFO
193 %token FIFO_DIR
194 %token FIFO_MODE
195 %token FIFO_DB_URL
196 %token SERVER_SIGNATURE
197 %token REPLY_TO_VIA
198 %token LOADMODULE
199 %token MODPARAM
200 %token MAXBUFFER
201 %token USER
202 %token GROUP
203 %token CHROOT
204 %token WDIR
205 %token MHOMED
206 %token DISABLE_TCP
207 %token TCP_ACCEPT_ALIASES
208 %token TCP_CHILDREN
209 %token DISABLE_TLS
210 %token TLSLOG
211 %token TLS_PORT_NO
212 %token TLS_METHOD
213 %token SSLv23
214 %token SSLv2
215 %token SSLv3
216 %token TLSv1
217 %token TLS_VERIFY
218 %token TLS_REQUIRE_CERTIFICATE
219 %token TLS_CERTIFICATE
220 %token TLS_PRIVATE_KEY
221 %token TLS_CA_LIST
222 %token ADVERTISED_ADDRESS
223 %token ADVERTISED_PORT
224
225
226
227
228
229 /* operators */
230 %nonassoc EQUAL
231 %nonassoc EQUAL_T
232 %nonassoc GT
233 %nonassoc LT
234 %nonassoc GTE
235 %nonassoc LTE
236 %nonassoc DIFF
237 %nonassoc MATCH
238 %left OR
239 %left AND
240 %left NOT
241
242 /* values */
243 %token <intval> NUMBER
244 %token <strval> ID
245 %token <strval> STRING
246 %token <strval> IPV6ADDR
247
248 /* other */
249 %token COMMA
250 %token SEMICOLON
251 %token RPAREN
252 %token LPAREN
253 %token LBRACE
254 %token RBRACE
255 %token LBRACK
256 %token RBRACK
257 %token SLASH
258 %token DOT
259 %token CR
260 %token COLON
261 %token STAR
262
263
264 /*non-terminals */
265 %type <expr> exp exp_elem /*, condition*/
266 %type <action> action actions cmd if_cmd stm
267 %type <ipaddr> ipv4 ipv6 ipv6addr ip
268 %type <ipnet> ipnet
269 %type <strval> host
270 %type <strval> listen_id
271 %type <idlst>  id_lst
272 %type <idlst>  phostport
273 %type <intval> proto port
274 %type <intval> equalop strop intop
275 /*%type <route_el> rules;
276   %type <route_el> rule;
277 */
278
279
280
281 %%
282
283
284 cfg:    statements
285         ;
286
287 statements:     statements statement {}
288                 | statement {}
289                 | statements error { yyerror(""); YYABORT;}
290         ;
291
292 statement:      assign_stm 
293                 | module_stm
294                 | {rt=REQUEST_ROUTE;} route_stm 
295                 | {rt=FAILURE_ROUTE;} failure_route_stm
296                 | {rt=ONREPLY_ROUTE;} onreply_route_stm
297
298                 | CR    /* null statement*/
299         ;
300
301 listen_id:      ip                      {       tmp=ip_addr2a($1);
302                                                         if(tmp==0){
303                                                                 LOG(L_CRIT, "ERROR: cfg. parser: bad ip "
304                                                                                 "addresss.\n");
305                                                                 $$=0;
306                                                         }else{
307                                                                 $$=pkg_malloc(strlen(tmp)+1);
308                                                                 if ($$==0){
309                                                                         LOG(L_CRIT, "ERROR: cfg. parser: out of "
310                                                                                         "memory.\n");
311                                                                 }else{
312                                                                         strncpy($$, tmp, strlen(tmp)+1);
313                                                                 }
314                                                         }
315                                                 }
316                  |      STRING                  {       $$=pkg_malloc(strlen($1)+1);
317                                                         if ($$==0){
318                                                                         LOG(L_CRIT, "ERROR: cfg. parser: out of "
319                                                                                         "memory.\n");
320                                                         }else{
321                                                                         strncpy($$, $1, strlen($1)+1);
322                                                         }
323                                                 }
324                  |      host            {       $$=pkg_malloc(strlen($1)+1);
325                                                         if ($$==0){
326                                                                         LOG(L_CRIT, "ERROR: cfg. parser: out of "
327                                                                                         "memory.\n");
328                                                         }else{
329                                                                         strncpy($$, $1, strlen($1)+1);
330                                                         }
331                                                 }
332         ;
333
334 proto:    UDP   { $$=PROTO_UDP; }
335                 | TCP   { $$=PROTO_TCP; }
336                 | TLS   { $$=PROTO_TLS; }
337                 | STAR  { $$=0; }
338                 ;
339
340 port:     NUMBER        { $$=$1; }
341                 | STAR          { $$=0; }
342 ;
343
344 phostport:      listen_id                               { $$=mk_listen_id($1, 0, 0); }
345                         | listen_id COLON port  { $$=mk_listen_id($1, 0, $3); }
346                         | proto COLON listen_id { $$=mk_listen_id($3, $1, 0); }
347                         | proto COLON listen_id COLON port      { $$=mk_listen_id($3, $1, $5);}
348                         | listen_id COLON error { $$=0; yyerror(" port number expected"); }
349                         ;
350
351 id_lst:         phostport               {  $$=$1 ; }
352                 | phostport id_lst      { $$=$1; $$->next=$2; }
353                 ;
354
355
356 assign_stm:     DEBUG EQUAL NUMBER { debug=$3; }
357                 | DEBUG EQUAL error  { yyerror("number  expected"); }
358                 | FORK  EQUAL NUMBER { dont_fork= ! $3; }
359                 | FORK  EQUAL error  { yyerror("boolean value expected"); }
360                 | LOGSTDERROR EQUAL NUMBER { if (!config_check) log_stderr=$3; }
361                 | LOGSTDERROR EQUAL error { yyerror("boolean value expected"); }
362                 | DNS EQUAL NUMBER   { received_dns|= ($3)?DO_DNS:0; }
363                 | DNS EQUAL error { yyerror("boolean value expected"); }
364                 | REV_DNS EQUAL NUMBER { received_dns|= ($3)?DO_REV_DNS:0; }
365                 | REV_DNS EQUAL error { yyerror("boolean value expected"); }
366                 | PORT EQUAL NUMBER   { port_no=$3; }
367                 | STAT EQUAL STRING {
368                                         #ifdef STATS
369                                                         stat_file=$3;
370                                         #endif
371                                                         }
372                 | MAXBUFFER EQUAL NUMBER { maxbuffer=$3; }
373                 | MAXBUFFER EQUAL error { yyerror("number expected"); }
374                 | PORT EQUAL error    { yyerror("number expected"); } 
375                 | CHILDREN EQUAL NUMBER { children_no=$3; }
376                 | CHILDREN EQUAL error { yyerror("number expected"); } 
377                 | CHECK_VIA EQUAL NUMBER { check_via=$3; }
378                 | CHECK_VIA EQUAL error { yyerror("boolean value expected"); }
379                 | SYN_BRANCH EQUAL NUMBER { syn_branch=$3; }
380                 | SYN_BRANCH EQUAL error { yyerror("boolean value expected"); }
381                 | MEMLOG EQUAL NUMBER { memlog=$3; }
382                 | MEMLOG EQUAL error { yyerror("int value expected"); }
383                 | SIP_WARNING EQUAL NUMBER { sip_warning=$3; }
384                 | SIP_WARNING EQUAL error { yyerror("boolean value expected"); }
385                 | FIFO EQUAL STRING { fifo=$3; }
386                 | FIFO EQUAL error { yyerror("string value expected"); }
387                 | FIFO_DIR EQUAL STRING { fifo_dir=$3; }
388                 | FIFO_DIR EQUAL error { yyerror("string value expected"); }
389                 | FIFO_MODE EQUAL NUMBER { fifo_mode=$3; }
390                 | FIFO_MODE EQUAL error { yyerror("int value expected"); }
391                 | FIFO_DB_URL EQUAL STRING { fifo_db_url=$3; }
392                 | FIFO_DB_URL EQUAL error { yyerror("string value expected"); }
393                 | USER EQUAL STRING     { user=$3; }
394                 | USER EQUAL ID         { user=$3; }
395                 | USER EQUAL error      { yyerror("string value expected"); }
396                 | GROUP EQUAL STRING     { group=$3; }
397                 | GROUP EQUAL ID         { group=$3; }
398                 | GROUP EQUAL error      { yyerror("string value expected"); }
399                 | CHROOT EQUAL STRING     { chroot_dir=$3; }
400                 | CHROOT EQUAL ID         { chroot_dir=$3; }
401                 | CHROOT EQUAL error      { yyerror("string value expected"); }
402                 | WDIR EQUAL STRING     { working_dir=$3; }
403                 | WDIR EQUAL ID         { working_dir=$3; }
404                 | WDIR EQUAL error      { yyerror("string value expected"); }
405                 | MHOMED EQUAL NUMBER { mhomed=$3; }
406                 | MHOMED EQUAL error { yyerror("boolean value expected"); }
407                 | DISABLE_TCP EQUAL NUMBER {
408                                                                         #ifdef USE_TCP
409                                                                                 tcp_disable=$3;
410                                                                         #else
411                                                                                 warn("tcp support not compiled in");
412                                                                         #endif
413                                                                         }
414                 | DISABLE_TCP EQUAL error { yyerror("boolean value expected"); }
415                 | TCP_ACCEPT_ALIASES EQUAL NUMBER {
416                                                                         #ifdef USE_TCP
417                                                                                 tcp_accept_aliases=$3;
418                                                                         #else
419                                                                                 warn("tcp support not compiled in");
420                                                                         #endif
421                                                                         }
422                 | TCP_ACCEPT_ALIASES EQUAL error { yyerror("boolean value expected"); }
423                 | TCP_CHILDREN EQUAL NUMBER {
424                                                                         #ifdef USE_TCP
425                                                                                 tcp_children_no=$3;
426                                                                         #else
427                                                                                 warn("tcp support not compiled in");
428                                                                         #endif
429                                                                         }
430                 | TCP_CHILDREN EQUAL error { yyerror("number expected"); }
431                 | DISABLE_TLS EQUAL NUMBER {
432                                                                         #ifdef USE_TLS
433                                                                                 tls_disable=$3;
434                                                                         #else
435                                                                                 warn("tls support not compiled in");
436                                                                         #endif
437                                                                         }
438                 | DISABLE_TLS EQUAL error { yyerror("boolean value expected"); }
439                 | TLSLOG EQUAL NUMBER           { 
440                                                                         #ifdef USE_TLS
441                                                                                 tls_log=$3;
442                                                                         #else
443                                                                                 warn("tls support not compiled in");
444                                                                         #endif
445                                                                         }
446                 | TLSLOG EQUAL error { yyerror("int value expected"); }
447                 | TLS_PORT_NO EQUAL NUMBER {
448                                                                         #ifdef USE_TLS
449                                                                                 tls_port_no=$3;
450                                                                         #else
451                                                                                 warn("tls support not compiled in");
452                                                                         #endif
453                                                                         }
454                 | TLS_PORT_NO EQUAL error { yyerror("number expected"); }
455                 | TLS_METHOD EQUAL SSLv23 {
456                                                                         #ifdef USE_TLS
457                                                                                 tls_method=TLS_USE_SSLv23;
458                                                                         #else
459                                                                                 warn("tls support not compiled in");
460                                                                         #endif
461                                                                         }
462                 | TLS_METHOD EQUAL SSLv2 {
463                                                                         #ifdef USE_TLS
464                                                                                 tls_method=TLS_USE_SSLv2;
465                                                                         #else
466                                                                                 warn("tls support not compiled in");
467                                                                         #endif
468                                                                         }
469                 | TLS_METHOD EQUAL SSLv3 {
470                                                                         #ifdef USE_TLS
471                                                                                 tls_method=TLS_USE_SSLv3;
472                                                                         #else
473                                                                                 warn("tls support not compiled in");
474                                                                         #endif
475                                                                         }
476                 | TLS_METHOD EQUAL TLSv1 {
477                                                                         #ifdef USE_TLS
478                                                                                 tls_method=TLS_USE_TLSv1;
479                                                                         #else
480                                                                                 warn("tls support not compiled in");
481                                                                         #endif
482                                                                         }
483                 | TLS_METHOD EQUAL error {
484                                                                         #ifdef USE_TLS
485                                                                                 yyerror("SSLv23, SSLv2, SSLv3 or TLSv1"
486                                                                                                         " expected");
487                                                                         #else
488                                                                                 warn("tls support not compiled in");
489                                                                         #endif
490                                                                         }
491                                                                                 
492                 | TLS_VERIFY EQUAL NUMBER {
493                                                                         #ifdef USE_TLS
494                                                                                 tls_verify_cert=$3;
495                                                                         #else
496                                                                                 warn("tls support not compiled in");
497                                                                         #endif
498                                                                         }
499                 | TLS_VERIFY EQUAL error { yyerror("boolean value expected"); }
500                 | TLS_REQUIRE_CERTIFICATE EQUAL NUMBER {
501                                                                         #ifdef USE_TLS
502                                                                                 tls_require_cert=$3;
503                                                                         #else
504                                                                                 warn( "tls support not compiled in");
505                                                                         #endif
506                                                                         }
507                 | TLS_REQUIRE_CERTIFICATE EQUAL error { yyerror("boolean value"
508                                                                                                                                 " expected"); }
509                 | TLS_CERTIFICATE EQUAL STRING { 
510                                                                         #ifdef USE_TLS
511                                                                                         tls_cert_file=$3;
512                                                                         #else
513                                                                                 warn("tls support not compiled in");
514                                                                         #endif
515                                                                         }
516                 | TLS_CERTIFICATE EQUAL error { yyerror("string value expected"); }
517                 | TLS_PRIVATE_KEY EQUAL STRING { 
518                                                                         #ifdef USE_TLS
519                                                                                         tls_pkey_file=$3;
520                                                                         #else
521                                                                                 warn("tls support not compiled in");
522                                                                         #endif
523                                                                         }
524                 | TLS_PRIVATE_KEY EQUAL error { yyerror("string value expected"); }
525                 | TLS_CA_LIST EQUAL STRING { 
526                                                                         #ifdef USE_TLS
527                                                                                         tls_ca_file=$3;
528                                                                         #else
529                                                                                 warn("tls support not compiled in");
530                                                                         #endif
531                                                                         }
532                 | TLS_CA_LIST EQUAL error { yyerror("string value expected"); }
533                 | SERVER_SIGNATURE EQUAL NUMBER { server_signature=$3; }
534                 | SERVER_SIGNATURE EQUAL error { yyerror("boolean value expected"); }
535                 | REPLY_TO_VIA EQUAL NUMBER { reply_to_via=$3; }
536                 | REPLY_TO_VIA EQUAL error { yyerror("boolean value expected"); }
537                 | LISTEN EQUAL id_lst {
538                                                         for(lst_tmp=$3; lst_tmp; lst_tmp=lst_tmp->next){
539                                                                 if (add_listen_iface(   lst_tmp->name,
540                                                                                                                 lst_tmp->port,
541                                                                                                                 lst_tmp->proto,
542                                                                                                                 0
543                                                                                                         )!=0){
544                                                                         LOG(L_CRIT,  "ERROR: cfg. parser: failed"
545                                                                                         " to add listen address\n");
546                                                                         break;
547                                                                 }
548                                                         }
549                                                          }
550                 | LISTEN EQUAL  error { yyerror("ip address or hostname"
551                                                 "expected"); }
552                 | ALIAS EQUAL  id_lst { 
553                                                         for(lst_tmp=$3; lst_tmp; lst_tmp=lst_tmp->next)
554                                                                 add_alias(lst_tmp->name, strlen(lst_tmp->name),
555                                                                                         lst_tmp->port, lst_tmp->proto);
556                                                           }
557                 | ALIAS  EQUAL error  { yyerror(" hostname expected"); }
558                 | ADVERTISED_ADDRESS EQUAL listen_id {
559                                                                 default_global_address.s=$3;
560                                                                 default_global_address.len=strlen($3);
561                                                                 }
562                 |ADVERTISED_ADDRESS EQUAL error {yyerror("ip address or hostname "
563                                                                                                 "expected"); }
564                 | ADVERTISED_PORT EQUAL NUMBER {
565                                                                 tmp=int2str($3, &i_tmp);
566                                                                 if ((default_global_port.s=pkg_malloc(i_tmp))
567                                                                                 ==0){
568                                                                                 LOG(L_CRIT, "ERROR: cfg. parser:"
569                                                                                                         " out of memory.\n");
570                                                                                 default_global_port.len=0;
571                                                                 }else{
572                                                                         default_global_port.len=i_tmp;
573                                                                         memcpy(default_global_port.s, tmp,
574                                                                                         default_global_port.len);
575                                                                 };
576                                                                 }
577                 |ADVERTISED_PORT EQUAL error {yyerror("ip address or hostname "
578                                                                                                 "expected"); }
579                 | error EQUAL { yyerror("unknown config variable"); }
580         ;
581
582 module_stm:     LOADMODULE STRING       { DBG("loading module %s\n", $2);
583                                                                   if (load_module($2)!=0){
584                                                                                 yyerror("failed to load module");
585                                                                   }
586                                                                 }
587                  | LOADMODULE error     { yyerror("string expected");  }
588                  | MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
589                          if (set_mod_param_regex($3, $5, STR_PARAM, $7) != 0) {
590                                  yyerror("Can't set module parameter");
591                          }
592                    }
593                  | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
594                          if (set_mod_param_regex($3, $5, INT_PARAM, (void*)$7) != 0) {
595                                  yyerror("Can't set module parameter");
596                          }
597                    }
598                  | MODPARAM error { yyerror("Invalid arguments"); }
599                  ;
600
601
602 ip:              ipv4  { $$=$1; }
603                 |ipv6  { $$=$1; }
604                 ;
605
606 ipv4:   NUMBER DOT NUMBER DOT NUMBER DOT NUMBER { 
607                                                                                         $$=pkg_malloc(
608                                                                                                         sizeof(struct ip_addr));
609                                                                                         if ($$==0){
610                                                                                                 LOG(L_CRIT, "ERROR: cfg. "
611                                                                                                         "parser: out of memory.\n"
612                                                                                                         );
613                                                                                         }else{
614                                                                                                 memset($$, 0, 
615                                                                                                         sizeof(struct ip_addr));
616                                                                                                 $$->af=AF_INET;
617                                                                                                 $$->len=4;
618                                                                                                 if (($1>255) || ($1<0) ||
619                                                                                                         ($3>255) || ($3<0) ||
620                                                                                                         ($5>255) || ($5<0) ||
621                                                                                                         ($7>255) || ($7<0)){
622                                                                                                         yyerror("invalid ipv4"
623                                                                                                                         "address");
624                                                                                                         $$->u.addr32[0]=0;
625                                                                                                         /* $$=0; */
626                                                                                                 }else{
627                                                                                                         $$->u.addr[0]=$1;
628                                                                                                         $$->u.addr[1]=$3;
629                                                                                                         $$->u.addr[2]=$5;
630                                                                                                         $$->u.addr[3]=$7;
631                                                                                                         /*
632                                                                                                         $$=htonl( ($1<<24)|
633                                                                                                         ($3<<16)| ($5<<8)|$7 );
634                                                                                                         */
635                                                                                                 }
636                                                                                         }
637                                                                                                 }
638         ;
639
640 ipv6addr:       IPV6ADDR {
641                                         $$=pkg_malloc(sizeof(struct ip_addr));
642                                         if ($$==0){
643                                                 LOG(L_CRIT, "ERROR: cfg. parser: out of memory.\n");
644                                         }else{
645                                                 memset($$, 0, sizeof(struct ip_addr));
646                                                 $$->af=AF_INET6;
647                                                 $$->len=16;
648                                         #ifdef USE_IPV6
649                                                 if (inet_pton(AF_INET6, $1, $$->u.addr)<=0){
650                                                         yyerror("bad ipv6 address");
651                                                 }
652                                         #else
653                                                 yyerror("ipv6 address & no ipv6 support compiled in");
654                                                 YYABORT;
655                                         #endif
656                                         }
657                                 }
658         ;
659
660 ipv6:   ipv6addr { $$=$1; }
661         | LBRACK ipv6addr RBRACK {$$=$2; }
662 ;
663
664
665 route_stm:  ROUTE LBRACE actions RBRACE { push($3, &rlist[DEFAULT_RT]); }
666
667             | ROUTE LBRACK NUMBER RBRACK LBRACE actions RBRACE { 
668                                                                                 if (($3<RT_NO) && ($3>=0)){
669                                                                                         push($6, &rlist[$3]);
670                                                                                 }else{
671                                                                                         yyerror("invalid routing"
672                                                                                                         "table number");
673                                                                                         YYABORT; }
674                                                                                 }
675                 | ROUTE error { yyerror("invalid  route  statement"); }
676         ;
677
678 failure_route_stm: ROUTE_FAILURE LBRACK NUMBER RBRACK LBRACE actions RBRACE {
679                                                                                 if (($3<FAILURE_RT_NO)&&($3>=1)){
680                                                                                         push($6, &failure_rlist[$3]);
681                                                                                 } else {
682                                                                                         yyerror("invalid reply routing"
683                                                                                                 "table number");
684                                                                                         YYABORT; }
685                                                                                 }
686                 | ROUTE_FAILURE error { yyerror("invalid failure_route statement"); }
687         ;
688
689 onreply_route_stm: ROUTE_ONREPLY LBRACK NUMBER RBRACK LBRACE actions RBRACE {
690                                                                                 if (($3<ONREPLY_RT_NO)&&($3>=1)){
691                                                                                         push($6, &onreply_rlist[$3]);
692                                                                                 } else {
693                                                                                         yyerror("invalid reply routing"
694                                                                                                 "table number");
695                                                                                         YYABORT; }
696                                                                                 }
697                 | ROUTE_ONREPLY error { yyerror("invalid failure_route statement"); }
698         ;
699 /*
700 rules:  rules rule { push($2, &$1); $$=$1; }
701         | rule {$$=$1; }
702         | rules error { $$=0; yyerror("invalid rule"); }
703          ;
704
705 rule:   condition       actions CR {
706                                                                 $$=0;
707                                                                 if (add_rule($1, $2, &$$)<0) {
708                                                                         yyerror("error calling add_rule");
709                                                                         YYABORT;
710                                                                 }
711                                                           }
712         | CR            { $$=0;}
713         | condition error { $$=0; yyerror("bad actions in rule"); }
714         ;
715
716 condition:      exp {$$=$1;}
717 */
718
719 exp:    exp AND exp     { $$=mk_exp(AND_OP, $1, $3); }
720         | exp OR  exp           { $$=mk_exp(OR_OP, $1, $3);  }
721         | NOT exp                       { $$=mk_exp(NOT_OP, $2, 0);  }
722         | LPAREN exp RPAREN     { $$=$2; }
723         | exp_elem                      { $$=$1; }
724         ;
725
726 equalop:          EQUAL_T {$$=EQUAL_OP; }
727                         | DIFF  {$$=DIFF_OP; }
728                 ;
729                 
730 intop:  equalop {$$=$1; }
731                 |  GT   {$$=GT_OP; }
732                 | LT    {$$=LT_OP; }
733                 | GTE   {$$=GTE_OP; }
734                 | LTE   {$$=LTE_OP; }
735                 ;
736                 
737 strop:  equalop {$$=$1; }
738                 | MATCH {$$=MATCH_OP; }
739                 ;
740
741 exp_elem:       METHOD strop STRING     {$$= mk_elem(   $2, STRING_ST, 
742                                                                                                         METHOD_O, $3);
743                                                                         }
744                 | METHOD strop  ID      {$$ = mk_elem(  $2, STRING_ST,
745                                                                                         METHOD_O, $3); 
746                                                         }
747                 | METHOD strop error { $$=0; yyerror("string expected"); }
748                 | METHOD error  { $$=0; yyerror("invalid operator,"
749                                                                                 "== , !=, or =~ expected");
750                                                 }
751                 | URI strop STRING      {$$ = mk_elem(  $2, STRING_ST,
752                                                                                                 URI_O, $3); 
753                                                                 }
754                 | URI strop host        {$$ = mk_elem(  $2, STRING_ST,
755                                                                                         URI_O, $3); 
756                                                         }
757                 | URI equalop MYSELF    { $$=mk_elem(   $2, MYSELF_ST,
758                                                                                                 URI_O, 0);
759                                                                 }
760                 | URI strop error { $$=0; yyerror("string or MYSELF expected"); }
761                 | URI error     { $$=0; yyerror("invalid operator,"
762                                                                         " == , != or =~ expected");
763                                         }
764                 | SRCPORT intop NUMBER  { $$=mk_elem(   $2, NUMBER_ST,
765                                                                                                 SRCPORT_O, (void *) $3 ); }
766                 | SRCPORT intop error { $$=0; yyerror("number expected"); }
767                 | SRCPORT error { $$=0; yyerror("==, !=, <,>, >= or <=  expected"); }
768                 | DSTPORT intop NUMBER  { $$=mk_elem(   $2, NUMBER_ST,
769                                                                                                 DSTPORT_O, (void *) $3 ); }
770                 | DSTPORT intop error { $$=0; yyerror("number expected"); }
771                 | DSTPORT error { $$=0; yyerror("==, !=, <,>, >= or <=  expected"); }
772                 | PROTO intop proto     { $$=mk_elem(   $2, NUMBER_ST,
773                                                                                                 PROTO_O, (void *) $3 ); }
774                 | PROTO intop error { $$=0;
775                                                                 yyerror("protocol expected (udp, tcp or tls)");
776                                                         }
777                 | PROTO error { $$=0; yyerror("equal/!= operator expected"); }
778                 | AF intop NUMBER       { $$=mk_elem(   $2, NUMBER_ST,
779                                                                                                 AF_O, (void *) $3 ); }
780                 | AF intop error { $$=0; yyerror("number expected"); }
781                 | AF error { $$=0; yyerror("equal/!= operator expected"); }
782                 | MSGLEN intop NUMBER   { $$=mk_elem(   $2, NUMBER_ST,
783                                                                                                 MSGLEN_O, (void *) $3 ); }
784                 | MSGLEN intop MAX_LEN  { $$=mk_elem(   $2, NUMBER_ST,
785                                                                                                 MSGLEN_O, (void *) BUF_SIZE); }
786                 | MSGLEN intop error { $$=0; yyerror("number expected"); }
787                 | MSGLEN error { $$=0; yyerror("equal/!= operator expected"); }
788                 | SRCIP equalop ipnet   { $$=mk_elem(   $2, NET_ST,
789                                                                                                 SRCIP_O, $3);
790                                                                 }
791                 | SRCIP strop STRING    { $$=mk_elem(   $2, STRING_ST,
792                                                                                                 SRCIP_O, $3);
793                                                                 }
794                 | SRCIP strop host      { $$=mk_elem(   $2, STRING_ST,
795                                                                                                 SRCIP_O, $3);
796                                                                 }
797                 | SRCIP equalop MYSELF  { $$=mk_elem(   $2, MYSELF_ST,
798                                                                                                 SRCIP_O, 0);
799                                                                 }
800                 | SRCIP strop error { $$=0; yyerror( "ip address or hostname"
801                                                  "expected" ); }
802                 | SRCIP error  { $$=0; 
803                                                  yyerror("invalid operator, ==, != or =~ expected");}
804                 | DSTIP equalop ipnet   { $$=mk_elem(   $2, NET_ST,
805                                                                                                 DSTIP_O, $3);
806                                                                 }
807                 | DSTIP strop STRING    { $$=mk_elem(   $2, STRING_ST,
808                                                                                                 DSTIP_O, $3);
809                                                                 }
810                 | DSTIP strop host      { $$=mk_elem(   $2, STRING_ST,
811                                                                                                 DSTIP_O, $3);
812                                                                 }
813                 | DSTIP equalop MYSELF  { $$=mk_elem(   $2, MYSELF_ST,
814                                                                                                 DSTIP_O, 0);
815                                                                 }
816                 | DSTIP strop error { $$=0; yyerror( "ip address or hostname"
817                                                                         "expected" ); }
818                 | DSTIP error { $$=0; 
819                                                 yyerror("invalid operator, ==, != or =~ expected");}
820                 | MYSELF equalop URI    { $$=mk_elem(   $2, MYSELF_ST,
821                                                                                                 URI_O, 0);
822                                                                 }
823                 | MYSELF equalop SRCIP  { $$=mk_elem(   $2, MYSELF_ST,
824                                                                                                 SRCIP_O, 0);
825                                                                 }
826                 | MYSELF equalop DSTIP  { $$=mk_elem(   $2, MYSELF_ST,
827                                                                                                 DSTIP_O, 0);
828                                                                 }
829                 | MYSELF equalop error {        $$=0; 
830                                                                         yyerror(" URI, SRCIP or DSTIP expected"); }
831                 | MYSELF error  { $$=0; 
832                                                         yyerror ("invalid operator, == or != expected");
833                                                 }
834                 | stm                           { $$=mk_elem( NO_OP, ACTIONS_ST, ACTION_O, $1 ); }
835                 | NUMBER                {$$=mk_elem( NO_OP, NUMBER_ST, NUMBER_O, (void*)$1 ); }
836         ;
837
838 ipnet:  ip SLASH ip     { $$=mk_net($1, $3); } 
839         | ip SLASH NUMBER       {       if (($3<0) || ($3>$1->len*8)){
840                                                                 yyerror("invalid bit number in netmask");
841                                                                 $$=0;
842                                                         }else{
843                                                                 $$=mk_net_bitlen($1, $3);
844                                                         /*
845                                                                 $$=mk_net($1, 
846                                                                                 htonl( ($3)?~( (1<<(32-$3))-1 ):0 ) );
847                                                         */
848                                                         }
849                                                 }
850         | ip                            { $$=mk_net_bitlen($1, $1->len*8); }
851         | ip SLASH error        { $$=0;
852                                                  yyerror("netmask (eg:255.0.0.0 or 8) expected");
853                                                 }
854         ;
855
856 host:   ID                              { $$=$1; }
857         | host DOT ID           { $$=(char*)pkg_malloc(strlen($1)+1+strlen($3)+1);
858                                                   if ($$==0){
859                                                         LOG(L_CRIT, "ERROR: cfg. parser: memory allocation"
860                                                                                 " failure while parsing host\n");
861                                                   }else{
862                                                         memcpy($$, $1, strlen($1));
863                                                         $$[strlen($1)]='.';
864                                                         memcpy($$+strlen($1)+1, $3, strlen($3));
865                                                         $$[strlen($1)+1+strlen($3)]=0;
866                                                   }
867                                                   pkg_free($1); pkg_free($3);
868                                                 }
869         | host DOT error { $$=0; pkg_free($1); yyerror("invalid hostname"); }
870         ;
871
872
873 stm:            cmd                                             { $$=$1; }
874                 |       if_cmd                                  { $$=$1; }
875                 |       LBRACE actions RBRACE   { $$=$2; }
876         ;
877
878 actions:        actions action  {$$=append_action($1, $2); }
879                 | action                        {$$=$1;}
880                 | actions error { $$=0; yyerror("bad command"); }
881         ;
882
883 action:         cmd SEMICOLON {$$=$1;}
884                 | if_cmd {$$=$1;}
885                 | SEMICOLON /* null action */ {$$=0;}
886                 | cmd error { $$=0; yyerror("bad command: missing ';'?"); }
887         ;
888
889 if_cmd:         IF exp stm                              { $$=mk_action3( IF_T,
890                                                                                                          EXPR_ST,
891                                                                                                          ACTIONS_ST,
892                                                                                                          NOSUBTYPE,
893                                                                                                          $2,
894                                                                                                          $3,
895                                                                                                          0);
896                                                                         }
897                 |       IF exp stm ELSE stm             { $$=mk_action3( IF_T,
898                                                                                                          EXPR_ST,
899                                                                                                          ACTIONS_ST,
900                                                                                                          ACTIONS_ST,
901                                                                                                          $2,
902                                                                                                          $3,
903                                                                                                          $5);
904                                                                         }
905         ;
906
907 cmd:            FORWARD LPAREN host RPAREN      { $$=mk_action( FORWARD_T,
908                                                                                                                 STRING_ST,
909                                                                                                                 NUMBER_ST,
910                                                                                                                 $3,
911                                                                                                                 0);
912                                                                                 }
913                 | FORWARD LPAREN STRING RPAREN  { $$=mk_action( FORWARD_T,
914                                                                                                                 STRING_ST,
915                                                                                                                 NUMBER_ST,
916                                                                                                                 $3,
917                                                                                                                 0);
918                                                                                 }
919                 | FORWARD LPAREN ip RPAREN      { $$=mk_action( FORWARD_T,
920                                                                                                                 IP_ST,
921                                                                                                                 NUMBER_ST,
922                                                                                                                 (void*)$3,
923                                                                                                                 0);
924                                                                                 }
925                 | FORWARD LPAREN host COMMA NUMBER RPAREN { $$=mk_action(FORWARD_T,
926                                                                                                                                  STRING_ST,
927                                                                                                                                  NUMBER_ST,
928                                                                                                                                 $3,
929                                                                                                                                 (void*)$5);
930                                                                                                  }
931                 | FORWARD LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action(FORWARD_T,
932                                                                                                                                  STRING_ST,
933                                                                                                                                  NUMBER_ST,
934                                                                                                                                 $3,
935                                                                                                                                 (void*)$5);
936                                                                                                         }
937                 | FORWARD LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(FORWARD_T,
938                                                                                                                                  IP_ST,
939                                                                                                                                  NUMBER_ST,
940                                                                                                                                  (void*)$3,
941                                                                                                                                 (void*)$5);
942                                                                                                   }
943                 | FORWARD LPAREN URIHOST COMMA URIPORT RPAREN {
944                                                                                                         $$=mk_action(FORWARD_T,
945                                                                                                                                  URIHOST_ST,
946                                                                                                                                  URIPORT_ST,
947                                                                                                                                 0,
948                                                                                                                                 0);
949                                                                                                         }
950                                                                                                         
951                                                                         
952                 | FORWARD LPAREN URIHOST COMMA NUMBER RPAREN {
953                                                                                                         $$=mk_action(FORWARD_T,
954                                                                                                                                  URIHOST_ST,
955                                                                                                                                  NUMBER_ST,
956                                                                                                                                 0,
957                                                                                                                                 (void*)$5);
958                                                                                                         }
959                 | FORWARD LPAREN URIHOST RPAREN {
960                                                                                                         $$=mk_action(FORWARD_T,
961                                                                                                                                  URIHOST_ST,
962                                                                                                                                  NUMBER_ST,
963                                                                                                                                 0,
964                                                                                                                                 0);
965                                                                                 }
966                 | FORWARD error { $$=0; yyerror("missing '(' or ')' ?"); }
967                 | FORWARD LPAREN error RPAREN { $$=0; yyerror("bad forward"
968                                                                                 "argument"); }
969                 | FORWARD_UDP LPAREN host RPAREN        { $$=mk_action( FORWARD_UDP_T,
970                                                                                                                 STRING_ST,
971                                                                                                                 NUMBER_ST,
972                                                                                                                 $3,
973                                                                                                                 0);
974                                                                                 }
975                 | FORWARD_UDP LPAREN STRING RPAREN      { $$=mk_action( FORWARD_UDP_T,
976                                                                                                                 STRING_ST,
977                                                                                                                 NUMBER_ST,
978                                                                                                                 $3,
979                                                                                                                 0);
980                                                                                 }
981                 | FORWARD_UDP LPAREN ip RPAREN  { $$=mk_action( FORWARD_UDP_T,
982                                                                                                                 IP_ST,
983                                                                                                                 NUMBER_ST,
984                                                                                                                 (void*)$3,
985                                                                                                                 0);
986                                                                                 }
987                 | FORWARD_UDP LPAREN host COMMA NUMBER RPAREN { $$=mk_action(
988                                                                                                                                 FORWARD_UDP_T,
989                                                                                                                                  STRING_ST,
990                                                                                                                                  NUMBER_ST,
991                                                                                                                                 $3,
992                                                                                                                                 (void*)$5);
993                                                                                                  }
994                 | FORWARD_UDP LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action(
995                                                                                                                                 FORWARD_UDP_T,
996                                                                                                                                  STRING_ST,
997                                                                                                                                  NUMBER_ST,
998                                                                                                                                 $3,
999                                                                                                                                 (void*)$5);
1000                                                                                                         }
1001                 | FORWARD_UDP LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(
1002                                                                                                                                 FORWARD_UDP_T,
1003                                                                                                                                  IP_ST,
1004                                                                                                                                  NUMBER_ST,
1005                                                                                                                                  (void*)$3,
1006                                                                                                                                 (void*)$5);
1007                                                                                                   }
1008                 | FORWARD_UDP LPAREN URIHOST COMMA URIPORT RPAREN {
1009                                                                                                         $$=mk_action(FORWARD_UDP_T,
1010                                                                                                                                  URIHOST_ST,
1011                                                                                                                                  URIPORT_ST,
1012                                                                                                                                 0,
1013                                                                                                                                 0);
1014                                                                                                         }
1015                                                                                                         
1016                                                                         
1017                 | FORWARD_UDP LPAREN URIHOST COMMA NUMBER RPAREN {
1018                                                                                                         $$=mk_action(FORWARD_UDP_T,
1019                                                                                                                                  URIHOST_ST,
1020                                                                                                                                  NUMBER_ST,
1021                                                                                                                                 0,
1022                                                                                                                                 (void*)$5);
1023                                                                                                         }
1024                 | FORWARD_UDP LPAREN URIHOST RPAREN {
1025                                                                                                         $$=mk_action(FORWARD_UDP_T,
1026                                                                                                                                  URIHOST_ST,
1027                                                                                                                                  NUMBER_ST,
1028                                                                                                                                 0,
1029                                                                                                                                 0);
1030                                                                                 }
1031                 | FORWARD_UDP error { $$=0; yyerror("missing '(' or ')' ?"); }
1032                 | FORWARD_UDP LPAREN error RPAREN { $$=0; yyerror("bad forward_udp"
1033                                                                                 "argument"); }
1034                 | FORWARD_TCP LPAREN host RPAREN        { $$=mk_action( FORWARD_TCP_T,
1035                                                                                                                 STRING_ST,
1036                                                                                                                 NUMBER_ST,
1037                                                                                                                 $3,
1038                                                                                                                 0);
1039                                                                                 }
1040                 | FORWARD_TCP LPAREN STRING RPAREN      { $$=mk_action( FORWARD_TCP_T,
1041                                                                                                                 STRING_ST,
1042                                                                                                                 NUMBER_ST,
1043                                                                                                                 $3,
1044                                                                                                                 0);
1045                                                                                 }
1046                 | FORWARD_TCP LPAREN ip RPAREN  { $$=mk_action( FORWARD_TCP_T,
1047                                                                                                                 IP_ST,
1048                                                                                                                 NUMBER_ST,
1049                                                                                                                 (void*)$3,
1050                                                                                                                 0);
1051                                                                                 }
1052                 | FORWARD_TCP LPAREN host COMMA NUMBER RPAREN { $$=mk_action(
1053                                                                                                                                 FORWARD_TCP_T,
1054                                                                                                                                  STRING_ST,
1055                                                                                                                                  NUMBER_ST,
1056                                                                                                                                 $3,
1057                                                                                                                                 (void*)$5);
1058                                                                                                  }
1059                 | FORWARD_TCP LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action(
1060                                                                                                                                 FORWARD_TCP_T,
1061                                                                                                                                  STRING_ST,
1062                                                                                                                                  NUMBER_ST,
1063                                                                                                                                 $3,
1064                                                                                                                                 (void*)$5);
1065                                                                                                         }
1066                 | FORWARD_TCP LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(FORWARD_TCP_T,
1067                                                                                                                                  IP_ST,
1068                                                                                                                                  NUMBER_ST,
1069                                                                                                                                  (void*)$3,
1070                                                                                                                                 (void*)$5);
1071                                                                                                   }
1072                 | FORWARD_TCP LPAREN URIHOST COMMA URIPORT RPAREN {
1073                                                                                                         $$=mk_action(FORWARD_TCP_T,
1074                                                                                                                                  URIHOST_ST,
1075                                                                                                                                  URIPORT_ST,
1076                                                                                                                                 0,
1077                                                                                                                                 0);
1078                                                                                                         }
1079                                                                                                         
1080                                                                         
1081                 | FORWARD_TCP LPAREN URIHOST COMMA NUMBER RPAREN {
1082                                                                                                         $$=mk_action(FORWARD_TCP_T,
1083                                                                                                                                  URIHOST_ST,
1084                                                                                                                                  NUMBER_ST,
1085                                                                                                                                 0,
1086                                                                                                                                 (void*)$5);
1087                                                                                                         }
1088                 | FORWARD_TCP LPAREN URIHOST RPAREN {
1089                                                                                                         $$=mk_action(FORWARD_TCP_T,
1090                                                                                                                                  URIHOST_ST,
1091                                                                                                                                  NUMBER_ST,
1092                                                                                                                                 0,
1093                                                                                                                                 0);
1094                                                                                 }
1095                 | FORWARD_TCP error { $$=0; yyerror("missing '(' or ')' ?"); }
1096                 | FORWARD_TCP LPAREN error RPAREN { $$=0; yyerror("bad forward_tcp"
1097                                                                                 "argument"); }
1098                 | FORWARD_TLS LPAREN host RPAREN        {
1099                                                                                 #ifdef USE_TLS
1100                                                                                         $$=mk_action(   FORWARD_TLS_T,
1101                                                                                                                 STRING_ST,
1102                                                                                                                 NUMBER_ST,
1103                                                                                                                 $3,
1104                                                                                                                 0);
1105                                                                                 #else
1106                                                                                         $$=0;
1107                                                                                         yyerror("tls support not "
1108                                                                                                         "compiled in");
1109                                                                                 #endif
1110                                                                                 }
1111                 | FORWARD_TLS LPAREN STRING RPAREN      {
1112                                                                                 #ifdef USE_TLS
1113                                                                                         $$=mk_action(   FORWARD_TLS_T,
1114                                                                                                                         STRING_ST,
1115                                                                                                                         NUMBER_ST,
1116                                                                                                                         $3,
1117                                                                                                                         0);
1118                                                                                 #else
1119                                                                                         $$=0;
1120                                                                                         yyerror("tls support not "
1121                                                                                                         "compiled in");
1122                                                                                 #endif
1123                                                                                 }
1124                 | FORWARD_TLS LPAREN ip RPAREN  { 
1125                                                                                 #ifdef USE_TLS
1126                                                                                         $$=mk_action(   FORWARD_TLS_T,
1127                                                                                                                         IP_ST,
1128                                                                                                                         NUMBER_ST,
1129                                                                                                                         (void*)$3,
1130                                                                                                                         0);
1131                                                                                 #else
1132                                                                                         $$=0;
1133                                                                                         yyerror("tls support not "
1134                                                                                                         "compiled in");
1135                                                                                 #endif
1136                                                                                 }
1137                 | FORWARD_TLS LPAREN host COMMA NUMBER RPAREN { 
1138                                                                                 #ifdef USE_TLS
1139                                                                                         $$=mk_action(   FORWARD_TLS_T,
1140                                                                                                                          STRING_ST,
1141                                                                                                                          NUMBER_ST,
1142                                                                                                                         $3,
1143                                                                                                                         (void*)$5);
1144                                                                                 #else
1145                                                                                         $$=0;
1146                                                                                         yyerror("tls support not "
1147                                                                                                         "compiled in");
1148                                                                                 #endif
1149                                                                                                  }
1150                 | FORWARD_TLS LPAREN STRING COMMA NUMBER RPAREN {
1151                                                                                 #ifdef USE_TLS
1152                                                                                         $$=mk_action(   FORWARD_TLS_T,
1153                                                                                                                          STRING_ST,
1154                                                                                                                          NUMBER_ST,
1155                                                                                                                         $3,
1156                                                                                                                         (void*)$5);
1157                                                                                 #else
1158                                                                                         $$=0;
1159                                                                                         yyerror("tls support not "
1160                                                                                                         "compiled in");
1161                                                                                 #endif
1162                                                                                                         }
1163                 | FORWARD_TLS LPAREN ip COMMA NUMBER RPAREN {
1164                                                                                 #ifdef USE_TLS
1165                                                                                         $$=mk_action(   FORWARD_TLS_T,
1166                                                                                                                          IP_ST,
1167                                                                                                                          NUMBER_ST,
1168                                                                                                                          (void*)$3,
1169                                                                                                                         (void*)$5);
1170                                                                                 #else
1171                                                                                         $$=0;
1172                                                                                         yyerror("tls support not "
1173                                                                                                         "compiled in");
1174                                                                                 #endif
1175                                                                                                   }
1176                 | FORWARD_TLS LPAREN URIHOST COMMA URIPORT RPAREN {
1177                                                                                 #ifdef USE_TLS
1178                                                                                         $$=mk_action(   FORWARD_TLS_T,
1179                                                                                                                          URIHOST_ST,
1180                                                                                                                          URIPORT_ST,
1181                                                                                                                         0,
1182                                                                                                                         0);
1183                                                                                 #else
1184                                                                                         $$=0;
1185                                                                                         yyerror("tls support not "
1186                                                                                                         "compiled in");
1187                                                                                 #endif
1188                                                                                                         }
1189                                                                                                         
1190                                                                         
1191                 | FORWARD_TLS LPAREN URIHOST COMMA NUMBER RPAREN {
1192                                                                                 #ifdef USE_TLS
1193                                                                                         $$=mk_action(   FORWARD_TLS_T,
1194                                                                                                                          URIHOST_ST,
1195                                                                                                                          NUMBER_ST,
1196                                                                                                                         0,
1197                                                                                                                         (void*)$5);
1198                                                                                 #else
1199                                                                                         $$=0;
1200                                                                                         yyerror("tls support not "
1201                                                                                                         "compiled in");
1202                                                                                 #endif
1203                                                                                                         }
1204                 | FORWARD_TLS LPAREN URIHOST RPAREN {
1205                                                                                 #ifdef USE_TLS
1206                                                                                         $$=mk_action(   FORWARD_TLS_T,
1207                                                                                                                          URIHOST_ST,
1208                                                                                                                          NUMBER_ST,
1209                                                                                                                         0,
1210                                                                                                                         0);
1211                                                                                 #else
1212                                                                                         $$=0;
1213                                                                                         yyerror("tls support not "
1214                                                                                                         "compiled in");
1215                                                                                 #endif
1216                                                                                 }
1217                 | FORWARD_TLS error { $$=0; yyerror("missing '(' or ')' ?"); }
1218                 | FORWARD_TLS LPAREN error RPAREN { $$=0; yyerror("bad forward_tls"
1219                                                                                 "argument"); }
1220                 
1221                 | SEND LPAREN host RPAREN       { $$=mk_action( SEND_T,
1222                                                                                                         STRING_ST,
1223                                                                                                         NUMBER_ST,
1224                                                                                                         $3,
1225                                                                                                         0);
1226                                                                         }
1227                 | SEND LPAREN STRING RPAREN { $$=mk_action(     SEND_T,
1228                                                                                                         STRING_ST,
1229                                                                                                         NUMBER_ST,
1230                                                                                                         $3,
1231                                                                                                         0);
1232                                                                         }
1233                 | SEND LPAREN ip RPAREN         { $$=mk_action( SEND_T,
1234                                                                                                         IP_ST,
1235                                                                                                         NUMBER_ST,
1236                                                                                                         (void*)$3,
1237                                                                                                         0);
1238                                                                         }
1239                 | SEND LPAREN host COMMA NUMBER RPAREN  { $$=mk_action( SEND_T,
1240                                                                                                                                 STRING_ST,
1241                                                                                                                                 NUMBER_ST,
1242                                                                                                                                 $3,
1243                                                                                                                                 (void*)$5);
1244                                                                                                 }
1245                 | SEND LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action( SEND_T,
1246                                                                                                                                 STRING_ST,
1247                                                                                                                                 NUMBER_ST,
1248                                                                                                                                 $3,
1249                                                                                                                                 (void*)$5);
1250                                                                                                 }
1251                 | SEND LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(    SEND_T,
1252                                                                                                                                 IP_ST,
1253                                                                                                                                 NUMBER_ST,
1254                                                                                                                                 (void*)$3,
1255                                                                                                                                 (void*)$5);
1256                                                                                            }
1257                 | SEND error { $$=0; yyerror("missing '(' or ')' ?"); }
1258                 | SEND LPAREN error RPAREN { $$=0; yyerror("bad send"
1259                                                                                                         "argument"); }
1260                 | SEND_TCP LPAREN host RPAREN   { $$=mk_action( SEND_TCP_T,
1261                                                                                                         STRING_ST,
1262                                                                                                         NUMBER_ST,
1263                                                                                                         $3,
1264                                                                                                         0);
1265                                                                         }
1266                 | SEND_TCP LPAREN STRING RPAREN { $$=mk_action( SEND_TCP_T,
1267                                                                                                         STRING_ST,
1268                                                                                                         NUMBER_ST,
1269                                                                                                         $3,
1270                                                                                                         0);
1271                                                                         }
1272                 | SEND_TCP LPAREN ip RPAREN             { $$=mk_action( SEND_TCP_T,
1273                                                                                                         IP_ST,
1274                                                                                                         NUMBER_ST,
1275                                                                                                         (void*)$3,
1276                                                                                                         0);
1277                                                                         }
1278                 | SEND_TCP LPAREN host COMMA NUMBER RPAREN      { $$=mk_action( SEND_TCP_T,
1279                                                                                                                                 STRING_ST,
1280                                                                                                                                 NUMBER_ST,
1281                                                                                                                                 $3,
1282                                                                                                                                 (void*)$5);
1283                                                                                                 }
1284                 | SEND_TCP LPAREN STRING COMMA NUMBER RPAREN {$$=mk_action(     SEND_TCP_T,
1285                                                                                                                                 STRING_ST,
1286                                                                                                                                 NUMBER_ST,
1287                                                                                                                                 $3,
1288                                                                                                                                 (void*)$5);
1289                                                                                                 }
1290                 | SEND_TCP LPAREN ip COMMA NUMBER RPAREN { $$=mk_action(        SEND_TCP_T,
1291                                                                                                                                 IP_ST,
1292                                                                                                                                 NUMBER_ST,
1293                                                                                                                                 (void*)$3,
1294                                                                                                                                 (void*)$5);
1295                                                                                            }
1296                 | SEND_TCP error { $$=0; yyerror("missing '(' or ')' ?"); }
1297                 | SEND_TCP LPAREN error RPAREN { $$=0; yyerror("bad send_tcp"
1298                                                                                                         "argument"); }
1299                 | DROP LPAREN RPAREN    {$$=mk_action(DROP_T,0, 0, 0, 0); }
1300                 | DROP                                  {$$=mk_action(DROP_T,0, 0, 0, 0); }
1301                 | LOG_TOK LPAREN STRING RPAREN  {$$=mk_action(  LOG_T, NUMBER_ST, 
1302                                                                                                         STRING_ST,(void*)4,$3);
1303                                                                         }
1304                 | LOG_TOK LPAREN NUMBER COMMA STRING RPAREN     {$$=mk_action(  LOG_T,
1305                                                                                                                                 NUMBER_ST, 
1306                                                                                                                                 STRING_ST,
1307                                                                                                                                 (void*)$3,
1308                                                                                                                                 $5);
1309                                                                                                 }
1310                 | LOG_TOK error { $$=0; yyerror("missing '(' or ')' ?"); }
1311                 | LOG_TOK LPAREN error RPAREN { $$=0; yyerror("bad log"
1312                                                                         "argument"); }
1313                 | SETFLAG LPAREN NUMBER RPAREN {$$=mk_action( SETFLAG_T, NUMBER_ST, 0,
1314                                                                                                         (void *)$3, 0 ); }
1315                 | SETFLAG error { $$=0; yyerror("missing '(' or ')'?"); }
1316                 | RESETFLAG LPAREN NUMBER RPAREN {$$=mk_action( RESETFLAG_T, NUMBER_ST, 0,
1317                                                                                                         (void *)$3, 0 ); }
1318                 | RESETFLAG error { $$=0; yyerror("missing '(' or ')'?"); }
1319                 | ISFLAGSET LPAREN NUMBER RPAREN {$$=mk_action( ISFLAGSET_T, NUMBER_ST, 0,
1320                                                                                                         (void *)$3, 0 ); }
1321                 | ISFLAGSET error { $$=0; yyerror("missing '(' or ')'?"); }
1322                 | ERROR LPAREN STRING COMMA STRING RPAREN {$$=mk_action(ERROR_T,
1323                                                                                                                                 STRING_ST, 
1324                                                                                                                                 STRING_ST,
1325                                                                                                                                 $3,
1326                                                                                                                                 $5);
1327                                                                                                   }
1328                 | ERROR error { $$=0; yyerror("missing '(' or ')' ?"); }
1329                 | ERROR LPAREN error RPAREN { $$=0; yyerror("bad error"
1330                                                                                                                 "argument"); }
1331                 | ROUTE LPAREN NUMBER RPAREN    { $$=mk_action(ROUTE_T, NUMBER_ST,
1332                                                                                                                 0, (void*)$3, 0);
1333                                                                                 }
1334                 | ROUTE error { $$=0; yyerror("missing '(' or ')' ?"); }
1335                 | ROUTE LPAREN error RPAREN { $$=0; yyerror("bad route"
1336                                                 "argument"); }
1337                 | EXEC LPAREN STRING RPAREN     { $$=mk_action( EXEC_T, STRING_ST, 0,
1338                                                                                                         $3, 0);
1339                                                                         }
1340                 | SET_HOST LPAREN STRING RPAREN { $$=mk_action(SET_HOST_T, STRING_ST,
1341                                                                                                                 0, $3, 0); }
1342                 | SET_HOST error { $$=0; yyerror("missing '(' or ')' ?"); }
1343                 | SET_HOST LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1344                                                                                                                 "string expected"); }
1345
1346                 | PREFIX LPAREN STRING RPAREN { $$=mk_action(PREFIX_T, STRING_ST,
1347                                                                                                                 0, $3, 0); }
1348                 | PREFIX error { $$=0; yyerror("missing '(' or ')' ?"); }
1349                 | PREFIX LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1350                                                                                                                 "string expected"); }
1351                 | STRIP_TAIL LPAREN NUMBER RPAREN { $$=mk_action(STRIP_TAIL_T, 
1352                                                                         NUMBER_ST, 0, (void *) $3, 0); }
1353                 | STRIP_TAIL error { $$=0; yyerror("missing '(' or ')' ?"); }
1354                 | STRIP_TAIL LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1355                                                                                                                 "number expected"); }
1356
1357                 | STRIP LPAREN NUMBER RPAREN { $$=mk_action(STRIP_T, NUMBER_ST,
1358                                                                                                                 0, (void *) $3, 0); }
1359                 | STRIP error { $$=0; yyerror("missing '(' or ')' ?"); }
1360                 | STRIP LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1361                                                                                                                 "number expected"); }
1362
1363                 | APPEND_BRANCH LPAREN STRING RPAREN { $$=mk_action( APPEND_BRANCH_T,
1364                                                                                                         STRING_ST, 0, $3, 0) ; }
1365                 | APPEND_BRANCH LPAREN RPAREN { $$=mk_action( APPEND_BRANCH_T,
1366                                                                                                         STRING_ST, 0, 0, 0 ) ; }
1367                 | APPEND_BRANCH {  $$=mk_action( APPEND_BRANCH_T, STRING_ST, 0, 0, 0 ) ; }
1368
1369                 | SET_HOSTPORT LPAREN STRING RPAREN { $$=mk_action( SET_HOSTPORT_T, 
1370                                                                                                                 STRING_ST, 0, $3, 0); }
1371                 | SET_HOSTPORT error { $$=0; yyerror("missing '(' or ')' ?"); }
1372                 | SET_HOSTPORT LPAREN error RPAREN { $$=0; yyerror("bad argument,"
1373                                                                                                 " string expected"); }
1374                 | SET_PORT LPAREN STRING RPAREN { $$=mk_action( SET_PORT_T, STRING_ST,
1375                                                                                                                 0, $3, 0); }
1376                 | SET_PORT error { $$=0; yyerror("missing '(' or ')' ?"); }
1377                 | SET_PORT LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1378                                                                                                                 "string expected"); }
1379                 | SET_USER LPAREN STRING RPAREN { $$=mk_action( SET_USER_T, STRING_ST,
1380                                                                                                                 0, $3, 0); }
1381                 | SET_USER error { $$=0; yyerror("missing '(' or ')' ?"); }
1382                 | SET_USER LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1383                                                                                                                 "string expected"); }
1384                 | SET_USERPASS LPAREN STRING RPAREN { $$=mk_action( SET_USERPASS_T, 
1385                                                                                                                 STRING_ST, 0, $3, 0); }
1386                 | SET_USERPASS error { $$=0; yyerror("missing '(' or ')' ?"); }
1387                 | SET_USERPASS LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1388                                                                                                                 "string expected"); }
1389                 | SET_URI LPAREN STRING RPAREN { $$=mk_action( SET_URI_T, STRING_ST, 
1390                                                                                                                 0, $3, 0); }
1391                 | SET_URI error { $$=0; yyerror("missing '(' or ')' ?"); }
1392                 | SET_URI LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1393                                                                                 "string expected"); }
1394                 | REVERT_URI LPAREN RPAREN { $$=mk_action( REVERT_URI_T, 0,0,0,0); }
1395                 | REVERT_URI { $$=mk_action( REVERT_URI_T, 0,0,0,0); }
1396                 | FORCE_RPORT LPAREN RPAREN     {
1397                                                         #ifdef USE_TCP
1398                                                                 $$=mk_action(FORCE_RPORT_T,0, 0, 0, 0); 
1399                                                         #else
1400                                                                 yyerror("tcp support not compiled in");
1401                                                         #endif
1402                                                         }
1403                 | FORCE_RPORT                           {$$=mk_action(FORCE_RPORT_T,0, 0, 0, 0); }
1404                 | FORCE_TCP_ALIAS LPAREN NUMBER RPAREN  {
1405                                         #ifdef USE_TCP
1406                                                 $$=mk_action(FORCE_TCP_ALIAS_T,NUMBER_ST, 0,
1407                                                                                 (void*)$3, 0);
1408                                         #else
1409                                                 yyerror("tcp support not compiled in");
1410                                         #endif
1411                                                                                                 }
1412                 | FORCE_TCP_ALIAS LPAREN RPAREN {
1413                                         #ifdef USE_TCP
1414                                                 $$=mk_action(FORCE_TCP_ALIAS_T,0, 0, 0, 0); 
1415                                         #else
1416                                                 yyerror("tcp support not compiled in");
1417                                         #endif
1418                                                                                 }
1419                 | FORCE_TCP_ALIAS                               {
1420                                         #ifdef USE_TCP
1421                                                 $$=mk_action(FORCE_TCP_ALIAS_T,0, 0, 0, 0);
1422                                         #else
1423                                                 yyerror("tcp support not compiled in");
1424                                         #endif
1425                                                                                 }
1426                 | FORCE_TCP_ALIAS LPAREN error RPAREN   {$$=0; 
1427                                                                                                 yyerror("bad argument,"
1428                                                                                                                 " number expected");
1429                                                                                                 }
1430                 | SET_ADV_ADDRESS LPAREN listen_id RPAREN {
1431                                                                 $$=0;
1432                                                                 if ((str_tmp=pkg_malloc(sizeof(str)))==0){
1433                                                                                 LOG(L_CRIT, "ERROR: cfg. parser:"
1434                                                                                                         " out of memory.\n");
1435                                                                 }else{
1436                                                                                 str_tmp->s=$3;
1437                                                                                 str_tmp->len=strlen($3);
1438                                                                                 $$=mk_action(SET_ADV_ADDR_T, STR_ST,
1439                                                                                              0, str_tmp, 0);
1440                                                                 }
1441                                                                                                   }
1442                 | SET_ADV_ADDRESS LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1443                                                                                                                 "string expected"); }
1444                 | SET_ADV_ADDRESS error {$$=0; yyerror("missing '(' or ')' ?"); }
1445                 | SET_ADV_PORT LPAREN NUMBER RPAREN {
1446                                                                 $$=0;
1447                                                                 tmp=int2str($3, &i_tmp);
1448                                                                 if ((str_tmp=pkg_malloc(sizeof(str)))==0){
1449                                                                                 LOG(L_CRIT, "ERROR: cfg. parser:"
1450                                                                                                         " out of memory.\n");
1451                                                                 }else{
1452                                                                         if ((str_tmp->s=pkg_malloc(i_tmp))==0){
1453                                                                                 LOG(L_CRIT, "ERROR: cfg. parser:"
1454                                                                                                         " out of memory.\n");
1455                                                                         }else{
1456                                                                                 memcpy(str_tmp->s, tmp, i_tmp);
1457                                                                                 str_tmp->len=i_tmp;
1458                                                                                 $$=mk_action(SET_ADV_PORT_T, STR_ST,
1459                                                                                                         0, str_tmp, 0);
1460                                                                         }
1461                                                                 }
1462                                                                             }
1463                 | SET_ADV_PORT LPAREN error RPAREN { $$=0; yyerror("bad argument, "
1464                                                                                                                 "string expected"); }
1465                 | SET_ADV_PORT  error {$$=0; yyerror("missing '(' or ')' ?"); }
1466                 | ID LPAREN RPAREN                      { f_tmp=(void*)find_export($1, 0, rt);
1467                                                                            if (f_tmp==0){
1468                                                                                    if (find_export($1, 0, 0)) {
1469                                                                                            yyerror("Command cannot be used in the block\n");
1470                                                                                    } else {
1471                                                                                            yyerror("unknown command, missing"
1472                                                                                                    " loadmodule?\n");
1473                                                                                    }
1474                                                                                 $$=0;
1475                                                                            }else{
1476                                                                                 $$=mk_action(   MODULE_T,
1477                                                                                                                 CMDF_ST,
1478                                                                                                                 0,
1479                                                                                                                 f_tmp,
1480                                                                                                                 0
1481                                                                                                         );
1482                                                                            }
1483                                                                         }
1484                 | ID LPAREN STRING RPAREN { f_tmp=(void*)find_export($1, 1, rt);
1485                                                                         if (f_tmp==0){
1486                                                                                 if (find_export($1, 1, 0)) {
1487                                                                                         yyerror("Command cannot be used in the block\n");
1488                                                                                 } else {
1489                                                                                         yyerror("unknown command, missing"
1490                                                                                                 " loadmodule?\n");
1491                                                                                 }
1492                                                                                 $$=0;
1493                                                                         }else{
1494                                                                                 $$=mk_action(   MODULE_T,
1495                                                                                                                 CMDF_ST,
1496                                                                                                                 STRING_ST,
1497                                                                                                                 f_tmp,
1498                                                                                                                 $3
1499                                                                                                         );
1500                                                                         }
1501                                                                   }
1502                 | ID LPAREN STRING  COMMA STRING RPAREN 
1503                                                                   { f_tmp=(void*)find_export($1, 2, rt);
1504                                                                         if (f_tmp==0){
1505                                                                                 if (find_export($1, 2, 0)) {
1506                                                                                         yyerror("Command cannot be used in the block\n");
1507                                                                                 } else {
1508                                                                                         yyerror("unknown command, missing"
1509                                                                                                 " loadmodule?\n");
1510                                                                                 }
1511                                                                                 $$=0;
1512                                                                         }else{
1513                                                                                 $$=mk_action3(  MODULE_T,
1514                                                                                                                 CMDF_ST,
1515                                                                                                                 STRING_ST,
1516                                                                                                                 STRING_ST,
1517                                                                                                                 f_tmp,
1518                                                                                                                 $3,
1519                                                                                                                 $5
1520                                                                                                         );
1521                                                                         }
1522                                                                   }
1523                 | ID LPAREN error RPAREN { $$=0; yyerror("bad arguments"); }
1524         ;
1525
1526
1527 %%
1528
1529 extern int line;
1530 extern int column;
1531 extern int startcolumn;
1532 void warn(char* s)
1533 {
1534         LOG(L_WARN, "cfg. warning: (%d,%d-%d): %s\n", line, startcolumn, 
1535                         column, s);
1536         cfg_errors++;
1537 }
1538
1539 void yyerror(char* s)
1540 {
1541         LOG(L_CRIT, "parse error (%d,%d-%d): %s\n", line, startcolumn, 
1542                         column, s);
1543         cfg_errors++;
1544 }
1545
1546
1547 static struct id_list* mk_listen_id(char* host, int proto, int port)
1548 {
1549         struct id_list* l;
1550         l=pkg_malloc(sizeof(struct id_list));
1551         if (l==0){
1552                 LOG(L_CRIT,"ERROR: cfg. parser: out of memory.\n");
1553         }else{
1554                 l->name=host;
1555                 l->port=port;
1556                 l->proto=proto;
1557                 l->next=0;
1558         }
1559         return l;
1560 }
1561
1562
1563 /*
1564 int main(int argc, char ** argv)
1565 {
1566         if (yyparse()!=0)
1567                 fprintf(stderr, "parsing error\n");
1568 }
1569 */