config modifications and such
[CBFWR.git] / libcbfwr / config.php
index 7bffc1d..372bd04 100644 (file)
@@ -4,11 +4,10 @@ class Config {
        // here we load a config if we can find it
        // there are two sides to every class, the fwd side
        // and the web page site (command line is web for all intents)
-       function __construct()
+       function __construct($look_for_config = false)
        {
-               global $AM_DAEMON;
                
-               if($AM_DAEMON) {
+               if($look_for_config) {
                        $this->config_file = null;
                        if(file_exists("../var/fw.conf")) {
                                $this->config_file = realpath("../var/fw.conf");
@@ -28,47 +27,18 @@ class Config {
                                        $this->config_file = "/var/lib/fwd/fw.conf";
                                } else if(is_dir("../var/")) {
                                        echo "no config file found. Will use ../var/fw.conf for now\n";
-                                       $this->config_file = "../var/";
+                                       $this->config_file = "../var/fw.conf";
                                } else {
                                        echo "No directory where i can create a config, bailing\n";
                                        $this->config["status"] = "nodir";
                                }
                        }
-                       
-                       
-                       
                }
        }
        
-       function bootHardware()
-       {
-               $this->findHardware();
-
-               $mycomm = new Comms();
-               $mycomm->putConfig($this->config, 1);
-               
-       }
-       
-       function loadConfig()
+       function loadConfig($config)
        {
-               global $AM_DAEMON;
-               
-               if($AM_DAEMON) {
-                       if($this->config["status"] == "conf") { 
-                               $this->loadConfigFile($this->config_file);
-                               $this->applyConfig();
-                               $this->findHardware();
-                               
-                               $mycomm = new Comms();
-                               $mycomm->putConfig($this->config);
-                               $this->boot_config = $mycomm->getConfig(1);
-                       }
-               } else {
-                       $mycomm = new Comms();
-                       $this->config = $mycomm->getConfig(0);
-                       $this->boot_config = $mycomm->getConfig(1);
-                       
-               }
+               $this->config = $config;
        }
        
        function findHardware()
@@ -93,6 +63,7 @@ class Config {
                                        }
                                        fclose($fp);
                                }
+                               if(file_exists("/sys/class/net/$fname/mtu")) $this->config["hardware"]["netdev"][$fname]["mtu"] = file_get_contents("/sys/class/net/$fname/mtu");
                                if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = file_get_contents("/sys/class/net/$fname/address");
                                if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
                                if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
@@ -105,11 +76,6 @@ class Config {
                return $this->config;
        }
        
-       function getBootConfig()
-       {
-               return $this->boot_config;
-       }
-       
        function applyConfig()
        {
                global $AM_DAEMON;
@@ -119,10 +85,98 @@ class Config {
                return true;
        }
        
-       function loadConfigFile($file)
+       function mergeConfig($configone, $configtwo)
        {
-               global $AM_DAEMON;
-               if(!$AM_DAEMON) return true;
+               // yep this means apply changes in configtwo to configone
+               $newconf = $configone;
+               
+               // this is gunna be tough its gunna be a meet on meet sandwitch
+               foreach($configtwo as $key => $var) {
+                       if($key != "status") {
+                               error_log("apply change $key");
+                               foreach($var as $key2=>$var2) {
+                                       error_log("which is $key2");
+                                       $splits = explode(" ", $var2);
+                                       
+                                       // find a description
+                                       $description = null;
+                                       $hasdescription = false;
+                                       for($i=0; $i<count($splits); $i++) {
+                                               if($hasdescription !== false) {
+                                                       if($description != null) {
+                                                               $description .= " ";
+                                                       }
+                                                       $description .= $splits[$i];
+                                               } 
+                                               if($splits[$i] == "#") {
+                                                       $hasdescription = $i;
+                                               }
+                                       }
+                                       
+                                       
+                                       switch($key2) {
+                                               case "add":
+                                                       error_log("would add $var2");
+                                                       $thing = $splits[0];
+                                                       switch($thing) {
+                                                               case "zone":
+                                                                       $newconf["zone"][$splits[1]]["name"] = 1;
+                                                                       if($hasdescription) {
+                                                                               $newconf["zone"][$splits[1]]["description"] = $description;
+                                                                       }
+                                                                       break;
+                                                               
+                                                       }
+                                                       break;
+                                               case "modify":
+                                                       error_log("would modify $var2");
+                                                       $thing = $splits[0];
+                                                       switch($thing) {
+                                                               case "zone":
+                                                                       $zonename = $splits[1];
+                                                                       if($hasdescription) {
+                                                                               $newconf["zone"][$splits[1]]["description"] = $description;
+                                                                       }
+                                                                       if($splits[2] == "rename") {
+                                                                               $newname = $splits[3];
+                                                                               $tree = $newconf["zone"][$splits[1]];
+                                                                               unset($newconf["zone"][$splits[1]]);
+                                                                               $newconf["zone"][$newname] = $tree;
+                                                                       }
+                                                                       break;
+                                                               
+                                                       }
+                                                       break;
+                                                       
+                                                       
+                                               case "delete":
+                                                       error_log("would delete $var2");
+                                                       $thing = $splits[0];
+                                                       switch($thing) {
+                                                               case "zone":
+                                                                       $deletezone = $splits[1];
+                                                                       unset($newconf["zone"][$deletezone]);
+                                                                       break;
+                                                               
+                                                       }
+                                                       break;
+                                       }
+                               } 
+                       }
+               }
+               return $newconf;
+       }
+       
+       function loadConfigFile($file=null)
+       {
+               
+               if($file == null) {
+                       if($this->config["status"] == "nodir") {
+                               return "nodir";
+                       } else $file = $this->config_file;
+               }
+               
+               if(!file_exists($file)) return "noconf";
                
                $fp = fopen($file, "r");
                
@@ -351,7 +405,6 @@ class Config {
        
        private $config_file;
        private $config;
-       private $boot_config;
 };
 
 ?>
\ No newline at end of file