You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sql.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * Copyright (C) 2005-2013, The Roundcube Dev Team
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see http://www.gnu.org/licenses/.
  24. */
  25. class rcube_sql_password
  26. {
  27. function save($curpass, $passwd)
  28. {
  29. $rcmail = rcmail::get_instance();
  30. if (!($sql = $rcmail->config->get('password_query'))) {
  31. $sql = 'SELECT update_passwd(%c, %u)';
  32. }
  33. if ($dsn = $rcmail->config->get('password_db_dsn')) {
  34. $db = rcube_db::factory($dsn, '', false);
  35. $db->set_debug((bool)$rcmail->config->get('sql_debug'));
  36. }
  37. else {
  38. $db = $rcmail->get_dbh();
  39. }
  40. if ($db->is_error()) {
  41. return PASSWORD_ERROR;
  42. }
  43. // new password - default hash method
  44. if (strpos($sql, '%P') !== false) {
  45. $password = password::hash_password($passwd);
  46. if ($password === false) {
  47. return PASSWORD_CRYPT_ERROR;
  48. }
  49. $sql = str_replace('%P', $db->quote($password), $sql);
  50. }
  51. // old password - default hash method
  52. if (strpos($sql, '%O') !== false) {
  53. $password = password::hash_password($curpass);
  54. if ($password === false) {
  55. return PASSWORD_CRYPT_ERROR;
  56. }
  57. $sql = str_replace('%O', $db->quote($password), $sql);
  58. }
  59. // crypted password (deprecated, use %P)
  60. if (strpos($sql, '%c') !== false) {
  61. $password = password::hash_password($passwd, 'crypt', false);
  62. if ($password === false) {
  63. return PASSWORD_CRYPT_ERROR;
  64. }
  65. $sql = str_replace('%c', $db->quote($password), $sql);
  66. }
  67. // dovecotpw (deprecated, use %P)
  68. if (strpos($sql, '%D') !== false) {
  69. $password = password::hash_password($passwd, 'dovecot', false);
  70. if ($password === false) {
  71. return PASSWORD_CRYPT_ERROR;
  72. }
  73. $sql = str_replace('%D', $db->quote($password), $sql);
  74. }
  75. // hashed passwords (deprecated, use %P)
  76. if (strpos($sql, '%n') !== false) {
  77. $password = password::hash_password($passwd, 'hash', false);
  78. if ($password === false) {
  79. return PASSWORD_CRYPT_ERROR;
  80. }
  81. $sql = str_replace('%n', $db->quote($password, 'text'), $sql);
  82. }
  83. // hashed passwords (deprecated, use %P)
  84. if (strpos($sql, '%q') !== false) {
  85. $password = password::hash_password($curpass, 'hash', false);
  86. if ($password === false) {
  87. return PASSWORD_CRYPT_ERROR;
  88. }
  89. $sql = str_replace('%q', $db->quote($password, 'text'), $sql);
  90. }
  91. // Handle clear text passwords securely (#1487034)
  92. $sql_vars = array();
  93. if (preg_match_all('/%[p|o]/', $sql, $m)) {
  94. foreach ($m[0] as $var) {
  95. if ($var == '%p') {
  96. $sql = preg_replace('/%p/', '?', $sql, 1);
  97. $sql_vars[] = (string) $passwd;
  98. }
  99. else { // %o
  100. $sql = preg_replace('/%o/', '?', $sql, 1);
  101. $sql_vars[] = (string) $curpass;
  102. }
  103. }
  104. }
  105. $local_part = $rcmail->user->get_username('local');
  106. $domain_part = $rcmail->user->get_username('domain');
  107. $username = $_SESSION['username'];
  108. $host = $_SESSION['imap_host'];
  109. // convert domains to/from punnycode
  110. if ($rcmail->config->get('password_idn_ascii')) {
  111. $domain_part = rcube_utils::idn_to_ascii($domain_part);
  112. $username = rcube_utils::idn_to_ascii($username);
  113. $host = rcube_utils::idn_to_ascii($host);
  114. }
  115. else {
  116. $domain_part = rcube_utils::idn_to_utf8($domain_part);
  117. $username = rcube_utils::idn_to_utf8($username);
  118. $host = rcube_utils::idn_to_utf8($host);
  119. }
  120. // at least we should always have the local part
  121. $sql = str_replace('%l', $db->quote($local_part, 'text'), $sql);
  122. $sql = str_replace('%d', $db->quote($domain_part, 'text'), $sql);
  123. $sql = str_replace('%u', $db->quote($username, 'text'), $sql);
  124. $sql = str_replace('%h', $db->quote($host, 'text'), $sql);
  125. $res = $db->query($sql, $sql_vars);
  126. if (!$db->is_error()) {
  127. if (strtolower(substr(trim($sql),0,6)) == 'select') {
  128. if ($db->fetch_array($res)) {
  129. return PASSWORD_SUCCESS;
  130. }
  131. }
  132. else {
  133. // This is the good case: 1 row updated
  134. if ($db->affected_rows($res) == 1)
  135. return PASSWORD_SUCCESS;
  136. // @TODO: Some queries don't affect any rows
  137. // Should we assume a success if there was no error?
  138. }
  139. }
  140. return PASSWORD_ERROR;
  141. }
  142. }