Home | History | Annotate | Line # | Download | only in mrouted
cfparse.y revision 1.5
      1 %{
      2 /*	$NetBSD: cfparse.y,v 1.5 1998/03/30 02:33:37 mrg Exp $	*/
      3 
      4 /*
      5  * Configuration file parser for mrouted.
      6  *
      7  * Written by Bill Fenner, NRL, 1994
      8  */
      9 #include <stdio.h>
     10 #ifdef __STDC__
     11 #include <stdarg.h>
     12 #else
     13 #include <string.h>
     14 #include <varargs.h>
     15 #endif
     16 #include "defs.h"
     17 #include <netdb.h>
     18 
     19 /*
     20  * Local function declarations
     21  */
     22 static void		fatal __P((char *fmt, ...));
     23 static void		warn __P((char *fmt, ...));
     24 static void		yyerror __P((char *s));
     25 static char *		next_word __P((void));
     26 static int		yylex __P((void));
     27 static u_int32_t	valid_if __P((char *s));
     28 static struct ifreq *	ifconfaddr __P((struct ifconf *ifcp, u_int32_t a));
     29 int			yyparse __P((void));
     30 
     31 static FILE *f __attribute__((__unused__));	/* XXX egcs */
     32 extern int udp_socket;
     33 char *configfilename = _PATH_MROUTED_CONF;
     34 
     35 extern int cache_lifetime;
     36 extern int max_prune_lifetime;
     37 
     38 static int lineno;
     39 static struct ifreq ifbuf[32];
     40 static struct ifconf ifc;
     41 
     42 static struct uvif *v;
     43 
     44 static int order;
     45 
     46 struct addrmask {
     47 	u_int32_t	addr;
     48 	int	mask;
     49 };
     50 
     51 struct boundnam {
     52 	char		*name;
     53 	struct addrmask	 bound;
     54 };
     55 
     56 #define MAXBOUNDS 20
     57 
     58 struct boundnam boundlist[MAXBOUNDS];	/* Max. of 20 named boundaries */
     59 int numbounds = 0;			/* Number of named boundaries */
     60 
     61 %}
     62 
     63 %union
     64 {
     65 	int num;
     66 	char *ptr;
     67 	struct addrmask addrmask;
     68 	u_int32_t addr;
     69 };
     70 
     71 %token CACHE_LIFETIME PRUNING
     72 %token PHYINT TUNNEL NAME
     73 %token DISABLE IGMPV1 SRCRT
     74 %token METRIC THRESHOLD RATE_LIMIT BOUNDARY NETMASK ALTNET
     75 %token SYSNAM SYSCONTACT SYSVERSION SYSLOCATION
     76 %token <num> BOOLEAN
     77 %token <num> NUMBER
     78 %token <ptr> STRING
     79 %token <addrmask> ADDRMASK
     80 %token <addr> ADDR
     81 
     82 %type <addr> interface addrname
     83 %type <addrmask> bound boundary addrmask
     84 
     85 %start conf
     86 
     87 %%
     88 
     89 conf	: stmts
     90 	;
     91 
     92 stmts	: /* Empty */
     93 	| stmts stmt
     94 	;
     95 
     96 stmt	: error
     97 	| PHYINT interface 		{
     98 
     99 			vifi_t vifi;
    100 
    101 			if (order)
    102 			    fatal("phyints must appear before tunnels");
    103 
    104 			for (vifi = 0, v = uvifs;
    105 			     vifi < numvifs;
    106 			     ++vifi, ++v)
    107 			    if (!(v->uv_flags & VIFF_TUNNEL) &&
    108 				$2 == v->uv_lcl_addr)
    109 				break;
    110 
    111 			if (vifi == numvifs)
    112 			    fatal("%s is not a configured interface",
    113 				inet_fmt($2,s1));
    114 
    115 					}
    116 		ifmods
    117 	| TUNNEL interface addrname	{
    118 
    119 			struct ifreq *ifr;
    120 			struct ifreq ffr;
    121 			vifi_t vifi;
    122 
    123 			order++;
    124 
    125 			ifr = ifconfaddr(&ifc, $2);
    126 			if (ifr == 0)
    127 			    fatal("Tunnel local address %s is not mine",
    128 				inet_fmt($2, s1));
    129 
    130 			strncpy(ffr.ifr_name, ifr->ifr_name, IFNAMSIZ);
    131 			if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ffr)<0)
    132 			    fatal("ioctl SIOCGIFFLAGS on %s",ffr.ifr_name);
    133 			if (ffr.ifr_flags & IFF_LOOPBACK)
    134 			    fatal("Tunnel local address %s is a loopback interface",
    135 				inet_fmt($2, s1));
    136 
    137 			if (ifconfaddr(&ifc, $3) != 0)
    138 			    fatal("Tunnel remote address %s is one of mine",
    139 				inet_fmt($3, s1));
    140 
    141 			for (vifi = 0, v = uvifs;
    142 			     vifi < numvifs;
    143 			     ++vifi, ++v)
    144 			    if (v->uv_flags & VIFF_TUNNEL) {
    145 				if ($3 == v->uv_rmt_addr)
    146 				    fatal("Duplicate tunnel to %s",
    147 					inet_fmt($3, s1));
    148 			    } else if (!(v->uv_flags & VIFF_DISABLED)) {
    149 				if (($3 & v->uv_subnetmask) == v->uv_subnet)
    150 				    fatal("Unnecessary tunnel to %s",
    151 					inet_fmt($3,s1));
    152 			    }
    153 
    154 			if (numvifs == MAXVIFS)
    155 			    fatal("too many vifs");
    156 
    157 			v = &uvifs[numvifs];
    158 			v->uv_flags	= VIFF_TUNNEL;
    159 			v->uv_metric	= DEFAULT_METRIC;
    160 			v->uv_rate_limit= DEFAULT_TUN_RATE_LIMIT;
    161 			v->uv_threshold	= DEFAULT_THRESHOLD;
    162 			v->uv_lcl_addr	= $2;
    163 			v->uv_rmt_addr	= $3;
    164 			v->uv_subnet	= 0;
    165 			v->uv_subnetmask= 0;
    166 			v->uv_subnetbcast= 0;
    167 			strncpy(v->uv_name, ffr.ifr_name, IFNAMSIZ);
    168 			v->uv_groups	= NULL;
    169 			v->uv_neighbors	= NULL;
    170 			v->uv_acl	= NULL;
    171 			v->uv_addrs	= NULL;
    172 
    173 			if (!(ffr.ifr_flags & IFF_UP)) {
    174 			    v->uv_flags |= VIFF_DOWN;
    175 			    vifs_down = TRUE;
    176 			}
    177 					}
    178 		tunnelmods
    179 					{
    180 			log(LOG_INFO, 0,
    181 			    "installing tunnel from %s to %s as vif #%u - rate=%d",
    182 			    inet_fmt($2, s1), inet_fmt($3, s2),
    183 			    numvifs, v->uv_rate_limit);
    184 
    185 			++numvifs;
    186 					}
    187 	| PRUNING BOOLEAN	    { pruning = $2; }
    188 	| CACHE_LIFETIME NUMBER     { cache_lifetime = $2;
    189 				      max_prune_lifetime = cache_lifetime * 2;
    190 				    }
    191 	| NAME STRING boundary	    { if (numbounds >= MAXBOUNDS) {
    192 					fatal("Too many named boundaries (max %d)", MAXBOUNDS);
    193 				      }
    194 
    195 				      boundlist[numbounds].name = malloc(strlen($2) + 1);
    196 				      strcpy(boundlist[numbounds].name, $2);
    197 				      boundlist[numbounds++].bound = $3;
    198 				    }
    199 	| SYSNAM STRING    {
    200 #ifdef SNMP
    201 			    set_sysName($2);
    202 #endif /* SNMP */
    203 			    }
    204 	| SYSCONTACT STRING {
    205 #ifdef SNMP
    206 			    set_sysContact($2);
    207 #endif /* SNMP */
    208 			    }
    209         | SYSVERSION STRING {
    210 #ifdef SNMP
    211 			    set_sysVersion($2);
    212 #endif /* SNMP */
    213 			    }
    214 	| SYSLOCATION STRING {
    215 #ifdef SNMP
    216 			    set_sysLocation($2);
    217 #endif /* SNMP */
    218 			    }
    219 	;
    220 
    221 tunnelmods	: /* empty */
    222 	| tunnelmods tunnelmod
    223 	;
    224 
    225 tunnelmod	: mod
    226 	| SRCRT			{ fatal("Source-route tunnels not supported"); }
    227 	;
    228 
    229 ifmods	: /* empty */
    230 	| ifmods ifmod
    231 	;
    232 
    233 ifmod	: mod
    234 	| DISABLE		{ v->uv_flags |= VIFF_DISABLED; }
    235 	| IGMPV1		{ v->uv_flags |= VIFF_IGMPV1; }
    236 	| NETMASK addrname	{
    237 				  u_int32_t subnet, mask;
    238 
    239 				  mask = $2;
    240 				  subnet = v->uv_lcl_addr & mask;
    241 				  if (!inet_valid_subnet(subnet, mask))
    242 					fatal("Invalid netmask");
    243 				  v->uv_subnet = subnet;
    244 				  v->uv_subnetmask = mask;
    245 				  v->uv_subnetbcast = subnet | ~mask;
    246 				}
    247 	| NETMASK		{
    248 
    249 		    warn("Expected address after netmask keyword, ignored");
    250 
    251 				}
    252 	| ALTNET addrmask	{
    253 
    254 		    struct phaddr *ph;
    255 
    256 		    ph = (struct phaddr *)malloc(sizeof(struct phaddr));
    257 		    if (ph == NULL)
    258 			fatal("out of memory");
    259 		    if ($2.mask) {
    260 			VAL_TO_MASK(ph->pa_subnetmask, $2.mask);
    261 		    } else
    262 			ph->pa_subnetmask = v->uv_subnetmask;
    263 		    ph->pa_subnet = $2.addr & ph->pa_subnetmask;
    264 		    ph->pa_subnetbcast = ph->pa_subnet | ~ph->pa_subnetmask;
    265 		    if ($2.addr & ~ph->pa_subnetmask)
    266 			warn("Extra subnet %s/%d has host bits set",
    267 				inet_fmt($2.addr,s1), $2.mask);
    268 		    ph->pa_next = v->uv_addrs;
    269 		    v->uv_addrs = ph;
    270 
    271 				}
    272 	| ALTNET		{
    273 
    274 		    warn("Expected address after altnet keyword, ignored");
    275 
    276 				}
    277 	;
    278 
    279 mod	: THRESHOLD NUMBER	{ if ($2 < 1 || $2 > 255)
    280 				    fatal("Invalid threshold %d",$2);
    281 				  v->uv_threshold = $2;
    282 				}
    283 	| THRESHOLD		{
    284 
    285 		    warn("Expected number after threshold keyword, ignored");
    286 
    287 				}
    288 	| METRIC NUMBER		{ if ($2 < 1 || $2 > UNREACHABLE)
    289 				    fatal("Invalid metric %d",$2);
    290 				  v->uv_metric = $2;
    291 				}
    292 	| METRIC		{
    293 
    294 		    warn("Expected number after metric keyword, ignored");
    295 
    296 				}
    297 	| RATE_LIMIT NUMBER	{ if ($2 > MAX_RATE_LIMIT)
    298 				    fatal("Invalid rate_limit %d",$2);
    299 				  v->uv_rate_limit = $2;
    300 				}
    301 	| RATE_LIMIT		{
    302 
    303 		    warn("Expected number after rate_limit keyword, ignored");
    304 
    305 				}
    306 	| BOUNDARY bound	{
    307 
    308 		    struct vif_acl *v_acl;
    309 
    310 		    v_acl = (struct vif_acl *)malloc(sizeof(struct vif_acl));
    311 		    if (v_acl == NULL)
    312 			fatal("out of memory");
    313 		    VAL_TO_MASK(v_acl->acl_mask, $2.mask);
    314 		    v_acl->acl_addr = $2.addr & v_acl->acl_mask;
    315 		    if ($2.addr & ~v_acl->acl_mask)
    316 			warn("Boundary spec %s/%d has host bits set",
    317 				inet_fmt($2.addr,s1),$2.mask);
    318 		    v_acl->acl_next = v->uv_acl;
    319 		    v->uv_acl = v_acl;
    320 
    321 				}
    322 	| BOUNDARY		{
    323 
    324 		warn("Expected boundary spec after boundary keyword, ignored");
    325 
    326 				}
    327 	;
    328 
    329 interface	: ADDR		{ $$ = $1; }
    330 	| STRING		{
    331 				  $$ = valid_if($1);
    332 				  if ($$ == 0)
    333 					fatal("Invalid interface name %s",$1);
    334 				}
    335 	;
    336 
    337 addrname	: ADDR		{ $$ = $1; }
    338 	| STRING		{ struct hostent *hp;
    339 
    340 				  if ((hp = gethostbyname($1)) == NULL)
    341 				    fatal("No such host %s", $1);
    342 
    343 				  if (hp->h_addr_list[1])
    344 				    fatal("Hostname %s does not %s",
    345 					$1, "map to a unique address");
    346 
    347 				  bcopy(hp->h_addr_list[0], &$$,
    348 					    hp->h_length);
    349 				}
    350 
    351 bound	: boundary		{ $$ = $1; }
    352 	| STRING		{ int i;
    353 
    354 				  for (i=0; i < numbounds; i++) {
    355 				    if (!strcmp(boundlist[i].name, $1)) {
    356 					$$ = boundlist[i].bound;
    357 					break;
    358 				    }
    359 				  }
    360 				  if (i == numbounds) {
    361 				    fatal("Invalid boundary name %s",$1);
    362 				  }
    363 				}
    364 	;
    365 
    366 boundary	: ADDRMASK	{
    367 
    368 			if ((ntohl($1.addr) & 0xff000000) != 0xef000000) {
    369 			    fatal("Boundaries must be 239.x.x.x, not %s/%d",
    370 				inet_fmt($1.addr, s1), $1.mask);
    371 			}
    372 			$$ = $1;
    373 
    374 				}
    375 	;
    376 
    377 addrmask	: ADDRMASK	{ $$ = $1; }
    378 	| ADDR			{ $$.addr = $1; $$.mask = 0; }
    379 	;
    380 %%
    381 #ifdef __STDC__
    382 static void
    383 fatal(char *fmt, ...)
    384 {
    385 	va_list ap;
    386 	char buf[200];
    387 
    388 	va_start(ap, fmt);
    389 #else
    390 /*VARARGS1*/
    391 static void
    392 fatal(fmt, va_alist)
    393 char *fmt;
    394 va_dcl
    395 {
    396 	va_list ap;
    397 	char buf[200];
    398 
    399 	va_start(ap);
    400 #endif
    401 	vsprintf(buf, fmt, ap);
    402 	va_end(ap);
    403 
    404 	log(LOG_ERR,0,"%s: %s near line %d", configfilename, buf, lineno);
    405 }
    406 
    407 #ifdef __STDC__
    408 static void
    409 warn(char *fmt, ...)
    410 {
    411 	va_list ap;
    412 	char buf[200];
    413 
    414 	va_start(ap, fmt);
    415 #else
    416 /*VARARGS1*/
    417 static void
    418 warn(fmt, va_alist)
    419 char *fmt;
    420 va_dcl
    421 {
    422 	va_list ap;
    423 	char buf[200];
    424 
    425 	va_start(ap);
    426 #endif
    427 	vsprintf(buf, fmt, ap);
    428 	va_end(ap);
    429 
    430 	log(LOG_WARNING,0,"%s: %s near line %d", configfilename, buf, lineno);
    431 }
    432 
    433 static void
    434 yyerror(s)
    435 char *s;
    436 {
    437 	log(LOG_ERR, 0, "%s: %s near line %d", configfilename, s, lineno);
    438 }
    439 
    440 static char *
    441 next_word()
    442 {
    443 	static char buf[1024];
    444 	static char *p=NULL;
    445 	extern FILE *f;
    446 	char *q;
    447 
    448 	while (1) {
    449 	    if (!p || !*p) {
    450 		lineno++;
    451 		if (fgets(buf, sizeof(buf), f) == NULL)
    452 		    return NULL;
    453 		p = buf;
    454 	    }
    455 	    while (*p && (*p == ' ' || *p == '\t'))	/* skip whitespace */
    456 		p++;
    457 	    if (*p == '#') {
    458 		p = NULL;		/* skip comments */
    459 		continue;
    460 	    }
    461 	    q = p;
    462 #ifdef SNMP
    463        if (*p == '"') {
    464           p++;
    465 	       while (*p && *p != '"' && *p != '\n')
    466 		      p++;		/* find next whitespace */
    467           if (*p == '"')
    468              p++;
    469        } else
    470 #endif
    471 	    while (*p && *p != ' ' && *p != '\t' && *p != '\n')
    472 		p++;		/* find next whitespace */
    473 	    *p++ = '\0';	/* null-terminate string */
    474 
    475 	    if (!*q) {
    476 		p = NULL;
    477 		continue;	/* if 0-length string, read another line */
    478 	    }
    479 
    480 	    return q;
    481 	}
    482 }
    483 
    484 static int
    485 yylex()
    486 {
    487 	int n;
    488 	u_int32_t addr;
    489 	char *q;
    490 
    491 	if ((q = next_word()) == NULL) {
    492 		return 0;
    493 	}
    494 
    495 	if (!strcmp(q,"cache_lifetime"))
    496 		return CACHE_LIFETIME;
    497 	if (!strcmp(q,"pruning"))
    498 		return PRUNING;
    499 	if (!strcmp(q,"phyint"))
    500 		return PHYINT;
    501 	if (!strcmp(q,"tunnel"))
    502 		return TUNNEL;
    503 	if (!strcmp(q,"disable"))
    504 		return DISABLE;
    505 	if (!strcmp(q,"metric"))
    506 		return METRIC;
    507 	if (!strcmp(q,"threshold"))
    508 		return THRESHOLD;
    509 	if (!strcmp(q,"rate_limit"))
    510 		return RATE_LIMIT;
    511 	if (!strcmp(q,"srcrt") || !strcmp(q,"sourceroute"))
    512 		return SRCRT;
    513 	if (!strcmp(q,"boundary"))
    514 		return BOUNDARY;
    515 	if (!strcmp(q,"netmask"))
    516 		return NETMASK;
    517 	if (!strcmp(q,"igmpv1"))
    518 		return IGMPV1;
    519 	if (!strcmp(q,"altnet"))
    520 		return ALTNET;
    521 	if (!strcmp(q,"name"))
    522 		return NAME;
    523 	if (!strcmp(q,"on") || !strcmp(q,"yes")) {
    524 		yylval.num = 1;
    525 		return BOOLEAN;
    526 	}
    527 	if (!strcmp(q,"off") || !strcmp(q,"no")) {
    528 		yylval.num = 0;
    529 		return BOOLEAN;
    530 	}
    531 	if (sscanf(q,"%[.0-9]/%d%c",s1,&n,s2) == 2) {
    532 		if ((addr = inet_parse(s1)) != 0xffffffff) {
    533 			yylval.addrmask.mask = n;
    534 			yylval.addrmask.addr = addr;
    535 			return ADDRMASK;
    536 		}
    537 		/* fall through to returning STRING */
    538 	}
    539 	if (sscanf(q,"%[.0-9]%c",s1,s2) == 1) {
    540 		if ((addr = inet_parse(s1)) != 0xffffffff &&
    541 		    inet_valid_host(addr)) {
    542 			yylval.addr = addr;
    543 			return ADDR;
    544 		}
    545 	}
    546 	if (sscanf(q,"0x%8x%c",&n,s1) == 1) {
    547 		yylval.addr = n;
    548 		return ADDR;
    549 	}
    550 	if (sscanf(q,"%d%c",&n,s1) == 1) {
    551 		yylval.num = n;
    552 		return NUMBER;
    553 	}
    554 #ifdef SNMP
    555 	if (!strcmp(q,"sysName"))
    556 		return SYSNAM;
    557 	if (!strcmp(q,"sysContact"))
    558 		return SYSCONTACT;
    559 	if (!strcmp(q,"sysVersion"))
    560 		return SYSVERSION;
    561 	if (!strcmp(q,"sysLocation"))
    562 		return SYSLOCATION;
    563    if (*q=='"') {
    564       if (q[ strlen(q)-1 ]=='"')
    565          q[ strlen(q)-1 ]='\0'; /* trash trailing quote */
    566       yylval.ptr = q+1;
    567       return STRING;
    568    }
    569 #endif
    570 	yylval.ptr = q;
    571 	return STRING;
    572 }
    573 
    574 void
    575 config_vifs_from_file()
    576 {
    577 	extern FILE *f;
    578 
    579 	order = 0;
    580 	numbounds = 0;
    581 	lineno = 0;
    582 
    583 	if ((f = fopen(configfilename, "r")) == NULL) {
    584 	    if (errno != ENOENT)
    585 		log(LOG_ERR, errno, "can't open %s", configfilename);
    586 	    return;
    587 	}
    588 
    589 	ifc.ifc_buf = (char *)ifbuf;
    590 	ifc.ifc_len = sizeof(ifbuf);
    591 	if (ioctl(udp_socket, SIOCGIFCONF, (char *)&ifc) < 0)
    592 	    log(LOG_ERR, errno, "ioctl SIOCGIFCONF");
    593 
    594 	yyparse();
    595 
    596 	fclose(f);
    597 }
    598 
    599 static u_int32_t
    600 valid_if(s)
    601 char *s;
    602 {
    603 	register vifi_t vifi;
    604 	register struct uvif *v;
    605 
    606 	for (vifi=0, v=uvifs; vifi<numvifs; vifi++, v++)
    607 	    if (!strcmp(v->uv_name, s))
    608 		return v->uv_lcl_addr;
    609 
    610 	return 0;
    611 }
    612 
    613 static struct ifreq *
    614 ifconfaddr(ifcp, a)
    615     struct ifconf *ifcp;
    616     u_int32_t a;
    617 {
    618     int n;
    619     struct ifreq *ifrp = (struct ifreq *)ifcp->ifc_buf;
    620     struct ifreq *ifend = (struct ifreq *)((char *)ifrp + ifcp->ifc_len);
    621 
    622     while (ifrp < ifend) {
    623 	    if (ifrp->ifr_addr.sa_family == AF_INET &&
    624 		((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr == a)
    625 		    return (ifrp);
    626 #if (defined(BSD) && (BSD >= 199006))
    627 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
    628 		if (n < sizeof(*ifrp))
    629 			++ifrp;
    630 		else
    631 			ifrp = (struct ifreq *)((char *)ifrp + n);
    632 #else
    633 		++ifrp;
    634 #endif
    635     }
    636     return (0);
    637 }
    638