Prepend .ovpn filename to client + login

This commit is contained in:
billz 2021-02-06 11:03:30 +00:00
parent fd62520355
commit dc03d9ea00
2 changed files with 48 additions and 3 deletions

View file

@ -224,6 +224,43 @@ function safefilerewrite($fileName, $dataToSave)
} }
} }
/**
* Prepends data to a file if not exists
*
* @param string $filename
* @param string $dataToSave
* @return boolean
*/
function file_prepend_data($filename, $dataToSave)
{
$context = stream_context_create();
$file = fopen($filename, 'r', 1, $context);
$file_data = readfile($file);
if (!preg_match('/^'.$dataToSave.'/', $file_data)) {
$tmp_file = tempnam(sys_get_temp_dir(), 'php_prepend_');
file_put_contents($tmp_file, $dataToSave);
file_put_contents($tmp_file, $file, FILE_APPEND);
fclose($file);
unlink($filename);
rename($tmp_file, $filename);
return true;
} else {
return false;
}
}
/**
* Callback function for array_filter
*
* @param string $var
* @return filtered value
*/
function filter_comments($var)
{
return $var[0] != '#';
}
/** /**
* Saves a CSRF token in the session * Saves a CSRF token in the session
*/ */

View file

@ -47,8 +47,9 @@ function DisplayOpenVPNConfig()
// parse client auth credentials // parse client auth credentials
if (!empty($auth)) { if (!empty($auth)) {
$authUser = $auth[0]; $auth = array_filter($auth, 'filter_comments');
$authPassword = $auth[1]; $authUser = current($auth);
$authPassword = next($auth);
} }
echo renderTemplate( echo renderTemplate(
@ -136,18 +137,25 @@ function SaveOpenVPNConfig($status, $file, $authUser, $authPassword)
) { ) {
throw new RuntimeException('Unable to move uploaded file'); throw new RuntimeException('Unable to move uploaded file');
} }
// Good file upload, update auth credentials if present // Good file upload, update auth credentials if present
$prepend = '# filename '.pathinfo($file['name'], PATHINFO_FILENAME) .PHP_EOL;
if (!empty($authUser) && !empty($authPassword)) { if (!empty($authUser) && !empty($authPassword)) {
$auth_flag = 1; $auth_flag = 1;
// Move tmp authdata to /etc/openvpn/login.conf // Move tmp authdata to /etc/openvpn/login.conf
$auth = $authUser .PHP_EOL . $authPassword .PHP_EOL; $auth.= $authUser .PHP_EOL . $authPassword .PHP_EOL;
file_put_contents($tmp_authdata, $auth); file_put_contents($tmp_authdata, $auth);
file_prepend_data($tmp_authdata, $prepend);
system("sudo cp $tmp_authdata " . RASPI_OPENVPN_CLIENT_LOGIN, $return); system("sudo cp $tmp_authdata " . RASPI_OPENVPN_CLIENT_LOGIN, $return);
if ($return !=0) { if ($return !=0) {
$status->addMessage('Unable to save client auth credentials', 'danger'); $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 // Set iptables rules and, optionally, auth-user-pass
exec("sudo /etc/raspap/openvpn/configauth.sh $tmp_ovpnclient $auth_flag " .$_SESSION['ap_interface'], $return); exec("sudo /etc/raspap/openvpn/configauth.sh $tmp_ovpnclient $auth_flag " .$_SESSION['ap_interface'], $return);
foreach ($return as $line) { foreach ($return as $line) {