Source for file auth.php

Documentation is available at auth.php

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

Documentation generated on Sat, 07 Oct 2006 16:29:56 +0300 by phpDocumentor 1.3.0RC6