[daemons] Sanity check port number arguments before use

2008-05-29 Martin Nagy <mnagy@redhat.com>

	* */*main.c: Sanity check port numbers before using.
diff --git a/ChangeLog b/ChangeLog
index ae97cf3..2fbdd8a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2008-05-29 Martin Nagy <mnagy@redhat.com>
+
+	* */*main.c: Sanity check port numbers before using.
+
 2008-01-30 Peter Szilagyi <sp615@hszk.bme.hu>
 
 	* lib/stream.h: Remove named 'new' parameter in prototype
diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c
index e6d34af..2089c6b 100644
--- a/bgpd/bgp_main.c
+++ b/bgpd/bgp_main.c
@@ -203,6 +203,7 @@
   int dryrun = 0;
   char *progname;
   struct thread thread;
+  int tmp_port;
 
   /* Set umask before anything for security */
   umask (0027);
@@ -238,7 +239,11 @@
           pid_file = optarg;
           break;
 	case 'p':
-	  bm->port = atoi (optarg);
+	  tmp_port = atoi (optarg);
+	  if (tmp_port <= 0 || tmp_port > 0xffff)
+	    bm->port = BGP_PORT_DEFAULT;
+	  else
+	    bm->port = tmp_port;
 	  break;
 	case 'A':
 	  vty_addr = optarg;
@@ -252,7 +257,8 @@
               break;
             } 
           vty_port = atoi (optarg);
-          vty_port = (vty_port ? vty_port : BGP_VTY_PORT);
+	  if (vty_port <= 0 || vty_port > 0xffff)
+	    vty_port = BGP_VTY_PORT;
 	  break;
 	case 'r':
 	  retain_mode = 1;
diff --git a/ospf6d/ospf6_main.c b/ospf6d/ospf6_main.c
index 8380bc8..680f4b7 100644
--- a/ospf6d/ospf6_main.c
+++ b/ospf6d/ospf6_main.c
@@ -227,7 +227,8 @@
               break;
             }
           vty_port = atoi (optarg);
-          vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT);
+          if (vty_port <= 0 || vty_port > 0xffff)
+            vty_port = OSPF6_VTY_PORT;
           break;
         case 'u':
           ospf6d_privs.user = optarg;
diff --git a/ospfd/ospf_main.c b/ospfd/ospf_main.c
index 27a12dd..1a200a8 100644
--- a/ospfd/ospf_main.c
+++ b/ospfd/ospf_main.c
@@ -245,7 +245,8 @@
               break;
             } 
           vty_port = atoi (optarg);
-          vty_port = (vty_port ? vty_port : OSPF_VTY_PORT);
+          if (vty_port <= 0 || vty_port > 0xffff)
+            vty_port = OSPF_VTY_PORT;
   	  break;
 	case 'u':
 	  ospfd_privs.user = optarg;
diff --git a/ripd/rip_main.c b/ripd/rip_main.c
index dfcd6c2..0b29107 100644
--- a/ripd/rip_main.c
+++ b/ripd/rip_main.c
@@ -236,7 +236,8 @@
               break;
             } 
           vty_port = atoi (optarg);
-          vty_port = (vty_port ? vty_port : RIP_VTY_PORT);
+          if (vty_port <= 0 || vty_port > 0xffff)
+            vty_port = RIP_VTY_PORT;
 	  break;
 	case 'r':
 	  retain_mode = 1;
diff --git a/ripngd/ripng_main.c b/ripngd/ripng_main.c
index 7055391..a18ce9d 100644
--- a/ripngd/ripng_main.c
+++ b/ripngd/ripng_main.c
@@ -240,7 +240,8 @@
               break;
             } 
           vty_port = atoi (optarg);
-          vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT);
+          if (vty_port <= 0 || vty_port > 0xffff)
+            vty_port = RIPNG_VTY_PORT;
           break;
 	case 'r':
 	  retain_mode = 1;
diff --git a/zebra/main.c b/zebra/main.c
index 6019260..61750f1 100644
--- a/zebra/main.c
+++ b/zebra/main.c
@@ -275,7 +275,8 @@
 	      break;
 	    } 
 	  vty_port = atoi (optarg);
-	  vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT);
+	  if (vty_port <= 0 || vty_port > 0xffff)
+	    vty_port = ZEBRA_VTY_PORT;
 	  break;
 	case 'r':
 	  retain_mode = 1;