auth.php 5.7 KB

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