lots of changed, though it'll probably be all backed-out because
authorpaulr <me@pjr.cc>
Sat, 11 Jun 2011 19:11:14 +0000 (05:11 +1000)
committerpaulr <me@pjr.cc>
Sat, 11 Jun 2011 19:11:14 +0000 (05:11 +1000)
I want to go to a simplier version that wont support unix

lib/db.php
lib/genericssh.plugin.php
lib/lib.php
lib/screenos.plugin.php
lib/www.php

index 58f729d..dd5b1e3 100644 (file)
@@ -2,10 +2,11 @@
 
 function db_getDB()
 {
-       global $DB_URL;
+       global $DB_URL, $IS_WEB_REQUEST;
        
        if(!isset($IS_WEB_REQUEST)) {
                // only the web requests are allowed to create dbs
+               echo "no web request, no db\n";
                return false;
        }
        
@@ -63,17 +64,40 @@ function db_insertData($tablename)
        }
        $sql .=")";
        
+       //echo "sql is $sql\n";
+       
        $db->query($sql);
 }
 
-function db_selectData($tablename, $column="", $value="")
+function db_getMaxValue($tablename, $column, $columnsel="", $wheresel="")
 {
        $db = db_getDB();
        
        if(!$db) return false;
        
+       if($columnsel != "") {
+               $extra = " where $columnsel = '$wheresel'";
+       }
+       
+       $sql = "select max($column) from \"$tablename\"$extra";
+       $res = $db->query($sql);
+       if(!$res) return false;
+       $data = $res->fetchAll();
+       
+       return $data;
+}
+
+
+function db_selectData($tablename, $column="", $value="", $orderby = "")
+{
+       $db = db_getDB();
+       
+       if(!$db) return false;
+       
+       
        if($column != "") $extra = " where $column like '%$value%'";
        else $extra = "";
+       if($orderby != "") $extra .= " order by $orderby";
        $sql = "select * from \"$tablename\"$extra";
        $res = $db->query($sql);
        if(!$res) return false;
index 1842d6d..428d34d 100644 (file)
@@ -3,6 +3,12 @@
 function genericssh_ssh($host, $username, $password, $command)
 {
        $connector = ssh2_connect("$host");
+       error_log("attempting connect to $host with $username");
+       if($connector) {
+               error_log("connector true");
+       } else {
+               error_log("connector false");
+       }
        ssh2_auth_password($connector, "$username", "$password");
        $stream = ssh2_exec($connector, "$command");
        stream_set_blocking($stream, true);
index 49482a9..5e87f3d 100644 (file)
@@ -28,8 +28,5 @@ if(is_dir("$basedir")) {
                        }
                }
        }
-} else {
-       echo "No plugins dir ($basedir/plugins), continuing without\n";
 }
-
 ?>
\ No newline at end of file
index 7b1f2db..30da607 100644 (file)
@@ -2,10 +2,15 @@
 $HOST_TYPE["screenos"]["name"] = "Netscreen Screen OS";
 $HOST_TYPE["screenos"]["configform"] = "nsos_formFunction";
 $HOST_TYPE["screenos"]["postfunction"] = "nsos_postFunction";
+$HOST_TYPE["screenos"]["updateconfig"] = "nsos_updateConfig";
+$HOST_TYPE["screenos"]["detail"] = "nsos_detail";
+$HOST_TYPE["screenos"]["getconfig"] = "nsos_getConfigFromDB";
 
 function nsos_formFunction()
 {
        ?>
+Note: If your router/firewall/device uses a one-time password, leave these blank and manually update from the
+home page<br>
 Name <input type="text" name="username"><br>
 Password <input type="password" name="password"><br>
 Method <select name="methodtype"><option value="ssh">SSH</option><option value="telnet">Telnet</option></select>
@@ -14,14 +19,116 @@ Method <select name="methodtype"><option value="ssh">SSH</option><option value="
 
 function nsos_postFunction()
 {
-       echo "i am post function, your awesome<br>";
+       if($_REQUEST["password"] != "" && $_REQUEST["username"] != "") {
+               
+               $hip = $_REQUEST["hostip"];
+               $user = $_REQUEST["username"];
+               $pass = $_REQUEST["password"];
+               $config = nsos_getConfig($hip, $user, $pass);
+               $hconfig["username"] = $user;
+               $hconfig["password"] = $pass;
+               $lastupdate = time();
+               $upsched = $_REQUEST["upsched"];
+               db_updateData("hosts", "hostconfig", base64_encode(serialize($hconfig)), "hostip", "$hip");
+               db_updateData("hosts", "lastupdate", "$lastupdate", "hostip", "$hip");
+               db_updateData("hosts", "updateschedule", "$upsched", "hostip", "$hip");
+               db_insertData("configs", "$hip", "$lastupdate", base64_encode($config));
+               //db_createTable("configs", "hostip", "configtime", "configdata");
+               //db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
+               //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
+               
+               echo "if the below is accurate, this has worked<br>";
+               
+               echo "Attempting first config grab, i see<br>";
+               echo "<pre>";
+               echo $config;
+               echo "</pre>";
+       } else {
+               echo "No username and password set, you can update manually from the home page\n";
+               $hconfig["username"] = "";
+               $hconfig["password"] = "";
+               db_updateData("hosts", "hostconfig", base64_encode(serialize($hconfig)), "hostip", "$hip");
+       }
+       
+       
        echo "<pre>";
        print_r($_REQUEST);
        echo "</pre>";
 }
 
-function nsos_getConfig($istest)
+
+function nsos_getConfigFromDB($hip, $cid)
+{
+       $data = db_selectData("configs", "configs_id", "$cid");
+       
+       return base64_decode($data[0]["configdata"]);
+}
+
+function nsos_detail($hip)
+{
+       $data = db_selectData("configs", "hostip", "$hip", "configtime desc");
+       
+       echo "<table border=\"1\">";
+       echo "<tr><th>Config Data</th><th>Compare</th></tr>";
+       foreach($data as $dstruct) {
+               //echo "<pre>";
+               //print_r($dstruct);
+               //echo "</pre>";
+               $dold = round((time()-$dstruct["configtime"])/86400);
+                               
+               $lupdate = strftime("%T %e %b %G", $dstruct["configtime"])." ($dold days old)";
+               $cid = $dstruct["configs_id"];
+               echo "<tr><td>$lupdate <a href=\"?action=getconfig&cid=$cid&hip=$hip\">Get</a> <a href=\"?action=downloadconfig&cid=$cid&hip=$hip\">Download</a></td><td>Tick</td></tr>";
+       }
+       echo "</table>";
+       
+}
+
+function nsos_updateConfig($hip, $hostconfig)
+{
+       $hconf = unserialize(base64_decode($hostconfig));
+       if($hconf["username"] == "" && !isset($_REQUEST["username"])) {
+               echo "No username/password data<br>";
+?>
+<form method="post" action="?action=update&hip=<?php echo $hip ?>">
+Name <input type="text" name="username"><br>
+Password <input type="password" name="password"><br>
+<input type="submit" name="Go" value="Go"><br>
+</form>
+<?php
+               return 0;
+       }
+       
+       if(isset($_REQUEST["username"])) {
+               $hconf["username"] = $_REQUEST["username"];
+               $hconf["password"] = $_REQUEST["password"];
+       }
+       
+       $lastconfig_n_d = db_getMaxValue("configs", "configs_id", "hostip", $hip);
+       $lastconfig_n = $lastconfig_n_d[0][0];
+       $lastconfig_d = db_selectData("configs", "configs_id", "$lastconfig_n");
+       $lastconfig = base64_decode($lastconfig_d[0]["configdata"]);
+       
+       $config = nsos_getConfig($hip, $hconf["username"], $hconf["password"]);
+       
+       if(md5($config) == md5($lastconfig)) {
+               echo "Retrieved config is same as previous one, ignoring<br>";
+               return 0;
+       }
+       
+       //error_log("getting config with ".$hconf["username"]." and ".$conf["password"]);
+       echo "Got config, saving it to db<br>";
+       $lastupdate = time();
+       db_updateData("hosts", "lastupdate", "$lastupdate", "hostip", "$hip");
+       db_insertData("configs", "$hip", "$lastupdate", base64_encode($config));
+
+       echo "Config is:<br><pre>";
+       echo $config;
+       echo "</pre>";
+}
+
+function nsos_getConfig($host, $username, $password)
 {
-       genericssh_ssh($host, $username, $password, "cli show configuration");
+       return genericssh_ssh($host, $username, $password, "cli show configuration");
 }
 ?>
\ No newline at end of file
index 794279b..3b7c506 100644 (file)
@@ -15,7 +15,11 @@ $FUNCTIONS["config"] = "www_configure";
 $FUNCTIONS["addhost"] = "www_addhost";
 $FUNCTIONS["addhostnext"] = "www_addHostStageTwo";
 $FUNCTIONS["addhostthree"] = "www_addHostStageThree";
-
+$FUNCTIONS["delete"] = "www_deleteHost";
+$FUNCTIONS["update"] = "www_updateHost";
+$FUNCTIONS["detail"] = "www_hostDetail";
+$FUNCTIONS["getconfig"] = "www_getConfig";
+$FUNCTIONS["downloadconfig"] = "www_downloadConfig";
 
 function www_header()
 {
@@ -74,9 +78,120 @@ function www_body()
        
        $db = db_getDB();
        
-       // here we do the normal body, whatever that
+       // here we do the normal body
        // we'll list the hosts, the size of the current config and
        // the last time it was updated, plus the number of versions
+       $data = db_selectData("hosts");
+       if(!$data) {
+               echo "No hosts currently exist, add them from the right<br>"; 
+       } else {
+               echo "<table border=\"1\">";
+               echo "<tr><th>Name</th><th>Address</th><th>Last Update</th><th>Schedule</th><th>Versions</th><th>Controls</th></tr>";
+               foreach($data as $dstruct) {
+                       //print_r($dstruct);
+                       $name = $dstruct["hostname"];
+                       $addr = $dstruct["hostip"];
+                       if($dstruct["lastupdate"] != 0) {
+                               $dold = round((time()-$dstruct["lastupdate"])/86400);
+                               
+                               $lupdate = strftime("%T %e %b %G", $dstruct["lastupdate"])." ($dold days old)";
+                       } else {
+                               $lupdate = "Never";
+                       }
+                       $sched = $dstruct["updateschedule"];
+                       $vd = count(db_selectData("configs", "hostip", "$addr"));
+                       echo "<tr><td><a href=\"?action=detail&hip=$addr\">$name</a></td><td>$addr</td><td>$lupdate</td><td>$sched</td><td>$vd</td><td>";
+                       echo "<a href=\"?action=delete&hip=$addr\">Delete</a> <a href=\"?action=edit&hip=$addr\">Edit</a> <a href=\"?action=update&hip=$addr\">Update</a>";
+                       echo "</td></tr>";
+               
+               }
+               echo "</table>";
+       }
+       
+}
+
+function www_getConfig()
+{
+       global $HOST_TYPE;
+       
+       $cid = $_REQUEST["cid"];        
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hostname = $data[0]["hostname"];
+       $htype = $data[0]["hosttype"];
+       
+       $func = $HOST_TYPE["$htype"]["getconfig"];
+       
+       error_log("calling $func with $hip, and $cid");
+       $rdata = $func("$hip", "$cid");
+       
+       echo "Config:<br>";
+       echo "<pre>";
+       echo $rdata;
+       echo "</pre>";
+}
+
+function www_downloadConfig()
+{
+       global $HOST_TYPE;
+       
+       $cid = $_REQUEST["cid"];        
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hostname = $data[0]["hostname"];
+       $htype = $data[0]["hosttype"];
+       
+       $func = $HOST_TYPE["$htype"]["getconfig"];
+       
+       error_log("calling $func with $hip, and $cid");
+       $rdata = $func("$hip", "$cid");
+       
+       echo "Config:<br>";
+       echo "<pre>";
+       echo $rdata;
+       echo "</pre>";
+}
+
+function www_hostDetail()
+{
+       global $HOST_TYPE;
+       
+       //db_createTable("configs", "hostip", "configtime", "configdata");
+       //db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
+       //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hconfig = $data[0]["hostconfig"];
+       $hostname = $data[0]["hostname"];
+       $htype = $data[0]["hosttype"];
+       
+       $func = $HOST_TYPE["$htype"]["detail"];
+       
+       error_log("calling $func with $hip, and $hconfig");
+       $func("$hip");
+}
+
+function www_updateHost()
+{
+       global $HOST_TYPE;
+       
+       //db_createTable("configs", "hostip", "configtime", "configdata");
+       //db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
+       //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hconfig = $data[0]["hostconfig"];
+       $hostname = $data[0]["hostname"];
+       $htype = $data[0]["hosttype"];
+       
+       $func = $HOST_TYPE["$htype"]["updateconfig"];
+       
+       error_log("calling $func with $hip, and $hconfig");
+       $func("$hip", $hconfig);
        
 }
 
@@ -91,15 +206,54 @@ function www_hostTypesDropDown()
        }
 }
 
+function www_deleteHost()
+{
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hostname = $data[0]["hostname"];
+
+       if(isset($_REQUEST["confirmed"])) {
+               db_deleteData("hosts", "hostip", "$hip");
+               db_deleteData("configs", "hostip", "$hip");
+               echo "Done!";
+       } else {
+               echo "Are you use you wish to delete the host, $hostname with IP address $hip? (and all associated config info)<br>";
+               echo "<a href=\"?action=delete&hip=$hip&confirmed=yes\">Yes</a>";
+               echo "<pre>";
+               print_r($data);
+               echo "</pre>";
+       }
+}
+
+function www_updateSchedule()
+{
+?>
+<option value="0">Manual Only</option>
+<option value="900">Every 15 Minutes</option>
+<option value="3600">Every Hour</option>
+<option value="14400">Every 4 Hours</option>
+<option value="86400">Once a Day</option>
+<option value="604800">Once a Week</option>
+<option value="2592000">Once a Month (30 Days)</option>
+<option value="7776000">Every 3 Months (90 days)</option>
+<option value="10368000">Every 6 Months (180 Days)</option>
+<?php
+}
+
 function www_addhost()
 {
+       $db = db_getDB();
+       
+       db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
+       db_createTable("configs", "hostip", "configtime", "configdata");
        ?>
 <form method="post" action="?action=addhostnext">
 <table>
 <tr><td>Name</td><td><input type="text" name="cname"></td></tr>
 <tr><td>Hostname/IP Address</td><td><input type="text" name="hname"></td></tr>
 <tr><td>Host Type</td><td><select name="hosttype"><?php www_hostTypesDropDown() ?></select></td></tr>
-</table>
+<tr><td>Update Schedule</td><td><select name="updatesched"><?php www_updateSchedule() ?></select></td></tr></table>
 <input type="submit" value="Next" name="Next">
 </form>
        <?php
@@ -108,11 +262,23 @@ function www_addhost()
 function www_addHostStageTwo()
 {
        global $HOST_TYPE;
+       
+       $data = db_selectData("hosts", "hostip", $_REQUEST["hname"]);
+       if($data) {
+               echo "Host already exists in database\n";
+               return false;
+       } else {
+               db_insertData("hosts", $_REQUEST["cname"], $_REQUEST["hname"], $_REQUEST["hosttype"], "", "", $_REQUEST["updatesched"]);
+       }
        if(isset($_REQUEST["hosttype"])) {
                $htype = $_REQUEST["hosttype"];
                $func = $HOST_TYPE["$htype"]["configform"];
+               $hip = $_REQUEST["hname"];
+               $hname = $_REQUEST["hname"];
+               $upsched = $_REQUEST["updatesched"];
                if(function_exists($func)) {
-                       echo "<form method=\"post\" action=\"?action=addhostthree&hosttype=$htype\">";
+                       
+                       echo "<form method=\"post\" action=\"?action=addhostthree&hosttype=$htype&hostname=$hname&hostip=$hip&upsched=$upsched\">";
                        $func();
                        echo "<input type=\"submit\" value=\"Add\" name=\"Add\">";
                } else echo "would call $func for $htype but it doesnt exist\n";
@@ -123,7 +289,7 @@ function www_addHostStageThree()
 {
        global $HOST_TYPE;
        if(isset($_REQUEST["hosttype"])) {
-               $htype = $_REQUEST["htype"];
+               $htype = $_REQUEST["hosttype"];
                $func = $HOST_TYPE["$htype"]["postfunction"];
                if(function_exists($func)) {
                        $func();