From 1647aa3c737f182e54d6c175b6502d46349525ce Mon Sep 17 00:00:00 2001 From: billz Date: Sun, 6 Jun 2021 20:50:20 +0100 Subject: [PATCH] Refactor config handling w/ symbolic links --- includes/openvpn.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/includes/openvpn.php b/includes/openvpn.php index c5b00d1c..711c1c45 100755 --- a/includes/openvpn.php +++ b/includes/openvpn.php @@ -53,7 +53,7 @@ function DisplayOpenVPNConfig() $authUser = current($auth); $authPassword = next($auth); } - $clients = preg_grep('/client.(conf)$/', scandir(pathinfo(RASPI_OPENVPN_CLIENT_CONFIG, PATHINFO_DIRNAME))); + $clients = preg_grep('/_client.(conf)$/', scandir(pathinfo(RASPI_OPENVPN_CLIENT_CONFIG, PATHINFO_DIRNAME))); $logEnable = 0; if (!empty($_POST) && !isset($_POST['log-openvpn'])) { @@ -158,36 +158,34 @@ function SaveOpenVPNConfig($status, $file, $authUser, $authPassword) throw new RuntimeException('Unable to move uploaded file'); } - // Good file upload, update auth credentials if present - $prepend = '# filename '.pathinfo($file['name'], PATHINFO_FILENAME) .PHP_EOL; if (!empty($authUser) && !empty($authPassword)) { $auth_flag = 1; // Move tmp authdata to /etc/openvpn/login.conf $auth.= $authUser .PHP_EOL . $authPassword .PHP_EOL; file_put_contents($tmp_authdata, $auth); - file_prepend_data($tmp_authdata, $prepend); - file_move_config(RASPI_OPENVPN_CLIENT_LOGIN); chmod($tmp_authdata, 0644); - system("sudo cp $tmp_authdata " . RASPI_OPENVPN_CLIENT_LOGIN, $return); + $client_auth = RASPI_OPENVPN_CLIENT_PATH.pathinfo($file['name'], PATHINFO_FILENAME).'_login.conf'; + system("sudo cp $tmp_authdata $client_auth", $return); + system("sudo rm ".RASPI_OPENVPN_CLIENT_LOGIN, $return); + system("sudo ln -s $client_auth ".RASPI_OPENVPN_CLIENT_LOGIN, $return); if ($return !=0) { $status->addMessage('Unable to save client auth credentials', 'danger'); } } - // Prepend filname tag to .ovpn client config - file_prepend_data($tmp_ovpnclient, $prepend); - // Set iptables rules and, optionally, auth-user-pass exec("sudo /etc/raspap/openvpn/configauth.sh $tmp_ovpnclient $auth_flag " .$_SESSION['ap_interface'], $return); foreach ($return as $line) { $status->addMessage($line, 'info'); } - // Copy tmp client config to /etc/openvpn/client - file_move_config(RASPI_OPENVPN_CLIENT_CONFIG); + $client_ovpn = RASPI_OPENVPN_CLIENT_PATH.pathinfo($file['name'], PATHINFO_FILENAME).'_client.conf'; chmod($tmp_ovpnclient, 0644); - system("sudo cp $tmp_ovpnclient " . RASPI_OPENVPN_CLIENT_CONFIG, $return); + system("sudo cp $tmp_ovpnclient $client_ovpn", $return); + system("sudo rm ".RASPI_OPENVPN_CLIENT_CONFIG, $return); + system("sudo ln -s $client_ovpn ".RASPI_OPENVPN_CLIENT_CONFIG, $return); + if ($return ==0) { $status->addMessage('OpenVPN client.conf uploaded successfully', 'info'); } else {