]> git.donarmstrong.com Git - roundcube.git/blob - plugins/password/drivers/sql.php
Imported Upstream version 0.7
[roundcube.git] / plugins / password / drivers / sql.php
1 <?php
2
3 /**
4  * SQL Password Driver
5  *
6  * Driver for passwords stored in SQL database
7  *
8  * @version 1.4
9  * @author Aleksander 'A.L.E.C' Machniak <alec@alec.pl>
10  *
11  */
12
13 function password_save($curpass, $passwd)
14 {
15     $rcmail = rcmail::get_instance();
16
17     if (!($sql = $rcmail->config->get('password_query')))
18         $sql = 'SELECT update_passwd(%c, %u)';
19
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';
26
27         $db = new rcube_mdb2($dsn, '', FALSE);
28         $db->set_debug((bool)$rcmail->config->get('sql_debug'));
29         $db->db_connect('w');
30     } else {
31         $db = $rcmail->get_dbh();
32     }
33
34     if ($err = $db->is_error())
35         return PASSWORD_ERROR;
36
37     // crypted password
38     if (strpos($sql, '%c') !== FALSE) {
39         $salt = '';
40         if (CRYPT_MD5) {
41             // Always use eight salt characters for MD5 (#1488136)
42             $len = 8;
43         } else if (CRYPT_STD_DES) {
44             $len = 2;
45         } else {
46             return PASSWORD_CRYPT_ERROR;
47         }
48
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)];
53         }
54
55         $sql = str_replace('%c',  $db->quote(crypt($passwd, CRYPT_MD5 ? '$1$'.$salt.'$' : $salt)), $sql);
56     }
57
58     // dovecotpw
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')))
63             $method = 'CRAM-MD5';
64
65         // use common temp dir
66         $tmp_dir = $rcmail->config->get('temp_dir');
67         $tmpfile = tempnam($tmp_dir, 'roundcube-');
68
69         $pipe = popen("$dovecotpw -s '$method' > '$tmpfile'", "w");
70         if (!$pipe) {
71             unlink($tmpfile);
72             return PASSWORD_CRYPT_ERROR;
73         }
74         else {
75             fwrite($pipe, $passwd . "\n", 1+strlen($passwd)); usleep(1000);
76             fwrite($pipe, $passwd . "\n", 1+strlen($passwd));
77             pclose($pipe);
78             $newpass = trim(file_get_contents($tmpfile), "\n");
79             if (!preg_match('/^\{' . $method . '\}/', $newpass)) {
80                 return PASSWORD_CRYPT_ERROR;
81             }
82             if (!$rcmail->config->get('password_dovecotpw_with_method'))
83                 $newpass = trim(str_replace('{' . $method . '}', '', $newpass));
84             unlink($tmpfile);
85         }
86         $sql = str_replace('%D', $db->quote($newpass), $sql);
87     }
88
89     // hashed passwords
90     if (preg_match('/%[n|q]/', $sql)) {
91
92             if (!extension_loaded('hash')) {
93                 raise_error(array(
94                     'code' => 600,
95                         'type' => 'php',
96                         'file' => __FILE__, 'line' => __LINE__,
97                         'message' => "Password plugin: 'hash' extension not loaded!"
98                     ), true, false);
99
100                 return PASSWORD_ERROR;
101             }
102
103             if (!($hash_algo = strtolower($rcmail->config->get('password_hash_algorithm'))))
104             $hash_algo = 'sha1';
105
106             $hash_passwd = hash($hash_algo, $passwd);
107         $hash_curpass = hash($hash_algo, $curpass);
108
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));
112         }
113
114             $sql = str_replace('%n', $db->quote($hash_passwd, 'text'), $sql);
115             $sql = str_replace('%q', $db->quote($hash_curpass, 'text'), $sql);
116     }
117
118     // Handle clear text passwords securely (#1487034)
119     $sql_vars = array();
120     if (preg_match_all('/%[p|o]/', $sql, $m)) {
121         foreach ($m[0] as $var) {
122             if ($var == '%p') {
123                 $sql = preg_replace('/%p/', '?', $sql, 1);
124                 $sql_vars[] = (string) $passwd;
125             }
126             else { // %o
127                 $sql = preg_replace('/%o/', '?', $sql, 1);
128                 $sql_vars[] = (string) $curpass;
129             }
130         }
131     }
132
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'];
137
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);
143     }
144     else {
145         $domain_part = rcube_idn_to_utf8($domain_part);
146         $username    = rcube_idn_to_utf8($username);
147         $host        = rcube_idn_to_utf8($host);
148     }
149
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);
155
156     $res = $db->query($sql, $sql_vars);
157
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;
162             } else {
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?
168             }
169     }
170
171     return PASSWORD_ERROR;
172 }
173
174 ?>