config modifications and such
[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"] = file_get_contents("/sys/class/net/$fname/mtu");
67                                 if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = 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                         }
71                 }
72         }
73         
74         function getConfig()
75         {
76                 return $this->config;
77         }
78         
79         function applyConfig()
80         {
81                 global $AM_DAEMON;
82                 if(!$AM_DAEMON) return true;
83                 
84                 // oh the joy
85                 return true;
86         }
87         
88         function mergeConfig($configone, $configtwo)
89         {
90                 // yep this means apply changes in configtwo to configone
91                 $newconf = $configone;
92                 
93                 // this is gunna be tough its gunna be a meet on meet sandwitch
94                 foreach($configtwo as $key => $var) {
95                         if($key != "status") {
96                                 error_log("apply change $key");
97                                 foreach($var as $key2=>$var2) {
98                                         error_log("which is $key2");
99                                         $splits = explode(" ", $var2);
100                                         
101                                         // find a description
102                                         $description = null;
103                                         $hasdescription = false;
104                                         for($i=0; $i<count($splits); $i++) {
105                                                 if($hasdescription !== false) {
106                                                         if($description != null) {
107                                                                 $description .= " ";
108                                                         }
109                                                         $description .= $splits[$i];
110                                                 } 
111                                                 if($splits[$i] == "#") {
112                                                         $hasdescription = $i;
113                                                 }
114                                         }
115                                         
116                                         
117                                         switch($key2) {
118                                                 case "add":
119                                                         error_log("would add $var2");
120                                                         $thing = $splits[0];
121                                                         switch($thing) {
122                                                                 case "zone":
123                                                                         $newconf["zone"][$splits[1]]["name"] = 1;
124                                                                         if($hasdescription) {
125                                                                                 $newconf["zone"][$splits[1]]["description"] = $description;
126                                                                         }
127                                                                         break;
128                                                                 
129                                                         }
130                                                         break;
131                                                 case "modify":
132                                                         error_log("would modify $var2");
133                                                         $thing = $splits[0];
134                                                         switch($thing) {
135                                                                 case "zone":
136                                                                         $zonename = $splits[1];
137                                                                         if($hasdescription) {
138                                                                                 $newconf["zone"][$splits[1]]["description"] = $description;
139                                                                         }
140                                                                         if($splits[2] == "rename") {
141                                                                                 $newname = $splits[3];
142                                                                                 $tree = $newconf["zone"][$splits[1]];
143                                                                                 unset($newconf["zone"][$splits[1]]);
144                                                                                 $newconf["zone"][$newname] = $tree;
145                                                                         }
146                                                                         break;
147                                                                 
148                                                         }
149                                                         break;
150                                                         
151                                                         
152                                                 case "delete":
153                                                         error_log("would delete $var2");
154                                                         $thing = $splits[0];
155                                                         switch($thing) {
156                                                                 case "zone":
157                                                                         $deletezone = $splits[1];
158                                                                         unset($newconf["zone"][$deletezone]);
159                                                                         break;
160                                                                 
161                                                         }
162                                                         break;
163                                         }
164                                 } 
165                         }
166                 }
167                 return $newconf;
168         }
169         
170         function loadConfigFile($file=null)
171         {
172                 
173                 if($file == null) {
174                         if($this->config["status"] == "nodir") {
175                                 return "nodir";
176                         } else $file = $this->config_file;
177                 }
178                 
179                 if(!file_exists($file)) return "noconf";
180                 
181                 $fp = fopen($file, "r");
182                 
183                 $i = 1;
184                 while($line = fgets($fp)) {
185                         $line = trim($line);
186                         echo "read line $line\n";
187                         if($line != "") $this->parseLine($line, $i++);
188                 }
189         }
190         
191         function parseLine($line, $lineno)
192         {
193                 $expl = preg_split("/ +/", $line);
194                 
195                 echo "process command ".$expl[0]."\n";
196                 
197                 // find a description
198                 $description = null;
199                 $hasdescription = false;
200                 for($i=0; $i<count($expl); $i++) {
201                         if($hasdescription !== false) {
202                                 if($description != null) {
203                                         $description .= " ";
204                                 }
205                                 $description .= $expl[$i];
206                         } 
207                         if($expl[$i] == "#") {
208                                 $hasdescription = $i;
209                         }
210                 }
211                 
212                 // now rebuild the array if there was one
213                 if($hasdescription) {
214                         for($i=0; $i<$hasdescription; $i++) {
215                                 $expl_r[$i] = $expl[$i];
216                         }
217                         $expl = $expl_r;
218                 }
219                 
220                 switch($expl[0]) {
221                         case "hostname":
222                                 // set the hostname to $1
223                                 $this->config["hostname"] = $expl[1];
224                                 break;
225                                 
226                         case "domainname":
227                                 // set the hostname to $1
228                                 $this->config["domainname"] = $expl[1];
229                                 break;
230                                 
231                         case "zone":
232                                 if($hasdescription) {
233                                         $this->config["zone"][$expl[2]]["description"] = $description;
234                                 }
235                                 $this->config["zone"][$expl[2]]["name"] = true;
236                                 break;
237                                 
238                         case "interface":
239                                 switch($expl[1]) {
240                                         case "dev":
241                                                 $int = $expl[2];
242
243                                                 if($hasdescription) {
244                                                         $this->config["interface"][$int]["description"] = $description;
245                                                 }
246         
247                                                 switch($expl[3]) {
248                                                         case "address4":
249                                                                 $this->config["interface"]["$int"]["address4"] = $expl[4];
250                                                                 break;
251                                                         case "address6":
252                                                                 $this->config["interface"]["$int"]["address6"] = $expl[4];
253                                                                 break;
254                                                         case "name":
255                                                                 $this->config["interface"]["$int"]["name"] = $expl[4];
256                                                                 break;
257                                                         case "status":
258                                                                 $this->config["interface"]["$int"]["status"] = $expl[4];
259                                                                 break;
260                                                         case "mtu":
261                                                                 $this->config["interface"]["$int"]["mtu"] = $expl[4];
262                                                                 break;
263                                                         case "zone":
264                                                                 $this->config["interface"]["$int"]["zone"] = $expl[4];
265                                                                 break;
266                                                         case "speed":
267                                                                 $this->config["interface"]["$int"]["speed"] = $expl[4];
268                                                                 break;
269                                                         case "duplex":
270                                                                 $this->config["interface"]["$int"]["duplex"] = $expl[4];
271                                                                 break;
272                                                 }
273                                                 break;
274                                                 
275                                         case "vlan":
276                                                 $vlanid = $expl[2];
277                                                 $name = $expl[4];
278                                                 $from = $expl[6];
279                                                 $this->config["vlan"][$name]["parent"] = $from;
280                                                 $this->config["vlan"][$name]["id"] = $vlanid;
281                                                 if($hasdescription) {
282                                                         $this->config["vlan"][$name]["description"] = $description;
283                                                 }
284                                                 break;
285                                                 
286                                         case "lag":
287                                                 $name = $expl[3];
288                                                 for($i=5; $i<count($expl); $i++) {
289                                                         $this->config["lag"][$name][$i-5] = $expl[$i];
290                                                 }
291                                                 if($hasdescription) {
292                                                         $this->config["lag"][$name]["description"] = $description;
293                                                 }
294                                                 break;
295
296                                         case "bridge":
297                                                 $name = $expl[3];
298                                                 for($i=5; $i<count($expl); $i++) {
299                                                         $this->config["bridge"][$name][$i-5] = $expl[$i];
300                                                 }
301                                                 if($hasdescription) {
302                                                         $this->config["bridge"][$name]["description"] = $description;
303                                                 }
304                                                 break;
305                                                 
306                                 }
307                                 break;
308                                 
309                                 
310                         case "login":
311                                 $this->config["login"][$expl[1]] = $expl[3];
312                                 if($hasdescription) {
313                                         $this->config["login"][$expl[1]]["description"] = $description;
314                                 }
315                                 break;
316                         
317                         case "route4":
318                                 $route = $expl[1];
319                                 $via = $expl[2];
320                                 $dest = $expl[3];
321                                 if($via == "to") {
322                                         $this->config["route4"][$route]["address"] = $dest;
323                                 } else {
324                                         $this->config["route4"][$route]["device"] = $dest;
325                                 }
326                                 if(isset($expl[4])) {
327                                         if($expl[4] == "dev") {
328                                                 if(isset($expl[5])) {
329                                                         $this->config["route4"][$route]["device"] = $expl[5];
330                                                 }
331                                         }
332                                 }
333                                 if($hasdescription) {
334                                         $this->config["route4"][$route]["description"] = $description;
335                                 }
336                                 break;
337                                 
338                                 
339                         case "route6":
340                                 $route = $expl[1];
341                                 $via = $expl[2];
342                                 $dest = $expl[3];
343                                 if($via == "to") {
344                                         $this->config["route6"][$route]["address"] = $dest;
345                                 } else {
346                                         $this->config["route6"][$route]["device"] = $dest;
347                                 }
348                                 if(isset($expl[4])) {
349                                         if($expl[4] == "dev") {
350                                                 if(isset($expl[5])) {
351                                                         $this->config["route6"][$route]["device"] = $expl[5];
352                                                 }
353                                         }
354                                 }
355                                 if($hasdescription) {
356                                         $this->config["route6"][$route]["description"] = $description;
357                                 }
358                                 
359                                 // here we should check "$route"
360                                 break;
361                                 
362                                 
363                         case "dns":
364                                 if(isset($this->config["dns"]["nservers"])) {
365                                         $dns_servers = $this->config["dns"]["nservers"];
366                                 } else {
367                                         $dns_servers = 0;
368                                 }
369                                 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
370                                 if($hasdescription) {
371                                         $this->config["dns"]["server"][$dns_servers]["description"] = $description;
372                                 }
373                                 $this->config["dns"]["nservers"] = $dns_servers+1;
374                                 break;
375                                 
376                                 
377                         case "ntp":
378                                 if(isset($this->config["ntp"]["nservers"])) {
379                                         $ntp_servers = $this->config["ntp"]["nservers"];
380                                 } else {
381                                         $ntp_servers = 0;
382                                 }
383                                 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
384                                 if($hasdescription) {
385                                         $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
386                                 }
387                                 $this->config["ntp"]["nservers"] = $ntp_servers+1;
388                                 break;
389                                 
390                                 
391                                 
392                         default:
393                                 echo "Errr, unknown config directive on line $lineno, $line\n";
394                 }
395                 
396
397         }
398         
399         function saveConfigFile($file)
400         {
401                 global $AM_DAEMON;
402                 if(!$AM_DAEMON) return true;
403                 
404         }       
405         
406         private $config_file;
407         private $config;
408 };
409
410 ?>