hostgroup scanning works.
authorpaulr <me@pjr.cc>
Mon, 8 Aug 2011 18:44:02 +0000 (04:44 +1000)
committerpaulr <me@pjr.cc>
Mon, 8 Aug 2011 18:44:02 +0000 (04:44 +1000)
libglcas/config.php
libglcas/web.php
plugins/adhoc.php
plugins/hosts.php

index 499c97b..3f4588e 100644 (file)
@@ -55,9 +55,9 @@ class GLCASConfig {
        
        function addData($configType, $configCat, $configName, $configVal)
        {
-               $sql = "insert into datatable values (NULL, '$configType', '$configCat', $configName', '$configVal')";
+               $sql = "insert into datatable values (NULL, '$configType', '$configCat', '$configName', '$configVal')";
                $this->dbobject->query($sql);
-               
+               error_log("CONFIG: adddata as $sql");
        }
        
        function saveConfig()
@@ -70,43 +70,53 @@ class GLCASConfig {
                $ret = null;
                $nret = 0;
                $haveWhere = false;
+
+               $wheredata = "where data_type='$configType'";
                if($configCat != "") {
-                       $haveWhere = true;
-                       $wheredata = "where data_category='$configCat'";
+                       $wheredata .= " and data_category='$configCat'";
                }
                
                if($configName != "") {
-                       if($haveWhere) {
-                               $wheredata .= " and data_name='$configName'";
-                       } else {
-                               $wheredata = "where data_name='$configName'";
-                       }
+                       $wheredata .= " and data_name='$configName'";
                }
                
-               $sql = "select data_val,data_category,data_name from datatable $wheredata";
+               $sql = "select data_category,data_name,data_val from datatable $wheredata";
+               error_log("CONFIG: get via $sql");
+               
                $res = $this->dbobject->query($sql);
                
                foreach($res as $row) {
-                       $ret[$nret]["val"] = $row[0];
-                       $ret[$nret]["cat"] = $row[1];
-                       $ret[$nret]["name"] = $row[2];
+                       $ret[$nret]["val"] = $row[2];
+                       $ret[$nret]["category"] = $row[0];
+                       $ret[$nret]["name"] = $row[1];
                        $nret++;
                }
+               if($nret == 0) return false;
                
                return $ret;
        }
        
-       function delData($configType, $configName, $configVal)
+       function delData($configType, $configCat, $configName="", $configVal="")
        {
-               $sql = "delete from datatable where data_type='$configType' and data_name='$configName' and data_val='$configVal')";
+               $extrawhere = "";
+               if($configName != "") {
+                       $extrawhere = " and data_name='$configName'";
+               }
+               if($configVal != "") {
+                       $extrawhere .= " and data_val='$configVal'";
+               }
+               $sql = "delete from datatable where data_type='$configType' and data_category='$configCat' $extrawhere";
                $this->dbobject->query($sql);
+               
+               error_log("del all data was $sql");
        }
        
        function delAllDAta($configType, $configCat)
        {
-               $sql = "delete from datatable where data_type='$configType' and data_name='$configName'";
+               $sql = "delete from datatable where data_type='$configType' and data_category='$configCat'";
                //echo "sql is $sql\n";
                $this->dbobject->query($sql);
+               error_log("del all data was $sql");
        }
        
        function setupTables()
@@ -125,7 +135,7 @@ class GLCASConfig {
                $sql = 'CREATE TABLE "config" ( "configname" TEXT,"configvar" TEXT);';
                $this->dbobject->query($sql);
                
-               $sql = 'CREATE TABLE "datatable" ("data_id" INTEGER PRIMARY KEY AUTOINCREMENT,"data_category" TEXT,"data_type" TEXT,"data_name" TEXT,"data_val" TEXT);';
+               $sql = 'CREATE TABLE "datatable" ("data_id" INTEGER PRIMARY KEY AUTOINCREMENT,"data_type" TEXT,"data_category" TEXT,"data_name" TEXT,"data_val" TEXT);';
                $this->dbobject->query($sql);
 
                $sql = 'CREATE TABLE sqlite_sequence(name,seq);';
index 96b787e..6f93f80 100644 (file)
@@ -34,7 +34,7 @@ function GLCASMenuBuilder()
 {
        global $BASE_URL, $MENU_ITEMS;
        
-       sort($MENU_ITEMS);
+       ksort($MENU_ITEMS);
        ?>
        <table border="1"><tr><th>Menu</th><td>
 <a href="<?php echo $BASE_URL ?>">Home</a></td>
index 9bfbb65..7b56ef8 100644 (file)
@@ -25,6 +25,17 @@ class GLCASAdhoc {
        
        function go($url)
        {
+               error_log("ADHOC: url is $url in");
+               $tags = explode("/", $url);
+               if(isset($tags[1])) {
+                       error_log("tags 1 is set: \"".$tags[1]."\"");
+                       if($tags[1] != "") {
+                               if($this->doPackageRequest($tags[1])) return 0;
+                       } else {
+                               GLCASpageBuilder($this, "doPackageList");
+                               return 0;
+                       }
+               }
                if(isset($_REQUEST["action"])) {
                        switch($_REQUEST["action"]) {
                                case "addpackage":
@@ -43,22 +54,31 @@ class GLCASAdhoc {
                
        }
        
+       function doPackageList()
+       {
+               echo "im a package list\n";
+       }
+       
+       function doPackageRequest($package)
+       {
+               echo "i got a request for $package\n";
+               
+               return true;
+       }
+       
        function delPackage($url)
        {       
                $repostore = $this->config->getConfig("storagelocation");
                
                $pkgid = $_REQUEST["pkgid"];
+               $pkgname = $_REQUEST["pkgname"];
                
-               $pkgs = unserialize($this->config->getConfig("adhocpackages"));
-               
-               if(isset($pkgs[$pkgid])) {
-                       unset($pkgs[$pkgid]);
-                       $this->config->setConfig("adhocpackages", serialize($pkgs));
-                       $this->config->saveConfig();
-               }
+               $this->config->delAllData("adhocpkgdata", $pkgid);
+               $this->config->delData("adhoc", "pkglist", "$pkgname", "$pkgid");
                
                global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
-               header("Location: $BASE_URL/adhoc/");
+               header("Location: $BASE_URL/adhoc");
+               
                
        }
        
@@ -67,10 +87,16 @@ class GLCASAdhoc {
                $repostore = $this->config->getConfig("storagelocation");
                
                if(!isset($_REQUEST["pkgname"])) {
-                       echo "<font color=\"red\">Error: No package name set</font><br>";
+                       if(isset($_REQUEST["origpkg"])) if($_REQUEST["origpkg"] == -1) {
+                               echo "<font color=\"red\">Error: No package name set</font><br>";
+                               return 0;
+                       }
                } 
                if($_REQUEST["pkgname"] == "") {
-                       echo "<font color=\"red\">Error: Package name cant be blank</font><br>";
+                       if(isset($_REQUEST["origpkg"])) if($_REQUEST["origpkg"] == -1) {
+                               echo "<font color=\"red\">Error: Package name cant be blank</font><br>";
+                               return 0;
+                       }
                }
                
 
@@ -97,50 +123,63 @@ class GLCASAdhoc {
                
                if(!$uploaded && !$fileurl) {
                        echo "<font color=\"red\">Error: no uploaded file and no file url, you need one or the other buddy</font><br>";
+                       return 0;
                }
                
                $getfile = "";
                if($fileurl) {
                        $getfile = file_get_contents($_REQUEST["fileurl"]);
-               }
+               } 
                
                if($uploaded||$fileurl) {
                        echo "Got a file named $fname, $lname";
                }
                
-               $pkgs = unserialize($this->config->getConfig("adhocpackages"));
+               $myname = $_REQUEST["pkgname"];
+               $myarch = $_REQUEST["arch"];
+               $mytype = $_REQUEST["type"];
+               $myfileurl = $ultfileurl;
+               
+               if(isset($_REQUEST["origpkg"])) if($_REQUEST["origpkg"] != -1) {
+                       $n = $_REQUEST["origpkg"];
+                       $this->config->addData("adhocpkgdata", "$n", "arch", $myarch);
+                       $this->config->addData("adhocpkgdata", "$n", "type", $mytype);
+                       $outputfile = "$repostore/adhoc/$n.$myarch.$mytype";
+                       if($uploaded) {
+                               copy("$fname", "$outputfile");
+                       } else {
+                               file_put_contents("$outputfile", $getfile);
+                       }
+                       
+                       global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+                       header("Location: $BASE_URL/adhoc");
+                       return 0;
+               }
                
-               $n = -1;
-               foreach($pkgs as $key => $var) {
-                       $n = $key;
+               // assuming add a new
+               $n = 0;
+               $pkglist = $this->config->getData("adhoc", "pkglist");
+               if($pkglist !== false) foreach($pkglist as $key => $val) {
+                       $n = $val["name"]+1;
                }
-               $n++;
+
+               error_log("would add package as $n");
                
-               echo "Would add package as $n";
+               $this->config->addData("adhoc", "pkglist", "$myname", $n);
+               $this->config->addData("adhocpkgdata", "$n", "arch", $myarch);
+               $this->config->addData("adhocpkgdata", "$n", "type", $mytype);
                
-               $pkgs[$n]["name"] = $_REQUEST["pkgname"];
-               $pkgs[$n]["arch"] = $_REQUEST["arch"];
-               $pkgs[$n]["type"] = $_REQUEST["type"];
-               $pkgs[$n]["fileurl"] = $ultfileurl;
-
+               
+               $outputfile = "$repostore/adhoc/$n.$myarch.$mytype";
                if($uploaded) {
-                       copy("$fname", "$repostore/adhoc/$n");
+                       copy("$fname", "$outputfile");
                } else {
-                       file_put_contents("$repostore/adhoc/$n", $fetfile);
+                       file_put_contents("$outputfile", $getfile);
                }
                
-               $this->config->setConfig("adhocpackages", serialize($pkgs));
-               $this->config->saveConfig();
-               
-               echo "Done";
-               
-               
-               echo "<pre>";
-               print_r($_SERVER);
-               print_r($_FILES);
-               print_r($_REQUEST);
-               print_r($pkgs);
-               echo "</pre>";
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               header("Location: $BASE_URL/adhoc");
+
        }
        
        
@@ -148,23 +187,51 @@ class GLCASAdhoc {
        {
                echo "<h2>Packages</h2>";
 
+               /* TODO: to new config
                $pkgs = unserialize($this->config->getConfig("adhocpackages"));
-               echo "<table border=\"1\"><tr><th>Name</th><th>Package Types</th><th>Architecture</th><th>Control</th></tr>";
-               
-               foreach($pkgs as $key => $var) {
-                       $name = $var["pkgname"];
-                       $arch = $var["arch"];
-                       $type = $var["type"];
-                       echo "<tr><td>$name</td><td>$type</td><td>$arch</td><td>Add another arch <a href=\"?action=deletepkg&pkgid=$key\">Delete</a></td></tr>";
+               */
+               $pkgs = $this->config->getData("adhoc");
+               //print_r($pkgs);
+               if($pkgs !== false) {
+                       echo "<table border=\"1\"><tr><th>Name</th><th>Package Types</th><th>Architecture</th><th>Control</th></tr>";
+                       
+                       foreach($pkgs as $key => $var) {
+                               $name = $var["name"];
+                               $id = $var["val"];
+                               $arch_v = $this->config->getData("adhocpkgdata", $id, "arch");
+                               $arch = $arch_v[0]["val"];
+                               $type_v = $this->config->getData("adhocpkgdata", $id, "type");
+                               $type = $type_v[0]["val"];
+                               echo "<tr><td>$name</td><td>$type</td><td>$arch</td><td><a href=\"?action=deletepkg&pkgid=$id&pkgname=$name\">Delete</a></td></tr>";
+                               if(count($arch_v) > 1) {
+                                       for($i=1; $i < count($arch_v); $i++) {
+                                               $arch = $arch_v[$i]["val"];
+                                               $type_v = $this->config->getData("adhocpkgdata", $id, "type");
+                                               $type = $type_v[$i]["val"];
+                                               echo "<tr><td></td><td>$type</td><td>$arch</td><td></td></tr>";
+                                       }
+                               }
+                       }
+                       echo "<table>";
                }
-               echo "<table>";
-               
+                       
                $kos = getKnownOSList();
                
                echo "<hr>";
                echo "<h3>Add a Package</h3><br>";
                echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"?action=addpackage\">";
-               echo "Name <input type=\"text\" name=\"pkgname\"> or Add version of <select name=\"origpkg\"></select><br>";
+               echo "Name <input type=\"text\" name=\"pkgname\">";
+               if($pkgs !== false) {
+                       echo "or Add version of <select name=\"origpkg\">";
+                       echo "<option value=\"-1\" default>None</option>";
+                       foreach($pkgs as $key=>$var) {
+                               $id = $var["val"];
+                               $name = $var["name"];
+                               echo "<option value=\"$id\">$name</option>";
+                       }
+                       echo "</select><br>";
+               }
+               else echo "<br>";
                echo "Arch <select name=\"arch\">";
                foreach($kos["pkgarch"] as $key => $val) {
                        echo "<option value=\"$key\">$val</option>";
index 76e3e59..3634682 100644 (file)
@@ -22,11 +22,18 @@ class GLCASHosts {
                        switch($_REQUEST["action"]) {
                                case "addhost":
                                        error_log("in updaterepo");
-                                       GLCASpageBuilder($this,"addPackage");
+                                       GLCASpageBuilder($this,"addHost");
                                        return;
-                               case "delhost":
-                                       error_log("in delete pkg");
-                                       GLCASpageBuilder($this, "delPackage");
+                               case "addgroup":
+                                       error_log("in add group");
+                                       GLCASpageBuilder($this, "addGroup");
+                                       return;
+                               case "deletehost":
+                                       error_log("in add group");
+                                       GLCASpageBuilder($this, "deleteHost");
+                                       return;
+                               case "scanrange":
+                                       GLCASpageBuilder($this, "scanIPRange");
                                        return;
                        }
                }
@@ -36,9 +43,194 @@ class GLCASHosts {
                
        }
        
+       function scanIPRange($url)
+       {
+               $iprange = $_REQUEST["scanip"];
+               $hostgroup = "";
+               if(isset($_REQUEST["hostgroup"])) {
+                       $hostgroup = $_REQUEST["hostgroup"];
+               }
+               
+               // we just assume class c atm
+               $ips_v = explode(".", $iprange);
+               
+               $ips = $ips_v[0].".".$ips_v[1].".".$ips_v[2];
+               
+               for($i = 1; $i < 32; $i++) {
+                       $hostname = "";
+                       $ips_me = "$ips.$i";
+                       echo "Scanning $ips_me<br>";
+                       error_log("Scanning $ips_me<br>");
+                       flush();
+                       $hostname = gethostbyaddr($ips_me);
+                       if($hostname != $ips_me) {
+                               echo "Found host on $ips_me as $hostname<br>";
+                               error_log("Found host on $ips_me as $hostname");
+                               flush();
+                               $hosts = $this->config->getData("hosts");
+                               $exists = false;
+                               foreach($hosts as $key => $val) {
+                                       if($val["category"] == $hostname && $val["name"] == $ips_me) {
+                                               echo "Host in db already<br>";
+                                               $exists = true;
+                                               flush();
+                                       }
+                                       
+                               }
+                               if(!$exists) $this->config->addData("hosts", "$hostname", "$ips_me", "$hostgroup");
+                               
+                               
+                       }
+               }
+               echo "Finished";
+       }
+       
+       function deleteHost($url)
+       {
+               $hostname = $_REQUEST["hostname"];
+               $this->config->delData("hosts", "$hostname");
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               header("Location: $BASE_URL/hosts");
+       }
+       
+       function addHost($url)
+       {
+               $hg = $_REQUEST["hostgroup"];
+               $hostname = $_REQUEST["hostname"];
+               $ip = $_REQUEST["ipaddr"];
+               
+               if($hostname == "" && $ip == "") {
+                       echo "Error: must have either ip or hostname\n";
+                       return 0;
+               }
+               
+               if($hostname == "") {
+                       // try to lookup hostname from ip
+                       $hostname = gethostbyaddr($ip);
+               }
+               
+               if($ip == "") {
+                       // try to lookup ip from hostname
+                       $ip = gethostbyname($hostname);
+               }
+               
+               $hosts = $this->config->getData("hosts");
+               foreach($hosts as $key => $val) {
+                       if($val["category"] == $hostname && $val["name"] == $ip) {
+                               echo "Error: host already exists<br>";
+                               return 0;
+                       }
+               }
+               
+               $this->config->addData("hosts", "$hostname", "$ip", "$hg");
+               
+               
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               header("Location: $BASE_URL/hosts");
+               
+       }
+       
+       function addGroup($url)
+       {
+               $grpname = $_REQUEST["groupname"];
+               $groups = $this->config->getData("hostgroups");
+               
+               foreach($groups as $key => $val) {
+                       if($val["category"] == $grpname) {
+                               echo "Error: gorup already exists";
+                               return 0;
+                       }
+               }
+               
+               $this->config->adddata("hostgroups", "$grpname", "", "");
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               header("Location: $BASE_URL/hosts");
+               
+       }
+       
        function mainBody($url)
        {
-               echo "I am a hosts thingy";
+               $hosts = $this->config->getData("hosts");
+               $groups = $this->config->getData("hostgroups");
+               
+               echo "<h2>Hosts</h2>";
+               if($hosts) {
+                       echo "<table border=\"1\"><tr><th>Host</th><th>IP</th><th>Host Group</th><th>Last Seen</th><th>Control</th></tr>";
+                       foreach($hosts as $key => $val) {
+                               $hname = $val["category"];
+                               $hip = $val["name"];
+                               $hg = $val["val"];
+                               if($hg == "") $hg = "-";
+                               echo "<tr><td>$hname</td><td>$hip</td><td>$hg</td><td>...</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
+                       }
+                       echo "</table>";
+               } else {
+                       echo "No hosts defined yet<br>"; 
+               }
+               echo "<hr>";
+               
+               // groups
+               echo "<h2>Host Groups</h2>";
+               if($groups) {
+                       echo "<table border=\"1\"><tr><th>Group Name</th></tr>";
+                       foreach($groups as $key=>$val) {
+                               $grpname = $val["category"];
+                               echo "<tr><td>$grpname</td></tr>";
+                       }
+                       echo "</table>";
+               } else {
+                       echo "No host groups defined yet<br>";
+               }
+               echo "<hr>";
+               
+               
+               echo "<table><tr valign=\"top\"><td>";
+               
+               // the add hosts dialog
+               echo "<h2>Add Host</h2>";
+               echo "<form method=\"post\" action=\"?action=addhost\">";
+               echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
+               echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
+               if($groups) {
+                       echo "Host Group: <select name=\"hostgroup\">";
+                       echo "<option value=\"\">None</option>";
+                       foreach($groups as $key => $val) {
+                               $hgname = $val["category"];
+                               echo "<option value=\"$hgname\">$hgname</option>";
+                       }
+                       echo "</select><br>";
+               }
+               echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
+               echo "</form>";
+               
+               echo "</td><td>";
+               
+               // the add groups dialog
+               echo "<h2>Add Group</h2>";
+               echo "<form method=\"post\" action=\"?action=addgroup\">";
+               echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
+               echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
+               echo "</form>";
+               
+               echo "</td><td>";
+               //scan ip range via dns
+               echo "<h2>Scan Range</h2>";
+               echo "<form method=\"post\" action=\"?action=scanrange\">";
+               echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
+               if($groups) {
+                       echo "Host Group: <select name=\"hostgroup\">";
+                       echo "<option value=\"\">None</option>";
+                       foreach($groups as $key => $val) {
+                               $hgname = $val["category"];
+                               echo "<option value=\"$hgname\">$hgname</option>";
+                       }
+                       echo "</select><br>";
+               }
+               echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
+               echo "</form>";
+                               
+               echo "</table>";
+               
        }
        
        private $config;