From 95722ee11e42b3ca01ddfdd38be196a2e1888f9e Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 20 Mar 2024 12:12:00 +0100 Subject: [PATCH] ovpnmain.cgi: Refactor addccdnet() Signed-off-by: Michael Tremer --- html/cgi-bin/ovpnmain.cgi | 78 +++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/html/cgi-bin/ovpnmain.cgi b/html/cgi-bin/ovpnmain.cgi index b0a978720..555da41f9 100755 --- a/html/cgi-bin/ovpnmain.cgi +++ b/html/cgi-bin/ovpnmain.cgi @@ -408,60 +408,48 @@ sub delccdnet($) { return 0; } -sub addccdnet -{ - my %ccdconfhash=(); - my @ccdconf=(); - my $ccdname=$_[0]; - my $ccdnet=$_[1]; - my $subcidr; - my @ip2=(); - my $checkup; - my $ccdip; - my $baseaddress; - +sub addccdnet($$) { + my $name = shift; + my $network = shift; - #check name - if ($ccdname eq '') - { - $errormessage=$errormessage.$Lang::tr{'ccd err name'}."
"; - return - } + my %ccdconfhash = (); - if(!&validccdname($ccdname)) - { - $errormessage=$Lang::tr{'ccd err invalidname'}; + # Check if the name is valid + unless (&validccdname($name)) { + $errormessage = $Lang::tr{'ccd err invalidname'}; return; } - ($ccdip,$subcidr) = split (/\//,$ccdnet); - $subcidr=&General::iporsubtocidr($subcidr); - #check subnet - if ($subcidr > 30) - { - $errormessage=$Lang::tr{'ccd err invalidnet'}; + # Fetch the network address & prefix + my $address = &Network::get_netaddress($network); + my $prefix = &Network::get_prefix($network); + + # If we could not decode the subnet, it must be invalid + if (!defined $address || !defined $prefix) { + $errormessage = $Lang::tr{'ccd err invalidnet'}; return; - } - #check ip - if (!&General::validipandmask($ccdnet)){ - $errormessage=$Lang::tr{'ccd err invalidnet'}; + + # If the network is smaller than /30, there is no point in using it + } elsif ($prefix > 30) { + $errormessage = $Lang::tr{'ccd err invalidnet'}; return; } - if (!$errormessage) { - my %ccdconfhash=(); - $baseaddress=&General::getnetworkip($ccdip,$subcidr); - &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash); - my $key = &General::findhasharraykey (\%ccdconfhash); - foreach my $i (0 .. 1) { $ccdconfhash{$key}[$i] = "";} - $ccdconfhash{$key}[0] = $ccdname; - $ccdconfhash{$key}[1] = $baseaddress."/".$subcidr; - &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash); - &writeserverconf; - $cgiparams{'ccdname'}=''; - $cgiparams{'ccdsubnet'}=''; - return 1; - } + # Read the configuration + &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash); + + # Create a new entry + my $key = &General::findhasharraykey(\%ccdconfhash); + + # Store name + $ccdconfhash{$key}[0] = $name; + $ccdconfhash{$key}[1] = "$address/$prefix"; + + # Write the hash back + &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash); + + # Update the server configuration to add routes + &writeserverconf(); } sub modccdnet -- 2.39.5