auth.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * auth.php
  4. *
  5. * Copyright (c) 1999-2004 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. * @version $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. /**
  27. * Check if user has previously logged in to the Squirrelmail session. If user
  28. * has not logged in, execution will stop inside this function.
  29. *
  30. * @return int A positive value is returned if user has previously logged in
  31. * successfully.
  32. */
  33. function is_logged_in() {
  34. if ( sqsession_is_registered('user_is_logged_in') ) {
  35. return;
  36. } else {
  37. global $PHP_SELF, $session_expired_post,
  38. $session_expired_location, $squirrelmail_language;
  39. /* First we store some information in the new session to prevent
  40. * information-loss.
  41. */
  42. $session_expired_post = $_POST;
  43. $session_expired_location = $PHP_SELF;
  44. if (!sqsession_is_registered('session_expired_post')) {
  45. sqsession_register($session_expired_post,'session_expired_post');
  46. }
  47. if (!sqsession_is_registered('session_expired_location')) {
  48. sqsession_register($session_expired_location,'session_expired_location');
  49. }
  50. include_once( SM_PATH . 'functions/display_messages.php' );
  51. set_up_language($squirrelmail_language, true);
  52. logout_error( _("You must be logged in to access this page.") );
  53. exit;
  54. }
  55. }
  56. /**
  57. * Given the challenge from the server, supply the response using cram-md5 (See
  58. * RFC 2195 for details)
  59. *
  60. * @param string $username User ID
  61. * @param string $password User password supplied by User
  62. * @param string $challenge The challenge supplied by the server
  63. * @return string The response to be sent to the IMAP server
  64. *
  65. */
  66. function cram_md5_response ($username,$password,$challenge) {
  67. $challenge=base64_decode($challenge);
  68. $hash=bin2hex(hmac_md5($challenge,$password));
  69. $response=base64_encode($username . " " . $hash) . "\r\n";
  70. return $response;
  71. }
  72. /**
  73. * Return Digest-MD5 response.
  74. * Given the challenge from the server, calculate and return the
  75. * response-string for digest-md5 authentication. (See RFC 2831 for more
  76. * details)
  77. *
  78. * @param string $username User ID
  79. * @param string $password User password supplied by User
  80. * @param string $challenge The challenge supplied by the server
  81. * @param string $service The service name, usually 'imap'; it is used to
  82. * define the digest-uri.
  83. * @param string $host The host name, usually the server's FQDN; it is used to
  84. * define the digest-uri.
  85. * @return string The response to be sent to the IMAP server
  86. */
  87. function digest_md5_response ($username,$password,$challenge,$service,$host) {
  88. $result=digest_md5_parse_challenge($challenge);
  89. // verify server supports qop=auth
  90. // $qop = explode(",",$result['qop']);
  91. //if (!in_array("auth",$qop)) {
  92. // rfc2831: client MUST fail if no qop methods supported
  93. // return false;
  94. //}
  95. $cnonce = base64_encode(bin2hex(hmac_md5(microtime())));
  96. $ncount = "00000001";
  97. /* This can be auth (authentication only), auth-int (integrity protection), or
  98. auth-conf (confidentiality protection). Right now only auth is supported.
  99. DO NOT CHANGE THIS VALUE */
  100. $qop_value = "auth";
  101. $digest_uri_value = $service . '/' . $host;
  102. // build the $response_value
  103. //FIXME This will probably break badly if a server sends more than one realm
  104. $string_a1 = utf8_encode($username).":";
  105. $string_a1 .= utf8_encode($result['realm']).":";
  106. $string_a1 .= utf8_encode($password);
  107. $string_a1 = hmac_md5($string_a1);
  108. $A1 = $string_a1 . ":" . $result['nonce'] . ":" . $cnonce;
  109. $A1 = bin2hex(hmac_md5($A1));
  110. $A2 = "AUTHENTICATE:$digest_uri_value";
  111. // If qop is auth-int or auth-conf, A2 gets a little extra
  112. if ($qop_value != 'auth') {
  113. $A2 .= ':00000000000000000000000000000000';
  114. }
  115. $A2 = bin2hex(hmac_md5($A2));
  116. $string_response = $result['nonce'] . ':' . $ncount . ':' . $cnonce . ':' . $qop_value;
  117. $response_value = bin2hex(hmac_md5($A1.":".$string_response.":".$A2));
  118. $reply = 'charset=utf-8,username="' . $username . '",realm="' . $result["realm"] . '",';
  119. $reply .= 'nonce="' . $result['nonce'] . '",nc=' . $ncount . ',cnonce="' . $cnonce . '",';
  120. $reply .= "digest-uri=\"$digest_uri_value\",response=$response_value";
  121. $reply .= ',qop=' . $qop_value;
  122. $reply = base64_encode($reply);
  123. return $reply . "\r\n";
  124. }
  125. /**
  126. * Parse Digest-MD5 challenge.
  127. * This function parses the challenge sent during DIGEST-MD5 authentication and
  128. * returns an array. See the RFC for details on what's in the challenge string.
  129. *
  130. * @param string $challenge Digest-MD5 Challenge
  131. * @return array Digest-MD5 challenge decoded data
  132. */
  133. function digest_md5_parse_challenge($challenge) {
  134. $challenge=base64_decode($challenge);
  135. while (isset($challenge)) {
  136. if ($challenge{0} == ',') { // First char is a comma, must not be 1st time through loop
  137. $challenge=substr($challenge,1);
  138. }
  139. $key=explode('=',$challenge,2);
  140. $challenge=$key[1];
  141. $key=$key[0];
  142. if ($challenge{0} == '"') {
  143. // We're in a quoted value
  144. // Drop the first quote, since we don't care about it
  145. $challenge=substr($challenge,1);
  146. // Now explode() to the next quote, which is the end of our value
  147. $val=explode('"',$challenge,2);
  148. $challenge=$val[1]; // The rest of the challenge, work on it in next iteration of loop
  149. $value=explode(',',$val[0]);
  150. // Now, for those quoted values that are only 1 piece..
  151. if (sizeof($value) == 1) {
  152. $value=$value[0]; // Convert to non-array
  153. }
  154. } else {
  155. // We're in a "simple" value - explode to next comma
  156. $val=explode(',',$challenge,2);
  157. if (isset($val[1])) {
  158. $challenge=$val[1];
  159. } else {
  160. unset($challenge);
  161. }
  162. $value=$val[0];
  163. }
  164. $parsed["$key"]=$value;
  165. } // End of while loop
  166. return $parsed;
  167. }
  168. /**
  169. * Creates a HMAC digest that can be used for auth purposes
  170. * See RFCs 2104, 2617, 2831
  171. * Uses mhash() extension if available
  172. *
  173. * @param string $data Data to apply hash function to.
  174. * @param string $key Optional key, which, if supplied, will be used to
  175. * calculate data's HMAC.
  176. * @return string HMAC Digest string
  177. */
  178. function hmac_md5($data, $key='') {
  179. if (extension_loaded('mhash')) {
  180. if ($key== '') {
  181. $mhash=mhash(MHASH_MD5,$data);
  182. } else {
  183. $mhash=mhash(MHASH_MD5,$data,$key);
  184. }
  185. return $mhash;
  186. }
  187. if (!$key) {
  188. return pack('H*',md5($data));
  189. }
  190. $key = str_pad($key,64,chr(0x00));
  191. if (strlen($key) > 64) {
  192. $key = pack("H*",md5($key));
  193. }
  194. $k_ipad = $key ^ str_repeat(chr(0x36), 64) ;
  195. $k_opad = $key ^ str_repeat(chr(0x5c), 64) ;
  196. /* Heh, let's get recursive. */
  197. $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) );
  198. return $hmac;
  199. }
  200. /**
  201. * Fillin user and password based on SMTP auth settings.
  202. *
  203. * @param string $user Reference to SMTP username
  204. * @param string $pass Reference to SMTP password (unencrypted)
  205. */
  206. function get_smtp_user(&$user, &$pass) {
  207. global $username, $smtp_auth_mech,
  208. $smtp_sitewide_user, $smtp_sitewide_pass;
  209. if ($smtp_auth_mech == 'none') {
  210. $user = '';
  211. $pass = '';
  212. } elseif ( isset($smtp_sitewide_user) && isset($smtp_sitewide_pass) ) {
  213. $user = $smtp_sitewide_user;
  214. $pass = $smtp_sitewide_pass;
  215. } else {
  216. global $key, $onetimepad;
  217. $user = $username;
  218. $pass = OneTimePadDecrypt($key, $onetimepad);
  219. }
  220. }
  221. ?>