From 779ccbdea52a495b9150c12c6d69af2aa370d8f1 Mon Sep 17 00:00:00 2001 From: paulr Date: Sun, 12 Jun 2011 05:11:14 +1000 Subject: [PATCH] lots of changed, though it'll probably be all backed-out because I want to go to a simplier version that wont support unix --- lib/db.php | 28 +++++++- lib/genericssh.plugin.php | 6 ++ lib/lib.php | 3 - lib/screenos.plugin.php | 113 ++++++++++++++++++++++++++++- lib/www.php | 176 +++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 313 insertions(+), 13 deletions(-) diff --git a/lib/db.php b/lib/db.php index 58f729d..dd5b1e3 100644 --- a/lib/db.php +++ b/lib/db.php @@ -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; diff --git a/lib/genericssh.plugin.php b/lib/genericssh.plugin.php index 1842d6d..428d34d 100644 --- a/lib/genericssh.plugin.php +++ b/lib/genericssh.plugin.php @@ -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); diff --git a/lib/lib.php b/lib/lib.php index 49482a9..5e87f3d 100644 --- a/lib/lib.php +++ b/lib/lib.php @@ -28,8 +28,5 @@ if(is_dir("$basedir")) { } } } -} else { - echo "No plugins dir ($basedir/plugins), continuing without\n"; } - ?> \ No newline at end of file diff --git a/lib/screenos.plugin.php b/lib/screenos.plugin.php index 7b1f2db..30da607 100644 --- a/lib/screenos.plugin.php +++ b/lib/screenos.plugin.php @@ -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
Name
Password
Method @@ -14,14 +19,116 @@ Method
+Password
+
+ +"; + return 0; + } + + //error_log("getting config with ".$hconf["username"]." and ".$conf["password"]); + echo "Got config, saving it to db
"; + $lastupdate = time(); + db_updateData("hosts", "lastupdate", "$lastupdate", "hostip", "$hip"); + db_insertData("configs", "$hip", "$lastupdate", base64_encode($config)); + + echo "Config is:
";
+	echo $config;
+	echo "
"; +} + +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 diff --git a/lib/www.php b/lib/www.php index 794279b..3b7c506 100644 --- a/lib/www.php +++ b/lib/www.php @@ -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
"; + } else { + echo ""; + echo ""; + 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 ""; + + } + echo "
NameAddressLast UpdateScheduleVersionsControls
$name$addr$lupdate$sched$vd"; + echo "Delete Edit Update"; + echo "
"; + } + +} + +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:
"; + echo "
";
+	echo $rdata;
+	echo "
"; +} + +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:
"; + echo "
";
+	echo $rdata;
+	echo "
"; +} + +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)
"; + echo "Yes"; + echo "
";
+		print_r($data);
+		echo "
"; + } +} + +function www_updateSchedule() +{ +?> + + + + + + + + + +
-
Name
Hostname/IP Address
Host Type
+Update Schedule
"; + + echo "
"; $func(); echo ""; } 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(); -- 1.7.0.4