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