6 * Driver for passwords stored in SQL database
9 * @author Aleksander 'A.L.E.C' Machniak <alec@alec.pl>
13 function password_save($curpass, $passwd)
15 $rcmail = rcmail::get_instance();
17 if (!($sql = $rcmail->config->get('password_query')))
18 $sql = 'SELECT update_passwd(%c, %u)';
20 if ($dsn = $rcmail->config->get('password_db_dsn')) {
21 // #1486067: enable new_link option
22 if (is_array($dsn) && empty($dsn['new_link']))
23 $dsn['new_link'] = true;
24 else if (!is_array($dsn) && !preg_match('/\?new_link=true/', $dsn))
25 $dsn .= '?new_link=true';
27 $db = new rcube_mdb2($dsn, '', FALSE);
28 $db->set_debug((bool)$rcmail->config->get('sql_debug'));
31 $db = $rcmail->get_dbh();
34 if ($err = $db->is_error())
35 return PASSWORD_ERROR;
38 if (strpos($sql, '%c') !== FALSE) {
41 // Always use eight salt characters for MD5 (#1488136)
43 } else if (CRYPT_STD_DES) {
46 return PASSWORD_CRYPT_ERROR;
49 //Restrict the character set used as salt (#1488136)
50 $seedchars = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
51 for ($i = 0; $i < $len ; $i++) {
52 $salt .= $seedchars[rand(0, 63)];
55 $sql = str_replace('%c', $db->quote(crypt($passwd, CRYPT_MD5 ? '$1$'.$salt.'$' : $salt)), $sql);
59 if (strpos($sql, '%D') !== FALSE) {
60 if (!($dovecotpw = $rcmail->config->get('password_dovecotpw')))
61 $dovecotpw = 'dovecotpw';
62 if (!($method = $rcmail->config->get('password_dovecotpw_method')))
65 // use common temp dir
66 $tmp_dir = $rcmail->config->get('temp_dir');
67 $tmpfile = tempnam($tmp_dir, 'roundcube-');
69 $pipe = popen("$dovecotpw -s '$method' > '$tmpfile'", "w");
72 return PASSWORD_CRYPT_ERROR;
75 fwrite($pipe, $passwd . "\n", 1+strlen($passwd)); usleep(1000);
76 fwrite($pipe, $passwd . "\n", 1+strlen($passwd));
78 $newpass = trim(file_get_contents($tmpfile), "\n");
79 if (!preg_match('/^\{' . $method . '\}/', $newpass)) {
80 return PASSWORD_CRYPT_ERROR;
82 if (!$rcmail->config->get('password_dovecotpw_with_method'))
83 $newpass = trim(str_replace('{' . $method . '}', '', $newpass));
86 $sql = str_replace('%D', $db->quote($newpass), $sql);
90 if (preg_match('/%[n|q]/', $sql)) {
92 if (!extension_loaded('hash')) {
96 'file' => __FILE__, 'line' => __LINE__,
97 'message' => "Password plugin: 'hash' extension not loaded!"
100 return PASSWORD_ERROR;
103 if (!($hash_algo = strtolower($rcmail->config->get('password_hash_algorithm'))))
106 $hash_passwd = hash($hash_algo, $passwd);
107 $hash_curpass = hash($hash_algo, $curpass);
109 if ($rcmail->config->get('password_hash_base64')) {
110 $hash_passwd = base64_encode(pack('H*', $hash_passwd));
111 $hash_curpass = base64_encode(pack('H*', $hash_curpass));
114 $sql = str_replace('%n', $db->quote($hash_passwd, 'text'), $sql);
115 $sql = str_replace('%q', $db->quote($hash_curpass, 'text'), $sql);
118 // Handle clear text passwords securely (#1487034)
120 if (preg_match_all('/%[p|o]/', $sql, $m)) {
121 foreach ($m[0] as $var) {
123 $sql = preg_replace('/%p/', '?', $sql, 1);
124 $sql_vars[] = (string) $passwd;
127 $sql = preg_replace('/%o/', '?', $sql, 1);
128 $sql_vars[] = (string) $curpass;
133 $local_part = $rcmail->user->get_username('local');
134 $domain_part = $rcmail->user->get_username('domain');
135 $username = $_SESSION['username'];
136 $host = $_SESSION['imap_host'];
138 // convert domains to/from punnycode
139 if ($rcmail->config->get('password_idn_ascii')) {
140 $domain_part = rcube_idn_to_ascii($domain_part);
141 $username = rcube_idn_to_ascii($username);
142 $host = rcube_idn_to_ascii($host);
145 $domain_part = rcube_idn_to_utf8($domain_part);
146 $username = rcube_idn_to_utf8($username);
147 $host = rcube_idn_to_utf8($host);
150 // at least we should always have the local part
151 $sql = str_replace('%l', $db->quote($local_part, 'text'), $sql);
152 $sql = str_replace('%d', $db->quote($domain_part, 'text'), $sql);
153 $sql = str_replace('%u', $db->quote($username, 'text'), $sql);
154 $sql = str_replace('%h', $db->quote($host, 'text'), $sql);
156 $res = $db->query($sql, $sql_vars);
158 if (!$db->is_error()) {
159 if (strtolower(substr(trim($query),0,6))=='select') {
160 if ($result = $db->fetch_array($res))
161 return PASSWORD_SUCCESS;
163 // This is the good case: 1 row updated
164 if ($db->affected_rows($res) == 1)
165 return PASSWORD_SUCCESS;
166 // @TODO: Some queries don't affect any rows
167 // Should we assume a success if there was no error?
171 return PASSWORD_ERROR;