6 * Contains functions used to do authentication.
8 * @copyright © 1999-2005 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
14 /** Put in a safety net here, in case a naughty admin didn't run conf.pl when they upgraded */
16 if (! isset($smtp_auth_mech)) {
17 $smtp_auth_mech = 'none';
20 if (! isset($imap_auth_mech)) {
21 $imap_auth_mech = 'login';
24 if (! isset($use_imap_tls)) {
25 $use_imap_tls = false;
28 if (! isset($use_smtp_tls)) {
29 $use_smtp_tls = false;
33 * Check if user has previously logged in to the SquirrelMail session. If user
34 * has not logged in, execution will stop inside this function.
36 * @return int A positive value is returned if user has previously logged in
39 function is_logged_in() {
41 if ( sqsession_is_registered('user_is_logged_in') ) {
44 global $PHP_SELF, $session_expired_post,
45 $session_expired_location, $squirrelmail_language;
47 // First we store some information in the new session to prevent
50 $session_expired_post = $_POST;
51 $session_expired_location = $PHP_SELF;
52 if (!sqsession_is_registered('session_expired_post')) {
53 sqsession_register($session_expired_post,'session_expired_post');
55 if (!sqsession_is_registered('session_expired_location')) {
56 sqsession_register($session_expired_location,'session_expired_location');
59 // signout page will deal with users who aren't logged
60 // in on its own; don't show error here
62 if (strpos($PHP_SELF, 'signout.php') !== FALSE) {
66 include_once( SM_PATH
. 'functions/display_messages.php' );
67 set_up_language($squirrelmail_language, true);
68 logout_error( _("You must be logged in to access this page.") );
74 * Given the challenge from the server, supply the response using cram-md5 (See
75 * RFC 2195 for details)
77 * @param string $username User ID
78 * @param string $password User password supplied by User
79 * @param string $challenge The challenge supplied by the server
80 * @return string The response to be sent to the IMAP server
83 function cram_md5_response ($username,$password,$challenge) {
84 $challenge=base64_decode($challenge);
85 $hash=bin2hex(hmac_md5($challenge,$password));
86 $response=base64_encode($username . " " . $hash) . "\r\n";
91 * Return Digest-MD5 response.
92 * Given the challenge from the server, calculate and return the
93 * response-string for digest-md5 authentication. (See RFC 2831 for more
96 * @param string $username User ID
97 * @param string $password User password supplied by User
98 * @param string $challenge The challenge supplied by the server
99 * @param string $service The service name, usually 'imap'; it is used to
100 * define the digest-uri.
101 * @param string $host The host name, usually the server's FQDN; it is used to
102 * define the digest-uri.
103 * @return string The response to be sent to the IMAP server
105 function digest_md5_response ($username,$password,$challenge,$service,$host) {
106 $result=digest_md5_parse_challenge($challenge);
108 // verify server supports qop=auth
109 // $qop = explode(",",$result['qop']);
110 //if (!in_array("auth",$qop)) {
111 // rfc2831: client MUST fail if no qop methods supported
114 $cnonce = base64_encode(bin2hex(hmac_md5(microtime())));
115 $ncount = "00000001";
117 /* This can be auth (authentication only), auth-int (integrity protection), or
118 auth-conf (confidentiality protection). Right now only auth is supported.
119 DO NOT CHANGE THIS VALUE */
122 $digest_uri_value = $service . '/' . $host;
124 // build the $response_value
125 //FIXME This will probably break badly if a server sends more than one realm
126 $string_a1 = utf8_encode($username).":";
127 $string_a1 .= utf8_encode($result['realm']).":";
128 $string_a1 .= utf8_encode($password);
129 $string_a1 = hmac_md5($string_a1);
130 $A1 = $string_a1 . ":" . $result['nonce'] . ":" . $cnonce;
131 $A1 = bin2hex(hmac_md5($A1));
132 $A2 = "AUTHENTICATE:$digest_uri_value";
133 // If qop is auth-int or auth-conf, A2 gets a little extra
134 if ($qop_value != 'auth') {
135 $A2 .= ':00000000000000000000000000000000';
137 $A2 = bin2hex(hmac_md5($A2));
139 $string_response = $result['nonce'] . ':' . $ncount . ':' . $cnonce . ':' . $qop_value;
140 $response_value = bin2hex(hmac_md5($A1.":".$string_response.":".$A2));
142 $reply = 'charset=utf-8,username="' . $username . '",realm="' . $result["realm"] . '",';
143 $reply .= 'nonce="' . $result['nonce'] . '",nc=' . $ncount . ',cnonce="' . $cnonce . '",';
144 $reply .= "digest-uri=\"$digest_uri_value\",response=$response_value";
145 $reply .= ',qop=' . $qop_value;
146 $reply = base64_encode($reply);
147 return $reply . "\r\n";
152 * Parse Digest-MD5 challenge.
153 * This function parses the challenge sent during DIGEST-MD5 authentication and
154 * returns an array. See the RFC for details on what's in the challenge string.
156 * @param string $challenge Digest-MD5 Challenge
157 * @return array Digest-MD5 challenge decoded data
159 function digest_md5_parse_challenge($challenge) {
160 $challenge=base64_decode($challenge);
161 while (isset($challenge)) {
162 if ($challenge{0} == ',') { // First char is a comma, must not be 1st time through loop
163 $challenge=substr($challenge,1);
165 $key=explode('=',$challenge,2);
168 if ($challenge{0} == '"') {
169 // We're in a quoted value
170 // Drop the first quote, since we don't care about it
171 $challenge=substr($challenge,1);
172 // Now explode() to the next quote, which is the end of our value
173 $val=explode('"',$challenge,2);
174 $challenge=$val[1]; // The rest of the challenge, work on it in next iteration of loop
175 $value=explode(',',$val[0]);
176 // Now, for those quoted values that are only 1 piece..
177 if (sizeof($value) == 1) {
178 $value=$value[0]; // Convert to non-array
181 // We're in a "simple" value - explode to next comma
182 $val=explode(',',$challenge,2);
183 if (isset($val[1])) {
190 $parsed["$key"]=$value;
191 } // End of while loop
196 * Creates a HMAC digest that can be used for auth purposes
197 * See RFCs 2104, 2617, 2831
198 * Uses mhash() extension if available
200 * @param string $data Data to apply hash function to.
201 * @param string $key Optional key, which, if supplied, will be used to
202 * calculate data's HMAC.
203 * @return string HMAC Digest string
205 function hmac_md5($data, $key='') {
206 if (extension_loaded('mhash')) {
208 $mhash=mhash(MHASH_MD5
,$data);
210 $mhash=mhash(MHASH_MD5
,$data,$key);
215 return pack('H*',md5($data));
217 $key = str_pad($key,64,chr(0x00));
218 if (strlen($key) > 64) {
219 $key = pack("H*",md5($key));
221 $k_ipad = $key ^
str_repeat(chr(0x36), 64) ;
222 $k_opad = $key ^
str_repeat(chr(0x5c), 64) ;
223 /* Heh, let's get recursive. */
224 $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) );
229 * Fillin user and password based on SMTP auth settings.
231 * @param string $user Reference to SMTP username
232 * @param string $pass Reference to SMTP password (unencrypted)
234 function get_smtp_user(&$user, &$pass) {
235 global $username, $smtp_auth_mech,
236 $smtp_sitewide_user, $smtp_sitewide_pass;
238 if ($smtp_auth_mech == 'none') {
241 } elseif ( isset($smtp_sitewide_user) && isset($smtp_sitewide_pass) ) {
242 $user = $smtp_sitewide_user;
243 $pass = $smtp_sitewide_pass;
245 global $key, $onetimepad;
247 $pass = OneTimePadDecrypt($key, $onetimepad);