289fe19bb7a967223335c3a6406f973455cc125c
[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("1", "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 = $val["timestamp"];
170                         $sev = $val["level"];
171                         $message = $val["message"];
172                         echo "<tr><td>$datetime</td><td>$sev</td><td>$message</td></tr>";
173                         $show++;
174                         if($show >= $maxshow) break;
175                 }
176                 echo "</table>";
177         }
178 }
179
180 function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="CBFW", $bodyfunctiondata=null)
181 {
182         global $WEB_ROOT_FS, $BASE_URL;
183         
184         // TODO: load css
185         // header
186         echo "<html><head><title>$title</title>";
187         
188         // load css
189         if(file_exists("$WEB_ROOT_FS/css")) {
190                 $dh = opendir("$WEB_ROOT_FS/css");
191                 if($dh) {
192                         while(($file = readdir($dh))!==false) {
193                                 $mt = preg_match("/.*.css$/", $file);
194                                 if($mt > 0) {
195                                         error_log("loading css $file");
196                                         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$BASE_URL/css/$file\">";
197                                         //echo "required $basedir/$file\n";
198                                 }
199                         }
200                 }               
201         }
202
203         // load js
204         if(file_exists("$WEB_ROOT_FS/js")) {
205                 $dh = opendir("$WEB_ROOT_FS/js");
206                 if($dh) {
207                         while(($file = readdir($dh))!==false) {
208                                 $mt = preg_match("/.*.js$/", $file);
209                                 if($mt > 0) {
210                                         error_log("loading js $file");
211                                         echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
212                                         //echo "required $basedir/$file\n";
213                                 }
214                         }
215                 }               
216         }
217         
218         
219         // start body
220         echo "</head><body>";
221         
222         // page top
223         echo "<h1>CBFW</h1><br>";
224         echo "<table width=\"100%\"><tr width=\"100%\"><td>";
225         CBFWMessageBuilder();
226         echo "<td><td align=\"right\">";
227         CBFWConfigStatus();
228         echo "</td></tr><tr><td>";
229         
230         // menu, then body
231         echo "<table><tr><td>";
232         CBFWMenuBuilder();
233         echo "</td></tr><tr><td>";
234         // body
235         $url = "/";
236         if(isset($_REQUEST["q"])) {
237                 $url = $_REQUEST["q"];
238         }
239         
240         if($bodyClass != null) {
241                 $bodyClass->$bodyFunction($bodyfunctiondata);
242         } else if( $bodyFunction != null) {
243                 $bodyFunction($bodyfunctiondata);
244         } else echo $bodycontent;
245         echo "</td></tr></table>";
246         
247         
248         // close the big wrap-around table
249         echo "</td></tr></table>";
250         
251         // footer
252         echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
253         
254 }
255
256 function cbfw_getLastSeen($ip, $data)
257 {
258         $last = "never";
259         
260         if(!$data) return $last;
261         
262         
263         
264         foreach($data as $key=>$val) {
265                 $last = $val["name"];
266                 error_log("got $last for $ip");
267         }
268         
269         if($last == "never") return $last;
270         
271         $last = cbfw_tdiffToAgo(time()-$last);
272         
273         error_log("last for $ip set to $last");
274         
275         return $last;
276         
277 }
278
279 function CBFWConfigStatus()
280 {
281         global $WEB_ROOT_FS, $BASE_URL;
282         
283         $comms = new Comms();
284         
285         $conf0 = $comms->getConfig(0);
286         $conf1 = $comms->getConfig(2);
287         $conf2 = $comms->getConfig(3);
288         
289         if($conf1["status"] == "nochange") {
290                 $saved = "Current";
291                 $running = "Current";
292         } else {
293                 $saved = "Old";
294                 $running = "Current";           
295         }
296         if($conf2["status"] == "nochange") {
297                 $client = "Current";
298         } else {
299                 $ch = $conf2["status"];
300                 $client = "$ch Changes <a href=\"$BASE_URL/changes/show\">Show</a>";
301         }
302         
303         
304         echo "Saved Config: $saved<br>";
305         echo "Running Config: $running<br>";
306         echo "Client Config: $client<br>";
307 }
308
309 function cbfw_tdiffToAgo($time_in_sec)
310 {
311         $tdiff = $time_in_sec;
312         $tdiff_min = (int)($tdiff/60);
313         $tdiff_hour = (int)($tdiff/3600);
314         $tdiff_days = (int)($tdiff/86400);
315         
316         if($tdiff < 60) $last = "Less then a minute";
317         if($tdiff >= 60 && $tdiff < 7200) $last = "$tdiff_min minutes ago";
318         if($tdiff >= 7200 && $tdiff < 86400) $last = "$tdiff_hour hours ago";
319         if($tdiff >= 86400) $last = "$tdiff_days days ago";
320         
321         return $last;
322 }
323
324 function cbfw_startInstaller()
325 {
326         global $WEB_ROOT_FS, $BASE_URL;
327         
328         $uid = posix_geteuid();
329         $gid = posix_getegid();
330         $uid_a = posix_getpwuid($uid);
331         $uid = $uid_a["name"];
332         
333         $gid_a = posix_getgrgid($gid);
334         $gid = $gid_a["name"];
335         error_log("user id is $uid, group id is $gid");
336         
337         if(isset($_REQUEST["installdir"])) {
338                 $c = new CBFWConfig();
339                 touch($_REQUEST["installdir"]."/webconfig");
340                 $c->loadConfig($_REQUEST["installdir"]."/webconfig");
341                 header("Location: index.php");
342                 return;
343         }
344         
345         $underroot = realpath($WEB_ROOT_FS."/../");
346         
347 ?>
348 <html>
349 <h1>Welcome to CBFW</h1>
350 Welcome to CBFW, I cant find my configuration file so im assuming you installing me for the first time<br>
351 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
352 config, cause that will make life easier.<br><br>
353 However, if this is the first time you have run this app, then all is good with the world. All I need right
354 now is a place where i can store my config. I search the following directories for the location of my 
355 configuration (webconfig)<br>
356 <li> /var/run/glcas/
357 <li> /var/lib/glcas/
358 <li> <?php echo $underroot?>/var/glcas/
359 <br><br>
360
361 As root, you must now create one of these directories and change the ownership of the directory to the web owner.
362 (chown <?php echo $uid?>:<?php echo $gid ?> the_path_you_choose).<br>
363
364 Now, tell me where you want me to create the webconfig file:<br>
365 <form method="post">
366 <select name="installdir">
367 <option value="/var/run/glcas">/var/run/glcas</option>
368 <option value="/var/lib/glcas">/var/lib/glcas</option>
369 <option value="<?php echo $underroot?>/var/glcas/"><?php echo $underroot?>/var/glcas/</option>
370 </select>
371 <input type="submit" name="Go" value="Go">
372
373 </select>
374 </form>
375
376 </html>
377 <?php 
378 }
379
380 function cbfw_startnodir()
381 {
382 ?>
383 <html>
384 <h1>Cant Run</h1>
385 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
386 </html>
387 <?php
388 }
389
390
391 ?>