index.php 4.2 KB

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