index.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 ($_SESSION['user'] == 'admin' && !empty($_GET['loginas'])) {
  13. exec (HESTIA_CMD . "v-list-user ".escapeshellarg($_GET['loginas'])." json", $output, $return_var);
  14. if ( $return_var == 0 ) {
  15. $data = json_decode(implode('', $output), true);
  16. reset($data);
  17. $_SESSION['look'] = key($data);
  18. $_SESSION['look_alert'] = 'yes';
  19. }
  20. }
  21. if ($_SESSION['user'] == 'admin' && empty($_GET['loginas'])) {
  22. header("Location: /list/user/");
  23. } else {
  24. header("Location: /list/web/");
  25. }
  26. exit;
  27. }
  28. // Basic auth
  29. if (isset($_POST['user']) && isset($_POST['password'])) {
  30. if(isset($_SESSION['token']) && isset($_POST['token']) && $_POST['token'] == $_SESSION['token']) {
  31. $v_user = escapeshellarg($_POST['user']);
  32. $v_ip = escapeshellarg($_SERVER['REMOTE_ADDR']);
  33. if (isset($_POST['twofa'])) {
  34. $v_twofa = escapeshellarg($_POST['twofa']);
  35. }
  36. // Get user's salt
  37. $output = '';
  38. exec (HESTIA_CMD."v-get-user-salt ".$v_user." ".$v_ip." json" , $output, $return_var);
  39. $pam = json_decode(implode('', $output), true);
  40. if ( $return_var > 0 ) {
  41. $ERROR = "<a class=\"error\">".__('Invalid username or password')."</a>";
  42. } else {
  43. $user = $_POST['user'];
  44. $password = $_POST['password'];
  45. $salt = $pam[$user]['SALT'];
  46. $method = $pam[$user]['METHOD'];
  47. if ($method == 'md5' ) {
  48. $hash = crypt($password, '$1$'.$salt.'$');
  49. }
  50. if ($method == 'sha-512' ) {
  51. $hash = crypt($password, '$6$rounds=5000$'.$salt.'$');
  52. $hash = str_replace('$rounds=5000','',$hash);
  53. }
  54. if ($method == 'des' ) {
  55. $hash = crypt($password, $salt);
  56. }
  57. // Send hash via tmp file
  58. $v_hash = exec('mktemp -p /tmp');
  59. $fp = fopen($v_hash, "w");
  60. fwrite($fp, $hash."\n");
  61. fclose($fp);
  62. // Check user hash
  63. exec(HESTIA_CMD ."v-check-user-hash ".$v_user." ".$v_hash." ".$v_ip, $output, $return_var);
  64. unset($output);
  65. // Remove tmp file
  66. unlink($v_hash);
  67. // Check API answer
  68. if ( $return_var > 0 ) {
  69. $ERROR = "<a class=\"error\">".__('Invalid username or password')."</a>";
  70. } else {
  71. // Make root admin user
  72. if ($_POST['user'] == 'root') $v_user = 'admin';
  73. // Get user speciefic parameters
  74. exec (HESTIA_CMD . "v-list-user ".$v_user." json", $output, $return_var);
  75. $data = json_decode(implode('', $output), true);
  76. // Check if 2FA is active
  77. if ($data[$_POST['user']]['TWOFA'] != '') {
  78. if (isset($v_twofa)){
  79. $v_twofa = str_replace(' ', '', $v_twofa);
  80. exec(HESTIA_CMD ."v-check-user-2fa ".$v_user." ".$v_twofa, $output, $return_var);
  81. unset($output);
  82. if ( $return_var > 0 ) {
  83. $ERROR = "<a class=\"error\">".__('Invalid or missing 2FA token')."</a>";
  84. }
  85. } else {
  86. $ERROR = "<a class=\"error\">".__('Invalid or missing 2FA token')."</a>";
  87. }
  88. }
  89. // Check if 2FA was successfully
  90. if ( ! isset($v_twofa) || $ERROR == '' ) {
  91. // Define session user
  92. $_SESSION['user'] = key($data);
  93. $v_user = $_SESSION['user'];
  94. // Get user favorites
  95. get_favourites();
  96. // Define language
  97. $output = '';
  98. exec (HESTIA_CMD."v-list-sys-languages json", $output, $return_var);
  99. $languages = json_decode(implode('', $output), true);
  100. if (in_array($data[$v_user]['LANGUAGE'], $languages)){
  101. $_SESSION['language'] = $data[$v_user]['LANGUAGE'];
  102. } else {
  103. $_SESSION['language'] = 'en';
  104. }
  105. // Regenerate session id to prevent session fixation
  106. session_regenerate_id();
  107. // Redirect request to control panel interface
  108. if (!empty($_SESSION['request_uri'])) {
  109. header("Location: ".$_SESSION['request_uri']);
  110. unset($_SESSION['request_uri']);
  111. exit;
  112. } else {
  113. if ($v_user == 'admin') {
  114. header("Location: /list/user/");
  115. } else {
  116. header("Location: /list/web/");
  117. }
  118. exit;
  119. }
  120. }
  121. }
  122. }
  123. } else {
  124. $ERROR = "<a class=\"error\">".__('Invalid or missing token')."</a>";
  125. }
  126. }
  127. // Check system configuration
  128. load_hestia_config();
  129. // Detect language
  130. if (empty($_SESSION['language'])) {
  131. $output = '';
  132. exec (HESTIA_CMD."v-list-sys-config json", $output, $return_var);
  133. $config = json_decode(implode('', $output), true);
  134. $lang = $config['config']['LANGUAGE'];
  135. $output = '';
  136. exec (HESTIA_CMD."v-list-sys-languages json", $output, $return_var);
  137. $languages = json_decode(implode('', $output), true);
  138. if(in_array($lang, $languages)){
  139. $_SESSION['language'] = $lang;
  140. }
  141. else {
  142. $_SESSION['language'] = 'en';
  143. }
  144. }
  145. // Generate CSRF token
  146. $_SESSION['token'] = md5(uniqid(mt_rand(), true));
  147. require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.$_SESSION['language'].'.php');
  148. require_once('../templates/header.html');
  149. require_once('../templates/login.html');