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 // find a description
168 $hasdescription = false;
169 for($i=0; $i<count($expl); $i++) {
170 if($hasdescription !== false) {
171 if($description != null) {
174 $description .= $expl[$i];
176 if($expl[$i] == "#") {
177 $hasdescription = $i;
181 // now rebuild the array if there was a description
182 if($hasdescription) {
183 for($i=0; $i<$hasdescription; $i++) {
184 $expl_r[$i] = $expl[$i];
190 // check for delete on the line
191 if($expl[0] == "delete") {
194 // re-order the array
195 for($i=0; $i < (count($expl)-1); $i++) {
196 $expl[$i] = $expl[$i+1];
198 unset($expl[count($expl)-1]);
203 // check for delete on the line
204 if($expl[0] == "modify") {
207 // re-order the array
208 for($i=0; $i < (count($expl)-1); $i++) {
209 $expl[$i] = $expl[$i+1];
211 unset($expl[count($expl)-1]);
216 // set the hostname to $1
218 unset($this->config["hostname"]);
220 $this->config["hostname"] = $expl[1];
225 // set the hostname to $1
227 unset($this->config["domainname"]);
229 $this->config["domainname"] = $expl[1];
235 error_log("delete zone ".$expl[2]);
236 unset($this->config["zone"][$expl[2]]);
237 if(count($this->config["zone"]) < 1) {
238 error_log("zone now empty, delete zones");
239 unset($this->config["zone"]);
242 if($hasdescription) {
243 $this->config["zone"][$expl[2]]["description"] = $description;
245 $this->config["zone"][$expl[2]]["name"] = true;
252 $int = $this->resolveInterfaceName($expl[2]);
254 if($int != $expl[2]) {
255 error_log("resolved int as $int from ".$expl[2]);
258 error_log("got config for interface ".$expl[2]." but i cannot find it, i will assume its hardware for now");
261 if($hasdescription) {
262 $this->config["interface"]["dev"][$int]["description"] = $description;
267 $this->config["interface"]["dev"]["$int"]["address4"] = $expl[4];
270 $this->config["interface"]["dev"]["$int"]["address6"] = $expl[4];
273 $this->config["interface"]["dev"]["$int"]["name"] = $expl[4];
276 $this->config["interface"]["dev"]["$int"]["status"] = $expl[4];
279 $this->config["interface"]["dev"]["$int"]["mtu"] = $expl[4];
282 $this->config["interface"]["dev"]["$int"]["zone"] = $expl[4];
285 $this->config["interface"]["dev"]["$int"]["speed"] = $expl[4];
288 $this->config["interface"]["$int"]["duplex"] = $expl[4];
297 $this->config["vlan"][$name]["parent"] = $from;
298 $this->config["vlan"][$name]["id"] = $vlanid;
299 if($hasdescription) {
300 $this->config["vlan"][$name]["description"] = $description;
306 for($i=5; $i<count($expl); $i++) {
307 $this->config["lag"][$name][$i-5] = $expl[$i];
309 if($hasdescription) {
310 $this->config["lag"][$name]["description"] = $description;
316 for($i=5; $i<count($expl); $i++) {
317 $this->config["bridge"][$name][$i-5] = $expl[$i];
319 if($hasdescription) {
320 $this->config["bridge"][$name]["description"] = $description;
330 unset($this->config["login"][$expl[1]]);
332 $this->config["login"][$expl[1]] = $expl[3];
333 if($hasdescription) {
334 $this->config["login"][$expl[1]]["description"] = $description;
341 unset($this->config["route4"][$expl[1]]);
347 $this->config["route4"][$route]["address"] = $dest;
349 $this->config["route4"][$route]["device"] = $dest;
351 if(isset($expl[4])) {
352 if($expl[4] == "dev") {
353 if(isset($expl[5])) {
354 $this->config["route4"][$route]["device"] = $expl[5];
358 if($hasdescription) {
359 $this->config["route4"][$route]["description"] = $description;
367 unset($this->config["route6"][$expl[1]]);
373 $this->config["route6"][$route]["address"] = $dest;
375 $this->config["route6"][$route]["device"] = $dest;
377 if(isset($expl[4])) {
378 if($expl[4] == "dev") {
379 if(isset($expl[5])) {
380 $this->config["route6"][$route]["device"] = $expl[5];
384 if($hasdescription) {
385 $this->config["route6"][$route]["description"] = $description;
388 // here we should check "$route"
393 if(isset($this->config["dns"]["nservers"])) {
394 $dns_servers = $this->config["dns"]["nservers"];
398 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
399 if($hasdescription) {
400 $this->config["dns"]["server"][$dns_servers]["description"] = $description;
402 $this->config["dns"]["nservers"] = $dns_servers+1;
407 if(isset($this->config["ntp"]["nservers"])) {
408 $ntp_servers = $this->config["ntp"]["nservers"];
412 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
413 if($hasdescription) {
414 $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
416 $this->config["ntp"]["nservers"] = $ntp_servers+1;
422 echo "Errr, unknown config directive on line $lineno, $line\n";
428 function saveConfigFile($file)
431 if(!$AM_DAEMON) return true;
435 private $config_file;