auth.php 11 KB

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