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