9ca0ff9d8e1ecc5762ed51591071496017c4c613
[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()
8         {
9                 global $AM_DAEMON;
10                 
11                 if($AM_DAEMON) {
12                         $this->config_file = null;
13                         if(file_exists("../var/fw.conf")) {
14                                 $this->config_file = realpath("../var/fw.conf");
15                                 $this->config["status"] = "conf";
16                                 
17                         }
18                         if(file_exists("/var/lib/fwd/fw.conf")) {
19                                 $this->config_file = "/var/lib/fwd/fw.conf";
20                                 $this->config["status"] = "conf";
21                         }
22                         
23                         
24                         if($this->config_file == null) {
25                                 $this->config["status"] = "noconf";
26                                 if(is_dir("/var/lib/fwd/")) {
27                                         echo "no config file found. Will use ../var/fw.conf for now\n";
28                                         $this->config_file = "/var/lib/fwd/fw.conf";
29                                 } else if(is_dir("../var/")) {
30                                         echo "no config file found. Will use ../var/fw.conf for now\n";
31                                         $this->config_file = "../var/";
32                                 } else {
33                                         echo "No directory where i can create a config, bailing\n";
34                                         $this->config["status"] = "unconf";
35                                 }
36                         }
37                         
38                         
39                         
40                 }
41         }
42         
43         function bootHardware()
44         {
45                 $this->findHardware();
46
47                 $mycomm = new Comms();
48                 $mycomm->putConfig($this->config, 1);
49                 
50         }
51         
52         function loadConfig()
53         {
54                 global $AM_DAEMON;
55                 
56                 if($AM_DAEMON) {
57                         if($this->config["status"] == "conf") { 
58                                 $this->loadConfigFile($this->config_file);
59                                 $this->applyConfig();
60                                 $this->findHardware();
61                                 
62                                 $mycomm = new Comms();
63                                 $mycomm->putConfig($this->config);
64                                 $this->boot_config = $mycomm->getConfig(1);
65                         }
66                 } else {
67                         $mycomm = new Comms();
68                         $this->config = $mycomm->getConfig(0);
69                         $this->boot_config = $mycomm->getConfig(1);
70                         
71                 }
72         }
73         
74         function findHardware()
75         {
76                 
77                 
78                 // first, network interfaces
79                 $dh = opendir("/sys/class/net/");
80                 while(($fname = readdir($dh)) !== false) {
81                         if($fname != "." && $fname != ".." && $fname != "lo" && is_dir("/sys/class/net/$fname/")) {
82                                 $this->config["hardware"]["netdev"][$fname]["name"] = $fname;
83                                 // now read drive name if you can
84                                 if(file_exists("/sys/class/net/$fname/device/uevent")) $fp = fopen("/sys/class/net/$fname/device/uevent", "r");
85                                 else $fp = false;
86                                 if($fp) {
87                                         while(!feof($fp)) {
88                                                 $line = trim(fgets($fp));
89                                                 $lpl = explode("=", $line);
90                                                 if($lpl[0] == "DRIVER") {
91                                                         $this->config["hardware"]["netdev"][$fname]["driver"] = $lpl[1];
92                                                 }
93                                         }
94                                         fclose($fp);
95                                 }
96                                 if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
97                                 if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
98                         }
99                 }
100         }
101         
102         function getConfig()
103         {
104                 return $this->config;
105         }
106         
107         function getBootConfig()
108         {
109                 return $this->boot_config;
110         }
111         
112         function applyConfig()
113         {
114                 global $AM_DAEMON;
115                 if(!$AM_DAEMON) return true;
116                 
117                 // oh the joy
118                 return true;
119         }
120         
121         function loadConfigFile($file)
122         {
123                 global $AM_DAEMON;
124                 if(!$AM_DAEMON) return true;
125                 
126                 $fp = fopen($file, "r");
127                 
128                 $i = 1;
129                 while($line = fgets($fp)) {
130                         $line = trim($line);
131                         echo "read line $line\n";
132                         if($line != "") $this->parseLine($line, $i++);
133                 }
134         }
135         
136         function parseLine($line, $lineno)
137         {
138                 $expl = preg_split("/ +/", $line);
139                 
140                 echo "process command ".$expl[0]."\n";
141                 
142                 // find a description
143                 $description = null;
144                 $hasdescription = false;
145                 for($i=0; $i<count($expl); $i++) {
146                         if($hasdescription !== false) {
147                                 if($description != null) {
148                                         $description .= " ";
149                                 }
150                                 $description .= $expl[$i];
151                         } 
152                         if($expl[$i] == "#") {
153                                 $hasdescription = $i;
154                         }
155                 }
156                 
157                 // now rebuild the array if there was one
158                 if($hasdescription) {
159                         for($i=0; $i<$hasdescription; $i++) {
160                                 $expl_r[$i] = $expl[$i];
161                         }
162                         $expl = $expl_r;
163                 }
164                 
165                 switch($expl[0]) {
166                         case "hostname":
167                                 // set the hostname to $1
168                                 $this->config["hostname"] = $expl[1];
169                                 break;
170                                 
171                         case "domainname":
172                                 // set the hostname to $1
173                                 $this->config["domainname"] = $expl[1];
174                                 break;
175                                 
176                         case "zone":
177                                 if($hasdescription) {
178                                         $this->config["zone"][$expl[2]]["description"] = $description;
179                                 }
180                                 $this->config["zone"][$expl[2]]["name"] = true;
181                                 break;
182                                 
183                         case "interface":
184                                 switch($expl[1]) {
185                                         case "dev":
186                                                 $int = $expl[2];
187
188                                                 if($hasdescription) {
189                                                         $this->config["interface"][$int]["description"] = $description;
190                                                 }
191         
192                                                 switch($expl[3]) {
193                                                         case "address4":
194                                                                 $this->config["interface"]["$int"]["address4"] = $expl[4];
195                                                                 break;
196                                                         case "address6":
197                                                                 $this->config["interface"]["$int"]["address6"] = $expl[4];
198                                                                 break;
199                                                         case "name":
200                                                                 $this->config["interface"]["$int"]["name"] = $expl[4];
201                                                                 break;
202                                                         case "status":
203                                                                 $this->config["interface"]["$int"]["status"] = $expl[4];
204                                                                 break;
205                                                         case "mtu":
206                                                                 $this->config["interface"]["$int"]["mtu"] = $expl[4];
207                                                                 break;
208                                                         case "zone":
209                                                                 $this->config["interface"]["$int"]["zone"] = $expl[4];
210                                                                 break;
211                                                         case "speed":
212                                                                 $this->config["interface"]["$int"]["speed"] = $expl[4];
213                                                                 break;
214                                                         case "duplex":
215                                                                 $this->config["interface"]["$int"]["duplex"] = $expl[4];
216                                                                 break;
217                                                 }
218                                                 break;
219                                                 
220                                         case "vlan":
221                                                 $vlanid = $expl[2];
222                                                 $name = $expl[4];
223                                                 $from = $expl[6];
224                                                 $this->config["vlan"][$name]["parent"] = $from;
225                                                 $this->config["vlan"][$name]["id"] = $vlanid;
226                                                 if($hasdescription) {
227                                                         $this->config["vlan"][$name]["description"] = $description;
228                                                 }
229                                                 break;
230                                                 
231                                         case "lag":
232                                                 $name = $expl[3];
233                                                 for($i=5; $i<count($expl); $i++) {
234                                                         $this->config["lag"][$name][$i-5] = $expl[$i];
235                                                 }
236                                                 if($hasdescription) {
237                                                         $this->config["lag"][$name]["description"] = $description;
238                                                 }
239                                                 break;
240
241                                         case "bridge":
242                                                 $name = $expl[3];
243                                                 for($i=5; $i<count($expl); $i++) {
244                                                         $this->config["bridge"][$name][$i-5] = $expl[$i];
245                                                 }
246                                                 if($hasdescription) {
247                                                         $this->config["bridge"][$name]["description"] = $description;
248                                                 }
249                                                 break;
250                                                 
251                                 }
252                                 break;
253                                 
254                                 
255                         case "login":
256                                 $this->config["login"][$expl[1]] = $expl[3];
257                                 if($hasdescription) {
258                                         $this->config["login"][$expl[1]]["description"] = $description;
259                                 }
260                                 break;
261                         
262                         case "route4":
263                                 $route = $expl[1];
264                                 $via = $expl[2];
265                                 $dest = $expl[3];
266                                 if($via == "to") {
267                                         $this->config["route4"][$route]["address"] = $dest;
268                                 } else {
269                                         $this->config["route4"][$route]["device"] = $dest;
270                                 }
271                                 if(isset($expl[4])) {
272                                         if($expl[4] == "dev") {
273                                                 if(isset($expl[5])) {
274                                                         $this->config["route4"][$route]["device"] = $expl[5];
275                                                 }
276                                         }
277                                 }
278                                 if($hasdescription) {
279                                         $this->config["route4"][$route]["description"] = $description;
280                                 }
281                                 break;
282                                 
283                                 
284                         case "route6":
285                                 $route = $expl[1];
286                                 $via = $expl[2];
287                                 $dest = $expl[3];
288                                 if($via == "to") {
289                                         $this->config["route6"][$route]["address"] = $dest;
290                                 } else {
291                                         $this->config["route6"][$route]["device"] = $dest;
292                                 }
293                                 if(isset($expl[4])) {
294                                         if($expl[4] == "dev") {
295                                                 if(isset($expl[5])) {
296                                                         $this->config["route6"][$route]["device"] = $expl[5];
297                                                 }
298                                         }
299                                 }
300                                 if($hasdescription) {
301                                         $this->config["route6"][$route]["description"] = $description;
302                                 }
303                                 
304                                 // here we should check "$route"
305                                 break;
306                                 
307                                 
308                         case "dns":
309                                 if(isset($this->config["dns"]["nservers"])) {
310                                         $dns_servers = $this->config["dns"]["nservers"];
311                                 } else {
312                                         $dns_servers = 0;
313                                 }
314                                 if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
315                                 if($hasdescription) {
316                                         $this->config["dns"]["server"][$dns_servers]["description"] = $description;
317                                 }
318                                 $this->config["dns"]["nservers"] = $dns_servers+1;
319                                 break;
320                                 
321                                 
322                         case "ntp":
323                                 if(isset($this->config["ntp"]["nservers"])) {
324                                         $ntp_servers = $this->config["ntp"]["nservers"];
325                                 } else {
326                                         $ntp_servers = 0;
327                                 }
328                                 if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
329                                 if($hasdescription) {
330                                         $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
331                                 }
332                                 $this->config["ntp"]["nservers"] = $ntp_servers+1;
333                                 break;
334                                 
335                                 
336                                 
337                         default:
338                                 echo "Errr, unknown config directive on line $lineno, $line\n";
339                 }
340                 
341
342         }
343         
344         function saveConfigFile($file)
345         {
346                 global $AM_DAEMON;
347                 if(!$AM_DAEMON) return true;
348                 
349         }       
350         
351         private $config_file;
352         private $config;
353         private $boot_config;
354 };
355
356 ?>