2f6ce0b7704fe1c68e89530f921f5a6299ae54be
[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                 // find a description
167                 $description = null;
168                 $hasdescription = false;
169                 for($i=0; $i<count($expl); $i++) {
170                         if($hasdescription !== false) {
171                                 if($description != null) {
172                                         $description .= " ";
173                                 }
174                                 $description .= $expl[$i];
175                         } 
176                         if($expl[$i] == "#") {
177                                 $hasdescription = $i;
178                         }
179                 }
180                 
181                 // now rebuild the array if there was a description
182                 if($hasdescription) {
183                         for($i=0; $i<$hasdescription; $i++) {
184                                 $expl_r[$i] = $expl[$i];
185                         }
186                         $expl = $expl_r;
187                 }
188                 
189                 $delete = false;
190                 // check for delete on the line
191                 if($expl[0] == "delete") {
192                         $delete = true;
193                         
194                         // re-order the array
195                         for($i=0; $i < (count($expl)-1); $i++) {
196                         $expl[$i] = $expl[$i+1];
197                         }
198                         unset($expl[count($expl)-1]);
199                 }
200                 
201                 // now for modify
202                 $modify = false;
203                 // check for delete on the line
204                 if($expl[0] == "modify") {
205                         $modify = true;
206                         
207                         // re-order the array
208                         for($i=0; $i < (count($expl)-1); $i++) {
209                         $expl[$i] = $expl[$i+1];
210                         }
211                         unset($expl[count($expl)-1]);
212                 }
213                 
214                 switch($expl[0]) {
215                         case "hostname":
216                                 // set the hostname to $1
217                                 if($delete) {
218                                         unset($this->config["hostname"]);                               
219                                 } else {
220                                         $this->config["hostname"] = $expl[1];
221                                 }
222                                 break;
223                                 
224                         case "domainname":
225                                 // set the hostname to $1
226                                 if($delete) {
227                                         unset($this->config["domainname"]);
228                                 } else {
229                                         $this->config["domainname"] = $expl[1];
230                                 }
231                                 break;
232                                 
233                         case "zone":
234                                 if($delete) {
235                                         error_log("delete zone ".$expl[2]);
236                                         unset($this->config["zone"][$expl[2]]);
237                                         if(count($this->config["zone"]) < 1) {
238                                                 error_log("zone now empty, delete zones");
239                                                 unset($this->config["zone"]);
240                                         }
241                                 } else {
242                                         if($hasdescription) {
243                                                 $this->config["zone"][$expl[2]]["description"] = $description;
244                                         }
245                                         $this->config["zone"][$expl[2]]["name"] = true;
246                                 }
247                                 break;
248                                 
249                         case "interface":
250                                 switch($expl[1]) {
251                                         case "dev":
252                                                 $int = $this->resolveInterfaceName($expl[2]);
253                                                 
254                                                 if($int != $expl[2]) {
255                                                         error_log("resolved int as $int from ".$expl[2]);
256                                                 }
257                                                 if($int == null) {
258                                                         error_log("got config for interface ".$expl[2]." but i cannot find it, i will assume its hardware for now");
259                                                         $int = $expl[2];
260                                                 }
261                                                 if($hasdescription) {
262                                                         $this->config["interface"]["dev"][$int]["description"] = $description;
263                                                 }
264         
265                                                 switch($expl[3]) {
266                                                         case "address4":
267                                                                 $this->config["interface"]["dev"]["$int"]["address4"] = $expl[4];
268                                                                 break;
269                                                         case "address6":
270                                                                 $this->config["interface"]["dev"]["$int"]["address6"] = $expl[4];
271                                                                 break;
272                                                         case "name":
273                                                                 $this->config["interface"]["dev"]["$int"]["name"] = $expl[4];
274                                                                 break;
275                                                         case "status":
276                                                                 $this->config["interface"]["dev"]["$int"]["status"] = $expl[4];
277                                                                 break;
278                                                         case "mtu":
279                                                                 $this->config["interface"]["dev"]["$int"]["mtu"] = $expl[4];
280                                                                 break;
281                                                         case "zone":
282                                                                 $this->config["interface"]["dev"]["$int"]["zone"] = $expl[4];
283                                                                 break;
284                                                         case "speed":
285                                                                 $this->config["interface"]["dev"]["$int"]["speed"] = $expl[4];
286                                                                 break;
287                                                         case "duplex":
288                                                                 $this->config["interface"]["$int"]["duplex"] = $expl[4];
289                                                                 break;
290                                                 }
291                                                 break;
292                                                 
293                                         case "vlan":
294                                                 $vlanid = $expl[2];
295                                                 $name = $expl[4];
296                                                 $from = $expl[6];
297                                                 $this->config["vlan"][$name]["parent"] = $from;
298                                                 $this->config["vlan"][$name]["id"] = $vlanid;
299                                                 if($hasdescription) {
300                                                         $this->config["vlan"][$name]["description"] = $description;
301                                                 }
302                                                 break;
303                                                 
304                                         case "lag":
305                                                 $name = $expl[3];
306                                                 for($i=5; $i<count($expl); $i++) {
307                                                         $this->config["lag"][$name][$i-5] = $expl[$i];
308                                                 }
309                                                 if($hasdescription) {
310                                                         $this->config["lag"][$name]["description"] = $description;
311                                                 }
312                                                 break;
313
314                                         case "bridge":
315                                                 $name = $expl[3];
316                                                 for($i=5; $i<count($expl); $i++) {
317                                                         $this->config["bridge"][$name][$i-5] = $expl[$i];
318                                                 }
319                                                 if($hasdescription) {
320                                                         $this->config["bridge"][$name]["description"] = $description;
321                                                 }
322                                                 break;
323                                                 
324                                 }
325                                 break;
326                                 
327                                 
328                         case "login":
329                                 if($delete) {
330                                         unset($this->config["login"][$expl[1]]);
331                                 } else {
332                                         $this->config["login"][$expl[1]] = $expl[3];
333                                         if($hasdescription) {
334                                                 $this->config["login"][$expl[1]]["description"] = $description;
335                                         }
336                                 }
337                                 break;
338                         
339                         case "route4":
340                                 if($delete) {
341                                         unset($this->config["route4"][$expl[1]]);
342                                 } else {
343                                         $route = $expl[1];
344                                         $via = $expl[2];
345                                         $dest = $expl[3];
346                                         if($via == "to") {
347                                                 $this->config["route4"][$route]["address"] = $dest;
348                                         } else {
349                                                 $this->config["route4"][$route]["device"] = $dest;
350                                         }
351                                         if(isset($expl[4])) {
352                                                 if($expl[4] == "dev") {
353                                                         if(isset($expl[5])) {
354                                                                 $this->config["route4"][$route]["device"] = $expl[5];
355                                                         }
356                                                 }
357                                         }
358                                         if($hasdescription) {
359                                                 $this->config["route4"][$route]["description"] = $description;
360                                         }
361                                 }
362                                 break;
363                                 
364                                 
365                         case "route6":
366                                 if($delete) {
367                                         unset($this->config["route6"][$expl[1]]);
368                                 } else {
369                                         $route = $expl[1];
370                                         $via = $expl[2];
371                                         $dest = $expl[3];
372                                         if($via == "to") {
373                                                 $this->config["route6"][$route]["address"] = $dest;
374                                         } else {
375                                                 $this->config["route6"][$route]["device"] = $dest;
376                                         }
377                                         if(isset($expl[4])) {
378                                                 if($expl[4] == "dev") {
379                                                         if(isset($expl[5])) {
380                                                                 $this->config["route6"][$route]["device"] = $expl[5];
381                                                         }
382                                                 }
383                                         }
384                                         if($hasdescription) {
385                                                 $this->config["route6"][$route]["description"] = $description;
386                                         }
387                                 }
388                                 // here we should check "$route"
389                                 break;
390                                 
391                                 
392                         case "dns":
393                                 if(isset($this->config["dns"]["nservers"])) {
394                                         $dns_servers = $this->config["dns"]["nservers"];
395                                 } else {
396                                         $dns_servers = 0;
397                                 }
398                                 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
399                                 if($hasdescription) {
400                                         $this->config["dns"]["server"][$dns_servers]["description"] = $description;
401                                 }
402                                 $this->config["dns"]["nservers"] = $dns_servers+1;
403                                 break;
404                                 
405                                 
406                         case "ntp":
407                                 if(isset($this->config["ntp"]["nservers"])) {
408                                         $ntp_servers = $this->config["ntp"]["nservers"];
409                                 } else {
410                                         $ntp_servers = 0;
411                                 }
412                                 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
413                                 if($hasdescription) {
414                                         $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
415                                 }
416                                 $this->config["ntp"]["nservers"] = $ntp_servers+1;
417                                 break;
418                                 
419                                 
420                                 
421                         default:
422                                 echo "Errr, unknown config directive on line $lineno, $line\n";
423                 }
424                 
425
426         }
427         
428         function saveConfigFile($file)
429         {
430                 global $AM_DAEMON;
431                 if(!$AM_DAEMON) return true;
432                 
433         }       
434         
435         private $config_file;
436         private $config;
437 };
438
439 ?>