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 parseEthtool($interface)
46 // im not going to enjoy this, not one little bit
49 function findHardware()
53 // first, network interfaces
54 $dh = opendir("/sys/class/net/");
55 while(($fname = readdir($dh)) !== false) {
56 if($fname != "." && $fname != ".." && $fname != "lo" && is_dir("/sys/class/net/$fname/")) {
57 $this->config["hardware"]["netdev"][$fname]["name"] = $fname;
58 // now read drive name if you can
59 if(file_exists("/sys/class/net/$fname/device/uevent")) $fp = fopen("/sys/class/net/$fname/device/uevent", "r");
63 $line = trim(fgets($fp));
64 $lpl = explode("=", $line);
65 if($lpl[0] == "DRIVER") {
66 $this->config["hardware"]["netdev"][$fname]["driver"] = $lpl[1];
73 if(file_exists("/sys/class/net/$fname/mtu")) $this->config["hardware"]["netdev"][$fname]["mtu"] = trim(file_get_contents("/sys/class/net/$fname/mtu"));
74 if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = trim(file_get_contents("/sys/class/net/$fname/address"));
75 if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
76 if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
77 if(file_exists("/sys/class/net/$fname/duplex")) {
78 $myval = trim(file_get_contents("/sys/class/net/$fname/duplex"));
79 if($myval == null) $myval = "default";
80 $this->config["hardware"]["netdev"][$fname]["duplex"] = $myval;
82 if(file_exists("/sys/class/net/$fname/speed")) {
83 $myval = trim(file_get_contents("/sys/class/net/$fname/speed"));
84 if($myval == null) $myval = "default";
85 $this->config["hardware"]["netdev"][$fname]["speed"] = $myval;
96 function setConfig($config) {
97 $this->config = $config;
100 function applyConfig()
103 if(!$AM_DAEMON) return true;
111 function loadConfigFile($file=null)
115 if($this->config["status"] == "nodir") {
117 } else $file = $this->config_file;
120 if(!file_exists($file)) return "noconf";
122 $fp = fopen($file, "r");
125 while($line = fgets($fp)) {
127 echo "read line $line\n";
128 if($line != "") $this->addConfigLine($line, $i++);
132 function resolveInterfaceName($name)
134 // $config only ever contains real interface names
135 if(isset($this->config["interface"]["dev"][$name])) return $name;
137 if(isset($this->config["interface"])) {
138 foreach($this->config["interface"]["dev"] as $key => $var) {
139 if(isset($var["name"])) {
140 if($var["name"] == $name) {
146 // have to assume its a hardware name and check /sys
147 if(file_exists("/sys/class/net/$name")) return $name;
153 function addConfigLine($line, $lineno=0)
155 $line_n = trim($line);
157 $expl = preg_split("/ +/", $line);
159 if(count($expl) < 2) {
161 error_log("Not a valid config line ($c), $line");
164 echo "process command ".$expl[0]."\n";
166 // TODO: when dealing with descriptions, we need to deal with a null description (i.e. delete the description)
167 // find a description
169 $hasdescription = false;
170 for($i=0; $i<count($expl); $i++) {
171 if($hasdescription !== false) {
172 if($description != null) {
175 $description .= $expl[$i];
177 if($expl[$i] == "#") {
178 $hasdescription = $i;
182 // now rebuild the array if there was a description
183 if($hasdescription) {
184 for($i=0; $i<$hasdescription; $i++) {
185 $expl_r[$i] = $expl[$i];
190 // this is a special case for debugging
191 if($expl[0] == "hold") {
192 error_log("command is $line");
197 // check for delete on the line
198 if($expl[0] == "delete") {
201 // re-order the array
202 for($i=0; $i < (count($expl)-1); $i++) {
203 $expl[$i] = $expl[$i+1];
205 unset($expl[count($expl)-1]);
210 // check for delete on the line
211 if($expl[0] == "modify") {
214 // re-order the array
215 for($i=0; $i < (count($expl)-1); $i++) {
216 $expl[$i] = $expl[$i+1];
218 unset($expl[count($expl)-1]);
223 // set the hostname to $1
225 unset($this->config["hostname"]);
227 $this->config["hostname"] = $expl[1];
232 // set the hostname to $1
234 unset($this->config["domainname"]);
236 $this->config["domainname"] = $expl[1];
242 error_log("delete zone ".$expl[2]);
243 unset($this->config["zone"][$expl[2]]);
244 if(count($this->config["zone"]) < 1) {
245 error_log("zone now empty, delete zones");
246 unset($this->config["zone"]);
249 if($hasdescription) {
250 $this->config["zone"][$expl[2]]["description"] = $description;
252 $this->config["zone"][$expl[2]]["name"] = true;
259 $int = $this->resolveInterfaceName($expl[2]);
261 if($int != $expl[2]) {
262 error_log("resolved int as $int from ".$expl[2]);
265 error_log("got config for interface ".$expl[2]." but i cannot find it, i will assume its hardware for now");
268 if($hasdescription) {
269 $this->config["interface"]["dev"][$int]["description"] = $description;
274 $this->config["interface"]["dev"]["$int"]["address4"] = $expl[4];
277 $this->config["interface"]["dev"]["$int"]["address6"] = $expl[4];
280 $this->config["interface"]["dev"]["$int"]["name"] = $expl[4];
283 $this->config["interface"]["dev"]["$int"]["status"] = $expl[4];
286 $this->config["interface"]["dev"]["$int"]["mtu"] = $expl[4];
289 $this->config["interface"]["dev"]["$int"]["zone"] = $expl[4];
292 $this->config["interface"]["dev"]["$int"]["speed"] = $expl[4];
295 $this->config["interface"]["$int"]["duplex"] = $expl[4];
304 $this->config["vlan"][$name]["parent"] = $from;
305 $this->config["vlan"][$name]["id"] = $vlanid;
306 if($hasdescription) {
307 $this->config["vlan"][$name]["description"] = $description;
313 for($i=5; $i<count($expl); $i++) {
314 $this->config["lag"][$name][$i-5] = $expl[$i];
316 if($hasdescription) {
317 $this->config["lag"][$name]["description"] = $description;
323 for($i=5; $i<count($expl); $i++) {
324 $this->config["bridge"][$name][$i-5] = $expl[$i];
326 if($hasdescription) {
327 $this->config["bridge"][$name]["description"] = $description;
337 unset($this->config["login"][$expl[1]]);
339 $this->config["login"][$expl[1]] = $expl[3];
340 if($hasdescription) {
341 $this->config["login"][$expl[1]]["description"] = $description;
348 unset($this->config["route4"][$expl[1]]);
354 $this->config["route4"][$route]["address"] = $dest;
356 $this->config["route4"][$route]["device"] = $dest;
358 if(isset($expl[4])) {
359 if($expl[4] == "dev") {
360 if(isset($expl[5])) {
361 $this->config["route4"][$route]["device"] = $expl[5];
365 if($hasdescription) {
366 $this->config["route4"][$route]["description"] = $description;
374 unset($this->config["route6"][$expl[1]]);
380 $this->config["route6"][$route]["address"] = $dest;
382 $this->config["route6"][$route]["device"] = $dest;
384 if(isset($expl[4])) {
385 if($expl[4] == "dev") {
386 if(isset($expl[5])) {
387 $this->config["route6"][$route]["device"] = $expl[5];
391 if($hasdescription) {
392 $this->config["route6"][$route]["description"] = $description;
395 // here we should check "$route"
400 if(isset($this->config["dns"]["nservers"])) {
401 $dns_servers = $this->config["dns"]["nservers"];
405 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
406 if($hasdescription) {
407 $this->config["dns"]["server"][$dns_servers]["description"] = $description;
409 $this->config["dns"]["nservers"] = $dns_servers+1;
414 if(isset($this->config["ntp"]["nservers"])) {
415 $ntp_servers = $this->config["ntp"]["nservers"];
419 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
420 if($hasdescription) {
421 $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
423 $this->config["ntp"]["nservers"] = $ntp_servers+1;
429 echo "Errr, unknown config directive on line $lineno, $line\n";
435 function saveConfigFile($file)
438 if(!$AM_DAEMON) return true;
442 private $config_file;