index.php 4.2 KB

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