Aleksander Machniak
2012-11-26 789e5988aaebb78d368b137b98169ec1e616159c
commit | author | age
48e9c1 1 <?php
T 2
3 /*
4  +-------------------------------------------------------------------------+
5  | Password Plugin for Roundcube                                           |
204e1e 6  | @version @package_version@                                              |
48e9c1 7  |                                                                         |
T 8  | Copyright (C) 2009-2010, Roundcube Dev.                                 |
9  |                                                                         |
10  | This program is free software; you can redistribute it and/or modify    |
11  | it under the terms of the GNU General Public License version 2          |
12  | as published by the Free Software Foundation.                           |
13  |                                                                         |
14  | This program is distributed in the hope that it will be useful,         |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
17  | GNU General Public License for more details.                            |
18  |                                                                         |
19  | You should have received a copy of the GNU General Public License along |
20  | with this program; if not, write to the Free Software Foundation, Inc., |
21  | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.             |
22  |                                                                         |
23  +-------------------------------------------------------------------------+
24  | Author: Aleksander Machniak <alec@alec.pl>                              |
25  +-------------------------------------------------------------------------+
26
27  $Id: index.php 2645 2009-06-15 07:01:36Z alec $
28
29 */
30
31 define('PASSWORD_CRYPT_ERROR', 1);
32 define('PASSWORD_ERROR', 2);
33 define('PASSWORD_CONNECT_ERROR', 3);
34 define('PASSWORD_SUCCESS', 0);
35
36 /**
37  * Change password plugin
38  *
39  * Plugin that adds functionality to change a users password.
40  * It provides common functionality and user interface and supports
41  * several backends to finally update the password.
42  *
43  * For installation and configuration instructions please read the README file.
44  *
45  * @author Aleksander Machniak
46  */
47 class password extends rcube_plugin
48 {
49     public $task    = 'settings';
50     public $noframe = true;
51     public $noajax  = true;
52
53     function init()
54     {
55         $rcmail = rcmail::get_instance();
56
57         $this->load_config();
98128f 58
AM 59         // Host exceptions
60         $hosts = $rcmail->config->get('password_hosts');
61         if (!empty($hosts) && !in_array($_SESSION['storage_host'], $hosts)) {
7978f8 62             return;
N 63         }
48e9c1 64
98128f 65         // Login exceptions
48e9c1 66         if ($exceptions = $rcmail->config->get('password_login_exceptions')) {
T 67             $exceptions = array_map('trim', (array) $exceptions);
68             $exceptions = array_filter($exceptions);
69             $username   = $_SESSION['username'];
70
71             foreach ($exceptions as $ec) {
72                 if ($username === $ec) {
73                     return;
74                 }
75             }
76         }
77
78         // add Tab label
79         $rcmail->output->add_label('password');
80         $this->register_action('plugin.password', array($this, 'password_init'));
81         $this->register_action('plugin.password-save', array($this, 'password_save'));
82         $this->include_script('password.js');
83     }
84
85     function password_init()
86     {
87         $this->add_texts('localization/');
88         $this->register_handler('plugin.body', array($this, 'password_form'));
89
90         $rcmail = rcmail::get_instance();
91         $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
92         $rcmail->output->send('plugin');
93     }
94
95     function password_save()
96     {
97         $rcmail = rcmail::get_instance();
98
99         $this->add_texts('localization/');
100         $this->register_handler('plugin.body', array($this, 'password_form'));
101         $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
102
103         $confirm = $rcmail->config->get('password_confirm_current');
104         $required_length = intval($rcmail->config->get('password_minimum_length'));
105         $check_strength = $rcmail->config->get('password_require_nonalpha');
106
107         if (($confirm && !isset($_POST['_curpasswd'])) || !isset($_POST['_newpasswd'])) {
108             $rcmail->output->command('display_message', $this->gettext('nopassword'), 'error');
109         }
110         else {
111             $charset    = strtoupper($rcmail->config->get('password_charset', 'ISO-8859-1'));
112             $rc_charset = strtoupper($rcmail->output->get_charset());
113
114             $sespwd = $rcmail->decrypt($_SESSION['password']);
115             $curpwd = $confirm ? get_input_value('_curpasswd', RCUBE_INPUT_POST, true, $charset) : $sespwd;
116             $newpwd = get_input_value('_newpasswd', RCUBE_INPUT_POST, true);
117             $conpwd = get_input_value('_confpasswd', RCUBE_INPUT_POST, true);
118
119             // check allowed characters according to the configured 'password_charset' option
120             // by converting the password entered by the user to this charset and back to UTF-8
121             $orig_pwd = $newpwd;
122             $chk_pwd = rcube_charset_convert($orig_pwd, $rc_charset, $charset);
123             $chk_pwd = rcube_charset_convert($chk_pwd, $charset, $rc_charset);
124
125             // WARNING: Default password_charset is ISO-8859-1, so conversion will
126             // change national characters. This may disable possibility of using
127             // the same password in other MUA's.
128             // We're doing this for consistence with Roundcube core
129             $newpwd = rcube_charset_convert($newpwd, $rc_charset, $charset);
130             $conpwd = rcube_charset_convert($conpwd, $rc_charset, $charset);
131
132             if ($chk_pwd != $orig_pwd) {
133                 $rcmail->output->command('display_message', $this->gettext('passwordforbidden'), 'error');
134             }
135             // other passwords validity checks
136             else if ($conpwd != $newpwd) {
137                 $rcmail->output->command('display_message', $this->gettext('passwordinconsistency'), 'error');
138             }
139             else if ($confirm && $sespwd != $curpwd) {
140                 $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error');
141             }
142             else if ($required_length && strlen($newpwd) < $required_length) {
143                 $rcmail->output->command('display_message', $this->gettext(
144                     array('name' => 'passwordshort', 'vars' => array('length' => $required_length))), 'error');
145             }
146             else if ($check_strength && (!preg_match("/[0-9]/", $newpwd) || !preg_match("/[^A-Za-z0-9]/", $newpwd))) {
147                 $rcmail->output->command('display_message', $this->gettext('passwordweak'), 'error');
148             }
149             // password is the same as the old one, do nothing, return success
150             else if ($sespwd == $newpwd) {
151                 $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
152             }
153             // try to save the password
154             else if (!($res = $this->_save($curpwd, $newpwd))) {
155                 $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
156
157                 // allow additional actions after password change (e.g. reset some backends)
158                 $plugin = $rcmail->plugins->exec_hook('password_change', array(
159                     'old_pass' => $curpwd, 'new_pass' => $newpwd));
160
161                 // Reset session password
162                 $_SESSION['password'] = $rcmail->encrypt($plugin['new_pass']);
163
164                 // Log password change
165                 if ($rcmail->config->get('password_log')) {
166                     write_log('password', sprintf('Password changed for user %s (ID: %d) from %s',
789e59 167                         $rcmail->get_user_name(), $rcmail->user->ID, rcmail_remote_ip()));
48e9c1 168                 }
T 169             }
170             else {
171                 $rcmail->output->command('display_message', $res, 'error');
172             }
173         }
174
175         rcmail_overwrite_action('plugin.password');
176         $rcmail->output->send('plugin');
177     }
178
179     function password_form()
180     {
181         $rcmail = rcmail::get_instance();
182
183         // add some labels to client
184         $rcmail->output->add_label(
185             'password.nopassword',
186             'password.nocurpassword',
187             'password.passwordinconsistency'
188         );
189
190         $rcmail->output->set_env('product_name', $rcmail->config->get('product_name'));
191
192         $table = new html_table(array('cols' => 2));
193
194         if ($rcmail->config->get('password_confirm_current')) {
195             // show current password selection
196             $field_id = 'curpasswd';
197             $input_curpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id,
198                 'size' => 20, 'autocomplete' => 'off'));
199
200             $table->add('title', html::label($field_id, Q($this->gettext('curpasswd'))));
201             $table->add(null, $input_curpasswd->show());
202         }
203
204         // show new password selection
205         $field_id = 'newpasswd';
206         $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id,
207             'size' => 20, 'autocomplete' => 'off'));
208
209         $table->add('title', html::label($field_id, Q($this->gettext('newpasswd'))));
210         $table->add(null, $input_newpasswd->show());
211
212         // show confirm password selection
213         $field_id = 'confpasswd';
214         $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id,
215             'size' => 20, 'autocomplete' => 'off'));
216
217         $table->add('title', html::label($field_id, Q($this->gettext('confpasswd'))));
218         $table->add(null, $input_confpasswd->show());
219
220         $out = html::div(array('class' => 'box'),
221             html::div(array('id' => 'prefs-title', 'class' => 'boxtitle'), $this->gettext('changepasswd')) .
222             html::div(array('class' => 'boxcontent'), $table->show() .
223             html::p(null,
224                 $rcmail->output->button(array(
225                     'command' => 'plugin.password-save',
226                     'type' => 'input',
227                     'class' => 'button mainaction',
228                     'label' => 'save'
229             )))));
230
231         $rcmail->output->add_gui_object('passform', 'password-form');
232
233         return $rcmail->output->form_tag(array(
234             'id' => 'password-form',
235             'name' => 'password-form',
236             'method' => 'post',
237             'action' => './?_task=settings&_action=plugin.password-save',
238         ), $out);
239     }
240
241     private function _save($curpass, $passwd)
242     {
243         $config = rcmail::get_instance()->config;
244         $driver = $config->get('password_driver', 'sql');
245         $class  = "rcube_{$driver}_password";
246         $file   = $this->home . "/drivers/$driver.php";
247
248         if (!file_exists($file)) {
249             raise_error(array(
250                 'code' => 600,
251                 'type' => 'php',
252                 'file' => __FILE__, 'line' => __LINE__,
253                 'message' => "Password plugin: Unable to open driver file ($file)"
254             ), true, false);
255             return $this->gettext('internalerror');
256         }
257
258         include_once $file;
259
260         if (!class_exists($class, false) || !method_exists($class, 'save')) {
261             raise_error(array(
262                 'code' => 600,
263                 'type' => 'php',
264                 'file' => __FILE__, 'line' => __LINE__,
265                 'message' => "Password plugin: Broken driver $driver"
266             ), true, false);
267             return $this->gettext('internalerror');
268         }
269
270         $object = new $class;
271         $result = $object->save($curpass, $passwd);
272
273         if (is_array($result)) {
274             $message = $result['message'];
275             $result  = $result['code'];
276         }
277
278         switch ($result) {
279             case PASSWORD_SUCCESS:
280                 return;
281             case PASSWORD_CRYPT_ERROR;
282                 $reason = $this->gettext('crypterror');
306745 283                 break;
48e9c1 284             case PASSWORD_CONNECT_ERROR;
T 285                 $reason = $this->gettext('connecterror');
306745 286                 break;
48e9c1 287             case PASSWORD_ERROR:
T 288             default:
289                 $reason = $this->gettext('internalerror');
290         }
291
292         if ($message) {
293             $reason .= ' ' . $message;
294         }
295
296         return $reason;
297     }
298 }