Browse code

core: config parser listen fix

- do not crash when the listen=host line contains an invalid host (e.g. foo.1)
- more null checks

Reported-by: Cristian Constantin cristian.constantin at iptel org

Andrei Pelinescu-Onciul authored on 17/07/2009 13:37:01
Showing 1 changed files
... ...
@@ -556,18 +556,20 @@ statement:
556 556
 	;
557 557
 listen_id:
558 558
 	ip {
559
-		tmp=ip_addr2a($1);
560
-		if (tmp==0) {
561
-			LOG(L_CRIT, "ERROR: cfg. parser: bad ip "
562
-					"address.\n");
563
-			$$=0;
564
-		} else {
565
-			$$=pkg_malloc(strlen(tmp)+1);
566
-			if ($$==0) {
567
-				LOG(L_CRIT, "ERROR: cfg. parser: out of "
568
-						"memory.\n");
559
+		if ($1){
560
+			tmp=ip_addr2a($1);
561
+			if (tmp==0) {
562
+				LOG(L_CRIT, "ERROR: cfg. parser: bad ip "
563
+						"address.\n");
564
+				$$=0;
569 565
 			} else {
570
-				strncpy($$, tmp, strlen(tmp)+1);
566
+				$$=pkg_malloc(strlen(tmp)+1);
567
+				if ($$==0) {
568
+					LOG(L_CRIT, "ERROR: cfg. parser: out of "
569
+							"memory.\n");
570
+				} else {
571
+					strncpy($$, tmp, strlen(tmp)+1);
572
+				}
571 573
 			}
572 574
 		}
573 575
 	}
... ...
@@ -581,12 +583,14 @@ listen_id:
581 581
 		}
582 582
 	}
583 583
 	| host {
584
-		$$=pkg_malloc(strlen($1)+1);
585
-		if ($$==0) {
586
-				LOG(L_CRIT, "ERROR: cfg. parser: out of "
587
-						"memory.\n");
588
-		} else {
589
-				strncpy($$, $1, strlen($1)+1);
584
+		if ($1){
585
+			$$=pkg_malloc(strlen($1)+1);
586
+			if ($$==0) {
587
+					LOG(L_CRIT, "ERROR: cfg. parser: out of "
588
+							"memory.\n");
589
+			} else {
590
+					strncpy($$, $1, strlen($1)+1);
591
+			}
590 592
 		}
591 593
 	}
592 594
 	;
... ...
@@ -633,7 +637,7 @@ listen_phostport:
633 633
 
634 634
 id_lst:
635 635
 	listen_phostport		{  $$=$1 ; }
636
-	| listen_phostport id_lst	{ $$=$1; $$->next=$2; }
636
+	| listen_phostport id_lst	{ $$=$1;  if ($$) $$->next=$2; }
637 637
 	;
638 638
 
639 639
 flags_decl:		FLAGS_DECL	flag_list
... ...
@@ -1300,7 +1304,8 @@ assign_stm:
1300 1300
 		}
1301 1301
 		free_socket_id_lst($3);
1302 1302
 	}
1303
-	| LISTEN EQUAL  error { yyerror("ip address or hostname expected"); }
1303
+	| LISTEN EQUAL  error { yyerror("ip address, interface name or"
1304
+									" hostname expected"); }
1304 1305
 	| ALIAS EQUAL  id_lst {
1305 1306
 		for(lst_tmp=$3; lst_tmp; lst_tmp=lst_tmp->next){
1306 1307
 			add_alias(	lst_tmp->addr_lst->name,
... ...
@@ -1314,8 +1319,10 @@ assign_stm:
1314 1314
 	}
1315 1315
 	| ALIAS  EQUAL error  { yyerror(" hostname expected"); }
1316 1316
 	| ADVERTISED_ADDRESS EQUAL listen_id {
1317
-		default_global_address.s=$3;
1318
-		default_global_address.len=strlen($3);
1317
+		if ($3){
1318
+			default_global_address.s=$3;
1319
+			default_global_address.len=strlen($3);
1320
+		}
1319 1321
 	}
1320 1322
 	| ADVERTISED_ADDRESS EQUAL error {yyerror("ip address or hostname expected"); }
1321 1323
 	| ADVERTISED_PORT EQUAL NUMBER {
... ...
@@ -1900,16 +1907,19 @@ host_sep:
1900 1900
 host:
1901 1901
 	ID { $$=$1; }
1902 1902
 	| host host_sep ID {
1903
-		$$=(char*)pkg_malloc(strlen($1)+1+strlen($3)+1);
1904
-		if ($$==0) {
1905
-			LOG(L_CRIT, "ERROR: cfg. parser: memory allocation failure while parsing host\n");
1906
-		} else {
1907
-			memcpy($$, $1, strlen($1));
1908
-			$$[strlen($1)]=*$2;
1909
-			memcpy($$+strlen($1)+1, $3, strlen($3));
1910
-			$$[strlen($1)+1+strlen($3)]=0;
1903
+		if ($1){
1904
+			$$=(char*)pkg_malloc(strlen($1)+1+strlen($3)+1);
1905
+			if ($$==0) {
1906
+				LOG(L_CRIT, "ERROR: cfg. parser: memory allocation"
1907
+							" failure while parsing host\n");
1908
+			} else {
1909
+				memcpy($$, $1, strlen($1));
1910
+				$$[strlen($1)]=*$2;
1911
+				memcpy($$+strlen($1)+1, $3, strlen($3));
1912
+				$$[strlen($1)+1+strlen($3)]=0;
1913
+			}
1914
+			pkg_free($1);
1911 1915
 		}
1912
-		pkg_free($1);
1913 1916
 		pkg_free($3);
1914 1917
 	}
1915 1918
 	| host DOT error { $$=0; pkg_free($1); yyerror("invalid hostname"); }
... ...
@@ -2476,7 +2486,7 @@ cmd:
2476 2476
 			LOG(L_CRIT, "ERROR: cfg. parser: out of memory.\n");
2477 2477
 		} else {
2478 2478
 			str_tmp->s=$3;
2479
-			str_tmp->len=strlen($3);
2479
+			str_tmp->len=$3?strlen($3):0;
2480 2480
 			$$=mk_action(SET_ADV_ADDR_T, 1, STR_ST, str_tmp);
2481 2481
 		}
2482 2482
 	}
... ...
@@ -2569,6 +2579,7 @@ static void yyerror(char* s)
2569 2569
 static struct name_lst* mk_name_lst(char* host, int flags)
2570 2570
 {
2571 2571
 	struct name_lst* l;
2572
+	if (host==0) return 0;
2572 2573
 	l=pkg_malloc(sizeof(struct name_lst));
2573 2574
 	if (l==0) {
2574 2575
 		LOG(L_CRIT,"ERROR: cfg. parser: out of memory.\n");
... ...
@@ -2584,6 +2595,7 @@ static struct name_lst* mk_name_lst(char* host, int flags)
2584 2584
 static struct socket_id* mk_listen_id(char* host, int proto, int port)
2585 2585
 {
2586 2586
 	struct socket_id* l;
2587
+	if (host==0) return 0;
2587 2588
 	l=pkg_malloc(sizeof(struct socket_id));
2588 2589
 	if (l==0) {
2589 2590
 		LOG(L_CRIT,"ERROR: cfg. parser: out of memory.\n");
... ...
@@ -2618,6 +2630,7 @@ static struct socket_id* mk_listen_id2(struct name_lst* addr_l, int proto,
2618 2618
 										int port)
2619 2619
 {
2620 2620
 	struct socket_id* l;
2621
+	if (addr_l==0) return 0;
2621 2622
 	l=pkg_malloc(sizeof(struct socket_id));
2622 2623
 	if (l==0) {
2623 2624
 		LOG(L_CRIT,"ERROR: cfg. parser: out of memory.\n");