3c13b9fb |
1 | <?php |
2 | |
35586184 |
3 | /** |
4 | * auth.php |
5 | * |
867fed37 |
6 | * Contains functions used to do authentication. |
7 | * |
8 | * Dependencies: |
9 | * functions/global.php |
10 | * functions/strings.php. |
35586184 |
11 | * |
4b5049de |
12 | * @copyright © 1999-2007 The SquirrelMail Project Team |
4b4abf93 |
13 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
14 | * @version $Id$ |
d6c32258 |
15 | * @package squirrelmail |
35586184 |
16 | */ |
17 | |
3c13b9fb |
18 | |
f7948940 |
19 | /** |
cffbdf90 |
20 | * Detect whether user is logged in |
202bcbcc |
21 | * |
22 | * Function is similar to is_logged_in() function. If user is logged in, function |
f7948940 |
23 | * returns true. If user is not logged in or session is expired, function saves $_POST |
de219480 |
24 | * and PAGE_NAME in session and returns false. POST information is saved in |
25 | * 'session_expired_post' variable, PAGE_NAME is saved in 'session_expired_location'. |
f7948940 |
26 | * |
202bcbcc |
27 | * Script that uses this function instead of is_logged_in() function, must handle user |
f7948940 |
28 | * level messages. |
29 | * @return boolean |
30 | * @since 1.5.1 |
31 | */ |
32 | function sqauth_is_logged_in() { |
33 | if ( sqsession_is_registered('user_is_logged_in') ) { |
34 | return true; |
cffbdf90 |
35 | } |
c6f28eb1 |
36 | |
f74ec578 |
37 | // First we store some information in the new session to prevent |
38 | // information-loss. |
39 | $session_expired_post = $_POST; |
40 | if (defined('PAGE_NAME')) |
41 | $session_expired_location = PAGE_NAME; |
42 | else |
43 | $session_expired_location = ''; |
44 | |
45 | if (!sqsession_is_registered('session_expired_post')) { |
46 | sqsession_register($session_expired_post,'session_expired_post'); |
47 | } |
48 | if (!sqsession_is_registered('session_expired_location')) { |
49 | sqsession_register($session_expired_location,'session_expired_location'); |
50 | } |
51 | |
52 | session_write_close(); |
53 | |
54 | return false; |
f7948940 |
55 | } |
56 | |
57 | /** |
58 | * Reads and decodes stored user password information |
59 | * |
60 | * Direct access to password information is deprecated. |
61 | * @return string password in plain text |
62 | * @since 1.5.1 |
63 | */ |
64 | function sqauth_read_password() { |
65 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
66 | sqgetGlobalVar('onetimepad', $onetimepad,SQ_SESSION); |
67 | |
68 | return OneTimePadDecrypt($key, $onetimepad); |
69 | } |
70 | |
71 | /** |
72 | * Saves or updates user password information |
202bcbcc |
73 | * |
45d5ec97 |
74 | * This function is used to update the password information that |
75 | * SquirrelMail stores in the existing PHP session. It does NOT |
76 | * modify the password stored in the authentication system used |
77 | * by the IMAP server. |
78 | * |
79 | * This function must be called before any html output is started. |
80 | * Direct access to password information is deprecated. The saved |
81 | * password information is available only to the SquirrelMail script |
82 | * that is called/executed AFTER the current one. If your script |
83 | * needs access to the saved password after a sqauth_save_password() |
84 | * call, use the returned OTP encrypted key. |
f7948940 |
85 | * |
f7948940 |
86 | * @param string $pass password |
45d5ec97 |
87 | * |
88 | * @return string Password encrypted with OTP. In case the script |
89 | * wants to access the password information before |
90 | * the end of its execution. |
91 | * |
f7948940 |
92 | * @since 1.5.1 |
45d5ec97 |
93 | * |
f7948940 |
94 | */ |
95 | function sqauth_save_password($pass) { |
96 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
97 | |
98 | $onetimepad = OneTimePadCreate(strlen($pass)); |
99 | sqsession_register($onetimepad,'onetimepad'); |
100 | $key = OneTimePadEncrypt($pass, $onetimepad); |
381ba319 |
101 | sqsetcookie('key', $key, false, $base_uri); |
102 | return $key; |
f7948940 |
103 | } |
104 | |
8b52a4ca |
105 | /** |
106 | * Given the challenge from the server, supply the response using cram-md5 (See |
107 | * RFC 2195 for details) |
108 | * |
109 | * @param string $username User ID |
110 | * @param string $password User password supplied by User |
111 | * @param string $challenge The challenge supplied by the server |
112 | * @return string The response to be sent to the IMAP server |
f7948940 |
113 | * @since 1.4.0 |
8b52a4ca |
114 | */ |
47a29326 |
115 | function cram_md5_response ($username,$password,$challenge) { |
8b52a4ca |
116 | $challenge=base64_decode($challenge); |
117 | $hash=bin2hex(hmac_md5($challenge,$password)); |
118 | $response=base64_encode($username . " " . $hash) . "\r\n"; |
119 | return $response; |
47a29326 |
120 | } |
121 | |
8b52a4ca |
122 | /** |
123 | * Return Digest-MD5 response. |
124 | * Given the challenge from the server, calculate and return the |
125 | * response-string for digest-md5 authentication. (See RFC 2831 for more |
126 | * details) |
127 | * |
128 | * @param string $username User ID |
129 | * @param string $password User password supplied by User |
130 | * @param string $challenge The challenge supplied by the server |
131 | * @param string $service The service name, usually 'imap'; it is used to |
132 | * define the digest-uri. |
133 | * @param string $host The host name, usually the server's FQDN; it is used to |
134 | * define the digest-uri. |
b0434236 |
135 | * @param string $authz Authorization ID (since 1.5.2) |
8b52a4ca |
136 | * @return string The response to be sent to the IMAP server |
f7948940 |
137 | * @since 1.4.0 |
8b52a4ca |
138 | */ |
b0434236 |
139 | function digest_md5_response ($username,$password,$challenge,$service,$host,$authz='') { |
ea321915 |
140 | $result=digest_md5_parse_challenge($challenge); |
62f7daa5 |
141 | |
ea321915 |
142 | // verify server supports qop=auth |
143 | // $qop = explode(",",$result['qop']); |
144 | //if (!in_array("auth",$qop)) { |
47a29326 |
145 | // rfc2831: client MUST fail if no qop methods supported |
ea321915 |
146 | // return false; |
147 | //} |
148 | $cnonce = base64_encode(bin2hex(hmac_md5(microtime()))); |
149 | $ncount = "00000001"; |
150 | |
151 | /* This can be auth (authentication only), auth-int (integrity protection), or |
152 | auth-conf (confidentiality protection). Right now only auth is supported. |
153 | DO NOT CHANGE THIS VALUE */ |
154 | $qop_value = "auth"; |
155 | |
156 | $digest_uri_value = $service . '/' . $host; |
157 | |
158 | // build the $response_value |
159 | //FIXME This will probably break badly if a server sends more than one realm |
160 | $string_a1 = utf8_encode($username).":"; |
161 | $string_a1 .= utf8_encode($result['realm']).":"; |
162 | $string_a1 .= utf8_encode($password); |
163 | $string_a1 = hmac_md5($string_a1); |
164 | $A1 = $string_a1 . ":" . $result['nonce'] . ":" . $cnonce; |
31dd6e3c |
165 | if(!empty($authz)) { |
166 | $A1 .= ":" . utf8_encode($authz); |
167 | } |
ea321915 |
168 | $A1 = bin2hex(hmac_md5($A1)); |
169 | $A2 = "AUTHENTICATE:$digest_uri_value"; |
170 | // If qop is auth-int or auth-conf, A2 gets a little extra |
171 | if ($qop_value != 'auth') { |
172 | $A2 .= ':00000000000000000000000000000000'; |
173 | } |
174 | $A2 = bin2hex(hmac_md5($A2)); |
175 | |
176 | $string_response = $result['nonce'] . ':' . $ncount . ':' . $cnonce . ':' . $qop_value; |
177 | $response_value = bin2hex(hmac_md5($A1.":".$string_response.":".$A2)); |
178 | |
179 | $reply = 'charset=utf-8,username="' . $username . '",realm="' . $result["realm"] . '",'; |
180 | $reply .= 'nonce="' . $result['nonce'] . '",nc=' . $ncount . ',cnonce="' . $cnonce . '",'; |
181 | $reply .= "digest-uri=\"$digest_uri_value\",response=$response_value"; |
182 | $reply .= ',qop=' . $qop_value; |
b0434236 |
183 | if(!empty($authz)) { |
184 | $reply .= ',authzid=' . $authz; |
185 | } |
ea321915 |
186 | $reply = base64_encode($reply); |
187 | return $reply . "\r\n"; |
62f7daa5 |
188 | |
47a29326 |
189 | } |
190 | |
8b52a4ca |
191 | /** |
192 | * Parse Digest-MD5 challenge. |
193 | * This function parses the challenge sent during DIGEST-MD5 authentication and |
194 | * returns an array. See the RFC for details on what's in the challenge string. |
195 | * |
196 | * @param string $challenge Digest-MD5 Challenge |
197 | * @return array Digest-MD5 challenge decoded data |
f7948940 |
198 | * @since 1.4.0 |
8b52a4ca |
199 | */ |
47a29326 |
200 | function digest_md5_parse_challenge($challenge) { |
ea321915 |
201 | $challenge=base64_decode($challenge); |
475bcb51 |
202 | while (isset($challenge) && $challenge !== FALSE) { |
ea321915 |
203 | if ($challenge{0} == ',') { // First char is a comma, must not be 1st time through loop |
204 | $challenge=substr($challenge,1); |
205 | } |
206 | $key=explode('=',$challenge,2); |
207 | $challenge=$key[1]; |
208 | $key=$key[0]; |
209 | if ($challenge{0} == '"') { |
210 | // We're in a quoted value |
211 | // Drop the first quote, since we don't care about it |
212 | $challenge=substr($challenge,1); |
213 | // Now explode() to the next quote, which is the end of our value |
214 | $val=explode('"',$challenge,2); |
215 | $challenge=$val[1]; // The rest of the challenge, work on it in next iteration of loop |
216 | $value=explode(',',$val[0]); |
217 | // Now, for those quoted values that are only 1 piece.. |
218 | if (sizeof($value) == 1) { |
219 | $value=$value[0]; // Convert to non-array |
220 | } |
221 | } else { |
222 | // We're in a "simple" value - explode to next comma |
223 | $val=explode(',',$challenge,2); |
224 | if (isset($val[1])) { |
225 | $challenge=$val[1]; |
226 | } else { |
227 | unset($challenge); |
228 | } |
229 | $value=$val[0]; |
230 | } |
231 | $parsed["$key"]=$value; |
232 | } // End of while loop |
233 | return $parsed; |
47a29326 |
234 | } |
235 | |
8b52a4ca |
236 | /** |
237 | * Creates a HMAC digest that can be used for auth purposes |
238 | * See RFCs 2104, 2617, 2831 |
239 | * Uses mhash() extension if available |
240 | * |
241 | * @param string $data Data to apply hash function to. |
242 | * @param string $key Optional key, which, if supplied, will be used to |
243 | * calculate data's HMAC. |
244 | * @return string HMAC Digest string |
f7948940 |
245 | * @since 1.4.0 |
8b52a4ca |
246 | */ |
1c6d997a |
247 | function hmac_md5($data, $key='') { |
639c7164 |
248 | if (extension_loaded('mhash')) { |
ea321915 |
249 | if ($key== '') { |
250 | $mhash=mhash(MHASH_MD5,$data); |
251 | } else { |
252 | $mhash=mhash(MHASH_MD5,$data,$key); |
253 | } |
254 | return $mhash; |
639c7164 |
255 | } |
256 | if (!$key) { |
ea321915 |
257 | return pack('H*',md5($data)); |
639c7164 |
258 | } |
259 | $key = str_pad($key,64,chr(0x00)); |
260 | if (strlen($key) > 64) { |
261 | $key = pack("H*",md5($key)); |
262 | } |
263 | $k_ipad = $key ^ str_repeat(chr(0x36), 64) ; |
264 | $k_opad = $key ^ str_repeat(chr(0x5c), 64) ; |
1c6d997a |
265 | /* Heh, let's get recursive. */ |
266 | $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) ); |
639c7164 |
267 | return $hmac; |
268 | } |
269 | |
62f7daa5 |
270 | /** |
9bd3b1e6 |
271 | * Fillin user and password based on SMTP auth settings. |
272 | * |
9bd3b1e6 |
273 | * @param string $user Reference to SMTP username |
274 | * @param string $pass Reference to SMTP password (unencrypted) |
0940b016 |
275 | * @since 1.4.11 |
9bd3b1e6 |
276 | */ |
277 | function get_smtp_user(&$user, &$pass) { |
62f7daa5 |
278 | global $username, $smtp_auth_mech, |
9bd3b1e6 |
279 | $smtp_sitewide_user, $smtp_sitewide_pass; |
280 | |
281 | if ($smtp_auth_mech == 'none') { |
282 | $user = ''; |
283 | $pass = ''; |
db84eecd |
284 | } elseif ( isset($smtp_sitewide_user) && isset($smtp_sitewide_pass) && |
285 | !empty($smtp_sitewide_user)) { |
9bd3b1e6 |
286 | $user = $smtp_sitewide_user; |
287 | $pass = $smtp_sitewide_pass; |
288 | } else { |
9bd3b1e6 |
289 | $user = $username; |
f7948940 |
290 | $pass = sqauth_read_password(); |
9bd3b1e6 |
291 | } |
93917f92 |
292 | |
293 | // plugin authors note: override $user or $pass by |
294 | // directly changing the arguments array contents |
295 | // in your plugin e.g., $args[0] = 'new_username'; |
296 | // |
502b1f24 |
297 | $temp = array(&$user, &$pass); |
298 | do_hook('smtp_auth', $temp); |
9bd3b1e6 |
299 | } |