X-Git-Url: http://git.pjr.cc/?p=CBFWR.git;a=blobdiff_plain;f=libcbfwr%2Fconfig.php;h=372bd04d50123d7ff54d2707053e3f91d3c02417;hp=c0873b5e9c753e0520f3716c8e62e6e216754ddf;hb=60673f4f00bab805471fcbb85c8816aebb3a9582;hpb=db4f63e0e2c434ded60cd1fd324be62a2aadcae1 diff --git a/libcbfwr/config.php b/libcbfwr/config.php index c0873b5..372bd04 100644 --- a/libcbfwr/config.php +++ b/libcbfwr/config.php @@ -4,19 +4,22 @@ 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"); + $this->config["status"] = "conf"; + } if(file_exists("/var/lib/fwd/fw.conf")) { $this->config_file = "/var/lib/fwd/fw.conf"; + $this->config["status"] = "conf"; } + if($this->config_file == null) { $this->config["status"] = "noconf"; if(is_dir("/var/lib/fwd/")) { @@ -24,17 +27,381 @@ 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"; - exit(0); + $this->config["status"] = "nodir"; + } + } + } + } + + function loadConfig($config) + { + $this->config = $config; + } + + function findHardware() + { + + + // first, network interfaces + $dh = opendir("/sys/class/net/"); + while(($fname = readdir($dh)) !== false) { + if($fname != "." && $fname != ".." && $fname != "lo" && is_dir("/sys/class/net/$fname/")) { + $this->config["hardware"]["netdev"][$fname]["name"] = $fname; + // now read drive name if you can + if(file_exists("/sys/class/net/$fname/device/uevent")) $fp = fopen("/sys/class/net/$fname/device/uevent", "r"); + else $fp = false; + if($fp) { + while(!feof($fp)) { + $line = trim(fgets($fp)); + $lpl = explode("=", $line); + if($lpl[0] == "DRIVER") { + $this->config["hardware"]["netdev"][$fname]["driver"] = $lpl[1]; + } + } + 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; + } + } + } + + function getConfig() + { + return $this->config; + } + + function applyConfig() + { + global $AM_DAEMON; + if(!$AM_DAEMON) return true; + + // oh the joy + return true; + } + + function mergeConfig($configone, $configtwo) + { + // 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; $iconfig["status"] == "nodir") { + return "nodir"; + } else $file = $this->config_file; + } + + if(!file_exists($file)) return "noconf"; + + $fp = fopen($file, "r"); + + $i = 1; + while($line = fgets($fp)) { + $line = trim($line); + echo "read line $line\n"; + if($line != "") $this->parseLine($line, $i++); + } + } + + function parseLine($line, $lineno) + { + $expl = preg_split("/ +/", $line); + + echo "process command ".$expl[0]."\n"; + + // find a description + $description = null; + $hasdescription = false; + for($i=0; $iconfig["hostname"] = $expl[1]; + break; + + case "domainname": + // set the hostname to $1 + $this->config["domainname"] = $expl[1]; + break; + + case "zone": + if($hasdescription) { + $this->config["zone"][$expl[2]]["description"] = $description; + } + $this->config["zone"][$expl[2]]["name"] = true; + break; + + case "interface": + switch($expl[1]) { + case "dev": + $int = $expl[2]; + + if($hasdescription) { + $this->config["interface"][$int]["description"] = $description; + } + + switch($expl[3]) { + case "address4": + $this->config["interface"]["$int"]["address4"] = $expl[4]; + break; + case "address6": + $this->config["interface"]["$int"]["address6"] = $expl[4]; + break; + case "name": + $this->config["interface"]["$int"]["name"] = $expl[4]; + break; + case "status": + $this->config["interface"]["$int"]["status"] = $expl[4]; + break; + case "mtu": + $this->config["interface"]["$int"]["mtu"] = $expl[4]; + break; + case "zone": + $this->config["interface"]["$int"]["zone"] = $expl[4]; + break; + case "speed": + $this->config["interface"]["$int"]["speed"] = $expl[4]; + break; + case "duplex": + $this->config["interface"]["$int"]["duplex"] = $expl[4]; + break; + } + break; + + case "vlan": + $vlanid = $expl[2]; + $name = $expl[4]; + $from = $expl[6]; + $this->config["vlan"][$name]["parent"] = $from; + $this->config["vlan"][$name]["id"] = $vlanid; + if($hasdescription) { + $this->config["vlan"][$name]["description"] = $description; + } + break; + + case "lag": + $name = $expl[3]; + for($i=5; $iconfig["lag"][$name][$i-5] = $expl[$i]; + } + if($hasdescription) { + $this->config["lag"][$name]["description"] = $description; + } + break; + + case "bridge": + $name = $expl[3]; + for($i=5; $iconfig["bridge"][$name][$i-5] = $expl[$i]; + } + if($hasdescription) { + $this->config["bridge"][$name]["description"] = $description; + } + break; + + } + break; + + + case "login": + $this->config["login"][$expl[1]] = $expl[3]; + if($hasdescription) { + $this->config["login"][$expl[1]]["description"] = $description; + } + break; + + case "route4": + $route = $expl[1]; + $via = $expl[2]; + $dest = $expl[3]; + if($via == "to") { + $this->config["route4"][$route]["address"] = $dest; + } else { + $this->config["route4"][$route]["device"] = $dest; + } + if(isset($expl[4])) { + if($expl[4] == "dev") { + if(isset($expl[5])) { + $this->config["route4"][$route]["device"] = $expl[5]; + } + } + } + if($hasdescription) { + $this->config["route4"][$route]["description"] = $description; + } + break; + + + case "route6": + $route = $expl[1]; + $via = $expl[2]; + $dest = $expl[3]; + if($via == "to") { + $this->config["route6"][$route]["address"] = $dest; + } else { + $this->config["route6"][$route]["device"] = $dest; + } + if(isset($expl[4])) { + if($expl[4] == "dev") { + if(isset($expl[5])) { + $this->config["route6"][$route]["device"] = $expl[5]; + } + } + } + if($hasdescription) { + $this->config["route6"][$route]["description"] = $description; + } + + // here we should check "$route" + break; + + + case "dns": + if(isset($this->config["dns"]["nservers"])) { + $dns_servers = $this->config["dns"]["nservers"]; + } else { + $dns_servers = 0; + } + if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2]; + if($hasdescription) { + $this->config["dns"]["server"][$dns_servers]["description"] = $description; + } + $this->config["dns"]["nservers"] = $dns_servers+1; + break; + + + case "ntp": + if(isset($this->config["ntp"]["nservers"])) { + $ntp_servers = $this->config["ntp"]["nservers"]; + } else { + $ntp_servers = 0; + } + if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2]; + if($hasdescription) { + $this->config["ntp"]["server"][$ntp_servers]["description"] = $description; + } + $this->config["ntp"]["nservers"] = $ntp_servers+1; + break; + + + + default: + echo "Errr, unknown config directive on line $lineno, $line\n"; + } + + } + function saveConfigFile($file) + { + global $AM_DAEMON; + if(!$AM_DAEMON) return true; + + } private $config_file; private $config;