4 * Change password vmailmgrd backend
6 * Backend won't work, if vmail.inc file is not included. vmail.inc file
7 * should be part of your vmailmgr install. In some cases it is included in
10 * If you use modified vmail.inc, it must provide vchpass() function that
11 * acts same way as stock (vmailmgr v.0.96.9) vmail.inc function call
12 * and other vmail.inc functions should use same $vm_tcphost and
13 * $vm_tcphost_port globals as used by stock vm_daemon_raw() function call.
14 * If you have heavily modified vmail.inc and this backend does not work
15 * correctly - recheck, if you can reproduce your problem with stock
16 * vmail.inc or adjust backend configuration for your site.
18 * Backend also needs vmailmgrd service. You can find information about
19 * installing this service in vmailmgr FAQ and vmailmgrd.html.
21 * Backend might require functions, that are available only in SquirrelMail
22 * v.1.5.1 and v.1.4.4.
24 * @author Tomas Kuliavas <tokul at users.sourceforge.net>
25 * @copyright © 2005 The SquirrelMail Project Team
26 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
28 * @link http://www.vmailmgr.org vmailmgr site
30 * @subpackage change_password
33 /* Default backend configuration */
38 * This variable must provide full path to vmail.inc file including filename.
40 * WARNING: Don't disable this variable. It must be set to correct value or
41 * to empty string. If variable is missing, backend can have security problems
42 * in some PHP configurations.
43 * @global string $vmail_inc_path
45 global $vmail_inc_path;
49 * address of vmailmgrd host.
51 * Leave it empty, if you want to use unix socket
52 * global is used by vmail.inc functions
53 * @global string $vm_tcphost
61 * global is used by vmail.inc functions.
62 * @global integer $vm_tcphost_port
64 global $vm_tcphost_port;
68 * Option that controls use of 8bit passwords
69 * Use of such passwords is not safe, because squirrelmail interface
70 * can be running in different charsets.
73 global $cpw_vmailmgrd_8bitpw;
74 $cpw_vmailmgrd_8bitpw=false;
76 /* end of backend configuration */
78 /** load configuration from config.php */
79 if ( isset($cpw_vmailmgrd) && is_array($cpw_vmailmgrd) && !empty($cpw_vmailmgrd) ) {
80 if (isset($cpw_vmailmgrd['vmail_inc_path']))
81 $vmail_inc_path=$cpw_vmailmgrd['vmail_inc_path'];
82 if (isset($cpw_vmailmgrd['vm_tcphost']))
83 $vm_tcphost=$cpw_vmailmgrd['vm_tcphost'];
84 if (isset($cpw_vmailmgrd['vm_tcphost_port']))
85 $vm_tcphost_port=$cpw_vmailmgrd['vm_tcphost_port'];
86 if (isset($cpw_vmailmgrd['8bitpw']))
87 $cpw_vmailmgrd_8bitpw=$cpw_vmailmgrd['8bitpw'];
92 * Init change_password plugin hooks.
94 global $squirrelmail_plugin_hooks;
95 $squirrelmail_plugin_hooks['change_password_dochange']['vmailmgrd'] =
96 'cpw_vmailmgrd_dochange';
97 $squirrelmail_plugin_hooks['change_password_init']['vmailmgrd'] =
102 * Use this function to do any backend-specific initialisation,
103 * e.g. checking requirements, before the password change form
104 * is displayed to the user.
106 function cpw_vmailmgrd_init(){
107 global $vmail_inc_path, $color, $username;
110 * If SM_PATH isn't defined, define it. Required to include files.
113 if (!defined('SM_PATH')) {
114 define('SM_PATH','../../../');
117 // load error_box() function
118 include_once(SM_PATH
. 'functions/display_messages.php');
120 if ($vmail_inc_path=='' ||
! file_exists($vmail_inc_path)) {
121 // $vmail_inc_path is not set or file does not exist
122 error_box(_("Incorrent path to vmail.inc file."),$color);
123 // close html and stop script execution
124 echo "</body></html>\n";
128 include_once($vmail_inc_path);
130 if (! function_exists('vchpass')) {
131 // included vmail.inc does not have required functions.
132 error_box(_("Invalid or corrupted vmail.inc file."),$color);
133 // close html and stop script execution
134 echo "</body></html>\n";
138 if (! preg_match("/(.*)\@(.*)/", $username)) {
139 // username does not match vmailmgr syntax
140 error_box(_("Invalid user."),$color);
141 // close html and stop script execution
142 echo "</body></html>\n";
149 * function used to change password in change_password plugin hooks.
151 * @param array $data The username/curpw/newpw data.
152 * @return array Array of error messages.
154 function cpw_vmailmgrd_dochange($data)
156 global $cpw_vmailmgrd_8bitpw;
159 * getting params from hook function.
161 $username = $data['username'];
162 $curpw = $data['curpw'];
163 $newpw = $data['newpw'];
167 // check for new 8bit password
168 if (! $cpw_vmailmgrd_8bitpw && sq_is8bit($newpw)) {
169 // 8bit chars in password when backend is configured to block them
170 array_push($msgs,CPW_INVALID_PW
);
174 // extract username and domain
175 if (preg_match("/(.*)\@(.*)/", $username, $parts)) {
177 $vm_domain=$parts[2];
180 // check if old password matches
181 $vmgrd_response1 = cpw_vmailmgrd_passwd($vm_user,$vm_domain,$curpw,$curpw);
182 if ($vmgrd_response1[0]!=0) {
183 array_push($msgs, CPW_CURRENT_NOMATCH
);
188 $vmgrd_response2 = cpw_vmailmgrd_passwd($vm_user,$vm_domain,$curpw,$newpw);
189 if ($vmgrd_response2[0]!=0) {
190 // TODO: add vmail.inc error message parser.
191 array_push($msgs, cpw_i18n_vmail_response($vmgrd_response2[1]));
198 * function that calls required vmail.inc functions and returns error codes.
200 * Information about vmailmgr return codes.
201 * vmailmgr functions return array with two keys.
203 * [0] => error code, integer (0=no error)
204 * [1] => error message, string
208 function cpw_vmailmgrd_passwd($user,$domain,$oldpass,$newpass) {
209 global $vmail_inc_path;
211 // variable should be checked by cpw_vmailmgrd_init function
212 include_once($vmail_inc_path);
214 return vchpass($domain,$oldpass,$user,$newpass);
218 * Function is used to translate messages returned by vmailmgr
219 * php library and vmailmgr daemon.
220 * @param string $string vmailmrgd message.
221 * @return string translated string.
223 function cpw_i18n_vmail_response($string) {
224 if ($string=='Empty domain') {
225 // block one: vchpass responses
226 $ret = _("Empty domain");
227 } elseif ($string=='Empty domain password') {
228 $ret = _("Empty domain password");
229 } elseif ($string=='Empty username') {
230 $ret = _("Empty username");
231 } elseif ($string=='Empty new password') {
232 $ret = _("Empty new password");
234 * block is disabled in order to reduce load on translators.
235 * these error messages should be very rare.
236 } elseif ($string=='Invalid or unknown base user or domain') {
237 // block two: vmailmgr daemon strings
238 $ret = _("Invalid or unknown base user or domain");
239 } elseif ($string=='Invalid or unknown virtual user') {
240 $ret = _("Invalid or unknown virtual user");
241 } elseif ($string=='Invalid or incorrect password') {
242 $ret = _("Invalid or incorrect password");
243 } elseif ($string=='Unknown operation to stat') {
244 $ret = _("Unknown operation to stat");
245 } elseif (preg_match("/^Incorrect number of parameters to command (.+)/",$string,$match)) {
246 $ret = sprintf(_("Incorrect number of parameters to command %s"),$match[1]);
247 } elseif (preg_match("/^Invalid or unknown domain name: (.+)/",$string,$match)) {
248 $ret = sprintf(_("Invalid or unknown domain name: %s"),$match[1]);
249 } elseif ($string=='Invalid operation') {
250 $ret = _("Invalid operation");
251 } elseif (preg_match("/^Invalid or unknown base user name: (.+)/",$string,$match)) {
252 $ret = sprintf(_("Invalid or unknown base user name: %s"),$match[1]);
253 } elseif ($string=='Invalid or incorrect password') {
254 $ret = _("Invalid or incorrect password");
255 } elseif ($string=='Base user has no virtual password table') {
256 $ret = _("Base user has no virtual password table");
257 } elseif ($string=='Failed while writing initial OK response') {
258 $ret = _("Failed while writing initial OK response");
259 } elseif ($string=='Failed while writing list entry') {
260 $ret = _("Failed while writing list entry");
261 } elseif ($string=='Internal error -- userpass && !mustexist') {
262 $ret = _("Internal error -- userpass && !mustexist");
263 } elseif ($string=='Invalid or unknown base user or domain') {
264 $ret = _("Invalid or unknown base user or domain");
265 } elseif ($string=='Incorrect password') {
266 $ret = CPW_INVALID_PW;
267 } elseif ($string=='User name does not refer to a virtual user') {
268 $ret = _("User name does not refer to a virtual user");
269 } elseif ($string=='Invalid or unknown virtual user') {
270 $ret = _("Invalid or unknown virtual user");
271 } elseif ($string=='Virtual user already exists') {
272 $ret = _("Virtual user already exists");
273 } elseif ($string=='Timed out waiting for remote') {
274 $ret = _("Timed out waiting for remote");
275 } elseif ($string=='Connection to client lost') {
276 $ret = _("Connection to client lost");
277 } elseif ($string=="Couldn't decode the command string") {
278 $ret = _("Couldn't decode the command string");
279 } elseif ($string=='Empty command string') {
280 $ret = _("Empty command string");
281 } elseif ($string=='Error decoding a command parameter') {
282 $ret = _("Error decoding a command parameter");
283 } elseif ($string=='read system call failed or was interrupted') {
284 $ret = _("read system call failed or was interrupted");
285 } elseif ($string=='Short read while reading protocol header') {
286 $ret = _("Short read while reading protocol header");
287 } elseif ($string=='Invalid protocol from client') {
288 $ret = _("Invalid protocol from client");
289 } elseif ($string=='Short read while reading message data') {
290 $ret = _("Short read while reading message data");
291 } elseif ($string=='Error writing response') {
292 $ret = _("Error writing response");
295 // return unknown strings