bb2fc8e7b935e4d32d69d888f66744bf7b22ae5e
[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 CBFWInterfacesPage($urls)
196 {
197         if(isset($urls[1])) {
198                 switch($urls[1]) {
199                         case "edit":
200                                 CBFWpageBuilder(null, "CBFWInterfacesPageEdit", null, null, $urls[2]);
201                                 break;
202                         case "change":
203                                 CBFWInterfacesChange();
204                                 break;
205                         default:
206                                 CBFWpageBuilder(null, "CBFWInterfacesPageDisplay");
207                 }
208         } else {
209                 error_log("main interface page");
210                 CBFWpageBuilder(null, "CBFWInterfacesPageDisplay");
211         }
212 }
213
214 function CBFWInterfacesChange()
215 {
216         global $BASE_URL, $MENU_ITEMS;
217         $comms = new Comms();
218         
219         $comms->sendMessage("interface eth0 changename poof");
220         header("Location: $BASE_URL/interfaces");
221 }
222
223 function CBFWInterfacesPageEdit($urls)
224 {
225         global $BASE_URL, $MENU_ITEMS;
226         $interface = $urls;
227         
228         echo "<h3>Edit Interface $interface</h3><br>";
229         echo "<form method=\"post\" action=\"$BASE_URL/interfaces/change/$interface\">";
230         echo "<table>";
231         echo "<tr><td>Name</td><td><input type=\"text\" name=\"name\"></td></tr>";
232         echo "<tr><td>HW Address</td><td><input type=\"text\" name=\"hwaddr\"></td></tr>";
233         echo "<tr><td>Zone</td><td><input type=\"text\" name=\"zone\"></td></tr>";
234         echo "<tr><td>IPv4 Address</td><td><input type=\"text\" name=\"ipv4addr\"></td></tr>";
235         echo "<tr><td>IPv6 Address</td><td><input type=\"text\" name=\"ipv6addr\"></td></tr>";
236         echo "<tr><td>Speed</td><td><input type=\"text\" name=\"speed\"></td></tr>";
237         echo "<tr><td>Duplex</td><td><input type=\"text\" name=\"duplex\"></td></tr>";
238         echo "</table>";
239         echo "<input type=\"submit\" name=\"Change\" value=\"Change\"><br>";
240         //echo "<a href=\"$BASE_URL/interfaces/\">Back</a>";
241         echo "</form>";
242 }
243
244 function CBFWInterfacesPageDisplay()
245 {
246         global $BASE_URL, $MENU_ITEMS;
247         $comms = new Comms();
248         
249         $config = getWebUserConfig();
250         $boot_config = $comms->getConfig(1);
251         
252         // at the top, we put in the creation bits
253         echo "<h2>Create Interface</h2>";
254         echo "<li><a href=\"$BASE_URL/interfaces/create/bond\">Link Aggregation Group (bonded/teamed interfaces)</a><br>";
255         echo "<li><a href=\"$BASE_URL/interfaces/create/vlan\">VLAN Tagged Sub Interfaces</a><br>";
256         echo "<li><a href=\"$BASE_URL/interfaces/create/bridge\">Bridged Interfaces</a><br>";
257         echo "<hr>";
258         
259         // now build an interface table
260         $table = array();
261         foreach($boot_config["hardware"]["netdev"] as $key=>$val) {
262                 $ename = $key;
263                 $table[$key]["realname"] = $val["name"];
264                 $table[$key]["name"] = $val["name"];
265                 
266                 
267                 // driver
268                 if(isset($val["driver"])) $table[$key]["type"] = $val["driver"];
269                 else $table[$key]["type"] = "Unknown";
270                 
271                 // hw address
272                 if(isset($val["hwaddress"])) $table[$key]["hwaddress"] = $val["hwaddress"];
273                 
274                 // current mtu
275                 if(isset($val["mtu"])) $table[$key]["mtu"] = $val["mtu"];
276                 
277                 // speed
278                 if(isset($val["speed"])) $table[$key]["speed"] = $val["speed"];
279                 else $table[$key]["speed"] = "default";
280                 
281                 // duplex
282                 if(isset($val["duplex"])) $table[$key]["duplex"] = $val["duplex"];
283                 else $table[$key]["duplex"] = "default";
284         }
285         
286         foreach($config["interface"]["dev"] as $key => $val) {
287                 // TODO construction zone
288                 error_log("doing $key");
289                 if(isset($val["name"])) $table[$key]["name"] = $val["name"];
290                 if(isset($val["address4"])) $table[$key]["address4"] = $val["address4"];
291                 if(isset($val["address6"])) $table[$key]["address6"] = $val["address6"];
292                 if(isset($val["description"])) $table[$key]["description"] = $val["description"];
293                 if(isset($val["zone"])) $table[$key]["zone"] = $val["zone"];
294         }
295
296         
297         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>";
298         foreach($table as $key =>$val) {
299                 if($val["realname"] == $val["name"]) $name = $val["name"];
300                 else $name = $val["name"]." (".$val["realname"].")";
301                 
302                 // zone
303                 if(isset($val["zone"])) {
304                         if($val["zone"] == null) $zone = "-";
305                         else $zone = $val["zone"];
306                 } else $zone = "-";
307                 
308                 // mtu
309                 if(isset($val["mtu"])) {
310                         $mtu = $val["mtu"];
311                 } else $mtu = "-";
312                 
313                 // driver type
314                 if(isset($val["type"])) { 
315                         $type = $val["type"];
316                 } else $type = "-";
317                 
318                 // hardware address
319                 if(isset($val["hwaddress"])) {
320                         $mac = $val["hwaddress"];
321                 } else $mac = "-";
322                 
323                 // speed and duplex
324                 if(isset($val["duplex"])) {
325                         if($val["duplex"] == null) $dup = "default";
326                         else $dup = $val["duplex"];
327                 } //else $dup = "default"; // TODO: needs to go away in fav of hardware setting
328                 
329                 if(isset($val["speed"])) {
330                         if($val["speed"] == null) $spd = "default";
331                         else $spd = $val["speed"];
332                 } //else $spd = "default"; // TODO: needs to go away inf av of hardware setting
333                 
334                 $spanddu = "$spd/$dup";
335                 
336                 // network address
337                 if(isset($val["address4"]) && isset($val["address6"])) {
338                         $address = $val["address4"]."<br>".$val["address6"];
339                 } else if(isset($val["address4"])) {
340                         $address = $val["address4"];
341                 } else if(isset($val["address6"])) {
342                         $address = $val["address6"];
343                 } else {
344                         $address = "-";
345                 }
346                 
347                 if(isset($val["description"])) $desc = $val["description"];
348                 else $desc = "-";
349                 
350                 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>";
351         }
352         echo "</table>";
353         
354         // to test
355         echo "config: <pre>";
356         print_r($config);
357         echo "</pre>";
358 }
359
360 function CBFWObjectsPage($urls)
361 {
362         $comms = new Comms();
363         
364         $config = $comms->getConfig();
365         
366         CBFWpageBuilder(null, "CBFWObjectsDisplay");
367 }
368
369 function CBFWObjectsDisplay()
370 {
371         global $BASE_URL, $MENU_ITEMS;
372         $config = getWebUserConfig();
373         
374         echo "<h2>Objects</h2>";
375         
376         if(isset($config["objects"])) {
377                 echo "<table>";
378                 echo "<tr><th>Networks</th><th>Hosts</th><th>Services</th><th>Network Groups</th><th>Host Groups</th><th>Service Groups</th></tr>";
379                 echo "</table>";
380                 foreach($config["objects"] as $key => $var) {
381                 }
382         } else {
383                 echo "No objects set\n";
384         }
385         
386         echo "<hr>";
387         echo "<table cellpadding=\"20\"><tr valign=\"top\">";
388         echo "<td>";
389         echo "<h3>Create Network</h3><br>";
390         echo "<form method=\"post\" action=\"$BASE_URL/objects/network/create\">";
391         echo "Name <input type=\"text\" name=\"objname\"><br>";
392         echo "IPv4 <input type=\"text\" name=\"addr4\">/<input type=\"text\" name=\"mask4\" maxlength=\"3\" size=\"3\"><br>";
393         echo "IPv6 <input type=\"text\" name=\"addr6\">/<input type=\"text\" name=\"mask6\" maxlength=\"3\" size=\"3\"><br>";
394         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
395         echo "</form>";
396         echo "</td>";
397         echo "<td>";
398         echo "<h3>Create Host</h3><br>";
399         echo "<form method=\"post\" action=\"$BASE_URL/objects/host/create\">";
400         echo "Name <input type=\"text\" name=\"objname\"><br>";
401         echo "IPv4 <input type=\"text\" name=\"addr4\"><br>";
402         echo "IPv6 <input type=\"text\" name=\"addr6\"><br>";
403         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
404         echo "</form>";
405         echo "</td>";
406         echo "<td>";
407         echo "<h3>Service</h3><br>";
408         echo "<form method=\"post\" action=\"$BASE_URL/objects/host/create\">";
409         echo "Name <input type=\"text\" name=\"objname\"><br>";
410         echo "Port <input type=\"text\" name=\"port\">";
411         echo "<select name=\"proto\"><option value=\"tcp\">TCP</option><option value=\"udp\">UDP</option></select><br>";
412         echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
413         echo "</form>";
414         echo "</td>";
415         echo "</tr><tr>";
416         echo "<td>";
417         echo "<h3>Network Group</h3>";
418         echo "<form method=\"post\" action=\"$BASE_URL/objects/networkgroup/create\">";
419         echo "Name <input type=\"text\" name=\"objname\"><br>";
420         echo "</form>";
421         echo "</td>";
422         echo "<td>";
423         echo "<h3>Host Group</h3>";
424         echo "<form method=\"post\" action=\"$BASE_URL/objects/hostgroup/create\">";
425         echo "Name <input type=\"text\" name=\"objname\"><br>";
426         echo "</form>";
427         echo "</td>";
428         echo "<td>";
429         echo "<h3>Service Group</h3>";
430         echo "<form method=\"post\" action=\"$BASE_URL/objects/servicegroup/create\">";
431         echo "Name <input type=\"text\" name=\"objname\"><br>";
432         echo "</form>";
433         echo "</td>";
434         
435         
436         echo "</tr></table>";
437         
438 }
439
440 function CBFWChangesPage($urls)
441 {
442         global $BASE_URL, $MENU_ITEMS;
443         
444         if(isset($urls[1])) {
445                 switch($urls[1]) {
446                         case "show":
447                                 CBFWpageBuilder(null, "CBFWChangesDisplay");
448                                 break;
449                         case "delete":
450                                 $delete = $urls[2];
451                                 error_log("would delete change $delete");
452                                 header("Location: $BASE_URL/changes/show");
453                                 break;
454                 }
455         }
456 }
457
458 function CBFWChangesDisplay()
459 {
460         global $BASE_URL, $MENU_ITEMS;
461         $comms = new Comms();
462         
463         $config = $comms->getConfig(3);
464         
465         echo "<h2>Changes</h2>";
466         echo "This page shows the changes on the web from the currently running configuration<br>";
467         if($config["status"] == "nochange") {
468                 echo "There are currently no changes from the running configuraiton.";
469         } else {
470                 echo "<table border=\"1\">";
471                 echo "<tr><th>Change No</th><th>Change</th><th>Control</th></tr>";
472                 foreach($config["changes"] as $key => $var) {
473                         echo "<tr><td>$key</td><td>$var</td><td><a href=\"$BASE_URL/changes/delete/$key\">Delete</a></td></tr>";
474                 }
475                 echo "</table>";
476         }
477 }
478
479 function CBFWRulesPage()
480 {
481         $comms = new Comms();
482         
483         $config = $comms->getConfig();
484 }
485
486 function CBFWAdminPage($urls)
487 {
488         $comms = new Comms();
489         
490         $config = $comms->getConfig();
491         
492         CBFWpageBuilder(null, "CBFWAdminDisplay");
493 }
494
495 function CBFWAdminDisplay()
496 {
497 ?>
498 <h2>Administration</h2>
499 <table cellpadding=10>
500 <tr valign="top">
501 <td>
502 <h3>Users</h3>
503 <table>
504 <tr><th>Username</th><th>Description</th><th>Enabled?</th></tr>
505 <tr><td>Admin</td><td>Twattle</td><td>Yes</td></tr>
506 </table>
507 <hr>
508 <h3>Add User</h3>
509 <form method="post" action="asdf">
510 Username <input type="text" name="username"><br>
511 Password <input type="password" name="pass"><br>
512 Description <input type="text" name="desc"><br>
513 <input type="submit" name="add" value="Add"><br>
514 </form>
515 </td>
516 <td>
517 <h3>Network Access</h3>
518 <table>
519 <tr><th>From</th><th>Interface</th><th>Type</th></tr>
520 <tr><td>Any</td><td>twaddle</td><td>http,https,ssh</td></tr>
521 </table>
522 <hr>
523 <h3>Add Access</h3>
524 <form method="post" action="asdf">
525 From <select name="from"><option value="something">Any</option><option name="some">Intranet</option><option name="som">SomeHost</option></select><br>
526 Interface <select name="interface"><option value="something">twad</option><option name="some">craw</option></select>
527 Protocols <input type="checkbox" name="http">HTTP <input type="checkbox" name="http">HTTPS <input type="checkbox" name="http">SSH<br> 
528 <input type="submit" name="add" value="Add"><br>
529 </form>
530 </td>
531 </tr>
532 </table>
533 <?php 
534 }
535
536
537
538 function findUrl($url_s)
539 {
540         global $URL_HANDLERS;
541         
542         foreach($URL_HANDLERS as $key => $val) {
543                 
544         }
545 }
546 ?>