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