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)
12 $this->config_file = null;
13 if(file_exists("../var/fw.conf")) {
14 $this->config_file = realpath("../var/fw.conf");
15 $this->config["status"] = "conf";
18 if(file_exists("/var/lib/fwd/fw.conf")) {
19 $this->config_file = "/var/lib/fwd/fw.conf";
20 $this->config["status"] = "conf";
24 if($this->config_file == null) {
25 $this->config["status"] = "noconf";
26 if(is_dir("/var/lib/fwd/")) {
27 echo "no config file found. Will use ../var/fw.conf for now\n";
28 $this->config_file = "/var/lib/fwd/fw.conf";
29 } else if(is_dir("../var/")) {
30 echo "no config file found. Will use ../var/fw.conf for now\n";
31 $this->config_file = "../var/";
33 echo "No directory where i can create a config, bailing\n";
34 $this->config["status"] = "unconf";
43 function bootHardware()
45 $this->findHardware();
47 $mycomm = new Comms();
48 $mycomm->putConfig($this->config, 1);
57 if($this->config["status"] == "conf") {
58 $this->loadConfigFile($this->config_file);
60 $this->findHardware();
62 $mycomm = new Comms();
63 $mycomm->putConfig($this->config);
64 $this->boot_config = $mycomm->getConfig(1);
67 $mycomm = new Comms();
68 $this->config = $mycomm->getConfig(0);
69 $this->boot_config = $mycomm->getConfig(1);
74 function findHardware()
78 // first, network interfaces
79 $dh = opendir("/sys/class/net/");
80 while(($fname = readdir($dh)) !== false) {
81 if($fname != "." && $fname != ".." && $fname != "lo" && is_dir("/sys/class/net/$fname/")) {
82 $this->config["hardware"]["netdev"][$fname]["name"] = $fname;
83 // now read drive name if you can
84 if(file_exists("/sys/class/net/$fname/device/uevent")) $fp = fopen("/sys/class/net/$fname/device/uevent", "r");
88 $line = trim(fgets($fp));
89 $lpl = explode("=", $line);
90 if($lpl[0] == "DRIVER") {
91 $this->config["hardware"]["netdev"][$fname]["driver"] = $lpl[1];
96 if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
97 if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
104 return $this->config;
107 function getBootConfig()
109 return $this->boot_config;
112 function applyConfig()
115 if(!$AM_DAEMON) return true;
121 function loadConfigFile($file)
124 if(!$AM_DAEMON) return true;
126 $fp = fopen($file, "r");
129 while($line = fgets($fp)) {
131 echo "read line $line\n";
132 if($line != "") $this->parseLine($line, $i++);
136 function parseLine($line, $lineno)
138 $expl = preg_split("/ +/", $line);
140 echo "process command ".$expl[0]."\n";
142 // find a description
144 $hasdescription = false;
145 for($i=0; $i<count($expl); $i++) {
146 if($hasdescription !== false) {
147 if($description != null) {
150 $description .= $expl[$i];
152 if($expl[$i] == "#") {
153 $hasdescription = $i;
157 // now rebuild the array if there was one
158 if($hasdescription) {
159 for($i=0; $i<$hasdescription; $i++) {
160 $expl_r[$i] = $expl[$i];
167 // set the hostname to $1
168 $this->config["hostname"] = $expl[1];
172 // set the hostname to $1
173 $this->config["domainname"] = $expl[1];
177 if($hasdescription) {
178 $this->config["zone"][$expl[2]]["description"] = $description;
180 $this->config["zone"][$expl[2]]["name"] = true;
188 if($hasdescription) {
189 $this->config["interface"][$int]["description"] = $description;
194 $this->config["interface"]["$int"]["address4"] = $expl[4];
197 $this->config["interface"]["$int"]["address6"] = $expl[4];
200 $this->config["interface"]["$int"]["name"] = $expl[4];
203 $this->config["interface"]["$int"]["status"] = $expl[4];
206 $this->config["interface"]["$int"]["mtu"] = $expl[4];
209 $this->config["interface"]["$int"]["zone"] = $expl[4];
212 $this->config["interface"]["$int"]["speed"] = $expl[4];
215 $this->config["interface"]["$int"]["duplex"] = $expl[4];
224 $this->config["vlan"][$name]["parent"] = $from;
225 $this->config["vlan"][$name]["id"] = $vlanid;
226 if($hasdescription) {
227 $this->config["vlan"][$name]["description"] = $description;
233 for($i=5; $i<count($expl); $i++) {
234 $this->config["lag"][$name][$i-5] = $expl[$i];
236 if($hasdescription) {
237 $this->config["lag"][$name]["description"] = $description;
243 for($i=5; $i<count($expl); $i++) {
244 $this->config["bridge"][$name][$i-5] = $expl[$i];
246 if($hasdescription) {
247 $this->config["bridge"][$name]["description"] = $description;
256 $this->config["login"][$expl[1]] = $expl[3];
257 if($hasdescription) {
258 $this->config["login"][$expl[1]]["description"] = $description;
267 $this->config["route4"][$route]["address"] = $dest;
269 $this->config["route4"][$route]["device"] = $dest;
271 if(isset($expl[4])) {
272 if($expl[4] == "dev") {
273 if(isset($expl[5])) {
274 $this->config["route4"][$route]["device"] = $expl[5];
278 if($hasdescription) {
279 $this->config["route4"][$route]["description"] = $description;
289 $this->config["route6"][$route]["address"] = $dest;
291 $this->config["route6"][$route]["device"] = $dest;
293 if(isset($expl[4])) {
294 if($expl[4] == "dev") {
295 if(isset($expl[5])) {
296 $this->config["route6"][$route]["device"] = $expl[5];
300 if($hasdescription) {
301 $this->config["route6"][$route]["description"] = $description;
304 // here we should check "$route"
309 if(isset($this->config["dns"]["nservers"])) {
310 $dns_servers = $this->config["dns"]["nservers"];
314 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
315 if($hasdescription) {
316 $this->config["dns"]["server"][$dns_servers]["description"] = $description;
318 $this->config["dns"]["nservers"] = $dns_servers+1;
323 if(isset($this->config["ntp"]["nservers"])) {
324 $ntp_servers = $this->config["ntp"]["nservers"];
328 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
329 if($hasdescription) {
330 $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
332 $this->config["ntp"]["nservers"] = $ntp_servers+1;
338 echo "Errr, unknown config directive on line $lineno, $line\n";
344 function saveConfigFile($file)
347 if(!$AM_DAEMON) return true;
351 private $config_file;
353 private $boot_config;