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