working on the interfaces page
[CBFWR.git] / libcbfwr / fwui.php
index a94b4ad..adc9541 100644 (file)
 <?php
 
-function CBFWZonesPage()
+function CBFWZonesPage($urls)
 {
-       return "hi";
+       global $BASE_URL, $MENU_ITEMS;
+       $comms = new Comms();
+       
+       if(isset($urls[1])) {
+               switch($urls[1]) {
+                       case "modify":
+                               
+                               if($_REQUEST["description"] != "") $desc = $_REQUEST["description"];
+                               else $desc = null;
+                               
+                               $oldname = $_REQUEST["oldname"];
+                               $newname = $_REQUEST["zonename"];
+                               
+                               $renewname = null;
+                               error_log("comparing \"$oldname\" to \"$newname\"");
+                               if($oldname != $newname) {
+                                       $renewname = " rename $newname";
+                               }
+                               
+                               $oldconf = getWebUserConfig();
+                               $olddesc = $oldconf["zone"][$oldname]["description"];
+                               error_log("Comparing \"$olddesc\" and \"$desc\"");
+                               if($desc != $olddesc) {
+                                       $newdesc = " # $desc";
+                               } else $newdesc = null;
+                               
+                               //echo "confs: \"$olddesc\", \"$newdesc\", $oldname, $newname<br>";
+                               //exit(0);
+                               
+                               if($renewname == null && $newdesc == null) {
+                                       header("Location: $BASE_URL/zones");
+                                       error_log("no changes?");
+                                       return;
+                               }
+                               
+                               
+                               $conf = $comms->getConfig(3);
+                               $changes = $conf["status"];
+                               if($changes == "nochange") $conf["status"] = 1;
+                               else $conf["status"] = $changes + 1;
+                               $cnum = $conf["status"];
+                               
+                               $conf[$cnum]["modify"] = "zone $oldname$renewname$newdesc";
+                               $comms->putConfig($conf, 3);
+                               header("Location: $BASE_URL/zones");
+                               
+                               break;
+                               
+                               
+                       case "edit":
+                               CBFWpageBuilder(null, "CBFWZonesEditPage", null, null, $urls);
+                               break;
+                               
+                               
+                       case "delete":
+                               $zonetodelete = $urls[2];
+
+                               addWebUserChange("delete zone name $zonetodelete");
+                               header("Location: $BASE_URL/zones");
+                               break;
+                               
+                               
+                       case "add":
+                               
+                               // UGLY
+                               $nametoadd = $_REQUEST["toadd"];
+                               $desc = $_REQUEST["desc"];
+                               
+                               $descbit = "";
+                               if($desc != "") $descbit = " # $desc";
+                               if($nametoadd == "" || $nametoadd == null) {
+                                       return;
+                               }
+                               
+                               addWebUserChange("zone name $nametoadd $descbit");
+                               header("Location: $BASE_URL/zones");
+                               break;
+                               
+                               
+                       default:
+                               CBFWpageBuilder(null, "CBFWZonesPageDisplay");
+               }
+       } else {
+               CBFWpageBuilder(null, "CBFWZonesPageDisplay");
+       }
+}
+
+function CBFWZonesEditPage($urls)
+{
+       global $BASE_URL, $MENU_ITEMS;
+       
+       $zone = $urls[2];
+       
+       $config = getWebUserConfig();
+       
+       
+       echo "<h2>Edit Zone $zone</h2>";
+       echo "<form method=\"post\" action=\"$BASE_URL/zones/modify/$zone\">";
+       echo "<input type=\"hidden\" name=\"oldname\" value=\"$zone\">";
+       echo "Name: <input type=\"text\" name=\"zonename\" value=\"$zone\"><br>";
+       
+       if(isset($config["zone"][$zone]["description"])) $desc = $config["zone"][$zone]["description"];
+       
+       echo "Description: <input type=\"text\" name=\"description\" value=\"$desc\"><br>";
+       echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
+       echo "</form>";
+}
+
+function addWebUserChange($change)
+{
+       $comms = new Comms();
+       $myconf = new Config();
+       
+       $conf = $comms->getConfig(3);
+       
+       if(!isset($conf["status"])) {
+               $conf["status"] = "nochange";
+       }
+       
+       $changes = $conf["status"];
+       if($changes == "nochange") $conf["status"] = 1;
+       else $conf["status"] = $changes + 1;
+       $cnum = $conf["status"];
+       
+       $conf["changes"][$cnum] = "$change";
+       $comms->putConfig($conf, 3);
+       
 }
 
-function CBFWInterfacesPage()
+// TODO: we need to really sit and think about this one
+function getWebUserConfig()
 {
        $comms = new Comms();
+       $myconf = new Config();
+       
+       
+       // check to see if pre-cache config matches change level of config change buffer
+       $config = $comms->getConfig(5);
+       $conf2 = $comms->getConfig(3);
+       if(isset($config["status"])) {
+               if($config["status"] == $conf2["status"]) {
+                       // it did, return the pre-cache
+                       error_log("got pre-cache config");
+                       return $config;
+               }
+       }
        
+       // it did not, do a new merge and cache
+       // TODO: need to do this part
        $config = $comms->getConfig();
+       $reconfig = $config;
+       $myconf->setConfig($config);
+       if(isset($conf2["changes"])) foreach($conf2["changes"] as $key => $var) {
+               error_log("Adding config line $key, $var");
+               $myconf->addConfigLine($var);
+       }
+       $reconfig = $myconf->getConfig();
+       $reconfig["status"] = $conf2["status"];
+       error_log("put pre-cache config");
+       $comms->putConfig($reconfig, 5);        
+       
+       return $reconfig;
+}
+
+function CBFWZonesPageDisplay()
+{
+       global $BASE_URL, $MENU_ITEMS;
+       
+       $config = getWebUserConfig();
+
+       echo "<h2>Zones</h2>";
+       echo "<table border=\"1\"><tr><th>Zone</th><th>Interfaces</th><th>Description</th><th>Edit</th></tr>";
+       foreach($config["zone"] as $key => $var) {
+               $name = $key;
+               $url = "<a href=\"$BASE_URL/zones/edit/$name\">Edit</a>";
+               $url2 = "<a href=\"$BASE_URL/zones/delete/$name\">Delete</a>";
+               $desc = $var["description"];
+               echo "<tr><td>$name</td><td>...</td><td>$desc</td><td>$url $url2</td></tr>";
+       }
+               
+       echo "</table><br>";
+       echo "<form method=\"post\" action=\"$BASE_URL/zones/add\">";
+       echo "<hr><h3>Add Zone</h3><br>";
+       echo "Name: <input type=\"text\" name=\"toadd\"><br>";
+       echo "Description: <input type=\"text\" name=\"desc\"><br>";
+       echo "<input type=\"submit\" name=\"Add\" value=\"Add\">";
+       echo "</form>";
+       
+}
+
+function CBFWRoutingPage($urls)
+{
+       if(isset($urls[1])) {
+               switch($urls[1]) {
+                       case "edit":
+                               CBFWpageBuilder(null, "CBFWRoutingPageEdit", null, null, $urls[2]);
+                               break;
+                       case "change":
+                               CBFWRoutingChange();
+                               break;
+                       default:
+                               CBFWpageBuilder(null, "CBFWRoutingPageDisplay");
+               }
+       } else {
+               error_log("main interface page");
+               CBFWpageBuilder(null, "CBFWRoutingPageDisplay");
+       }
+       
+}
+
+function CBFWRoutingPageDisplay()
+{
+       echo "<h2>Routing</h2>";
+}
+
+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();
+       
+       header("Location: $BASE_URL/interfaces");
+}
+
+function CBFWInterfacesPageEdit($urls)
+{
+       global $BASE_URL, $MENU_ITEMS;
+       $interface = $urls;
+       
+       $comms = new Comms();
+       
+       $config = getWebUserConfig();
+       $boot_config = $comms->getConfig(1);
+       
+       if(isset($config["interface"]["dev"]["$interface"]["name"])) $name = $config["interface"]["dev"]["$interface"]["name"];
+       else $name = "";
+       
+       $extra2 = "";
+       if(file_exists("/sys/class/net/$interface/address")) {
+               $extra2 = trim(file_get_contents("/sys/class/net/$interface/address"));
+       }
+       
+       $extra = "";
+       if(isset($boot_config["hardware"]["netdev"]["$interface"]["hwaddress"])) {
+               $hwaddr = $boot_config["hardware"]["netdev"]["$interface"]["hwaddress"];
+               $extra = " <i>Original address: $hwaddr</i>";
+               if($hwaddr != $extra2 && $extra2 != "") {
+                       $extra .= " <i>Current Address: $hwaddr</i>";
+               }
+       }
+       if(isset($config["interface"]["dev"]["$interface"]["hwaddress"])) $hwaddr = $config["interface"]["dev"]["$interface"]["hwaddress"];
+       
+       if(isset($config["interface"]["dev"]["$interface"]["zone"])) $zone = $config["interface"]["dev"]["$interface"]["zone"];
+       
+       if(isset($config["interface"]["dev"]["$interface"]["address4"])) $ip4 = $config["interface"]["dev"]["$interface"]["address4"];
+       
+       if(isset($config["interface"]["dev"]["$interface"]["address6"])) $ip6 = $config["interface"]["dev"]["$interface"]["address6"];
+       
+       if(isset($boot_config["hardware"]["netdev"]["$interface"]["speed"])) $speed = $boot_config["hardware"]["netdev"]["$interface"]["speed"];
+       if(isset($boot_config["hardware"]["netdev"]["$interface"]["duplex"])) $duplex = $boot_config["hardware"]["netdev"]["$interface"]["duplex"];
+       if(isset($config["interface"]["dev"]["$interface"]["speed"])) $speed = $config["interface"]["dev"]["$interface"]["speed"];
+       if(isset($config["interface"]["dev"]["$interface"]["duplex"])) $duplex = $config["interface"]["dev"]["$interface"]["duplex"];
+       
+       echo "<h3>Edit Interface $interface</h3><br>";
+       echo "<form method=\"post\" action=\"$BASE_URL/interfaces/change/$interface\">";
+       echo "<table>";
+       echo "<tr><td>Name</td><td><input type=\"text\" name=\"name\" value=\"$name\"></td></tr>";
+       echo "<tr><td>HW Address</td><td><input type=\"text\" name=\"hwaddr\" value=\"$hwaddr\">$extra</td></tr>";
+       echo "<tr><td>Zone</td><td><input type=\"text\" name=\"zone\" value=\"$zone\"></td></tr>";
+       echo "<tr><td>IPv4 Address</td><td><input type=\"text\" name=\"ipv4addr\" value=\"$ip4\"></td></tr>";
+       echo "<tr><td>IPv6 Address</td><td><input type=\"text\" name=\"ipv6addr\" value=\"$ip6\"></td></tr>";
+       echo "<tr><td>Speed</td><td><select name=\"duplex\"><option value=\"auto\">Auto</option><option value=\"10\">10</option>";
+       echo "<option value=\"100\">100</option><option value=\"1000\">1000</option><option value=\"10000\">10000</option></select></td></tr>";
+       echo "<tr><td>Duplex</td><td><select name=\"duplex\"><option value=\"auto\">Auto</option><option value=\"full\">Full</option><option value=\"half\">Half</option></select></td></tr>";
+       echo "</table>";
+       echo "<input type=\"submit\" name=\"Change\" value=\"Change\"><br>";
+       //echo "<a href=\"$BASE_URL/interfaces/\">Back</a>";
+       echo "</form>";
+}
+
+function CBFWInterfacesPageDisplay()
+{
+       global $BASE_URL, $MENU_ITEMS;
+       $comms = new Comms();
+       
+       $config = getWebUserConfig();
+       $boot_config = $comms->getConfig(1);
+       
+       // at the top, we put in the creation bits
+       echo "<h2>Create Interface</h2>";
+       echo "<li><a href=\"$BASE_URL/interfaces/create/bond\">Link Aggregation Group (bonded/teamed interfaces)</a><br>";
+       echo "<li><a href=\"$BASE_URL/interfaces/create/vlan\">VLAN Tagged Sub Interfaces</a><br>";
+       echo "<li><a href=\"$BASE_URL/interfaces/create/bridge\">Bridged Interfaces</a><br>";
+       echo "<hr>";
+       
+       // 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"];
+               
+               // current mtu
+               if(isset($val["mtu"])) $table[$key]["mtu"] = $val["mtu"];
+               
+               // speed
+               if(isset($val["speed"])) $table[$key]["speed"] = $val["speed"];
+               else $table[$key]["speed"] = "default";
+               
+               // duplex
+               if(isset($val["duplex"])) $table[$key]["duplex"] = $val["duplex"];
+               else $table[$key]["duplex"] = "default";
+       }
+       
+       foreach($config["interface"]["dev"] as $key => $val) {
+               // TODO construction zone
+               error_log("doing $key");
+               if(isset($val["name"])) $table[$key]["name"] = $val["name"];
+               if(isset($val["address4"])) $table[$key]["address4"] = $val["address4"];
+               if(isset($val["address6"])) $table[$key]["address6"] = $val["address6"];
+               if(isset($val["description"])) $table[$key]["description"] = $val["description"];
+               if(isset($val["zone"])) $table[$key]["zone"] = $val["zone"];
+       }
+
+       
+       echo "<h2>Interfaces</h2><table border=\"1\"><th>Device</th><th>Zone</th><th>Type</th><th>MAC Address</th><th>Address</th><th>MTU</th><th>Speed/Duplex</th><th>Description</th><th>Control</th></tr>";
+       foreach($table as $key =>$val) {
+               if($val["realname"] == $val["name"]) $name = $val["name"];
+               else $name = $val["name"]." (".$val["realname"].")";
+               
+               // zone
+               if(isset($val["zone"])) {
+                       if($val["zone"] == null) $zone = "-";
+                       else $zone = $val["zone"];
+               } else $zone = "-";
+               
+               // mtu
+               if(isset($val["mtu"])) {
+                       $mtu = $val["mtu"];
+               } else $mtu = "-";
+               
+               // driver type
+               if(isset($val["type"])) { 
+                       $type = $val["type"];
+               } else $type = "-";
+               
+               // hardware address
+               if(isset($val["hwaddress"])) {
+                       $mac = $val["hwaddress"];
+               } else $mac = "-";
+               
+               // speed and duplex
+               if(isset($val["duplex"])) {
+                       if($val["duplex"] == null) $dup = "default";
+                       else $dup = $val["duplex"];
+               } //else $dup = "default"; // TODO: needs to go away in fav of hardware setting
+               
+               if(isset($val["speed"])) {
+                       if($val["speed"] == null) $spd = "default";
+                       else $spd = $val["speed"];
+               } //else $spd = "default"; // TODO: needs to go away inf av of hardware setting
+               
+               $spanddu = "$spd/$dup";
+               
+               // network address
+               if(isset($val["address4"]) && isset($val["address6"])) {
+                       $address = $val["address4"]."<br>".$val["address6"];
+               } else if(isset($val["address4"])) {
+                       $address = $val["address4"];
+               } else if(isset($val["address6"])) {
+                       $address = $val["address6"];
+               } else {
+                       $address = "-";
+               }
+               
+               if(isset($val["description"])) $desc = $val["description"];
+               else $desc = "-";
+               
+               echo "<tr><th>$name</th><td>$zone</td><td>$type</td><td>$mac</td><td>$address</td><td>$mtu</td><td>$spanddu</td><td>$desc</td><td><a href=\"$BASE_URL/interfaces/edit/".$val["realname"]."\">Edit</a></td></tr>";
+       }
+       echo "</table>";
+       
+       // to test
+       echo "config: <pre>";
+       print_r($config);
+       echo "</pre>";
 }
 
-function CBFWObjectsPage()
+function CBFWObjectsPage($urls)
 {
        $comms = new Comms();
        
        $config = $comms->getConfig();
+       
+       CBFWpageBuilder(null, "CBFWObjectsDisplay");
+}
+
+function CBFWObjectsDisplay()
+{
+       global $BASE_URL, $MENU_ITEMS;
+       $config = getWebUserConfig();
+       
+       echo "<h2>Objects</h2>";
+       
+       if(isset($config["objects"])) {
+               echo "<table>";
+               echo "<tr><th>Networks</th><th>Hosts</th><th>Services</th><th>Network Groups</th><th>Host Groups</th><th>Service Groups</th></tr>";
+               echo "</table>";
+               foreach($config["objects"] as $key => $var) {
+               }
+       } else {
+               echo "No objects set\n";
+       }
+       
+       echo "<hr>";
+       echo "<table cellpadding=\"20\"><tr valign=\"top\">";
+       echo "<td>";
+       echo "<h3>Create Network</h3><br>";
+       echo "<form method=\"post\" action=\"$BASE_URL/objects/network/create\">";
+       echo "Name <input type=\"text\" name=\"objname\"><br>";
+       echo "IPv4 <input type=\"text\" name=\"addr4\">/<input type=\"text\" name=\"mask4\" maxlength=\"3\" size=\"3\"><br>";
+       echo "IPv6 <input type=\"text\" name=\"addr6\">/<input type=\"text\" name=\"mask6\" maxlength=\"3\" size=\"3\"><br>";
+       echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
+       echo "</form>";
+       echo "</td>";
+       echo "<td>";
+       echo "<h3>Create Host</h3><br>";
+       echo "<form method=\"post\" action=\"$BASE_URL/objects/host/create\">";
+       echo "Name <input type=\"text\" name=\"objname\"><br>";
+       echo "IPv4 <input type=\"text\" name=\"addr4\"><br>";
+       echo "IPv6 <input type=\"text\" name=\"addr6\"><br>";
+       echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
+       echo "</form>";
+       echo "</td>";
+       echo "<td>";
+       echo "<h3>Service</h3><br>";
+       echo "<form method=\"post\" action=\"$BASE_URL/objects/host/create\">";
+       echo "Name <input type=\"text\" name=\"objname\"><br>";
+       echo "Port <input type=\"text\" name=\"port\">";
+       echo "<select name=\"proto\"><option value=\"tcp\">TCP</option><option value=\"udp\">UDP</option></select><br>";
+       echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
+       echo "</form>";
+       echo "</td>";
+       echo "</tr><tr>";
+       echo "<td>";
+       echo "<h3>Network Group</h3>";
+       echo "<form method=\"post\" action=\"$BASE_URL/objects/networkgroup/create\">";
+       echo "Name <input type=\"text\" name=\"objname\"><br>";
+       echo "</form>";
+       echo "</td>";
+       echo "<td>";
+       echo "<h3>Host Group</h3>";
+       echo "<form method=\"post\" action=\"$BASE_URL/objects/hostgroup/create\">";
+       echo "Name <input type=\"text\" name=\"objname\"><br>";
+       echo "</form>";
+       echo "</td>";
+       echo "<td>";
+       echo "<h3>Service Group</h3>";
+       echo "<form method=\"post\" action=\"$BASE_URL/objects/servicegroup/create\">";
+       echo "Name <input type=\"text\" name=\"objname\"><br>";
+       echo "</form>";
+       echo "</td>";
+       
+       
+       echo "</tr></table>";
+       
+}
+
+function CBFWChangesPage($urls)
+{
+       global $BASE_URL, $MENU_ITEMS;
+       
+       if(isset($urls[1])) {
+               switch($urls[1]) {
+                       case "show":
+                               CBFWpageBuilder(null, "CBFWChangesDisplay");
+                               break;
+                       case "delete":
+                               $delete = $urls[2];
+                               error_log("would delete change $delete");
+                               header("Location: $BASE_URL/changes/show");
+                               break;
+               }
+       }
+}
+
+function CBFWChangesDisplay()
+{
+       global $BASE_URL, $MENU_ITEMS;
+       $comms = new Comms();
+       
+       $config = $comms->getConfig(3);
+       
+       echo "<h2>Changes</h2>";
+       echo "This page shows the changes on the web from the currently running configuration<br>";
+       if($config["status"] == "nochange") {
+               echo "There are currently no changes from the running configuraiton.";
+       } else {
+               echo "<table border=\"1\">";
+               echo "<tr><th>Change No</th><th>Change</th><th>Control</th></tr>";
+               foreach($config["changes"] as $key => $var) {
+                       echo "<tr><td>$key</td><td>$var</td><td><a href=\"$BASE_URL/changes/delete/$key\">Delete</a></td></tr>";
+               }
+               echo "</table>";
+       }
 }
 
 function CBFWRulesPage()
@@ -26,6 +542,58 @@ function CBFWRulesPage()
        $config = $comms->getConfig();
 }
 
+function CBFWAdminPage($urls)
+{
+       $comms = new Comms();
+       
+       $config = $comms->getConfig();
+       
+       CBFWpageBuilder(null, "CBFWAdminDisplay");
+}
+
+function CBFWAdminDisplay()
+{
+?>
+<h2>Administration</h2>
+<table cellpadding=10>
+<tr valign="top">
+<td>
+<h3>Users</h3>
+<table>
+<tr><th>Username</th><th>Description</th><th>Enabled?</th></tr>
+<tr><td>Admin</td><td>Twattle</td><td>Yes</td></tr>
+</table>
+<hr>
+<h3>Add User</h3>
+<form method="post" action="asdf">
+Username <input type="text" name="username"><br>
+Password <input type="password" name="pass"><br>
+Description <input type="text" name="desc"><br>
+<input type="submit" name="add" value="Add"><br>
+</form>
+</td>
+<td>
+<h3>Network Access</h3>
+<table>
+<tr><th>From</th><th>Interface</th><th>Type</th></tr>
+<tr><td>Any</td><td>twaddle</td><td>http,https,ssh</td></tr>
+</table>
+<hr>
+<h3>Add Access</h3>
+<form method="post" action="asdf">
+From <select name="from"><option value="something">Any</option><option name="some">Intranet</option><option name="som">SomeHost</option></select><br>
+Interface <select name="interface"><option value="something">twad</option><option name="some">craw</option></select>
+Protocols <input type="checkbox" name="http">HTTP <input type="checkbox" name="http">HTTPS <input type="checkbox" name="http">SSH<br> 
+<input type="submit" name="add" value="Add"><br>
+</form>
+</td>
+</tr>
+</table>
+<?php 
+}
+
+
+
 function findUrl($url_s)
 {
        global $URL_HANDLERS;