]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/wlanap.cgi
fb21c4cb518924e7fcf31e7b9aadde0d84136b77
[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{'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 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
253 if ( $wlan_card_status ne '' ){
254 print "<tr><td class='base'>WLAN card ($wlanapsettings{'DRIVER'})</td>";
255 print $wlan_card_status eq 'up' ? $status_started : $status_stopped;
256 print "<tr><td class='base'>Access Point</td>";
257 print $wlan_ap_status eq 'up' ? $status_started : $status_stopped;
258 if ( $wlan_card_status eq 'up' ){
259 print "<tr><td colspan='2' align='center'><input type='submit' name='ACTION' value='Stop' />";
260 print "<input type='submit' name='ACTION' value='Restart' /></td></tr>";
261 }else{
262 print "<tr><td colspan='2' align='center'><input type='submit' name='ACTION' value='Start' /></td></tr>";
263 }
264 }else{
265 print "<tr><td colspan='2' class='base'><b>$message</b></td></tr>";
266 }
267 print "</form>";
268 print "</table>";
269
270 if ( $wlan_card_status eq '' ){
271 &Header::closebox();
272 &Header::closebigbox();
273 &Header::closepage();
274 exit 0;
275 }
276 print <<END
277 <br />
278 <table width='95%' cellspacing='0'>
279 <tr><td bgcolor='$color{'color20'}' colspan='4' align='left'><b>WLAN Settings</b>
280 <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>
281 <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>
282 <tr><td width='25%' class='base'>Encryption:&nbsp;</td><td class='base' colspan='3'>
283 <select name='ENC'>
284 <option value='none' $selected{'ENC'}{'none'}>none</option>
285 <option value='wpa1' $selected{'ENC'}{'wpa1'}>wpa1</option>
286 <option value='wpa2' $selected{'ENC'}{'wpa2'}>wpa2</option>
287 </select>
288 </td></tr>
289 <tr><td width='25%' class='base'>Channel:&nbsp;</td><td class='base' colspan='3'>
290 <select name='CHANNEL'>
291 END
292 ;
293 foreach $channel (@channellist){
294 print "<option $selected{'CHANNEL'}{$channel}>$channel</option>";
295 }
296
297 print <<END
298 </select></td></tr>
299 <tr><td width='25%' class='base'>Tx Power:&nbsp;</td><td class='base' colspan='3'><select name='TXPOWER'>
300 END
301 ;
302 foreach $txpower (@txpower){
303 print "<option $selected{'TXPOWER'}{$txpower}>$txpower</option>&nbsp;dBm";
304 }
305 print <<END
306 </select></td></tr>
307 <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>
308 <tr><td width='25%' class='base'>Loglevel (hostapd):&nbsp;</td><td class='base' width='25%'>
309 <select name='SYSLOGLEVEL'>
310 <option value='0' $selected{'SYSLOGLEVEL'}{'0'}>0 (verbose)</option>
311 <option value='1' $selected{'SYSLOGLEVEL'}{'1'}>1 (debugging)</option>
312 <option value='2' $selected{'SYSLOGLEVEL'}{'2'}>2 (informations)</option>
313 <option value='3' $selected{'SYSLOGLEVEL'}{'3'}>3 (notifications)</option>
314 <option value='4' $selected{'SYSLOGLEVEL'}{'4'}>4 (warnings)</option>
315 </select>
316 </td>
317 <td width='25%' class='base'>Debuglevel (hostapd):&nbsp;</td><td class='base' width='25%'>
318 <select name='DEBUG'>
319 <option value='0' $selected{'DEBUG'}{'0'}>0 (verbose)</option>
320 <option value='1' $selected{'DEBUG'}{'1'}>1 (debugging)</option>
321 <option value='2' $selected{'DEBUG'}{'2'}>2 (informations)</option>
322 <option value='3' $selected{'DEBUG'}{'3'}>3 (notifications)</option>
323 <option value='4' $selected{'DEBUG'}{'4'}>4 (warnings)</option>
324 </select>
325 </td></tr>
326 </table>
327 END
328 ;
329 if ( $wlanapsettings{'INTERFACE'} =~ /green0/ ){
330 print <<END
331 <br />
332 <table width='95%' cellspacing='0'>
333 <td width='25%' class='base'>Mac Filter:&nbsp;</td><td class='base' width='25%'>
334 <select name='MACMODE'>
335 <option value='0' $selected{'MACMODE'}{'0'}>0 (off)</option>
336 <option value='1' $selected{'MACMODE'}{'1'}>1 (Accept MACs)</option>
337 <option value='2' $selected{'MACMODE'}{'2'}>2 (Deny MACs)</option>
338 </select>
339 </td><td colspan='2'>Mac Adress List (one per line)<br /><textarea name='MACS' cols='20' rows='5' wrap='off'>
340 END
341 ;
342 print `cat /var/ipfire/wlanap/macfile`;
343 print <<END
344 </textarea></td>
345 </table>
346 END
347 ;
348 }
349 print <<END
350 <br />
351 <table width='10%' cellspacing='0'>
352 <tr><td align='center'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
353 <input type='hidden' name='ACTION' value=$Lang::tr{'save'} />
354 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form></td>
355 </tr>
356 </table>
357 END
358 ;
359
360 if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){
361 $status = `wlanconfig $wlanapsettings{'INTERFACE'} list`;
362 }
363 print <<END
364 <br />
365 <table width='95%' cellspacing='0'>
366 <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Status</b></td></tr>
367 <tr><td><pre>@channellist_cmd</pre></td><td><pre>@txpower_cmd</pre></td></tr>
368 <tr><td colspan='2'><pre>$status</pre></td></tr>
369 </table>
370 END
371 ;
372 &Header::closebox();
373 print "</form>";
374 &Header::closebigbox();
375 &Header::closepage();
376
377 sub WriteConfig_hostapd{
378 $wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'});
379
380 open (CONFIGFILE, ">/var/ipfire/wlanap/hostapd.conf");
381 print CONFIGFILE <<END
382 ######################### basic hostapd configuration ##########################
383 #
384 interface=$wlanapsettings{'INTERFACE'}
385 driver=$wlanapsettings{'DRIVER_HOSTAPD'}
386 logger_syslog=-1
387 logger_syslog_level=$wlanapsettings{'SYSLOGLEVEL'}
388 logger_stdout=-1
389 logger_stdout_level=$wlanapsettings{'DEBUG'}
390 dump_file=/tmp/hostapd.dump
391 auth_algs=3
392 ctrl_interface=/var/run/hostapd
393 ctrl_interface_group=0
394 END
395 ;
396 if ( $wlanapsettings{'HIDESSID'} eq 'on' ){
397 print CONFIGFILE <<END
398 ssid=$wlanapsettings{'SSID'}
399 ignore_broadcast_ssid=2
400 END
401 ;
402
403 }else{
404 print CONFIGFILE <<END
405 ssid=$wlanapsettings{'SSID'}
406 ignore_broadcast_ssid=0
407 END
408 ;
409
410 }
411
412 if ( $wlanapsettings{'ENC'} eq 'wpa1'){
413 print CONFIGFILE <<END
414 ######################### wpa hostapd configuration ############################
415 #
416 wpa=1
417 wpa_passphrase=$wlanapsettings{'PWD'}
418 wpa_key_mgmt=WPA-PSK
419 wpa_pairwise=CCMP TKIP
420 END
421 ;
422 }elsif ( $wlanapsettings{'ENC'} eq 'wpa2'){
423 print CONFIGFILE <<END
424 ######################### wpa hostapd configuration ############################
425 #
426 wpa=2
427 wpa_passphrase=$wlanapsettings{'PWD'}
428 wpa_key_mgmt=WPA-PSK
429 wpa_pairwise=CCMP TKIP
430 END
431 ;
432 }
433 close CONFIGFILE;
434
435 $wlanapsettings{'MACS'} =~ s/\r//gi;
436 chomp($wlanapsettings{'MACS'});
437 open (MACFILE, ">/var/ipfire/wlanap/macfile");
438 print MACFILE <<END
439 $wlanapsettings{'MACS'}
440 END
441 ;
442 close MACFILE;
443 }