"Fossies" - the Fresh Open Source Software Archive

Member "portfwd-0.29/src/conf.y" (28 Jan 2004, 8414 Bytes) of package /linux/privat/old/portfwd-0.29.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bison source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2   conf.y
    3 
    4   $Id: conf.y,v 1.6 2004/01/28 19:14:10 evertonm Exp $
    5 */
    6 
    7 %{
    8 
    9 #include <stdio.h>
   10 #include <stdlib.h>
   11 #include <syslog.h>
   12 
   13 #include "util.h"
   14 #include "solve.h"
   15 #include "port_pair.h"
   16 #include "proto_map.hpp"
   17 #include "entry.hpp"
   18 #include "dst_addr.hpp"
   19 #include "director.hpp"
   20 #include "portfwd.h"
   21 
   22 /*
   23  * These are from the lexical analyzer defined in conf.lex
   24  */
   25 extern int yylex();
   26 extern char yytext[];
   27 extern void show_last_token();
   28 extern int conf_line_number;
   29 extern char conf_ident[];
   30 extern char conf_lex_str_buf[];
   31 
   32 /*
   33  * Some useful constants
   34  */
   35 const char *const ANY_ADDR = "0.0.0.0";
   36 const int MIN_MASK_LEN = 0;
   37 const int MAX_MASK_LEN = 32;
   38 
   39 /*
   40  * We store the number of syntax errors here
   41  */
   42 int conf_syntax_errors = 0;
   43 
   44 void yyerror(const char *msg)
   45 {
   46   ++conf_syntax_errors;
   47   syslog(LOG_ERR, "Syntax error: %s\n", msg);
   48   show_last_token();
   49 }
   50 
   51 /* Variaveis Auxiliares */
   52 
   53 proto_t curr_proto;
   54 vector<from_addr*> *from_vector;
   55 vector<to_addr*>   *dst_vector;
   56 vector<host_map*>  *host_vector;
   57 vector<int>        *port_vector;
   58 vector<proto_map*> *map_vector;
   59 vector<entry*>     *entry_vector = new vector<entry*>();
   60 
   61 int            conf_user   = -1;
   62 int            conf_group  = -1;
   63 
   64 const struct ip_addr conf_any_addr = solve_hostname(ANY_ADDR);
   65 struct ip_addr conf_listen         = conf_any_addr;
   66 struct ip_addr conf_source         = conf_any_addr;
   67 struct ip_addr *conf_src           = 0;
   68 
   69 /* Funcoes Auxiliares */
   70 
   71 int mask_len_value(const char *len)
   72 {
   73   return atoi(len);
   74 }
   75 
   76 void set_protoname(proto_t proto)
   77 {
   78   curr_proto = proto;
   79   ONVERBOSE(syslog(LOG_DEBUG, "Protocol: %s\n", get_protoname(curr_proto)))
   80 }
   81 
   82 int use_port(char *portname)
   83 {
   84   int port = solve_port(portname, get_protoname(curr_proto));
   85   free(portname);
   86   return port;
   87 }
   88 
   89 struct ip_addr use_hostname(char *hostname)
   90 {
   91   struct ip_addr ip = solve_hostname(hostname);
   92   free(hostname);
   93   return ip;
   94 }
   95 
   96 net_portion *use_hostprefix(char *hostname, int prefix_len)
   97 {
   98   return new net_portion(use_hostname(hostname), prefix_len);  
   99 }
  100 
  101 to_addr *use_dstaddr(char *hostname, int port)
  102 {
  103   return new dst_addr(on_the_fly_dns ? safe_strdup(hostname) : 0, 
  104              use_hostname(hostname), 
  105              port);
  106 }
  107 
  108 %}
  109 
  110 %token TK_NAME
  111 
  112 %token TK_TCP
  113 %token TK_UDP
  114 
  115 %token TK_COLON
  116 %token TK_SCOLON
  117 %token TK_COMMA
  118 %token TK_SLASH
  119 %token TK_RANGE
  120 %token TK_LBRACE
  121 %token TK_RBRACE
  122 %token TK_ARROW
  123 %token TK_ACTV
  124 %token TK_PASV
  125 %token TK_USER
  126 %token TK_GROUP
  127 %token TK_BIND
  128 %token TK_LISTEN
  129 %token TK_SOURCE
  130 %token TK_STRING
  131 %token TK_FRAGILE
  132 
  133 %token TK_ILLEGAL
  134 
  135 %union {
  136     int            int_type;
  137     bool               bool_type;
  138         char               *str_type;
  139         port_pair          *port_type;
  140         net_portion        *net_type;
  141     from_addr          *from_type;
  142     vector<from_addr*> *from_list_type;
  143     to_addr        *dst_type;
  144     vector<to_addr*>   *dst_list_type;
  145     host_map           *host_map_type;
  146     vector<host_map*>  *host_list_type;
  147     vector<int>        *port_list_type;
  148     proto_map          *map_type;
  149     vector<proto_map*> *map_list_type;
  150     entry          *entry_type;
  151 }
  152 
  153 %type <int_type>    prefix_length;
  154 %type <bool_type>   fragile;
  155 %type <port_type>       port_range;
  156 %type <str_type>        name;
  157 %type <net_type>        host_prefix;
  158 %type <from_type>       from;
  159 %type <from_list_type>  from_list;
  160 %type <dst_type>        dst;
  161 %type <dst_list_type>   dst_list;
  162 %type <host_map_type>   host_map;
  163 %type <host_list_type>  host_list;
  164 %type <port_list_type>  port_list;
  165 %type <map_type>        map;
  166 %type <map_list_type>   map_list;
  167 %type <map_list_type>   section;
  168 %type <entry_type>      entry;
  169 
  170 %{
  171   /* Simbolo nao-terminal inicial */
  172 %}
  173 
  174 %start conf
  175 
  176 %%
  177 
  178 conf:       /* empty */ |
  179                 stmt_list ;
  180 
  181 stmt_list:      stmt |
  182                 stmt_list stmt ;
  183 
  184 stmt:           entry { entry_vector-> push($1); } |
  185                 global_option ;
  186 
  187 global_option:  TK_USER TK_NAME { conf_user = solve_user(conf_ident); } |
  188                 TK_GROUP TK_NAME { conf_group = solve_group(conf_ident); } |
  189                 TK_LISTEN TK_NAME { conf_listen = solve_hostname(conf_ident); } |
  190                 TK_SOURCE TK_NAME { 
  191                     conf_source = solve_hostname(conf_ident); 
  192                     conf_src = &conf_source;
  193         } |
  194         TK_BIND TK_NAME { conf_listen = solve_hostname(conf_ident); };
  195 
  196 entry:         fragile TK_TCP set_proto_tcp section { $$ = new entry(P_TCP, $4, $1); } | 
  197                        TK_UDP set_proto_udp section { $$ = new entry(P_UDP, $3, 0 /* false */); } ;
  198 
  199 fragile: /* empty */ { $$ = 0; /* false */ } |
  200          TK_FRAGILE { $$ = 1; /* true */ } ;
  201 
  202 set_proto_tcp:  { set_protoname(P_TCP); } ;
  203 set_proto_udp:  { set_protoname(P_UDP); } ;
  204 
  205 section:        TK_LBRACE map_list TK_RBRACE { $$ = $2; } ;
  206 
  207 map_list:   map {
  208             map_vector = new vector<proto_map*>();
  209             map_vector->push($1);
  210             $$ = map_vector;
  211         } | 
  212         map_list TK_SCOLON map {
  213             map_vector->push($3);
  214             $$ = map_vector;
  215         } ;
  216 
  217 map:            port_list TK_LBRACE host_list TK_RBRACE {
  218             $$ = new proto_map($1, $3, 0, 0, conf_user, conf_group, conf_listen, conf_src);
  219         } | 
  220                 port_list TK_ACTV name TK_LBRACE host_list TK_RBRACE {
  221                 struct ip_addr ip = use_hostname($3);
  222             $$ = new proto_map($1, $5, &ip, 0, conf_user, conf_group, conf_listen, conf_src);
  223         } |
  224                 port_list TK_PASV name TK_LBRACE host_list TK_RBRACE {
  225                 struct ip_addr ip = use_hostname($3);
  226             $$ = new proto_map($1, $5, 0, &ip, conf_user, conf_group, conf_listen, conf_src);
  227         } |
  228                 port_list TK_ACTV name TK_PASV name TK_LBRACE host_list TK_RBRACE {
  229                 struct ip_addr ip1 = use_hostname($3);
  230                 struct ip_addr ip2 = use_hostname($5);
  231             $$ = new proto_map($1, $7, &ip1, &ip2, conf_user, conf_group, conf_listen, conf_src);
  232         } |
  233                 port_list TK_PASV name TK_ACTV name TK_LBRACE host_list TK_RBRACE {
  234                 struct ip_addr ip1 = use_hostname($3);
  235                 struct ip_addr ip2 = use_hostname($5);
  236             $$ = new proto_map($1, $7, &ip2, &ip1, conf_user, conf_group, conf_listen, conf_src);
  237         } ;
  238 
  239 name:           TK_NAME { $$ = safe_strdup(conf_ident); } ;
  240 
  241 port_list:      name {
  242             port_vector = new vector<int>();
  243             port_vector->push(use_port($1));
  244             $$ = port_vector; 
  245         } |
  246                 port_list TK_COMMA name {
  247             port_vector->push(use_port($3));
  248             $$ = port_vector; 
  249         } ;
  250 
  251 host_list:  host_map {
  252                     host_vector = new vector<host_map*>();
  253             host_vector->push($1);
  254             $$ = host_vector;
  255         } |
  256             host_list TK_SCOLON host_map {
  257             host_vector->push($3);
  258             $$ = host_vector;
  259         } ;
  260 
  261 host_map:   from_list TK_ARROW dst_list {
  262             $$ = new host_map($1, $3);
  263         } ;
  264 
  265 dst_list:       dst {
  266             dst_vector = new vector<to_addr*>();
  267             dst_vector->push($1);
  268             $$ = dst_vector;
  269                 } |
  270                 dst_list TK_COMMA dst {
  271             dst_vector->push($3);
  272             $$ = dst_vector;
  273                 } ;
  274 
  275 dst:            name TK_COLON name {
  276             int port = use_port($3);   /* solve portname */
  277             $$ = use_dstaddr($1, port); /* new dst_addr() */
  278                 } |
  279                 TK_STRING {
  280                         $$ = new director(conf_lex_str_buf);
  281         } ;
  282 
  283 from_list:  from {
  284             from_vector = new vector<from_addr*>();
  285             from_vector->push($1);
  286             $$ = from_vector;
  287         } |
  288         from_list TK_COMMA from {
  289             from_vector->push($3);
  290             $$ = from_vector;
  291         } ;
  292 
  293 from:           /* empty */ { 
  294             $$ = new from_addr(new net_portion(solve_hostname(ANY_ADDR), MIN_MASK_LEN), new port_pair(FIRST_PORT, LAST_PORT)); 
  295         } |
  296                 host_prefix { 
  297             $$ = new from_addr($1, new port_pair(FIRST_PORT, LAST_PORT)); 
  298         } |
  299                 TK_COLON port_range { 
  300             $$ = new from_addr(new net_portion(solve_hostname(ANY_ADDR), MIN_MASK_LEN), $2); 
  301         } |
  302                 host_prefix TK_COLON port_range { 
  303             $$ = new from_addr($1, $3); 
  304         } ;
  305 
  306 host_prefix:    name prefix_length { 
  307             /* use_hostprefix(): new net_portion() */
  308             $$ = use_hostprefix($1, $2); 
  309         } ;
  310 
  311 prefix_length:  /* empty */ { $$ = MAX_MASK_LEN; } |
  312                 TK_SLASH TK_NAME { $$ = mask_len_value(conf_ident); } ;
  313 
  314 port_range: name {
  315             int port = use_port($1);
  316             $$ = new port_pair(port, port);
  317         } |
  318             name TK_RANGE { 
  319             $$ = new port_pair(use_port($1), 
  320                        LAST_PORT); 
  321         } |
  322             TK_RANGE name { 
  323             $$ = new port_pair(FIRST_PORT, 
  324                            use_port($2)); 
  325         } |
  326             name TK_RANGE name { 
  327             $$ = new port_pair(use_port($1),
  328                            use_port($3));
  329         } ;
  330 
  331 
  332 %%
  333 
  334 /* C code */
  335 
  336 /* eof: conf.y */
  337