#!/usr/bin/perl ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2007-2014 IPFire Team # # # # 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 $status = ''; my $errormessage = ''; my $status_started = "$Lang::tr{'running'}"; my $status_stopped = "$Lang::tr{'stopped'}"; my $count=0; my $col=''; # 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 $country = ''; 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{'ACTION'} = ''; $wlanapsettings{'MACMODE'} = '0'; $wlanapsettings{'INTERFACE'} = ''; $wlanapsettings{'SSID'} = 'IPFire'; $wlanapsettings{'HIDESSID'} = 'off'; $wlanapsettings{'ENC'} = 'wpa2'; # none / wpa1 /wpa2 $wlanapsettings{'TXPOWER'} = 'auto'; $wlanapsettings{'CHANNEL'} = '05'; $wlanapsettings{'COUNTRY'} = '00'; $wlanapsettings{'HW_MODE'} = 'g'; $wlanapsettings{'PWD'} = 'IPFire-2.x'; $wlanapsettings{'SYSLOGLEVEL'} = '0'; $wlanapsettings{'DEBUG'} = '4'; $wlanapsettings{'DRIVER'} = 'MADWIFI'; $wlanapsettings{'HTCAPS'} = ''; &General::readhash("/var/ipfire/wlanap/settings", \%wlanapsettings); &Header::getcgihash(\%wlanapsettings); my @macs = $wlanapsettings{'MACS'}; delete $wlanapsettings{'__CGI__'}; delete $wlanapsettings{'x'}; delete $wlanapsettings{'y'}; delete $wlanapsettings{'MACS'}; delete $wlanapsettings{'ACCEPT_MACS'}; delete $wlanapsettings{'DENY_MACS'}; &Header::showhttpheaders(); my $string=(); my $status=(); my $errormessage = ''; my $memory = 0; my @memory=(); my @pid=(); my @hostapd=(); sub pid { # for pid and memory open(FILE, '/usr/local/bin/addonctrl hostapd status | '); @hostapd = ; close(FILE); $string = join("", @hostapd); $string =~ s/[a-z_]//gi; $string =~ s/\[[0-1]\;[0-9]+//gi; $string =~ s/[\(\)\.]//gi; $string =~ s/ //gi; $string =~ s///gi; @pid = split(/\s/,$string); if (open(FILE, "/proc/$pid[0]/statm")){ my $temp = ; @memory = split(/ /,$temp); close(FILE); } $memory+=$memory[0]; } pid(); if ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'wlanap del interface'}" ){ delete $wlanapsettings{'INTERFACE'}; &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings); } if ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'save'}" ){ # verify WPA Passphrase - only with enabled enc if (($wlanapsettings{'ENC'} eq "wpa1") || ($wlanapsettings{'ENC'} eq "wpa2") || ($wlanapsettings{'ENC'} eq "wpa1+2")){ # must be 8 .. 63 characters if ( (length($wlanapsettings{'PWD'}) < 8) || (length($wlanapsettings{'PWD'}) > 63)){ $errormessage .= "$Lang::tr{'wlanap invalid wpa'}
"; } # only ASCII alowed if ( !($wlanapsettings{'PWD'} !~ /[^\x00-\x7f]/) ){ $errormessage .= "$Lang::tr{'wlanap invalid wpa'}
"; } } if ( $errormessage eq '' ){ &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings); &WriteConfig_hostapd(); system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1"); pid(); } }elsif ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'wlanap interface'}" ){ &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings); }elsif ( ($wlanapsettings{'ACTION'} eq "$Lang::tr{'start'}") && ($memory == 0) ){ system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1"); pid(); }elsif ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'stop'}" ){ system("/usr/local/bin/wlanapctrl stop >/dev/null 2>&1"); $memory=0; } &Header::openpage('', 1, '', ''); &Header::openbigbox('100%', 'left', '', $errormessage); if ( $errormessage ){ &Header::openbox('100%', 'center', $Lang::tr{'error messages'}); print "$errormessage\n"; print " \n"; &Header::closebox(); } # Found this usefull piece of code in BlockOutTraffic AddOn 8-) # fwrules.cgi ############### # DEBUG DEBUG if ( $debug ){ &Header::openbox('100%', 'center', 'DEBUG'); my $debugCount = 0; foreach my $line (sort keys %wlanapsettings) { print "$line = '$wlanapsettings{$line}'
\n"; $debugCount++; } print " Count: $debugCount\n"; &Header::closebox(); } # DEBUG DEBUG ############### # # Driver and status detection # my $wlan_card_status = 'dummy'; my $wlan_ap_status = ''; my $message = ""; $selected{'INTERFACE'}{'green0'} = ''; $selected{'INTERFACE'}{'blue0'} = ''; $selected{'ENC'}{$wlanapsettings{'INTERFACE'}} = "selected='selected'"; if ( ($wlanapsettings{'INTERFACE'} eq '') ){ $message = $Lang::tr{'wlanap select interface'}; &Header::openbox('100%', 'center', "WLAN AP"); print <



END ; &Header::closebox(); &Header::closebigbox(); &Header::closepage(); exit; }else{ my $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} 2>/dev/null`; if ( $cmd_out eq '' ){ $message = "$Lang::tr{'wlanap no interface'}"; $wlan_card_status = ''; }else{ $cmd_out = `/sbin/ifconfig | /bin/grep $wlanapsettings{'INTERFACE'}`; if ( $cmd_out eq '' ){ $wlan_card_status = 'down'; }else{ $wlan_card_status = 'up'; $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} | /bin/grep "Mode:Master"`; if ( $cmd_out ne '' ){ $wlan_ap_status = 'up'; } } } } # Change old "n" to "gn" if ( $wlanapsettings{'HW_MODE'} eq 'n' ) { $wlanapsettings{'HW_MODE'}='gn'; } $checked{'HIDESSID'}{'off'} = ''; $checked{'HIDESSID'}{'on'} = ''; $checked{'HIDESSID'}{$wlanapsettings{'HIDESSID'}} = "checked='checked'"; $selected{'ENC'}{$wlanapsettings{'ENC'}} = "selected='selected'"; $selected{'CHANNEL'}{$wlanapsettings{'CHANNEL'}} = "selected='selected'"; $selected{'COUNTRY'}{$wlanapsettings{'COUNTRY'}} = "selected='selected'"; $selected{'TXPOWER'}{$wlanapsettings{'TXPOWER'}} = "selected='selected'"; $selected{'HW_MODE'}{$wlanapsettings{'HW_MODE'}} = "selected='selected'"; $selected{'MACMODE'}{$wlanapsettings{'MACMODE'}} = "selected='selected'"; my $monwlaninterface = $wlanapsettings{'INTERFACE'}; if ( -d '/sys/class/net/mon.'.$wlanapsettings{'INTERFACE'} ) { $monwlaninterface = 'mon.'.$wlanapsettings{'INTERFACE'}; } my @channellist_cmd; my @channellist; if ( $wlanapsettings{'DRIVER'} eq 'NL80211' ){ my $wiphy = `iw dev $wlanapsettings{'INTERFACE'} info | grep wiphy | cut -d" " -f2`; chomp $wiphy; @channellist_cmd = `iw phy phy$wiphy info | grep " MHz \\\[" | grep -v "(disabled)" | grep -v "no IBSS" | grep -v "passive scanning" 2>/dev/null`; # get available channels my @temp; foreach (@channellist_cmd){ $_ =~ /(.*) \[(\d+)(.*)\]/; $channel = $2;chomp $channel; if ( $channel =~ /\d+/ ){push(@temp,$channel);} } @channellist = @temp; } else { @channellist_cmd = `iwlist $monwlaninterface channel|tail -n +2 2>/dev/null`; # get available channels my @temp; foreach (@channellist_cmd){ $_ =~ /(.*)Channel (\d+)(.*):/; $channel = $2;chomp $channel; if ( $channel =~ /\d+/ ){push(@temp,$channel);} } @channellist = @temp; } my @countrylist_cmd = `regdbdump /usr/lib/crda/regulatory.bin 2>/dev/null`; # get available country codes my @temp; foreach (@countrylist_cmd){ $_ =~ /country (.*):/; $country = $1;chomp $country; if ( $country =~ /[0,A-Z][0,A-Z]/ ) {push(@temp,$country);} } my @countrylist = @temp; my @txpower_cmd = `iwlist $monwlaninterface txpower 2>/dev/null`; if ( $wlanapsettings{'DRIVER'} eq 'NL80211' ){ # There is a bug with NL80211 only all devices can displayed @txpower_cmd = `iwlist txpower 2>/dev/null | sed -e "s|unknown transmit-power information.||g"`; } # get available power my @temp; foreach (@txpower_cmd){ $_ =~ /(\s)(\d+)(\s)dBm(\s)(.*)(\W)(\d+)(.*)/; $txpower = $7;chomp $txpower; if ( $txpower =~ /\d+/ ){push(@temp,$txpower."mW");} } my @txpower = @temp; push(@txpower,"auto"); $selected{'SYSLOGLEVEL'}{$wlanapsettings{'SYSLOGLEVEL'}} = "selected='selected'"; $selected{'DEBUG'}{$wlanapsettings{'DEBUG'}} = "selected='selected'"; # # Status box # &Header::openbox('100%', 'center', "WLAN AP"); print < END ; if ( $wlan_card_status ne '' ){ print "$Lang::tr{'service'}StatusPID$Lang::tr{'memory'}$Lang::tr{'action'}"; print "$Lang::tr{'wlanap wlan card'} ($wlanapsettings{'DRIVER'})"; print $wlan_card_status eq 'up' ? $status_started : $status_stopped; print""; print "$Lang::tr{'wlanap access point'}"; print $wlan_ap_status eq 'up' ? $status_started : $status_stopped; if ( ($memory != 0) && (@pid[0] ne "///") ){ print "@pid[0]"; print "$memory KB"; print "
"; print "
"; }else{ print""; print "
"; print "
"; } }else{ print "$message"; } print ""; if ( $wlan_card_status eq '' ){ print "
"; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print "
 
"; } if ( $wlan_card_status eq '' ){ &Header::closebox(); &Header::closebigbox(); &Header::closepage(); exit 0; } print <
END ; if ( scalar @channellist > 0 ){ print <" } else { print < END ; } print< END ; print <"; } else { print "" } print <
$Lang::tr{'wlanap wlan settings'}

SSID: 
SSID Broadcast: on | off
$Lang::tr{'wlanap country'}: 
HW Mode: 
$Lang::tr{'wlanap channel'}: 
$Lang::tr{'wlanap channel'}: 

$Lang::tr{'wlanap encryption'}: 
Passphrase: 

HT Caps: 
Tx Power:  END ; if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){ print "
Loglevel (hostapd):  Debuglevel (hostapd): 

END ; if ( $wlanapsettings{'INTERFACE'} =~ /green0/ ){ print <
$Lang::tr{'mac filter'}
Mac Filter:  Mac Adress List (one per line)
END ; } print <
END ; my @status; if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){ @status = `wlanconfig $wlanapsettings{'INTERFACE'} list`; } if ( $wlanapsettings{'DRIVER'} eq 'NL80211' ){ @status = `iw dev $wlanapsettings{'INTERFACE'} info && iw dev $wlanapsettings{'INTERFACE'} station dump && echo ""`; } print < END ; for (my $i=0;$i<$#status;$i++){ if (@status[$i]=~"^Station ") { $count++; } if ($count % 2){ $col="bgcolor='$color{'color20'}'"; }else{ $col="bgcolor='$color{'color22'}'"; } print""; if (! @status[$i]=~"^/t" ) { $count++; } } $count++; foreach my $nr (@channellist_cmd){ if ($count % 2){ $col="bgcolor='$color{'color20'}'"; }else{ $col="bgcolor='$color{'color22'}'"; } print""; $count++; } for (my $i=0;$i<$#txpower_cmd;$i=$i+2){ if ($count % 2){ $col="bgcolor='$color{'color20'}'"; }else{ $col="bgcolor='$color{'color22'}'"; } print ""; $count++; } print "
$Lang::tr{'wlanap wlan status'}
@status[$i]
$nr
@txpower_cmd[$i]

"; print <
$Lang::tr{'wlan clients'}
 $Lang::tr{'wlanap link wireless'}
 $Lang::tr{'wlanap link dhcp'}

END ; &Header::closebox(); print ""; &Header::closebigbox(); &Header::closepage(); sub WriteConfig_hostapd{ $wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'}); open (CONFIGFILE, ">/var/ipfire/wlanap/hostapd.conf"); print CONFIGFILE </var/ipfire/wlanap/macfile"); foreach(@macs){ $_ =~ s/\r//gi; chomp($_); if ( $_ ne "" ){print MACFILE $_;} } close MACFILE; }