auth.php 8.2 KB

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