PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/password/drivers/sql.php

https://github.com/netconstructor/roundcubemail
PHP | 200 lines | 152 code | 27 blank | 21 comment | 39 complexity | e148ae27e6cba183ae3f856181172162 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SQL Password Driver
  4. *
  5. * Driver for passwords stored in SQL database
  6. *
  7. * @version 2.0
  8. * @author Aleksander 'A.L.E.C' Machniak <alec@alec.pl>
  9. *
  10. */
  11. class rcube_sql_password
  12. {
  13. function save($curpass, $passwd)
  14. {
  15. $rcmail = rcmail::get_instance();
  16. if (!($sql = $rcmail->config->get('password_query')))
  17. $sql = 'SELECT update_passwd(%c, %u)';
  18. if ($dsn = $rcmail->config->get('password_db_dsn')) {
  19. // #1486067: enable new_link option
  20. if (is_array($dsn) && empty($dsn['new_link']))
  21. $dsn['new_link'] = true;
  22. else if (!is_array($dsn) && !preg_match('/\?new_link=true/', $dsn))
  23. $dsn .= '?new_link=true';
  24. $db = rcube_db::factory($dsn, '', false);
  25. $db->set_debug((bool)$rcmail->config->get('sql_debug'));
  26. $db->db_connect('w');
  27. }
  28. else {
  29. $db = $rcmail->get_dbh();
  30. }
  31. if ($err = $db->is_error())
  32. return PASSWORD_ERROR;
  33. // crypted password
  34. if (strpos($sql, '%c') !== FALSE) {
  35. $salt = '';
  36. if (!($crypt_hash = $rcmail->config->get('password_crypt_hash')))
  37. {
  38. if (CRYPT_MD5)
  39. $crypt_hash = 'md5';
  40. else if (CRYPT_STD_DES)
  41. $crypt_hash = 'des';
  42. }
  43. switch ($crypt_hash)
  44. {
  45. case 'md5':
  46. $len = 8;
  47. $salt_hashindicator = '$1$';
  48. break;
  49. case 'des':
  50. $len = 2;
  51. break;
  52. case 'blowfish':
  53. $len = 22;
  54. $salt_hashindicator = '$2a$';
  55. break;
  56. case 'sha256':
  57. $len = 16;
  58. $salt_hashindicator = '$5$';
  59. break;
  60. case 'sha512':
  61. $len = 16;
  62. $salt_hashindicator = '$6$';
  63. break;
  64. default:
  65. return PASSWORD_CRYPT_ERROR;
  66. }
  67. //Restrict the character set used as salt (#1488136)
  68. $seedchars = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  69. for ($i = 0; $i < $len ; $i++) {
  70. $salt .= $seedchars[rand(0, 63)];
  71. }
  72. $sql = str_replace('%c', $db->quote(crypt($passwd, $salt_hashindicator ? $salt_hashindicator .$salt.'$' : $salt)), $sql);
  73. }
  74. // dovecotpw
  75. if (strpos($sql, '%D') !== FALSE) {
  76. if (!($dovecotpw = $rcmail->config->get('password_dovecotpw')))
  77. $dovecotpw = 'dovecotpw';
  78. if (!($method = $rcmail->config->get('password_dovecotpw_method')))
  79. $method = 'CRAM-MD5';
  80. // use common temp dir
  81. $tmp_dir = $rcmail->config->get('temp_dir');
  82. $tmpfile = tempnam($tmp_dir, 'roundcube-');
  83. $pipe = popen("$dovecotpw -s '$method' > '$tmpfile'", "w");
  84. if (!$pipe) {
  85. unlink($tmpfile);
  86. return PASSWORD_CRYPT_ERROR;
  87. }
  88. else {
  89. fwrite($pipe, $passwd . "\n", 1+strlen($passwd)); usleep(1000);
  90. fwrite($pipe, $passwd . "\n", 1+strlen($passwd));
  91. pclose($pipe);
  92. $newpass = trim(file_get_contents($tmpfile), "\n");
  93. if (!preg_match('/^\{' . $method . '\}/', $newpass)) {
  94. return PASSWORD_CRYPT_ERROR;
  95. }
  96. if (!$rcmail->config->get('password_dovecotpw_with_method'))
  97. $newpass = trim(str_replace('{' . $method . '}', '', $newpass));
  98. unlink($tmpfile);
  99. }
  100. $sql = str_replace('%D', $db->quote($newpass), $sql);
  101. }
  102. // hashed passwords
  103. if (preg_match('/%[n|q]/', $sql)) {
  104. if (!extension_loaded('hash')) {
  105. raise_error(array(
  106. 'code' => 600,
  107. 'type' => 'php',
  108. 'file' => __FILE__, 'line' => __LINE__,
  109. 'message' => "Password plugin: 'hash' extension not loaded!"
  110. ), true, false);
  111. return PASSWORD_ERROR;
  112. }
  113. if (!($hash_algo = strtolower($rcmail->config->get('password_hash_algorithm'))))
  114. $hash_algo = 'sha1';
  115. $hash_passwd = hash($hash_algo, $passwd);
  116. $hash_curpass = hash($hash_algo, $curpass);
  117. if ($rcmail->config->get('password_hash_base64')) {
  118. $hash_passwd = base64_encode(pack('H*', $hash_passwd));
  119. $hash_curpass = base64_encode(pack('H*', $hash_curpass));
  120. }
  121. $sql = str_replace('%n', $db->quote($hash_passwd, 'text'), $sql);
  122. $sql = str_replace('%q', $db->quote($hash_curpass, 'text'), $sql);
  123. }
  124. // Handle clear text passwords securely (#1487034)
  125. $sql_vars = array();
  126. if (preg_match_all('/%[p|o]/', $sql, $m)) {
  127. foreach ($m[0] as $var) {
  128. if ($var == '%p') {
  129. $sql = preg_replace('/%p/', '?', $sql, 1);
  130. $sql_vars[] = (string) $passwd;
  131. }
  132. else { // %o
  133. $sql = preg_replace('/%o/', '?', $sql, 1);
  134. $sql_vars[] = (string) $curpass;
  135. }
  136. }
  137. }
  138. $local_part = $rcmail->user->get_username('local');
  139. $domain_part = $rcmail->user->get_username('domain');
  140. $username = $_SESSION['username'];
  141. $host = $_SESSION['imap_host'];
  142. // convert domains to/from punnycode
  143. if ($rcmail->config->get('password_idn_ascii')) {
  144. $domain_part = rcube_idn_to_ascii($domain_part);
  145. $username = rcube_idn_to_ascii($username);
  146. $host = rcube_idn_to_ascii($host);
  147. }
  148. else {
  149. $domain_part = rcube_idn_to_utf8($domain_part);
  150. $username = rcube_idn_to_utf8($username);
  151. $host = rcube_idn_to_utf8($host);
  152. }
  153. // at least we should always have the local part
  154. $sql = str_replace('%l', $db->quote($local_part, 'text'), $sql);
  155. $sql = str_replace('%d', $db->quote($domain_part, 'text'), $sql);
  156. $sql = str_replace('%u', $db->quote($username, 'text'), $sql);
  157. $sql = str_replace('%h', $db->quote($host, 'text'), $sql);
  158. $res = $db->query($sql, $sql_vars);
  159. if (!$db->is_error()) {
  160. if (strtolower(substr(trim($query),0,6))=='select') {
  161. if ($result = $db->fetch_array($res))
  162. return PASSWORD_SUCCESS;
  163. } else {
  164. // This is the good case: 1 row updated
  165. if ($db->affected_rows($res) == 1)
  166. return PASSWORD_SUCCESS;
  167. // @TODO: Some queries don't affect any rows
  168. // Should we assume a success if there was no error?
  169. }
  170. }
  171. return PASSWORD_ERROR;
  172. }
  173. }