#!/usr/bin/perl
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
###############################################################################
#
# WLAN AP cgi based on wlanap.cgi written by Markus Hoffmann & Olaf Westrik
#
use strict;
# enable only the following on debugging purpose
use warnings;
use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require '/var/ipfire/lang.pl';
require '/var/ipfire/header.pl';
my $debug = 0;
my $i = 0;
my $errormessage = '';
my $status_started = "
$Lang::tr{'running'} ";
my $status_stopped = "$Lang::tr{'stopped'} ";
# get rid of used only once warnings
my @onlyonce = ( $Header::colourgreen, $Header::colourred );
undef @onlyonce;
my %selected=();
my %checked=();
my %color = ();
my %mainsettings = ();
my $channel = '';
my $txpower = '';
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
my %wlanapsettings=();
$wlanapsettings{'APMODE'} = 'on';
$wlanapsettings{'BOOTSTART'} = 'on';
$wlanapsettings{'SSID'} = 'IPFire';
$wlanapsettings{'HIDESSID'} = 'off';
$wlanapsettings{'ENC'} = 'wpa'; # none / wep / wpa / wep+wpa
$wlanapsettings{'ANTENNA'} = 'both';
$wlanapsettings{'TXPOWER'} = 'auto';
# $wlanapsettings{'CC'} = '276'; # CountryCode, 276 = Germany
$wlanapsettings{'CHAN'} = '5';
$wlanapsettings{'WEPKEY1'} = 'BF715772DADA8A3E7AFFA5C26B';
$wlanapsettings{'WEPKEY2'} = '';
$wlanapsettings{'WEPKEY3'} = '';
$wlanapsettings{'WEPKEY4'} = '';
$wlanapsettings{'USEDKEY'} = '1';
$wlanapsettings{'PWD'} = 'IPFire-2.x';
$wlanapsettings{'PSK'} = '69eb868ed7b3cc36d767b729048c9c585234723d1eafbe66e5a16957b7c85e9c';
$wlanapsettings{'WPA'} = '3';
$wlanapsettings{'SYSLOGLEVEL'} = '0';
$wlanapsettings{'DEBUG'} = '4';
$wlanapsettings{'DRIVER'} = 'MADWIFI'; # UNKNOWN / MADWIFI / RT2500 / PRISM54 / ...
# WLANMODE= (a/b/g)
&General::readhash("/var/ipfire/wlanap/settings", \%wlanapsettings);
my %netsettings=();
&General::readhash("/var/ipfire/ethernet/settings", \%netsettings);
my %cgiparams=();
$cgiparams{'ACTION'} = '';
$cgiparams{'RUNNING'} = 'off';
$cgiparams{'APMODE'} = 'on';
$cgiparams{'BOOTSTART'} = 'on';
$cgiparams{'SSID'} = 'IPFire';
$cgiparams{'HIDESSID'} = 'off';
$cgiparams{'ENC'} = 'wpa'; # none / wep / wpa / wep+wpa
$cgiparams{'ANTENNA'} = 'both';
$cgiparams{'TXPOWER'} = 'auto';
$cgiparams{'CHAN'} = '5';
$cgiparams{'WEPKEY1'} = 'BF715772DADA8A3E7AFFA5C26B';
$cgiparams{'WEPKEY2'} = '';
$cgiparams{'WEPKEY3'} = '';
$cgiparams{'WEPKEY4'} = '';
$cgiparams{'USEDKEY'} = '1';
$cgiparams{'WEPPWD'} = '';
$cgiparams{'WEPKEYCALC'} = '';
$cgiparams{'PWD'} = 'IPFire-2.x';
$cgiparams{'PSK'} = '69eb868ed7b3cc36d767b729048c9c585234723d1eafbe66e5a16957b7c85e9c';
$cgiparams{'WPA'} = '3';
$cgiparams{'SYSLOGLEVEL'} = '0';
$cgiparams{'DEBUG'} = '4';
&Header::getcgihash(\%cgiparams);
&Header::showhttpheaders();
if ( $cgiparams{'ACTION'} eq "$Lang::tr{'save'}" ){
$wlanapsettings{'APMODE'} = $cgiparams{'APMODE'};
$wlanapsettings{'BOOTSTART'} = $cgiparams{'BOOTSTART'};
$wlanapsettings{'SSID'} = $cgiparams{'SSID'};
$wlanapsettings{'HIDESSID'} = $cgiparams{'HIDESSID'};
$wlanapsettings{'ENC'} = $cgiparams{'ENC'};
$wlanapsettings{'ANTENNA'} = $cgiparams{'ANTENNA'};
$wlanapsettings{'CHAN'} = $cgiparams{'CHAN'};
$wlanapsettings{'TXPOWER'} = $cgiparams{'TXPOWER'};
$wlanapsettings{'WEPKEY1'} = $cgiparams{'WEPKEY1'};
$wlanapsettings{'WEPKEY2'} = $cgiparams{'WEPKEY2'};
$wlanapsettings{'WEPKEY3'} = $cgiparams{'WEPKEY3'};
$wlanapsettings{'WEPKEY4'} = $cgiparams{'WEPKEY4'};
$wlanapsettings{'USEDKEY'} = $cgiparams{'USEDKEY'};
$wlanapsettings{'PWD'} = $cgiparams{'PWD'};
$wlanapsettings{'PSK'} = $cgiparams{'PSK'};
$wlanapsettings{'WPA'} = $cgiparams{'WPA'};
$wlanapsettings{'SYSLOGLEVEL'}= $cgiparams{'SYSLOGLEVEL'};
$wlanapsettings{'DEBUG'} = $cgiparams{'DEBUG'};
# verify WEP keys, allowed characters are 0..9A..F, length must be 10 or 26 characters
for $i ( 1 .. 4 ){
my $wepkey = $wlanapsettings{"WEPKEY${i}"};
next if ( $wepkey eq '' );
if ( (length($wepkey) != 10) && (length($wepkey) != 26) ){
$errormessage .= "Invalid length in WEP Key $i. Key must be 10 or 26 characters. ";
next;
}
if ( $wepkey !~ /[0-9A-Fa-f]$/ ){
$errormessage .= "Invalid character in WEP Key $i. Only A..F and 0..9 allowed. ";
next;
}
$wlanapsettings{"WEPKEY${i}"} = uc($wepkey);
}
# verify WPA Passphrase, must be 8 .. 63 characters
if ( (length($wlanapsettings{'PWD'}) < 8) || (length($wlanapsettings{'PWD'}) > 63) ){
$errormessage .= "Invalid length in WPA Passphrase. Must be between 8 and 63 characters. ";
}
if ( $errormessage eq '' ){
&WriteConfig();
&WriteConfig_hostapd();
system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1") if ( $cgiparams{'RUNNING'} eq 'on' );
}
}elsif ( $cgiparams{'ACTION'} eq 'Start' ){
system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1");
}elsif ( $cgiparams{'ACTION'} eq 'Stop' ){
system("/usr/local/bin/wlanapctrl stop >/dev/null 2>&1");
}elsif ( $cgiparams{'ACTION'} eq 'Calc WEP Key' ){
$cgiparams{'WEPKEYCALC'} = '';
$errormessage = "Invalid length in WEP Passphrase. Must be exactly 13 characters. " if ( length($cgiparams{'WEPPWD'}) != 13 );
if ( $errormessage eq '' ){
$cgiparams{'WEPKEYCALC'} = uc(&WEPKeyCalc($cgiparams{'WEPPWD'}));
}
}elsif ( $cgiparams{'ACTION'} eq 'Random WEP Key' ){
$cgiparams{'WEPKEYCALC'} = &WEPKeyRandom();
}
&Header::openpage('WLAN', 1, '', '');
&Header::openbigbox('100%', 'left', '', $errormessage);
print "";
&Header::closebigbox();
&Header::closepage();
exit 0;
}
#
# WLAN settings
#
&Header::openbox('100%', 'left', "WLAN Settings");
print <
Access Point:
SSID:
Autostart after Boot:
Disable SSID broadcast:
Encryption:
none
wep
wpa
wep+wpa
Use Antenna:
both
1 only
2 only
Channel:
END
;
foreach $channel (@channellist){
print "$channel ";
}
print <
Tx Power:
END
;
foreach $txpower (@txpowerlist){
print "$txpower dBm";
}
print <
END
;
&Header::closebox();
#
# WEP
#
&Header::openbox('100%', 'left', "WEP Configuration");
print <
Key 1:
Key 2:
Key 3:
Key 4:
WEP Key to Use:
1
2
3
4
END
;
&Header::closebox();
#
# WPA
#
&Header::openbox('100%', 'left', "WPA Configuration");
print <
Passphrase:
WPA Version:
1
2
1+2
Loglevel (hostapd):
0 (verbose)
1 (debugging)
2 (informations)
3 (notifications)
4 (warnings)
Debuglevel (hostapd):
0 (no debugging)
1 (minimal)
2 (verbose)
3 (msg dumps)
4 (excessive)
END
;
&Header::closebox();
print "";
&Header::closebigbox();
&Header::closepage();
sub WEPKeyRandom{
my $length = 26; # 10 is also allowed
my $string = "0123456789ABCDEF";
my @chars = split(//,$string);
my $n = @chars;
my $index;
my $key = '';
for ( $i = 0; $i < $length; $i++){
$index = int(rand $n);
$key = $key . $chars[$index];
}
return $key;
}
sub WEPKeyCalc{
require Digest::MD5;
return substr Digest::MD5::md5_hex( substr( shift() x 64, 0, 64 ) ), 0, 26;
}
sub WriteConfig{
&General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings);
}
sub WriteConfig_hostapd{
$wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'});
open (CONFIGFILE, ">/var/ipfire/wlanap/hostapd.conf");
print CONFIGFILE <