index.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // Init
  3. define('NO_AUTH_REQUIRED',true);
  4. define('NO_AUTH_REQUIRED2',true);
  5. header('Content-Type: text/plain; charset=utf-8');
  6. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  7. // Checking IP of incoming connection, checking is it NAT address
  8. $ok=0;
  9. $ip=$_SERVER['REMOTE_ADDR'];
  10. exec (HESTIA_CMD."v-list-sys-ips json", $output, $return_var);
  11. $output=implode('', $output);
  12. $arr=json_decode($output, true);
  13. foreach ($arr as $arr_key => $arr_val) {
  14. // search for NAT IPs and allow them
  15. if ($ip==$arr_key || $ip==$arr_val['NAT']) {
  16. $ok=1;
  17. break;
  18. }
  19. }
  20. if ($ip == $_SERVER['SERVER_ADDR']) $ok=1;
  21. if ($ip == '127.0.0.1') $ok=1;
  22. if ($ok==0) exit;
  23. if (isset($_SERVER['HTTP_X_REAL_IP']) || isset($_SERVER['HTTP_X_FORWARDED_FOR'])) exit;
  24. // Check arguments
  25. if (empty($_POST['email'])) {
  26. echo "error email address not provided";
  27. exit;
  28. }
  29. if (empty($_POST['password'])) {
  30. echo "error old password provided";
  31. exit;
  32. }
  33. if (empty($_POST['new'])) {
  34. echo "error new password not provided";
  35. exit;
  36. }
  37. list($v_account, $v_domain) = explode('@', $_POST['email']);
  38. $v_domain = escapeshellarg($v_domain);
  39. $v_account = escapeshellarg($v_account);
  40. $v_password = $_POST['password'];
  41. // Get domain owner
  42. exec(HESTIA_CMD . "v-search-domain-owner " . $v_domain . " 'mail'", $output, $return_var);
  43. if ($return_var != 0 || empty($output[0])) {
  44. echo "error domain owner not found";
  45. exit;
  46. }
  47. $v_user = $output[0];
  48. unset($output);
  49. // Get current password hash (called "md5" for legacy reasons, it's not guaranteed to be md5)
  50. exec(HESTIA_CMD . "v-get-mail-account-value " . escapeshellarg($v_user) . " " . $v_domain . " " . $v_account . " 'md5'", $output, $return_var);
  51. if ($return_var != 0 || empty($output[0])) {
  52. echo "error unable to get current account password hash";
  53. exit;
  54. }
  55. $v_hash = $output[0];
  56. unset($output);
  57. // v_hash use doveadm password hash format, which is basically {HASH_NAME}normal_crypt_format,
  58. // so we just need to remove the {HASH_NAME} before we can ask password_verify if its correct or not.
  59. $hash_for_password_verify = explode('}', $v_hash, 2);
  60. $hash_for_password_verify = end($hash_for_password_verify);
  61. if (!password_verify($v_password, $hash_for_password_verify)) {
  62. die("error old password does not match");
  63. }
  64. // Change password
  65. $fp = tmpfile();
  66. $new_password_file = stream_get_meta_data($fp)['uri'];
  67. fwrite($fp, $_POST['new'] . "\n");
  68. exec(HESTIA_CMD . "v-change-mail-account-password " . escapeshellarg($v_user) . " " . $v_domain . " " . $v_account . " " . escapeshellarg($new_password_file), $output, $return_var);
  69. fclose($fp);
  70. if ($return_var == 0) {
  71. echo "==ok==";
  72. exit;
  73. }
  74. echo 'error v-change-mail-account-password returned non-zero: ' . $return_var;
  75. exit;