A fix to make SquirrelMail work under PHP5 (at least the beta that was released
[squirrelmail.git] / functions / auth.php
1 <?php
2
3 /**
4 * auth.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Contains functions used to do authentication.
10 *
11 * $Id$
12 * @package squirrelmail
13 */
14
15 /** Put in a safety net here, in case a naughty admin didn't run conf.pl when they upgraded */
16
17 if (! isset($smtp_auth_mech)) {
18 $smtp_auth_mech = 'none';
19 }
20
21 if (! isset($imap_auth_mech)) {
22 $imap_auth_mech = 'login';
23 }
24
25 if (! isset($use_imap_tls)) {
26 $use_imap_tls = false;
27 }
28
29 if (! isset($use_smtp_tls)) {
30 $use_smtp_tls = false;
31 }
32
33 function is_logged_in() {
34
35 if ( sqsession_is_registered('user_is_logged_in') ) {
36 return;
37 } else {
38 global $PHP_SELF, $session_expired_post,
39 $session_expired_location;
40
41 /* First we store some information in the new session to prevent
42 * information-loss.
43 */
44
45 $session_expired_post = $_POST;
46 $session_expired_location = $PHP_SELF;
47 if (!sqsession_is_registered('session_expired_post')) {
48 sqsession_register($session_expired_post,'session_expired_post');
49 }
50 if (!sqsession_is_registered('session_expired_location')) {
51 sqsession_register($session_expired_location,'session_expired_location');
52 }
53 include_once( SM_PATH . 'functions/display_messages.php' );
54 logout_error( _("You must be logged in to access this page.") );
55 exit;
56 }
57 }
58
59 function cram_md5_response ($username,$password,$challenge) {
60
61 /* Given the challenge from the server, supply the response using
62 cram-md5 (See RFC 2195 for details)
63 */
64 $challenge=base64_decode($challenge);
65 $hash=bin2hex(hmac_md5($challenge,$password));
66 $response=base64_encode($username . " " . $hash) . "\r\n";
67 return $response;
68 }
69
70 function digest_md5_response ($username,$password,$challenge,$service,$host) {
71 /* Given the challenge from the server, calculate and return the response-string
72 for digest-md5 authentication. (See RFC 2831 for more details) */
73 $result=digest_md5_parse_challenge($challenge);
74
75 // verify server supports qop=auth
76 // $qop = explode(",",$result['qop']);
77 //if (!in_array("auth",$qop)) {
78 // rfc2831: client MUST fail if no qop methods supported
79 // return false;
80 //}
81 $cnonce = base64_encode(bin2hex(hmac_md5(microtime())));
82 $ncount = "00000001";
83
84 /* This can be auth (authentication only), auth-int (integrity protection), or
85 auth-conf (confidentiality protection). Right now only auth is supported.
86 DO NOT CHANGE THIS VALUE */
87 $qop_value = "auth";
88
89 $digest_uri_value = $service . '/' . $host;
90
91 // build the $response_value
92 //FIXME This will probably break badly if a server sends more than one realm
93 $string_a1 = utf8_encode($username).":";
94 $string_a1 .= utf8_encode($result['realm']).":";
95 $string_a1 .= utf8_encode($password);
96 $string_a1 = hmac_md5($string_a1);
97 $A1 = $string_a1 . ":" . $result['nonce'] . ":" . $cnonce;
98 $A1 = bin2hex(hmac_md5($A1));
99 $A2 = "AUTHENTICATE:$digest_uri_value";
100 // If qop is auth-int or auth-conf, A2 gets a little extra
101 if ($qop_value != 'auth') {
102 $A2 .= ':00000000000000000000000000000000';
103 }
104 $A2 = bin2hex(hmac_md5($A2));
105
106 $string_response = $result['nonce'] . ':' . $ncount . ':' . $cnonce . ':' . $qop_value;
107 $response_value = bin2hex(hmac_md5($A1.":".$string_response.":".$A2));
108
109 $reply = 'charset=utf-8,username="' . $username . '",realm="' . $result["realm"] . '",';
110 $reply .= 'nonce="' . $result['nonce'] . '",nc=' . $ncount . ',cnonce="' . $cnonce . '",';
111 $reply .= "digest-uri=\"$digest_uri_value\",response=$response_value";
112 $reply .= ',qop=' . $qop_value;
113 $reply = base64_encode($reply);
114 return $reply . "\r\n";
115
116 }
117
118 function digest_md5_parse_challenge($challenge) {
119 /* This function parses the challenge sent during DIGEST-MD5 authentication and
120 returns an array. See the RFC for details on what's in the challenge string.
121 */
122 $challenge=base64_decode($challenge);
123 while (isset($challenge)) {
124 if ($challenge{0} == ',') { // First char is a comma, must not be 1st time through loop
125 $challenge=substr($challenge,1);
126 }
127 $key=explode('=',$challenge,2);
128 $challenge=$key[1];
129 $key=$key[0];
130 if ($challenge{0} == '"') {
131 // We're in a quoted value
132 // Drop the first quote, since we don't care about it
133 $challenge=substr($challenge,1);
134 // Now explode() to the next quote, which is the end of our value
135 $val=explode('"',$challenge,2);
136 $challenge=$val[1]; // The rest of the challenge, work on it in next iteration of loop
137 $value=explode(',',$val[0]);
138 // Now, for those quoted values that are only 1 piece..
139 if (sizeof($value) == 1) {
140 $value=$value[0]; // Convert to non-array
141 }
142 } else {
143 // We're in a "simple" value - explode to next comma
144 $val=explode(',',$challenge,2);
145 if (isset($val[1])) {
146 $challenge=$val[1];
147 } else {
148 unset($challenge);
149 }
150 $value=$val[0];
151 }
152 $parsed["$key"]=$value;
153 } // End of while loop
154 return $parsed;
155 }
156
157 function hmac_md5($data, $key='') {
158 // Creates a HMAC digest that can be used for auth purposes
159 // See RFCs 2104, 2617, 2831
160 // Uses mhash() extension if available
161 if (extension_loaded('mhash')) {
162 if ($key== '') {
163 $mhash=mhash(MHASH_MD5,$data);
164 } else {
165 $mhash=mhash(MHASH_MD5,$data,$key);
166 }
167 return $mhash;
168 }
169 if (!$key) {
170 return pack('H*',md5($data));
171 }
172 $key = str_pad($key,64,chr(0x00));
173 if (strlen($key) > 64) {
174 $key = pack("H*",md5($key));
175 }
176 $k_ipad = $key ^ str_repeat(chr(0x36), 64) ;
177 $k_opad = $key ^ str_repeat(chr(0x5c), 64) ;
178 /* Heh, let's get recursive. */
179 $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) );
180 return $hmac;
181 }
182
183 ?>