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