eb93989795df1cc96125aa20f833eb41ea0ff652
[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                 $groups_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>Host Group</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=="-") echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$hg</td><td>...</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
275                 }
276                 echo "</table>";
277                 echo "</td>";
278
279                 if($groups) {
280                         foreach($groups as $key=>$val) {
281                                 $gs++;
282                                 echo "<td>";
283                                 $grpname = $val["category"];
284                                 echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
285                                 echo "<table border=\"1\"><tr><th><input type=\"checkbox\" name=\"$grpname-selectgroup\"></th><th>Host</th><th>IP</th><th>Host Group</th><th>Last Seen</th><th>Control</th></tr>";
286                                 if($hosts != false) foreach($hosts as $key => $val) {
287                                         $hname = $val["category"];
288                                         $hip = $val["name"];
289                                         $hg = $val["val"];
290                                         if($hg == "") $hg = "-";
291                                         if($hg==$grpname) echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$hg</td><td>...</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
292                                 }
293                                 echo "</table>";
294                                 echo "</td>";
295                                 if(($gs%4)==0) echo "</tr><tr>";
296                         }
297                 }
298                 
299                 
300                 echo "</tr></table>";
301                 echo "</form>";
302                 
303                 echo "<hr>";
304                 
305                 
306                 
307                 echo "<table><tr valign=\"top\"><td>";
308                 
309                 // the add hosts dialog
310                 echo "<h2>Add Host</h2>";
311                 echo "<form method=\"post\" action=\"?action=addhost\">";
312                 echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
313                 echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
314                 if($groups) {
315                         echo "Host Group: <select name=\"hostgroup\">";
316                         echo "<option value=\"\">None</option>";
317                         foreach($groups as $key => $val) {
318                                 $hgname = $val["category"];
319                                 echo "<option value=\"$hgname\">$hgname</option>";
320                         }
321                         echo "</select><br>";
322                 }
323                 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
324                 echo "</form>";
325                 
326                 echo "</td><td>";
327                 
328                 // the add groups dialog
329                 echo "<h2>Add Group</h2>";
330                 echo "<form method=\"post\" action=\"?action=addgroup\">";
331                 echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
332                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
333                 echo "</form>";
334                 
335                 echo "</td><td>";
336                 //scan ip range via dns
337                 echo "<h2>Scan Range</h2>";
338                 echo "<form method=\"post\" action=\"?action=scanrange\">";
339                 echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
340                 if($groups) {
341                         echo "Host Group: <select name=\"hostgroup\">";
342                         echo "<option value=\"\">None</option>";
343                         foreach($groups as $key => $val) {
344                                 $hgname = $val["category"];
345                                 echo "<option value=\"$hgname\">$hgname</option>";
346                         }
347                         echo "</select><br>";
348                 }
349                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
350                 echo "</form>";
351                                 
352                 echo "</table>";
353                 
354         }
355         
356         private $config;
357 }
358
359 ?>