index.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // Init
  3. define('NO_AUTH_REQUIRED',true);
  4. define('NO_AUTH_REQUIRED2',true);
  5. error_reporting(NULL);
  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 (VESTA_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. //
  25. // sourceforge.net/projects/postfixadmin/
  26. // md5crypt
  27. // Action: Creates MD5 encrypted password
  28. // Call: md5crypt (string cleartextpassword)
  29. //
  30. function md5crypt ($pw, $salt="", $magic="")
  31. {
  32. $MAGIC = "$1$";
  33. if ($magic == "") $magic = $MAGIC;
  34. if ($salt == "") $salt = create_salt ();
  35. $slist = explode ("$", $salt);
  36. if ($slist[0] == "1") $salt = $slist[1];
  37. $salt = substr ($salt, 0, 8);
  38. $ctx = $pw . $magic . $salt;
  39. $final = hex2bin (md5 ($pw . $salt . $pw));
  40. for ($i=strlen ($pw); $i>0; $i-=16)
  41. {
  42. if ($i > 16)
  43. {
  44. $ctx .= substr ($final,0,16);
  45. }
  46. else
  47. {
  48. $ctx .= substr ($final,0,$i);
  49. }
  50. }
  51. $i = strlen ($pw);
  52. while ($i > 0)
  53. {
  54. if ($i & 1) $ctx .= chr (0);
  55. else $ctx .= $pw[0];
  56. $i = $i >> 1;
  57. }
  58. $final = hex2bin (md5 ($ctx));
  59. for ($i=0;$i<1000;$i++)
  60. {
  61. $ctx1 = "";
  62. if ($i & 1)
  63. {
  64. $ctx1 .= $pw;
  65. }
  66. else
  67. {
  68. $ctx1 .= substr ($final,0,16);
  69. }
  70. if ($i % 3) $ctx1 .= $salt;
  71. if ($i % 7) $ctx1 .= $pw;
  72. if ($i & 1)
  73. {
  74. $ctx1 .= substr ($final,0,16);
  75. }
  76. else
  77. {
  78. $ctx1 .= $pw;
  79. }
  80. $final = hex2bin (md5 ($ctx1));
  81. }
  82. $passwd = "";
  83. $passwd .= to64 (((ord ($final[0]) << 16) | (ord ($final[6]) << 8) | (ord ($final[12]))), 4);
  84. $passwd .= to64 (((ord ($final[1]) << 16) | (ord ($final[7]) << 8) | (ord ($final[13]))), 4);
  85. $passwd .= to64 (((ord ($final[2]) << 16) | (ord ($final[8]) << 8) | (ord ($final[14]))), 4);
  86. $passwd .= to64 (((ord ($final[3]) << 16) | (ord ($final[9]) << 8) | (ord ($final[15]))), 4);
  87. $passwd .= to64 (((ord ($final[4]) << 16) | (ord ($final[10]) << 8) | (ord ($final[5]))), 4);
  88. $passwd .= to64 (ord ($final[11]), 2);
  89. return "$magic$salt\$$passwd";
  90. }
  91. //
  92. // sourceforge.net/projects/postfixadmin/
  93. // to64
  94. //
  95. function to64 ($v, $n)
  96. {
  97. $ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  98. $ret = "";
  99. while (($n - 1) >= 0)
  100. {
  101. $n--;
  102. $ret .= $ITOA64[$v & 0x3f];
  103. $v = $v >> 6;
  104. }
  105. return $ret;
  106. }
  107. // Check arguments
  108. if ((!empty($_POST['email'])) && (!empty($_POST['password'])) && (!empty($_POST['new']))) {
  109. list($v_account, $v_domain) = explode('@', $_POST['email']);
  110. $v_domain = escapeshellarg($v_domain);
  111. $v_account = escapeshellarg($v_account);
  112. $v_password = $_POST['password'];
  113. // Get domain owner
  114. exec (VESTA_CMD."v-search-domain-owner ".$v_domain." mail", $output, $return_var);
  115. if (($return_var == 0) && (!empty($output[0]))) {
  116. $v_user = escapeshellarg($output[0]);
  117. }
  118. unset($output);
  119. // Get current md5 hash
  120. if (!empty($v_user)) {
  121. exec (VESTA_CMD."v-get-mail-account-value ".$v_user." ".$v_domain." ".$v_account." md5", $output, $return_var);
  122. if ($return_var == 0) {
  123. $v_hash = $output[0];
  124. }
  125. }
  126. unset($output);
  127. // Compare hashes
  128. if (!empty($v_hash)) {
  129. $salt = explode('$', $v_hash);
  130. $n_hash = md5crypt($v_password, $salt[2]);
  131. $n_hash = '{MD5}'.$n_hash;
  132. // Change password
  133. if ( hash_equals($v_hash, $n_hash ) ) {
  134. $v_new_password = tempnam("/tmp","vst");
  135. $fp = fopen($v_new_password, "w");
  136. fwrite($fp, $_POST['new']."\n");
  137. fclose($fp);
  138. exec (VESTA_CMD."v-change-mail-account-password ".$v_user." ".$v_domain." ".$v_account." ".$v_new_password, $output, $return_var);
  139. if ($return_var == 0) {
  140. echo "==ok==";
  141. exit;
  142. }
  143. }
  144. }
  145. }
  146. echo 'error';
  147. exit;