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