2019-11-12 17:03:26 +01:00
|
|
|
<?php
|
|
|
|
|
2020-02-15 18:57:46 +01:00
|
|
|
require_once 'includes/status_messages.php';
|
2020-06-07 18:17:16 +02:00
|
|
|
require_once 'includes/config.php';
|
|
|
|
require_once 'includes/wifi_functions.php';
|
2021-07-04 00:03:14 +02:00
|
|
|
require_once 'app/lib/uploader.php';
|
2020-06-07 18:17:16 +02:00
|
|
|
|
|
|
|
getWifiInterface();
|
2019-11-12 17:03:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage OpenVPN configuration
|
|
|
|
*/
|
|
|
|
function DisplayOpenVPNConfig()
|
|
|
|
{
|
2019-11-16 11:10:25 +01:00
|
|
|
$status = new StatusMessages();
|
2020-02-14 03:38:46 +01:00
|
|
|
if (!RASPI_MONITOR_ENABLED) {
|
|
|
|
if (isset($_POST['SaveOpenVPNSettings'])) {
|
|
|
|
if (isset($_POST['authUser'])) {
|
|
|
|
$authUser = strip_tags(trim($_POST['authUser']));
|
|
|
|
}
|
|
|
|
if (isset($_POST['authPassword'])) {
|
|
|
|
$authPassword = strip_tags(trim($_POST['authPassword']));
|
|
|
|
}
|
2021-02-09 22:57:15 +01:00
|
|
|
if (is_uploaded_file( $_FILES["customFile"]["tmp_name"])) {
|
|
|
|
$return = SaveOpenVPNConfig($status, $_FILES['customFile'], $authUser, $authPassword);
|
|
|
|
}
|
2020-02-14 03:38:46 +01:00
|
|
|
} elseif (isset($_POST['StartOpenVPN'])) {
|
|
|
|
$status->addMessage('Attempting to start OpenVPN', 'info');
|
|
|
|
exec('sudo /bin/systemctl start openvpn-client@client', $return);
|
2020-03-15 12:02:20 +01:00
|
|
|
exec('sudo /bin/systemctl enable openvpn-client@client', $return);
|
2020-02-14 03:38:46 +01:00
|
|
|
foreach ($return as $line) {
|
|
|
|
$status->addMessage($line, 'info');
|
|
|
|
}
|
|
|
|
} elseif (isset($_POST['StopOpenVPN'])) {
|
|
|
|
$status->addMessage('Attempting to stop OpenVPN', 'info');
|
|
|
|
exec('sudo /bin/systemctl stop openvpn-client@client', $return);
|
2020-03-15 12:02:20 +01:00
|
|
|
exec('sudo /bin/systemctl disable openvpn-client@client', $return);
|
2020-02-14 03:38:46 +01:00
|
|
|
foreach ($return as $line) {
|
|
|
|
$status->addMessage($line, 'info');
|
|
|
|
}
|
2019-11-16 11:10:25 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-12 17:03:26 +01:00
|
|
|
|
|
|
|
exec('pidof openvpn | wc -l', $openvpnstatus);
|
2019-11-16 11:10:25 +01:00
|
|
|
$serviceStatus = $openvpnstatus[0] == 0 ? "down" : "up";
|
2019-11-17 13:00:30 +01:00
|
|
|
$auth = file(RASPI_OPENVPN_CLIENT_LOGIN, FILE_IGNORE_NEW_LINES);
|
2021-07-08 00:25:23 +02:00
|
|
|
$public_ip = get_public_ip();
|
2019-11-12 17:03:26 +01:00
|
|
|
|
2019-11-16 11:10:25 +01:00
|
|
|
// parse client auth credentials
|
|
|
|
if (!empty($auth)) {
|
2021-02-06 12:03:30 +01:00
|
|
|
$auth = array_filter($auth, 'filter_comments');
|
|
|
|
$authUser = current($auth);
|
|
|
|
$authPassword = next($auth);
|
2019-11-12 17:03:26 +01:00
|
|
|
}
|
2021-06-06 21:50:20 +02:00
|
|
|
$clients = preg_grep('/_client.(conf)$/', scandir(pathinfo(RASPI_OPENVPN_CLIENT_CONFIG, PATHINFO_DIRNAME)));
|
2021-06-12 16:21:20 +02:00
|
|
|
exec("readlink ".RASPI_OPENVPN_CLIENT_CONFIG." | xargs basename", $ret);
|
|
|
|
$conf_default = empty($ret) ? "none" : $ret[0];
|
2019-11-16 11:10:25 +01:00
|
|
|
|
2021-02-10 12:27:24 +01:00
|
|
|
$logEnable = 0;
|
|
|
|
if (!empty($_POST) && !isset($_POST['log-openvpn'])) {
|
|
|
|
$logOutput = "";
|
|
|
|
$f = @fopen("/tmp/openvpn.log", "r+");
|
|
|
|
if ($f !== false) {
|
|
|
|
ftruncate($f, 0);
|
|
|
|
fclose($f);
|
|
|
|
}
|
2023-09-03 09:47:51 +02:00
|
|
|
} elseif (isset($_POST['log-openvpn']) || file_exists('/tmp/openvpn.log')) {
|
2021-02-09 22:57:15 +01:00
|
|
|
$logEnable = 1;
|
|
|
|
exec("sudo /etc/raspap/openvpn/openvpnlog.sh", $logOutput);
|
|
|
|
$logOutput = file_get_contents('/tmp/openvpn.log');
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:57:46 +01:00
|
|
|
echo renderTemplate(
|
|
|
|
"openvpn", compact(
|
|
|
|
"status",
|
|
|
|
"serviceStatus",
|
|
|
|
"openvpnstatus",
|
2021-02-09 22:57:15 +01:00
|
|
|
"logEnable",
|
|
|
|
"logOutput",
|
2020-02-15 18:57:46 +01:00
|
|
|
"public_ip",
|
|
|
|
"authUser",
|
2021-02-07 12:54:57 +01:00
|
|
|
"authPassword",
|
2021-06-12 16:21:20 +02:00
|
|
|
"clients",
|
|
|
|
"conf_default"
|
2020-02-15 18:57:46 +01:00
|
|
|
)
|
|
|
|
);
|
2019-11-12 17:03:26 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 22:05:21 +01:00
|
|
|
/**
|
2019-11-16 11:10:25 +01:00
|
|
|
* Validates uploaded .ovpn file, adds auth-user-pass and
|
|
|
|
* stores auth credentials in login.conf. Copies files from
|
|
|
|
* tmp to OpenVPN
|
|
|
|
*
|
2020-02-15 18:57:46 +01:00
|
|
|
* @param object $status
|
|
|
|
* @param object $file
|
|
|
|
* @param string $authUser
|
|
|
|
* @param string $authPassword
|
2019-11-16 11:10:25 +01:00
|
|
|
* @return object $status
|
|
|
|
*/
|
|
|
|
function SaveOpenVPNConfig($status, $file, $authUser, $authPassword)
|
2019-11-12 22:05:21 +01:00
|
|
|
{
|
2021-07-04 00:03:14 +02:00
|
|
|
define('KB', 1024);
|
|
|
|
$tmp_destdir = '/tmp/';
|
2019-11-17 19:16:14 +01:00
|
|
|
$auth_flag = 0;
|
2019-11-16 11:10:25 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
// If undefined or multiple files, treat as invalid
|
|
|
|
if (!isset($file['error']) || is_array($file['error'])) {
|
|
|
|
throw new RuntimeException('Invalid parameters');
|
2019-11-12 22:05:21 +01:00
|
|
|
}
|
2019-11-16 11:10:25 +01:00
|
|
|
|
2021-07-04 12:15:50 +02:00
|
|
|
$upload = \RaspAP\Uploader\Upload::factory('ovpn',$tmp_destdir);
|
2021-07-04 00:03:14 +02:00
|
|
|
$upload->set_max_file_size(64*KB);
|
|
|
|
$upload->set_allowed_mime_types(array('ovpn' => 'text/plain'));
|
|
|
|
$upload->file($file);
|
2019-11-12 22:05:21 +01:00
|
|
|
|
2021-07-04 00:03:14 +02:00
|
|
|
$validation = new validation;
|
|
|
|
$upload->callbacks($validation, array('check_name_length'));
|
|
|
|
$results = $upload->upload();
|
2019-11-12 22:05:21 +01:00
|
|
|
|
2021-07-04 00:03:14 +02:00
|
|
|
if (!empty($results['errors'])) {
|
|
|
|
throw new RuntimeException($results['errors'][0]);
|
2019-11-16 11:10:25 +01:00
|
|
|
}
|
2021-02-06 12:03:30 +01:00
|
|
|
|
2019-11-17 19:16:14 +01:00
|
|
|
// Good file upload, update auth credentials if present
|
|
|
|
if (!empty($authUser) && !empty($authPassword)) {
|
|
|
|
$auth_flag = 1;
|
2021-07-04 11:47:45 +02:00
|
|
|
$tmp_authdata = $tmp_destdir .'ovpn/authdata';
|
2021-07-04 12:15:50 +02:00
|
|
|
$auth = $authUser .PHP_EOL . $authPassword .PHP_EOL;
|
2019-11-17 19:16:14 +01:00
|
|
|
file_put_contents($tmp_authdata, $auth);
|
2021-02-07 12:54:57 +01:00
|
|
|
chmod($tmp_authdata, 0644);
|
2021-06-06 21:50:20 +02:00
|
|
|
$client_auth = RASPI_OPENVPN_CLIENT_PATH.pathinfo($file['name'], PATHINFO_FILENAME).'_login.conf';
|
2021-07-04 11:47:45 +02:00
|
|
|
system("sudo mv $tmp_authdata $client_auth", $return);
|
2021-06-06 21:50:20 +02:00
|
|
|
system("sudo rm ".RASPI_OPENVPN_CLIENT_LOGIN, $return);
|
|
|
|
system("sudo ln -s $client_auth ".RASPI_OPENVPN_CLIENT_LOGIN, $return);
|
2019-11-17 19:16:14 +01:00
|
|
|
if ($return !=0) {
|
|
|
|
$status->addMessage('Unable to save client auth credentials', 'danger');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set iptables rules and, optionally, auth-user-pass
|
2021-07-04 12:15:50 +02:00
|
|
|
$tmp_ovpn = $results['full_path'];
|
|
|
|
exec("sudo /etc/raspap/openvpn/configauth.sh $tmp_ovpn $auth_flag " .$_SESSION['ap_interface'], $return);
|
2019-11-16 11:10:25 +01:00
|
|
|
foreach ($return as $line) {
|
|
|
|
$status->addMessage($line, 'info');
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:47:45 +02:00
|
|
|
// Move uploaded ovpn config from /tmp and create symlink
|
2021-06-06 21:50:20 +02:00
|
|
|
$client_ovpn = RASPI_OPENVPN_CLIENT_PATH.pathinfo($file['name'], PATHINFO_FILENAME).'_client.conf';
|
2021-07-04 12:15:50 +02:00
|
|
|
chmod($tmp_ovpn, 0644);
|
2021-07-04 11:47:45 +02:00
|
|
|
system("sudo mv $tmp_ovpn $client_ovpn", $return);
|
2021-06-06 21:50:20 +02:00
|
|
|
system("sudo rm ".RASPI_OPENVPN_CLIENT_CONFIG, $return);
|
|
|
|
system("sudo ln -s $client_ovpn ".RASPI_OPENVPN_CLIENT_CONFIG, $return);
|
|
|
|
|
2019-11-16 11:10:25 +01:00
|
|
|
if ($return ==0) {
|
2019-11-17 19:16:14 +01:00
|
|
|
$status->addMessage('OpenVPN client.conf uploaded successfully', 'info');
|
2019-11-16 11:10:25 +01:00
|
|
|
} else {
|
2019-11-17 19:16:14 +01:00
|
|
|
$status->addMessage('Unable to save OpenVPN client config', 'danger');
|
2019-11-16 11:10:25 +01:00
|
|
|
}
|
2019-11-17 19:16:14 +01:00
|
|
|
|
2019-11-16 11:10:25 +01:00
|
|
|
return $status;
|
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
$status->addMessage($e->getMessage(), 'danger');
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
}
|
2021-07-04 11:47:45 +02:00
|
|
|
|