index.php 3.8 KB

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