adc95416a886aa96ab5a9c21287fe547ba1c89eb
[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                                 addWebUserChange("delete zone name $zonetodelete");
63                                 header("Location: $BASE_URL/zones");
64                                 break;
65                                 
66                                 
67                         case "add":
68                                 
69                                 // UGLY
70                                 $nametoadd = $_REQUEST["toadd"];
71                                 $desc = $_REQUEST["desc"];
72                                 
73                                 $descbit = "";
74                                 if($desc != "") $descbit = " # $desc";
75                                 if($nametoadd == "" || $nametoadd == null) {
76                                         return;
77                                 }
78                                 
79                                 addWebUserChange("zone name $nametoadd $descbit");
80                                 header("Location: $BASE_URL/zones");
81                                 break;
82                                 
83                                 
84                         default:
85                                 CBFWpageBuilder(null, "CBFWZonesPageDisplay");
86                 }
87         } else {
88                 CBFWpageBuilder(null, "CBFWZonesPageDisplay");
89         }
90 }
91
92 function CBFWZonesEditPage($urls)
93 {
94         global $BASE_URL, $MENU_ITEMS;
95         
96         $zone = $urls[2];
97         
98         $config = getWebUserConfig();
99         
100         
101         echo "<h2>Edit Zone $zone</h2>";
102         echo "<form method=\"post\" action=\"$BASE_URL/zones/modify/$zone\">";
103         echo "<input type=\"hidden\" name=\"oldname\" value=\"$zone\">";
104         echo "Name: <input type=\"text\" name=\"zonename\" value=\"$zone\"><br>";
105         
106         if(isset($config["zone"][$zone]["description"])) $desc = $config["zone"][$zone]["description"];
107         
108         echo "Description: <input type=\"text\" name=\"description\" value=\"$desc\"><br>";
109         echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
110         echo "</form>";
111 }
112
113 function addWebUserChange($change)
114 {
115         $comms = new Comms();
116         $myconf = new Config();
117         
118         $conf = $comms->getConfig(3);
119         
120         if(!isset($conf["status"])) {
121                 $conf["status"] = "nochange";
122         }
123         
124         $changes = $conf["status"];
125         if($changes == "nochange") $conf["status"] = 1;
126         else $conf["status"] = $changes + 1;
127         $cnum = $conf["status"];
128         
129         $conf["changes"][$cnum] = "$change";
130         $comms->putConfig($conf, 3);
131         
132 }
133
134 // TODO: we need to really sit and think about this one
135 function getWebUserConfig()
136 {
137         $comms = new Comms();
138         $myconf = new Config();
139         
140         
141         // check to see if pre-cache config matches change level of config change buffer
142         $config = $comms->getConfig(5);
143         $conf2 = $comms->getConfig(3);
144         if(isset($config["status"])) {
145                 if($config["status"] == $conf2["status"]) {
146                         // it did, return the pre-cache
147                         error_log("got pre-cache config");
148                         return $config;
149                 }
150         }
151         
152         // it did not, do a new merge and cache
153         // TODO: need to do this part
154         $config = $comms->getConfig();
155         $reconfig = $config;
156         $myconf->setConfig($config);
157         if(isset($conf2["changes"])) foreach($conf2["changes"] as $key => $var) {
158                 error_log("Adding config line $key, $var");
159                 $myconf->addConfigLine($var);
160         }
161         $reconfig = $myconf->getConfig();
162         $reconfig["status"] = $conf2["status"];
163         error_log("put pre-cache config");
164         $comms->putConfig($reconfig, 5);        
165         
166         return $reconfig;
167 }
168
169 function CBFWZonesPageDisplay()
170 {
171         global $BASE_URL, $MENU_ITEMS;
172         
173         $config = getWebUserConfig();
174
175         echo "<h2>Zones</h2>";
176         echo "<table border=\"1\"><tr><th>Zone</th><th>Interfaces</th><th>Description</th><th>Edit</th></tr>";
177         foreach($config["zone"] as $key => $var) {
178                 $name = $key;
179                 $url = "<a href=\"$BASE_URL/zones/edit/$name\">Edit</a>";
180                 $url2 = "<a href=\"$BASE_URL/zones/delete/$name\">Delete</a>";
181                 $desc = $var["description"];
182                 echo "<tr><td>$name</td><td>...</td><td>$desc</td><td>$url $url2</td></tr>";
183         }
184                 
185         echo "</table><br>";
186         echo "<form method=\"post\" action=\"$BASE_URL/zones/add\">";
187         echo "<hr><h3>Add Zone</h3><br>";
188         echo "Name: <input type=\"text\" name=\"toadd\"><br>";
189         echo "Description: <input type=\"text\" name=\"desc\"><br>";
190         echo "<input type=\"submit\" name=\"Add\" value=\"Add\">";
191         echo "</form>";
192         
193 }
194
195 function CBFWRoutingPage($urls)
196 {
197         if(isset($urls[1])) {
198                 switch($urls[1]) {
199                         case "edit":
200                                 CBFWpageBuilder(null, "CBFWRoutingPageEdit", null, null, $urls[2]);
201                                 break;
202                         case "change":
203                                 CBFWRoutingChange();
204                                 break;
205                         default:
206                                 CBFWpageBuilder(null, "CBFWRoutingPageDisplay");
207                 }
208         } else {
209                 error_log("main interface page");
210                 CBFWpageBuilder(null, "CBFWRoutingPageDisplay");
211         }
212         
213 }
214
215 function CBFWRoutingPageDisplay()
216 {
217         echo "<h2>Routing</h2>";
218 }
219
220 function CBFWInterfacesPage($urls)
221 {
222         if(isset($urls[1])) {
223                 switch($urls[1]) {
224                         case "edit":
225                                 CBFWpageBuilder(null, "CBFWInterfacesPageEdit", null, null, $urls[2]);
226                                 break;
227                         case "change":
228                                 CBFWInterfacesChange();
229                                 break;
230                         default:
231                                 CBFWpageBuilder(null, "CBFWInterfacesPageDisplay");
232                 }
233         } else {
234                 error_log("main interface page");
235                 CBFWpageBuilder(null, "CBFWInterfacesPageDisplay");
236         }
237 }
238
239 function CBFWInterfacesChange()
240 {
241         global $BASE_URL, $MENU_ITEMS;
242         $comms = new Comms();
243         
244         header("Location: $BASE_URL/interfaces");
245 }
246
247 function CBFWInterfacesPageEdit($urls)
248 {
249         global $BASE_URL, $MENU_ITEMS;
250         $interface = $urls;
251         
252         $comms = new Comms();
253         
254         $config = getWebUserConfig();
255         $boot_config = $comms->getConfig(1);
256         
257         if(isset($config["interface"]["dev"]["$interface"]["name"])) $name = $config["interface"]["dev"]["$interface"]["name"];
258         else $name = "";
259         
260         $extra2 = "";
261         if(file_exists("/sys/class/net/$interface/address")) {
262                 $extra2 = trim(file_get_contents("/sys/class/net/$interface/address"));
263         }
264         
265         $extra = "";
266         if(isset($boot_config["hardware"]["netdev"]["$interface"]["hwaddress"])) {
267                 $hwaddr = $boot_config["hardware"]["netdev"]["$interface"]["hwaddress"];
268                 $extra = " <i>Original address: $hwaddr</i>";
269                 if($hwaddr != $extra2 && $extra2 != "") {
270                         $extra .= " <i>Current Address: $hwaddr</i>";
271                 }
272         }
273         if(isset($config["interface"]["dev"]["$interface"]["hwaddress"])) $hwaddr = $config["interface"]["dev"]["$interface"]["hwaddress"];
274         
275         if(isset($config["interface"]["dev"]["$interface"]["zone"])) $zone = $config["interface"]["dev"]["$interface"]["zone"];
276         
277         if(isset($config["interface"]["dev"]["$interface"]["address4"])) $ip4 = $config["interface"]["dev"]["$interface"]["address4"];
278         
279         if(isset($config["interface"]["dev"]["$interface"]["address6"])) $ip6 = $config["interface"]["dev"]["$interface"]["address6"];
280         
281         if(isset($boot_config["hardware"]["netdev"]["$interface"]["speed"])) $speed = $boot_config["hardware"]["netdev"]["$interface"]["speed"];
282         if(isset($boot_config["hardware"]["netdev"]["$interface"]["duplex"])) $duplex = $boot_config["hardware"]["netdev"]["$interface"]["duplex"];
283         if(isset($config["interface"]["dev"]["$interface"]["speed"])) $speed = $config["interface"]["dev"]["$interface"]["speed"];
284         if(isset($config["interface"]["dev"]["$interface"]["duplex"])) $duplex = $config["interface"]["dev"]["$interface"]["duplex"];
285         
286         echo "<h3>Edit Interface $interface</h3><br>";
287         echo "<form method=\"post\" action=\"$BASE_URL/interfaces/change/$interface\">";
288         echo "<table>";
289         echo "<tr><td>Name</td><td><input type=\"text\" name=\"name\" value=\"$name\"></td></tr>";
290         echo "<tr><td>HW Address</td><td><input type=\"text\" name=\"hwaddr\" value=\"$hwaddr\">$extra</td></tr>";
291         echo "<tr><td>Zone</td><td><input type=\"text\" name=\"zone\" value=\"$zone\"></td></tr>";
292         echo "<tr><td>IPv4 Address</td><td><input type=\"text\" name=\"ipv4addr\" value=\"$ip4\"></td></tr>";
293         echo "<tr><td>IPv6 Address</td><td><input type=\"text\" name=\"ipv6addr\" value=\"$ip6\"></td></tr>";
294         echo "<tr><td>Speed</td><td><select name=\"duplex\"><option value=\"auto\">Auto</option><option value=\"10\">10</option>";
295         echo "<option value=\"100\">100</option><option value=\"1000\">1000</option><option value=\"10000\">10000</option></select></td></tr>";
296         echo "<tr><td>Duplex</td><td><select name=\"duplex\"><option value=\"auto\">Auto</option><option value=\"full\">Full</option><option value=\"half\">Half</option></select></td></tr>";
297         echo "</table>";
298         echo "<input type=\"submit\" name=\"Change\" value=\"Change\"><br>";
299         //echo "<a href=\"$BASE_URL/interfaces/\">Back</a>";
300         echo "</form>";
301 }
302
303 function CBFWInterfacesPageDisplay()
304 {
305         global $BASE_URL, $MENU_ITEMS;
306         $comms = new Comms();
307         
308         $config = getWebUserConfig();
309         $boot_config = $comms->getConfig(1);
310         
311         // at the top, we put in the creation bits
312         echo "<h2>Create Interface</h2>";
313         echo "<li><a href=\"$BASE_URL/interfaces/create/bond\">Link Aggregation Group (bonded/teamed interfaces)</a><br>";
314         echo "<li><a href=\"$BASE_URL/interfaces/create/vlan\">VLAN Tagged Sub Interfaces</a><br>";
315         echo "<li><a href=\"$BASE_URL/interfaces/create/bridge\">Bridged Interfaces</a><br>";
316         echo "<hr>";
317         
318         // now build an interface table
319         $table = array();
320         foreach($boot_config["hardware"]["netdev"] as $key=>$val) {
321                 $ename = $key;
322                 $table[$key]["realname"] = $val["name"];
323                 $table[$key]["name"] = $val["name"];
324                 
325                 
326                 // driver
327                 if(isset($val["driver"])) $table[$key]["type"] = $val["driver"];
328                 else $table[$key]["type"] = "Unknown";
329                 
330                 // hw address
331                 if(isset($val["hwaddress"])) $table[$key]["hwaddress"] = $val["hwaddress"];
332                 
333                 // current mtu
334                 if(isset($val["mtu"])) $table[$key]["mtu"] = $val["mtu"];
335                 
336                 // speed
337                 if(isset($val["speed"])) $table[$key]["speed"] = $val["speed"];
338                 else $table[$key]["speed"] = "default";
339                 
340                 // duplex
341                 if(isset($val["duplex"])) $table[$key]["duplex"] = $val["duplex"];
342                 else $table[$key]["duplex"] = "default";
343         }
344         
345         foreach($config["interface"]["dev"] as $key => $val) {
346                 // TODO construction zone
347                 error_log("doing $key");
348                 if(isset($val["name"])) $table[$key]["name"] = $val["name"];
349                 if(isset($val["address4"])) $table[$key]["address4"] = $val["address4"];
350                 if(isset($val["address6"])) $table[$key]["address6"] = $val["address6"];
351                 if(isset($val["description"])) $table[$key]["description"] = $val["description"];
352                 if(isset($val["zone"])) $table[$key]["zone"] = $val["zone"];
353         }
354
355         
356         echo "<h2>Interfaces</h2><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>Description</th><th>Control</th></tr>";
357         foreach($table as $key =>$val) {
358                 if($val["realname"] == $val["name"]) $name = $val["name"];
359                 else $name = $val["name"]." (".$val["realname"].")";
360                 
361                 // zone
362                 if(isset($val["zone"])) {
363                         if($val["zone"] == null) $zone = "-";
364                         else $zone = $val["zone"];
365                 } else $zone = "-";
366                 
367                 // mtu
368                 if(isset($val["mtu"])) {
369                         $mtu = $val["mtu"];
370                 } else $mtu = "-";
371                 
372                 // driver type
373                 if(isset($val["type"])) { 
374                         $type = $val["type"];
375                 } else $type = "-";
376                 
377                 // hardware address
378                 if(isset($val["hwaddress"])) {
379                         $mac = $val["hwaddress"];
380                 } else $mac = "-";
381                 
382                 // speed and duplex
383                 if(isset($val["duplex"])) {
384                         if($val["duplex"] == null) $dup = "default";
385                         else $dup = $val["duplex"];
386                 } //else $dup = "default"; // TODO: needs to go away in fav of hardware setting
387                 
388                 if(isset($val["speed"])) {
389                         if($val["speed"] == null) $spd = "default";
390                         else $spd = $val["speed"];
391                 } //else $spd = "default"; // TODO: needs to go away inf av of hardware setting
392                 
393                 $spanddu = "$spd/$dup";
394                 
395                 // network address
396                 if(isset($val["address4"]) && isset($val["address6"])) {
397                         $address = $val["address4"]."<br>".$val["address6"];
398                 } else if(isset($val["address4"])) {
399                         $address = $val["address4"];
400                 } else if(isset($val["address6"])) {
401                         $address = $val["address6"];
402                 } else {
403                         $address = "-";
404                 }
405                 
406                 if(isset($val["description"])) $desc = $val["description"];
407                 else $desc = "-";
408                 
409                 echo "<tr><th>$name</th><td>$zone</td><td>$type</td><td>$mac</td><td>$address</td><td>$mtu</td><td>$spanddu</td><td>$desc</td><td><a href=\"$BASE_URL/interfaces/edit/".$val["realname"]."\">Edit</a></td></tr>";
410         }
411         echo "</table>";
412         
413         // to test
414         echo "config: <pre>";
415         print_r($config);
416         echo "</pre>";
417 }
418
419 function CBFWObjectsPage($urls)
420 {
421         $comms = new Comms();
422         
423         $config = $comms->getConfig();
424         
425         CBFWpageBuilder(null, "CBFWObjectsDisplay");
426 }
427
428 function CBFWObjectsDisplay()
429 {
430         global $BASE_URL, $MENU_ITEMS;
431         $config = getWebUserConfig();
432         
433         echo "<h2>Objects</h2>";
434         
435         if(isset($config["objects"])) {
436                 echo "<table>";
437                 echo "<tr><th>Networks</th><th>Hosts</th><th>Services</th><th>Network Groups</th><th>Host Groups</th><th>Service Groups</th></tr>";
438                 echo "</table>";
439                 foreach($config["objects"] as $key => $var) {
440                 }
441         } else {
442                 echo "No objects set\n";
443         }
444         
445         echo "<hr>";
446         echo "<table cellpadding=\"20\"><tr valign=\"top\">";
447         echo "<td>";
448         echo "<h3>Create Network</h3><br>";
449         echo "<form method=\"post\" action=\"$BASE_URL/objects/network/create\">";
450         echo "Name <input type=\"text\" name=\"objname\"><br>";
451         echo "IPv4 <input type=\"text\" name=\"addr4\">/<input type=\"text\" name=\"mask4\" maxlength=\"3\" size=\"3\"><br>";
452         echo "IPv6 <input type=\"text\" name=\"addr6\">/<input type=\"text\" name=\"mask6\" maxlength=\"3\" size=\"3\"><br>";
453         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
454         echo "</form>";
455         echo "</td>";
456         echo "<td>";
457         echo "<h3>Create Host</h3><br>";
458         echo "<form method=\"post\" action=\"$BASE_URL/objects/host/create\">";
459         echo "Name <input type=\"text\" name=\"objname\"><br>";
460         echo "IPv4 <input type=\"text\" name=\"addr4\"><br>";
461         echo "IPv6 <input type=\"text\" name=\"addr6\"><br>";
462         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
463         echo "</form>";
464         echo "</td>";
465         echo "<td>";
466         echo "<h3>Service</h3><br>";
467         echo "<form method=\"post\" action=\"$BASE_URL/objects/host/create\">";
468         echo "Name <input type=\"text\" name=\"objname\"><br>";
469         echo "Port <input type=\"text\" name=\"port\">";
470         echo "<select name=\"proto\"><option value=\"tcp\">TCP</option><option value=\"udp\">UDP</option></select><br>";
471         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
472         echo "</form>";
473         echo "</td>";
474         echo "</tr><tr>";
475         echo "<td>";
476         echo "<h3>Network Group</h3>";
477         echo "<form method=\"post\" action=\"$BASE_URL/objects/networkgroup/create\">";
478         echo "Name <input type=\"text\" name=\"objname\"><br>";
479         echo "</form>";
480         echo "</td>";
481         echo "<td>";
482         echo "<h3>Host Group</h3>";
483         echo "<form method=\"post\" action=\"$BASE_URL/objects/hostgroup/create\">";
484         echo "Name <input type=\"text\" name=\"objname\"><br>";
485         echo "</form>";
486         echo "</td>";
487         echo "<td>";
488         echo "<h3>Service Group</h3>";
489         echo "<form method=\"post\" action=\"$BASE_URL/objects/servicegroup/create\">";
490         echo "Name <input type=\"text\" name=\"objname\"><br>";
491         echo "</form>";
492         echo "</td>";
493         
494         
495         echo "</tr></table>";
496         
497 }
498
499 function CBFWChangesPage($urls)
500 {
501         global $BASE_URL, $MENU_ITEMS;
502         
503         if(isset($urls[1])) {
504                 switch($urls[1]) {
505                         case "show":
506                                 CBFWpageBuilder(null, "CBFWChangesDisplay");
507                                 break;
508                         case "delete":
509                                 $delete = $urls[2];
510                                 error_log("would delete change $delete");
511                                 header("Location: $BASE_URL/changes/show");
512                                 break;
513                 }
514         }
515 }
516
517 function CBFWChangesDisplay()
518 {
519         global $BASE_URL, $MENU_ITEMS;
520         $comms = new Comms();
521         
522         $config = $comms->getConfig(3);
523         
524         echo "<h2>Changes</h2>";
525         echo "This page shows the changes on the web from the currently running configuration<br>";
526         if($config["status"] == "nochange") {
527                 echo "There are currently no changes from the running configuraiton.";
528         } else {
529                 echo "<table border=\"1\">";
530                 echo "<tr><th>Change No</th><th>Change</th><th>Control</th></tr>";
531                 foreach($config["changes"] as $key => $var) {
532                         echo "<tr><td>$key</td><td>$var</td><td><a href=\"$BASE_URL/changes/delete/$key\">Delete</a></td></tr>";
533                 }
534                 echo "</table>";
535         }
536 }
537
538 function CBFWRulesPage()
539 {
540         $comms = new Comms();
541         
542         $config = $comms->getConfig();
543 }
544
545 function CBFWAdminPage($urls)
546 {
547         $comms = new Comms();
548         
549         $config = $comms->getConfig();
550         
551         CBFWpageBuilder(null, "CBFWAdminDisplay");
552 }
553
554 function CBFWAdminDisplay()
555 {
556 ?>
557 <h2>Administration</h2>
558 <table cellpadding=10>
559 <tr valign="top">
560 <td>
561 <h3>Users</h3>
562 <table>
563 <tr><th>Username</th><th>Description</th><th>Enabled?</th></tr>
564 <tr><td>Admin</td><td>Twattle</td><td>Yes</td></tr>
565 </table>
566 <hr>
567 <h3>Add User</h3>
568 <form method="post" action="asdf">
569 Username <input type="text" name="username"><br>
570 Password <input type="password" name="pass"><br>
571 Description <input type="text" name="desc"><br>
572 <input type="submit" name="add" value="Add"><br>
573 </form>
574 </td>
575 <td>
576 <h3>Network Access</h3>
577 <table>
578 <tr><th>From</th><th>Interface</th><th>Type</th></tr>
579 <tr><td>Any</td><td>twaddle</td><td>http,https,ssh</td></tr>
580 </table>
581 <hr>
582 <h3>Add Access</h3>
583 <form method="post" action="asdf">
584 From <select name="from"><option value="something">Any</option><option name="some">Intranet</option><option name="som">SomeHost</option></select><br>
585 Interface <select name="interface"><option value="something">twad</option><option name="some">craw</option></select>
586 Protocols <input type="checkbox" name="http">HTTP <input type="checkbox" name="http">HTTPS <input type="checkbox" name="http">SSH<br> 
587 <input type="submit" name="add" value="Add"><br>
588 </form>
589 </td>
590 </tr>
591 </table>
592 <?php 
593 }
594
595
596
597 function findUrl($url_s)
598 {
599         global $URL_HANDLERS;
600         
601         foreach($URL_HANDLERS as $key => $val) {
602                 
603         }
604 }
605 ?>