#!/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 %netsettings=();
my %wlanapsettings=();
my $channel = '';
my $txpower = '';
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
&General::readhash("/var/ipfire/ethernet/settings", \%netsettings);
$wlanapsettings{'APMODE'} = 'on';
$wlanapsettings{'INTERFACE'} = $netsettings{'BLUE_DEV'};
$wlanapsettings{'SSID'} = 'IPFire';
$wlanapsettings{'HIDESSID'} = 'off';
$wlanapsettings{'ENC'} = 'wpa'; # none / wpa
$wlanapsettings{'TXPOWER'} = 'auto';
# $wlanapsettings{'CC'} = '276'; # CountryCode, 276 = Germany
$wlanapsettings{'CHAN'} = '05';
$wlanapsettings{'PWD'} = 'IPFire-2.x';
$wlanapsettings{'PSK'} = '69eb868ed7b3cc36d767b729048c9c585234723d1eafbe66e5a16957b7c85e9c';
$wlanapsettings{'WPA'} = '1';
$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 %cgiparams=();
$cgiparams{'ACTION'} = '';
$cgiparams{'RUNNING'} = 'off';
$cgiparams{'APMODE'} = 'on';
$cgiparams{'SSID'} = 'IPFire';
$cgiparams{'HIDESSID'} = 'off';
$cgiparams{'ENC'} = 'wpa'; # none / wep / wpa / wep+wpa
$cgiparams{'TXPOWER'} = 'auto';
$cgiparams{'CHAN'} = '05';
$cgiparams{'PWD'} = 'IPFire-2.x';
$cgiparams{'PSK'} = '69eb868ed7b3cc36d767b729048c9c585234723d1eafbe66e5a16957b7c85e9c';
$cgiparams{'WPA'} = '1';
$cgiparams{'SYSLOGLEVEL'} = '0';
$cgiparams{'DEBUG'} = '4';
&Header::getcgihash(\%cgiparams);
&Header::showhttpheaders();
if ( $cgiparams{'ACTION'} eq "$Lang::tr{'save'}" ){
$wlanapsettings{'APMODE'} = $cgiparams{'APMODE'};
$wlanapsettings{'SSID'} = $cgiparams{'SSID'};
$wlanapsettings{'HIDESSID'} = $cgiparams{'HIDESSID'};
$wlanapsettings{'ENC'} = $cgiparams{'ENC'};
$wlanapsettings{'CHAN'} = $cgiparams{'CHAN'};
$wlanapsettings{'TXPOWER'} = $cgiparams{'TXPOWER'};
$wlanapsettings{'PWD'} = $cgiparams{'PWD'};
$wlanapsettings{'PSK'} = $cgiparams{'PSK'};
$wlanapsettings{'WPA'} = $cgiparams{'WPA'};
$wlanapsettings{'SYSLOGLEVEL'}= $cgiparams{'SYSLOGLEVEL'};
$wlanapsettings{'DEBUG'} = $cgiparams{'DEBUG'};
# 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");
}
&Header::openpage('WLAN', 1, '', '');
&Header::openbigbox('100%', 'left', '', $errormessage);
print "";
&Header::closebigbox();
&Header::closepage();
exit 0;
}
print <
END
;
my $status = `cat /proc/net/madwifi/$netsettings{'BLUE_DEV'}/associated_sta`;
print <
END
;
&Header::closebox();
print "";
&Header::closebigbox();
&Header::closepage();
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 <$Lang::tr{'stopped'}";
my $pid = '';
my $testcmd = '';
my $exename;
$cmd =~ /(^[a-z]+)/;
$exename = $1;
if (open(FILE, "/var/run/${cmd}.pid"))
{
$pid = ; chomp $pid;
close FILE;
if (open(FILE, "/proc/${pid}/status"))
{
while ()
{
if (/^Name:\W+(.*)/)
{
$testcmd = $1;
}
}
close FILE;
if ($testcmd =~ /$exename/)
{
$status = "$Lang::tr{'running'} | ";
}
}
}
return $status;
}