5b521cf6499f042a5a49b00ac27965f23f6a344c
[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                                 case "delgroup":
39                                         GLCASpageBuilder($this, "deleteGroup");
40                                         return;
41                                 case "dooperation":
42                                         GLCASpageBuilder($this, "doOperation");
43                                         return;
44                         }
45                 }
46                 
47                 GLCASpageBuilder($this,"mainBody");
48                 return;
49                 
50         }
51         
52         
53         // TODO: this whole function wont work correctly
54         // once we move to hosts being in multiple groups..
55         function doOperation($url)
56         {
57                 $operation = $_REQUEST["operation"];
58                 $groupop = $_REQUEST["groupop"];
59                 $hosts = $this->config->getData("hosts");
60                 $groups = $this->config->getData("hostgroups");
61                 $nogroup = false;
62                 $hosts_to_do = null;
63                 $nhosts = 0;
64                 $group_to_do = null;
65                 $ngroups = 0;
66                 
67                 // first check if nogroup is selected
68                 if(isset($_REQUEST["nonegroup-selected"])) {
69                         //echo "nonegroup operation<br>";
70                         $nogroup = true;
71                 }
72                 
73                 foreach($groups as $key=>$val) {
74                         $grpname = $val["category"];
75                         if(isset($_REQUEST["$grpname-selectgroup"])) {
76                                 $group_to_do[$ngroups++] = $grpname;
77                                 
78                         }
79                 }
80                 
81                 foreach($hosts as $key=>$val) {
82                         $dohost = false;
83                         $hname = $val["category"];
84                         // this bits annoying TODO: find out why this happens
85                         $thname = preg_replace("/\./", "_", $hname);
86                         $hip = $val["name"];
87                         $hg = $val["val"];
88                         
89                         echo "<br>Checking $hname, $hip, $hg<br>";
90                         
91                         // check if no group is on
92                         if($nogroup && $hg == "") {
93                                 //echo "Set doing true on nogroup<br>";
94                                 $dohost = true;
95                         }
96                         
97                         // check if host was in a group selected
98                         if($group_to_do !== null) foreach($group_to_do as $dothese) {
99                                 if($hg == $dothese) $dohost = true;
100                         }
101                         
102                         // directly selected hosts
103                         if(isset($_REQUEST["$thname-selecthost"])) {
104                                 //echo "add host $hname<br>";
105                                 $dohost = true;
106                         }
107                         
108                         // now do the op
109                         if($dohost) {
110                                 if($operation == "move") {
111                                         $this->config->delData("hosts", "$hname", "$hip", "$hg");
112                                         $this->config->addData("hosts", "$hname", "$hip", "$groupop");
113                                 }
114                                 if($operation == "delete") {
115                                         $this->config->delData("hosts", "$hname", "$hip", "$hg");
116                                 }
117                         }
118                 }
119                 
120                 /*echo "total to do:<br><pre>";
121                 print_r($hosts_to_do);
122                 print_r($_REQUEST);
123                 echo "</pre>";*/
124                 
125                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
126                 header("Location: $BASE_URL/hosts");
127                 
128                 
129         }
130         
131         function deleteGroup($url)
132         {
133                 $grpname = $_REQUEST["grpname"];
134                 $this->config->delData("hostgroups", "$grpname");
135                 $hosts = $this->config->getData("hosts");
136                 foreach($hosts as $key=>$val) {
137                         $hname = $val["category"];
138                         $hip = $val["name"];
139                         $hg = $val["val"];
140                         
141                         if($hg == $grpname) {
142                                 $this->config->delData("hosts", "$hname", "$hip", "$hg");
143                                 $this->config->addData("hosts", "$hname", "$hip", "");
144                         }
145                 }
146                 
147                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
148                 header("Location: $BASE_URL/hosts");
149                 
150         }
151         
152         function scanIPRange($url)
153         {
154                 $iprange = $_REQUEST["scanip"];
155                 $hostgroup = "";
156                 if(isset($_REQUEST["hostgroup"])) {
157                         $hostgroup = $_REQUEST["hostgroup"];
158                 }
159                 
160                 // we just assume class c atm
161                 error_log("background scanner, start");
162                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
163                 if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
164                         $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/scanhelper.php '$iprange' '$hostgroup' > /tmp/scanlog 2>&1 &";
165                         system($scall);
166                 } else {
167                         error_log("cant find download helper... dieing");
168                 }
169                 
170                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
171                 header("Location: $BASE_URL/hosts");
172                 
173                 
174         }
175         
176         function deleteHost($url)
177         {
178                 $hostname = $_REQUEST["hostname"];
179                 $this->config->delData("hosts", "$hostname");
180                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
181                 header("Location: $BASE_URL/hosts");
182         }
183         
184         function addHost($url)
185         {
186                 $hg = $_REQUEST["hostgroup"];
187                 $hostname = $_REQUEST["hostname"];
188                 $ip = $_REQUEST["ipaddr"];
189                 
190                 if($hostname == "" && $ip == "") {
191                         echo "Error: must have either ip or hostname\n";
192                         return 0;
193                 }
194                 
195                 if($hostname == "") {
196                         // try to lookup hostname from ip
197                         $hostname = gethostbyaddr($ip);
198                 }
199                 
200                 if($ip == "") {
201                         // try to lookup ip from hostname
202                         $ip = gethostbyname($hostname);
203                 }
204                 
205                 $hosts = $this->config->getData("hosts");
206                 foreach($hosts as $key => $val) {
207                         if($val["category"] == $hostname && $val["name"] == $ip) {
208                                 echo "Error: host already exists<br>";
209                                 return 0;
210                         }
211                 }
212                 
213                 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
214                 
215                 
216                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
217                 header("Location: $BASE_URL/hosts");
218                 
219         }
220         
221         function addGroup($url)
222         {
223                 $grpname = $_REQUEST["groupname"];
224                 $groups = $this->config->getData("hostgroups");
225                 
226                 foreach($groups as $key => $val) {
227                         if($val["category"] == $grpname) {
228                                 echo "Error: gorup already exists";
229                                 return 0;
230                         }
231                 }
232                 
233                 $this->config->adddata("hostgroups", "$grpname", "", "");
234                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
235                 header("Location: $BASE_URL/hosts");
236                 
237         }
238         
239         function mainBody($url)
240         {
241                 $hosts = $this->config->getData("hosts");
242                 $groups = $this->config->getData("hostgroups");
243                 
244                 $gs = 0;
245                 echo "<h2>Hosts and Groups</h2>";
246                 echo "<form method=\"post\"action=\"?action=dooperation\">";
247                 echo "Selected hosts operation: <select name=\"operation\">";
248                 echo "<option value=\"move\">Move To</option>";
249                 echo "<option value=\"delete\">Delete</option>";
250                 echo "</select>";
251                 echo "Group <select name=\"groupop\">";
252                 echo "<option value=\"\">None</option>";
253                 foreach($groups as $key => $val) {
254                         $grpname = $val["category"];
255                         echo "<option value=\"$grpname\">$grpname</option>";
256                 }
257                 echo "</select>";
258                 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
259                 
260                 
261                 echo "<table border=\"1\">";
262                 
263                 $gs++;
264                 // fist print ungrouped
265                 echo "<tr valign=\"top\">";
266                 echo "<td>";
267                 echo "<h3>Ungrouped Hosts</h3><br>";
268                 echo "<table border=\"1\"><tr><th><input type=\"checkbox\" name=\"nonegroup-selected\"></th><th>Host</th><th>IP</th><th>Last Seen</th><th>Control</th></tr>";
269                 foreach($hosts as $key => $val) {
270                         $hname = $val["category"];
271                         $hip = $val["name"];
272                         $hg = $val["val"];
273                         if($hg == "") $hg = "-";
274                         if($hg=="-") {
275                                 $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
276                                 echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
277                         }
278                 }
279                 echo "</table>";
280                 echo "</td>";
281
282                 if($groups) {
283                         foreach($groups as $key=>$val) {
284                                 $gs++;
285                                 echo "<td>";
286                                 $grpname = $val["category"];
287                                 echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
288                                 echo "<table border=\"1\"><tr><th><input type=\"checkbox\" name=\"$grpname-selectgroup\"></th><th>Host</th><th>IP</th><th>Last Seen</th><th>Control</th></tr>";
289                                 if($hosts != false) foreach($hosts as $key => $val) {
290                                         $hname = $val["category"];
291                                         $hip = $val["name"];
292                                         $hg = $val["val"];
293                                         if($hg == "") $hg = "-";
294                                         if($hg==$grpname) {
295                                                 $lastseen = glcas_getLastSeen($hip, $this->config->getData("lastseen", "$hip"));
296                                                 echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$lastseen</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
297                                         }
298                                 }
299                                 echo "</table>";
300                                 echo "</td>";
301                                 if(($gs%4)==0) echo "</tr><tr>";
302                         }
303                 }
304                 
305                 
306                 echo "</tr></table>";
307                 echo "</form>";
308                 
309                 echo "<hr>";
310                 
311                 
312                 
313                 echo "<table><tr valign=\"top\"><td>";
314                 
315                 // the add hosts dialog
316                 echo "<h2>Add Host</h2>";
317                 echo "<form method=\"post\" action=\"?action=addhost\">";
318                 echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
319                 echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
320                 if($groups) {
321                         echo "Host Group: <select name=\"hostgroup\">";
322                         echo "<option value=\"\">None</option>";
323                         foreach($groups as $key => $val) {
324                                 $hgname = $val["category"];
325                                 echo "<option value=\"$hgname\">$hgname</option>";
326                         }
327                         echo "</select><br>";
328                 }
329                 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
330                 echo "</form>";
331                 
332                 echo "</td><td>";
333                 
334                 // the add groups dialog
335                 echo "<h2>Add Group</h2>";
336                 echo "<form method=\"post\" action=\"?action=addgroup\">";
337                 echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
338                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
339                 echo "</form>";
340                 
341                 echo "</td><td>";
342                 //scan ip range via dns
343                 echo "<h2>Scan Range</h2>";
344                 echo "<form method=\"post\" action=\"?action=scanrange\">";
345                 echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
346                 if($groups) {
347                         echo "Host Group: <select name=\"hostgroup\">";
348                         echo "<option value=\"\">None</option>";
349                         foreach($groups as $key => $val) {
350                                 $hgname = $val["category"];
351                                 echo "<option value=\"$hgname\">$hgname</option>";
352                         }
353                         echo "</select><br>";
354                 }
355                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
356                 echo "</form>";
357                                 
358                 echo "</table>";
359                 
360         }
361         
362         private $config;
363 }
364
365 ?>