e827c2ca879676fd4af03b2940af6e27a0aa8e16
[CBFWR.git] / libcbfwr / fwui.php
1 <?php
2
3 function CBFWZonesPage($urls)
4 {
5         global $BASE_URL, $MENU_ITEMS;
6         $comms = new Comms();
7         
8         if(isset($urls[1])) {
9                 switch($urls[1]) {
10                         case "modify":
11                                 
12                                 if($_REQUEST["description"] != "") $desc = $_REQUEST["description"];
13                                 else $desc = null;
14                                 
15                                 $oldname = $_REQUEST["oldname"];
16                                 $newname = $_REQUEST["zonename"];
17                                 
18                                 $renewname = null;
19                                 error_log("comparing \"$oldname\" to \"$newname\"");
20                                 if($oldname != $newname) {
21                                         $renewname = " rename $newname";
22                                 }
23                                 
24                                 $oldconf = getWebUserConfig();
25                                 $olddesc = $oldconf["zone"][$oldname]["description"];
26                                 error_log("Comparing \"$olddesc\" and \"$desc\"");
27                                 if($desc != $olddesc) {
28                                         $newdesc = " # $desc";
29                                 } else $newdesc = null;
30                                 
31                                 //echo "confs: \"$olddesc\", \"$newdesc\", $oldname, $newname<br>";
32                                 //exit(0);
33                                 
34                                 if($renewname == null && $newdesc == null) {
35                                         header("Location: $BASE_URL/zones");
36                                         error_log("no changes?");
37                                         return;
38                                 }
39                                 
40                                 
41                                 $conf = $comms->getConfig(3);
42                                 $changes = $conf["status"];
43                                 if($changes == "nochange") $conf["status"] = 1;
44                                 else $conf["status"] = $changes + 1;
45                                 $cnum = $conf["status"];
46                                 
47                                 $conf[$cnum]["modify"] = "zone $oldname$renewname$newdesc";
48                                 $comms->putConfig($conf, 3);
49                                 header("Location: $BASE_URL/zones");
50                                 
51                                 break;
52                                 
53                                 
54                         case "edit":
55                                 CBFWpageBuilder(null, "CBFWZonesEditPage", null, null, $urls);
56                                 break;
57                                 
58                                 
59                         case "delete":
60                                 $zonetodelete = $urls[2];
61                                 
62                                 $conf = $comms->getConfig(3);
63                                 $changes = $conf["status"];
64                                 if($changes == "nochange") $conf["status"] = 1;
65                                 else $conf["status"] = $changes + 1;
66                                 $cnum = $conf["status"];
67                                 
68                                 $conf[$cnum]["delete"] = "zone $zonetodelete";
69                                 $comms->putConfig($conf, 3);
70                                 header("Location: $BASE_URL/zones");
71                                 break;
72                                 
73                                 
74                         case "add":
75                                 
76                                 // UGLY
77                                 $cnum = 0;
78                                 $nametoadd = $_REQUEST["toadd"];
79                                 $desc = $_REQUEST["desc"];
80                                 $descbit = "";
81                                 if($desc != "") $descbit = " # $desc";
82                                 if($nametoadd == "" || $nametoadd == null) {
83                                         return;
84                                 }
85                                 $conf = $comms->getConfig(3);
86                                 $changes = $conf["status"];
87                                 if($changes == "nochange") $conf["status"] = 1;
88                                 else $conf["status"] = $changes + 1;
89                                 $cnum = $conf["status"];
90                                 $conf[$cnum]["add"] = "zone $nametoadd$descbit";
91                                 $comms->putConfig($conf, 3);
92                                 header("Location: $BASE_URL/zones");
93                                 break;
94                                 
95                                 
96                         default:
97                                 CBFWpageBuilder(null, "CBFWZonesPageDisplay");
98                 }
99         } else {
100                 CBFWpageBuilder(null, "CBFWZonesPageDisplay");
101         }
102 }
103
104 function CBFWZonesEditPage($urls)
105 {
106         global $BASE_URL, $MENU_ITEMS;
107         
108         $zone = $urls[2];
109         
110         $config = getWebUserConfig();
111         
112         
113         echo "<h2>Edit Zone $zone</h2>";
114         echo "<form method=\"post\" action=\"$BASE_URL/zones/modify/$zone\">";
115         echo "<input type=\"hidden\" name=\"oldname\" value=\"$zone\">";
116         echo "Name: <input type=\"text\" name=\"zonename\" value=\"$zone\"><br>";
117         
118         if(isset($config["zone"][$zone]["description"])) $desc = $config["zone"][$zone]["description"];
119         
120         echo "Description: <input type=\"text\" name=\"description\" value=\"$desc\"><br>";
121         echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
122         echo "</form>";
123 }
124
125 function getWebUserConfig()
126 {
127         $comms = new Comms();
128         $myconf = new Config();
129         
130         
131         // check to see if pre-cache config matches change level of config change buffer
132         $config = $comms->getConfig(5);
133         $conf2 = $comms->getConfig(3);
134         if(isset($config["status"])) {
135                 if($config["status"] == $conf2["status"]) {
136                         // it did, return the pre-cache
137                         error_log("got pre-cache config");
138                         return $config;
139                 }
140         }
141         
142         // it did not, do a new merge and cache
143         $config = $myconf->mergeConfig($comms->getConfig(), $conf2);
144         $reconfig = $config;
145         $reconfig["status"] = $conf2["status"];
146         error_log("put pre-cache config");
147         $comms->putConfig($reconfig, 5);        
148         
149         return $config;
150 }
151
152 function CBFWZonesPageDisplay()
153 {
154         global $BASE_URL, $MENU_ITEMS;
155         
156         $config = getWebUserConfig();
157
158         echo "<h2>Zones</h2>";
159         echo "<table border=\"1\"><tr><th>Zone</th><th>Interfaces</th><th>Description</th><th>Edit</th></tr>";
160         foreach($config["zone"] as $key => $var) {
161                 $name = $key;
162                 $url = "<a href=\"$BASE_URL/zones/edit/$name\">Edit</a>";
163                 $url2 = "<a href=\"$BASE_URL/zones/delete/$name\">Delete</a>";
164                 $desc = $var["description"];
165                 echo "<tr><td>$name</td><td>...</td><td>$desc</td><td>$url $url2</td></tr>";
166         }
167                 
168         echo "</table><br>";
169         echo "<form method=\"post\" action=\"$BASE_URL/zones/add\">";
170         echo "<hr><h3>Add Zone</h3><br>";
171         echo "Name: <input type=\"text\" name=\"toadd\"><br>";
172         echo "Description: <input type=\"text\" name=\"desc\"><br>";
173         echo "<input type=\"submit\" name=\"Add\" value=\"Add\">";
174         echo "</form>";
175         
176 }
177
178 function CBFWInterfacesPage($urls)
179 {
180         if(isset($urls[1])) {
181                 switch($urls[1]) {
182                         case "edit":
183                                 CBFWpageBuilder(null, "CBFWInterfacesPageEdit", null, null, $urls[2]);
184                                 break;
185                         case "change":
186                                 CBFWInterfacesChange();
187                                 break;
188                         default:
189                                 CBFWpageBuilder(null, "CBFWInterfacesPageDisplay");
190                 }
191         } else {
192                 error_log("main interface page");
193                 CBFWpageBuilder(null, "CBFWInterfacesPageDisplay");
194         }
195 }
196
197 function CBFWInterfacesChange()
198 {
199         global $BASE_URL, $MENU_ITEMS;
200         $comms = new Comms();
201         
202         $comms->sendMessage("interface eth0 changename poof");
203         header("Location: $BASE_URL/interfaces");
204 }
205
206 function CBFWInterfacesPageEdit($urls)
207 {
208         global $BASE_URL, $MENU_ITEMS;
209         $interface = $urls;
210         
211         echo "<h3>Edit Interface $interface</h3><br>";
212         echo "<form method=\"post\" action=\"$BASE_URL/interfaces/change/$interface\">";
213         echo "<table>";
214         echo "<tr><td>Name</td><td><input type=\"text\" name=\"name\"></td></tr>";
215         echo "<tr><td>HW Address</td><td><input type=\"text\" name=\"hwaddr\"></td></tr>";
216         echo "<tr><td>Zone</td><td><input type=\"text\" name=\"zone\"></td></tr>";
217         echo "<tr><td>IPv4 Address</td><td><input type=\"text\" name=\"ipv4addr\"></td></tr>";
218         echo "<tr><td>IPv6 Address</td><td><input type=\"text\" name=\"ipv6addr\"></td></tr>";
219         echo "<tr><td>Speed</td><td><input type=\"text\" name=\"speed\"></td></tr>";
220         echo "<tr><td>Duplex</td><td><input type=\"text\" name=\"duplex\"></td></tr>";
221         echo "</table>";
222         echo "<input type=\"submit\" name=\"Change\" value=\"Change\"><br>";
223         //echo "<a href=\"$BASE_URL/interfaces/\">Back</a>";
224         echo "</form>";
225 }
226
227 function CBFWInterfacesPageDisplay()
228 {
229         global $BASE_URL, $MENU_ITEMS;
230         $comms = new Comms();
231         
232         $config = $comms->getConfig(0);
233         $boot_config = $comms->getConfig(1);
234         
235         // now build an interface table
236         $table = array();
237         foreach($boot_config["hardware"]["netdev"] as $key=>$val) {
238                 $ename = $key;
239                 $table[$key]["realname"] = $val["name"];
240                 $table[$key]["name"] = $val["name"];
241                 
242                 
243                 // driver
244                 if(isset($val["driver"])) $table[$key]["type"] = $val["driver"];
245                 else $table[$key]["type"] = "Unknown";
246                 
247                 // hw address
248                 if(isset($val["hwaddress"])) $table[$key]["hwaddress"] = $val["hwaddress"];
249                 
250                 // current mtu
251                 if(isset($val["mtu"])) $table[$key]["mtu"] = $val["mtu"];
252         }
253         
254         foreach($config["hardware"]["netdev"] as $key=>$val) {
255                 $ename = $key;
256                 if(isset($config["interface"]["$key"]["name"])) $table[$key]["name"] = $config["interface"]["$key"]["name"];
257                 $table[$key]["address4"] = null;
258                 $table[$key]["address6"] = null;
259                 
260                 if(isset($config["interface"][$table[$key]["name"]]["address4"])) {
261                         $table[$key]["address4"] = $config["interface"][$table[$key]["name"]]["address4"];
262                 }
263                 
264                 if(isset($config["interface"][$table[$key]["name"]]["address6"])) {
265                         $table[$key]["address6"] = $config["interface"][$table[$key]["name"]]["address6"];
266                 }
267                 
268                 if(isset($config["interface"][$table[$key]["name"]]["mtu"])) {
269                         $table[$key]["mtu"] = $config["interface"][$table[$key]["name"]]["mtu"];
270                 } else {
271                         $table[$key]["mtu"] = "1500"; // TODO: this needs to actually come from somewhere
272                 }
273                 
274                 if(isset($config["interface"][$table[$key]["name"]]["speed"])) {
275                         $table[$key]["speed"] = $config["interface"][$table[$key]["name"]]["speed"];
276                 } else {
277                         $table[$key]["speed"] = null;
278                 }
279
280                 if(isset($config["interface"][$table[$key]["name"]]["duplex"])) {
281                         $table[$key]["duplex"] = $config["interface"][$table[$key]["name"]]["duplex"];
282                 } else {
283                         $table[$key]["duplex"] = null;
284                 }
285                 
286                 if(isset($config["interface"][$table[$key]["name"]]["zone"])) {
287                         $table[$key]["zone"] = $config["interface"][$table[$key]["name"]]["zone"];
288                 } else {
289                         $table[$key]["zone"] = null;
290                 }
291         }
292         
293         
294         echo "<table border=\"1\"><th>Device</th><th>Zone</th><th>Type</th><th>MAC Address</th><th>Address</th><th>MTU</th><th>Speed/Duplex</th><th>Control</th></tr>";
295         foreach($table as $key =>$val) {
296                 if($val["realname"] == $val["name"]) $name = $val["name"];
297                 else $name = $val["name"]." (".$val["realname"].")";
298                 
299                 // zone
300                 if($val["zone"] == null) $zone = "-";
301                 else $zone = $val["zone"];
302                 
303                 // mtu
304                 $mtu = $val["mtu"];
305                 
306                 // driver type
307                 $type = $val["type"];
308                 
309                 // hardware address
310                 $mac = $val["hwaddress"];
311                 
312                 // speed and duplex
313                 if($val["duplex"] == null) $dup = "default";
314                 else $dup = $val["duplex"];
315                 
316                 if($val["speed"] == null) $spd = "default";
317                 else $spd = $val["speed"];
318                 
319                 $spanddu = "$spd/$dup";
320                 
321                 // network address
322                 if($val["address4"]!=null && $val["address6"]!=null) {
323                         $address = $val["address4"]."<br>".$val["address6"];
324                 } else if($val["address4"] != null) {
325                         $address = $val["address4"];
326                 } else if($val["address6"] != null) {
327                         $address = $val["address6"];
328                 } else {
329                         $address = "-";
330                 }
331                 
332                 echo "<tr><th>$name</th><td>$zone</td><td>$type</td><td>$mac</td><td>$address</td><td>$mtu</td><td>$spanddu</td><td><a href=\"$BASE_URL/interfaces/edit/".$val["realname"]."\">Edit</a></td></tr>";
333         }
334         echo "</table>";
335 }
336
337 function CBFWObjectsPage($urls)
338 {
339         $comms = new Comms();
340         
341         $config = $comms->getConfig();
342         
343         CBFWpageBuilder(null, "CBFWObjectsDisplay");
344 }
345
346 function CBFWObjectsDisplay()
347 {
348         global $BASE_URL, $MENU_ITEMS;
349         $config = getWebUserConfig();
350         
351         echo "<h2>Objects</h2>";
352         
353         if(isset($config["objects"])) {
354                 echo "<table>";
355                 echo "<tr><th>Networks</th><th>Hosts</th><th>Services</th><th>Network Groups</th><th>Host Groups</th><th>Service Groups</th></tr>";
356                 echo "</table>";
357                 foreach($config["objects"] as $key => $var) {
358                 }
359         } else {
360                 echo "No objects set\n";
361         }
362         
363         echo "<hr>";
364         echo "<table cellpadding=\"20\"><tr valign=\"top\">";
365         echo "<td>";
366         echo "<h3>Create Network</h3><br>";
367         echo "<form method=\"post\" action=\"$BASE_URL/objects/network/create\">";
368         echo "Name <input type=\"text\" name=\"objname\"><br>";
369         echo "IPv4 <input type=\"text\" name=\"addr4\">/<input type=\"text\" name=\"mask4\" maxlength=\"3\" size=\"3\"><br>";
370         echo "IPv6 <input type=\"text\" name=\"addr6\">/<input type=\"text\" name=\"mask6\" maxlength=\"3\" size=\"3\"><br>";
371         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
372         echo "</form>";
373         echo "</td>";
374         echo "<td>";
375         echo "<h3>Create Host</h3><br>";
376         echo "<form method=\"post\" action=\"$BASE_URL/objects/host/create\">";
377         echo "Name <input type=\"text\" name=\"objname\"><br>";
378         echo "IPv4 <input type=\"text\" name=\"addr4\"><br>";
379         echo "IPv6 <input type=\"text\" name=\"addr6\"><br>";
380         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
381         echo "</form>";
382         echo "</td>";
383         echo "<td>";
384         echo "<h3>Service</h3><br>";
385         echo "<form method=\"post\" action=\"$BASE_URL/objects/host/create\">";
386         echo "Name <input type=\"text\" name=\"objname\"><br>";
387         echo "Port <input type=\"text\" name=\"port\">";
388         echo "<select name=\"proto\"><option value=\"tcp\">TCP</option><option value=\"udp\">UDP</option></select><br>";
389         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
390         echo "</form>";
391         echo "</td>";
392         echo "</tr><tr>";
393         echo "<td>";
394         echo "<h3>Network Group</h3>";
395         echo "<form method=\"post\" action=\"$BASE_URL/objects/networkgroup/create\">";
396         echo "Name <input type=\"text\" name=\"objname\"><br>";
397         echo "</form>";
398         echo "</td>";
399         echo "<td>";
400         echo "<h3>Host Group</h3>";
401         echo "<form method=\"post\" action=\"$BASE_URL/objects/hostgroup/create\">";
402         echo "Name <input type=\"text\" name=\"objname\"><br>";
403         echo "</form>";
404         echo "</td>";
405         echo "<td>";
406         echo "<h3>Service Group</h3>";
407         echo "<form method=\"post\" action=\"$BASE_URL/objects/servicegroup/create\">";
408         echo "Name <input type=\"text\" name=\"objname\"><br>";
409         echo "</form>";
410         echo "</td>";
411         
412         
413         echo "</tr></table>";
414         
415 }
416
417 function CBFWRulesPage()
418 {
419         $comms = new Comms();
420         
421         $config = $comms->getConfig();
422 }
423
424 function CBFWAdminPage($urls)
425 {
426         $comms = new Comms();
427         
428         $config = $comms->getConfig();
429         
430         CBFWpageBuilder(null, "CBFWAdminDisplay");
431 }
432
433 function CBFWAdminDisplay()
434 {
435 ?>
436 <h2>Administration</h2>
437 <table cellpadding=10>
438 <tr valign="top">
439 <td>
440 <h3>Users</h3>
441 <table>
442 <tr><th>Username</th><th>Description</th><th>Enabled?</th></tr>
443 <tr><td>Admin</td><td>Twattle</td><td>Yes</td></tr>
444 </table>
445 <hr>
446 <h3>Add User</h3>
447 <form method="post" action="asdf">
448 Username <input type="text" name="username"><br>
449 Password <input type="password" name="pass"><br>
450 Description <input type="text" name="desc"><br>
451 <input type="submit" name="add" value="Add"><br>
452 </form>
453 </td>
454 <td>
455 <h3>Network Access</h3>
456 <table>
457 <tr><th>From</th><th>Interface</th><th>Type</th></tr>
458 <tr><td>Any</td><td>twaddle</td><td>http,https,ssh</td></tr>
459 </table>
460 <hr>
461 <h3>Add Access</h3>
462 <form method="post" action="asdf">
463 From <input type="text" name="from"> <i>either "any", an address (1.2.3.4) or a network (1.2.3.4/23)</i><br>
464 Interface <select><option value="something">twad</option><option name="some">craw</option></select>
465 Protocols <input type="checkbox" name="http">HTTP <input type="checkbox" name="http">HTTPS <input type="checkbox" name="http">SSH<br> 
466 <input type="submit" name="add" value="Add"><br>
467 </form>
468 </td>
469 </tr>
470 </table>
471 <?php 
472 }
473
474
475
476 function findUrl($url_s)
477 {
478         global $URL_HANDLERS;
479         
480         foreach($URL_HANDLERS as $key => $val) {
481                 
482         }
483 }
484 ?>