some structure to how configs are loaded, how the daemon works, etc
authorpaulr <me@pjr.cc>
Tue, 20 Sep 2011 17:31:26 +0000 (03:31 +1000)
committerpaulr <me@pjr.cc>
Tue, 20 Sep 2011 17:31:26 +0000 (03:31 +1000)
bin/cbfwrd.php
doco/commands.txt [new file with mode: 0644]
libcbfwr/comms.php
libcbfwr/config.php
libcbfwr/fwui.php
libcbfwr/web.php

index 3b29870..4bf1513 100644 (file)
@@ -19,8 +19,17 @@ glcas_pluginLoader();
 
 error_log("CBFWRD starting");
 
+
 $config = new Config();
 
+if(isset($argv[1])) {
+       if($argv[1] == "boot") {
+               // we get called here on boot.
+               $config->bootHardware();
+               exit(0);
+       }
+}
+
 // now we got into daemon modes
 $cont = true;
 
@@ -30,12 +39,12 @@ $cont = true;
 while($cont) {
        $comms = new Comms;
        
+       $config->loadConfig();
        $comms->putConfig($config->getConfig());
        
        $msg = $comms->waitForMessage();
        
        //echo "Got message: $msg\n";
-       
-       $cont = false;
+       if($msg == "quit") $cont = false;
 }
 ?>
\ No newline at end of file
diff --git a/doco/commands.txt b/doco/commands.txt
new file mode 100644 (file)
index 0000000..9619cf8
--- /dev/null
@@ -0,0 +1,42 @@
+hostname <hostname>
+domainname <domainname>
+
+dns server <server addr>
+
+ntp server <ntp addr>
+
+login <username> auth <sha1password>
+
+admin via http on dev <device> from <context>
+admin via https on dev <device> from <context>
+
+interace dev devname address4 addr/mask
+interace dev devname address6 addr/mask
+interace dev devname zone zonename
+interace dev devname mtu|speed|duplex ...
+
+interface lag name <name> with <dev1> <dev2>....
+
+interface vlan <vlanid> name <name> from <dev>
+
+interface bridge name <name> with <dev1> <dev2> <dev3>...
+
+
+route4|6 default|addr to <address> dev <device>
+route4|6 default|addr to <address>
+route4|6 default|addr dev <device>
+
+rule add <ruletype> and log from <context> to <context> in zones <zonecontext>
+rule move <zonecontext> to <zonecontext>
+
+<ruletype> is reject, drop, accept
+
+nat add... need to figure this one out
+
+<context> is:
+object/type/name
+
+where type is, host, network... thats it for now
+
+zonecontext is:
+Zone/zonename/rulenumber
\ No newline at end of file
index db0215e..610959e 100644 (file)
@@ -15,23 +15,26 @@ class Comms {
                $this->msgres = msg_get_queue($MESSAGE_KEY, 0666);
        }
        
-       function getConfig()
+       function getConfig($type=0)
        {
                global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
                sem_acquire($this->semres);
                $shm_space = shm_attach($STORE_KEY, 16*1024*1024);
-               $config = shm_get_var($shm_space, 0);
+               $config = shm_get_var($shm_space, $type);
                sem_release($this->semres);
                
                return $config;
        }
        
-       function putConfig($config)
+       
+       // type = 0 for the main config
+       // type = 1 for the boot hardware config
+       function putConfig($config, $type=0)
        {
                global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
                sem_acquire($this->semres);
                $shm_space = shm_attach($STORE_KEY, 16*1024*1024);
-               shm_put_var($shm_space, 0, $config);
+               shm_put_var($shm_space, $type, $config);
                sem_release($this->semres);             
        }
        
index 94cf1b3..9ca0ff9 100644 (file)
@@ -31,29 +31,50 @@ class Config {
                                        $this->config_file = "../var/";
                                } else {
                                        echo "No directory where i can create a config, bailing\n";
-                                       exit(0);
+                                       $this->config["status"] = "unconf";
                                }
                        }
                        
-                       if($this->config["status"] == "conf") {
-                               
-                               $this->loadConfig($this->config_file);
+                       
+                       
+               }
+       }
+       
+       function bootHardware()
+       {
+               $this->findHardware();
+
+               $mycomm = new Comms();
+               $mycomm->putConfig($this->config, 1);
+               
+       }
+       
+       function loadConfig()
+       {
+               global $AM_DAEMON;
+               
+               if($AM_DAEMON) {
+                       if($this->config["status"] == "conf") { 
+                               $this->loadConfigFile($this->config_file);
+                               $this->applyConfig();
                                $this->findHardware();
                                
-                               print_r($this->config);
-                               $this->applyConfig();
-                       } else {
-                               // we go into firstrun mode
+                               $mycomm = new Comms();
+                               $mycomm->putConfig($this->config);
+                               $this->boot_config = $mycomm->getConfig(1);
                        }
-                       
-                       
                } else {
-                       // config comes from shm... we'll get there
-               }       
+                       $mycomm = new Comms();
+                       $this->config = $mycomm->getConfig(0);
+                       $this->boot_config = $mycomm->getConfig(1);
+                       
+               }
        }
        
        function findHardware()
        {
+               
+               
                // first, network interfaces
                $dh = opendir("/sys/class/net/");
                while(($fname = readdir($dh)) !== false) {
@@ -83,6 +104,11 @@ class Config {
                return $this->config;
        }
        
+       function getBootConfig()
+       {
+               return $this->boot_config;
+       }
+       
        function applyConfig()
        {
                global $AM_DAEMON;
@@ -92,7 +118,7 @@ class Config {
                return true;
        }
        
-       function loadConfig($file)
+       function loadConfigFile($file)
        {
                global $AM_DAEMON;
                if(!$AM_DAEMON) return true;
@@ -315,7 +341,7 @@ class Config {
 
        }
        
-       function saveConfig()
+       function saveConfigFile($file)
        {
                global $AM_DAEMON;
                if(!$AM_DAEMON) return true;
@@ -324,6 +350,7 @@ class Config {
        
        private $config_file;
        private $config;
+       private $boot_config;
 };
 
 ?>
\ No newline at end of file
index a94b4ad..cf8056b 100644 (file)
@@ -2,7 +2,9 @@
 
 function CBFWZonesPage()
 {
-       return "hi";
+       $comms = new Comms();
+       
+       $config = $comms->getConfig();
 }
 
 function CBFWInterfacesPage()
index 8e4967c..aa039a8 100644 (file)
@@ -1,12 +1,12 @@
 <?php
-$MENU_ITEMS["Zones"]["link"] = "$BASE_URL/zones"; 
-$MENU_ITEMS["Zones"]["name"] = "Zones"; 
-$MENU_ITEMS["Interfaces"]["link"] = "$BASE_URL/interfaces"; 
-$MENU_ITEMS["Interfaces"]["name"] = "Interfaces"; 
-$MENU_ITEMS["Objects"]["link"] = "$BASE_URL/objects"; 
-$MENU_ITEMS["Objects"]["name"] = "Objects"; 
-$MENU_ITEMS["Rules"]["link"] = "$BASE_URL/rules"; 
-$MENU_ITEMS["Rules"]["name"] = "Rules"; 
+$MENU_ITEMS["15_Zones"]["link"] = "$BASE_URL/zones"; 
+$MENU_ITEMS["15_Zones"]["name"] = "Zones"; 
+$MENU_ITEMS["20_Objects"]["link"] = "$BASE_URL/objects"; 
+$MENU_ITEMS["20_Objects"]["name"] = "Objects"; 
+$MENU_ITEMS["30_Rules"]["link"] = "$BASE_URL/rules"; 
+$MENU_ITEMS["30_Rules"]["name"] = "Rules"; 
+$MENU_ITEMS["10_Interfaces"]["link"] = "$BASE_URL/interfaces"; 
+$MENU_ITEMS["10_Interfaces"]["name"] = "Interfaces"; 
 
 // if i believed in name spacing in php, i'd use it.
 error_log("cbfwweb loaded");
@@ -30,25 +30,42 @@ class CBFWWeb {
                        
                        switch($url_s[0]) {
                                case "zones":
-                                       $bodycontent = CBFWZonesPage($url_s);
+                                       $bodyFunction = "CBFWZonesPage";
                                        break;
                                case "interfaces":
-                                       $bodycontent = CBFWInterfacesPage($url_s);
+                                       $bodyFunction = "CBFWInterfacesPage";
                                        break;
                                case "objects":
-                                       $bodycontent = CBFWObjectsPage($url_s);
+                                       $bodyFunction = "CBFWObjectsPage";
                                        break;
                                case "rules":
-                                       $bodycontent = CBFWRulesPage($url_s);
+                                       $bodyFunction = "CBFWRulesPage";
                                        break;
                                default:
-                                       $bodycontent = findUrl($url_s);
+                                       $bodyFunction = findUrl($url_s);
                        }
+               } else {
+                       $bodyFunction = "CBFBuildHomePage";
                }
-               CBFWpageBuilder(null, null, $bodycontent);
+               CBFWpageBuilder(null, $bodyFunction);
        }       
 }
 
+function CBFBuildHomePage($urls)
+{
+       echo "Must remember this, gotta rules could apply to multiple zones not just one<br>";
+       echo "i.e.: add rule reject from object/host/hostname to address6/2003::123 in zones Zone/zonename/rulenum Zone/zonename/rulenum";
+       $conf = new Config();
+       $conf->loadConfig();
+       
+       echo "<pre>";
+       print_r($conf->getConfig());
+       echo "\n\n\n";
+       print_r($conf->getBootConfig());
+       echo "</pre>";
+       
+}
+
 function CBFWMenuBuilder()
 {
        global $BASE_URL, $MENU_ITEMS;