Aleksander Machniak
2012-06-28 d9012055cbc94e478c41c975c2d22843d847b065
commit | author | age
48e9c1 1 <?php
T 2
3 /*
4  +-------------------------------------------------------------------------+
5  | Password Plugin for Roundcube                                           |
6  | @version @package_version@                                                             |
7  |                                                                         |
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();
58
59         // Exceptions list
60         if ($exceptions = $rcmail->config->get('password_login_exceptions')) {
61             $exceptions = array_map('trim', (array) $exceptions);
62             $exceptions = array_filter($exceptions);
63             $username   = $_SESSION['username'];
64
65             foreach ($exceptions as $ec) {
66                 if ($username === $ec) {
67                     return;
68                 }
69             }
70         }
71
72         // add Tab label
73         $rcmail->output->add_label('password');
74         $this->register_action('plugin.password', array($this, 'password_init'));
75         $this->register_action('plugin.password-save', array($this, 'password_save'));
76         $this->include_script('password.js');
77     }
78
79     function password_init()
80     {
81         $this->add_texts('localization/');
82         $this->register_handler('plugin.body', array($this, 'password_form'));
83
84         $rcmail = rcmail::get_instance();
85         $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
86         $rcmail->output->send('plugin');
87     }
88
89     function password_save()
90     {
91         $rcmail = rcmail::get_instance();
92
93         $this->add_texts('localization/');
94         $this->register_handler('plugin.body', array($this, 'password_form'));
95         $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
96
97         $confirm = $rcmail->config->get('password_confirm_current');
98         $required_length = intval($rcmail->config->get('password_minimum_length'));
99         $check_strength = $rcmail->config->get('password_require_nonalpha');
100
101         if (($confirm && !isset($_POST['_curpasswd'])) || !isset($_POST['_newpasswd'])) {
102             $rcmail->output->command('display_message', $this->gettext('nopassword'), 'error');
103         }
104         else {
105             $charset    = strtoupper($rcmail->config->get('password_charset', 'ISO-8859-1'));
106             $rc_charset = strtoupper($rcmail->output->get_charset());
107
108             $sespwd = $rcmail->decrypt($_SESSION['password']);
109             $curpwd = $confirm ? get_input_value('_curpasswd', RCUBE_INPUT_POST, true, $charset) : $sespwd;
110             $newpwd = get_input_value('_newpasswd', RCUBE_INPUT_POST, true);
111             $conpwd = get_input_value('_confpasswd', RCUBE_INPUT_POST, true);
112
113             // check allowed characters according to the configured 'password_charset' option
114             // by converting the password entered by the user to this charset and back to UTF-8
115             $orig_pwd = $newpwd;
116             $chk_pwd = rcube_charset_convert($orig_pwd, $rc_charset, $charset);
117             $chk_pwd = rcube_charset_convert($chk_pwd, $charset, $rc_charset);
118
119             // WARNING: Default password_charset is ISO-8859-1, so conversion will
120             // change national characters. This may disable possibility of using
121             // the same password in other MUA's.
122             // We're doing this for consistence with Roundcube core
123             $newpwd = rcube_charset_convert($newpwd, $rc_charset, $charset);
124             $conpwd = rcube_charset_convert($conpwd, $rc_charset, $charset);
125
126             if ($chk_pwd != $orig_pwd) {
127                 $rcmail->output->command('display_message', $this->gettext('passwordforbidden'), 'error');
128             }
129             // other passwords validity checks
130             else if ($conpwd != $newpwd) {
131                 $rcmail->output->command('display_message', $this->gettext('passwordinconsistency'), 'error');
132             }
133             else if ($confirm && $sespwd != $curpwd) {
134                 $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error');
135             }
136             else if ($required_length && strlen($newpwd) < $required_length) {
137                 $rcmail->output->command('display_message', $this->gettext(
138                     array('name' => 'passwordshort', 'vars' => array('length' => $required_length))), 'error');
139             }
140             else if ($check_strength && (!preg_match("/[0-9]/", $newpwd) || !preg_match("/[^A-Za-z0-9]/", $newpwd))) {
141                 $rcmail->output->command('display_message', $this->gettext('passwordweak'), 'error');
142             }
143             // password is the same as the old one, do nothing, return success
144             else if ($sespwd == $newpwd) {
145                 $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
146             }
147             // try to save the password
148             else if (!($res = $this->_save($curpwd, $newpwd))) {
149                 $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
150
151                 // allow additional actions after password change (e.g. reset some backends)
152                 $plugin = $rcmail->plugins->exec_hook('password_change', array(
153                     'old_pass' => $curpwd, 'new_pass' => $newpwd));
154
155                 // Reset session password
156                 $_SESSION['password'] = $rcmail->encrypt($plugin['new_pass']);
157
158                 // Log password change
159                 if ($rcmail->config->get('password_log')) {
160                     write_log('password', sprintf('Password changed for user %s (ID: %d) from %s',
161                         $rcmail->user->get_username(), $rcmail->user->ID, rcmail_remote_ip()));
162                 }
163             }
164             else {
165                 $rcmail->output->command('display_message', $res, 'error');
166             }
167         }
168
169         rcmail_overwrite_action('plugin.password');
170         $rcmail->output->send('plugin');
171     }
172
173     function password_form()
174     {
175         $rcmail = rcmail::get_instance();
176
177         // add some labels to client
178         $rcmail->output->add_label(
179             'password.nopassword',
180             'password.nocurpassword',
181             'password.passwordinconsistency'
182         );
183
184         $rcmail->output->set_env('product_name', $rcmail->config->get('product_name'));
185
186         $table = new html_table(array('cols' => 2));
187
188         if ($rcmail->config->get('password_confirm_current')) {
189             // show current password selection
190             $field_id = 'curpasswd';
191             $input_curpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id,
192                 'size' => 20, 'autocomplete' => 'off'));
193
194             $table->add('title', html::label($field_id, Q($this->gettext('curpasswd'))));
195             $table->add(null, $input_curpasswd->show());
196         }
197
198         // show new password selection
199         $field_id = 'newpasswd';
200         $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id,
201             'size' => 20, 'autocomplete' => 'off'));
202
203         $table->add('title', html::label($field_id, Q($this->gettext('newpasswd'))));
204         $table->add(null, $input_newpasswd->show());
205
206         // show confirm password selection
207         $field_id = 'confpasswd';
208         $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id,
209             'size' => 20, 'autocomplete' => 'off'));
210
211         $table->add('title', html::label($field_id, Q($this->gettext('confpasswd'))));
212         $table->add(null, $input_confpasswd->show());
213
214         $out = html::div(array('class' => 'box'),
215             html::div(array('id' => 'prefs-title', 'class' => 'boxtitle'), $this->gettext('changepasswd')) .
216             html::div(array('class' => 'boxcontent'), $table->show() .
217             html::p(null,
218                 $rcmail->output->button(array(
219                     'command' => 'plugin.password-save',
220                     'type' => 'input',
221                     'class' => 'button mainaction',
222                     'label' => 'save'
223             )))));
224
225         $rcmail->output->add_gui_object('passform', 'password-form');
226
227         return $rcmail->output->form_tag(array(
228             'id' => 'password-form',
229             'name' => 'password-form',
230             'method' => 'post',
231             'action' => './?_task=settings&_action=plugin.password-save',
232         ), $out);
233     }
234
235     private function _save($curpass, $passwd)
236     {
237         $config = rcmail::get_instance()->config;
238         $driver = $config->get('password_driver', 'sql');
239         $class  = "rcube_{$driver}_password";
240         $file   = $this->home . "/drivers/$driver.php";
241
242         if (!file_exists($file)) {
243             raise_error(array(
244                 'code' => 600,
245                 'type' => 'php',
246                 'file' => __FILE__, 'line' => __LINE__,
247                 'message' => "Password plugin: Unable to open driver file ($file)"
248             ), true, false);
249             return $this->gettext('internalerror');
250         }
251
252         include_once $file;
253
254         if (!class_exists($class, false) || !method_exists($class, 'save')) {
255             raise_error(array(
256                 'code' => 600,
257                 'type' => 'php',
258                 'file' => __FILE__, 'line' => __LINE__,
259                 'message' => "Password plugin: Broken driver $driver"
260             ), true, false);
261             return $this->gettext('internalerror');
262         }
263
264         $object = new $class;
265         $result = $object->save($curpass, $passwd);
266
267         if (is_array($result)) {
268             $message = $result['message'];
269             $result  = $result['code'];
270         }
271
272         switch ($result) {
273             case PASSWORD_SUCCESS:
274                 return;
275             case PASSWORD_CRYPT_ERROR;
276                 $reason = $this->gettext('crypterror');
277             case PASSWORD_CONNECT_ERROR;
278                 $reason = $this->gettext('connecterror');
279             case PASSWORD_ERROR:
280             default:
281                 $reason = $this->gettext('internalerror');
282         }
283
284         if ($message) {
285             $reason .= ' ' . $message;
286         }
287
288         return $reason;
289     }
290 }