From e1909c0af3e100a9da9f911b361b91358c558b60 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 23 Mar 2024 15:03:36 +0100 Subject: [PATCH] openvpnctrl: Rewrite the entire thing This binary because a major headache as it has been changed so many times by so many people neglegting the code quality. Therefore, the logic has now been moved into initscripts and the binary changed so that it only serves as a SUID wrapper to call the initscripts. Signed-off-by: Michael Tremer --- html/cgi-bin/ovpnmain.cgi | 22 +- src/misc-progs/openvpnctrl.c | 655 ++--------------------------------- 2 files changed, 35 insertions(+), 642 deletions(-) diff --git a/html/cgi-bin/ovpnmain.cgi b/html/cgi-bin/ovpnmain.cgi index e6aafccd1..c4c3e4b94 100644 --- a/html/cgi-bin/ovpnmain.cgi +++ b/html/cgi-bin/ovpnmain.cgi @@ -936,10 +936,10 @@ sub writecollectdconf { ### if ($cgiparams{'ACTION'} eq $Lang::tr{'start ovpn server'}) { - &General::system("/usr/local/bin/openvpnctrl", "-s"); + &General::system("/usr/local/bin/openvpnctrl", "rw", "start"); } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'stop ovpn server'}) { - &General::system("/usr/local/bin/openvpnctrl", "-k"); + &General::system("/usr/local/bin/openvpnctrl", "rw", "stop"); } ### @@ -1399,8 +1399,8 @@ SETTINGS_ERROR: my $file = ''; &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash); - # Kill all N2N connections - &General::system("/usr/local/bin/openvpnctrl", "-kn2n"); + # Stop all N2N connections + &General::system("/usr/local/bin/openvpnctrl", "n2n", "stop"); foreach my $key (keys %confighash) { my $name = $confighash{$cgiparams{'$key'}}[1]; @@ -1409,7 +1409,7 @@ SETTINGS_ERROR: delete $confighash{$cgiparams{'$key'}}; } - &General::system("/usr/local/bin/openvpnctrl", "-drrd", "$name"); + &General::system("/usr/local/bin/openvpnctrl", "n2n", "delete", "$name"); } while ($file = glob("${General::swroot}/ovpn/ca/*")) { unlink $file; @@ -2165,7 +2165,7 @@ END &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash); if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){ - &General::system("/usr/local/bin/openvpnctrl", "-sn2n", "$confighash{$cgiparams{'KEY'}}[1]"); + &General::system("/usr/local/bin/openvpnctrl", "n2n", "start", "$confighash{$cgiparams{'KEY'}}[1]"); &writecollectdconf(); } } else { @@ -2175,7 +2175,7 @@ END if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){ if ($n2nactive ne '') { - &General::system("/usr/local/bin/openvpnctrl", "-kn2n", "$confighash{$cgiparams{'KEY'}}[1]"); + &General::system("/usr/local/bin/openvpnctrl", "n2n", "stop", "$confighash{$cgiparams{'KEY'}}[1]"); &writecollectdconf(); } } @@ -2534,7 +2534,7 @@ else if ($confighash{$cgiparams{'KEY'}}[3] eq 'net') { # Stop the N2N connection before it is removed - &General::system("/usr/local/bin/openvpnctrl", "-kn2n", "$confighash{$cgiparams{'KEY'}}[1]"); + &General::system("/usr/local/bin/openvpnctrl", "n2n", "stop", "$confighash{$cgiparams{'KEY'}}[1]"); my $conffile = glob("${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]/$confighash{$cgiparams{'KEY'}}[1].conf"); my $certfile = glob("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12"); @@ -2575,7 +2575,7 @@ else # Update collectd configuration and delete all RRD files of the removed connection &writecollectdconf(); - &General::system("/usr/local/bin/openvpnctrl", "-drrd", "$confighash{$cgiparams{'KEY'}}[1]"); + &General::system("/usr/local/bin/openvpnctrl", "n2n", "delete", "$confighash{$cgiparams{'KEY'}}[1]"); delete $confighash{$cgiparams{'KEY'}}; &General::system("/usr/bin/openssl", "ca", "-gencrl", "-out", "${General::swroot}/ovpn/crls/cacrl.pem", "-config", "/usr/share/openvpn/ovpn.cnf"); @@ -4545,7 +4545,7 @@ if ($cgiparams{'TYPE'} eq 'net') { if ($cgiparams{'TYPE'} eq 'net') { if (-e "/var/run/$confighash{$key}[1]n2n.pid") { - &General::system("/usr/local/bin/openvpnctrl", "-kn2n", "$confighash{$cgiparams{'KEY'}}[1]"); + &General::system("/usr/local/bin/openvpnctrl", "n2n", "stop", "$confighash{$cgiparams{'KEY'}}[1]"); &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash); my $key = $cgiparams{'KEY'}; @@ -4559,7 +4559,7 @@ if ($cgiparams{'TYPE'} eq 'net') { $confighash{$key}[0] = 'on'; &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash); - &General::system("/usr/local/bin/openvpnctrl", "-sn2n", "$confighash{$cgiparams{'KEY'}}[1]"); + &General::system("/usr/local/bin/openvpnctrl", "n2n", "start", "$confighash{$cgiparams{'KEY'}}[1]"); } } diff --git a/src/misc-progs/openvpnctrl.c b/src/misc-progs/openvpnctrl.c index 0691c6627..cd2a9ca4d 100644 --- a/src/misc-progs/openvpnctrl.c +++ b/src/misc-progs/openvpnctrl.c @@ -1,648 +1,41 @@ -#define _DEFAULT_SOURCE -#define _XOPEN_SOURCE 500 -#include +/* This file is part of the IPFire Firewall. + * + * This program is distributed under the terms of the GNU General Public + * Licence. See the file COPYING for details. + * + */ + #include #include -#include -#include -#include -#include -#include -#include -#include -#include "setuid.h" -#include "netutil.h" -#include "libsmooth.h" - -#define noovpndebug - -// global vars - struct keyvalue *kv = NULL; - -// consts -char OVPNINPUT[STRING_SIZE] = "OVPNINPUT"; -char OVPNBLOCK[STRING_SIZE] = "OVPNBLOCK"; -char OVPNNAT[STRING_SIZE] = "OVPNNAT"; -char WRAPPERVERSION[STRING_SIZE] = "ipfire-2.2.4"; - -struct connection_struct { - char name[STRING_SIZE]; - char type[STRING_SIZE]; - char proto[STRING_SIZE]; - char status[STRING_SIZE]; - char local_subnet[STRING_SIZE]; - char transfer_subnet[STRING_SIZE]; - char role[STRING_SIZE]; - char port[STRING_SIZE]; - struct connection_struct *next; -}; - -typedef struct connection_struct connection; - -static int recursive_remove_callback(const char* fpath, const struct stat* sb, int typeflag, struct FTW* ftwbuf) { - int rv = remove(fpath); - if (rv) - perror(fpath); - - return rv; -} - -static int recursive_remove(const char* path) { - return nftw(path, recursive_remove_callback, 64, FTW_DEPTH | FTW_PHYS); -} - -void exithandler(void) -{ - if(kv) - freekeyvalues(kv); - if (ifacefile) - fclose(ifacefile); -} - -void usage(void) -{ -#ifdef ovpndebug - printf("Wrapper for OpenVPN %s-debug\n", WRAPPERVERSION); -#else - printf("Wrapper for OpenVPN %s\n", WRAPPERVERSION); -#endif - printf("openvpnctrl