auth.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * auth.php
  4. *
  5. * Contains functions used to do authentication.
  6. *
  7. * Dependencies:
  8. * functions/global.php
  9. * functions/strings.php.
  10. *
  11. * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  12. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  13. * @version $Id$
  14. * @package squirrelmail
  15. */
  16. /**
  17. * Detect logged user
  18. *
  19. * Function is similar to is_logged_in() function. If user is logged in, function
  20. * returns true. If user is not logged in or session is expired, function saves $_POST
  21. * and $PHP_SELF in session and returns false. POST information is saved in
  22. * 'session_expired_post' variable, PHP_SELF is saved in 'session_expired_location'.
  23. *
  24. * Script that uses this function instead of is_logged_in() function, must handle user
  25. * level messages.
  26. * @return boolean
  27. * @since 1.5.1
  28. */
  29. function sqauth_is_logged_in() {
  30. if ( sqsession_is_registered('user_is_logged_in') ) {
  31. return true;
  32. } else {
  33. global $PHP_SELF, $session_expired_post, $session_expired_location;
  34. // First we store some information in the new session to prevent
  35. // information-loss.
  36. //
  37. $session_expired_post = $_POST;
  38. $session_expired_location = $PHP_SELF;
  39. if (!sqsession_is_registered('session_expired_post')) {
  40. sqsession_register($session_expired_post,'session_expired_post');
  41. }
  42. if (!sqsession_is_registered('session_expired_location')) {
  43. sqsession_register($session_expired_location,'session_expired_location');
  44. }
  45. return false;
  46. }
  47. }
  48. /**
  49. * Reads and decodes stored user password information
  50. *
  51. * Direct access to password information is deprecated.
  52. * @return string password in plain text
  53. * @since 1.5.1
  54. */
  55. function sqauth_read_password() {
  56. sqgetGlobalVar('key', $key, SQ_COOKIE);
  57. sqgetGlobalVar('onetimepad', $onetimepad,SQ_SESSION);
  58. return OneTimePadDecrypt($key, $onetimepad);
  59. }
  60. /**
  61. * Saves or updates user password information
  62. *
  63. * This function is used to update password information that SquirrelMail
  64. * stores during existing web session. It does not modify password stored
  65. * in authentication system used by IMAP server.
  66. *
  67. * Function must be called before any html output started. Direct access
  68. * to password information is deprecated. Saved password information is
  69. * available only to next executed SquirrelMail script. If your script needs
  70. * access to saved password after sqauth_save_password() call, use returned
  71. * OTP encrypted key.
  72. * @param string $pass password
  73. * @return string password encrypted with OTP. In case script wants to access
  74. * password information before reloading page.
  75. * @since 1.5.1
  76. */
  77. function sqauth_save_password($pass) {
  78. sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
  79. $onetimepad = OneTimePadCreate(strlen($pass));
  80. sqsession_register($onetimepad,'onetimepad');
  81. $key = OneTimePadEncrypt($pass, $onetimepad);
  82. sqsetcookie('key', $key, false, $base_uri);
  83. return $key;
  84. }
  85. /**
  86. * Given the challenge from the server, supply the response using cram-md5 (See
  87. * RFC 2195 for details)
  88. *
  89. * @param string $username User ID
  90. * @param string $password User password supplied by User
  91. * @param string $challenge The challenge supplied by the server
  92. * @return string The response to be sent to the IMAP server
  93. * @since 1.4.0
  94. */
  95. function cram_md5_response ($username,$password,$challenge) {
  96. $challenge=base64_decode($challenge);
  97. $hash=bin2hex(hmac_md5($challenge,$password));
  98. $response=base64_encode($username . " " . $hash) . "\r\n";
  99. return $response;
  100. }
  101. /**
  102. * Return Digest-MD5 response.
  103. * Given the challenge from the server, calculate and return the
  104. * response-string for digest-md5 authentication. (See RFC 2831 for more
  105. * details)
  106. *
  107. * @param string $username User ID
  108. * @param string $password User password supplied by User
  109. * @param string $challenge The challenge supplied by the server
  110. * @param string $service The service name, usually 'imap'; it is used to
  111. * define the digest-uri.
  112. * @param string $host The host name, usually the server's FQDN; it is used to
  113. * define the digest-uri.
  114. * @return string The response to be sent to the IMAP server
  115. * @since 1.4.0
  116. */
  117. function digest_md5_response ($username,$password,$challenge,$service,$host) {
  118. $result=digest_md5_parse_challenge($challenge);
  119. // verify server supports qop=auth
  120. // $qop = explode(",",$result['qop']);
  121. //if (!in_array("auth",$qop)) {
  122. // rfc2831: client MUST fail if no qop methods supported
  123. // return false;
  124. //}
  125. $cnonce = base64_encode(bin2hex(hmac_md5(microtime())));
  126. $ncount = "00000001";
  127. /* This can be auth (authentication only), auth-int (integrity protection), or
  128. auth-conf (confidentiality protection). Right now only auth is supported.
  129. DO NOT CHANGE THIS VALUE */
  130. $qop_value = "auth";
  131. $digest_uri_value = $service . '/' . $host;
  132. // build the $response_value
  133. //FIXME This will probably break badly if a server sends more than one realm
  134. $string_a1 = utf8_encode($username).":";
  135. $string_a1 .= utf8_encode($result['realm']).":";
  136. $string_a1 .= utf8_encode($password);
  137. $string_a1 = hmac_md5($string_a1);
  138. $A1 = $string_a1 . ":" . $result['nonce'] . ":" . $cnonce;
  139. $A1 = bin2hex(hmac_md5($A1));
  140. $A2 = "AUTHENTICATE:$digest_uri_value";
  141. // If qop is auth-int or auth-conf, A2 gets a little extra
  142. if ($qop_value != 'auth') {
  143. $A2 .= ':00000000000000000000000000000000';
  144. }
  145. $A2 = bin2hex(hmac_md5($A2));
  146. $string_response = $result['nonce'] . ':' . $ncount . ':' . $cnonce . ':' . $qop_value;
  147. $response_value = bin2hex(hmac_md5($A1.":".$string_response.":".$A2));
  148. $reply = 'charset=utf-8,username="' . $username . '",realm="' . $result["realm"] . '",';
  149. $reply .= 'nonce="' . $result['nonce'] . '",nc=' . $ncount . ',cnonce="' . $cnonce . '",';
  150. $reply .= "digest-uri=\"$digest_uri_value\",response=$response_value";
  151. $reply .= ',qop=' . $qop_value;
  152. $reply = base64_encode($reply);
  153. return $reply . "\r\n";
  154. }
  155. /**
  156. * Parse Digest-MD5 challenge.
  157. * This function parses the challenge sent during DIGEST-MD5 authentication and
  158. * returns an array. See the RFC for details on what's in the challenge string.
  159. *
  160. * @param string $challenge Digest-MD5 Challenge
  161. * @return array Digest-MD5 challenge decoded data
  162. * @since 1.4.0
  163. */
  164. function digest_md5_parse_challenge($challenge) {
  165. $challenge=base64_decode($challenge);
  166. while (isset($challenge)) {
  167. if ($challenge{0} == ',') { // First char is a comma, must not be 1st time through loop
  168. $challenge=substr($challenge,1);
  169. }
  170. $key=explode('=',$challenge,2);
  171. $challenge=$key[1];
  172. $key=$key[0];
  173. if ($challenge{0} == '"') {
  174. // We're in a quoted value
  175. // Drop the first quote, since we don't care about it
  176. $challenge=substr($challenge,1);
  177. // Now explode() to the next quote, which is the end of our value
  178. $val=explode('"',$challenge,2);
  179. $challenge=$val[1]; // The rest of the challenge, work on it in next iteration of loop
  180. $value=explode(',',$val[0]);
  181. // Now, for those quoted values that are only 1 piece..
  182. if (sizeof($value) == 1) {
  183. $value=$value[0]; // Convert to non-array
  184. }
  185. } else {
  186. // We're in a "simple" value - explode to next comma
  187. $val=explode(',',$challenge,2);
  188. if (isset($val[1])) {
  189. $challenge=$val[1];
  190. } else {
  191. unset($challenge);
  192. }
  193. $value=$val[0];
  194. }
  195. $parsed["$key"]=$value;
  196. } // End of while loop
  197. return $parsed;
  198. }
  199. /**
  200. * Creates a HMAC digest that can be used for auth purposes
  201. * See RFCs 2104, 2617, 2831
  202. * Uses mhash() extension if available
  203. *
  204. * @param string $data Data to apply hash function to.
  205. * @param string $key Optional key, which, if supplied, will be used to
  206. * calculate data's HMAC.
  207. * @return string HMAC Digest string
  208. * @since 1.4.0
  209. */
  210. function hmac_md5($data, $key='') {
  211. if (extension_loaded('mhash')) {
  212. if ($key== '') {
  213. $mhash=mhash(MHASH_MD5,$data);
  214. } else {
  215. $mhash=mhash(MHASH_MD5,$data,$key);
  216. }
  217. return $mhash;
  218. }
  219. if (!$key) {
  220. return pack('H*',md5($data));
  221. }
  222. $key = str_pad($key,64,chr(0x00));
  223. if (strlen($key) > 64) {
  224. $key = pack("H*",md5($key));
  225. }
  226. $k_ipad = $key ^ str_repeat(chr(0x36), 64) ;
  227. $k_opad = $key ^ str_repeat(chr(0x5c), 64) ;
  228. /* Heh, let's get recursive. */
  229. $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) );
  230. return $hmac;
  231. }
  232. /**
  233. * Fillin user and password based on SMTP auth settings.
  234. *
  235. * @param string $user Reference to SMTP username
  236. * @param string $pass Reference to SMTP password (unencrypted)
  237. * @since 1.5.0
  238. */
  239. function get_smtp_user(&$user, &$pass) {
  240. global $username, $smtp_auth_mech,
  241. $smtp_sitewide_user, $smtp_sitewide_pass;
  242. if ($smtp_auth_mech == 'none') {
  243. $user = '';
  244. $pass = '';
  245. } elseif ( isset($smtp_sitewide_user) && isset($smtp_sitewide_pass) ) {
  246. $user = $smtp_sitewide_user;
  247. $pass = $smtp_sitewide_pass;
  248. } else {
  249. $user = $username;
  250. $pass = sqauth_read_password();
  251. }
  252. }
  253. ?>