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