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