]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/wlanap.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2005-2012 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
22 # WLAN AP cgi based on wlanap.cgi written by Markus Hoffmann & Olaf Westrik
27 # enable only the following on debugging purpose
29 #use CGI::Carp 'fatalsToBrowser';
31 require '/var/ipfire/general-functions.pl';
32 require '/var/ipfire/lang.pl';
33 require '/var/ipfire/header.pl';
37 my $errormessage = '';
38 my $status_started = "<td align='center' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td></tr>";
39 my $status_stopped = "<td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td></tr>";
41 # get rid of used only once warnings
42 my @onlyonce = ( $Header::colourgreen
, $Header::colourred
);
48 my %mainsettings = ();
50 my %wlanapsettings=();
55 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
56 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
57 &General
::readhash
("/var/ipfire/ethernet/settings", \
%netsettings);
59 $wlanapsettings{'APMODE'} = 'on';
60 $wlanapsettings{'ACTION'} = '';
61 $wlanapsettings{'MACMODE'} = '0';
62 $wlanapsettings{'INTERFACE'} = '';
63 $wlanapsettings{'SSID'} = 'IPFire';
64 $wlanapsettings{'HIDESSID'} = 'off';
65 $wlanapsettings{'ENC'} = 'wpa2'; # none / wpa1 /wpa2
66 $wlanapsettings{'TXPOWER'} = 'auto';
67 $wlanapsettings{'CHANNEL'} = '05';
68 $wlanapsettings{'COUNTRY'} = '00';
69 $wlanapsettings{'HW_MODE'} = 'g';
70 $wlanapsettings{'PWD'} = 'IPFire-2.x';
71 $wlanapsettings{'SYSLOGLEVEL'} = '0';
72 $wlanapsettings{'DEBUG'} = '4';
73 $wlanapsettings{'DRIVER'} = 'MADWIFI';
74 $wlanapsettings{'HTCAPS'} = '';
76 &General
::readhash
("/var/ipfire/wlanap/settings", \
%wlanapsettings);
77 &Header
::getcgihash
(\
%wlanapsettings);
79 my @macs = $wlanapsettings{'MACS'};
81 delete $wlanapsettings{'__CGI__'};
82 delete $wlanapsettings{'x'};
83 delete $wlanapsettings{'y'};
84 delete $wlanapsettings{'MACS'};
85 delete $wlanapsettings{'ACCEPT_MACS'};
86 delete $wlanapsettings{'DENY_MACS'};
88 &Header
::showhttpheaders
();
92 my $errormessage = '';
100 open(FILE
, '/usr/local/bin/addonctrl hostapd status | ');
103 $string = join("", @hostapd);
104 $string =~ s/[a-z_]//gi;
105 $string =~ s/\[[0-1]\;[0-9]+//gi;
106 $string =~ s/[\(\)\.]//gi;
109 @pid = split(/\s/,$string);
110 if (open(FILE
, "/proc/$pid[0]/statm")){
112 @memory = split(/ /,$temp);
121 if ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'wlanap del interface'}" ){
122 delete $wlanapsettings{'INTERFACE'};
123 &General
::writehash
("/var/ipfire/wlanap/settings", \
%wlanapsettings);
126 if ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'save'}" ){
127 # verify WPA Passphrase, must be 8 .. 63 characters - only wiht enabled enc
128 if (($wlanapsettings{'ENC'} eq "wpa1") || ($wlanapsettings{'ENC'} eq "wpa2") || ($wlanapsettings{'ENC'} eq "wpa1+2")){
129 if ( (length($wlanapsettings{'PWD'}) < 8) || (length($wlanapsettings{'PWD'}) > 63)){
130 $errormessage .= "$Lang::tr{'wlanap invalid wpa'}<br />";
134 if ( $errormessage eq '' ){
135 &General
::writehash
("/var/ipfire/wlanap/settings", \
%wlanapsettings);
136 &WriteConfig_hostapd
();
138 system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1");
141 }elsif ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'wlanap interface'}" ){
142 &General
::writehash
("/var/ipfire/wlanap/settings", \
%wlanapsettings);
143 }elsif ( ($wlanapsettings{'ACTION'} eq "$Lang::tr{'start'}") && ($memory == 0) ){
144 system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1");
146 }elsif ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'stop'}" ){
147 system("/usr/local/bin/wlanapctrl stop >/dev/null 2>&1");
149 }elsif ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'restart'}" ){
150 system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1");
154 &Header
::openpage
('Wireless LAN', 1, '', '');
155 &Header
::openbigbox
('100%', 'left', '', $errormessage);
157 if ( $errormessage ){
158 &Header
::openbox
('100%', 'center', $Lang::tr
{'error messages'});
159 print "<class name='base'>$errormessage\n";
160 print " </class>\n";
165 # Found this usefull piece of code in BlockOutTraffic AddOn 8-)
170 &Header
::openbox
('100%', 'center', 'DEBUG');
172 foreach my $line (sort keys %wlanapsettings) {
173 print "$line = '$wlanapsettings{$line}'<br />\n";
176 print " Count: $debugCount\n";
183 # Driver and status detection
185 my $wlan_card_status = 'dummy';
186 my $wlan_ap_status = '';
189 $selected{'INTERFACE'}{'green0'} = '';
190 $selected{'INTERFACE'}{'blue0'} = '';
191 $selected{'ENC'}{$wlanapsettings{'INTERFACE'}} = "selected='selected'";
193 if ( ($wlanapsettings{'INTERFACE'} eq '') ){
194 $message = $Lang::tr
{'wlanap select interface'};
195 &Header
::openbox
('100%', 'center', "WLAN AP");
198 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
199 <select name='INTERFACE'>
202 if ( $netsettings{'BLUE_DEV'} ne ''){
203 print "<option value='blue0' $selected{'INTERFACE'}{'blue0'}>blue0</option>";
206 <option value='green0' $selected{'INTERFACE'}{'green0'}>green0</option>
210 <input type='submit' name='ACTION' value='$Lang::tr{'wlanap interface'}' /></form>
214 &Header
::closebigbox
();
215 &Header
::closepage
();
218 my $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} 2>/dev/null`;
220 if ( $cmd_out eq '' ){
221 $message = "$Lang::tr{'wlanap no interface'}";
222 $wlan_card_status = '';
224 $cmd_out = `/sbin/ifconfig | /bin/grep $wlanapsettings{'INTERFACE'}`;
225 if ( $cmd_out eq '' ){
226 $wlan_card_status = 'down';
228 $wlan_card_status = 'up';
229 $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} | /bin/grep "Mode:Master"`;
230 if ( $cmd_out ne '' ){
231 $wlan_ap_status = 'up';
237 # Change old "n" to "gn"
238 if ( $wlanapsettings{'HW_MODE'} eq 'n' ) {
239 $wlanapsettings{'HW_MODE'}='gn';
242 $checked{'HIDESSID'}{'off'} = '';
243 $checked{'HIDESSID'}{'on'} = '';
244 $checked{'HIDESSID'}{$wlanapsettings{'HIDESSID'}} = "checked='checked'";
246 $selected{'ENC'}{$wlanapsettings{'ENC'}} = "selected='selected'";
247 $selected{'CHANNEL'}{$wlanapsettings{'CHANNEL'}} = "selected='selected'";
248 $selected{'COUNTRY'}{$wlanapsettings{'COUNTRY'}} = "selected='selected'";
249 $selected{'TXPOWER'}{$wlanapsettings{'TXPOWER'}} = "selected='selected'";
250 $selected{'HW_MODE'}{$wlanapsettings{'HW_MODE'}} = "selected='selected'";
251 $selected{'MACMODE'}{$wlanapsettings{'MACMODE'}} = "selected='selected'";
253 my $monwlaninterface = $wlanapsettings{'INTERFACE'};
254 if ( $wlanapsettings{'DRIVER'} eq 'NL80211' ){
255 $monwlaninterface = 'mon.'.$wlanapsettings{'INTERFACE'};
258 my @channellist_cmd = `iwlist $monwlaninterface channel 2>/dev/null`;
259 # get available channels
262 foreach (@channellist_cmd){
263 $_ =~ /(.*)Channel (\d+)(.*):/;
264 $channel = $2;chomp $channel;
265 if ( $channel =~ /\d+/ ){push(@temp,$channel);}
267 my @channellist = @temp;
269 my @countrylist_cmd = `regdbdump /usr/lib/crda/regulatory.bin 2>/dev/null`;
270 # get available country codes
273 foreach (@countrylist_cmd){
274 $_ =~ /country (.*):/;
275 $country = $1;chomp $country;
276 if ( $country =~ /[0,A-Z][0,A-Z]/ ) {push(@temp,$country);}
278 my @countrylist = @temp;
280 my @txpower_cmd = `iwlist $monwlaninterface txpower 2>/dev/null`;
281 if ( $wlanapsettings{'DRIVER'} eq 'NL80211' ){
282 # There is a bug with NL80211 only all devices can displayed
283 @txpower_cmd = `iwlist txpower 2>/dev/null | sed -e "s|unknown transmit-power information.||g"`;
285 # get available power
288 foreach (@txpower_cmd){
289 $_ =~ /(\s)(\d+)(\s)dBm(\s)(.*)(\W)(\d+)(.*)/;
290 $txpower = $7;chomp $txpower;
291 if ( $txpower =~ /\d+/ ){push(@temp,$txpower."mW");}
294 push(@txpower,"auto");
296 $selected{'SYSLOGLEVEL'}{$wlanapsettings{'SYSLOGLEVEL'}} = "selected='selected'";
297 $selected{'DEBUG'}{$wlanapsettings{'DEBUG'}} = "selected='selected'";
302 &Header
::openbox
('100%', 'center', "WLAN AP");
304 <table width='95%' cellspacing='0'>
308 if ( $wlan_card_status ne '' ){
309 print "<tr><td bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'wlanap wlan services'}</strong></td></tr>";
310 print "<tr><td colspan='1' class='base'>$Lang::tr{'wlanap wlan card'} ($wlanapsettings{'DRIVER'})</td>";
311 print $wlan_card_status eq 'up' ?
$status_started : $status_stopped;
312 print "<tr><td colspan='1' class='base'>$Lang::tr{'wlanap access point'}</td>";
313 print $wlan_ap_status eq 'up' ?
$status_started : $status_stopped;
316 print "<tr><td colspan='2' class='base'>$message";
320 if ( ($memory != 0) && (@pid[0] ne "///") && ($wlan_card_status ne '') ){
321 print "<table width='95%' cellspacing='0' border='0'>";
323 print "<td align='center'></td>";
324 print "<td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>";
325 print "<td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>";
328 print "<td width='35%' align='right'><strong>hostapd</strong> </td>";
329 print "<td bgcolor='$color{'color22'}' align='center'>@pid[0]</td>";
330 print "<td bgcolor='$color{'color22'}' align='center'>$memory KB</td>";
335 if ( $wlan_card_status ne '' ){
337 print "<table width='95%' cellspacing='0' border='0'>";
338 print "<tr align='center'>";
340 print "<tr align='center'>";
341 print "<td width='40%'> </td>";
342 print "<td width='20%'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='$Lang::tr{'start'}' /><input type='image' alt='$Lang::tr{'start'}' title='$Lang::tr{'start'}' src='/images/go-up.png' /></form></td>";
343 print "<td width='20%'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='$Lang::tr{'stop'}' /><input type='image' alt='$Lang::tr{'stop'}' title='$Lang::tr{'stop'}' src='/images/go-down.png' /></form></td>";
344 print "<td width='20%'><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='$Lang::tr{'restart'}' /><input type='image' alt='$Lang::tr{'restart'}' title='$Lang::tr{'restart'}' src='/images/view-refresh.png' /></form></td>";
349 print "<table width='95%' cellspacing='0' border='0'>";
350 print "<tr align='center'>";
351 print "<td colspan='4'><hr size='1'></td>";
353 print "<tr align='center'>";
354 print "<td width='40%'> </td>";
355 print "<td width='20%'><form method='post' action='/cgi-bin/wlanap.cgi'><input type='submit' name='ACTION' value='$Lang::tr{'wlanap del interface'}' /></form></td>";
356 print "<td width='20%'></td>";
357 print "<td width='20%'></td>";
362 if ( $wlan_card_status eq '' ){
364 &Header
::closebigbox
();
365 &Header
::closepage
();
370 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
371 <table width='95%' cellspacing='0'>
372 <tr><td bgcolor='$color{'color20'}' colspan='4' align='left'><strong>$Lang::tr{'wlanap wlan settings'}</strong>
373 <tr><td width='25%' class='base'>SSID: </td><td class='base' colspan='3'><input type='text' name='SSID' size='40' value='$wlanapsettings{'SSID'}' /></td></tr>
374 <!--SSID Broadcast: on => HIDESSID: off -->
375 <tr><td width='25%' class='base'>SSID Broadcast: </td><td class='base' colspan='3'>on <input type='radio' name='HIDESSID' value='off' $checked{'HIDESSID'}{'off'} /> | <input type='radio' name='HIDESSID' value='on' $checked{'HIDESSID'}{'on'} /> off</td></tr>
376 <tr><td width='25%' class='base'>HW Mode: </td><td class='base' colspan='3'>
377 <select name='HW_MODE'>
378 <option value='a' $selected{'HW_MODE'}{'a'}>802.11a</option>
379 <option value='b' $selected{'HW_MODE'}{'b'}>802.11b</option>
380 <option value='g' $selected{'HW_MODE'}{'g'}>802.11g</option>
381 <option value='an' $selected{'HW_MODE'}{'an'}>802.11an</option>
382 <option value='gn' $selected{'HW_MODE'}{'gn'}>802.11gn</option>
386 <tr><td width='25%' class='base'>$Lang::tr{'wlanap encryption'}: </td><td class='base' colspan='3'>
388 <option value='none' $selected{'ENC'}{'none'}>$Lang::tr{'wlanap none'}</option>
389 <option value='wpa1' $selected{'ENC'}{'wpa1'}>WPA1</option>
390 <option value='wpa2' $selected{'ENC'}{'wpa2'}>WPA2</option>
391 <option value='wpa1+2' $selected{'ENC'}{'wpa1+2'}>WPA1+2</option>
394 <tr><td width='25%' class='base'>$Lang::tr{'wlanap channel'}: </td><td class='base' colspan='3'>
395 <select name='CHANNEL'>
398 foreach $channel (@channellist){
399 print "<option $selected{'CHANNEL'}{$channel}>$channel</option>";
404 <tr><td width='25%' class='base'>$Lang::tr{'wlanap country'}: </td><td class='base' colspan='3'>
405 <select name='COUNTRY'>
408 foreach $country (@countrylist){
409 print "<option $selected{'COUNTRY'}{$country}>$country</option>";
414 <tr><td width='25%' class='base'>Tx Power: </td><td class='base' colspan='3'>
418 if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){
419 print "<select name='TXPOWER'>";
420 foreach $txpower (@txpower){
421 print "<option $selected{'TXPOWER'}{$txpower}>$txpower</option> dBm";
423 print " </select></td></tr>";
425 print "<input type='text' name='TXPOWER' size='10' value='$wlanapsettings{'TXPOWER'}' /></td></tr>"
428 <tr><td width='25%' class='base'>Passphrase: </td><td class='base' colspan='3'><input type='text' name='PWD' size='63' value='$wlanapsettings{'PWD'}' /></td></tr>
429 <tr><td width='25%' class='base'>HT Caps: </td><td class='base' colspan='3'><input type='text' name='HTCAPS' size='63' value='$wlanapsettings{'HTCAPS'}' /></td></tr>
430 <tr><td width='25%' class='base'>Loglevel (hostapd): </td><td class='base' width='25%'>
431 <select name='SYSLOGLEVEL'>
432 <option value='0' $selected{'SYSLOGLEVEL'}{'0'}>0 ($Lang::tr{'wlanap verbose'})</option>
433 <option value='1' $selected{'SYSLOGLEVEL'}{'1'}>1 ($Lang::tr{'wlanap debugging'})</option>
434 <option value='2' $selected{'SYSLOGLEVEL'}{'2'}>2 ($Lang::tr{'wlanap informations'})</option>
435 <option value='3' $selected{'SYSLOGLEVEL'}{'3'}>3 ($Lang::tr{'wlanap notifications'})</option>
436 <option value='4' $selected{'SYSLOGLEVEL'}{'4'}>4 ($Lang::tr{'wlanap warnings'})</option>
439 <td width='25%' class='base'>Debuglevel (hostapd): </td><td class='base' width='25%'>
440 <select name='DEBUG'>
441 <option value='0' $selected{'DEBUG'}{'0'}>0 ($Lang::tr{'wlanap verbose'})</option>
442 <option value='1' $selected{'DEBUG'}{'1'}>1 ($Lang::tr{'wlanap debugging'})</option>
443 <option value='2' $selected{'DEBUG'}{'2'}>2 ($Lang::tr{'wlanap informations'})</option>
444 <option value='3' $selected{'DEBUG'}{'3'}>3 ($Lang::tr{'wlanap notifications'})</option>
445 <option value='4' $selected{'DEBUG'}{'4'}>4 ($Lang::tr{'wlanap warnings'})</option>
451 if ( $wlanapsettings{'INTERFACE'} =~ /green0/ ){
454 <table width='95%' cellspacing='0'>
455 <td width='25%' class='base'>Mac Filter: </td><td class='base' width='25%'>
456 <select name='MACMODE'>
457 <option value='0' $selected{'MACMODE'}{'0'}>0 (off)</option>
458 <option value='1' $selected{'MACMODE'}{'1'}>1 (Accept MACs)</option>
459 <option value='2' $selected{'MACMODE'}{'2'}>2 (Deny MACs)</option>
461 </td><td colspan='2'>Mac Adress List (one per line)<br /><textarea name='MACS' cols='20' rows='5' wrap='off'>
464 print `cat /var/ipfire/wlanap/macfile`;
473 <table width='95%' cellspacing='0'>
474 <tr><td align='center'>
475 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
476 <input type='hidden' name='ACTION' value=$Lang::tr{'save'} />
477 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form></td>
483 if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){
484 $status = `wlanconfig $wlanapsettings{'INTERFACE'} list`;
486 if ( $wlanapsettings{'DRIVER'} eq 'NL80211' ){
487 $status = `iw dev $wlanapsettings{'INTERFACE'} station dump`;
491 <table width='95%' cellspacing='0'>
492 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><strong>$Lang::tr{'wlanap wlan status'}</strong></td></tr>
493 <tr><td><pre>@channellist_cmd</pre></td><td><pre>@txpower_cmd</pre></td></tr>
494 <tr><td colspan='2'><pre>$status</pre></td></tr>
497 <table width='95%' cellspacing='0'>
498 <tr><td bgcolor='$color{'color20'}' align='left'><strong>WLan Clients</strong></td></tr>
499 <tr><td> <a href="/cgi-bin/wireless.cgi">$Lang::tr{'wlanap link wireless'}</a></td></tr>
500 <tr><td> <a href="/cgi-bin/dhcp.cgi">$Lang::tr{'wlanap link dhcp'}</a></td></tr>
506 &Header
::closebigbox
();
507 &Header
::closepage
();
509 sub WriteConfig_hostapd
{
510 $wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'});
512 open (CONFIGFILE
, ">/var/ipfire/wlanap/hostapd.conf");
513 print CONFIGFILE
<<END
514 ######################### basic hostapd configuration ##########################
516 interface=$wlanapsettings{'INTERFACE'}
517 driver=$wlanapsettings{'DRIVER_HOSTAPD'}
518 channel=$wlanapsettings{'CHANNEL'}
521 if ( $wlanapsettings{'HW_MODE'} eq 'an' ){
522 print CONFIGFILE
<<END
526 ht_capab=$wlanapsettings{'HTCAPS'}
530 }elsif ( $wlanapsettings{'HW_MODE'} eq 'gn' ){
531 print CONFIGFILE
<<END
535 ht_capab=$wlanapsettings{'HTCAPS'}
540 print CONFIGFILE
<<END
541 hw_mode=$wlanapsettings{'HW_MODE'}
547 print CONFIGFILE
<<END
549 logger_syslog_level=$wlanapsettings{'SYSLOGLEVEL'}
551 logger_stdout_level=$wlanapsettings{'DEBUG'}
552 dump_file=/tmp/hostapd.dump
554 ctrl_interface=/var/run/hostapd
555 ctrl_interface_group=0
558 if ( $wlanapsettings{'HIDESSID'} eq 'on' ){
559 print CONFIGFILE
<<END
560 ssid=$wlanapsettings{'SSID'}
561 ignore_broadcast_ssid=2
566 print CONFIGFILE
<<END
567 ssid=$wlanapsettings{'SSID'}
568 ignore_broadcast_ssid=0
574 if ( $wlanapsettings{'ENC'} eq 'wpa1'){
575 print CONFIGFILE
<<END
576 ######################### wpa hostapd configuration ############################
579 wpa_passphrase=$wlanapsettings{'PWD'}
584 }elsif ( $wlanapsettings{'ENC'} eq 'wpa2'){
585 print CONFIGFILE
<<END
586 ######################### wpa hostapd configuration ############################
589 wpa_passphrase=$wlanapsettings{'PWD'}
594 } elsif ( $wlanapsettings{'ENC'} eq 'wpa1+2'){
595 print CONFIGFILE
<<END
596 ######################### wpa hostapd configuration ############################
599 wpa_passphrase=$wlanapsettings{'PWD'}
608 open (MACFILE
, ">/var/ipfire/wlanap/macfile");
612 if ( $_ ne "" ){print MACFILE
$_;}