#!/usr/bin/perl
#
# SmoothWall CGIs
#
# This code is distributed under the terms of the GPL
#
# (c) The SmoothWall Team
#
use strict;
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
our %pppsettings=();
my %temppppsettings=();
our %modemsettings=();
our %isdnsettings=();
our %netsettings=();
my %selected=();
my %checked=();
my @profilenames=();
my $errormessage = '';
my $maxprofiles = 5;
my $kernel=`/bin/uname -r | /usr/bin/tr -d '\012'`;
my %color = ();
my %mainsettings = ();
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
&Header::showhttpheaders();
$pppsettings{'ACTION'} = '';
&initprofile();
&Header::getcgihash(\%pppsettings);
if ($pppsettings{'ACTION'} ne '' &&
(-e '/var/run/ppp-ipcop.pid' || -e "${General::swroot}/red/active"))
{
$errormessage = $Lang::tr{'unable to alter profiles while red is active'};
# read in the current vars
%pppsettings = ();
$pppsettings{'VALID'} = '';
&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
}
elsif ($pppsettings{'ACTION'} eq $Lang::tr{'refresh'})
{
unless ($pppsettings{'TYPE'} =~ /^(modem|serial|isdn|pppoe|pptp|alcatelusb|alcatelusbk|amedynusbadsl|conexantusbadsl|conexantpciadsl|3cp4218usbadsl|pulsardsl|eciadsl|fritzdsl|bewanadsl|eagleusbadsl)$/) {
$errormessage = $Lang::tr{'invalid input'};
goto ERROR; }
my $type = $pppsettings{'TYPE'};
&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
$pppsettings{'TYPE'} = $type;
}
elsif ($pppsettings{'ACTION'} eq $Lang::tr{'save'})
{
if ($pppsettings{'TYPE'} =~ /^(modem|serial|isdn)$/ && $pppsettings{'COMPORT'} !~ /^(ttyS0|ttyS1|ttyS2|ttyS3|ttyS4|ttyACM0|ttyACM1|ttyACM2|ttyACM3|isdn1|isdn2)$/) {
$errormessage = $Lang::tr{'invalid input'};
goto ERROR; }
if ($pppsettings{'TYPE'} =~ /^(modem|serial)$/ && $pppsettings{'DTERATE'} !~ /^(9600|19200|38400|57600|115200|230400|460800)$/) {
$errormessage = $Lang::tr{'invalid input'};
goto ERROR; }
if ($pppsettings{'TYPE'} eq 'modem' && $pppsettings{'DIALMODE'} !~ /^(T|P)$/) {
$errormessage = $Lang::tr{'invalid input'};
goto ERROR; }
if ($pppsettings{'AUTH'} !~ /^(pap-or-chap|pap|chap|standard-login-script|demon-login-script|other-login-script)$/) {
$errormessage = $Lang::tr{'invalid input'};
goto ERROR;
}
if ($pppsettings{'PROFILENAME'} eq '') {
$errormessage = $Lang::tr{'profile name not given'};
$pppsettings{'PROFILENAME'} = '';
goto ERROR; }
if ($pppsettings{'TYPE'} =~ /^(modem|isdn)$/) {
if ($pppsettings{'TELEPHONE'} eq '') {
$errormessage = $Lang::tr{'telephone not set'};
goto ERROR; }
if (!($pppsettings{'TELEPHONE'} =~ /^[\d\*\#\,]+$/)) {
$errormessage = $Lang::tr{'bad characters in the telephone number field'};
goto ERROR; }
}
unless (($pppsettings{'PROTOCOL'} eq 'RFC1483' && $pppsettings{'METHOD'} =~ /^(STATIC|DHCP)$/)) {
if ($pppsettings{'USERNAME'} eq '') {
$errormessage = $Lang::tr{'username not set'};
goto ERROR; }
if ($pppsettings{'PASSWORD'} eq '') {
$errormessage = $Lang::tr{'password not set'};
goto ERROR; }
}
if ($pppsettings{'TIMEOUT'} eq '') {
$errormessage = $Lang::tr{'idle timeout not set'};
goto ERROR; }
if (!($pppsettings{'TIMEOUT'} =~ /^\d+$/)) {
$errormessage = $Lang::tr{'only digits allowed in the idle timeout'};
goto ERROR; }
if ($pppsettings{'LOGINSCRIPT'} =~ /[.\/ ]/ ) {
$errormessage = $Lang::tr{'bad characters in script field'};
goto ERROR; }
if ($pppsettings{'DNS1'})
{
if (!(&General::validip($pppsettings{'DNS1'}))) {
$errormessage = $Lang::tr{'invalid primary dns'};
goto ERROR; }
}
if ($pppsettings{'DNS2'})
{
if (!(&General::validip($pppsettings{'DNS2'}))) {
$errormessage = $Lang::tr{'invalid secondary dns'};
goto ERROR; }
}
if ($pppsettings{'MAXRETRIES'} eq '') {
$errormessage = $Lang::tr{'max retries not set'};
goto ERROR; }
if (!($pppsettings{'MAXRETRIES'} =~ /^\d+$/)) {
$errormessage = $Lang::tr{'only digits allowed in max retries field'};
goto ERROR; }
if (!($pppsettings{'HOLDOFF'} =~ /^\d+$/)) {
$errormessage = $Lang::tr{'only digits allowed in holdoff field'};
goto ERROR; }
my $drivererror = 0;
if ($pppsettings{'TYPE'} =~ /^(alcatelusb|alcatelusbk)$/) {
my $modem = '';
my $speedtouch = &Header::speedtouchversion;
if ($speedtouch >=0 && $speedtouch <=4) {
if ($speedtouch ==4) { $modem='v4_b'; } else { $modem='v0123'; }
$pppsettings{'MODEM'} = $modem;
} else {
$modem='v0123';
$errormessage ="$Lang::tr{'unknown'} Rev $speedtouch";
goto ERROR;
}
if (! -e "${General::swroot}/alcatelusb/firmware.$modem.bin") {
$errormessage = $Lang::tr{'no alcatelusb firmware'};
$drivererror = 1;
goto ERROR;
}
}
if($pppsettings{'TYPE'} eq 'eciadsl' && (!(-e "${General::swroot}/eciadsl/synch.bin"))) {
$errormessage = $Lang::tr{'no eciadsl synch.bin file'};
$drivererror = 1;
goto ERROR; }
if($pppsettings{'TYPE'} eq 'fritzdsl' && (!(-e "/lib/modules/$kernel/misc/fcdslusb.o.gz"))) {
$errormessage = $Lang::tr{'no fritzdsl driver'};
$drivererror = 1;
goto ERROR; }
if( $pppsettings{'USEIBOD'} eq 'on' && $pppsettings{'COMPORT'} eq 'isdn1') {
$errormessage = $Lang::tr{'ibod for dual isdn only'};
goto ERROR; }
if ($pppsettings{'TYPE'} eq 'pptp') {
$errormessage = '';
if ($pppsettings{'METHOD'} eq 'STATIC') {
if (! &General::validip($pppsettings{'ROUTERIP'})) {
$errormessage = $Lang::tr{'router ip'}.' '.$Lang::tr{'invalid ip'};
}
} else {
if (($pppsettings{'DHCP_HOSTNAME'} ne '') && (! &General::validfqdn($pppsettings{'DHCP_HOSTNAME'})) ) {
$errormessage = $errormessage.' '.$Lang::tr{'hostname'}.' '.$Lang::tr{'invalid hostname'};
}
}
if ($errormessage ne '') {goto ERROR; }
}
if ($pppsettings{'TYPE'} =~ /^(alcatelusb|alcatelusbk|amedynusbadsl|conexantusbadsl|conexantpciadsl|3cp4218usbadsl|pulsardsl|eciadsl|fritzdsl|bewanadsl|eagleusbadsl)$/) {
if ( ($pppsettings{'VPI'} eq '') || ($pppsettings{'VCI'} eq '') ) {
$errormessage = $Lang::tr{'invalid vpi vpci'};
goto ERROR; }
if ( (!($pppsettings{'VPI'} =~ /^\d+$/)) || (!($pppsettings{'VCI'} =~ /^\d+$/)) ) {
$errormessage = $Lang::tr{'invalid vpi vpci'};
goto ERROR; }
if (($pppsettings{'VPI'} eq '0') && ($pppsettings{'VCI'} eq '0')) {
$errormessage = $Lang::tr{'invalid vpi vpci'};
goto ERROR; }
if ( $pppsettings{'PROTOCOL'} eq '' ) {
$errormessage = $Lang::tr{'invalid input'};
goto ERROR; }
}
if ( ($pppsettings{'PROTOCOL'} eq 'RFC1483') && ($pppsettings{'METHOD'} eq '') && \
($pppsettings{'TYPE'} !~ /^(alcatelusb|fritzdsl)$/)) {
$errormessage = $Lang::tr{'invalid input'};
goto ERROR; }
if (($pppsettings{'PROTOCOL'} eq 'RFC1483' && $pppsettings{'METHOD'} eq 'DHCP')) {
if ($pppsettings{'DHCP_HOSTNAME'} ne '') {
if (! &General::validfqdn($pppsettings{'DHCP_HOSTNAME'})) {
$errormessage = $errormessage.' '.$Lang::tr{'hostname'}.': '.$Lang::tr{'invalid hostname'}; }
}
}
if (($pppsettings{'PROTOCOL'} eq 'RFC1483' && $pppsettings{'METHOD'} eq 'STATIC')) {
$errormessage = '';
if (! &General::validip($pppsettings{'IP'})) {
$errormessage = $Lang::tr{'static ip'}.' '.$Lang::tr{'invalid ip'}; }
if (! &General::validip($pppsettings{'GATEWAY'})) {
$errormessage = $errormessage.' '.$Lang::tr{'gateway ip'}.' '.$Lang::tr{'invalid ip'}; }
if (! &General::validmask($pppsettings{'NETMASK'})) {
$errormessage = $errormessage.' '.$Lang::tr{'netmask'}.' '.$Lang::tr{'invalid netmask'}; }
if ($pppsettings{'BROADCAST'} ne '') {
if (! &General::validip($pppsettings{'BROADCAST'})) {
$errormessage = $errormessage.' '.$Lang::tr{'broadcast'}.' '.$Lang::tr{'invalid broadcast ip'}; }
}
if( $pppsettings{'DNS'} eq 'Automatic') {
$errormessage = $Lang::tr{'invalid input'}; }
if ($errormessage ne '') {goto ERROR; }
}
if( $pppsettings{'PROTOCOL'} eq 'RFC1483' && $pppsettings{'METHOD'} ne 'PPPOE' && \
$pppsettings{'RECONNECTION'} eq 'dialondemand' ) {
$errormessage = $Lang::tr{'invalid input'};
goto ERROR; }
if( $pppsettings{'RECONNECTION'} eq 'dialondemand' && `/bin/cat ${General::swroot}/ddns/config` =~ /,on$/m ) {
$errormessage = $Lang::tr{'dod not compatible with ddns'};
goto ERROR; }
if ( ($pppsettings{'TYPE'} =~ /^(bewanadsl)$/) && $pppsettings{'MODEM'} eq '') {
$errormessage = $Lang::tr{'no modem selected'};
goto ERROR; }
if( $pppsettings{'PROTOCOL'} eq 'RFC1483') {
$pppsettings{'ENCAP'} = $pppsettings{'ENCAP_RFC1483'}; }
if( $pppsettings{'PROTOCOL'} eq 'RFC2364') {
$pppsettings{'ENCAP'} = $pppsettings{'ENCAP_RFC2364'}; }
delete $pppsettings{'ENCAP_RFC1483'};
delete $pppsettings{'ENCAP_RFC2364'};
ERROR:
if ($errormessage) {
$pppsettings{'VALID'} = 'no'; }
else {
$pppsettings{'VALID'} = 'yes'; }
# write cgi vars to the file.
&General::writehash("${General::swroot}/ppp/settings-$pppsettings{'PROFILE'}",
\%pppsettings);
# make link and write secret file.
&updatesettings();
&writesecrets();
&General::log("$Lang::tr{'profile saved'} $pppsettings{'PROFILENAME'}");
if ($drivererror) {
my $refresh = "";
my $title = $Lang::tr{'upload'};
&Header::openpage($title, 0, $refresh);
}
}
elsif ($pppsettings{'ACTION'} eq $Lang::tr{'select'})
{
my $profile = $pppsettings{'PROFILE'};
%temppppsettings = ();
$temppppsettings{'PROFILE'} = '';
&General::readhash("${General::swroot}/ppp/settings-$pppsettings{'PROFILE'}",
\%temppppsettings);
# make link.
&updatesettings();
# read in the new params "early" so we can write secrets.
%pppsettings = ();
&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
$pppsettings{'PROFILE'} = $profile;
&General::writehash("${General::swroot}/ppp/settings-$pppsettings{'PROFILE'}",
\%pppsettings);
&writesecrets();
&General::log("$Lang::tr{'profile made current'} $pppsettings{'PROFILENAME'}");
}
elsif ($pppsettings{'ACTION'} eq $Lang::tr{'delete'})
{
&General::log("$Lang::tr{'profile deleted'} $pppsettings{'PROFILENAME'}");
my $profile = $pppsettings{'PROFILE'};
truncate ("${General::swroot}/ppp/settings-$pppsettings{'PROFILE'}", 0);
%temppppsettings = ();
$temppppsettings{'PROFILE'} = '';
&General::readhash("${General::swroot}/ppp/settings-$pppsettings{'PROFILE'}",
\%temppppsettings);
# make link.
&updatesettings();
# read in the new params "early" so we can write secrets.
%pppsettings = ();
&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
$pppsettings{'PROFILE'} = $profile;
&initprofile;
&General::writehash("${General::swroot}/ppp/settings-$pppsettings{'PROFILE'}",
\%pppsettings);
}
else
{
# read in the current vars
%pppsettings = ();
$pppsettings{'VALID'} = '';
&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);
}
# read in the profile names into @profilenames.
my $c=0;
for ($c = 1; $c <= $maxprofiles; $c++)
{
%temppppsettings = ();
$temppppsettings{'PROFILENAME'} = $Lang::tr{'empty profile'};
&General::readhash("${General::swroot}/ppp/settings-$c", \%temppppsettings);
$profilenames[$c] = $temppppsettings{'PROFILENAME'};
}
if ($pppsettings{'VALID'} eq '')
{
if ($pppsettings{'PROFILE'} eq '') {
$pppsettings{'PROFILE'} = '1';
&initprofile();
}
}
for ($c = 1; $c <= $maxprofiles; $c++) {
$selected{'PROFILE'}{$c} = ''; }
$selected{'PROFILE'}{$pppsettings{'PROFILE'}} = "selected='selected'";
for ($c = 1; $c <= $maxprofiles; $c++) {
$selected{'BACKUPPROFILE'}{$c} = ''; }
$selected{'BACKUPPROFILE'}{$pppsettings{'BACKUPPROFILE'}} = "selected='selected'";
$selected{'TYPE'}{'modem'} = '';
$selected{'TYPE'}{'serial'} = '';
$selected{'TYPE'}{'isdn'} = '';
$selected{'TYPE'}{'pppoe'} = '';
$selected{'TYPE'}{'pptp'} = '';
$selected{'TYPE'}{'alcatelusb'} = '';
$selected{'TYPE'}{'alcatelusbk'} = '';
$selected{'TYPE'}{'pulsardsl'} = '';
$selected{'TYPE'}{'eciadsl'} = '';
$selected{'TYPE'}{'fritzdsl'} = '';
$selected{'TYPE'}{'bewanadsl'} = '';
$selected{'TYPE'}{'eagleusbadsl'} = '';
$selected{'TYPE'}{'conexantusbadsl'} = '';
$selected{'TYPE'}{'conexantpciadsl'} = '';
$selected{'TYPE'}{'amedynusbadsl'} = '';
$selected{'TYPE'}{'3cp4218usbadsl'} = '';
$selected{'TYPE'}{$pppsettings{'TYPE'}} = "selected='selected'";
$checked{'DEBUG'}{'off'} = '';
$checked{'DEBUG'}{'on'} = '';
$checked{'DEBUG'}{$pppsettings{'DEBUG'}} = "checked='checked'";
$selected{'COMPORT'}{'ttyS0'} = '';
$selected{'COMPORT'}{'ttyS1'} = '';
$selected{'COMPORT'}{'ttyS2'} = '';
$selected{'COMPORT'}{'ttyS3'} = '';
$selected{'COMPORT'}{'ttyS4'} = '';
$selected{'COMPORT'}{'ttyACM0'} = '';
$selected{'COMPORT'}{'ttyACM1'} = '';
$selected{'COMPORT'}{'ttyACM2'} = '';
$selected{'COMPORT'}{'ttyACM3'} = '';
$selected{'COMPORT'}{'isdn1'} = '';
$selected{'COMPORT'}{'isdn2'} = '';
$selected{'COMPORT'}{$pppsettings{'COMPORT'}} = "selected='selected'";
$selected{'DTERATE'}{'9600'} = '';
$selected{'DTERATE'}{'19200'} = '';
$selected{'DTERATE'}{'38400'} = '';
$selected{'DTERATE'}{'57600'} = '';
$selected{'DTERATE'}{'115200'} = '';
$selected{'DTERATE'}{'230400'} = '';
$selected{'DTERATE'}{'460800'} = '';
$selected{'DTERATE'}{$pppsettings{'DTERATE'}} = "selected='selected'";
$checked{'SPEAKER'}{'off'} = '';
$checked{'SPEAKER'}{'on'} = '';
$checked{'SPEAKER'}{$pppsettings{'SPEAKER'}} = "checked='checked'";
$selected{'DIALMODE'}{'T'} = '';
$selected{'DIALMODE'}{'P'} = '';
$selected{'DIALMODE'}{$pppsettings{'DIALMODE'}} = "selected='selected'";
$checked{'RECONNECTION'}{'manual'} = '';
$checked{'RECONNECTION'}{'persistent'} = '';
$checked{'RECONNECTION'}{'dialondemand'} = '';
$checked{'RECONNECTION'}{$pppsettings{'RECONNECTION'}} = "checked='checked'";
$checked{'DIALONDEMANDDNS'}{'off'} = '';
$checked{'DIALONDEMANDDNS'}{'on'} = '';
$checked{'DIALONDEMANDDNS'}{$pppsettings{'DIALONDEMANDDNS'}} = "checked='checked'";
$checked{'AUTOCONNECT'}{'off'} = '';
$checked{'AUTOCONNECT'}{'on'} = '';
$checked{'AUTOCONNECT'}{$pppsettings{'AUTOCONNECT'}} = "checked='checked'";
$checked{'SENDCR'}{'off'} = '';
$checked{'SENDCR'}{'on'} = '';
$checked{'SENDCR'}{$pppsettings{'SENDCR'}} = "checked='checked'";
$checked{'USEDOV'}{'off'} = '';
$checked{'USEDOV'}{'on'} = '';
$checked{'USEDOV'}{$pppsettings{'USEDOV'}} = "checked='checked'";
$checked{'USEIBOD'}{'off'} = '';
$checked{'USEIBOD'}{'on'} = '';
$checked{'USEIBOD'}{$pppsettings{'USEIBOD'}} = "checked='checked'";
$checked{'MODEM'}{'PCIST'} = '';
$checked{'MODEM'}{'USB'} = '';
$checked{'MODEM'}{$pppsettings{'MODEM'}} = "checked='checked'";
$selected{'LINE'}{'WO'} = '';
$selected{'LINE'}{'ES'} = '';
$selected{'LINE'}{'ES03'} = '';
$selected{'LINE'}{'FR'} = '';
$selected{'LINE'}{'FR04'} = '';
$selected{'LINE'}{'FR10'} = '';
$selected{'LINE'}{'IT'} = '';
$selected{'LINE'}{$pppsettings{'LINE'}} = "selected='selected'";
$checked{'MODULATION'}{'GDMT'} = '';
$checked{'MODULATION'}{'ANSI'} = '';
$checked{'MODULATION'}{'GLITE'} = '';
$checked{'MODULATION'}{'AUTO'} = '';
$checked{'MODULATION'}{$pppsettings{'MODULATION'}} = "checked='checked'";
$checked{'PROTOCOL'}{'RFC1483'} = '';
$checked{'PROTOCOL'}{'RFC2364'} = '';
$checked{'PROTOCOL'}{$pppsettings{'PROTOCOL'}} = "checked='checked'";
$selected{'ENCAP'}{'0'} = '';
$selected{'ENCAP'}{'1'} = '';
$selected{'ENCAP'}{'2'} = '';
$selected{'ENCAP'}{'3'} = '';
$selected{'ENCAP'}{'4'} = '';
$selected{'ENCAP'}{$pppsettings{'ENCAP'}} = "selected='selected'";
$checked{'METHOD'}{'STATIC'} = '';
$checked{'METHOD'}{'PPPOE'} = '';
$checked{'METHOD'}{'PPPOE_PLUGIN'} = '';
$checked{'METHOD'}{'DHCP'} = '';
$checked{'METHOD'}{$pppsettings{'METHOD'}} = "checked='checked'";
$selected{'AUTH'}{'pap-or-chap'} = '';
$selected{'AUTH'}{'pap'} = '';
$selected{'AUTH'}{'chap'} = '';
$selected{'AUTH'}{'standard-login-script'} = '';
$selected{'AUTH'}{'demon-login-script'} = '';
$selected{'AUTH'}{'other-login-script'} = '';
$selected{'AUTH'}{$pppsettings{'AUTH'}} = "selected='selected'";
$checked{'DNS'}{'Automatic'} = '';
$checked{'DNS'}{'Manual'} = '';
$checked{'DNS'}{$pppsettings{'DNS'}} = "checked='checked'";
&Header::openpage($Lang::tr{'ppp setup'}, 1, '');
&Header::openbigbox('100%', 'left', '', $errormessage);
if ($errormessage) {
&Header::openbox('100%', 'center', $Lang::tr{'error messages'});
print "