]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/wlanap.cgi
Fixed WLANAP Channel
[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) 2008 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 $status = '';
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'} = 'wpa2'; # none / wpa1 /wpa2
63 $wlanapsettings{'TXPOWER'} = 'auto';
64 $wlanapsettings{'CHANNEL'} = '05';
65 $wlanapsettings{'PWD'} = 'IPFire-2.x';
66 $wlanapsettings{'SYSLOGLEVEL'} = '0';
67 $wlanapsettings{'DEBUG'} = '4';
68 $wlanapsettings{'DRIVER'} = 'MADWIFI';
69
70 &General::readhash("/var/ipfire/wlanap/settings", \%wlanapsettings);
71
72 my %cgiparams=();
73 $cgiparams{'ACTION'} = '';
74 $cgiparams{'APMODE'} = 'on';
75 $cgiparams{'SSID'} = 'IPFire';
76 $cgiparams{'HIDESSID'} = 'off';
77 $cgiparams{'ENC'} = 'wpa2'; # none / wep / wpa / wep+wpa
78 $cgiparams{'TXPOWER'} = 'auto';
79 $cgiparams{'CHANNEL'} = '05';
80 $cgiparams{'PWD'} = 'IPFire-2.x';
81 $cgiparams{'SYSLOGLEVEL'} = '0';
82 $cgiparams{'DEBUG'} = '4';
83 &Header::getcgihash(\%cgiparams);
84
85
86 &Header::showhttpheaders();
87
88 if ( $cgiparams{'ACTION'} eq "$Lang::tr{'save'}" ){
89 $wlanapsettings{'SSID'} = $cgiparams{'SSID'};
90 $wlanapsettings{'HIDESSID'} = $cgiparams{'HIDESSID'};
91 $wlanapsettings{'ENC'} = $cgiparams{'ENC'};
92 $wlanapsettings{'CHANNEL'} = $cgiparams{'CHANNEL'};
93 $wlanapsettings{'TXPOWER'} = $cgiparams{'TXPOWER'};
94
95 $wlanapsettings{'PWD'} = $cgiparams{'PWD'};
96 $wlanapsettings{'SYSLOGLEVEL'}= $cgiparams{'SYSLOGLEVEL'};
97 $wlanapsettings{'DEBUG'} = $cgiparams{'DEBUG'};
98
99 # verify WPA Passphrase, must be 8 .. 63 characters
100 if ( (length($wlanapsettings{'PWD'}) < 8) || (length($wlanapsettings{'PWD'}) > 63) ){
101 $errormessage .= "Invalid length in WPA Passphrase. Must be between 8 and 63 characters.<br />";
102 }
103
104 if ( $errormessage eq '' ){
105 &WriteConfig();
106 &WriteConfig_hostapd();
107
108 system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1");
109 }
110 }elsif ( $cgiparams{'ACTION'} eq 'Start' ){
111 system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1");
112 }elsif ( $cgiparams{'ACTION'} eq 'Stop' ){
113 system("/usr/local/bin/wlanapctrl stop >/dev/null 2>&1");
114 }
115
116 &Header::openpage('WLAN', 1, '', '');
117 &Header::openbigbox('100%', 'left', '', $errormessage);
118 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
119
120 if ( $errormessage ){
121 &Header::openbox('100%', 'center', $Lang::tr{'error messages'});
122 print "<class name='base'>$errormessage\n";
123 print "&nbsp;</class>\n";
124 &Header::closebox();
125 }
126
127
128 # Found this usefull piece of code in BlockOutTraffic AddOn 8-)
129 # fwrules.cgi
130 ###############
131 # DEBUG DEBUG
132 if ( $debug ){
133 &Header::openbox('100%', 'center', 'DEBUG');
134 my $debugCount = 0;
135 foreach my $line (sort keys %cgiparams) {
136 print "$line = '$cgiparams{$line}'<br />\n";
137 $debugCount++;
138 }
139 print "&nbsp;Count: $debugCount\n";
140 &Header::closebox();
141 }
142 # DEBUG DEBUG
143 ###############
144
145 #
146 # Driver and status detection
147 #
148 my $wlan_card_status = 'dummy';
149 my $wlan_ap_status = '';
150 my $blue_message = "";
151
152 if ( ($netsettings{'BLUE_DEV'} eq '') || ($netsettings{'BLUE_DRIVER'} eq '') ){
153 $blue_message = "No BLUE Interface.";
154 }else{
155 my $cmd_out = `/usr/sbin/iwconfig $netsettings{'BLUE_DEV'} 2>/dev/null`;
156
157 if ( $cmd_out eq '' ){
158 $blue_message = "BLUE Interface is not a WLAN card.";
159 }else{
160 $cmd_out = `/sbin/ifconfig | /bin/grep $netsettings{'BLUE_DEV'}`;
161 if ( $cmd_out eq '' ){
162 $wlan_card_status = 'down';
163 }else{
164 $wlan_card_status = 'up';
165 $cmd_out = `/usr/sbin/iwconfig $netsettings{'BLUE_DEV'} | /bin/grep "Mode:Master"`;
166 if ( $cmd_out ne '' ){
167 $wlan_ap_status = 'up';
168 }
169 }
170 }
171 }
172
173 my $checked_hidessid = '';
174 $checked_hidessid = "checked='checked'" if ( $wlanapsettings{'HIDESSID'} eq 'on' );
175
176 $selected{'ENC'}{'none'} = '';
177 $selected{'ENC'}{'wpa1'} = '';
178 $selected{'ENC'}{'wpa2'} = '';
179 $selected{'ENC'}{$wlanapsettings{'ENC'}} = "selected='selected'";
180 $selected{'CHANNEL'}{$wlanapsettings{'CHANNEL'}} = "selected='selected'";
181 $selected{'TXPOWER'}{$wlanapsettings{'TXPOWER'}} = "selected='selected'";
182
183 my @channellist_cmd = `iwlist $netsettings{'BLUE_DEV'} channel`;
184 # get available channels
185
186 my @temp;
187 foreach (@channellist_cmd){
188 $_ =~ /(.*)Channel (\d+)(.*):/;
189 $channel = $2;chomp $channel;
190 if ( $channel =~ /\d+/ ){push(@temp,$channel);}
191 }
192 my @channellist = @temp;
193
194 my @txpower_cmd = `iwlist $netsettings{'BLUE_DEV'} txpower`;
195 # get available channels
196
197 my @temp;
198 foreach (@txpower_cmd){
199 $_ =~ /(\s)(\d+)(\s)dBm(\s)(.*)(\W)(\d+)(.*)/;
200 $txpower = $7;chomp $txpower;
201 if ( $txpower =~ /\d+/ ){push(@temp,$txpower."mW");}
202 }
203 my @txpower = @temp;
204 push(@txpower,"auto");
205
206 $selected{'SYSLOGLEVEL'}{'0'} = '';
207 $selected{'SYSLOGLEVEL'}{'1'} = '';
208 $selected{'SYSLOGLEVEL'}{'2'} = '';
209 $selected{'SYSLOGLEVEL'}{'3'} = '';
210 $selected{'SYSLOGLEVEL'}{'4'} = '';
211 $selected{'SYSLOGLEVEL'}{$wlanapsettings{'SYSLOGLEVEL'}} = "selected='selected'";
212
213 $selected{'DEBUG'}{'0'} = '';
214 $selected{'DEBUG'}{'1'} = '';
215 $selected{'DEBUG'}{'2'} = '';
216 $selected{'DEBUG'}{'3'} = '';
217 $selected{'DEBUG'}{'4'} = '';
218 $selected{'DEBUG'}{$wlanapsettings{'DEBUG'}} = "selected='selected'";
219
220 #
221 # Status box
222 #
223 &Header::openbox('100%', 'center', "WLAN AP");
224 print <<END
225 <table width='95%' cellspacing='0'>
226 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Services</b></td></tr>
227 END
228 ;
229 if ( $wlan_card_status ne '' ){
230 print "<tr><td class='base'>WLAN card ($wlanapsettings{'DRIVER'})</td>";
231 print $wlan_card_status eq 'up' ? $status_started : $status_stopped;
232 print "<tr><td class='base'>Access Point</td>";
233 print $wlan_ap_status eq 'up' ? $status_started : $status_stopped;
234 if ( $wlan_card_status eq 'up' ){
235 print "<tr><td colspan='2' align='center'><input type='submit' name='ACTION' value='Stop' /><input type='hidden' name='RUNNING' value='on' /></td></tr>";
236 }else{
237 print "<tr><td colspan='2' align='center'><input type='submit' name='ACTION' value='Start' /></td></tr>";
238 }
239 }else{
240 print "<tr><td colspan='2' class='base'><b>$blue_message</b></td></tr>";
241 }
242 print "</table>";
243
244 if ( $wlan_card_status eq '' ){
245 print "</form>";
246 &Header::closebigbox();
247 &Header::closepage();
248 exit 0;
249 }
250 print <<END
251 <br />
252 <table width='95%' cellspacing='0'>
253 <tr><td bgcolor='$color{'color20'}' colspan='4' align='left'><b>WLAN Settings</b>
254 <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>
255 <tr><td width='25%' class='base'>Disable SSID broadcast:&nbsp;</td><td class='base' colspan='3'><input type='checkbox' name='HIDESSID' $checked_hidessid /></td></tr>
256 <tr><td width='25%' class='base'>Encryption:&nbsp;</td><td class='base' colspan='3'>
257 <select name='ENC'>
258 <option value='none' $selected{'ENC'}{'none'}>none</option>
259 <option value='wpa1' $selected{'ENC'}{'wpa1'}>wpa1</option>
260 <option value='wpa2' $selected{'ENC'}{'wpa2'}>wpa2</option>
261 </select>
262 </td></tr>
263 <tr><td width='25%' class='base'>Channel:&nbsp;</td><td class='base' colspan='3'>
264 <select name='CHANNEL'>
265 END
266 ;
267 foreach $channel (@channellist){
268 print "<option $selected{'CHANNEL'}{$channel}>$channel</option>";
269 }
270
271 print <<END
272 </select></td></tr>
273 <tr><td width='25%' class='base'>Tx Power:&nbsp;</td><td class='base' colspan='3'><select name='TXPOWER'>
274 END
275 ;
276 foreach $txpower (@txpower){
277 print "<option $selected{'TXPOWER'}{$txpower}>$txpower</option>&nbsp;dBm";
278 }
279 print <<END
280 </select></td></tr>
281 <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>
282 <tr><td width='25%' class='base'>Loglevel (hostapd):&nbsp;</td><td class='base' width='25%'>
283 <select name='SYSLOGLEVEL'>
284 <option value='0' $selected{'SYSLOGLEVEL'}{'0'}>0 (verbose)</option>
285 <option value='1' $selected{'SYSLOGLEVEL'}{'1'}>1 (debugging)</option>
286 <option value='2' $selected{'SYSLOGLEVEL'}{'2'}>2 (informations)</option>
287 <option value='3' $selected{'SYSLOGLEVEL'}{'3'}>3 (notifications)</option>
288 <option value='4' $selected{'SYSLOGLEVEL'}{'4'}>4 (warnings)</option>
289 </select>
290 </td>
291 <td width='25%' class='base'>Debuglevel (hostapd):&nbsp;</td><td class='base' width='25%'>
292 <select name='DEBUG'>
293 <option value='0' $selected{'DEBUG'}{'0'}>0 (verbose)</option>
294 <option value='1' $selected{'DEBUG'}{'1'}>1 (debugging)</option>
295 <option value='2' $selected{'DEBUG'}{'2'}>2 (informations)</option>
296 <option value='3' $selected{'DEBUG'}{'3'}>3 (notifications)</option>
297 <option value='4' $selected{'DEBUG'}{'4'}>4 (warnings)</option>
298 </select>
299 </td></tr>
300 </table>
301 <br />
302 <table width='10%' cellspacing='0'>
303 <tr><td align='center'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
304 <input type='hidden' name='ACTION' value=$Lang::tr{'save'} />
305 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form></td>
306 </tr>
307 </table>
308 END
309 ;
310
311 if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){
312 $status = `wlanconfig $netsettings{'BLUE_DEV'} list`;
313 }
314 print <<END
315 <br />
316 <table width='95%' cellspacing='0'>
317 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Status</b></td></tr>
318 <tr><td><pre>@channellist_cmd</pre></td><td><pre>@txpower_cmd</pre></td></tr>
319 <tr><td colspan='2'><pre>$status</pre></td></tr>
320 </table>
321 END
322 ;
323 &Header::closebox();
324 print "</form>";
325 &Header::closebigbox();
326 &Header::closepage();
327
328 sub WriteConfig{
329 &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings);
330 }
331
332 sub WriteConfig_hostapd{
333 $wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'});
334
335 open (CONFIGFILE, ">/var/ipfire/wlanap/hostapd.conf");
336 print CONFIGFILE <<END
337 ######################### basic hostapd configuration ##########################
338 #
339 interface=$netsettings{'BLUE_DEV'}
340 driver=$wlanapsettings{'DRIVER_HOSTAPD'}
341 logger_syslog=-1
342 logger_syslog_level=$wlanapsettings{'SYSLOGLEVEL'}
343 logger_stdout=-1
344 logger_stdout_level=$wlanapsettings{'DEBUG'}
345 dump_file=/tmp/hostapd.dump
346 auth_algs=3
347 ctrl_interface=/var/run/hostapd
348 ctrl_interface_group=0
349 macaddr_acl=0
350 END
351 ;
352 if ( $wlanapsettings{'HIDESSID'} eq 'on' ){
353 print CONFIGFILE <<END
354 ssid=$wlanapsettings{'SSID'}
355 ignore_broadcast_ssid=2
356 END
357 ;
358
359 }else{
360 print CONFIGFILE <<END
361 ssid=$wlanapsettings{'SSID'}
362 ignore_broadcast_ssid=0
363 END
364 ;
365
366 }
367
368 if ( $wlanapsettings{'ENC'} eq 'wpa1'){
369 print CONFIGFILE <<END
370 ######################### wpa hostapd configuration ############################
371 #
372 wpa=1
373 wpa_passphrase=$wlanapsettings{'PWD'}
374 wpa_key_mgmt=WPA-PSK
375 wpa_pairwise=CCMP TKIP
376 END
377 ;
378 }elsif ( $wlanapsettings{'ENC'} eq 'wpa2'){
379 print CONFIGFILE <<END
380 ######################### wpa hostapd configuration ############################
381 #
382 wpa=2
383 wpa_passphrase=$wlanapsettings{'PWD'}
384 wpa_key_mgmt=WPA-PSK
385 wpa_pairwise=CCMP TKIP
386 END
387 ;
388 }
389 close CONFIGFILE;
390 }