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"] = trim(file_get_contents("/sys/class/net/$fname/mtu"));
67 if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = trim(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;
70 if(file_exists("/sys/class/net/$fname/duplex")) {
71 $myval = trim(file_get_contents("/sys/class/net/$fname/duplex"));
72 if($myval == null) $myval = "default";
73 $this->config["hardware"]["netdev"][$fname]["duplex"] = $myval;
75 if(file_exists("/sys/class/net/$fname/speed")) {
76 $myval = trim(file_get_contents("/sys/class/net/$fname/speed"));
77 if($myval == null) $myval = "default";
78 $this->config["hardware"]["netdev"][$fname]["speed"] = $myval;
89 function setConfig($config) {
90 $this->config = $config;
93 function applyConfig()
96 if(!$AM_DAEMON) return true;
104 function loadConfigFile($file=null)
108 if($this->config["status"] == "nodir") {
110 } else $file = $this->config_file;
113 if(!file_exists($file)) return "noconf";
115 $fp = fopen($file, "r");
118 while($line = fgets($fp)) {
120 echo "read line $line\n";
121 if($line != "") $this->addConfigLine($line, $i++);
125 function resolveInterfaceName($name)
127 // $config only ever contains real interface names
128 if(isset($this->config["interface"]["dev"][$name])) return $name;
130 if(isset($this->config["interface"])) {
131 foreach($this->config["interface"]["dev"] as $key => $var) {
132 if(isset($var["name"])) {
133 if($var["name"] == $name) {
139 // have to assume its a hardware name and check /sys
140 if(file_exists("/sys/class/net/$name")) return $name;
146 function addConfigLine($line, $lineno=0)
148 $line_n = trim($line);
150 $expl = preg_split("/ +/", $line);
152 if(count($expl) < 2) {
154 error_log("Not a valid config line ($c), $line");
157 echo "process command ".$expl[0]."\n";
159 // find a description
161 $hasdescription = false;
162 for($i=0; $i<count($expl); $i++) {
163 if($hasdescription !== false) {
164 if($description != null) {
167 $description .= $expl[$i];
169 if($expl[$i] == "#") {
170 $hasdescription = $i;
174 // now rebuild the array if there was a description
175 if($hasdescription) {
176 for($i=0; $i<$hasdescription; $i++) {
177 $expl_r[$i] = $expl[$i];
183 // check for delete on the line
184 if($expl[0] == "delete") {
187 // re-order the array
188 for($i=0; $i < (count($expl)-1); $i++) {
189 $expl[$i] = $expl[$i+1];
191 unset($expl[count($expl)-1]);
196 // check for delete on the line
197 if($expl[0] == "modify") {
200 // re-order the array
201 for($i=0; $i < (count($expl)-1); $i++) {
202 $expl[$i] = $expl[$i+1];
204 unset($expl[count($expl)-1]);
209 // set the hostname to $1
211 unset($this->config["hostname"]);
213 $this->config["hostname"] = $expl[1];
218 // set the hostname to $1
220 unset($this->config["domainname"]);
222 $this->config["domainname"] = $expl[1];
228 error_log("delete zone ".$expl[2]);
229 unset($this->config["zone"][$expl[2]]);
230 if(count($this->config["zone"]) < 1) {
231 error_log("zone now empty, delete zones");
232 unset($this->config["zone"]);
235 if($hasdescription) {
236 $this->config["zone"][$expl[2]]["description"] = $description;
238 $this->config["zone"][$expl[2]]["name"] = true;
245 $int = $this->resolveInterfaceName($expl[2]);
247 if($int != $expl[2]) {
248 error_log("resolved int as $int from ".$expl[2]);
251 error_log("got config for interface ".$expl[2]." but i cannot find it, i will assume its hardware for now");
254 if($hasdescription) {
255 $this->config["interface"]["dev"][$int]["description"] = $description;
260 $this->config["interface"]["dev"]["$int"]["address4"] = $expl[4];
263 $this->config["interface"]["dev"]["$int"]["address6"] = $expl[4];
266 $this->config["interface"]["dev"]["$int"]["name"] = $expl[4];
269 $this->config["interface"]["dev"]["$int"]["status"] = $expl[4];
272 $this->config["interface"]["dev"]["$int"]["mtu"] = $expl[4];
275 $this->config["interface"]["dev"]["$int"]["zone"] = $expl[4];
278 $this->config["interface"]["dev"]["$int"]["speed"] = $expl[4];
281 $this->config["interface"]["$int"]["duplex"] = $expl[4];
290 $this->config["vlan"][$name]["parent"] = $from;
291 $this->config["vlan"][$name]["id"] = $vlanid;
292 if($hasdescription) {
293 $this->config["vlan"][$name]["description"] = $description;
299 for($i=5; $i<count($expl); $i++) {
300 $this->config["lag"][$name][$i-5] = $expl[$i];
302 if($hasdescription) {
303 $this->config["lag"][$name]["description"] = $description;
309 for($i=5; $i<count($expl); $i++) {
310 $this->config["bridge"][$name][$i-5] = $expl[$i];
312 if($hasdescription) {
313 $this->config["bridge"][$name]["description"] = $description;
323 unset($this->config["login"][$expl[1]]);
325 $this->config["login"][$expl[1]] = $expl[3];
326 if($hasdescription) {
327 $this->config["login"][$expl[1]]["description"] = $description;
334 unset($this->config["route4"][$expl[1]]);
340 $this->config["route4"][$route]["address"] = $dest;
342 $this->config["route4"][$route]["device"] = $dest;
344 if(isset($expl[4])) {
345 if($expl[4] == "dev") {
346 if(isset($expl[5])) {
347 $this->config["route4"][$route]["device"] = $expl[5];
351 if($hasdescription) {
352 $this->config["route4"][$route]["description"] = $description;
360 unset($this->config["route6"][$expl[1]]);
366 $this->config["route6"][$route]["address"] = $dest;
368 $this->config["route6"][$route]["device"] = $dest;
370 if(isset($expl[4])) {
371 if($expl[4] == "dev") {
372 if(isset($expl[5])) {
373 $this->config["route6"][$route]["device"] = $expl[5];
377 if($hasdescription) {
378 $this->config["route6"][$route]["description"] = $description;
381 // here we should check "$route"
386 if(isset($this->config["dns"]["nservers"])) {
387 $dns_servers = $this->config["dns"]["nservers"];
391 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
392 if($hasdescription) {
393 $this->config["dns"]["server"][$dns_servers]["description"] = $description;
395 $this->config["dns"]["nservers"] = $dns_servers+1;
400 if(isset($this->config["ntp"]["nservers"])) {
401 $ntp_servers = $this->config["ntp"]["nservers"];
405 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
406 if($hasdescription) {
407 $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
409 $this->config["ntp"]["nservers"] = $ntp_servers+1;
415 echo "Errr, unknown config directive on line $lineno, $line\n";
421 function saveConfigFile($file)
424 if(!$AM_DAEMON) return true;
428 private $config_file;