From: Michael Tremer Date: Wed, 20 Mar 2024 10:33:16 +0000 (+0100) Subject: ovpnmain.cgi: Refactor function to remove a static pool X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=65b05e55acd801bf88e6423cc7314007588b4ab7;p=people%2Fms%2Fipfire-2.x.git ovpnmain.cgi: Refactor function to remove a static pool Signed-off-by: Michael Tremer --- diff --git a/html/cgi-bin/ovpnmain.cgi b/html/cgi-bin/ovpnmain.cgi index add25cb36..d041b9474 100644 --- a/html/cgi-bin/ovpnmain.cgi +++ b/html/cgi-bin/ovpnmain.cgi @@ -350,29 +350,39 @@ sub writeserverconf { close(CONF); } -sub delccdnet -{ - my %ccdconfhash = (); - my %ccdhash = (); - my $ccdnetname=$_[0]; - if (-f "${General::swroot}/ovpn/ovpnconfig"){ - &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash); - foreach my $key (keys %ccdhash) { - if ($ccdhash{$key}[32] eq $ccdnetname) { - $errormessage=$Lang::tr{'ccd err hostinnet'}; - return; - } +sub delccdnet($) { + my $name = shift; + + my %conns = (); + my %subnets = (); + + # Load all connections + &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%conns); + + # Check if the subnet is in use + foreach my $key (keys %conns) { + if ($conns{$key}[32] eq $name) { + $errormessage = $Lang::tr{'ccd err hostinnet'}; + return 1; } } - &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash); - foreach my $key (keys %ccdconfhash) { - if ($ccdconfhash{$key}[0] eq $ccdnetname){ - delete $ccdconfhash{$key}; + + # Load all subnets + &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%subnets); + + # Remove the subnet + foreach my $key (keys %subnets) { + if ($subnets{$key}[0] eq $name){ + delete $subnets{$key}; } } - &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash); - &writeserverconf; + # Write the subnets back + &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%subnets); + + # Update the server configuration to remove routes + &writeserverconf(); + return 0; }