From 0f5458e4a9c18595f7d9501880011369e4e65942 Mon Sep 17 00:00:00 2001 From: paulr Date: Thu, 22 Sep 2011 14:39:55 +1000 Subject: [PATCH] running configuration and deltas and shit --- bin/cbfwrd.php | 41 +++++++-- doco/configs.txt | 6 + libcbfwr/comms.php | 14 +++- libcbfwr/fwui.php | 199 ++++++++++++++++++++++++++++++++++++++- libcbfwr/web.php | 13 ++- unittests/readconfigfromshm.php | 9 +- var/fw.conf | 10 ++ 7 files changed, 271 insertions(+), 21 deletions(-) create mode 100644 doco/configs.txt diff --git a/bin/cbfwrd.php b/bin/cbfwrd.php index 7271d4a..b8360e5 100644 --- a/bin/cbfwrd.php +++ b/bin/cbfwrd.php @@ -21,6 +21,7 @@ error_log("CBFWRD starting"); $config = new Config(); +global $config; if(isset($argv[1])) { if($argv[1] == "boot") { @@ -35,13 +36,13 @@ $cont = true; // setup our sem/shm stuff -// this stuff needs to go in comms +// do the initial stuff +$comms = new Comms; +$config->loadConfig(); +$rawconf = $config->getConfig(); +$comms->putConfig($rawconf); + while($cont) { - $comms = new Comms; - - $config->loadConfig(); - $comms->putConfig($config->getConfig()); - $msg = $comms->waitForMessage(); //echo "Got message: $msg\n"; @@ -52,11 +53,37 @@ while($cont) { function cbfwd_commandProcessor($command) { + global $config; $cmds = explode(" ", $command); + $comms = new Comms; + $comms->lockConfigs(); switch($cmds[0]) { - case "...": + case "goodone": + break; + + // interface change requests + case "interface": + $int = $cmds[1]; + if($cmds[2] == "changename") { + $rawconf = $comms->getConfig(); + if(isset($rawconf["interface"]["$int"]["name"])) $rawconf["interface"]["$int"]["name"] = $cmds[3]; + $comms->putConfig($rawconf); + } break; + + case "zone": + $cmd = $cmds[1]; + switch($cmd) { + case "add": + $zonename = $cmds[2]; + $rawconf = $comms->getConfig(); + $rawconf["zone"][$zonename]["name"] = 1; + $comms->putConfig($rawconf); + break; + + } } + $comms->unlockConfigs(); } ?> \ No newline at end of file diff --git a/doco/configs.txt b/doco/configs.txt new file mode 100644 index 0000000..7184e3b --- /dev/null +++ b/doco/configs.txt @@ -0,0 +1,6 @@ +there are 3 areas in shared memory reserved for configuration. + +0 - fs configuration +1 - boot hardware config +2 - running config (deltas from 0) +3 - web config (contains only deltas from 0). \ No newline at end of file diff --git a/libcbfwr/comms.php b/libcbfwr/comms.php index 610959e..a11f9b3 100644 --- a/libcbfwr/comms.php +++ b/libcbfwr/comms.php @@ -26,9 +26,21 @@ class Comms { return $config; } + function lockConfigs() + { + sem_acquire($this->semres); + } + + function unlockConfigs() + { + sem_release($this->semres); + } + - // type = 0 for the main config + // type = 0 for the fs config // type = 1 for the boot hardware config + // type = 2 for running config + // type = 3 for web config function putConfig($config, $type=0) { global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY; diff --git a/libcbfwr/fwui.php b/libcbfwr/fwui.php index cf8056b..decadf2 100644 --- a/libcbfwr/fwui.php +++ b/libcbfwr/fwui.php @@ -1,17 +1,210 @@ getConfig(); + if(isset($urls[1])) { + switch($urls[1]) { + case "edit": + break; + case "change": + break; + case "add": + $nametoadd = $_REQUEST["toadd"]; + $comms->sendMessage("zone add $nametoadd"); + //if($_REQUEST["desc"] != "") $comms->sendMessage("zone ") + header("Location: $BASE_URL/zones"); + break; + default: + CBFWpageBuilder(null, "CBFWZonesPageDisplay"); + } + } else { + CBFWpageBuilder(null, "CBFWZonesPageDisplay"); + } } -function CBFWInterfacesPage() +function CBFWZonesPageDisplay() { + global $BASE_URL, $MENU_ITEMS; + $comms = new Comms(); $config = $comms->getConfig(); + + echo "

Zones

"; + echo ""; + foreach($config["zone"] as $key => $var) { + $name = $key; + $url = "Edit"; + echo ""; + } + + echo "
ZoneInterfacesEdit
$name...$url

"; + echo "
"; + echo "

Add Zone


"; + echo "Name:
"; + echo "Description:
"; + echo ""; + echo "
"; + +} + +function CBFWInterfacesPage($urls) +{ + if(isset($urls[1])) { + switch($urls[1]) { + case "edit": + CBFWpageBuilder(null, "CBFWInterfacesPageEdit", null, null, $urls[2]); + break; + case "change": + CBFWInterfacesChange(); + break; + default: + CBFWpageBuilder(null, "CBFWInterfacesPageDisplay"); + } + } else { + error_log("main interface page"); + CBFWpageBuilder(null, "CBFWInterfacesPageDisplay"); + } +} + +function CBFWInterfacesChange() +{ + global $BASE_URL, $MENU_ITEMS; + $comms = new Comms(); + + $comms->sendMessage("interface eth0 changename poof"); + header("Location: $BASE_URL/interfaces"); +} + +function CBFWInterfacesPageEdit($urls) +{ + global $BASE_URL, $MENU_ITEMS; + $interface = $urls; + + echo "

Edit Interface $interface


"; + echo "
"; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo "
Name
HW Address
Zone
IPv4 Address
IPv6 Address
Speed
Duplex
"; + echo "
"; + //echo "Back"; + echo "
"; +} + +function CBFWInterfacesPageDisplay() +{ + global $BASE_URL, $MENU_ITEMS; + $comms = new Comms(); + + $config = $comms->getConfig(0); + $boot_config = $comms->getConfig(1); + + // now build an interface table + $table = array(); + foreach($boot_config["hardware"]["netdev"] as $key=>$val) { + $ename = $key; + $table[$key]["realname"] = $val["name"]; + $table[$key]["name"] = $val["name"]; + + + // driver + if(isset($val["driver"])) $table[$key]["type"] = $val["driver"]; + else $table[$key]["type"] = "Unknown"; + + // hw address + if(isset($val["hwaddress"])) $table[$key]["hwaddress"] = $val["hwaddress"]; + } + + foreach($config["hardware"]["netdev"] as $key=>$val) { + $ename = $key; + if(isset($config["interface"]["$key"]["name"])) $table[$key]["name"] = $config["interface"]["$key"]["name"]; + $table[$key]["address4"] = null; + $table[$key]["address6"] = null; + + if(isset($config["interface"][$table[$key]["name"]]["address4"])) { + $table[$key]["address4"] = $config["interface"][$table[$key]["name"]]["address4"]; + } + + if(isset($config["interface"][$table[$key]["name"]]["address6"])) { + $table[$key]["address6"] = $config["interface"][$table[$key]["name"]]["address6"]; + } + + if(isset($config["interface"][$table[$key]["name"]]["mtu"])) { + $table[$key]["mtu"] = $config["interface"][$table[$key]["name"]]["mtu"]; + } else { + $table[$key]["mtu"] = "1500"; // TODO: this needs to actually come from somewhere + } + + if(isset($config["interface"][$table[$key]["name"]]["speed"])) { + $table[$key]["speed"] = $config["interface"][$table[$key]["name"]]["speed"]; + } else { + $table[$key]["speed"] = null; + } + + if(isset($config["interface"][$table[$key]["name"]]["duplex"])) { + $table[$key]["duplex"] = $config["interface"][$table[$key]["name"]]["duplex"]; + } else { + $table[$key]["duplex"] = null; + } + + if(isset($config["interface"][$table[$key]["name"]]["zone"])) { + $table[$key]["zone"] = $config["interface"][$table[$key]["name"]]["zone"]; + } else { + $table[$key]["zone"] = null; + } + } + + + echo ""; + foreach($table as $key =>$val) { + if($val["realname"] == $val["name"]) $name = $val["name"]; + else $name = $val["name"]." (".$val["realname"].")"; + + // zone + if($val["zone"] == null) $zone = "-"; + else $zone = $val["zone"]; + + // mtu + $mtu = $val["mtu"]; + + // driver type + $type = $val["type"]; + + // hardware address + $mac = $val["hwaddress"]; + + // speed and duplex + if($val["duplex"] == null) $dup = "default"; + else $dup = $val["duplex"]; + + if($val["speed"] == null) $spd = "default"; + else $spd = $val["speed"]; + + $spanddu = "$spd/$dup"; + + // network address + if($val["address4"]!=null && $val["address6"]!=null) { + $address = $val["address4"]."
".$val["address6"]; + } else if($val["address4"] != null) { + $address = $val["address4"]; + } else if($val["address6"] != null) { + $address = $val["address6"]; + } else { + $address = "-"; + } + + echo ""; + } + echo "
DeviceZoneTypeMAC AddressAddressMTUSpeed/DuplexControl
$name$zone$type$mac$address$mtu$spandduEdit
"; } function CBFWObjectsPage() diff --git a/libcbfwr/web.php b/libcbfwr/web.php index 6bfcad6..66e7cdf 100644 --- a/libcbfwr/web.php +++ b/libcbfwr/web.php @@ -41,12 +41,13 @@ class CBFWWeb { // ok, now we try and find the basics $url_s = explode("/", $url); + $bodyFunction = null; switch($url_s[0]) { case "zones": - $bodyFunction = "CBFWZonesPage"; + CBFWZonesPage($url_s); break; case "interfaces": - $bodyFunction = "CBFWInterfacesPage"; + CBFWInterfacesPage($url_s); break; case "objects": $bodyFunction = "CBFWObjectsPage"; @@ -63,7 +64,7 @@ class CBFWWeb { } else { $bodyFunction = "CBFBuildHomePage"; } - CBFWpageBuilder(null, $bodyFunction); + if($bodyFunction != null) CBFWpageBuilder(null, $bodyFunction); } } @@ -123,7 +124,7 @@ function CBFWMessageBuilder() } -function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="CBFW") +function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="CBFW", $bodyfunctiondata=null) { global $WEB_ROOT_FS, $BASE_URL; @@ -182,9 +183,9 @@ function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="C } if($bodyClass != null) { - $bodyClass->$bodyFunction($url); + $bodyClass->$bodyFunction($bodyfunctiondata); } else if( $bodyFunction != null) { - $bodyFunction($url); + $bodyFunction($bodyfunctiondata); } else echo $bodycontent; echo ""; diff --git a/unittests/readconfigfromshm.php b/unittests/readconfigfromshm.php index 1c1247c..ce5a3fe 100644 --- a/unittests/readconfigfromshm.php +++ b/unittests/readconfigfromshm.php @@ -19,10 +19,11 @@ glcas_pluginLoader(); $comms = new Comms(); -$conf = $comms->getConfig(); +while(true) { + sleep(5); + $conf = $comms->getConfig(); -print_r($conf); - -$comms->sendMessage("hi there"); + print_r($conf); +} ?> \ No newline at end of file diff --git a/var/fw.conf b/var/fw.conf index dbaa35e..732c25a 100644 --- a/var/fw.conf +++ b/var/fw.conf @@ -2,3 +2,13 @@ hostname hostname domainname domain.name +zone name internet + +interface dev eth0 name twatter # twatter is our outbound interface +interface dev twatter address4 10.172.192.10/24 +interface dev twatter address6 2003:12:12:12::1/64 +interface dev twatter speed 1000 +interface dev twatter duplex full +interface dev twatter zone internet + +interface dev twatter mtu 1400 \ No newline at end of file -- 1.7.0.4