]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/wlanap.cgi
Add bluetooth.cgi only rfcomm/modem support yet
[people/teissler/ipfire-2.x.git] / html / cgi-bin / wlanap.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21 #
22 # WLAN AP cgi based on wlanap.cgi written by Markus Hoffmann & Olaf Westrik
23 #
24
25 use strict;
26
27 # enable only the following on debugging purpose
28 use warnings;
29 use CGI::Carp 'fatalsToBrowser';
30
31 require '/var/ipfire/general-functions.pl';
32 require '/var/ipfire/lang.pl';
33 require '/var/ipfire/header.pl';
34
35 my $debug = 0;
36 my $i = 0;
37 my $errormessage = '';
38 my $status_started = "<td align='center' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td></tr>";
39 my $status_stopped = "<td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td></tr>";
40
41 # get rid of used only once warnings
42 my @onlyonce = ( $Header::colourgreen, $Header::colourred );
43 undef @onlyonce;
44
45 my %selected=();
46 my %checked=();
47 my %color = ();
48 my %mainsettings = ();
49 my %netsettings=();
50 my %wlanapsettings=();
51 my $channel = '';
52 my $txpower = '';
53
54 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
55 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
56 &General::readhash("/var/ipfire/ethernet/settings", \%netsettings);
57
58 $wlanapsettings{'APMODE'} = 'on';
59 $wlanapsettings{'INTERFACE'} = $netsettings{'BLUE_DEV'};
60 $wlanapsettings{'SSID'} = 'IPFire';
61 $wlanapsettings{'HIDESSID'} = 'off';
62 $wlanapsettings{'ENC'} = 'wpa'; # none / wpa
63 $wlanapsettings{'TXPOWER'} = 'auto';
64 # $wlanapsettings{'CC'} = '276'; # CountryCode, 276 = Germany
65 $wlanapsettings{'CHAN'} = '05';
66 $wlanapsettings{'PWD'} = 'IPFire-2.x';
67 $wlanapsettings{'PSK'} = '69eb868ed7b3cc36d767b729048c9c585234723d1eafbe66e5a16957b7c85e9c';
68 $wlanapsettings{'WPA'} = '1';
69
70 $wlanapsettings{'SYSLOGLEVEL'} = '0';
71 $wlanapsettings{'DEBUG'} = '4';
72 $wlanapsettings{'DRIVER'} = 'MADWIFI'; # UNKNOWN / MADWIFI / RT2500 / PRISM54 / ...
73
74 # WLANMODE= (a/b/g)
75 &General::readhash("/var/ipfire/wlanap/settings", \%wlanapsettings);
76
77 my %cgiparams=();
78 $cgiparams{'ACTION'} = '';
79 $cgiparams{'RUNNING'} = 'off';
80 $cgiparams{'APMODE'} = 'on';
81 $cgiparams{'SSID'} = 'IPFire';
82 $cgiparams{'HIDESSID'} = 'off';
83 $cgiparams{'ENC'} = 'wpa'; # none / wep / wpa / wep+wpa
84 $cgiparams{'TXPOWER'} = 'auto';
85 $cgiparams{'CHAN'} = '05';
86
87 $cgiparams{'PWD'} = 'IPFire-2.x';
88 $cgiparams{'PSK'} = '69eb868ed7b3cc36d767b729048c9c585234723d1eafbe66e5a16957b7c85e9c';
89 $cgiparams{'WPA'} = '1';
90
91 $cgiparams{'SYSLOGLEVEL'} = '0';
92 $cgiparams{'DEBUG'} = '4';
93 &Header::getcgihash(\%cgiparams);
94
95
96 &Header::showhttpheaders();
97
98 if ( $cgiparams{'ACTION'} eq "$Lang::tr{'save'}" ){
99 $wlanapsettings{'APMODE'} = $cgiparams{'APMODE'};
100 $wlanapsettings{'SSID'} = $cgiparams{'SSID'};
101 $wlanapsettings{'HIDESSID'} = $cgiparams{'HIDESSID'};
102 $wlanapsettings{'ENC'} = $cgiparams{'ENC'};
103 $wlanapsettings{'CHAN'} = $cgiparams{'CHAN'};
104 $wlanapsettings{'TXPOWER'} = $cgiparams{'TXPOWER'};
105
106 $wlanapsettings{'PWD'} = $cgiparams{'PWD'};
107 $wlanapsettings{'PSK'} = $cgiparams{'PSK'};
108 $wlanapsettings{'WPA'} = $cgiparams{'WPA'};
109 $wlanapsettings{'SYSLOGLEVEL'}= $cgiparams{'SYSLOGLEVEL'};
110 $wlanapsettings{'DEBUG'} = $cgiparams{'DEBUG'};
111
112 # verify WPA Passphrase, must be 8 .. 63 characters
113 if ( (length($wlanapsettings{'PWD'}) < 8) || (length($wlanapsettings{'PWD'}) > 63) ){
114 $errormessage .= "Invalid length in WPA Passphrase. Must be between 8 and 63 characters.<br />";
115 }
116
117 if ( $errormessage eq '' ){
118 &WriteConfig();
119 &WriteConfig_hostapd();
120
121 system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1") if ( $cgiparams{'RUNNING'} eq 'on' );
122 }
123 }elsif ( $cgiparams{'ACTION'} eq 'Start' ){
124 system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1");
125 }elsif ( $cgiparams{'ACTION'} eq 'Stop' ){
126 system("/usr/local/bin/wlanapctrl stop >/dev/null 2>&1");
127 }
128
129 &Header::openpage('WLAN', 1, '', '');
130 &Header::openbigbox('100%', 'left', '', $errormessage);
131 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
132
133 if ( $errormessage ){
134 &Header::openbox('100%', 'center', $Lang::tr{'error messages'});
135 print "<class name='base'>$errormessage\n";
136 print "&nbsp;</class>\n";
137 &Header::closebox();
138 }
139
140
141 # Found this usefull piece of code in BlockOutTraffic AddOn 8-)
142 # fwrules.cgi
143 ###############
144 # DEBUG DEBUG
145 if ( $debug ){
146 &Header::openbox('100%', 'center', 'DEBUG');
147 my $debugCount = 0;
148 foreach my $line (sort keys %cgiparams) {
149 print "$line = '$cgiparams{$line}'<br />\n";
150 $debugCount++;
151 }
152 print "&nbsp;Count: $debugCount\n";
153 &Header::closebox();
154 }
155 # DEBUG DEBUG
156 ###############
157
158 my $test;
159
160 #
161 # Driver and status detection
162 #
163 my $wlan_card_status = 'dummy';
164 my $wlan_ap_status = '';
165 my $wlan_hostapd_status = '';
166 my $blue_message = "";
167
168 if ( ($netsettings{'BLUE_DEV'} eq '') || ($netsettings{'BLUE_DRIVER'} eq '') ){
169 $blue_message = "No BLUE Interface.";
170 }else{
171 my $cmd_out = `/usr/sbin/iwconfig $netsettings{'BLUE_DEV'} 2>/dev/null`;
172
173 if ( $cmd_out eq '' ){
174 $blue_message = "BLUE Interface is not a WLAN card.";
175 }else{
176 $cmd_out = `/sbin/ifconfig | /bin/grep $netsettings{'BLUE_DEV'}`;
177 if ( $cmd_out eq '' ){
178 $wlan_card_status = 'down';
179 }else{
180 $wlan_card_status = 'up';
181 $cmd_out = `/usr/sbin/iwconfig $netsettings{'BLUE_DEV'} | /bin/grep "Mode:Master"`;
182 if ( $cmd_out ne '' ){
183 $wlan_ap_status = 'up';
184 }
185 }
186 }
187 }
188
189 my $disabled_apmode = "disabled='disabled'";
190 $disabled_apmode = '' if ( ($wlanapsettings{'DRIVER'} eq 'MADWIFI') || ($wlanapsettings{'DRIVER'} eq 'HOSTAP') || ($wlanapsettings{'DRIVER'} eq 'ACX100') );
191
192 my $checked_apmode = '';
193 my $checked_hidessid = '';
194 $checked_apmode = "checked='checked'" if ( $wlanapsettings{'APMODE'} eq 'on' );
195 $checked_hidessid = "checked='checked'" if ( $wlanapsettings{'HIDESSID'} eq 'on' );
196
197 $selected{'ENC'}{'none'} = '';
198 $selected{'ENC'}{'wpa'} = '';
199 $selected{'ENC'}{$wlanapsettings{'ENC'}} = "selected='selected'";
200
201 $selected{'CHAN'}{$wlanapsettings{'CHAN'}} = "selected='selected'";
202 $selected{'TXPOWER'}{$wlanapsettings{'TXPOWER'}} = "selected='selected'";
203
204 my @channellist = `iwlist $netsettings{'BLUE_DEV'} channel`;
205 # get available channels
206
207 my @temp;
208 foreach (@channellist){
209 $_ =~ /(.*)Channel (\d+)(.*):/;
210 my $channel = $2;chomp $channel;
211 if ( $channel =~ /\d+/ ){push(@temp,$channel);}
212 }
213 @channellist = @temp;
214
215 my @txpower = `iwlist $netsettings{'BLUE_DEV'} txpower`;
216 # get available channels
217
218 my @temp;
219 foreach (@txpower){
220 $_ =~ /(\s)(\d+)(\s)dBm(\s)(.*)(\W)(\d+)(.*)/;
221 my $tx = $7;chomp $tx;
222 if ( $tx =~ /\d+/ ){push(@temp,$tx."mW");}
223 }
224 @txpower = @temp;
225 push(@txpower,"auto");
226
227 $selected{'WPA'}{'1'} = '';
228 $selected{'WPA'}{'2'} = '';
229 $selected{'WPA'}{$wlanapsettings{'WPA'}} = "selected='selected'";
230
231 $selected{'SYSLOGLEVEL'}{'0'} = '';
232 $selected{'SYSLOGLEVEL'}{'1'} = '';
233 $selected{'SYSLOGLEVEL'}{'2'} = '';
234 $selected{'SYSLOGLEVEL'}{'3'} = '';
235 $selected{'SYSLOGLEVEL'}{'4'} = '';
236 $selected{'SYSLOGLEVEL'}{$wlanapsettings{'SYSLOGLEVEL'}} = "selected='selected'";
237
238 $selected{'DEBUG'}{'0'} = '';
239 $selected{'DEBUG'}{'1'} = '';
240 $selected{'DEBUG'}{'2'} = '';
241 $selected{'DEBUG'}{'3'} = '';
242 $selected{'DEBUG'}{'4'} = '';
243 $selected{'DEBUG'}{$wlanapsettings{'DEBUG'}} = "selected='selected'";
244
245 #
246 # Status box
247 #
248 &Header::openbox('100%', 'center', "WLAN AP");
249 print <<END
250 <table width='95%' cellspacing='0'>
251 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Services</b></td></tr>
252 END
253 ;
254 if ( $wlan_card_status ne '' ){
255 print "<tr><td class='base'>WLAN card ($wlanapsettings{'DRIVER'})</td>";
256 print $wlan_card_status eq 'up' ? $status_started : $status_stopped;
257 print "<tr><td class='base'>Access Point</td>";
258 print $wlan_ap_status eq 'up' ? $status_started : $status_stopped;
259 if ( $wlan_card_status eq 'up' ){
260 print "<tr><td colspan='2' align='center'><input type='submit' name='ACTION' value='Stop' /><input type='hidden' name='RUNNING' value='on' /></td></tr>";
261 }else{
262 print "<tr><td colspan='2' align='center'><input type='submit' name='ACTION' value='Start' /></td></tr>";
263 }
264 }else{
265 print "<tr><td colspan='2' class='base'><b>$blue_message</b></td></tr>";
266 }
267 print "</table>";
268
269 if ( $wlan_card_status eq '' ){
270 print "</form>";
271 &Header::closebigbox();
272 &Header::closepage();
273 exit 0;
274 }
275 print <<END
276 <br />
277 <table width='95%' cellspacing='0'>
278 <tr><td bgcolor='$color{'color20'}' colspan='4' align='left'><b>WLAN Settings</b>
279 END
280 ;
281 #<tr><td width='25%' class='base'>Access Point:&nbsp;</td><td class='base' colspan='3'><input type='checkbox' name='APMODE' $checked_apmode $disabled_apmode /></td></tr>
282 print <<END
283 <tr><td width='25%' class='base'>SSID:&nbsp;</td><td class='base' colspan='3'><input type='text' name='SSID' size='40' value='$wlanapsettings{'SSID'}' /></td></tr>
284 <tr><td width='25%' class='base'>Disable SSID broadcast:&nbsp;</td><td class='base' colspan='3'><input type='checkbox' name='HIDESSID' $checked_hidessid $disabled_apmode /></td></tr>
285 <tr><td width='25%' class='base'>Encryption:&nbsp;</td><td class='base' colspan='3'>
286 <select name='ENC'>
287 <option value='none' $selected{'ENC'}{'none'}>none</option>
288 <option value='wpa' $selected{'ENC'}{'wpa'}>wpa</option>
289 </select>
290 </td></tr>
291 END
292 ;
293 print <<END
294 <!-- <tr><td width='25%' class='base'>Select Country:&nbsp;</td><td class='base' colspan='3'><input type='checkbox' name='CC' $checked_hidessid /></td></tr> -->
295 <tr><td width='25%' class='base'>Channel:&nbsp;</td><td class='base' colspan='3'>
296 <select name='CHAN'>
297 END
298 ;
299 foreach $channel (@channellist){
300 print "<option $selected{'CHAN'}{$channel}>$channel</option>";
301 }
302
303 print <<END
304 </select></td></tr>
305 <tr><td width='25%' class='base'>Tx Power:&nbsp;</td><td class='base' colspan='3'><select name='TXPOWER'>
306 END
307 ;
308 foreach $txpower (@txpower){
309 print "<option $selected{'TXPOWER'}{$txpower}>$txpower</option>&nbsp;dBm";
310 }
311 print <<END
312 </select></td></tr>
313 <tr><td width='25%' class='base'>Passphrase:&nbsp;</td><td class='base' colspan='3'><input type='text' name='PWD' size='63' value='$wlanapsettings{'PWD'}' /></td></tr>
314 <tr><td width='25%' class='base'>WPA Version:&nbsp;</td><td class='base' colspan='3'>
315 <select name='WPA'>
316 <option value='1' $selected{'WPA'}{'1'}>1</option>
317 <option value='2' $selected{'WPA'}{'2'}>2</option>
318 </select>
319 </td></tr>
320 <tr><td width='25%' class='base'>Loglevel (hostapd):&nbsp;</td><td class='base' width='25%'>
321 <select name='SYSLOGLEVEL'>
322 <option value='0' $selected{'SYSLOGLEVEL'}{'0'}>0 (verbose)</option>
323 <option value='1' $selected{'SYSLOGLEVEL'}{'1'}>1 (debugging)</option>
324 <option value='2' $selected{'SYSLOGLEVEL'}{'2'}>2 (informations)</option>
325 <option value='3' $selected{'SYSLOGLEVEL'}{'3'}>3 (notifications)</option>
326 <option value='4' $selected{'SYSLOGLEVEL'}{'4'}>4 (warnings)</option>
327 </select>
328 </td>
329 <td width='25%' class='base'>Debuglevel (hostapd):&nbsp;</td><td class='base' width='25%'>
330 <select name='DEBUG'>
331 <option value='0' $selected{'DEBUG'}{'0'}>0 (verbose)</option>
332 <option value='1' $selected{'DEBUG'}{'1'}>1 (debugging)</option>
333 <option value='2' $selected{'DEBUG'}{'2'}>2 (informations)</option>
334 <option value='3' $selected{'DEBUG'}{'3'}>3 (notifications)</option>
335 <option value='4' $selected{'DEBUG'}{'4'}>4 (warnings)</option>
336 </select>
337 </td></tr>
338 </table>
339 <br />
340 <table width='10%' cellspacing='0'>
341 <tr><td align='center'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
342 <input type='hidden' name='ACTION' value=$Lang::tr{'save'} />
343 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form></td>
344 </tr>
345 </table>
346 END
347 ;
348
349
350 my $status = `cat /proc/net/madwifi/$netsettings{'BLUE_DEV'}/associated_sta`;
351 print <<END
352 <br />
353 <table width='95%' cellspacing='0'>
354 <tr><td bgcolor='$color{'color20'}' align='left'><b>WLAN Status</b></td></tr>
355 <tr><td><pre>$status</pre></td></tr>
356 </table>
357 END
358 ;
359 &Header::closebox();
360 print "</form>";
361 &Header::closebigbox();
362 &Header::closepage();
363
364 sub WriteConfig{
365 &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings);
366 }
367
368 sub WriteConfig_hostapd{
369 $wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'});
370
371 open (CONFIGFILE, ">/var/ipfire/wlanap/hostapd.conf");
372 print CONFIGFILE <<END
373 ######################### basic hostapd configuration ##########################
374
375 interface=$netsettings{'BLUE_DEV'}
376 driver=$wlanapsettings{'DRIVER_HOSTAPD'}
377 logger_syslog=-1
378 logger_syslog_level=$wlanapsettings{'SYSLOGLEVEL'}
379 logger_stdout=-1
380 logger_stdout_level=$wlanapsettings{'DEBUG'}
381 dump_file=/tmp/hostapd.dump
382 ignore_broadcast_ssid=0
383 eapol_key_index_workaround=0
384 eap_server=0
385 own_ip_addr=127.0.0.1
386 auth_algs=3
387 ctrl_interface=/var/run/hostapd
388 ctrl_interface_group=0
389 macaddr_acl=0
390 END
391 ;
392 if ( $wlanapsettings{'HIDESSID'} eq 'on' ){
393 print CONFIGFILE <<END
394 ssid=$wlanapsettings{'SSID'}
395 ignore_broadcast_ssid=2
396 END
397 ;
398
399 }else{
400 print CONFIGFILE <<END
401 ssid=$wlanapsettings{'SSID'}
402 ignore_broadcast_ssid=0
403 END
404 ;
405
406 }
407
408 if ( $wlanapsettings{'ENC'} eq 'wpa'){
409 print CONFIGFILE <<END
410 ######################### wpa hostapd configuration ############################
411
412 wpa=$wlanapsettings{'WPA'}
413 wpa_passphrase=$wlanapsettings{'PWD'}
414 wpa_key_mgmt=WPA-PSK WPA-EAP
415 wpa_pairwise=CCMP TKIP
416 END
417 ;
418 }
419 close CONFIGFILE;
420 }
421
422 sub isrunning
423 {
424 my $cmd = $_[0];
425 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td>";
426 my $pid = '';
427 my $testcmd = '';
428 my $exename;
429
430 $cmd =~ /(^[a-z]+)/;
431 $exename = $1;
432
433 if (open(FILE, "/var/run/${cmd}.pid"))
434 {
435 $pid = <FILE>; chomp $pid;
436 close FILE;
437 if (open(FILE, "/proc/${pid}/status"))
438 {
439 while (<FILE>)
440 {
441 if (/^Name:\W+(.*)/)
442 {
443 $testcmd = $1;
444 }
445 }
446 close FILE;
447 if ($testcmd =~ /$exename/)
448 {
449 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
450 }
451 }
452 }
453 return $status;
454 }