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