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