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