Adding template for error box.
[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 *
47ccfad4 10 * @copyright &copy; 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
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
381ba319 133 * to password information is deprecated. Saved password information is
134 * available only to next executed SquirrelMail script. If your script needs
135 * access to saved password after sqauth_save_password() call, use returned
136 * OTP encrypted key.
f7948940 137 * @param string $pass password
381ba319 138 * @return string password encrypted with OTP. In case script wants to access
139 * password information before reloading page.
f7948940 140 * @since 1.5.1
141 */
142function sqauth_save_password($pass) {
143 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
144
145 $onetimepad = OneTimePadCreate(strlen($pass));
146 sqsession_register($onetimepad,'onetimepad');
147 $key = OneTimePadEncrypt($pass, $onetimepad);
381ba319 148 sqsetcookie('key', $key, false, $base_uri);
149 return $key;
f7948940 150}
151
8b52a4ca 152/**
153 * Given the challenge from the server, supply the response using cram-md5 (See
154 * RFC 2195 for details)
155 *
156 * @param string $username User ID
157 * @param string $password User password supplied by User
158 * @param string $challenge The challenge supplied by the server
159 * @return string The response to be sent to the IMAP server
f7948940 160 * @since 1.4.0
8b52a4ca 161 */
47a29326 162function cram_md5_response ($username,$password,$challenge) {
8b52a4ca 163 $challenge=base64_decode($challenge);
164 $hash=bin2hex(hmac_md5($challenge,$password));
165 $response=base64_encode($username . " " . $hash) . "\r\n";
166 return $response;
47a29326 167}
168
8b52a4ca 169/**
170 * Return Digest-MD5 response.
171 * Given the challenge from the server, calculate and return the
172 * response-string for digest-md5 authentication. (See RFC 2831 for more
173 * details)
174 *
175 * @param string $username User ID
176 * @param string $password User password supplied by User
177 * @param string $challenge The challenge supplied by the server
178 * @param string $service The service name, usually 'imap'; it is used to
179 * define the digest-uri.
180 * @param string $host The host name, usually the server's FQDN; it is used to
181 * define the digest-uri.
182 * @return string The response to be sent to the IMAP server
f7948940 183 * @since 1.4.0
8b52a4ca 184 */
47a29326 185function digest_md5_response ($username,$password,$challenge,$service,$host) {
ea321915 186 $result=digest_md5_parse_challenge($challenge);
62f7daa5 187
ea321915 188 // verify server supports qop=auth
189 // $qop = explode(",",$result['qop']);
190 //if (!in_array("auth",$qop)) {
47a29326 191 // rfc2831: client MUST fail if no qop methods supported
ea321915 192 // return false;
193 //}
194 $cnonce = base64_encode(bin2hex(hmac_md5(microtime())));
195 $ncount = "00000001";
196
197 /* This can be auth (authentication only), auth-int (integrity protection), or
198 auth-conf (confidentiality protection). Right now only auth is supported.
199 DO NOT CHANGE THIS VALUE */
200 $qop_value = "auth";
201
202 $digest_uri_value = $service . '/' . $host;
203
204 // build the $response_value
205 //FIXME This will probably break badly if a server sends more than one realm
206 $string_a1 = utf8_encode($username).":";
207 $string_a1 .= utf8_encode($result['realm']).":";
208 $string_a1 .= utf8_encode($password);
209 $string_a1 = hmac_md5($string_a1);
210 $A1 = $string_a1 . ":" . $result['nonce'] . ":" . $cnonce;
211 $A1 = bin2hex(hmac_md5($A1));
212 $A2 = "AUTHENTICATE:$digest_uri_value";
213 // If qop is auth-int or auth-conf, A2 gets a little extra
214 if ($qop_value != 'auth') {
215 $A2 .= ':00000000000000000000000000000000';
216 }
217 $A2 = bin2hex(hmac_md5($A2));
218
219 $string_response = $result['nonce'] . ':' . $ncount . ':' . $cnonce . ':' . $qop_value;
220 $response_value = bin2hex(hmac_md5($A1.":".$string_response.":".$A2));
221
222 $reply = 'charset=utf-8,username="' . $username . '",realm="' . $result["realm"] . '",';
223 $reply .= 'nonce="' . $result['nonce'] . '",nc=' . $ncount . ',cnonce="' . $cnonce . '",';
224 $reply .= "digest-uri=\"$digest_uri_value\",response=$response_value";
225 $reply .= ',qop=' . $qop_value;
226 $reply = base64_encode($reply);
227 return $reply . "\r\n";
62f7daa5 228
47a29326 229}
230
8b52a4ca 231/**
232 * Parse Digest-MD5 challenge.
233 * This function parses the challenge sent during DIGEST-MD5 authentication and
234 * returns an array. See the RFC for details on what's in the challenge string.
235 *
236 * @param string $challenge Digest-MD5 Challenge
237 * @return array Digest-MD5 challenge decoded data
f7948940 238 * @since 1.4.0
8b52a4ca 239 */
47a29326 240function digest_md5_parse_challenge($challenge) {
ea321915 241 $challenge=base64_decode($challenge);
242 while (isset($challenge)) {
243 if ($challenge{0} == ',') { // First char is a comma, must not be 1st time through loop
244 $challenge=substr($challenge,1);
245 }
246 $key=explode('=',$challenge,2);
247 $challenge=$key[1];
248 $key=$key[0];
249 if ($challenge{0} == '"') {
250 // We're in a quoted value
251 // Drop the first quote, since we don't care about it
252 $challenge=substr($challenge,1);
253 // Now explode() to the next quote, which is the end of our value
254 $val=explode('"',$challenge,2);
255 $challenge=$val[1]; // The rest of the challenge, work on it in next iteration of loop
256 $value=explode(',',$val[0]);
257 // Now, for those quoted values that are only 1 piece..
258 if (sizeof($value) == 1) {
259 $value=$value[0]; // Convert to non-array
260 }
261 } else {
262 // We're in a "simple" value - explode to next comma
263 $val=explode(',',$challenge,2);
264 if (isset($val[1])) {
265 $challenge=$val[1];
266 } else {
267 unset($challenge);
268 }
269 $value=$val[0];
270 }
271 $parsed["$key"]=$value;
272 } // End of while loop
273 return $parsed;
47a29326 274}
275
8b52a4ca 276/**
277 * Creates a HMAC digest that can be used for auth purposes
278 * See RFCs 2104, 2617, 2831
279 * Uses mhash() extension if available
280 *
281 * @param string $data Data to apply hash function to.
282 * @param string $key Optional key, which, if supplied, will be used to
283 * calculate data's HMAC.
284 * @return string HMAC Digest string
f7948940 285 * @since 1.4.0
8b52a4ca 286 */
1c6d997a 287function hmac_md5($data, $key='') {
639c7164 288 if (extension_loaded('mhash')) {
ea321915 289 if ($key== '') {
290 $mhash=mhash(MHASH_MD5,$data);
291 } else {
292 $mhash=mhash(MHASH_MD5,$data,$key);
293 }
294 return $mhash;
639c7164 295 }
296 if (!$key) {
ea321915 297 return pack('H*',md5($data));
639c7164 298 }
299 $key = str_pad($key,64,chr(0x00));
300 if (strlen($key) > 64) {
301 $key = pack("H*",md5($key));
302 }
303 $k_ipad = $key ^ str_repeat(chr(0x36), 64) ;
304 $k_opad = $key ^ str_repeat(chr(0x5c), 64) ;
1c6d997a 305 /* Heh, let's get recursive. */
306 $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) );
639c7164 307 return $hmac;
308}
309
62f7daa5 310/**
9bd3b1e6 311 * Fillin user and password based on SMTP auth settings.
312 *
9bd3b1e6 313 * @param string $user Reference to SMTP username
314 * @param string $pass Reference to SMTP password (unencrypted)
f7948940 315 * @since 1.5.0
9bd3b1e6 316 */
317function get_smtp_user(&$user, &$pass) {
62f7daa5 318 global $username, $smtp_auth_mech,
9bd3b1e6 319 $smtp_sitewide_user, $smtp_sitewide_pass;
320
321 if ($smtp_auth_mech == 'none') {
322 $user = '';
323 $pass = '';
324 } elseif ( isset($smtp_sitewide_user) && isset($smtp_sitewide_pass) ) {
325 $user = $smtp_sitewide_user;
326 $pass = $smtp_sitewide_pass;
327 } else {
9bd3b1e6 328 $user = $username;
f7948940 329 $pass = sqauth_read_password();
9bd3b1e6 330 }
331}
332
f8a1ed5a 333?>