index.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // Init
  3. define('NO_AUTH_REQUIRED',true);
  4. define('NO_AUTH_REQUIRED2',true);
  5. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  6. // Checking IP of incoming connection, checking is it NAT address
  7. $ok=0;
  8. $ip=$_SERVER['REMOTE_ADDR'];
  9. exec (HESTIA_CMD."v-list-sys-ips json", $output, $return_var);
  10. $output=implode('', $output);
  11. $arr=json_decode($output, true);
  12. foreach ($arr as $arr_key => $arr_val) {
  13. // search for NAT IPs and allow them
  14. if ($ip==$arr_key || $ip==$arr_val['NAT']) {
  15. $ok=1;
  16. break;
  17. }
  18. }
  19. if ($ip == $_SERVER['SERVER_ADDR']) $ok=1;
  20. if ($ip == '127.0.0.1') $ok=1;
  21. if ($ok==0) exit;
  22. if (isset($_SERVER['HTTP_X_REAL_IP']) || isset($_SERVER['HTTP_X_FORWARDED_FOR'])) exit;
  23. //
  24. // sourceforge.net/projects/postfixadmin/
  25. // md5crypt
  26. // Action: Creates MD5 encrypted password
  27. // Call: md5crypt (string cleartextpassword)
  28. //
  29. function md5crypt ($pw, $salt="", $magic="")
  30. {
  31. $MAGIC = "$1$";
  32. if ($magic == "") $magic = $MAGIC;
  33. if ($salt == "") $salt = create_salt ();
  34. $slist = explode ("$", $salt);
  35. if ($slist[0] == "1") $salt = $slist[1];
  36. $salt = substr ($salt, 0, 8);
  37. $ctx = $pw . $magic . $salt;
  38. $final = hex2bin (md5 ($pw . $salt . $pw));
  39. for ($i=strlen ($pw); $i>0; $i-=16)
  40. {
  41. if ($i > 16)
  42. {
  43. $ctx .= substr ($final,0,16);
  44. }
  45. else
  46. {
  47. $ctx .= substr ($final,0,$i);
  48. }
  49. }
  50. $i = strlen ($pw);
  51. while ($i > 0)
  52. {
  53. if ($i & 1) $ctx .= chr (0);
  54. else $ctx .= $pw[0];
  55. $i = $i >> 1;
  56. }
  57. $final = hex2bin (md5 ($ctx));
  58. for ($i=0;$i<1000;$i++)
  59. {
  60. $ctx1 = "";
  61. if ($i & 1)
  62. {
  63. $ctx1 .= $pw;
  64. }
  65. else
  66. {
  67. $ctx1 .= substr ($final,0,16);
  68. }
  69. if ($i % 3) $ctx1 .= $salt;
  70. if ($i % 7) $ctx1 .= $pw;
  71. if ($i & 1)
  72. {
  73. $ctx1 .= substr ($final,0,16);
  74. }
  75. else
  76. {
  77. $ctx1 .= $pw;
  78. }
  79. $final = hex2bin (md5 ($ctx1));
  80. }
  81. $passwd = "";
  82. $passwd .= to64 (((ord ($final[0]) << 16) | (ord ($final[6]) << 8) | (ord ($final[12]))), 4);
  83. $passwd .= to64 (((ord ($final[1]) << 16) | (ord ($final[7]) << 8) | (ord ($final[13]))), 4);
  84. $passwd .= to64 (((ord ($final[2]) << 16) | (ord ($final[8]) << 8) | (ord ($final[14]))), 4);
  85. $passwd .= to64 (((ord ($final[3]) << 16) | (ord ($final[9]) << 8) | (ord ($final[15]))), 4);
  86. $passwd .= to64 (((ord ($final[4]) << 16) | (ord ($final[10]) << 8) | (ord ($final[5]))), 4);
  87. $passwd .= to64 (ord ($final[11]), 2);
  88. return "$magic$salt\$$passwd";
  89. }
  90. //
  91. // sourceforge.net/projects/postfixadmin/
  92. // to64
  93. //
  94. function to64 ($v, $n)
  95. {
  96. $ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  97. $ret = "";
  98. while (($n - 1) >= 0)
  99. {
  100. $n--;
  101. $ret .= $ITOA64[$v & 0x3f];
  102. $v = $v >> 6;
  103. }
  104. return $ret;
  105. }
  106. // Check arguments
  107. if ((!empty($_POST['email'])) && (!empty($_POST['password'])) && (!empty($_POST['new']))) {
  108. list($v_account, $v_domain) = explode('@', $_POST['email']);
  109. $v_domain = escapeshellarg($v_domain);
  110. $v_account = escapeshellarg($v_account);
  111. $v_password = $_POST['password'];
  112. // Get domain owner
  113. exec (HESTIA_CMD."v-search-domain-owner ".$v_domain." 'mail'", $output, $return_var);
  114. if ($return_var == 0) {
  115. $v_user = $output[0];
  116. }
  117. unset($output);
  118. // Get current md5 hash
  119. if (!empty($v_user)) {
  120. exec (HESTIA_CMD."v-get-mail-account-value ".escapeshellarg($v_user)." ".$v_domain." ".$v_account." 'md5'", $output, $return_var);
  121. if ($return_var == 0) {
  122. $v_hash = $output[0];
  123. }
  124. }
  125. unset($output);
  126. // Compare hashes
  127. if (!empty($v_hash)) {
  128. $salt = explode('$', $v_hash);
  129. if($salt[0] == "{MD5}"){
  130. $n_hash = md5crypt($v_password, $salt[2]);
  131. $n_hash = '{MD5}'.$n_hash;
  132. }else{
  133. $v_password = escapeshellarg($v_password);
  134. exec("doveadm pw -s ARGON2ID -p $v_password -t '$v_hash'", $output, $return_var);
  135. if ($return_var == 0) {
  136. if (strpos($output, "(verified)") !== 0){
  137. $n_hash = $v_hash;
  138. }
  139. }
  140. }
  141. // Change password
  142. if ( $v_hash == $n_hash ) {
  143. $v_new_password = tempnam("/tmp","vst");
  144. $fp = fopen($v_new_password, "w");
  145. fwrite($fp, $_POST['new']."\n");
  146. fclose($fp);
  147. exec (HESTIA_CMD."v-change-mail-account-password ".escapeshellarg($v_user)." ".$v_domain." ".$v_account." ".$v_new_password, $output, $return_var);
  148. if ($return_var == 0) {
  149. echo "==ok==";
  150. exit;
  151. }
  152. }
  153. }
  154. }
  155. echo 'error';
  156. exit;