auth.php 5.7 KB

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