index.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. define('NO_AUTH_REQUIRED',true);
  3. // Main include
  4. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  5. $TAB = 'login';
  6. // Logout
  7. if (isset($_GET['logout'])) {
  8. session_destroy();
  9. }
  10. // Login as someone else
  11. if (isset($_SESSION['user'])) {
  12. if (empty($_GET['loginas']) ){
  13. header("Location: /list/web/");
  14. exit;
  15. }
  16. if ($_SESSION['user'] == 'admin' && !empty($_GET['loginas'])) {
  17. exec (HESTIA_CMD . "v-list-user ".escapeshellarg($_GET['loginas'])." json", $output, $return_var);
  18. if ( $return_var == 0 ) {
  19. $data = json_decode(implode('', $output), true);
  20. reset($data);
  21. $_SESSION['look'] = key($data);
  22. $_SESSION['look_alert'] = 'yes';
  23. }
  24. }
  25. if ($_SESSION['user'] == 'admin' && empty($_GET['loginas'])) {
  26. header("Location: /list/user/");
  27. } else {
  28. header("Location: /list/web/");
  29. }
  30. exit;
  31. }
  32. function authenticate_user($user, $password, $twofa = ''){
  33. unset($_SESSION['login']);
  34. if(isset($_SESSION['token']) && isset($_POST['token']) && $_POST['token'] == $_SESSION['token']) {
  35. $v_user = escapeshellarg($user);
  36. $v_ip = escapeshellarg($_SERVER['REMOTE_ADDR']);
  37. if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
  38. if(!empty($_SERVER['HTTP_CF_CONNECTING_IP'])){
  39. $v_ip = escapeshellarg($_SERVER['HTTP_CF_CONNECTING_IP']);
  40. }
  41. }
  42. // Get user's salt
  43. $output = '';
  44. exec (HESTIA_CMD."v-get-user-salt ".$v_user." ".$v_ip." json" , $output, $return_var);
  45. $pam = json_decode(implode('', $output), true);
  46. if ( $return_var > 0 ) {
  47. sleep(2);
  48. $error = "<a class=\"error\">"._('Invalid username or password')."</a>";
  49. return $error;
  50. } else {
  51. $salt = $pam[$user]['SALT'];
  52. $method = $pam[$user]['METHOD'];
  53. if ($method == 'md5' ) {
  54. $hash = crypt($password, '$1$'.$salt.'$');
  55. }
  56. if ($method == 'sha-512' ) {
  57. $hash = crypt($password, '$6$rounds=5000$'.$salt.'$');
  58. $hash = str_replace('$rounds=5000','',$hash);
  59. }
  60. if ($method == 'des' ) {
  61. $hash = crypt($password, $salt);
  62. }
  63. // Send hash via tmp file
  64. $v_hash = exec('mktemp -p /tmp');
  65. $fp = fopen($v_hash, "w");
  66. fwrite($fp, $hash."\n");
  67. fclose($fp);
  68. // Check user hash
  69. exec(HESTIA_CMD ."v-check-user-hash ".$v_user." ".$v_hash." ".$v_ip, $output, $return_var);
  70. unset($output);
  71. // Remove tmp file
  72. unlink($v_hash);
  73. // Check API answer
  74. if ( $return_var > 0 ) {
  75. sleep(2);
  76. $error = "<a class=\"error\">"._('Invalid username or password')."</a>";
  77. return $error;
  78. } else {
  79. // Get user speciefic parameters
  80. exec (HESTIA_CMD . "v-list-user ".$v_user." json", $output, $return_var);
  81. $data = json_decode(implode('', $output), true);
  82. unset($output);
  83. // Check if 2FA is active
  84. if ($data[$user]['TWOFA'] != '') {
  85. if(empty($twofa)){
  86. return false;
  87. }else{
  88. $v_twofa = escapeshellarg($twofa);
  89. exec(HESTIA_CMD ."v-check-user-2fa ".$v_user." ".$v_twofa, $output, $return_var);
  90. unset($output);
  91. if ( $return_var > 0 ) {
  92. sleep(2);
  93. $error = "<a class=\"error\">"._('Invalid or missing 2FA token')."</a>";
  94. $_SESSION['login']['username'] = $user;
  95. $_SESSION['login']['password'] = $password;
  96. return $error;
  97. unset($_POST['twofa']);
  98. }
  99. }
  100. }
  101. }
  102. if ($data[$user]['ROLE'] == 'admin'){
  103. exec (HESTIA_CMD . "v-list-user admin json", $output, $return_var);
  104. $data = json_decode(implode('', $output), true);
  105. unset($output);
  106. }
  107. // Define session user
  108. $_SESSION['user'] = key($data);
  109. $v_user = $_SESSION['user'];
  110. // Define language
  111. $output = '';
  112. exec (HESTIA_CMD."v-list-sys-languages json", $output, $return_var);
  113. $languages = json_decode(implode('', $output), true);
  114. if (in_array($data[$v_user]['LANGUAGE'], $languages)){
  115. $_SESSION['language'] = $data[$user]['LANGUAGE'];
  116. } else {
  117. $_SESSION['language'] = 'en';
  118. }
  119. // Regenerate session id to prevent session fixation
  120. session_regenerate_id();
  121. // Redirect request to control panel interface
  122. if (!empty($_SESSION['request_uri'])) {
  123. header("Location: ".$_SESSION['request_uri']);
  124. unset($_SESSION['request_uri']);
  125. exit;
  126. } else {
  127. if ($user == 'admin') {
  128. header("Location: /list/user/");
  129. } else {
  130. header("Location: /list/web/");
  131. }
  132. exit;
  133. }
  134. }
  135. }
  136. } else {
  137. unset($_POST);
  138. unset($_GET);
  139. unset($_SESSION);
  140. session_destroy();
  141. session_start();
  142. return false;
  143. }
  144. }
  145. if (!empty($_SESSION['login']['username']) && !empty($_SESSION['login']['password']) && !empty($_POST['twofa'])){
  146. $error = authenticate_user($_SESSION['login']['username'], $_SESSION['login']['password'], $_POST['twofa']);
  147. unset($_POST);
  148. } else if (!empty($_POST['user']) && !empty($_POST['password'])) {
  149. $error = authenticate_user($_POST['user'], $_POST['password']);
  150. unset($_POST);
  151. }else{
  152. unset($_SESSION['login']);
  153. }
  154. // Check system configuration
  155. load_hestia_config();
  156. // Detect language
  157. if (empty($_SESSION['language'])) {
  158. $output = '';
  159. exec (HESTIA_CMD."v-list-sys-config json", $output, $return_var);
  160. $config = json_decode(implode('', $output), true);
  161. $lang = $config['config']['LANGUAGE'];
  162. $output = '';
  163. exec (HESTIA_CMD."v-list-sys-languages json", $output, $return_var);
  164. $languages = json_decode(implode('', $output), true);
  165. if(in_array($lang, $languages)){
  166. $_SESSION['language'] = $lang;
  167. }
  168. else {
  169. $_SESSION['language'] = 'en';
  170. }
  171. }
  172. // Generate CSRF token
  173. $_SESSION['token'] = md5(uniqid(mt_rand(), true));
  174. require_once('../templates/header.html');
  175. if(!empty($_SESSION['login'])){
  176. require_once('../templates/login_2.html');
  177. }else if (empty($_POST['user'])) {
  178. if($_SESSION['LOGIN_STYLE'] == 'old'){
  179. require_once('../templates/login_a.html');
  180. }else{
  181. require_once('../templates/login.html');
  182. }
  183. }else if (empty($_POST['password'])) {
  184. require_once('../templates/login_1.html');
  185. }else{
  186. if($_SESSION['LOGIN_STYLE'] == 'old'){
  187. require_once('../templates/login_a.html');
  188. }else{
  189. require_once('../templates/login.html');
  190. }
  191. }
  192. ?>