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