working on the interfaces page
[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($look_for_config = false)
8         {
9                 
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";
15                                 
16                         }
17                         if(file_exists("/var/lib/fwd/fw.conf")) {
18                                 $this->config_file = "/var/lib/fwd/fw.conf";
19                                 $this->config["status"] = "conf";
20                         }
21                         
22                         
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";
31                                 } else {
32                                         echo "No directory where i can create a config, bailing\n";
33                                         $this->config["status"] = "nodir";
34                                 }
35                         }
36                 }
37         }
38         
39         function loadConfig($config)
40         {
41                 $this->config = $config;
42         }
43         
44         function findHardware()
45         {
46                 
47                 
48                 // first, network interfaces
49                 $dh = opendir("/sys/class/net/");
50                 while(($fname = readdir($dh)) !== false) {
51                         if($fname != "." && $fname != ".." && $fname != "lo" && is_dir("/sys/class/net/$fname/")) {
52                                 $this->config["hardware"]["netdev"][$fname]["name"] = $fname;
53                                 // now read drive name if you can
54                                 if(file_exists("/sys/class/net/$fname/device/uevent")) $fp = fopen("/sys/class/net/$fname/device/uevent", "r");
55                                 else $fp = false;
56                                 if($fp) {
57                                         while(!feof($fp)) {
58                                                 $line = trim(fgets($fp));
59                                                 $lpl = explode("=", $line);
60                                                 if($lpl[0] == "DRIVER") {
61                                                         $this->config["hardware"]["netdev"][$fname]["driver"] = $lpl[1];
62                                                 }
63                                         }
64                                         fclose($fp);
65                                 }
66                                 if(file_exists("/sys/class/net/$fname/mtu")) $this->config["hardware"]["netdev"][$fname]["mtu"] = trim(file_get_contents("/sys/class/net/$fname/mtu"));
67                                 if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = trim(file_get_contents("/sys/class/net/$fname/address"));
68                                 if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
69                                 if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
70                                 if(file_exists("/sys/class/net/$fname/duplex")) {
71                                         $myval = trim(file_get_contents("/sys/class/net/$fname/duplex"));
72                                         if($myval == null) $myval = "default";
73                                         $this->config["hardware"]["netdev"][$fname]["duplex"] = $myval; 
74                                 }
75                                 if(file_exists("/sys/class/net/$fname/speed")) {
76                                         $myval = trim(file_get_contents("/sys/class/net/$fname/speed"));
77                                         if($myval == null) $myval = "default";
78                                         $this->config["hardware"]["netdev"][$fname]["speed"] = $myval; 
79                                 }
80                         }
81                 }
82         }
83         
84         function getConfig()
85         {
86                 return $this->config;
87         }
88         
89         function setConfig($config) {
90                 $this->config = $config;
91         }
92         
93         function applyConfig()
94         {
95                 global $AM_DAEMON;
96                 if(!$AM_DAEMON) return true;
97                 
98                 // oh the joy
99                 return true;
100         }
101         
102
103         
104         function loadConfigFile($file=null)
105         {
106                 
107                 if($file == null) {
108                         if($this->config["status"] == "nodir") {
109                                 return "nodir";
110                         } else $file = $this->config_file;
111                 }
112                 
113                 if(!file_exists($file)) return "noconf";
114                 
115                 $fp = fopen($file, "r");
116                 
117                 $i = 1;
118                 while($line = fgets($fp)) {
119                         $line = trim($line);
120                         echo "read line $line\n";
121                         if($line != "") $this->addConfigLine($line, $i++);
122                 }
123         }
124         
125         function resolveInterfaceName($name)
126         {
127                 // $config only ever contains real interface names
128                 if(isset($this->config["interface"]["dev"][$name])) return $name;
129                 
130                 if(isset($this->config["interface"])) {
131                         foreach($this->config["interface"]["dev"] as $key => $var) {
132                                 if(isset($var["name"])) {
133                                         if($var["name"] == $name) {
134                                                 return $key;
135                                         }
136                                 }
137                         }
138                 } else {
139                         // have to assume its a hardware name and check /sys
140                         if(file_exists("/sys/class/net/$name")) return $name;
141                 }
142                 
143                 return null;
144         }
145         
146         function addConfigLine($line, $lineno=0)
147         {
148                 $line_n = trim($line);
149                 $line = $line_n;
150                 $expl = preg_split("/ +/", $line);
151                 
152                 if(count($expl) < 2) {
153                         $c = count($expl);
154                         error_log("Not a valid config line ($c), $line");
155                         return 0;
156                 }
157                 echo "process command ".$expl[0]."\n";
158                 
159                 // find a description
160                 $description = null;
161                 $hasdescription = false;
162                 for($i=0; $i<count($expl); $i++) {
163                         if($hasdescription !== false) {
164                                 if($description != null) {
165                                         $description .= " ";
166                                 }
167                                 $description .= $expl[$i];
168                         } 
169                         if($expl[$i] == "#") {
170                                 $hasdescription = $i;
171                         }
172                 }
173                 
174                 // now rebuild the array if there was a description
175                 if($hasdescription) {
176                         for($i=0; $i<$hasdescription; $i++) {
177                                 $expl_r[$i] = $expl[$i];
178                         }
179                         $expl = $expl_r;
180                 }
181                 
182                 $delete = false;
183                 // check for delete on the line
184                 if($expl[0] == "delete") {
185                         $delete = true;
186                         
187                         // re-order the array
188                         for($i=0; $i < (count($expl)-1); $i++) {
189                         $expl[$i] = $expl[$i+1];
190                         }
191                         unset($expl[count($expl)-1]);
192                 }
193                 
194                 // now for modify
195                 $modify = false;
196                 // check for delete on the line
197                 if($expl[0] == "modify") {
198                         $modify = true;
199                         
200                         // re-order the array
201                         for($i=0; $i < (count($expl)-1); $i++) {
202                         $expl[$i] = $expl[$i+1];
203                         }
204                         unset($expl[count($expl)-1]);
205                 }
206                 
207                 switch($expl[0]) {
208                         case "hostname":
209                                 // set the hostname to $1
210                                 if($delete) {
211                                         unset($this->config["hostname"]);                               
212                                 } else {
213                                         $this->config["hostname"] = $expl[1];
214                                 }
215                                 break;
216                                 
217                         case "domainname":
218                                 // set the hostname to $1
219                                 if($delete) {
220                                         unset($this->config["domainname"]);
221                                 } else {
222                                         $this->config["domainname"] = $expl[1];
223                                 }
224                                 break;
225                                 
226                         case "zone":
227                                 if($delete) {
228                                         error_log("delete zone ".$expl[2]);
229                                         unset($this->config["zone"][$expl[2]]);
230                                         if(count($this->config["zone"]) < 1) {
231                                                 error_log("zone now empty, delete zones");
232                                                 unset($this->config["zone"]);
233                                         }
234                                 } else {
235                                         if($hasdescription) {
236                                                 $this->config["zone"][$expl[2]]["description"] = $description;
237                                         }
238                                         $this->config["zone"][$expl[2]]["name"] = true;
239                                 }
240                                 break;
241                                 
242                         case "interface":
243                                 switch($expl[1]) {
244                                         case "dev":
245                                                 $int = $this->resolveInterfaceName($expl[2]);
246                                                 
247                                                 if($int != $expl[2]) {
248                                                         error_log("resolved int as $int from ".$expl[2]);
249                                                 }
250                                                 if($int == null) {
251                                                         error_log("got config for interface ".$expl[2]." but i cannot find it, i will assume its hardware for now");
252                                                         $int = $expl[2];
253                                                 }
254                                                 if($hasdescription) {
255                                                         $this->config["interface"]["dev"][$int]["description"] = $description;
256                                                 }
257         
258                                                 switch($expl[3]) {
259                                                         case "address4":
260                                                                 $this->config["interface"]["dev"]["$int"]["address4"] = $expl[4];
261                                                                 break;
262                                                         case "address6":
263                                                                 $this->config["interface"]["dev"]["$int"]["address6"] = $expl[4];
264                                                                 break;
265                                                         case "name":
266                                                                 $this->config["interface"]["dev"]["$int"]["name"] = $expl[4];
267                                                                 break;
268                                                         case "status":
269                                                                 $this->config["interface"]["dev"]["$int"]["status"] = $expl[4];
270                                                                 break;
271                                                         case "mtu":
272                                                                 $this->config["interface"]["dev"]["$int"]["mtu"] = $expl[4];
273                                                                 break;
274                                                         case "zone":
275                                                                 $this->config["interface"]["dev"]["$int"]["zone"] = $expl[4];
276                                                                 break;
277                                                         case "speed":
278                                                                 $this->config["interface"]["dev"]["$int"]["speed"] = $expl[4];
279                                                                 break;
280                                                         case "duplex":
281                                                                 $this->config["interface"]["$int"]["duplex"] = $expl[4];
282                                                                 break;
283                                                 }
284                                                 break;
285                                                 
286                                         case "vlan":
287                                                 $vlanid = $expl[2];
288                                                 $name = $expl[4];
289                                                 $from = $expl[6];
290                                                 $this->config["vlan"][$name]["parent"] = $from;
291                                                 $this->config["vlan"][$name]["id"] = $vlanid;
292                                                 if($hasdescription) {
293                                                         $this->config["vlan"][$name]["description"] = $description;
294                                                 }
295                                                 break;
296                                                 
297                                         case "lag":
298                                                 $name = $expl[3];
299                                                 for($i=5; $i<count($expl); $i++) {
300                                                         $this->config["lag"][$name][$i-5] = $expl[$i];
301                                                 }
302                                                 if($hasdescription) {
303                                                         $this->config["lag"][$name]["description"] = $description;
304                                                 }
305                                                 break;
306
307                                         case "bridge":
308                                                 $name = $expl[3];
309                                                 for($i=5; $i<count($expl); $i++) {
310                                                         $this->config["bridge"][$name][$i-5] = $expl[$i];
311                                                 }
312                                                 if($hasdescription) {
313                                                         $this->config["bridge"][$name]["description"] = $description;
314                                                 }
315                                                 break;
316                                                 
317                                 }
318                                 break;
319                                 
320                                 
321                         case "login":
322                                 if($delete) {
323                                         unset($this->config["login"][$expl[1]]);
324                                 } else {
325                                         $this->config["login"][$expl[1]] = $expl[3];
326                                         if($hasdescription) {
327                                                 $this->config["login"][$expl[1]]["description"] = $description;
328                                         }
329                                 }
330                                 break;
331                         
332                         case "route4":
333                                 if($delete) {
334                                         unset($this->config["route4"][$expl[1]]);
335                                 } else {
336                                         $route = $expl[1];
337                                         $via = $expl[2];
338                                         $dest = $expl[3];
339                                         if($via == "to") {
340                                                 $this->config["route4"][$route]["address"] = $dest;
341                                         } else {
342                                                 $this->config["route4"][$route]["device"] = $dest;
343                                         }
344                                         if(isset($expl[4])) {
345                                                 if($expl[4] == "dev") {
346                                                         if(isset($expl[5])) {
347                                                                 $this->config["route4"][$route]["device"] = $expl[5];
348                                                         }
349                                                 }
350                                         }
351                                         if($hasdescription) {
352                                                 $this->config["route4"][$route]["description"] = $description;
353                                         }
354                                 }
355                                 break;
356                                 
357                                 
358                         case "route6":
359                                 if($delete) {
360                                         unset($this->config["route6"][$expl[1]]);
361                                 } else {
362                                         $route = $expl[1];
363                                         $via = $expl[2];
364                                         $dest = $expl[3];
365                                         if($via == "to") {
366                                                 $this->config["route6"][$route]["address"] = $dest;
367                                         } else {
368                                                 $this->config["route6"][$route]["device"] = $dest;
369                                         }
370                                         if(isset($expl[4])) {
371                                                 if($expl[4] == "dev") {
372                                                         if(isset($expl[5])) {
373                                                                 $this->config["route6"][$route]["device"] = $expl[5];
374                                                         }
375                                                 }
376                                         }
377                                         if($hasdescription) {
378                                                 $this->config["route6"][$route]["description"] = $description;
379                                         }
380                                 }
381                                 // here we should check "$route"
382                                 break;
383                                 
384                                 
385                         case "dns":
386                                 if(isset($this->config["dns"]["nservers"])) {
387                                         $dns_servers = $this->config["dns"]["nservers"];
388                                 } else {
389                                         $dns_servers = 0;
390                                 }
391                                 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
392                                 if($hasdescription) {
393                                         $this->config["dns"]["server"][$dns_servers]["description"] = $description;
394                                 }
395                                 $this->config["dns"]["nservers"] = $dns_servers+1;
396                                 break;
397                                 
398                                 
399                         case "ntp":
400                                 if(isset($this->config["ntp"]["nservers"])) {
401                                         $ntp_servers = $this->config["ntp"]["nservers"];
402                                 } else {
403                                         $ntp_servers = 0;
404                                 }
405                                 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
406                                 if($hasdescription) {
407                                         $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
408                                 }
409                                 $this->config["ntp"]["nservers"] = $ntp_servers+1;
410                                 break;
411                                 
412                                 
413                                 
414                         default:
415                                 echo "Errr, unknown config directive on line $lineno, $line\n";
416                 }
417                 
418
419         }
420         
421         function saveConfigFile($file)
422         {
423                 global $AM_DAEMON;
424                 if(!$AM_DAEMON) return true;
425                 
426         }       
427         
428         private $config_file;
429         private $config;
430 };
431
432 ?>