password.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. /**
  3. * Password Plugin for Roundcube
  4. *
  5. * @author Aleksander Machniak <alec@alec.pl>
  6. *
  7. * Copyright (C) 2005-2015, The Roundcube Dev Team
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  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
  20. * along with this program. If not, see http://www.gnu.org/licenses/.
  21. */
  22. define('PASSWORD_CRYPT_ERROR', 1);
  23. define('PASSWORD_ERROR', 2);
  24. define('PASSWORD_CONNECT_ERROR', 3);
  25. define('PASSWORD_IN_HISTORY', 4);
  26. define('PASSWORD_SUCCESS', 0);
  27. /**
  28. * Change password plugin
  29. *
  30. * Plugin that adds functionality to change a users password.
  31. * It provides common functionality and user interface and supports
  32. * several backends to finally update the password.
  33. *
  34. * For installation and configuration instructions please read the README file.
  35. *
  36. * @author Aleksander Machniak
  37. */
  38. class password extends rcube_plugin
  39. {
  40. public $task = 'settings|login';
  41. public $noframe = true;
  42. public $noajax = true;
  43. private $newuser = false;
  44. function init()
  45. {
  46. $rcmail = rcmail::get_instance();
  47. $this->load_config();
  48. if ($rcmail->task == 'settings') {
  49. if (!$this->check_host_login_exceptions()) {
  50. return;
  51. }
  52. $this->add_texts('localization/');
  53. $this->add_hook('settings_actions', array($this, 'settings_actions'));
  54. $this->register_action('plugin.password', array($this, 'password_init'));
  55. $this->register_action('plugin.password-save', array($this, 'password_save'));
  56. }
  57. else if ($rcmail->config->get('password_force_new_user')) {
  58. $this->add_hook('user_create', array($this, 'user_create'));
  59. $this->add_hook('login_after', array($this, 'login_after'));
  60. }
  61. }
  62. function settings_actions($args)
  63. {
  64. // register as settings action
  65. $args['actions'][] = array(
  66. 'action' => 'plugin.password',
  67. 'class' => 'password',
  68. 'label' => 'password',
  69. 'title' => 'changepasswd',
  70. 'domain' => 'password',
  71. );
  72. return $args;
  73. }
  74. function password_init()
  75. {
  76. $this->register_handler('plugin.body', array($this, 'password_form'));
  77. $rcmail = rcmail::get_instance();
  78. $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
  79. if (rcube_utils::get_input_value('_first', rcube_utils::INPUT_GET)) {
  80. $rcmail->output->command('display_message', $this->gettext('firstloginchange'), 'notice');
  81. }
  82. else if (!empty($_SESSION['password_expires'])) {
  83. if ($_SESSION['password_expires'] == 1) {
  84. $rcmail->output->command('display_message', $this->gettext('passwdexpired'), 'error');
  85. }
  86. else {
  87. $rcmail->output->command('display_message', $this->gettext(array(
  88. 'name' => 'passwdexpirewarning',
  89. 'vars' => array('expirationdatetime' => $_SESSION['password_expires'])
  90. )), 'warning');
  91. }
  92. }
  93. $rcmail->output->send('plugin');
  94. }
  95. function password_save()
  96. {
  97. $this->register_handler('plugin.body', array($this, 'password_form'));
  98. $rcmail = rcmail::get_instance();
  99. $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
  100. $form_disabled = $rcmail->config->get('password_disabled');
  101. $confirm = $rcmail->config->get('password_confirm_current');
  102. $required_length = intval($rcmail->config->get('password_minimum_length'));
  103. $check_strength = $rcmail->config->get('password_require_nonalpha');
  104. if (($confirm && !isset($_POST['_curpasswd'])) || !isset($_POST['_newpasswd'])) {
  105. $rcmail->output->command('display_message', $this->gettext('nopassword'), 'error');
  106. }
  107. else {
  108. $charset = strtoupper($rcmail->config->get('password_charset', 'ISO-8859-1'));
  109. $rc_charset = strtoupper($rcmail->output->get_charset());
  110. $sespwd = $rcmail->decrypt($_SESSION['password']);
  111. $curpwd = $confirm ? rcube_utils::get_input_value('_curpasswd', rcube_utils::INPUT_POST, true, $charset) : $sespwd;
  112. $newpwd = rcube_utils::get_input_value('_newpasswd', rcube_utils::INPUT_POST, true);
  113. $conpwd = rcube_utils::get_input_value('_confpasswd', rcube_utils::INPUT_POST, true);
  114. // check allowed characters according to the configured 'password_charset' option
  115. // by converting the password entered by the user to this charset and back to UTF-8
  116. $orig_pwd = $newpwd;
  117. $chk_pwd = rcube_charset::convert($orig_pwd, $rc_charset, $charset);
  118. $chk_pwd = rcube_charset::convert($chk_pwd, $charset, $rc_charset);
  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. if ($chk_pwd != $orig_pwd) {
  126. $rcmail->output->command('display_message', $this->gettext('passwordforbidden'), 'error');
  127. }
  128. // other passwords validity checks
  129. else if ($conpwd != $newpwd) {
  130. $rcmail->output->command('display_message', $this->gettext('passwordinconsistency'), 'error');
  131. }
  132. else if ($confirm && $sespwd != $curpwd) {
  133. $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error');
  134. }
  135. else if ($required_length && strlen($newpwd) < $required_length) {
  136. $rcmail->output->command('display_message', $this->gettext(
  137. array('name' => 'passwordshort', 'vars' => array('length' => $required_length))), 'error');
  138. }
  139. else if ($check_strength && (!preg_match("/[0-9]/", $newpwd) || !preg_match("/[^A-Za-z0-9]/", $newpwd))) {
  140. $rcmail->output->command('display_message', $this->gettext('passwordweak'), 'error');
  141. }
  142. // password is the same as the old one, warn user, return error
  143. else if ($sespwd == $newpwd && !$rcmail->config->get('password_force_save')) {
  144. $rcmail->output->command('display_message', $this->gettext('samepasswd'), 'error');
  145. }
  146. // try to save the password
  147. else if (!($res = $this->_save($curpwd, $newpwd))) {
  148. $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
  149. // allow additional actions after password change (e.g. reset some backends)
  150. $plugin = $rcmail->plugins->exec_hook('password_change', array(
  151. 'old_pass' => $curpwd, 'new_pass' => $newpwd));
  152. // Reset session password
  153. $_SESSION['password'] = $rcmail->encrypt($plugin['new_pass']);
  154. // Log password change
  155. if ($rcmail->config->get('password_log')) {
  156. rcube::write_log('password', sprintf('Password changed for user %s (ID: %d) from %s',
  157. $rcmail->get_user_name(), $rcmail->user->ID, rcube_utils::remote_ip()));
  158. }
  159. // Remove expiration date/time
  160. $rcmail->session->remove('password_expires');
  161. }
  162. else {
  163. $rcmail->output->command('display_message', $res, 'error');
  164. }
  165. }
  166. $rcmail->overwrite_action('plugin.password');
  167. $rcmail->output->send('plugin');
  168. }
  169. function password_form()
  170. {
  171. $rcmail = rcmail::get_instance();
  172. // add some labels to client
  173. $rcmail->output->add_label(
  174. 'password.nopassword',
  175. 'password.nocurpassword',
  176. 'password.passwordinconsistency'
  177. );
  178. $form_disabled = $rcmail->config->get('password_disabled');
  179. $rcmail->output->set_env('product_name', $rcmail->config->get('product_name'));
  180. $rcmail->output->set_env('password_disabled', !empty($form_disabled));
  181. $table = new html_table(array('cols' => 2));
  182. if ($rcmail->config->get('password_confirm_current')) {
  183. // show current password selection
  184. $field_id = 'curpasswd';
  185. $input_curpasswd = new html_passwordfield(array(
  186. 'name' => '_curpasswd',
  187. 'id' => $field_id,
  188. 'size' => 20,
  189. 'autocomplete' => 'off',
  190. ));
  191. $table->add('title', html::label($field_id, rcube::Q($this->gettext('curpasswd'))));
  192. $table->add(null, $input_curpasswd->show());
  193. }
  194. // show new password selection
  195. $field_id = 'newpasswd';
  196. $input_newpasswd = new html_passwordfield(array(
  197. 'name' => '_newpasswd',
  198. 'id' => $field_id,
  199. 'size' => 20,
  200. 'autocomplete' => 'off',
  201. ));
  202. $table->add('title', html::label($field_id, rcube::Q($this->gettext('newpasswd'))));
  203. $table->add(null, $input_newpasswd->show());
  204. // show confirm password selection
  205. $field_id = 'confpasswd';
  206. $input_confpasswd = new html_passwordfield(array(
  207. 'name' => '_confpasswd',
  208. 'id' => $field_id,
  209. 'size' => 20,
  210. 'autocomplete' => 'off',
  211. ));
  212. $table->add('title', html::label($field_id, rcube::Q($this->gettext('confpasswd'))));
  213. $table->add(null, $input_confpasswd->show());
  214. $rules = '';
  215. $required_length = intval($rcmail->config->get('password_minimum_length'));
  216. if ($required_length > 0) {
  217. $rules .= html::tag('li', array('id' => 'required-length'), $this->gettext(array(
  218. 'name' => 'passwordshort',
  219. 'vars' => array('length' => $required_length)
  220. )));
  221. }
  222. if ($rcmail->config->get('password_require_nonalpha')) {
  223. $rules .= html::tag('li', array('id' => 'require-nonalpha'), $this->gettext('passwordweak'));
  224. }
  225. if (!empty($rules)) {
  226. $rules = html::tag('ul', array('id' => 'ruleslist'), $rules);
  227. }
  228. $disabled_msg = '';
  229. if ($form_disabled) {
  230. $disabled_msg = is_string($form_disabled) ? $form_disabled : $this->gettext('disablednotice');
  231. $disabled_msg = html::div(array('class' => 'boxwarning', 'id' => 'password-notice'), $disabled_msg);
  232. }
  233. $submit_button = $rcmail->output->button(array(
  234. 'command' => 'plugin.password-save',
  235. 'type' => 'input',
  236. 'class' => 'button mainaction',
  237. 'label' => 'save',
  238. ));
  239. $form_buttons = html::p(array('class' => 'formbuttons'), $submit_button);
  240. $out = html::div(array('class' => 'box'),
  241. html::div(array('id' => 'prefs-title', 'class' => 'boxtitle'), $this->gettext('changepasswd'))
  242. . html::div(array('class' => 'boxcontent'),
  243. $disabled_msg . $table->show() . $rules . $form_buttons));
  244. $rcmail->output->add_gui_object('passform', 'password-form');
  245. $this->include_script('password.js');
  246. return $rcmail->output->form_tag(array(
  247. 'id' => 'password-form',
  248. 'name' => 'password-form',
  249. 'method' => 'post',
  250. 'action' => './?_task=settings&_action=plugin.password-save',
  251. ), $out);
  252. }
  253. private function _save($curpass, $passwd)
  254. {
  255. $config = rcmail::get_instance()->config;
  256. $driver = $config->get('password_driver', 'sql');
  257. $class = "rcube_{$driver}_password";
  258. $file = $this->home . "/drivers/$driver.php";
  259. if (!file_exists($file)) {
  260. rcube::raise_error(array(
  261. 'code' => 600,
  262. 'type' => 'php',
  263. 'file' => __FILE__, 'line' => __LINE__,
  264. 'message' => "Password plugin: Unable to open driver file ($file)"
  265. ), true, false);
  266. return $this->gettext('internalerror');
  267. }
  268. include_once $file;
  269. if (!class_exists($class, false) || !method_exists($class, 'save')) {
  270. rcube::raise_error(array(
  271. 'code' => 600,
  272. 'type' => 'php',
  273. 'file' => __FILE__, 'line' => __LINE__,
  274. 'message' => "Password plugin: Broken driver $driver"
  275. ), true, false);
  276. return $this->gettext('internalerror');
  277. }
  278. $object = new $class;
  279. $result = $object->save($curpass, $passwd);
  280. $message = '';
  281. if (is_array($result)) {
  282. $message = $result['message'];
  283. $result = $result['code'];
  284. }
  285. switch ($result) {
  286. case PASSWORD_SUCCESS:
  287. return;
  288. case PASSWORD_CRYPT_ERROR:
  289. $reason = $this->gettext('crypterror');
  290. break;
  291. case PASSWORD_CONNECT_ERROR:
  292. $reason = $this->gettext('connecterror');
  293. break;
  294. case PASSWORD_IN_HISTORY:
  295. $reason = $this->gettext('passwdinhistory');
  296. break;
  297. case PASSWORD_ERROR:
  298. default:
  299. $reason = $this->gettext('internalerror');
  300. }
  301. if ($message) {
  302. $reason .= ' ' . $message;
  303. }
  304. return $reason;
  305. }
  306. function user_create($args)
  307. {
  308. $this->newuser = true;
  309. return $args;
  310. }
  311. function login_after($args)
  312. {
  313. if ($this->newuser && $this->check_host_login_exceptions()) {
  314. $args['_task'] = 'settings';
  315. $args['_action'] = 'plugin.password';
  316. $args['_first'] = 'true';
  317. }
  318. return $args;
  319. }
  320. // Check if host and login is allowed to change the password, false = not allowed, true = not allowed
  321. private function check_host_login_exceptions()
  322. {
  323. $rcmail = rcmail::get_instance();
  324. // Host exceptions
  325. $hosts = $rcmail->config->get('password_hosts');
  326. if (!empty($hosts) && !in_array($_SESSION['storage_host'], (array) $hosts)) {
  327. return false;
  328. }
  329. // Login exceptions
  330. if ($exceptions = $rcmail->config->get('password_login_exceptions')) {
  331. $exceptions = array_map('trim', (array) $exceptions);
  332. $exceptions = array_filter($exceptions);
  333. $username = $_SESSION['username'];
  334. foreach ($exceptions as $ec) {
  335. if ($username === $ec) {
  336. return false;
  337. }
  338. }
  339. }
  340. return true;
  341. }
  342. /**
  343. * Hashes a password and returns the hash based on the specified method
  344. *
  345. * Parts of the code originally from the phpLDAPadmin development team
  346. * http://phpldapadmin.sourceforge.net/
  347. *
  348. * @param string Clear password
  349. * @param string Hashing method
  350. * @param bool|string Prefix string or TRUE to add a default prefix
  351. *
  352. * @return string Hashed password
  353. */
  354. static function hash_password($password, $method = '', $prefixed = true)
  355. {
  356. $method = strtolower($method);
  357. $rcmail = rcmail::get_instance();
  358. $prefix = '';
  359. $crypted = '';
  360. $default = false;
  361. if (empty($method) || $method == 'default') {
  362. $method = $rcmail->config->get('password_algorithm');
  363. $prefixed = $rcmail->config->get('password_algorithm_prefix');
  364. $default = true;
  365. }
  366. else if ($method == 'crypt') { // deprecated
  367. if (!($method = $rcmail->config->get('password_crypt_hash'))) {
  368. $method = 'md5';
  369. }
  370. if (!strpos($method, '-crypt')) {
  371. $method .= '-crypt';
  372. }
  373. }
  374. switch ($method) {
  375. case 'des':
  376. case 'des-crypt':
  377. $crypted = crypt($password, rcube_utils::random_bytes(2));
  378. $prefix = '{CRYPT}';
  379. break;
  380. case 'ext_des': // for BC
  381. case 'ext-des-crypt':
  382. $crypted = crypt($password, '_' . rcube_utils::random_bytes(8));
  383. $prefix = '{CRYPT}';
  384. break;
  385. case 'md5crypt': // for BC
  386. case 'md5-crypt':
  387. $crypted = crypt($password, '$1$' . rcube_utils::random_bytes(9));
  388. $prefix = '{CRYPT}';
  389. break;
  390. case 'sha256-crypt':
  391. $rounds = (int) $rcmail->config->get('password_crypt_rounds');
  392. $prefix = '$5$';
  393. if ($rounds > 1000) {
  394. $prefix .= 'rounds=' . $rounds . '$';
  395. }
  396. $crypted = crypt($password, $prefix . rcube_utils::random_bytes(16));
  397. $prefix = '{CRYPT}';
  398. break;
  399. case 'sha512-crypt':
  400. $rounds = (int) $rcmail->config->get('password_crypt_rounds');
  401. $prefix = '$6$';
  402. if ($rounds > 1000) {
  403. $prefix .= 'rounds=' . $rounds . '$';
  404. }
  405. $crypted = crypt($password, $prefix . rcube_utils::random_bytes(16));
  406. $prefix = '{CRYPT}';
  407. break;
  408. case 'blowfish': // for BC
  409. case 'blowfish-crypt':
  410. $cost = (int) $rcmail->config->get('password_blowfish_cost');
  411. $cost = $cost < 4 || $cost > 31 ? 12 : $cost;
  412. $prefix = sprintf('$2a$%02d$', $cost);
  413. $crypted = crypt($password, $prefix . rcube_utils::random_bytes(22));
  414. $prefix = '{CRYPT}';
  415. break;
  416. case 'md5':
  417. $crypted = base64_encode(pack('H*', md5($password)));
  418. $prefix = '{MD5}';
  419. break;
  420. case 'sha':
  421. if (function_exists('sha1')) {
  422. $crypted = pack('H*', sha1($password));
  423. }
  424. else if (function_exists('hash')) {
  425. $crypted = hash('sha1', $password, true);
  426. }
  427. else if (function_exists('mhash')) {
  428. $crypted = mhash(MHASH_SHA1, $password);
  429. }
  430. else {
  431. rcube::raise_error(array(
  432. 'code' => 600, 'file' => __FILE__, 'line' => __LINE__,
  433. 'message' => "Password plugin: Your PHP install does not have the mhash()/hash() nor sha1() function"
  434. ), true, true);
  435. }
  436. $crypted = base64_encode($crypted);
  437. $prefix = '{SHA}';
  438. break;
  439. case 'ssha256':
  440. $salt = rcube_utils::random_bytes(8);
  441. $crypted = base64_encode( hash('sha256', $password . $salt, TRUE ) . $salt );
  442. $prefix = '{SSHA256}';
  443. break;
  444. case 'ssha':
  445. $salt = rcube_utils::random_bytes(8);
  446. if (function_exists('mhash') && function_exists('mhash_keygen_s2k')) {
  447. $salt = mhash_keygen_s2k(MHASH_SHA1, $password, $salt, 4);
  448. $crypted = mhash(MHASH_SHA1, $password . $salt);
  449. }
  450. else if (function_exists('sha1')) {
  451. $salt = substr(pack("H*", sha1($salt . $password)), 0, 4);
  452. $crypted = sha1($password . $salt, true);
  453. }
  454. else if (function_exists('hash')) {
  455. $salt = substr(pack("H*", hash('sha1', $salt . $password)), 0, 4);
  456. $crypted = hash('sha1', $password . $salt, true);
  457. }
  458. else {
  459. rcube::raise_error(array(
  460. 'code' => 600, 'file' => __FILE__, 'line' => __LINE__,
  461. 'message' => "Password plugin: Your PHP install does not have the mhash()/hash() nor sha1() function"
  462. ), true, true);
  463. }
  464. $crypted = base64_encode($crypted . $salt);
  465. $prefix = '{SSHA}';
  466. break;
  467. case 'smd5':
  468. $salt = rcube_utils::random_bytes(8);
  469. if (function_exists('mhash') && function_exists('mhash_keygen_s2k')) {
  470. $salt = mhash_keygen_s2k(MHASH_MD5, $password, $salt, 4);
  471. $crypted = mhash(MHASH_MD5, $password . $salt);
  472. }
  473. else if (function_exists('hash')) {
  474. $salt = substr(pack("H*", hash('md5', $salt . $password)), 0, 4);
  475. $crypted = hash('md5', $password . $salt, true);
  476. }
  477. else {
  478. $salt = substr(pack("H*", md5($salt . $password)), 0, 4);
  479. $crypted = md5($password . $salt, true);
  480. }
  481. $crypted = base64_encode($crypted . $salt);
  482. $prefix = '{SMD5}';
  483. break;
  484. case 'samba':
  485. if (function_exists('hash')) {
  486. $crypted = hash('md4', rcube_charset::convert($password, RCUBE_CHARSET, 'UTF-16LE'));
  487. $crypted = strtoupper($crypted);
  488. }
  489. else {
  490. rcube::raise_error(array(
  491. 'code' => 600, 'file' => __FILE__, 'line' => __LINE__,
  492. 'message' => "Password plugin: Your PHP install does not have hash() function"
  493. ), true, true);
  494. }
  495. break;
  496. case 'ad':
  497. $crypted = rcube_charset::convert('"' . $password . '"', RCUBE_CHARSET, 'UTF-16LE');
  498. break;
  499. case 'cram-md5': // deprecated
  500. require_once __DIR__ . '/../helpers/dovecot_hmacmd5.php';
  501. $crypted = dovecot_hmacmd5($password);
  502. $prefix = '{CRAM-MD5}';
  503. break;
  504. case 'dovecot':
  505. if (!($dovecotpw = $rcmail->config->get('password_dovecotpw'))) {
  506. $dovecotpw = 'dovecotpw';
  507. }
  508. if (!($method = $rcmail->config->get('password_dovecotpw_method'))) {
  509. $method = 'CRAM-MD5';
  510. }
  511. $spec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('file', '/dev/null', 'a'));
  512. $pipe = proc_open("$dovecotpw -s '$method'", $spec, $pipes);
  513. if (!is_resource($pipe)) {
  514. return false;
  515. }
  516. fwrite($pipes[0], $password . "\n", 1+strlen($password));
  517. usleep(1000);
  518. fwrite($pipes[0], $password . "\n", 1+strlen($password));
  519. $crypted = trim(stream_get_contents($pipes[1]), "\n");
  520. fclose($pipes[0]);
  521. fclose($pipes[1]);
  522. proc_close($pipe);
  523. if (!preg_match('/^\{' . $method . '\}/', $crypted)) {
  524. return false;
  525. }
  526. if (!$default) {
  527. $prefixed = (bool) $rcmail->config->get('password_dovecotpw_with_method');
  528. }
  529. if (!$prefixed) {
  530. $crypted = trim(str_replace('{' . $method . '}', '', $crypted));
  531. }
  532. $prefixed = false;
  533. break;
  534. case 'hash': // deprecated
  535. if (!extension_loaded('hash')) {
  536. rcube::raise_error(array(
  537. 'code' => 600, 'file' => __FILE__, 'line' => __LINE__,
  538. 'message' => "Password plugin: 'hash' extension not loaded!"
  539. ), true, true);
  540. }
  541. if (!($hash_algo = strtolower($rcmail->config->get('password_hash_algorithm')))) {
  542. $hash_algo = 'sha1';
  543. }
  544. $crypted = hash($hash_algo, $password);
  545. if ($rcmail->config->get('password_hash_base64')) {
  546. $crypted = base64_encode(pack('H*', $crypted));
  547. }
  548. break;
  549. case 'clear':
  550. $crypted = $password;
  551. }
  552. if ($crypted === null || $crypted === false) {
  553. return false;
  554. }
  555. if ($prefixed && $prefixed !== true) {
  556. $prefix = $prefixed;
  557. $prefixed = true;
  558. }
  559. if ($prefixed === true && $prefix) {
  560. $crypted = $prefix . $crypted;
  561. }
  562. return $crypted;
  563. }
  564. }