When wanting to use mb_strtoupper, test for the existence of that function
[squirrelmail.git] / plugins / change_password / backend / vmailmgrd.php
CommitLineData
ebf9211b 1<?php
2/**
3 * Change password vmailmgrd backend
4 *
d04cab42 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
ebf9211b 7 * separate package.
8 *
d04cab42 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
ebf9211b 12 * $vm_tcphost_port globals as used by stock vm_daemon_raw() function call.
d04cab42 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
ebf9211b 15 * vmail.inc or adjust backend configuration for your site.
16 *
d04cab42 17 * Backend also needs vmailmgrd service. You can find information about
ebf9211b 18 * installing this service in vmailmgr FAQ and vmailmgrd.html.
19 *
d04cab42 20 * Backend might require functions, that are available only in SquirrelMail
ebf9211b 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.
d04cab42 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.
ebf9211b 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/**
d04cab42 89 * Init change_password plugin hooks.
ebf9211b 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.
47a29972 188 array_push($msgs, cpw_i18n_vmail_response($vmgrd_response2[1]));
ebf9211b 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}
47a29972 213
214/**
215 * Function is used to translate messages returned by vmailmgr
216 * php library and vmailmgr daemon.
217 * @param string $string vmailmrgd message.
218 * @return string translated string.
219 */
220function cpw_i18n_vmail_response($string) {
221 if ($string=='Empty domain') {
222 // block one: vchpass responses
223 $ret = _("Empty domain");
224 } elseif ($string=='Empty domain password') {
225 $ret = _("Empty domain password");
226 } elseif ($string=='Empty username') {
227 $ret = _("Empty username");
228 } elseif ($string=='Empty new password') {
229 $ret = _("Empty new password");
230/*
231 * block is disabled in order to reduce load on translators.
232 * these error messages should be very rare.
233 } elseif ($string=='Invalid or unknown base user or domain') {
234 // block two: vmailmgr daemon strings
235 $ret = _("Invalid or unknown base user or domain");
236 } elseif ($string=='Invalid or unknown virtual user') {
237 $ret = _("Invalid or unknown virtual user");
238 } elseif ($string=='Invalid or incorrect password') {
239 $ret = _("Invalid or incorrect password");
240 } elseif ($string=='Unknown operation to stat') {
241 $ret = _("Unknown operation to stat");
242 } elseif (preg_match("/^Incorrect number of parameters to command (.+)/",$string,$match)) {
243 $ret = sprintf(_("Incorrect number of parameters to command %s"),$match[1]);
244 } elseif (preg_match("/^Invalid or unknown domain name: (.+)/",$string,$match)) {
245 $ret = sprintf(_("Invalid or unknown domain name: %s"),$match[1]);
246 } elseif ($string=='Invalid operation') {
247 $ret = _("Invalid operation");
248 } elseif (preg_match("/^Invalid or unknown base user name: (.+)/",$string,$match)) {
249 $ret = sprintf(_("Invalid or unknown base user name: %s"),$match[1]);
250 } elseif ($string=='Invalid or incorrect password') {
251 $ret = _("Invalid or incorrect password");
252 } elseif ($string=='Base user has no virtual password table') {
253 $ret = _("Base user has no virtual password table");
254 } elseif ($string=='Failed while writing initial OK response') {
255 $ret = _("Failed while writing initial OK response");
256 } elseif ($string=='Failed while writing list entry') {
257 $ret = _("Failed while writing list entry");
258 } elseif ($string=='Internal error -- userpass && !mustexist') {
259 $ret = _("Internal error -- userpass && !mustexist");
260 } elseif ($string=='Invalid or unknown base user or domain') {
261 $ret = _("Invalid or unknown base user or domain");
262 } elseif ($string=='Incorrect password') {
263 $ret = CPW_INVALID_PW;
264 } elseif ($string=='User name does not refer to a virtual user') {
265 $ret = _("User name does not refer to a virtual user");
266 } elseif ($string=='Invalid or unknown virtual user') {
267 $ret = _("Invalid or unknown virtual user");
268 } elseif ($string=='Virtual user already exists') {
269 $ret = _("Virtual user already exists");
270 } elseif ($string=='Timed out waiting for remote') {
271 $ret = _("Timed out waiting for remote");
272 } elseif ($string=='Connection to client lost') {
273 $ret = _("Connection to client lost");
274 } elseif ($string=="Couldn't decode the command string") {
275 $ret = _("Couldn't decode the command string");
276 } elseif ($string=='Empty command string') {
277 $ret = _("Empty command string");
278 } elseif ($string=='Error decoding a command parameter') {
279 $ret = _("Error decoding a command parameter");
280 } elseif ($string=='read system call failed or was interrupted') {
281 $ret = _("read system call failed or was interrupted");
282 } elseif ($string=='Short read while reading protocol header') {
283 $ret = _("Short read while reading protocol header");
284 } elseif ($string=='Invalid protocol from client') {
285 $ret = _("Invalid protocol from client");
286 } elseif ($string=='Short read while reading message data') {
287 $ret = _("Short read while reading message data");
288 } elseif ($string=='Error writing response') {
289 $ret = _("Error writing response");
290*/
291 } else {
292 // return unknown strings
293 $ret = $string;
294 }
295 return $ret;
296}
ebf9211b 297?>