]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/wlanap.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
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'><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>";
41 # get rid of used only once warnings
42 my @onlyonce = ( $Header::colourgreen
, $Header::colourred
);
48 my %mainsettings = ();
50 my %wlanapsettings=();
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);
58 $wlanapsettings{'APMODE'} = 'on';
59 $wlanapsettings{'MACMODE'} = '0';
60 $wlanapsettings{'INTERFACE'} = '';
61 $wlanapsettings{'SSID'} = 'IPFire';
62 $wlanapsettings{'HIDESSID'} = 'off';
63 $wlanapsettings{'ENC'} = 'wpa2'; # none / wpa1 /wpa2
64 $wlanapsettings{'TXPOWER'} = 'auto';
65 $wlanapsettings{'CHANNEL'} = '05';
66 $wlanapsettings{'PWD'} = 'IPFire-2.x';
67 $wlanapsettings{'SYSLOGLEVEL'} = '0';
68 $wlanapsettings{'DEBUG'} = '4';
69 $wlanapsettings{'DRIVER'} = 'MADWIFI';
71 &General
::readhash
("/var/ipfire/wlanap/settings", \
%wlanapsettings);
74 $cgiparams{'ACTION'} = '';
75 $cgiparams{'APMODE'} = 'on';
76 $cgiparams{'MACMODE'} = '0';
77 $cgiparams{'SSID'} = 'IPFire';
78 $cgiparams{'HIDESSID'} = 'off';
79 $cgiparams{'ENC'} = 'wpa2'; # none / wep / wpa / wep+wpa
80 $cgiparams{'TXPOWER'} = 'auto';
81 $cgiparams{'CHANNEL'} = '05';
82 $cgiparams{'PWD'} = 'IPFire-2.x';
83 $cgiparams{'SYSLOGLEVEL'} = '0';
84 $cgiparams{'DEBUG'} = '4';
85 &Header
::getcgihash
(\
%cgiparams);
88 &Header
::showhttpheaders
();
90 if ( $cgiparams{'ACTION'} eq "$Lang::tr{'save'}" ){
91 $wlanapsettings{'SSID'} = $cgiparams{'SSID'};
92 $wlanapsettings{'MACMODE'} = $cgiparams{'MACMODE'};
93 $wlanapsettings{'MACS'} = $cgiparams{'MACS'};
94 $wlanapsettings{'HIDESSID'} = $cgiparams{'HIDESSID'};
95 $wlanapsettings{'ENC'} = $cgiparams{'ENC'};
96 $wlanapsettings{'CHANNEL'} = $cgiparams{'CHANNEL'};
97 $wlanapsettings{'TXPOWER'} = $cgiparams{'TXPOWER'};
99 $wlanapsettings{'PWD'} = $cgiparams{'PWD'};
100 $wlanapsettings{'SYSLOGLEVEL'} = $cgiparams{'SYSLOGLEVEL'};
101 $wlanapsettings{'DEBUG'} = $cgiparams{'DEBUG'};
103 # verify WPA Passphrase, must be 8 .. 63 characters
104 if ( (length($wlanapsettings{'PWD'}) < 8) || (length($wlanapsettings{'PWD'}) > 63) ){
105 $errormessage .= "Invalid length in WPA Passphrase. Must be between 8 and 63 characters.<br />";
108 if ( $errormessage eq '' ){
109 &General
::writehash
("/var/ipfire/wlanap/settings", \
%wlanapsettings);
110 &WriteConfig_hostapd
();
112 system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1");
114 }elsif ( $cgiparams{'ACTION'} eq "$Lang::tr{'interface'}" ){
115 $wlanapsettings{'INTERFACE'} = $cgiparams{'INTERFACE'};
116 &General
::writehash
("/var/ipfire/wlanap/settings", \
%wlanapsettings);
117 }elsif ( $cgiparams{'ACTION'} eq 'Start' ){
118 system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1");
119 }elsif ( $cgiparams{'ACTION'} eq 'Stop' ){
120 system("/usr/local/bin/wlanapctrl stop >/dev/null 2>&1");
123 &Header
::openpage
('WLAN', 1, '', '');
124 &Header
::openbigbox
('100%', 'left', '', $errormessage);
125 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
127 if ( $errormessage ){
128 &Header
::openbox
('100%', 'center', $Lang::tr
{'error messages'});
129 print "<class name='base'>$errormessage\n";
130 print " </class>\n";
135 # Found this usefull piece of code in BlockOutTraffic AddOn 8-)
140 &Header
::openbox
('100%', 'center', 'DEBUG');
142 foreach my $line (sort keys %cgiparams) {
143 print "$line = '$cgiparams{$line}'<br />\n";
146 print " Count: $debugCount\n";
153 # Driver and status detection
155 my $wlan_card_status = 'dummy';
156 my $wlan_ap_status = '';
159 $selected{'INTERFACE'}{'green0'} = '';
160 $selected{'INTERFACE'}{'blue0'} = '';
161 $selected{'ENC'}{$wlanapsettings{'INTERFACE'}} = "selected='selected'";
163 if ( ($wlanapsettings{'INTERFACE'} eq '') ){
164 $message = "No WLan Interface selected.";
165 &Header
::openbox
('100%', 'center', "WLAN AP");
168 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
169 <select name='INTERFACE'>
170 <option value='green0' $selected{'INTERFACE'}{'green0'}>green0</option>
173 if ( $netsettings{'BLUE_DEV'} ne ''){
174 print "<option value='blue0' $selected{'INTERFACE'}{'blue0'}>blue0</option>";
179 <input type='hidden' name='ACTION' value='$Lang::tr{'interface'}' />
180 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form>
184 &Header
::closebigbox
();
185 &Header
::closepage
();
188 my $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} 2>/dev/null`;
190 if ( $cmd_out eq '' ){
191 $message = "Interface is not a WLAN card.";
192 $wlan_card_status = '';
194 $cmd_out = `/sbin/ifconfig | /bin/grep $wlanapsettings{'INTERFACE'}`;
195 if ( $cmd_out eq '' ){
196 $wlan_card_status = 'down';
198 $wlan_card_status = 'up';
199 $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} | /bin/grep "Mode:Master"`;
200 if ( $cmd_out ne '' ){
201 $wlan_ap_status = 'up';
207 my $checked_hidessid = '';
208 $checked_hidessid = "checked='checked'" if ( $wlanapsettings{'HIDESSID'} eq 'on' );
210 $selected{'ENC'}{$wlanapsettings{'ENC'}} = "selected='selected'";
211 $selected{'CHANNEL'}{$wlanapsettings{'CHANNEL'}} = "selected='selected'";
212 $selected{'TXPOWER'}{$wlanapsettings{'TXPOWER'}} = "selected='selected'";
213 $selected{'MACMODE'}{$wlanapsettings{'MACMODE'}} = "selected='selected'";
215 my @channellist_cmd = `iwlist $wlanapsettings{'INTERFACE'} channel`;
216 # get available channels
219 foreach (@channellist_cmd){
220 $_ =~ /(.*)Channel (\d+)(.*):/;
221 $channel = $2;chomp $channel;
222 if ( $channel =~ /\d+/ ){push(@temp,$channel);}
224 my @channellist = @temp;
226 my @txpower_cmd = `iwlist $wlanapsettings{'INTERFACE'} txpower`;
227 # get available channels
230 foreach (@txpower_cmd){
231 $_ =~ /(\s)(\d+)(\s)dBm(\s)(.*)(\W)(\d+)(.*)/;
232 $txpower = $7;chomp $txpower;
233 if ( $txpower =~ /\d+/ ){push(@temp,$txpower."mW");}
236 push(@txpower,"auto");
238 $selected{'SYSLOGLEVEL'}{$wlanapsettings{'SYSLOGLEVEL'}} = "selected='selected'";
239 $selected{'DEBUG'}{$wlanapsettings{'DEBUG'}} = "selected='selected'";
244 &Header
::openbox
('100%', 'center', "WLAN AP");
246 <table width='95%' cellspacing='0'>
247 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Services</b></td></tr>
250 if ( $wlan_card_status ne '' ){
251 print "<tr><td class='base'>WLAN card ($wlanapsettings{'DRIVER'})</td>";
252 print $wlan_card_status eq 'up' ?
$status_started : $status_stopped;
253 print "<tr><td class='base'>Access Point</td>";
254 print $wlan_ap_status eq 'up' ?
$status_started : $status_stopped;
255 if ( $wlan_card_status eq 'up' ){
256 print "<tr><td colspan='2' align='center'><input type='submit' name='ACTION' value='Stop' />";
257 print "<input type='submit' name='ACTION' value='Restart' /></td></tr>";
259 print "<tr><td colspan='2' align='center'><input type='submit' name='ACTION' value='Start' /></td></tr>";
262 print "<tr><td colspan='2' class='base'><b>$message</b></td></tr>";
266 if ( $wlan_card_status eq '' ){
268 &Header
::closebigbox
();
269 &Header
::closepage
();
274 <table width='95%' cellspacing='0'>
275 <tr><td bgcolor='$color{'color20'}' colspan='4' align='left'><b>WLAN Settings</b>
276 <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>
277 <tr><td width='25%' class='base'>Disable SSID broadcast: </td><td class='base' colspan='3'><input type='checkbox' name='HIDESSID' $checked_hidessid /></td></tr>
278 <tr><td width='25%' class='base'>Encryption: </td><td class='base' colspan='3'>
280 <option value='none' $selected{'ENC'}{'none'}>none</option>
281 <option value='wpa1' $selected{'ENC'}{'wpa1'}>wpa1</option>
282 <option value='wpa2' $selected{'ENC'}{'wpa2'}>wpa2</option>
285 <tr><td width='25%' class='base'>Channel: </td><td class='base' colspan='3'>
286 <select name='CHANNEL'>
289 foreach $channel (@channellist){
290 print "<option $selected{'CHANNEL'}{$channel}>$channel</option>";
295 <tr><td width='25%' class='base'>Tx Power: </td><td class='base' colspan='3'><select name='TXPOWER'>
298 foreach $txpower (@txpower){
299 print "<option $selected{'TXPOWER'}{$txpower}>$txpower</option> dBm";
303 <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>
304 <tr><td width='25%' class='base'>Loglevel (hostapd): </td><td class='base' width='25%'>
305 <select name='SYSLOGLEVEL'>
306 <option value='0' $selected{'SYSLOGLEVEL'}{'0'}>0 (verbose)</option>
307 <option value='1' $selected{'SYSLOGLEVEL'}{'1'}>1 (debugging)</option>
308 <option value='2' $selected{'SYSLOGLEVEL'}{'2'}>2 (informations)</option>
309 <option value='3' $selected{'SYSLOGLEVEL'}{'3'}>3 (notifications)</option>
310 <option value='4' $selected{'SYSLOGLEVEL'}{'4'}>4 (warnings)</option>
313 <td width='25%' class='base'>Debuglevel (hostapd): </td><td class='base' width='25%'>
314 <select name='DEBUG'>
315 <option value='0' $selected{'DEBUG'}{'0'}>0 (verbose)</option>
316 <option value='1' $selected{'DEBUG'}{'1'}>1 (debugging)</option>
317 <option value='2' $selected{'DEBUG'}{'2'}>2 (informations)</option>
318 <option value='3' $selected{'DEBUG'}{'3'}>3 (notifications)</option>
319 <option value='4' $selected{'DEBUG'}{'4'}>4 (warnings)</option>
325 if ( $wlanapsettings{'INTERFACE'} =~ /green0/ ){
328 <table width='95%' cellspacing='0'>
329 <td width='25%' class='base'>Mac Filter: </td><td class='base' width='25%'>
330 <select name='MACMODE'>
331 <option value='0' $selected{'MACMODE'}{'0'}>0 (off)</option>
332 <option value='1' $selected{'MACMODE'}{'1'}>1 (Accept MACs)</option>
333 <option value='2' $selected{'MACMODE'}{'2'}>2 (Deny MACs)</option>
335 </td><td colspan='2'>Mac Adress List (one per line)<br /><textarea name='MACS' cols='20' rows='5' wrap='off'>
338 print `cat /var/ipfire/wlanap/macfile`;
347 <table width='10%' cellspacing='0'>
348 <tr><td align='center'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
349 <input type='hidden' name='ACTION' value=$Lang::tr{'save'} />
350 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form></td>
356 if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){
357 $status = `wlanconfig $wlanapsettings{'INTERFACE'} list`;
361 <table width='95%' cellspacing='0'>
362 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Status</b></td></tr>
363 <tr><td><pre>@channellist_cmd</pre></td><td><pre>@txpower_cmd</pre></td></tr>
364 <tr><td colspan='2'><pre>$status</pre></td></tr>
370 &Header
::closebigbox
();
371 &Header
::closepage
();
373 sub WriteConfig_hostapd
{
374 $wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'});
376 open (CONFIGFILE
, ">/var/ipfire/wlanap/hostapd.conf");
377 print CONFIGFILE
<<END
378 ######################### basic hostapd configuration ##########################
380 interface=$wlanapsettings{'INTERFACE'}
381 driver=$wlanapsettings{'DRIVER_HOSTAPD'}
383 logger_syslog_level=$wlanapsettings{'SYSLOGLEVEL'}
385 logger_stdout_level=$wlanapsettings{'DEBUG'}
386 dump_file=/tmp/hostapd.dump
388 ctrl_interface=/var/run/hostapd
389 ctrl_interface_group=0
392 if ( $wlanapsettings{'HIDESSID'} eq 'on' ){
393 print CONFIGFILE
<<END
394 ssid=$wlanapsettings{'SSID'}
395 ignore_broadcast_ssid=2
400 print CONFIGFILE
<<END
401 ssid=$wlanapsettings{'SSID'}
402 ignore_broadcast_ssid=0
408 if ( $wlanapsettings{'ENC'} eq 'wpa1'){
409 print CONFIGFILE
<<END
410 ######################### wpa hostapd configuration ############################
413 wpa_passphrase=$wlanapsettings{'PWD'}
415 wpa_pairwise=CCMP TKIP
418 }elsif ( $wlanapsettings{'ENC'} eq 'wpa2'){
419 print CONFIGFILE
<<END
420 ######################### wpa hostapd configuration ############################
423 wpa_passphrase=$wlanapsettings{'PWD'}
425 wpa_pairwise=CCMP TKIP
431 $wlanapsettings{'MACS'} =~ s/\r//gi;
432 chomp($wlanapsettings{'MACS'});
433 open (MACFILE
, ">/var/ipfire/wlanap/macfile");
435 $wlanapsettings{'MACS'}