]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/wlanap.cgi
Fixed bug from 034c2b7e288590c35d1ec2f6f5baa324420340a6
[people/pmueller/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) 2009 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{'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';
70
71 &General::readhash("/var/ipfire/wlanap/settings", \%wlanapsettings);
72
73 my %cgiparams=();
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);
86
87
88 &Header::showhttpheaders();
89
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'};
98
99 $wlanapsettings{'PWD'} = $cgiparams{'PWD'};
100 $wlanapsettings{'SYSLOGLEVEL'} = $cgiparams{'SYSLOGLEVEL'};
101 $wlanapsettings{'DEBUG'} = $cgiparams{'DEBUG'};
102
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 />";
106 }
107
108 if ( $errormessage eq '' ){
109 &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings);
110 &WriteConfig_hostapd();
111
112 system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1");
113 }
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");
121 }elsif ( $cgiparams{'ACTION'} eq 'Restart' ){
122 system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1");
123 }
124
125 &Header::openpage('WLAN', 1, '', '');
126 &Header::openbigbox('100%', 'left', '', $errormessage);
127 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
128
129 if ( $errormessage ){
130 &Header::openbox('100%', 'center', $Lang::tr{'error messages'});
131 print "<class name='base'>$errormessage\n";
132 print "&nbsp;</class>\n";
133 &Header::closebox();
134 }
135
136
137 # Found this usefull piece of code in BlockOutTraffic AddOn 8-)
138 # fwrules.cgi
139 ###############
140 # DEBUG DEBUG
141 if ( $debug ){
142 &Header::openbox('100%', 'center', 'DEBUG');
143 my $debugCount = 0;
144 foreach my $line (sort keys %cgiparams) {
145 print "$line = '$cgiparams{$line}'<br />\n";
146 $debugCount++;
147 }
148 print "&nbsp;Count: $debugCount\n";
149 &Header::closebox();
150 }
151 # DEBUG DEBUG
152 ###############
153
154 #
155 # Driver and status detection
156 #
157 my $wlan_card_status = 'dummy';
158 my $wlan_ap_status = '';
159 my $message = "";
160
161 $selected{'INTERFACE'}{'green0'} = '';
162 $selected{'INTERFACE'}{'blue0'} = '';
163 $selected{'ENC'}{$wlanapsettings{'INTERFACE'}} = "selected='selected'";
164
165 if ( ($wlanapsettings{'INTERFACE'} eq '') ){
166 $message = "No WLan Interface selected.";
167 &Header::openbox('100%', 'center', "WLAN AP");
168 print <<END
169 $message<br />
170 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
171 <select name='INTERFACE'>
172 <option value='green0' $selected{'INTERFACE'}{'green0'}>green0</option>
173 END
174 ;
175 if ( $netsettings{'BLUE_DEV'} ne ''){
176 print "<option value='blue0' $selected{'INTERFACE'}{'blue0'}>blue0</option>";
177 }
178 print <<END
179 </select>
180 <br />
181 <input type='hidden' name='ACTION' value='$Lang::tr{'interface'}' />
182 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form>
183 END
184 ;
185 &Header::closebox();
186 &Header::closebigbox();
187 &Header::closepage();
188 exit;
189 }else{
190 my $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} 2>/dev/null`;
191
192 if ( $cmd_out eq '' ){
193 $message = "Interface is not a WLAN card.";
194 $wlan_card_status = '';
195 }else{
196 $cmd_out = `/sbin/ifconfig | /bin/grep $wlanapsettings{'INTERFACE'}`;
197 if ( $cmd_out eq '' ){
198 $wlan_card_status = 'down';
199 }else{
200 $wlan_card_status = 'up';
201 $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} | /bin/grep "Mode:Master"`;
202 if ( $cmd_out ne '' ){
203 $wlan_ap_status = 'up';
204 }
205 }
206 }
207 }
208
209 my $checked_hidessid = '';
210 $checked_hidessid = "checked='checked'" if ( $wlanapsettings{'HIDESSID'} eq 'on' );
211
212 $selected{'ENC'}{$wlanapsettings{'ENC'}} = "selected='selected'";
213 $selected{'CHANNEL'}{$wlanapsettings{'CHANNEL'}} = "selected='selected'";
214 $selected{'TXPOWER'}{$wlanapsettings{'TXPOWER'}} = "selected='selected'";
215 $selected{'MACMODE'}{$wlanapsettings{'MACMODE'}} = "selected='selected'";
216
217 my @channellist_cmd = `iwlist $wlanapsettings{'INTERFACE'} channel`;
218 # get available channels
219
220 my @temp;
221 foreach (@channellist_cmd){
222 $_ =~ /(.*)Channel (\d+)(.*):/;
223 $channel = $2;chomp $channel;
224 if ( $channel =~ /\d+/ ){push(@temp,$channel);}
225 }
226 my @channellist = @temp;
227
228 my @txpower_cmd = `iwlist $wlanapsettings{'INTERFACE'} txpower`;
229 # get available channels
230
231 my @temp;
232 foreach (@txpower_cmd){
233 $_ =~ /(\s)(\d+)(\s)dBm(\s)(.*)(\W)(\d+)(.*)/;
234 $txpower = $7;chomp $txpower;
235 if ( $txpower =~ /\d+/ ){push(@temp,$txpower."mW");}
236 }
237 my @txpower = @temp;
238 push(@txpower,"auto");
239
240 $selected{'SYSLOGLEVEL'}{$wlanapsettings{'SYSLOGLEVEL'}} = "selected='selected'";
241 $selected{'DEBUG'}{$wlanapsettings{'DEBUG'}} = "selected='selected'";
242
243 #
244 # Status box
245 #
246 &Header::openbox('100%', 'center', "WLAN AP");
247 print <<END
248 <table width='95%' cellspacing='0'>
249 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Services</b></td></tr>
250 END
251 ;
252 if ( $wlan_card_status ne '' ){
253 print "<tr><td class='base'>WLAN card ($wlanapsettings{'DRIVER'})</td>";
254 print $wlan_card_status eq 'up' ? $status_started : $status_stopped;
255 print "<tr><td class='base'>Access Point</td>";
256 print $wlan_ap_status eq 'up' ? $status_started : $status_stopped;
257 }else{
258 print "<tr><td colspan='2' class='base'><b>$message</b></td></tr>";
259 }
260 print "</table>";
261
262 print <<END
263 <br />
264 <table width='95%' cellspacing='0'>
265 <tr><td align='left' width='40%' />
266 <td align='center' ><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='Start' /><input type='image' alt='Start' title='Start' src='/images/go-up.png' /></form></td>
267 <td align='center' ><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='Stop' /><input type='image' alt='Stop' title='Stop' src='/images/go-down.png' /></form></td>
268 <td align='center' ><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='Restart' /><input type='image' alt='Restart' title='Restart' src='/images/view-refresh.png' /></form></td></tr>
269 </table>
270 END
271 ;
272
273 if ( $wlan_card_status eq '' ){
274 &Header::closebox();
275 &Header::closebigbox();
276 &Header::closepage();
277 exit 0;
278 }
279 print <<END
280 <br />
281 <table width='95%' cellspacing='0'>
282 <tr><td bgcolor='$color{'color20'}' colspan='4' align='left'><b>WLAN Settings</b>
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 /></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='wpa1' $selected{'ENC'}{'wpa1'}>wpa1</option>
289 <option value='wpa2' $selected{'ENC'}{'wpa2'}>wpa2</option>
290 </select>
291 </td></tr>
292 <tr><td width='25%' class='base'>Channel:&nbsp;</td><td class='base' colspan='3'>
293 <select name='CHANNEL'>
294 END
295 ;
296 foreach $channel (@channellist){
297 print "<option $selected{'CHANNEL'}{$channel}>$channel</option>";
298 }
299
300 print <<END
301 </select></td></tr>
302 <tr><td width='25%' class='base'>Tx Power:&nbsp;</td><td class='base' colspan='3'><select name='TXPOWER'>
303 END
304 ;
305 foreach $txpower (@txpower){
306 print "<option $selected{'TXPOWER'}{$txpower}>$txpower</option>&nbsp;dBm";
307 }
308 print <<END
309 </select></td></tr>
310 <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>
311 <tr><td width='25%' class='base'>Loglevel (hostapd):&nbsp;</td><td class='base' width='25%'>
312 <select name='SYSLOGLEVEL'>
313 <option value='0' $selected{'SYSLOGLEVEL'}{'0'}>0 (verbose)</option>
314 <option value='1' $selected{'SYSLOGLEVEL'}{'1'}>1 (debugging)</option>
315 <option value='2' $selected{'SYSLOGLEVEL'}{'2'}>2 (informations)</option>
316 <option value='3' $selected{'SYSLOGLEVEL'}{'3'}>3 (notifications)</option>
317 <option value='4' $selected{'SYSLOGLEVEL'}{'4'}>4 (warnings)</option>
318 </select>
319 </td>
320 <td width='25%' class='base'>Debuglevel (hostapd):&nbsp;</td><td class='base' width='25%'>
321 <select name='DEBUG'>
322 <option value='0' $selected{'DEBUG'}{'0'}>0 (verbose)</option>
323 <option value='1' $selected{'DEBUG'}{'1'}>1 (debugging)</option>
324 <option value='2' $selected{'DEBUG'}{'2'}>2 (informations)</option>
325 <option value='3' $selected{'DEBUG'}{'3'}>3 (notifications)</option>
326 <option value='4' $selected{'DEBUG'}{'4'}>4 (warnings)</option>
327 </select>
328 </td></tr>
329 </table>
330 END
331 ;
332 if ( $wlanapsettings{'INTERFACE'} =~ /green0/ ){
333 print <<END
334 <br />
335 <table width='95%' cellspacing='0'>
336 <td width='25%' class='base'>Mac Filter:&nbsp;</td><td class='base' width='25%'>
337 <select name='MACMODE'>
338 <option value='0' $selected{'MACMODE'}{'0'}>0 (off)</option>
339 <option value='1' $selected{'MACMODE'}{'1'}>1 (Accept MACs)</option>
340 <option value='2' $selected{'MACMODE'}{'2'}>2 (Deny MACs)</option>
341 </select>
342 </td><td colspan='2'>Mac Adress List (one per line)<br /><textarea name='MACS' cols='20' rows='5' wrap='off'>
343 END
344 ;
345 print `cat /var/ipfire/wlanap/macfile`;
346 print <<END
347 </textarea></td>
348 </table>
349 END
350 ;
351 }
352 print <<END
353 <br />
354 <table width='10%' cellspacing='0'>
355 <tr><td align='center'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
356 <input type='hidden' name='ACTION' value=$Lang::tr{'save'} />
357 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form></td>
358 </tr>
359 </table>
360 END
361 ;
362
363 if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){
364 $status = `wlanconfig $wlanapsettings{'INTERFACE'} list`;
365 }
366 print <<END
367 <br />
368 <table width='95%' cellspacing='0'>
369 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Status</b></td></tr>
370 <tr><td><pre>@channellist_cmd</pre></td><td><pre>@txpower_cmd</pre></td></tr>
371 <tr><td colspan='2'><pre>$status</pre></td></tr>
372 </table>
373 END
374 ;
375 &Header::closebox();
376 print "</form>";
377 &Header::closebigbox();
378 &Header::closepage();
379
380 sub WriteConfig_hostapd{
381 $wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'});
382
383 open (CONFIGFILE, ">/var/ipfire/wlanap/hostapd.conf");
384 print CONFIGFILE <<END
385 ######################### basic hostapd configuration ##########################
386 #
387 interface=$wlanapsettings{'INTERFACE'}
388 driver=$wlanapsettings{'DRIVER_HOSTAPD'}
389 logger_syslog=-1
390 logger_syslog_level=$wlanapsettings{'SYSLOGLEVEL'}
391 logger_stdout=-1
392 logger_stdout_level=$wlanapsettings{'DEBUG'}
393 dump_file=/tmp/hostapd.dump
394 auth_algs=3
395 ctrl_interface=/var/run/hostapd
396 ctrl_interface_group=0
397 END
398 ;
399 if ( $wlanapsettings{'HIDESSID'} eq 'on' ){
400 print CONFIGFILE <<END
401 ssid=$wlanapsettings{'SSID'}
402 ignore_broadcast_ssid=2
403 END
404 ;
405
406 }else{
407 print CONFIGFILE <<END
408 ssid=$wlanapsettings{'SSID'}
409 ignore_broadcast_ssid=0
410 END
411 ;
412
413 }
414
415 if ( $wlanapsettings{'ENC'} eq 'wpa1'){
416 print CONFIGFILE <<END
417 ######################### wpa hostapd configuration ############################
418 #
419 wpa=1
420 wpa_passphrase=$wlanapsettings{'PWD'}
421 wpa_key_mgmt=WPA-PSK
422 wpa_pairwise=CCMP TKIP
423 END
424 ;
425 }elsif ( $wlanapsettings{'ENC'} eq 'wpa2'){
426 print CONFIGFILE <<END
427 ######################### wpa hostapd configuration ############################
428 #
429 wpa=2
430 wpa_passphrase=$wlanapsettings{'PWD'}
431 wpa_key_mgmt=WPA-PSK
432 wpa_pairwise=CCMP TKIP
433 END
434 ;
435 }
436 close CONFIGFILE;
437
438 $wlanapsettings{'MACS'} =~ s/\r//gi;
439 chomp($wlanapsettings{'MACS'});
440 open (MACFILE, ">/var/ipfire/wlanap/macfile");
441 print MACFILE <<END
442 $wlanapsettings{'MACS'}
443 END
444 ;
445 close MACFILE;
446 }