var name is curpw and not currentpw.
[squirrelmail.git] / plugins / change_password / backend / vmailmgrd.php
CommitLineData
ebf9211b 1<?php
2/**
3 * Change password vmailmgrd backend
4 *
5 * Backend won't work, if vmail.inc file is not included. vmail.inc file
6 * should be part of your vmailmgr install. In some cases it is included in
7 * separate package.
8 *
9 * If you use modified vmail.inc, it must provide vchpass() function that
10 * acts same way as stock (vmailmgr v.0.96.9) vmail.inc function call
11 * and other vmail.inc functions should use same $vm_tcphost and
12 * $vm_tcphost_port globals as used by stock vm_daemon_raw() function call.
13 * If you have heavily modified vmail.inc and this backend does not work
14 * correctly - recheck, if you can reproduce your problem with stock
15 * vmail.inc or adjust backend configuration for your site.
16 *
17 * Backend also needs vmailmgrd service. You can find information about
18 * installing this service in vmailmgr FAQ and vmailmgrd.html.
19 *
20 * Backend might require functions, that are available only in SquirrelMail
21 * v.1.5.1 and v.1.4.4.
22 *
a391f3af 23 * @author Tomas Kuliavas <tokul@users.sourceforge.net>
ebf9211b 24 * @version $Id$
25 * @link http://www.vmailmgr.org vmailmgr site
26 * @package plugins
27 * @subpackage change_password
28 */
29
a391f3af 30/* Default backend configuration */
ebf9211b 31
32/**
33 * path to vmail.inc
34 *
35 * This variable must provide full path to vmail.inc file including filename.
36 *
37 * WARNING: Don't disable this variable. It must be set to correct value or
38 * to empty string. If variable is missing, backend can have security problems
39 * in some php configurations.
40 * @global string $vmail_inc_path
41 */
42global $vmail_inc_path;
43$vmail_inc_path='';
44
45/**
46 * address of vmailmgrd host.
47 *
48 * Leave it empty, if you want to use unix socket
49 * global is used by vmail.inc functions
50 * @global string $vm_tcphost
51 */
52global $vm_tcphost;
53$vm_tcphost='';
54
55/**
56 * port of vmailmgrd
57 *
58 * global is used by vmail.inc functions.
59 * @global integer $vm_tcphost_port
60 */
61global $vm_tcphost_port;
62$vm_tcphost_port=322;
63
64/**
65 * Option that controls use of 8bit passwords
66 * Use of such passwords is not safe, because squirrelmail interface
67 * can be running in different charsets.
68 * @global boolean
69 */
70global $cpw_vmailmgrd_8bitpw;
71$cpw_vmailmgrd_8bitpw=false;
72
73/* end of backend configuration */
74
a391f3af 75/** load configuration from config.php */
76if ( isset($vmailmgrd) && is_array($vmailmgrd) && !empty($vmailmgrd) ) {
77 if (isset($vmailmgrd['vmail_inc_path']))
78 $vmail_inc_path=$vmailmgrd['vmail_inc_path'];
79 if (isset($vmailmgrd['vm_tcphost']))
80 $vm_tcphost=$vmailmgrd['vm_tcphost'];
81 if (isset($vmailmgrd['vm_tcphost_port']))
82 $vm_tcphost_port=$vmailmgrd['vm_tcphost_port'];
83 if (isset($vmailmgrd['cpw_vmailmgrd_8bitpw']))
84 $cpw_vmailmgrd_8bitpw=$vmailmgrd['cpw_vmailmgrd_8bitpw'];
85}
86
ebf9211b 87
88/**
89 * Init change_password plugin hooks.
90 */
91global $squirrelmail_plugin_hooks;
92$squirrelmail_plugin_hooks['change_password_dochange']['vmailmgrd'] =
93 'cpw_vmailmgrd_dochange';
94$squirrelmail_plugin_hooks['change_password_init']['vmailmgrd'] =
95 'cpw_vmailmgrd_init';
96
97
98/**
99 * Use this function to do any backend-specific initialisation,
100 * e.g. checking requirements, before the password change form
101 * is displayed to the user.
102 */
103function cpw_vmailmgrd_init(){
104 global $vmail_inc_path, $color, $username;
105
106 /**
107 * If SM_PATH isn't defined, define it. Required to include files.
108 * @ignore
109 */
110 if (!defined('SM_PATH')) {
111 define('SM_PATH','../../../');
112 }
113
114 // load error_box() function
115 include_once(SM_PATH . 'functions/display_messages.php');
116
117 if ($vmail_inc_path=='' || ! file_exists($vmail_inc_path)) {
118 // $vmail_inc_path is not set or file does not exist
119 error_box(_("Incorrent path to vmail.inc file."),$color);
120 // close html and stop script execution
121 echo "</body></html>\n";
122 exit();
123 }
124
125 include_once($vmail_inc_path);
126
127 if (! function_exists('vchpass')) {
128 // included vmail.inc does not have required functions.
129 error_box(_("Invalid or corrupted vmail.inc file."),$color);
130 // close html and stop script execution
131 echo "</body></html>\n";
132 exit();
133 }
134
135 if (! preg_match("/(.*)\@(.*)/", $username)) {
136 // username does not match vmailmgr syntax
137 error_box(_("Invalid user."),$color);
138 // close html and stop script execution
139 echo "</body></html>\n";
140 exit();
141 }
142}
143
144
145/**
146 * function used to change password in change_password plugin hooks.
147 *
06f1fc3a 148 * @param array $data The username/curpw/newpw data.
ebf9211b 149 * @return array Array of error messages.
150 */
151function cpw_vmailmgrd_dochange($data)
152{
153 global $cpw_vmailmgrd_8bitpw;
154
155 /**
156 * getting params from hook function.
157 */
158 $username = $data['username'];
159 $curpw = $data['curpw'];
160 $newpw = $data['newpw'];
161
162 $msgs = array();
163
164 // check for new 8bit password
165 if (! $cpw_vmailmgrd_8bitpw && sq_is8bit($newpw)) {
166 // 8bit chars in password when backend is configured to block them
167 array_push($msgs,CPW_INVALID_PW);
168 return $msgs;
169 }
170
171 // extract username and domain
172 if (preg_match("/(.*)\@(.*)/", $username, $parts)) {
173 $vm_user=$parts[1];
174 $vm_domain=$parts[2];
175 }
176
177 // check if old password matches
178 $vmgrd_response1 = cpw_vmailmgrd_passwd($vm_user,$vm_domain,$curpw,$curpw);
179 if ($vmgrd_response1[0]!=0) {
180 array_push($msgs, CPW_CURRENT_NOMATCH);
181 return $msgs;
182 }
183
184 // change password
185 $vmgrd_response2 = cpw_vmailmgrd_passwd($vm_user,$vm_domain,$curpw,$newpw);
186 if ($vmgrd_response2[0]!=0) {
187 // TODO: add vmail.inc error message parser.
188 array_push($msgs, $vmgrd_response2[1]);
189 }
190
191 return $msgs;
192}
193
194/**
195 * function that calls required vmail.inc functions and returns error codes.
196 *
197 * Information about vmailmgr return codes.
198 * vmailmgr functions return array with two keys.
199 * Array(
200 * [0] => error code, integer (0=no error)
201 * [1] => error message, string
202 * )
203 * @return array
204 */
205function cpw_vmailmgrd_passwd($user,$domain,$oldpass,$newpass) {
206 global $vmail_inc_path;
207
208 // variable should be checked by cpw_vmailmgrd_init function
209 include_once($vmail_inc_path);
210
211 return vchpass($domain,$oldpass,$user,$newpass);
212}
213?>