index.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. define('NO_AUTH_REQUIRED', true);
  3. $TAB = 'RESET PASSWORD';
  4. // Main include
  5. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  6. if (isset($_SESSION['user'])) {
  7. header("Location: /list/user");
  8. }
  9. if ($_SESSION['POLICY_SYSTEM_PASSWORD_RESET'] == 'no') {
  10. header('Location: /login/');
  11. exit();
  12. }
  13. if ((!empty($_POST['user'])) && (empty($_POST['code']))) {
  14. // Check token
  15. verify_csrf($_POST);
  16. $v_user = escapeshellarg($_POST['user']);
  17. $user = $_POST['user'];
  18. $email = $_POST['email'];
  19. $cmd="/usr/bin/sudo /usr/local/hestia/bin/v-list-user";
  20. exec($cmd." ".$v_user." json", $output, $return_var);
  21. if ($return_var == 0) {
  22. $data = json_decode(implode('', $output), true);
  23. unset($output);
  24. exec(HESTIA_CMD . "v-get-user-value ".$v_user." RKEYEXP", $output, $return_var);
  25. $rkeyexp = json_decode(implode('', $output), true);
  26. if ($rkeyexp === null || $rkeyexp < time() - 900) {
  27. if ($email == $data[$user]['CONTACT']) {
  28. $rkey = substr(password_hash("", PASSWORD_DEFAULT), 8, 12);
  29. $hash = password_hash($rkey, PASSWORD_DEFAULT);
  30. $v_rkey = tempnam("/tmp", "vst");
  31. $fp = fopen($v_rkey, "w");
  32. fwrite($fp, $hash."\n");
  33. fclose($fp);
  34. exec(HESTIA_CMD . "v-change-user-rkey ".$v_user." ".$v_rkey."", $output, $return_var);
  35. unset($output);
  36. unlink($v_rkey);
  37. $name = $data[$user]['NAME'];
  38. $contact = $data[$user]['CONTACT'];
  39. $to = $data[$user]['CONTACT'];
  40. $subject = sprintf(_('MAIL_RESET_SUBJECT'), date("Y-m-d H:i:s"));
  41. $hostname = exec('hostname');
  42. $hostname_full = exec('hostname -f');
  43. if ($hostname.":".$_SERVER['SERVER_PORT'] == $_SERVER['HTTP_HOST']) {
  44. $check = true;
  45. $hostname_email = $hostname;
  46. }else if ($hostname_full.":".$_SERVER['SERVER_PORT'] == $_SERVER['HTTP_HOST']) {
  47. $check = true;
  48. $hostname_email = $hostname_full;
  49. }else{
  50. $check = false;
  51. $ERROR = "<a class=\"error\">"._('Invalid host domain')."</a>";
  52. }
  53. if ($check == true){
  54. $from = "noreply@".$hostname_email;
  55. $from_name = _('Hestia Control Panel');
  56. if (!empty($name)) {
  57. $mailtext = sprintf(_('GREETINGS_GORDON'), $name);
  58. } else {
  59. $mailtext = _('GREETINGS');
  60. }
  61. $mailtext .= sprintf(_('PASSWORD_RESET_REQUEST'), $_SERVER['HTTP_HOST'], $user, $rkey, $_SERVER['HTTP_HOST'], $user, $rkey);
  62. if (!empty($rkey)) {
  63. send_email($to, $subject, $mailtext, $from, $from_name, $data[$user]['NAME']);
  64. }
  65. header("Location: /reset/?action=code&user=".$_POST['user']);
  66. exit;
  67. }
  68. }
  69. } else {
  70. $ERROR = "<a class=\"error\">"._('Please wait 15 minutes before sending a new request')."</a>";
  71. }
  72. }
  73. unset($output);
  74. }
  75. if ((!empty($_POST['user'])) && (!empty($_POST['code'])) && (!empty($_POST['password']))) {
  76. // Check token
  77. verify_csrf($_POST);
  78. if ($_POST['password'] == $_POST['password_confirm']) {
  79. $v_user = escapeshellarg($_POST['user']);
  80. $user = $_POST['user'];
  81. exec(HESTIA_CMD . "v-list-user ".$v_user." json", $output, $return_var);
  82. if ($return_var == 0) {
  83. $data = json_decode(implode('', $output), true);
  84. $rkey = $data[$user]['RKEY'];
  85. if (password_verify($_POST['code'], $rkey)) {
  86. unset($output);
  87. exec(HESTIA_CMD . "v-get-user-value ".$v_user." RKEYEXP", $output, $return_var);
  88. if ($output[0] > time() - 900) {
  89. $v_password = tempnam("/tmp", "vst");
  90. $fp = fopen($v_password, "w");
  91. fwrite($fp, $_POST['password']."\n");
  92. fclose($fp);
  93. exec(HESTIA_CMD . "v-change-user-password ".$v_user." ".$v_password, $output, $return_var);
  94. unlink($v_password);
  95. if ($return_var > 0) {
  96. sleep(5);
  97. $ERROR = "<a class=\"error\">"._('An internal error occurred')."</a>";
  98. } else {
  99. $_SESSION['user'] = $_POST['user'];
  100. header("Location: /");
  101. exit;
  102. }
  103. } else {
  104. sleep(5);
  105. $ERROR = "<a class=\"error\">"._('Code has been expired')."</a>";
  106. exec(HESTIA_CMD . 'v-log-user-login ' . $v_user . ' ' . $v_ip . ' failed ' . $v_session_id . ' ' . $v_user_agent .' yes "Reset code has been expired"', $output, $return_var);
  107. }
  108. } else {
  109. sleep(5);
  110. $ERROR = "<a class=\"error\">"._('Invalid username or code')."</a>";
  111. exec(HESTIA_CMD . 'v-log-user-login ' . $v_user . ' ' . $v_ip . ' failed ' . $v_session_id . ' ' . $v_user_agent .' yes "Invalid Username or Code"', $output, $return_var);
  112. }
  113. } else {
  114. sleep(5);
  115. $ERROR = "<a class=\"error\">"._('Invalid username or code')."</a>";
  116. }
  117. } else {
  118. $ERROR = "<a class=\"error\">"._('Passwords not match')."</a>";
  119. }
  120. }
  121. if (empty($_GET['action'])) {
  122. require_once '../templates/header.html';
  123. require_once '../templates/pages/login/reset_1.html';
  124. } else {
  125. require_once '../templates/header.html';
  126. if ($_GET['action'] == 'code') {
  127. require_once '../templates/pages/login/reset_2.html';
  128. }
  129. if (($_GET['action'] == 'confirm') && (!empty($_GET['code']))) {
  130. require_once '../templates/pages/login/reset_3.html';
  131. }
  132. }