index.php 3.8 KB

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