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