| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Change Password |
|---|
| 5 | * |
|---|
| 6 | * Plugin that adds a possibility to change password using a database |
|---|
| 7 | * (Settings -> Password tab) |
|---|
| 8 | * |
|---|
| 9 | * @version 1.1 |
|---|
| 10 | * @author Aleksander 'A.L.E.C' Machniak |
|---|
| 11 | * @editor Daniel Black |
|---|
| 12 | * |
|---|
| 13 | * Configuration Items (config/main.inc.php): |
|---|
| 14 | * password_confirm_current - boolean to determine whether current password |
|---|
| 15 | * is required to change password. Defaults to FALSE. |
|---|
| 16 | * password_db_dsn - is the PEAR database DSN for performing the query. Defaults |
|---|
| 17 | * to the default databse setting in config/db.inc.php |
|---|
| 18 | * password_query - the SQL query used to change the password. |
|---|
| 19 | * If the SQL query is a SELECT it will return an error message in a row if unsuccessful |
|---|
| 20 | * If the SQL query is a UPDATE it will update a single row only. |
|---|
| 21 | * An UPDATE where zero rows changed will be inteperated to be a wrong username/password |
|---|
| 22 | * More than one row changed will be inteperated as an internal error |
|---|
| 23 | * The query can contain the following macros that will be expanded as follows: |
|---|
| 24 | * %p is replaced with the plaintext new password |
|---|
| 25 | * %c is replaced with the crypt version of the new password, MD5 if available |
|---|
| 26 | * otherwise DES. |
|---|
| 27 | * %u is replaced with the username (from the session info) |
|---|
| 28 | * %o is replaced with the password before the change |
|---|
| 29 | * %h is replaced with the imap host (from the session info) |
|---|
| 30 | * Escaping of macros is handled by this module. |
|---|
| 31 | * Defaults to "SELECT update_passwd(%c, %u)" |
|---|
| 32 | * To use this you need to define the update_passwd function in your |
|---|
| 33 | * database. |
|---|
| 34 | * |
|---|
| 35 | * Example SQL queries: |
|---|
| 36 | * These will typically need to define a function to change the password: |
|---|
| 37 | * |
|---|
| 38 | * Example implementations of an update_passwd function: |
|---|
| 39 | * |
|---|
| 40 | * This is for use with LMS (http://lms.org.pl) database and postgres: |
|---|
| 41 | * CREATE OR REPLACE FUNCTION update_passwd(hash text, account text) RETURNS integer AS $$ |
|---|
| 42 | * DECLARE |
|---|
| 43 | * res integer; |
|---|
| 44 | * BEGIN |
|---|
| 45 | * UPDATE passwd SET password = hash |
|---|
| 46 | * WHERE login = split_part(account, '@', 1) |
|---|
| 47 | * AND domainid = (SELECT id FROM domains WHERE name = split_part(account, '@', 2)) |
|---|
| 48 | * RETURNING id INTO res; |
|---|
| 49 | * RETURN res; |
|---|
| 50 | * END; |
|---|
| 51 | * $$ LANGUAGE plpgsql SECURITY DEFINER; |
|---|
| 52 | * |
|---|
| 53 | * This is for use with a SELECT update_passwd(%o,%c,%u) query |
|---|
| 54 | * Uupdates the password only when the old password matches the MD5 password in the database |
|---|
| 55 | * CREATE FUNCTION update_password (oldpass text, cryptpass text, user text) RETURNS text |
|---|
| 56 | * MODIFIES SQL DATA |
|---|
| 57 | * BEGIN |
|---|
| 58 | * DECLARE currentsalt varchar(20); |
|---|
| 59 | * DECLARE error text; |
|---|
| 60 | * SET error = 'incorrect current password'; |
|---|
| 61 | * SELECT substring_index(substr(user.password,4),_latin1'$',1) INTO currentsalt FROM users WHERE username=user; |
|---|
| 62 | * SELECT '' INTO error FROM users WHERE username=user AND password=ENCRYPT(oldpass,currentsalt); |
|---|
| 63 | * UPDATE users SET password=cryptpass WHERE username=user AND password=ENCRYPT(oldpass,currentsalt); |
|---|
| 64 | * RETURN error; |
|---|
| 65 | * END |
|---|
| 66 | * |
|---|
| 67 | * Example SQL UPDATEs: |
|---|
| 68 | * |
|---|
| 69 | * Plain text passwords: |
|---|
| 70 | * UPDATE users SET password=%p WHERE username=%u AND password=%o AND domain=%h LIMIT 1 |
|---|
| 71 | * |
|---|
| 72 | * Crypt text passwords: |
|---|
| 73 | * UPDATE users SET password=%c WHERE username=%u LIMIT 1 |
|---|
| 74 | * |
|---|
| 75 | * Use a MYSQL crypt function (*nix only) with random 8 character salt |
|---|
| 76 | * UPDATE users SET password=ENCRYPT(%p,concat(_utf8'$1$',right(md5(rand()),8),_utf8'$')) WHERE username=%u LIMIT 1 |
|---|
| 77 | * |
|---|
| 78 | * MD5 stored passwords: |
|---|
| 79 | * UPDATE users SET password=MD5(%p) WHERE username=%u AND password=MD5(%o) LIMIT 1 |
|---|
| 80 | * |
|---|
| 81 | */ |
|---|
| 82 | class password extends rcube_plugin |
|---|
| 83 | { |
|---|
| 84 | public $task = 'settings'; |
|---|
| 85 | |
|---|
| 86 | function init() |
|---|
| 87 | { |
|---|
| 88 | $rcmail = rcmail::get_instance(); |
|---|
| 89 | // add Tab label |
|---|
| 90 | $rcmail->output->add_label('password'); |
|---|
| 91 | $this->register_action('plugin.password', array($this, 'password_init')); |
|---|
| 92 | $this->register_action('plugin.password-save', array($this, 'password_save')); |
|---|
| 93 | $this->register_handler('plugin.body', array($this, 'password_form')); |
|---|
| 94 | $this->include_script('password.js'); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | function password_init() |
|---|
| 98 | { |
|---|
| 99 | $this->add_texts('localization/'); |
|---|
| 100 | rcmail::get_instance()->output->send('plugin'); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | function password_save() |
|---|
| 104 | { |
|---|
| 105 | $rcmail = rcmail::get_instance(); |
|---|
| 106 | |
|---|
| 107 | $confirm = $rcmail->config->get('password_confirm_current'); |
|---|
| 108 | $this->add_texts('localization/'); |
|---|
| 109 | |
|---|
| 110 | if (($confirm && !isset($_POST['_curpasswd'])) || !isset($_POST['_newpasswd'])) |
|---|
| 111 | $rcmail->output->command('display_message', $this->gettext('nopassword'), 'error'); |
|---|
| 112 | else { |
|---|
| 113 | $curpwd = get_input_value('_curpasswd', RCUBE_INPUT_POST); |
|---|
| 114 | $newpwd = get_input_value('_newpasswd', RCUBE_INPUT_POST); |
|---|
| 115 | |
|---|
| 116 | if ($confirm && $_SESSION['password'] != $rcmail->encrypt_passwd($curpwd)) |
|---|
| 117 | $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error'); |
|---|
| 118 | else if (!($res = $this->_save($curpwd,$newpwd))) { |
|---|
| 119 | $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation'); |
|---|
| 120 | $_SESSION['password'] = $rcmail->encrypt_passwd($newpwd); |
|---|
| 121 | } else |
|---|
| 122 | $rcmail->output->command('display_message', $res, 'error'); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | rcmail_overwrite_action('plugin.password'); |
|---|
| 126 | rcmail::get_instance()->output->send('plugin'); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | function password_form() |
|---|
| 130 | { |
|---|
| 131 | $rcmail = rcmail::get_instance(); |
|---|
| 132 | |
|---|
| 133 | $confirm = $rcmail->config->get('password_confirm_current'); |
|---|
| 134 | // add some labels to client |
|---|
| 135 | $rcmail->output->add_label( |
|---|
| 136 | 'password.nopassword', |
|---|
| 137 | 'password.nocurpassword', |
|---|
| 138 | 'password.passwordinconsistency', |
|---|
| 139 | 'password.changepasswd' |
|---|
| 140 | ); |
|---|
| 141 | // $rcmail->output->set_pagetitle($this->gettext('changepasswd')); |
|---|
| 142 | $rcmail->output->set_env('product_name', $rcmail->config->get('product_name')); |
|---|
| 143 | |
|---|
| 144 | // allow the following attributes to be added to the <table> tag |
|---|
| 145 | $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary')); |
|---|
| 146 | |
|---|
| 147 | // return the complete edit form as table |
|---|
| 148 | $out = '<table' . $attrib_str . ">\n\n"; |
|---|
| 149 | |
|---|
| 150 | $a_show_cols = array('newpasswd' => array('type' => 'text'), |
|---|
| 151 | 'confpasswd' => array('type' => 'text')); |
|---|
| 152 | |
|---|
| 153 | if ($confirm) { |
|---|
| 154 | $a_show_cols['curpasswd'] = array('type' => 'text'); |
|---|
| 155 | // show current password selection |
|---|
| 156 | $field_id = 'curpasswd'; |
|---|
| 157 | $input_newpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id, 'size' => 20)); |
|---|
| 158 | |
|---|
| 159 | $out .= sprintf("<tr><td class=\"title\"><label for=\"%s\">%s</label></td><td>%s</td></tr>\n", |
|---|
| 160 | $field_id, |
|---|
| 161 | rep_specialchars_output($this->gettext('curpasswd')), |
|---|
| 162 | $input_newpasswd->show($rcmail->config->get('curpasswd'))); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | // show new password selection |
|---|
| 166 | $field_id = 'newpasswd'; |
|---|
| 167 | $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id, 'size' => 20)); |
|---|
| 168 | |
|---|
| 169 | $out .= sprintf("<tr><td class=\"title\"><label for=\"%s\">%s</label></td><td>%s</td></tr>\n", |
|---|
| 170 | $field_id, |
|---|
| 171 | rep_specialchars_output($this->gettext('newpasswd')), |
|---|
| 172 | $input_newpasswd->show($rcmail->config->get('newpasswd'))); |
|---|
| 173 | |
|---|
| 174 | // show confirm password selection |
|---|
| 175 | $field_id = 'confpasswd'; |
|---|
| 176 | $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id, 'size' => 20)); |
|---|
| 177 | |
|---|
| 178 | $out .= sprintf("<tr><td class=\"title\"><label for=\"%s\">%s</label></td><td>%s</td></tr>\n", |
|---|
| 179 | $field_id, |
|---|
| 180 | rep_specialchars_output($this->gettext('confpasswd')), |
|---|
| 181 | $input_confpasswd->show($rcmail->config->get('confpasswd'))); |
|---|
| 182 | |
|---|
| 183 | $out .= "\n</table>"; |
|---|
| 184 | |
|---|
| 185 | $out .= '<br />'; |
|---|
| 186 | |
|---|
| 187 | $out .= $rcmail->output->button(array( |
|---|
| 188 | 'command' => 'plugin.password-save', |
|---|
| 189 | 'type' => 'input', |
|---|
| 190 | 'class' => 'button mainaction', |
|---|
| 191 | 'label' => 'save' |
|---|
| 192 | )); |
|---|
| 193 | |
|---|
| 194 | $rcmail->output->add_gui_object('passform', 'password-form'); |
|---|
| 195 | |
|---|
| 196 | return $rcmail->output->form_tag(array( |
|---|
| 197 | 'id' => 'password-form', |
|---|
| 198 | 'name' => 'password-form', |
|---|
| 199 | 'method' => 'post', |
|---|
| 200 | 'action' => './?_task=settings&_action=plugin.password-save', |
|---|
| 201 | ), $out); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | private function _save($curpass,$passwd) |
|---|
| 205 | { |
|---|
| 206 | $cfg = rcmail::get_instance()->config; |
|---|
| 207 | |
|---|
| 208 | if (!($sql = $cfg->get('password_query'))) |
|---|
| 209 | $sql = "SELECT update_passwd(%c, %u)"; |
|---|
| 210 | |
|---|
| 211 | if ($dsn = $cfg->get('password_db_dsn')) { |
|---|
| 212 | $db = new rcube_mdb2($dsn, '', FALSE); |
|---|
| 213 | $db->set_debug((bool)$cfg->get('sql_debug')); |
|---|
| 214 | $db->db_connect('w'); |
|---|
| 215 | } else { |
|---|
| 216 | $db = rcmail::get_instance()->get_dbh(); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | if ($err = $db->is_error()) |
|---|
| 220 | return $err; |
|---|
| 221 | |
|---|
| 222 | if (strpos($sql,'%c') !== FALSE) { |
|---|
| 223 | $salt = ''; |
|---|
| 224 | if (CRYPT_MD5) { |
|---|
| 225 | $len = rand(3,CRYPT_SALT_LENGTH); |
|---|
| 226 | } else if (CRYPT_STD_DES) { |
|---|
| 227 | $len = 2; |
|---|
| 228 | } else { |
|---|
| 229 | return $this->gettext('nocryptfunction'); |
|---|
| 230 | } |
|---|
| 231 | for ($i = 0; $i < $len ; $i++) { |
|---|
| 232 | $salt .= chr(rand(ord('.'),ord('z'))); |
|---|
| 233 | } |
|---|
| 234 | $sql = str_replace('%c', $db->quote(crypt($passwd, CRYPT_MD5 ? '$1$'.$salt.'$' : $salt)), $sql); |
|---|
| 235 | } |
|---|
| 236 | $sql = str_replace('%u', $db->quote($_SESSION['username'],'text'), $sql); |
|---|
| 237 | $sql = str_replace('%p', $db->quote($passwd,'text'), $sql); |
|---|
| 238 | $sql = str_replace('%o', $db->quote($curpass,'text'), $sql); |
|---|
| 239 | $sql = str_replace('%h', $db->quote($_SESSION['imap_host'],'text'), $sql); |
|---|
| 240 | |
|---|
| 241 | $res = $db->query($sql); |
|---|
| 242 | if ($err = $db->is_error()) |
|---|
| 243 | return $err; |
|---|
| 244 | if (strtolower(substr(trim($query),0,6))=='select') { |
|---|
| 245 | return $db->fetch_array($res); |
|---|
| 246 | } else { |
|---|
| 247 | $res = $db->affected_rows($res); |
|---|
| 248 | if ($res == 0) return $this->gettext('errorsaving'); |
|---|
| 249 | if ($res == 1) return FALSE; // THis is the good case - 1 row updated |
|---|
| 250 | return $this->gettext('internalerror'); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | ?> |
|---|