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