4 // here we load a config if we can find it
5 // there are two sides to every class, the fwd side
6 // and the web page site (command line is web for all intents)
7 function __construct($look_for_config = false)
10 if($look_for_config) {
11 $this->config_file = null;
12 if(file_exists("../var/fw.conf")) {
13 $this->config_file = realpath("../var/fw.conf");
14 $this->config["status"] = "conf";
17 if(file_exists("/var/lib/fwd/fw.conf")) {
18 $this->config_file = "/var/lib/fwd/fw.conf";
19 $this->config["status"] = "conf";
23 if($this->config_file == null) {
24 $this->config["status"] = "noconf";
25 if(is_dir("/var/lib/fwd/")) {
26 echo "no config file found. Will use ../var/fw.conf for now\n";
27 $this->config_file = "/var/lib/fwd/fw.conf";
28 } else if(is_dir("../var/")) {
29 echo "no config file found. Will use ../var/fw.conf for now\n";
30 $this->config_file = "../var/fw.conf";
32 echo "No directory where i can create a config, bailing\n";
33 $this->config["status"] = "nodir";
39 function loadConfig($config)
41 $this->config = $config;
44 function findHardware()
48 // first, network interfaces
49 $dh = opendir("/sys/class/net/");
50 while(($fname = readdir($dh)) !== false) {
51 if($fname != "." && $fname != ".." && $fname != "lo" && is_dir("/sys/class/net/$fname/")) {
52 $this->config["hardware"]["netdev"][$fname]["name"] = $fname;
53 // now read drive name if you can
54 if(file_exists("/sys/class/net/$fname/device/uevent")) $fp = fopen("/sys/class/net/$fname/device/uevent", "r");
58 $line = trim(fgets($fp));
59 $lpl = explode("=", $line);
60 if($lpl[0] == "DRIVER") {
61 $this->config["hardware"]["netdev"][$fname]["driver"] = $lpl[1];
66 if(file_exists("/sys/class/net/$fname/mtu")) $this->config["hardware"]["netdev"][$fname]["mtu"] = file_get_contents("/sys/class/net/$fname/mtu");
67 if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = file_get_contents("/sys/class/net/$fname/address");
68 if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
69 if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
79 function applyConfig()
82 if(!$AM_DAEMON) return true;
88 function mergeConfig($configone, $configtwo)
90 // yep this means apply changes in configtwo to configone
91 $newconf = $configone;
93 // this is gunna be tough its gunna be a meet on meet sandwitch
94 foreach($configtwo as $key => $var) {
95 if($key != "status") {
96 error_log("apply change $key");
97 foreach($var as $key2=>$var2) {
98 error_log("which is $key2");
99 $splits = explode(" ", $var2);
101 // find a description
103 $hasdescription = false;
104 for($i=0; $i<count($splits); $i++) {
105 if($hasdescription !== false) {
106 if($description != null) {
109 $description .= $splits[$i];
111 if($splits[$i] == "#") {
112 $hasdescription = $i;
119 error_log("would add $var2");
123 $newconf["zone"][$splits[1]]["name"] = 1;
124 if($hasdescription) {
125 $newconf["zone"][$splits[1]]["description"] = $description;
132 error_log("would modify $var2");
136 $zonename = $splits[1];
137 if($hasdescription) {
138 $newconf["zone"][$splits[1]]["description"] = $description;
140 if($splits[2] == "rename") {
141 $newname = $splits[3];
142 $tree = $newconf["zone"][$splits[1]];
143 unset($newconf["zone"][$splits[1]]);
144 $newconf["zone"][$newname] = $tree;
153 error_log("would delete $var2");
157 $deletezone = $splits[1];
158 unset($newconf["zone"][$deletezone]);
170 function loadConfigFile($file=null)
174 if($this->config["status"] == "nodir") {
176 } else $file = $this->config_file;
179 if(!file_exists($file)) return "noconf";
181 $fp = fopen($file, "r");
184 while($line = fgets($fp)) {
186 echo "read line $line\n";
187 if($line != "") $this->parseLine($line, $i++);
191 function parseLine($line, $lineno)
193 $expl = preg_split("/ +/", $line);
195 echo "process command ".$expl[0]."\n";
197 // find a description
199 $hasdescription = false;
200 for($i=0; $i<count($expl); $i++) {
201 if($hasdescription !== false) {
202 if($description != null) {
205 $description .= $expl[$i];
207 if($expl[$i] == "#") {
208 $hasdescription = $i;
212 // now rebuild the array if there was one
213 if($hasdescription) {
214 for($i=0; $i<$hasdescription; $i++) {
215 $expl_r[$i] = $expl[$i];
222 // set the hostname to $1
223 $this->config["hostname"] = $expl[1];
227 // set the hostname to $1
228 $this->config["domainname"] = $expl[1];
232 if($hasdescription) {
233 $this->config["zone"][$expl[2]]["description"] = $description;
235 $this->config["zone"][$expl[2]]["name"] = true;
243 if($hasdescription) {
244 $this->config["interface"][$int]["description"] = $description;
249 $this->config["interface"]["$int"]["address4"] = $expl[4];
252 $this->config["interface"]["$int"]["address6"] = $expl[4];
255 $this->config["interface"]["$int"]["name"] = $expl[4];
258 $this->config["interface"]["$int"]["status"] = $expl[4];
261 $this->config["interface"]["$int"]["mtu"] = $expl[4];
264 $this->config["interface"]["$int"]["zone"] = $expl[4];
267 $this->config["interface"]["$int"]["speed"] = $expl[4];
270 $this->config["interface"]["$int"]["duplex"] = $expl[4];
279 $this->config["vlan"][$name]["parent"] = $from;
280 $this->config["vlan"][$name]["id"] = $vlanid;
281 if($hasdescription) {
282 $this->config["vlan"][$name]["description"] = $description;
288 for($i=5; $i<count($expl); $i++) {
289 $this->config["lag"][$name][$i-5] = $expl[$i];
291 if($hasdescription) {
292 $this->config["lag"][$name]["description"] = $description;
298 for($i=5; $i<count($expl); $i++) {
299 $this->config["bridge"][$name][$i-5] = $expl[$i];
301 if($hasdescription) {
302 $this->config["bridge"][$name]["description"] = $description;
311 $this->config["login"][$expl[1]] = $expl[3];
312 if($hasdescription) {
313 $this->config["login"][$expl[1]]["description"] = $description;
322 $this->config["route4"][$route]["address"] = $dest;
324 $this->config["route4"][$route]["device"] = $dest;
326 if(isset($expl[4])) {
327 if($expl[4] == "dev") {
328 if(isset($expl[5])) {
329 $this->config["route4"][$route]["device"] = $expl[5];
333 if($hasdescription) {
334 $this->config["route4"][$route]["description"] = $description;
344 $this->config["route6"][$route]["address"] = $dest;
346 $this->config["route6"][$route]["device"] = $dest;
348 if(isset($expl[4])) {
349 if($expl[4] == "dev") {
350 if(isset($expl[5])) {
351 $this->config["route6"][$route]["device"] = $expl[5];
355 if($hasdescription) {
356 $this->config["route6"][$route]["description"] = $description;
359 // here we should check "$route"
364 if(isset($this->config["dns"]["nservers"])) {
365 $dns_servers = $this->config["dns"]["nservers"];
369 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
370 if($hasdescription) {
371 $this->config["dns"]["server"][$dns_servers]["description"] = $description;
373 $this->config["dns"]["nservers"] = $dns_servers+1;
378 if(isset($this->config["ntp"]["nservers"])) {
379 $ntp_servers = $this->config["ntp"]["nservers"];
383 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
384 if($hasdescription) {
385 $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
387 $this->config["ntp"]["nservers"] = $ntp_servers+1;
393 echo "Errr, unknown config directive on line $lineno, $line\n";
399 function saveConfigFile($file)
402 if(!$AM_DAEMON) return true;
406 private $config_file;