7bffc1d886e00aa1647397e9269ca2e46d7c2c68
[CBFWR.git] / libcbfwr / config.php
1 <?php
2
3 class Config {
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()
8         {
9                 global $AM_DAEMON;
10                 
11                 if($AM_DAEMON) {
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";
16                                 
17                         }
18                         if(file_exists("/var/lib/fwd/fw.conf")) {
19                                 $this->config_file = "/var/lib/fwd/fw.conf";
20                                 $this->config["status"] = "conf";
21                         }
22                         
23                         
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/";
32                                 } else {
33                                         echo "No directory where i can create a config, bailing\n";
34                                         $this->config["status"] = "nodir";
35                                 }
36                         }
37                         
38                         
39                         
40                 }
41         }
42         
43         function bootHardware()
44         {
45                 $this->findHardware();
46
47                 $mycomm = new Comms();
48                 $mycomm->putConfig($this->config, 1);
49                 
50         }
51         
52         function loadConfig()
53         {
54                 global $AM_DAEMON;
55                 
56                 if($AM_DAEMON) {
57                         if($this->config["status"] == "conf") { 
58                                 $this->loadConfigFile($this->config_file);
59                                 $this->applyConfig();
60                                 $this->findHardware();
61                                 
62                                 $mycomm = new Comms();
63                                 $mycomm->putConfig($this->config);
64                                 $this->boot_config = $mycomm->getConfig(1);
65                         }
66                 } else {
67                         $mycomm = new Comms();
68                         $this->config = $mycomm->getConfig(0);
69                         $this->boot_config = $mycomm->getConfig(1);
70                         
71                 }
72         }
73         
74         function findHardware()
75         {
76                 
77                 
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");
85                                 else $fp = false;
86                                 if($fp) {
87                                         while(!feof($fp)) {
88                                                 $line = trim(fgets($fp));
89                                                 $lpl = explode("=", $line);
90                                                 if($lpl[0] == "DRIVER") {
91                                                         $this->config["hardware"]["netdev"][$fname]["driver"] = $lpl[1];
92                                                 }
93                                         }
94                                         fclose($fp);
95                                 }
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;
99                         }
100                 }
101         }
102         
103         function getConfig()
104         {
105                 return $this->config;
106         }
107         
108         function getBootConfig()
109         {
110                 return $this->boot_config;
111         }
112         
113         function applyConfig()
114         {
115                 global $AM_DAEMON;
116                 if(!$AM_DAEMON) return true;
117                 
118                 // oh the joy
119                 return true;
120         }
121         
122         function loadConfigFile($file)
123         {
124                 global $AM_DAEMON;
125                 if(!$AM_DAEMON) return true;
126                 
127                 $fp = fopen($file, "r");
128                 
129                 $i = 1;
130                 while($line = fgets($fp)) {
131                         $line = trim($line);
132                         echo "read line $line\n";
133                         if($line != "") $this->parseLine($line, $i++);
134                 }
135         }
136         
137         function parseLine($line, $lineno)
138         {
139                 $expl = preg_split("/ +/", $line);
140                 
141                 echo "process command ".$expl[0]."\n";
142                 
143                 // find a description
144                 $description = null;
145                 $hasdescription = false;
146                 for($i=0; $i<count($expl); $i++) {
147                         if($hasdescription !== false) {
148                                 if($description != null) {
149                                         $description .= " ";
150                                 }
151                                 $description .= $expl[$i];
152                         } 
153                         if($expl[$i] == "#") {
154                                 $hasdescription = $i;
155                         }
156                 }
157                 
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];
162                         }
163                         $expl = $expl_r;
164                 }
165                 
166                 switch($expl[0]) {
167                         case "hostname":
168                                 // set the hostname to $1
169                                 $this->config["hostname"] = $expl[1];
170                                 break;
171                                 
172                         case "domainname":
173                                 // set the hostname to $1
174                                 $this->config["domainname"] = $expl[1];
175                                 break;
176                                 
177                         case "zone":
178                                 if($hasdescription) {
179                                         $this->config["zone"][$expl[2]]["description"] = $description;
180                                 }
181                                 $this->config["zone"][$expl[2]]["name"] = true;
182                                 break;
183                                 
184                         case "interface":
185                                 switch($expl[1]) {
186                                         case "dev":
187                                                 $int = $expl[2];
188
189                                                 if($hasdescription) {
190                                                         $this->config["interface"][$int]["description"] = $description;
191                                                 }
192         
193                                                 switch($expl[3]) {
194                                                         case "address4":
195                                                                 $this->config["interface"]["$int"]["address4"] = $expl[4];
196                                                                 break;
197                                                         case "address6":
198                                                                 $this->config["interface"]["$int"]["address6"] = $expl[4];
199                                                                 break;
200                                                         case "name":
201                                                                 $this->config["interface"]["$int"]["name"] = $expl[4];
202                                                                 break;
203                                                         case "status":
204                                                                 $this->config["interface"]["$int"]["status"] = $expl[4];
205                                                                 break;
206                                                         case "mtu":
207                                                                 $this->config["interface"]["$int"]["mtu"] = $expl[4];
208                                                                 break;
209                                                         case "zone":
210                                                                 $this->config["interface"]["$int"]["zone"] = $expl[4];
211                                                                 break;
212                                                         case "speed":
213                                                                 $this->config["interface"]["$int"]["speed"] = $expl[4];
214                                                                 break;
215                                                         case "duplex":
216                                                                 $this->config["interface"]["$int"]["duplex"] = $expl[4];
217                                                                 break;
218                                                 }
219                                                 break;
220                                                 
221                                         case "vlan":
222                                                 $vlanid = $expl[2];
223                                                 $name = $expl[4];
224                                                 $from = $expl[6];
225                                                 $this->config["vlan"][$name]["parent"] = $from;
226                                                 $this->config["vlan"][$name]["id"] = $vlanid;
227                                                 if($hasdescription) {
228                                                         $this->config["vlan"][$name]["description"] = $description;
229                                                 }
230                                                 break;
231                                                 
232                                         case "lag":
233                                                 $name = $expl[3];
234                                                 for($i=5; $i<count($expl); $i++) {
235                                                         $this->config["lag"][$name][$i-5] = $expl[$i];
236                                                 }
237                                                 if($hasdescription) {
238                                                         $this->config["lag"][$name]["description"] = $description;
239                                                 }
240                                                 break;
241
242                                         case "bridge":
243                                                 $name = $expl[3];
244                                                 for($i=5; $i<count($expl); $i++) {
245                                                         $this->config["bridge"][$name][$i-5] = $expl[$i];
246                                                 }
247                                                 if($hasdescription) {
248                                                         $this->config["bridge"][$name]["description"] = $description;
249                                                 }
250                                                 break;
251                                                 
252                                 }
253                                 break;
254                                 
255                                 
256                         case "login":
257                                 $this->config["login"][$expl[1]] = $expl[3];
258                                 if($hasdescription) {
259                                         $this->config["login"][$expl[1]]["description"] = $description;
260                                 }
261                                 break;
262                         
263                         case "route4":
264                                 $route = $expl[1];
265                                 $via = $expl[2];
266                                 $dest = $expl[3];
267                                 if($via == "to") {
268                                         $this->config["route4"][$route]["address"] = $dest;
269                                 } else {
270                                         $this->config["route4"][$route]["device"] = $dest;
271                                 }
272                                 if(isset($expl[4])) {
273                                         if($expl[4] == "dev") {
274                                                 if(isset($expl[5])) {
275                                                         $this->config["route4"][$route]["device"] = $expl[5];
276                                                 }
277                                         }
278                                 }
279                                 if($hasdescription) {
280                                         $this->config["route4"][$route]["description"] = $description;
281                                 }
282                                 break;
283                                 
284                                 
285                         case "route6":
286                                 $route = $expl[1];
287                                 $via = $expl[2];
288                                 $dest = $expl[3];
289                                 if($via == "to") {
290                                         $this->config["route6"][$route]["address"] = $dest;
291                                 } else {
292                                         $this->config["route6"][$route]["device"] = $dest;
293                                 }
294                                 if(isset($expl[4])) {
295                                         if($expl[4] == "dev") {
296                                                 if(isset($expl[5])) {
297                                                         $this->config["route6"][$route]["device"] = $expl[5];
298                                                 }
299                                         }
300                                 }
301                                 if($hasdescription) {
302                                         $this->config["route6"][$route]["description"] = $description;
303                                 }
304                                 
305                                 // here we should check "$route"
306                                 break;
307                                 
308                                 
309                         case "dns":
310                                 if(isset($this->config["dns"]["nservers"])) {
311                                         $dns_servers = $this->config["dns"]["nservers"];
312                                 } else {
313                                         $dns_servers = 0;
314                                 }
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;
318                                 }
319                                 $this->config["dns"]["nservers"] = $dns_servers+1;
320                                 break;
321                                 
322                                 
323                         case "ntp":
324                                 if(isset($this->config["ntp"]["nservers"])) {
325                                         $ntp_servers = $this->config["ntp"]["nservers"];
326                                 } else {
327                                         $ntp_servers = 0;
328                                 }
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;
332                                 }
333                                 $this->config["ntp"]["nservers"] = $ntp_servers+1;
334                                 break;
335                                 
336                                 
337                                 
338                         default:
339                                 echo "Errr, unknown config directive on line $lineno, $line\n";
340                 }
341                 
342
343         }
344         
345         function saveConfigFile($file)
346         {
347                 global $AM_DAEMON;
348                 if(!$AM_DAEMON) return true;
349                 
350         }       
351         
352         private $config_file;
353         private $config;
354         private $boot_config;
355 };
356
357 ?>