poppassd.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Poppassd change password backend
  4. *
  5. * @author Seth Randall <sethr at missoulafcu.org>
  6. * @copyright &copy; 2004-2007 The SquirrelMail Project Team
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. * @version $Id$
  9. * @package plugins
  10. * @subpackage change_password
  11. */
  12. /**
  13. * Config vars
  14. */
  15. /**
  16. * Set the address of the server your poppass daemon runs on.
  17. * If it's the same as your imap server, you can leave it blank
  18. */
  19. global $poppassd_server;
  20. $poppassd_server = '';
  21. /* get overrides from config.php */
  22. if (isset($cpw_poppassd['server'])) $poppassd_server=$cpw_poppassd['server'];
  23. /**
  24. * Define here the name of your password changing function.
  25. */
  26. global $squirrelmail_plugin_hooks;
  27. $squirrelmail_plugin_hooks['change_password_dochange']['poppassd'] = 'cpw_poppassd_dochange';
  28. /**
  29. * This is the function that is specific to your backend. It takes
  30. * the current password (as supplied by the user) and the desired
  31. * new password. It will return an array of messages. If everything
  32. * was successful, the array will be empty. Else, it will contain
  33. * the errormessage(s).
  34. * Constants to be used for these messages:
  35. * CPW_CURRENT_NOMATCH -> "Your current password is not correct."
  36. * CPW_INVALID_PW -> "Your new password contains invalid characters."
  37. *
  38. * @param array data The username/currentpw/newpw data.
  39. * @return array Array of error messages.
  40. */
  41. function cpw_poppassd_dochange($data) {
  42. // unfortunately, we can only pass one parameter to a hook function,
  43. // so we have to pass it as an array.
  44. $username = $data['username'];
  45. $curpw = $data['curpw'];
  46. $newpw = $data['newpw'];
  47. $msgs = array();
  48. // your code here to change the password for $username from
  49. // $currentpw into $newpw.
  50. $msgs = cpw_poppassd_go($username, $curpw, $newpw, 0);
  51. return $msgs;
  52. }
  53. function cpw_poppassd_closeport($pop_socket, &$messages, $debug = 0) {
  54. if ($debug) {
  55. array_push($messages, _("Closing Connection"));
  56. }
  57. fputs($pop_socket, "quit\r\n");
  58. fclose($pop_socket);
  59. }
  60. function cpw_poppassd_readfb($pop_socket, &$result, &$messages, $debug = 0) {
  61. $strResp = '';
  62. $result = '';
  63. if (!feof($pop_socket)) {
  64. $strResp = fgets($pop_socket, 1024);
  65. $result = substr(trim($strResp), 0, 3); // 200, 500
  66. if(!preg_match('/^[23]\d\d/', $result) || $debug) {
  67. $messages[] = "--> $strResp";
  68. }
  69. }
  70. }
  71. function cpw_poppassd_go($username, $old_pw, $new_pw, $debug = 0) {
  72. global $poppassd_server;
  73. global $imapServerAddress;
  74. /** sqimap_get_user_server() function */
  75. include_once(SM_PATH . 'functions/imap_general.php');
  76. if($poppassd_server == '') {
  77. // if poppassd address is not set, use imap server's address
  78. // make sure that setting contains address and not mapping
  79. $poppassd_server = sqimap_get_user_server($imapServerAddress,$username);
  80. }
  81. $messages = array();
  82. if ($debug) {
  83. $messages[] = _("Connecting to Password Server");
  84. }
  85. $pop_socket = fsockopen($poppassd_server, 106, $errno, $errstr);
  86. if (!$pop_socket) {
  87. $messages[] = _("ERROR") . ': ' . "$errstr ($errno)";
  88. return $messages;
  89. }
  90. cpw_poppassd_readfb($pop_socket, $result, $messages, $debug);
  91. if(!preg_match('/^2\d\d/', $result) ) {
  92. cpw_poppassd_closeport($pop_socket, $messages, $debug);
  93. return $messages;
  94. }
  95. fputs($pop_socket, "user $username\r\n");
  96. cpw_poppassd_readfb($pop_socket, $result, $messages, $debug);
  97. if(!preg_match('/^[23]\d\d/', $result) ) {
  98. cpw_poppassd_closeport($pop_socket, $messages, $debug);
  99. return $messages;
  100. }
  101. fputs($pop_socket, "pass $old_pw\r\n");
  102. cpw_poppassd_readfb($pop_socket, $result, $messages, $debug);
  103. if(!preg_match('/^[23]\d\d/', $result) ) {
  104. cpw_poppassd_closeport($pop_socket, $messages, $debug);
  105. return $messages;
  106. }
  107. fputs($pop_socket, "newpass $new_pw\r\n");
  108. cpw_poppassd_readfb($pop_socket, $result, $messages, $debug);
  109. cpw_poppassd_closeport($pop_socket, $messages, $debug);
  110. if(!preg_match('/^2\d\d/', $result) ) {
  111. return $messages;
  112. }
  113. return $messages;
  114. }