Skip to content

Commit 8e3cab7

Browse files
Fix issue with v-change-sys-api + error reporting hestia pma-sso (hestiacp#2105)
* Fix an bug where $RELEASE_BRANCH is not hosted at hestia github Download of api will fail and api will not work. No error is provided * Disable autoreload except taskmonitor * Improve phpmyadmin-sso by including debuggin information * Update change log / refresh hestia-sso.php Co-authored-by: Raphael Schneeberger <rs@scit.ch>
1 parent ef37bb1 commit 8e3cab7

File tree

5 files changed

+97
-70
lines changed

5 files changed

+97
-70
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file.
99
### Bugfixes
1010
- Improve the hostname check to prevent invalid hostnames or the use of an ip address (RFC1178).
1111
- Add small wait for /usr/bin/iptables-restore [Forum](https://forum.hestiacp.com/t/clean-install-arm64-does-not-start-after-reboot-v-start-service-iptables/4395/7)
12+
- Fix bug in v-change-sys-api. When using v-change-sys-api remove and then v-change-sys-api enable + custom release branch the resetting of api failed + no "error" output was producted
13+
- Improve error reporting pma-sso function
1214
- Fixed 2104 v-change-web-domain-name unable to start webserver
1315

1416
## [1.4.12] - Service release

bin/v-change-sys-api

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ check_hestia_demo_mode
3737
if [ "$status" = "enable" ]; then
3838
if [ ! -f "$HESTIA/web/api/index.php" ]; then
3939
wget -q https://raw.githubusercontent.com/hestiacp/hestiacp/$RELEASE_BRANCH/web/api/index.php -O $HESTIA/web/api/index.php
40-
check_api_download=$(cat $HESTIA/web/api/index.php)
41-
if [ -z "$HESTIA/web/api/index.php" ]; then
42-
# Throw error message to user
43-
echo "ERROR: API installation failed."
44-
# Remove empty file created by wget output
45-
rm -f "$HESTIA/web/api/index.php"
46-
exit 1
40+
if [ ! -s $HESTIA/web/api/index.php ]; then
41+
wget -q https://raw.githubusercontent.com/hestiacp/hestiacp/release/web/api/index.php -O $HESTIA/web/api/index.php
42+
if [ ! -s $HESTIA/web/api/index.php ]; then
43+
# Throw error message to user
44+
echo "ERROR: API installation failed."
45+
# Remove empty file created by wget output
46+
rm -f "$HESTIA/web/api/index.php"
47+
exit 1
48+
fi
4749
fi
4850
else
4951
sed -i 's|die("Error: Disabled");|//die("Error: Disabled");|g' $HESTIA/web/api/index.php

install/deb/phpmyadmin/hestia-sso.php

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
<?php
2+
23
/* Hestia way to enable support for SSO to PHPmyAdmin */
34
/* To install please run v-add-sys-pma-sso */
45

56
/* Following keys will get replaced when calling v-add-sys-pma-sso */
6-
define('PHPMYADMIN_KEY','%PHPMYADMIN_KEY%');
7-
define('API_HOST_NAME','%API_HOST_NAME%');
8-
define('API_HESTIA_PORT','%API_HESTIA_PORT%');
7+
define('PHPMYADMIN_KEY', '%PHPMYADMIN_KEY%');
8+
define('API_HOST_NAME', '%API_HOST_NAME%');
9+
define('API_HESTIA_PORT', '%API_HESTIA_PORT%');
910
define('API_KEY', '%API_KEY%');
1011

1112

12-
class Hestia_API {
13+
class Hestia_API
14+
{
1315
private $api_url;
14-
function __construct(){
16+
public function __construct()
17+
{
1518
$this -> hostname = 'https://' . API_HOST_NAME . ':' . API_HESTIA_PORT .'/api/';
1619
$this -> key = API_KEY;
17-
$this -> pma_key = PHPMYADMIN_KEY;
20+
$this -> pma_key = PHPMYADMIN_KEY;
1821
}
19-
22+
2023
/* Creates curl request */
21-
function request($postvars){
24+
public function request($postvars)
25+
{
2226
$postdata = http_build_query($postvars);
2327
$curl = curl_init();
2428
curl_setopt($curl, CURLOPT_URL, $this -> hostname);
@@ -30,9 +34,10 @@ function request($postvars){
3034
$answer = curl_exec($curl);
3135
return $answer;
3236
}
33-
37+
3438
/* Creates an new temp user in mysql */
35-
function create_temp_user ($database, $user, $host){
39+
public function create_temp_user($database, $user, $host)
40+
{
3641
$post_request = array(
3742
'hash' => $this -> key,
3843
'returncode' => 'no',
@@ -44,16 +49,17 @@ function create_temp_user ($database, $user, $host){
4449
);
4550
$request = $this -> request($post_request);
4651
$json = json_decode($request);
47-
if(json_last_error() == JSON_ERROR_NONE){
52+
if (json_last_error() == JSON_ERROR_NONE) {
4853
return $json;
49-
}else{
54+
} else {
55+
trigger_error('Unable to connect over API please check api connection', E_USER_WARNING);
5056
return false;
5157
}
52-
5358
}
54-
59+
5560
/* Delete an new temp user in mysql */
56-
function delete_temp_user ($database, $user, $dbuser, $host){
61+
public function delete_temp_user($database, $user, $dbuser, $host)
62+
{
5763
$post_request = array(
5864
'hash' => $this -> key,
5965
'returncode' => 'yes',
@@ -65,48 +71,49 @@ function delete_temp_user ($database, $user, $dbuser, $host){
6571
'arg5' => $host
6672
);
6773
$request = $this -> request($post_request);
68-
if(is_numeric($request) && $request == 0){
74+
if (is_numeric($request) && $request == 0) {
6975
return true;
70-
}else{
76+
} else {
7177
return false;
7278
}
7379
}
7480

75-
function get_user_ip(){
81+
public function get_user_ip()
82+
{
7683
// Saving user IPs to the session for preventing session hijacking
77-
$user_combined_ip = array();
78-
if($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR']){
84+
$user_combined_ip = array();
85+
if ($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR']) {
7986
$user_combined_ip[] = $_SERVER['REMOTE_ADDR'];
8087
}
81-
if(isset($_SERVER['HTTP_CLIENT_IP'])){
88+
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
8289
$user_combined_ip .= '|'. $_SERVER['HTTP_CLIENT_IP'];
8390
}
84-
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
85-
if($_SERVER['REMOTE_ADDR'] != $_SERVER['HTTP_X_FORWARDED_FOR']){
91+
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
92+
if ($_SERVER['REMOTE_ADDR'] != $_SERVER['HTTP_X_FORWARDED_FOR']) {
8693
$user_combined_ip[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
8794
}
8895
}
89-
if(isset($_SERVER['HTTP_FORWARDED_FOR'])){
90-
if($_SERVER['REMOTE_ADDR'] != $_SERVER['HTTP_FORWARDED_FOR']){
96+
if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
97+
if ($_SERVER['REMOTE_ADDR'] != $_SERVER['HTTP_FORWARDED_FOR']) {
9198
$user_combined_ip[] = $_SERVER['HTTP_FORWARDED_FOR'];
9299
}
93100
}
94-
if(isset($_SERVER['HTTP_X_FORWARDED'])){
95-
if($_SERVER['REMOTE_ADDR'] != $_SERVER['HTTP_X_FORWARDED']){
96-
$user_combined_ip[] = $_SERVER['HTTP_X_FORWARDED'];
101+
if (isset($_SERVER['HTTP_X_FORWARDED'])) {
102+
if ($_SERVER['REMOTE_ADDR'] != $_SERVER['HTTP_X_FORWARDED']) {
103+
$user_combined_ip[] = $_SERVER['HTTP_X_FORWARDED'];
97104
}
98-
}
99-
if(isset($_SERVER['HTTP_FORWARDED'])){
100-
if($_SERVER['REMOTE_ADDR'] != $_SERVER['HTTP_FORWARDED']){
105+
}
106+
if (isset($_SERVER['HTTP_FORWARDED'])) {
107+
if ($_SERVER['REMOTE_ADDR'] != $_SERVER['HTTP_FORWARDED']) {
101108
$user_combined_ip[] = '|'. $_SERVER['HTTP_FORWARDED'];
102109
}
103110
}
104-
if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
105-
if(!empty($_SERVER['HTTP_CF_CONNECTING_IP'])){
106-
$user_combined_ip[] = $_SERVER['HTTP_CF_CONNECTING_IP'];
111+
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
112+
if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
113+
$user_combined_ip[] = $_SERVER['HTTP_CF_CONNECTING_IP'];
107114
}
108115
}
109-
return implode($user_combined_ip,'|');
116+
return implode($user_combined_ip, '|');
110117
}
111118
}
112119

@@ -117,7 +124,8 @@ function get_user_ip(){
117124
session_name($session_name);
118125
@session_start();
119126

120-
function session_invalid(){
127+
function session_invalid()
128+
{
121129
global $session_name;
122130
//delete all current sessions
123131
session_destroy();
@@ -126,52 +134,58 @@ function session_invalid(){
126134
die();
127135
}
128136
$api = new Hestia_API();
129-
if(!empty($_GET)){
130-
if(isset($_GET['logout'])){
131-
$api -> delete_temp_user($_SESSION['HESTIA_sso_database'], $_SESSION['HESTIA_sso_user'], $_SESSION['PMA_single_signon_user'], $_SESSION['HESTIA_sso_host']);
137+
if (!empty($_GET)) {
138+
if (isset($_GET['logout'])) {
139+
$api -> delete_temp_user($_SESSION['HESTIA_sso_database'], $_SESSION['HESTIA_sso_user'], $_SESSION['PMA_single_signon_user'], $_SESSION['HESTIA_sso_host']);
132140
//remove sessin
133141
session_invalid();
134142
header("Location: " . dirname($_SERVER['PHP_SELF']) . "/index.php");
135143
die();
136-
}else{
137-
if(isset($_GET['user']) && isset($_GET['hestia_token'])){
144+
} else {
145+
if (isset($_GET['user']) && isset($_GET['hestia_token'])) {
138146
$database = $_GET['database'];
139147
$user = $_GET['user'];
140148
$host = 'localhost';
141149
$token = $_GET['hestia_token'];
142150
$time = $_GET['exp'];
143-
if($time + 60 > time()){
151+
152+
if ($time + 60 > time()) {
144153
//note: Possible issues with cloudflare due to ip obfuscation
145154
$ip = $api -> get_user_ip();
146-
if(!password_verify($database.$user.$ip.$time.PHPMYADMIN_KEY,$token)){
155+
if (!password_verify($database.$user.$ip.$time.PHPMYADMIN_KEY, $token)) {
156+
trigger_error('Access denied: There is a security token mismatch '. $time, E_USER_WARNING);
157+
session_invalid();
158+
die();
147159
session_invalid();
148-
}else{
160+
} else {
149161
$id = session_id();
150-
//create a new temp user
151-
$data = $api -> create_temp_user($database,$user, $host);
152-
$_SESSION['PMA_single_signon_user'] = $data -> login -> user;
153-
$_SESSION['PMA_single_signon_password'] = $data -> login -> password ;
154-
$_SESSION['PMA_single_signon_host'] = $host;
155-
//save database / username to be used for sending logout notification.
156-
$_SESSION['HESTIA_sso_user'] = $user;
157-
$_SESSION['HESTIA_sso_database'] = $database;
158-
$_SESSION['HESTIA_sso_host'] = $host;
159-
160-
@session_write_close();
161-
setcookie($session_name, $id , 0, "/");
162-
header("Location: " . dirname($_SERVER['PHP_SELF']) . "/index.php");
162+
//create a new temp user
163+
$data = $api -> create_temp_user($database, $user, $host);
164+
if ($data) {
165+
$_SESSION['PMA_single_signon_user'] = $data -> login -> user;
166+
$_SESSION['PMA_single_signon_password'] = $data -> login -> password ;
167+
$_SESSION['PMA_single_signon_host'] = $host;
168+
//save database / username to be used for sending logout notification.
169+
$_SESSION['HESTIA_sso_user'] = $user;
170+
$_SESSION['HESTIA_sso_database'] = $database;
171+
$_SESSION['HESTIA_sso_host'] = $host;
172+
173+
@session_write_close();
174+
setcookie($session_name, $id, 0, "/");
175+
header("Location: " . dirname($_SERVER['PHP_SELF']) . "/index.php");
176+
} else {
177+
session_invalid();
178+
}
163179
die();
164180
}
165-
}else{
181+
} else {
182+
trigger_error('Link has been expired: System time: '. time() .' / Time provided in link: '. $time, E_USER_WARNING);
166183
session_invalid();
167-
header("Location: " . dirname($_SERVER['PHP_SELF']) . "/index.php");
168184
die();
169185
}
170186
}
171187
}
172-
}else{
188+
} else {
173189
session_invalid();
174-
header("Location: " . dirname($_SERVER['PHP_SELF']) . "/index.php");
175190
die();
176191
}
177-
?>

install/upgrade/versions/1.4.13.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ fi
2828
# Empty $HESTIA/ssl/mail/ due to bug in #2066
2929
if [ -e "$HESTIA/ssl/mail/" ]; then
3030
rm -fr $HESTIA/ssl/mail/*
31+
fi
32+
33+
# Reset PMA SSO
34+
if [ "$PHPMYADMIN_KEY" != "" ]; then
35+
echo "[ * ] Refressh hestia-sso for PMA..."
36+
$BIN/v-delete-sys-pma-sso
37+
$BIN/v-add-sys-pma-sso
3138
fi

web/js/events.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ var reloadFunction = '';
215215

216216
$(document).ready(startTime);
217217
function startTime(){
218-
reloadFunction = setInterval(updateInterval, 100);
218+
if ($(".spinner")[0]){
219+
reloadFunction = setInterval(updateInterval, 100);
220+
}
219221
}
220222

221223
function updateInterval(){

0 commit comments

Comments
 (0)