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"] = "nodir";
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/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = file_get_contents("/sys/class/net/$fname/address");
97 if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
98 if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
105 return $this->config;
108 function getBootConfig()
110 return $this->boot_config;
113 function applyConfig()
116 if(!$AM_DAEMON) return true;
122 function loadConfigFile($file)
125 if(!$AM_DAEMON) return true;
127 $fp = fopen($file, "r");
130 while($line = fgets($fp)) {
132 echo "read line $line\n";
133 if($line != "") $this->parseLine($line, $i++);
137 function parseLine($line, $lineno)
139 $expl = preg_split("/ +/", $line);
141 echo "process command ".$expl[0]."\n";
143 // find a description
145 $hasdescription = false;
146 for($i=0; $i<count($expl); $i++) {
147 if($hasdescription !== false) {
148 if($description != null) {
151 $description .= $expl[$i];
153 if($expl[$i] == "#") {
154 $hasdescription = $i;
158 // now rebuild the array if there was one
159 if($hasdescription) {
160 for($i=0; $i<$hasdescription; $i++) {
161 $expl_r[$i] = $expl[$i];
168 // set the hostname to $1
169 $this->config["hostname"] = $expl[1];
173 // set the hostname to $1
174 $this->config["domainname"] = $expl[1];
178 if($hasdescription) {
179 $this->config["zone"][$expl[2]]["description"] = $description;
181 $this->config["zone"][$expl[2]]["name"] = true;
189 if($hasdescription) {
190 $this->config["interface"][$int]["description"] = $description;
195 $this->config["interface"]["$int"]["address4"] = $expl[4];
198 $this->config["interface"]["$int"]["address6"] = $expl[4];
201 $this->config["interface"]["$int"]["name"] = $expl[4];
204 $this->config["interface"]["$int"]["status"] = $expl[4];
207 $this->config["interface"]["$int"]["mtu"] = $expl[4];
210 $this->config["interface"]["$int"]["zone"] = $expl[4];
213 $this->config["interface"]["$int"]["speed"] = $expl[4];
216 $this->config["interface"]["$int"]["duplex"] = $expl[4];
225 $this->config["vlan"][$name]["parent"] = $from;
226 $this->config["vlan"][$name]["id"] = $vlanid;
227 if($hasdescription) {
228 $this->config["vlan"][$name]["description"] = $description;
234 for($i=5; $i<count($expl); $i++) {
235 $this->config["lag"][$name][$i-5] = $expl[$i];
237 if($hasdescription) {
238 $this->config["lag"][$name]["description"] = $description;
244 for($i=5; $i<count($expl); $i++) {
245 $this->config["bridge"][$name][$i-5] = $expl[$i];
247 if($hasdescription) {
248 $this->config["bridge"][$name]["description"] = $description;
257 $this->config["login"][$expl[1]] = $expl[3];
258 if($hasdescription) {
259 $this->config["login"][$expl[1]]["description"] = $description;
268 $this->config["route4"][$route]["address"] = $dest;
270 $this->config["route4"][$route]["device"] = $dest;
272 if(isset($expl[4])) {
273 if($expl[4] == "dev") {
274 if(isset($expl[5])) {
275 $this->config["route4"][$route]["device"] = $expl[5];
279 if($hasdescription) {
280 $this->config["route4"][$route]["description"] = $description;
290 $this->config["route6"][$route]["address"] = $dest;
292 $this->config["route6"][$route]["device"] = $dest;
294 if(isset($expl[4])) {
295 if($expl[4] == "dev") {
296 if(isset($expl[5])) {
297 $this->config["route6"][$route]["device"] = $expl[5];
301 if($hasdescription) {
302 $this->config["route6"][$route]["description"] = $description;
305 // here we should check "$route"
310 if(isset($this->config["dns"]["nservers"])) {
311 $dns_servers = $this->config["dns"]["nservers"];
315 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
316 if($hasdescription) {
317 $this->config["dns"]["server"][$dns_servers]["description"] = $description;
319 $this->config["dns"]["nservers"] = $dns_servers+1;
324 if(isset($this->config["ntp"]["nservers"])) {
325 $ntp_servers = $this->config["ntp"]["nservers"];
329 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
330 if($hasdescription) {
331 $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
333 $this->config["ntp"]["nservers"] = $ntp_servers+1;
339 echo "Errr, unknown config directive on line $lineno, $line\n";
345 function saveConfigFile($file)
348 if(!$AM_DAEMON) return true;
352 private $config_file;
354 private $boot_config;