3c7a54fb5a91cd84042b613254ab0123d6ab7058
[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/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = file_get_contents("/sys/class/net/$fname/address");
67                                 if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
68                                 if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
69                         }
70                 }
71         }
72         
73         function getConfig()
74         {
75                 return $this->config;
76         }
77         
78         function applyConfig()
79         {
80                 global $AM_DAEMON;
81                 if(!$AM_DAEMON) return true;
82                 
83                 // oh the joy
84                 return true;
85         }
86         
87         function mergeConfig($configone, $configtwo)
88         {
89                 // yep
90         }
91         
92         function loadConfigFile($file=null)
93         {
94                 
95                 if($file == null) {
96                         if($this->config["status"] == "nodir") {
97                                 return "nodir";
98                         } else $file = $this->config_file;
99                 }
100                 
101                 if(!file_exists($file)) return "noconf";
102                 
103                 $fp = fopen($file, "r");
104                 
105                 $i = 1;
106                 while($line = fgets($fp)) {
107                         $line = trim($line);
108                         echo "read line $line\n";
109                         if($line != "") $this->parseLine($line, $i++);
110                 }
111         }
112         
113         function parseLine($line, $lineno)
114         {
115                 $expl = preg_split("/ +/", $line);
116                 
117                 echo "process command ".$expl[0]."\n";
118                 
119                 // find a description
120                 $description = null;
121                 $hasdescription = false;
122                 for($i=0; $i<count($expl); $i++) {
123                         if($hasdescription !== false) {
124                                 if($description != null) {
125                                         $description .= " ";
126                                 }
127                                 $description .= $expl[$i];
128                         } 
129                         if($expl[$i] == "#") {
130                                 $hasdescription = $i;
131                         }
132                 }
133                 
134                 // now rebuild the array if there was one
135                 if($hasdescription) {
136                         for($i=0; $i<$hasdescription; $i++) {
137                                 $expl_r[$i] = $expl[$i];
138                         }
139                         $expl = $expl_r;
140                 }
141                 
142                 switch($expl[0]) {
143                         case "hostname":
144                                 // set the hostname to $1
145                                 $this->config["hostname"] = $expl[1];
146                                 break;
147                                 
148                         case "domainname":
149                                 // set the hostname to $1
150                                 $this->config["domainname"] = $expl[1];
151                                 break;
152                                 
153                         case "zone":
154                                 if($hasdescription) {
155                                         $this->config["zone"][$expl[2]]["description"] = $description;
156                                 }
157                                 $this->config["zone"][$expl[2]]["name"] = true;
158                                 break;
159                                 
160                         case "interface":
161                                 switch($expl[1]) {
162                                         case "dev":
163                                                 $int = $expl[2];
164
165                                                 if($hasdescription) {
166                                                         $this->config["interface"][$int]["description"] = $description;
167                                                 }
168         
169                                                 switch($expl[3]) {
170                                                         case "address4":
171                                                                 $this->config["interface"]["$int"]["address4"] = $expl[4];
172                                                                 break;
173                                                         case "address6":
174                                                                 $this->config["interface"]["$int"]["address6"] = $expl[4];
175                                                                 break;
176                                                         case "name":
177                                                                 $this->config["interface"]["$int"]["name"] = $expl[4];
178                                                                 break;
179                                                         case "status":
180                                                                 $this->config["interface"]["$int"]["status"] = $expl[4];
181                                                                 break;
182                                                         case "mtu":
183                                                                 $this->config["interface"]["$int"]["mtu"] = $expl[4];
184                                                                 break;
185                                                         case "zone":
186                                                                 $this->config["interface"]["$int"]["zone"] = $expl[4];
187                                                                 break;
188                                                         case "speed":
189                                                                 $this->config["interface"]["$int"]["speed"] = $expl[4];
190                                                                 break;
191                                                         case "duplex":
192                                                                 $this->config["interface"]["$int"]["duplex"] = $expl[4];
193                                                                 break;
194                                                 }
195                                                 break;
196                                                 
197                                         case "vlan":
198                                                 $vlanid = $expl[2];
199                                                 $name = $expl[4];
200                                                 $from = $expl[6];
201                                                 $this->config["vlan"][$name]["parent"] = $from;
202                                                 $this->config["vlan"][$name]["id"] = $vlanid;
203                                                 if($hasdescription) {
204                                                         $this->config["vlan"][$name]["description"] = $description;
205                                                 }
206                                                 break;
207                                                 
208                                         case "lag":
209                                                 $name = $expl[3];
210                                                 for($i=5; $i<count($expl); $i++) {
211                                                         $this->config["lag"][$name][$i-5] = $expl[$i];
212                                                 }
213                                                 if($hasdescription) {
214                                                         $this->config["lag"][$name]["description"] = $description;
215                                                 }
216                                                 break;
217
218                                         case "bridge":
219                                                 $name = $expl[3];
220                                                 for($i=5; $i<count($expl); $i++) {
221                                                         $this->config["bridge"][$name][$i-5] = $expl[$i];
222                                                 }
223                                                 if($hasdescription) {
224                                                         $this->config["bridge"][$name]["description"] = $description;
225                                                 }
226                                                 break;
227                                                 
228                                 }
229                                 break;
230                                 
231                                 
232                         case "login":
233                                 $this->config["login"][$expl[1]] = $expl[3];
234                                 if($hasdescription) {
235                                         $this->config["login"][$expl[1]]["description"] = $description;
236                                 }
237                                 break;
238                         
239                         case "route4":
240                                 $route = $expl[1];
241                                 $via = $expl[2];
242                                 $dest = $expl[3];
243                                 if($via == "to") {
244                                         $this->config["route4"][$route]["address"] = $dest;
245                                 } else {
246                                         $this->config["route4"][$route]["device"] = $dest;
247                                 }
248                                 if(isset($expl[4])) {
249                                         if($expl[4] == "dev") {
250                                                 if(isset($expl[5])) {
251                                                         $this->config["route4"][$route]["device"] = $expl[5];
252                                                 }
253                                         }
254                                 }
255                                 if($hasdescription) {
256                                         $this->config["route4"][$route]["description"] = $description;
257                                 }
258                                 break;
259                                 
260                                 
261                         case "route6":
262                                 $route = $expl[1];
263                                 $via = $expl[2];
264                                 $dest = $expl[3];
265                                 if($via == "to") {
266                                         $this->config["route6"][$route]["address"] = $dest;
267                                 } else {
268                                         $this->config["route6"][$route]["device"] = $dest;
269                                 }
270                                 if(isset($expl[4])) {
271                                         if($expl[4] == "dev") {
272                                                 if(isset($expl[5])) {
273                                                         $this->config["route6"][$route]["device"] = $expl[5];
274                                                 }
275                                         }
276                                 }
277                                 if($hasdescription) {
278                                         $this->config["route6"][$route]["description"] = $description;
279                                 }
280                                 
281                                 // here we should check "$route"
282                                 break;
283                                 
284                                 
285                         case "dns":
286                                 if(isset($this->config["dns"]["nservers"])) {
287                                         $dns_servers = $this->config["dns"]["nservers"];
288                                 } else {
289                                         $dns_servers = 0;
290                                 }
291                                 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
292                                 if($hasdescription) {
293                                         $this->config["dns"]["server"][$dns_servers]["description"] = $description;
294                                 }
295                                 $this->config["dns"]["nservers"] = $dns_servers+1;
296                                 break;
297                                 
298                                 
299                         case "ntp":
300                                 if(isset($this->config["ntp"]["nservers"])) {
301                                         $ntp_servers = $this->config["ntp"]["nservers"];
302                                 } else {
303                                         $ntp_servers = 0;
304                                 }
305                                 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
306                                 if($hasdescription) {
307                                         $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
308                                 }
309                                 $this->config["ntp"]["nservers"] = $ntp_servers+1;
310                                 break;
311                                 
312                                 
313                                 
314                         default:
315                                 echo "Errr, unknown config directive on line $lineno, $line\n";
316                 }
317                 
318
319         }
320         
321         function saveConfigFile($file)
322         {
323                 global $AM_DAEMON;
324                 if(!$AM_DAEMON) return true;
325                 
326         }       
327         
328         private $config_file;
329         private $config;
330 };
331
332 ?>