hostgroup scanning works.
[glcas.git] / plugins / hosts.php
1 <?php 
2
3 error_log("hosts loaded");
4
5 global $URL_HANDLERS;
6 $URL_HANDLERS["hosts.*"] = "GLCASHosts";
7 global $BASE_URL, $MENU_ITEMS;
8 $MENU_ITEMS["30hosts"]["name"] = "Known Machines";
9 $MENU_ITEMS["30hosts"]["link"] = "$BASE_URL/hosts";
10
11 class GLCASHosts {
12         function __construct($config)
13         {
14                 $this->config = $config;
15                 error_log("constructor for GLCASHosts");
16                 
17         }
18         
19         function go($url)
20         {
21                 if(isset($_REQUEST["action"])) {
22                         switch($_REQUEST["action"]) {
23                                 case "addhost":
24                                         error_log("in updaterepo");
25                                         GLCASpageBuilder($this,"addHost");
26                                         return;
27                                 case "addgroup":
28                                         error_log("in add group");
29                                         GLCASpageBuilder($this, "addGroup");
30                                         return;
31                                 case "deletehost":
32                                         error_log("in add group");
33                                         GLCASpageBuilder($this, "deleteHost");
34                                         return;
35                                 case "scanrange":
36                                         GLCASpageBuilder($this, "scanIPRange");
37                                         return;
38                         }
39                 }
40                 
41                 GLCASpageBuilder($this,"mainBody");
42                 return;
43                 
44         }
45         
46         function scanIPRange($url)
47         {
48                 $iprange = $_REQUEST["scanip"];
49                 $hostgroup = "";
50                 if(isset($_REQUEST["hostgroup"])) {
51                         $hostgroup = $_REQUEST["hostgroup"];
52                 }
53                 
54                 // we just assume class c atm
55                 $ips_v = explode(".", $iprange);
56                 
57                 $ips = $ips_v[0].".".$ips_v[1].".".$ips_v[2];
58                 
59                 for($i = 1; $i < 32; $i++) {
60                         $hostname = "";
61                         $ips_me = "$ips.$i";
62                         echo "Scanning $ips_me<br>";
63                         error_log("Scanning $ips_me<br>");
64                         flush();
65                         $hostname = gethostbyaddr($ips_me);
66                         if($hostname != $ips_me) {
67                                 echo "Found host on $ips_me as $hostname<br>";
68                                 error_log("Found host on $ips_me as $hostname");
69                                 flush();
70                                 $hosts = $this->config->getData("hosts");
71                                 $exists = false;
72                                 foreach($hosts as $key => $val) {
73                                         if($val["category"] == $hostname && $val["name"] == $ips_me) {
74                                                 echo "Host in db already<br>";
75                                                 $exists = true;
76                                                 flush();
77                                         }
78                                         
79                                 }
80                                 if(!$exists) $this->config->addData("hosts", "$hostname", "$ips_me", "$hostgroup");
81                                 
82                                 
83                         }
84                 }
85                 echo "Finished";
86         }
87         
88         function deleteHost($url)
89         {
90                 $hostname = $_REQUEST["hostname"];
91                 $this->config->delData("hosts", "$hostname");
92                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
93                 header("Location: $BASE_URL/hosts");
94         }
95         
96         function addHost($url)
97         {
98                 $hg = $_REQUEST["hostgroup"];
99                 $hostname = $_REQUEST["hostname"];
100                 $ip = $_REQUEST["ipaddr"];
101                 
102                 if($hostname == "" && $ip == "") {
103                         echo "Error: must have either ip or hostname\n";
104                         return 0;
105                 }
106                 
107                 if($hostname == "") {
108                         // try to lookup hostname from ip
109                         $hostname = gethostbyaddr($ip);
110                 }
111                 
112                 if($ip == "") {
113                         // try to lookup ip from hostname
114                         $ip = gethostbyname($hostname);
115                 }
116                 
117                 $hosts = $this->config->getData("hosts");
118                 foreach($hosts as $key => $val) {
119                         if($val["category"] == $hostname && $val["name"] == $ip) {
120                                 echo "Error: host already exists<br>";
121                                 return 0;
122                         }
123                 }
124                 
125                 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
126                 
127                 
128                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
129                 header("Location: $BASE_URL/hosts");
130                 
131         }
132         
133         function addGroup($url)
134         {
135                 $grpname = $_REQUEST["groupname"];
136                 $groups = $this->config->getData("hostgroups");
137                 
138                 foreach($groups as $key => $val) {
139                         if($val["category"] == $grpname) {
140                                 echo "Error: gorup already exists";
141                                 return 0;
142                         }
143                 }
144                 
145                 $this->config->adddata("hostgroups", "$grpname", "", "");
146                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
147                 header("Location: $BASE_URL/hosts");
148                 
149         }
150         
151         function mainBody($url)
152         {
153                 $hosts = $this->config->getData("hosts");
154                 $groups = $this->config->getData("hostgroups");
155                 
156                 echo "<h2>Hosts</h2>";
157                 if($hosts) {
158                         echo "<table border=\"1\"><tr><th>Host</th><th>IP</th><th>Host Group</th><th>Last Seen</th><th>Control</th></tr>";
159                         foreach($hosts as $key => $val) {
160                                 $hname = $val["category"];
161                                 $hip = $val["name"];
162                                 $hg = $val["val"];
163                                 if($hg == "") $hg = "-";
164                                 echo "<tr><td>$hname</td><td>$hip</td><td>$hg</td><td>...</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
165                         }
166                         echo "</table>";
167                 } else {
168                         echo "No hosts defined yet<br>"; 
169                 }
170                 echo "<hr>";
171                 
172                 // groups
173                 echo "<h2>Host Groups</h2>";
174                 if($groups) {
175                         echo "<table border=\"1\"><tr><th>Group Name</th></tr>";
176                         foreach($groups as $key=>$val) {
177                                 $grpname = $val["category"];
178                                 echo "<tr><td>$grpname</td></tr>";
179                         }
180                         echo "</table>";
181                 } else {
182                         echo "No host groups defined yet<br>";
183                 }
184                 echo "<hr>";
185                 
186                 
187                 echo "<table><tr valign=\"top\"><td>";
188                 
189                 // the add hosts dialog
190                 echo "<h2>Add Host</h2>";
191                 echo "<form method=\"post\" action=\"?action=addhost\">";
192                 echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
193                 echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
194                 if($groups) {
195                         echo "Host Group: <select name=\"hostgroup\">";
196                         echo "<option value=\"\">None</option>";
197                         foreach($groups as $key => $val) {
198                                 $hgname = $val["category"];
199                                 echo "<option value=\"$hgname\">$hgname</option>";
200                         }
201                         echo "</select><br>";
202                 }
203                 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
204                 echo "</form>";
205                 
206                 echo "</td><td>";
207                 
208                 // the add groups dialog
209                 echo "<h2>Add Group</h2>";
210                 echo "<form method=\"post\" action=\"?action=addgroup\">";
211                 echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
212                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
213                 echo "</form>";
214                 
215                 echo "</td><td>";
216                 //scan ip range via dns
217                 echo "<h2>Scan Range</h2>";
218                 echo "<form method=\"post\" action=\"?action=scanrange\">";
219                 echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
220                 if($groups) {
221                         echo "Host Group: <select name=\"hostgroup\">";
222                         echo "<option value=\"\">None</option>";
223                         foreach($groups as $key => $val) {
224                                 $hgname = $val["category"];
225                                 echo "<option value=\"$hgname\">$hgname</option>";
226                         }
227                         echo "</select><br>";
228                 }
229                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
230                 echo "</form>";
231                                 
232                 echo "</table>";
233                 
234         }
235         
236         private $config;
237 }
238
239 ?>