document html functions
[squirrelmail.git] / functions / auth.php
CommitLineData
3c13b9fb 1<?php
2
35586184 3/**
4 * auth.php
5 *
35586184 6 * Contains functions used to do authentication.
7 *
4b4abf93 8 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 10 * @version $Id$
d6c32258 11 * @package squirrelmail
35586184 12 */
13
d6c32258 14/** Put in a safety net here, in case a naughty admin didn't run conf.pl when they upgraded */
47a29326 15
16if (! isset($smtp_auth_mech)) {
ea321915 17 $smtp_auth_mech = 'none';
47a29326 18}
19
20if (! isset($imap_auth_mech)) {
ea321915 21 $imap_auth_mech = 'login';
47a29326 22}
23
24if (! isset($use_imap_tls)) {
ea321915 25 $use_imap_tls = false;
47a29326 26}
27
28if (! isset($use_smtp_tls)) {
ea321915 29 $use_smtp_tls = false;
47a29326 30}
31
8b52a4ca 32/**
598294a7 33 * Check if user has previously logged in to the SquirrelMail session. If user
8b52a4ca 34 * has not logged in, execution will stop inside this function.
35 *
36 * @return int A positive value is returned if user has previously logged in
37 * successfully.
38 */
9be8198d 39function is_logged_in() {
2d367c68 40
d7c82551 41 if ( sqsession_is_registered('user_is_logged_in') ) {
e110c214 42 return;
4d2c9f70 43 } else {
62f7daa5 44 global $PHP_SELF, $session_expired_post,
91e0dccc 45 $session_expired_location, $squirrelmail_language;
f04cab07 46
46b0c6f4 47 // First we store some information in the new session to prevent
48 // information-loss.
49 //
91e0dccc 50 $session_expired_post = $_POST;
f04cab07 51 $session_expired_location = $PHP_SELF;
62f7daa5 52 if (!sqsession_is_registered('session_expired_post')) {
ea321915 53 sqsession_register($session_expired_post,'session_expired_post');
f04cab07 54 }
d7c82551 55 if (!sqsession_is_registered('session_expired_location')) {
ea321915 56 sqsession_register($session_expired_location,'session_expired_location');
f04cab07 57 }
46b0c6f4 58
59 // signout page will deal with users who aren't logged
60 // in on its own; don't show error here
61 //
62 if (strpos($PHP_SELF, 'signout.php') !== FALSE) {
63 return;
64 }
65
014bba80 66 include_once( SM_PATH . 'functions/display_messages.php' );
ab4e0c48 67 set_up_language($squirrelmail_language, true);
9be8198d 68 logout_error( _("You must be logged in to access this page.") );
4d2c9f70 69 exit;
e110c214 70 }
e110c214 71}
3c13b9fb 72
8b52a4ca 73/**
74 * Given the challenge from the server, supply the response using cram-md5 (See
75 * RFC 2195 for details)
76 *
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
81 *
82 */
47a29326 83function cram_md5_response ($username,$password,$challenge) {
8b52a4ca 84 $challenge=base64_decode($challenge);
85 $hash=bin2hex(hmac_md5($challenge,$password));
86 $response=base64_encode($username . " " . $hash) . "\r\n";
87 return $response;
47a29326 88}
89
8b52a4ca 90/**
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
94 * details)
95 *
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
104 */
47a29326 105function digest_md5_response ($username,$password,$challenge,$service,$host) {
ea321915 106 $result=digest_md5_parse_challenge($challenge);
62f7daa5 107
ea321915 108 // verify server supports qop=auth
109 // $qop = explode(",",$result['qop']);
110 //if (!in_array("auth",$qop)) {
47a29326 111 // rfc2831: client MUST fail if no qop methods supported
ea321915 112 // return false;
113 //}
114 $cnonce = base64_encode(bin2hex(hmac_md5(microtime())));
115 $ncount = "00000001";
116
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 */
120 $qop_value = "auth";
121
122 $digest_uri_value = $service . '/' . $host;
123
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';
136 }
137 $A2 = bin2hex(hmac_md5($A2));
138
139 $string_response = $result['nonce'] . ':' . $ncount . ':' . $cnonce . ':' . $qop_value;
140 $response_value = bin2hex(hmac_md5($A1.":".$string_response.":".$A2));
141
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";
62f7daa5 148
47a29326 149}
150
8b52a4ca 151/**
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.
155 *
156 * @param string $challenge Digest-MD5 Challenge
157 * @return array Digest-MD5 challenge decoded data
158 */
47a29326 159function digest_md5_parse_challenge($challenge) {
ea321915 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);
164 }
165 $key=explode('=',$challenge,2);
166 $challenge=$key[1];
167 $key=$key[0];
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
179 }
180 } else {
181 // We're in a "simple" value - explode to next comma
182 $val=explode(',',$challenge,2);
183 if (isset($val[1])) {
184 $challenge=$val[1];
185 } else {
186 unset($challenge);
187 }
188 $value=$val[0];
189 }
190 $parsed["$key"]=$value;
191 } // End of while loop
192 return $parsed;
47a29326 193}
194
8b52a4ca 195/**
196 * Creates a HMAC digest that can be used for auth purposes
197 * See RFCs 2104, 2617, 2831
198 * Uses mhash() extension if available
199 *
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
204 */
1c6d997a 205function hmac_md5($data, $key='') {
639c7164 206 if (extension_loaded('mhash')) {
ea321915 207 if ($key== '') {
208 $mhash=mhash(MHASH_MD5,$data);
209 } else {
210 $mhash=mhash(MHASH_MD5,$data,$key);
211 }
212 return $mhash;
639c7164 213 }
214 if (!$key) {
ea321915 215 return pack('H*',md5($data));
639c7164 216 }
217 $key = str_pad($key,64,chr(0x00));
218 if (strlen($key) > 64) {
219 $key = pack("H*",md5($key));
220 }
221 $k_ipad = $key ^ str_repeat(chr(0x36), 64) ;
222 $k_opad = $key ^ str_repeat(chr(0x5c), 64) ;
1c6d997a 223 /* Heh, let's get recursive. */
224 $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) );
639c7164 225 return $hmac;
226}
227
62f7daa5 228/**
9bd3b1e6 229 * Fillin user and password based on SMTP auth settings.
230 *
9bd3b1e6 231 * @param string $user Reference to SMTP username
232 * @param string $pass Reference to SMTP password (unencrypted)
233 */
234function get_smtp_user(&$user, &$pass) {
62f7daa5 235 global $username, $smtp_auth_mech,
9bd3b1e6 236 $smtp_sitewide_user, $smtp_sitewide_pass;
237
238 if ($smtp_auth_mech == 'none') {
239 $user = '';
240 $pass = '';
241 } elseif ( isset($smtp_sitewide_user) && isset($smtp_sitewide_pass) ) {
242 $user = $smtp_sitewide_user;
243 $pass = $smtp_sitewide_pass;
244 } else {
245 global $key, $onetimepad;
246 $user = $username;
247 $pass = OneTimePadDecrypt($key, $onetimepad);
248 }
249}
250
f8a1ed5a 251?>