7f76f1109c52b04e1afbf78a7fca4b8c764aaf3e
[CBFWR.git] / libcbfwr / web.php
1 <?php
2 $MENU_ITEMS["15_Zones"]["link"] = "$BASE_URL/zones"; 
3 $MENU_ITEMS["15_Zones"]["name"] = "Zones"; 
4 $MENU_ITEMS["20_Objects"]["link"] = "$BASE_URL/objects"; 
5 $MENU_ITEMS["20_Objects"]["name"] = "Objects"; 
6 $MENU_ITEMS["30_Rules"]["link"] = "$BASE_URL/rules"; 
7 $MENU_ITEMS["30_Rules"]["name"] = "Rules"; 
8 $MENU_ITEMS["15_Routes"]["link"] = "$BASE_URL/routing"; 
9 $MENU_ITEMS["15_Routes"]["name"] = "Routing"; 
10 $MENU_ITEMS["10_Interfaces"]["link"] = "$BASE_URL/interfaces"; 
11 $MENU_ITEMS["10_Interfaces"]["name"] = "Interfaces"; 
12 $MENU_ITEMS["05_Admin"]["link"] = "$BASE_URL/admin"; 
13 $MENU_ITEMS["05_Admin"]["name"] = "Admin"; 
14
15 // if i believed in name spacing in php, i'd use it.
16 error_log("cbfwweb loaded");
17
18 class CBFWWeb {
19         
20         function __construct()
21         {
22
23         }
24         
25         function go()
26         {
27                 $url = "/";
28                 $bodycontent = null;
29                 
30                 $conf = new Config();
31                 $comms = new Comms(); 
32                 $conf->loadConfig($comms->getConfig(0));
33                 $rconfig = $conf->getConfig();
34                 
35                 if($rconfig["status"] != "conf") {
36                         if($rconfig["status"] == "nodir") {
37                                 cbfw_startnodir();
38                                 return 0;
39                         }
40                         cbfw_startinstaller();
41                         return 0;
42                 }
43                 
44                 
45                 if(isset($_REQUEST["q"])) {
46                         $url = $_REQUEST["q"];
47                         
48                         // ok, now we try and find the basics
49                         $url_s = explode("/", $url);
50                         
51                         $bodyFunction = null;
52                         switch($url_s[0]) {
53                                 case "zones":
54                                         CBFWZonesPage($url_s);
55                                         break;
56                                 case "interfaces":
57                                         CBFWInterfacesPage($url_s);
58                                         break;
59                                 case "objects":
60                                         CBFWObjectsPage($url_s);
61                                         break;
62                                 case "routing":
63                                         CBFWRoutingPage($url_s);
64                                         break;
65                                 case "rules":
66                                         $bodyFunction = "CBFWRulesPage";
67                                         break;
68                                 case "changes":
69                                         CBFWChangesPage($url_s);
70                                         break;
71                                 case "admin":
72                                         CBFWAdminPage($url_s);
73                                         break;
74                                 default:
75                                         $bodyFunction = findUrl($url_s);
76                         }
77                 } else {
78                         $bodyFunction = "CBFBuildHomePage";
79                 }
80                 if($bodyFunction != null) CBFWpageBuilder(null, $bodyFunction);
81         }       
82 }
83
84
85 function CBFBuildHomePage($urls)
86 {
87         echo "Must remember this, gotta rules could apply to multiple zones not just one<br>";
88         echo "i.e.: add rule reject from object/host/hostname to address6/2003::123 in zones Zone/zonename/rulenum Zone/zonename/rulenum";
89         
90         $comms = new Comms();
91         
92         
93         CBFWAddMessage(rand(1,6), "i am a message");
94         echo "<pre>";
95         print_r($comms->getConfig(0));
96         echo "\n\n\n";
97         print_r($comms->getConfig(1));
98         echo "\n\n\n";
99         print_r($comms->getConfig(2));
100         echo "\n\n\n";
101         print_r($comms->getConfig(3));
102         echo "\n\n\n";
103         print_r($comms->getConfig(4));
104         echo "\n\n\n";
105         print_r($comms->getConfig(5));
106         echo "</pre>";
107         
108 }
109
110 function CBFWMenuBuilder()
111 {
112         global $BASE_URL, $MENU_ITEMS;
113         
114         ksort($MENU_ITEMS);
115         ?>
116 <table border="1"><tr><th>Menu</th><td>
117 <a href="<?php echo $BASE_URL ?>">Home</a></td>
118         <?php
119         foreach($MENU_ITEMS as $key => $val) {
120                 $link = $val["link"];
121                 $name = $val["name"];
122                 echo "<td><a href=$link>$name</a></td>";
123         }
124         
125         echo "</table>";
126 }
127
128 // just a short one to call the long one
129 function gcam($m, $s)
130 {
131         global $glconfig;
132         CBFWAddMessage($m, $s);
133 }
134
135 function CBFWAddMessage($messagelev, $message)
136 {
137         $comms = new Comms();
138         $msgs = $comms->getConfig(6);
139         $cnum = 0;
140         if($msgs != false) {
141                 foreach($msgs as $key=>$val) {
142                         if($key >= $cnum) $cnum = $key+1;
143                 }
144         } else {
145                 $msgs = array();
146         }
147         $msgs[$cnum]["timestamp"] = time();
148         $msgs[$cnum]["level"] = $messagelev;
149         $msgs[$cnum]["message"] = $message;
150         
151         $comms->putConfig($msgs, 6);
152 }
153
154 function CBFWMessageBuilder()
155 {
156         // show only 10 messages on this page
157         $maxshow = 6;
158         
159         $comms = new Comms();
160         
161         $msgs = $comms->getConfig(6);
162         if($msgs != false) {
163                 krsort($msgs);
164         }
165         $show = 0;
166         if(count($msgs) > 0) {
167                 echo "<table>";
168                 foreach($msgs as $key => $val) {
169                         $datetime = strftime("%c", $val["timestamp"]);
170                         $sev = $val["level"];
171                         $message = $val["message"];
172                         
173                         $sevcolour = "#ddffdd";
174                         switch($sev) {
175                                 case "1":
176                                         $sevcolour = "#ffaaaa";
177                                         break;
178                                 case "2":
179                                         $sevcolour = "#ffdddd";
180                                         break;
181                                 case "3":
182                                         $sevcolour = "#ffffdd";
183                                         break;
184                                 case "4":
185                                         $sevcolour = "#ddffdd";
186                                         break;
187                                 case "5":
188                                         $sevcolour = "#aaffaa";
189                                         break;
190                                 case "6":
191                                         $sevcolour = "#ccaaaa";
192                                         break;
193                         }
194                         
195                         echo "<tr bgcolor=\"$sevcolour\"><td><i>$datetime</i></td><td>$message ($sev)</td></tr>";
196                         $show++;
197                         if($show >= $maxshow) break;
198                 }
199                 echo "</table>";
200         }
201 }
202
203 function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="CBFW", $bodyfunctiondata=null)
204 {
205         global $WEB_ROOT_FS, $BASE_URL;
206         
207         // TODO: load css
208         // header
209         echo "<html><head><title>$title</title>";
210         
211         // load css
212         if(file_exists("$WEB_ROOT_FS/css")) {
213                 $dh = opendir("$WEB_ROOT_FS/css");
214                 if($dh) {
215                         while(($file = readdir($dh))!==false) {
216                                 $mt = preg_match("/.*.css$/", $file);
217                                 if($mt > 0) {
218                                         error_log("loading css $file");
219                                         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$BASE_URL/css/$file\">";
220                                         //echo "required $basedir/$file\n";
221                                 }
222                         }
223                 }               
224         }
225
226         // load js
227         if(file_exists("$WEB_ROOT_FS/js")) {
228                 $dh = opendir("$WEB_ROOT_FS/js");
229                 if($dh) {
230                         while(($file = readdir($dh))!==false) {
231                                 $mt = preg_match("/.*.js$/", $file);
232                                 if($mt > 0) {
233                                         error_log("loading js $file");
234                                         echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
235                                         //echo "required $basedir/$file\n";
236                                 }
237                         }
238                 }               
239         }
240         
241         
242         // start body
243         echo "</head><body>";
244         
245         // page top
246         echo "<h1>CBFW</h1><br>";
247         echo "<table width=\"100%\"><tr width=\"100%\"><td>";
248         CBFWMessageBuilder();
249         echo "<td><td align=\"right\">";
250         CBFWConfigStatus();
251         echo "</td></tr><tr><td>";
252         
253         // menu, then body
254         echo "<table><tr><td>";
255         CBFWMenuBuilder();
256         echo "</td></tr><tr><td>";
257         // body
258         $url = "/";
259         if(isset($_REQUEST["q"])) {
260                 $url = $_REQUEST["q"];
261         }
262         
263         if($bodyClass != null) {
264                 $bodyClass->$bodyFunction($bodyfunctiondata);
265         } else if( $bodyFunction != null) {
266                 $bodyFunction($bodyfunctiondata);
267         } else echo $bodycontent;
268         echo "</td></tr></table>";
269         
270         
271         // close the big wrap-around table
272         echo "</td></tr></table>";
273         
274         // footer
275         echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
276         
277 }
278
279 function cbfw_getLastSeen($ip, $data)
280 {
281         $last = "never";
282         
283         if(!$data) return $last;
284         
285         
286         
287         foreach($data as $key=>$val) {
288                 $last = $val["name"];
289                 error_log("got $last for $ip");
290         }
291         
292         if($last == "never") return $last;
293         
294         $last = cbfw_tdiffToAgo(time()-$last);
295         
296         error_log("last for $ip set to $last");
297         
298         return $last;
299         
300 }
301
302 function CBFWConfigStatus()
303 {
304         global $WEB_ROOT_FS, $BASE_URL;
305         
306         $comms = new Comms();
307         
308         $conf0 = $comms->getConfig(0);
309         $conf1 = $comms->getConfig(2);
310         $conf2 = $comms->getConfig(3);
311         
312         if($conf1["status"] == "nochange") {
313                 $saved = "Current";
314                 $running = "Current";
315         } else {
316                 $saved = "Old";
317                 $running = "Current";           
318         }
319         if($conf2["status"] == "nochange") {
320                 $client = "Current";
321         } else {
322                 $ch = $conf2["status"];
323                 $client = "$ch Changes <a href=\"$BASE_URL/changes/show\">Show</a>";
324         }
325         
326         
327         echo "Saved Config: $saved<br>";
328         echo "Running Config: $running<br>";
329         echo "Client Config: $client<br>";
330 }
331
332 function cbfw_tdiffToAgo($time_in_sec)
333 {
334         $tdiff = $time_in_sec;
335         $tdiff_min = (int)($tdiff/60);
336         $tdiff_hour = (int)($tdiff/3600);
337         $tdiff_days = (int)($tdiff/86400);
338         
339         if($tdiff < 60) $last = "Less then a minute";
340         if($tdiff >= 60 && $tdiff < 7200) $last = "$tdiff_min minutes ago";
341         if($tdiff >= 7200 && $tdiff < 86400) $last = "$tdiff_hour hours ago";
342         if($tdiff >= 86400) $last = "$tdiff_days days ago";
343         
344         return $last;
345 }
346
347 function cbfw_startInstaller()
348 {
349         global $WEB_ROOT_FS, $BASE_URL;
350         
351         $uid = posix_geteuid();
352         $gid = posix_getegid();
353         $uid_a = posix_getpwuid($uid);
354         $uid = $uid_a["name"];
355         
356         $gid_a = posix_getgrgid($gid);
357         $gid = $gid_a["name"];
358         error_log("user id is $uid, group id is $gid");
359         
360         if(isset($_REQUEST["installdir"])) {
361                 $c = new CBFWConfig();
362                 touch($_REQUEST["installdir"]."/webconfig");
363                 $c->loadConfig($_REQUEST["installdir"]."/webconfig");
364                 header("Location: index.php");
365                 return;
366         }
367         
368         $underroot = realpath($WEB_ROOT_FS."/../");
369         
370 ?>
371 <html>
372 <h1>Welcome to CBFW</h1>
373 Welcome to CBFW, I cant find my configuration file so im assuming you installing me for the first time<br>
374 If this is not correct then we have a big problem that needs to be solved, i hope you have a backup of the old
375 config, cause that will make life easier.<br><br>
376 However, if this is the first time you have run this app, then all is good with the world. All I need right
377 now is a place where i can store my config. I search the following directories for the location of my 
378 configuration (webconfig)<br>
379 <li> /var/run/glcas/
380 <li> /var/lib/glcas/
381 <li> <?php echo $underroot?>/var/glcas/
382 <br><br>
383
384 As root, you must now create one of these directories and change the ownership of the directory to the web owner.
385 (chown <?php echo $uid?>:<?php echo $gid ?> the_path_you_choose).<br>
386
387 Now, tell me where you want me to create the webconfig file:<br>
388 <form method="post">
389 <select name="installdir">
390 <option value="/var/run/glcas">/var/run/glcas</option>
391 <option value="/var/lib/glcas">/var/lib/glcas</option>
392 <option value="<?php echo $underroot?>/var/glcas/"><?php echo $underroot?>/var/glcas/</option>
393 </select>
394 <input type="submit" name="Go" value="Go">
395
396 </select>
397 </form>
398
399 </html>
400 <?php 
401 }
402
403 function cbfw_startnodir()
404 {
405 ?>
406 <html>
407 <h1>Cant Run</h1>
408 There is no config and no directory where i can store one. Login to the server and either create /var/run/cbfwr or <?php echo $LIB_ROOT_FS?>/var
409 </html>
410 <?php
411 }
412
413
414 ?>