auth.php 8.2 KB

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