LSattr_ldap_password : Added encodePassword() method and used this in getUpdateData()

This commit is contained in:
Benjamin Renard 2010-11-09 16:47:29 +01:00
parent fd566cd588
commit 8585976768

View file

@ -51,16 +51,34 @@ class LSattr_ldap_password extends LSattr_ldap {
*/ */
function getUpdateData($data) { function getUpdateData($data) {
$this -> clearPassword = $data[0]; $this -> clearPassword = $data[0];
$data=array();
$data[]=$this -> encodePassword($this -> clearPassword);
return $data;
}
/**
* Encode the password
*
* Note : Password encoding was strongly inspired of the project phpLdapAdmin.
* URL : http://phpldapadmin.sourceforge.net/
*
* @param[in] $clearPassword string The clear password
*
* @retval strinf The encode password
*/
function encodePassword($clearPassword) {
if (!$this -> config['ldap_options']['encode']) { if (!$this -> config['ldap_options']['encode']) {
$this -> config['ldap_options']['encode'] = 'md5crypt'; $this -> config['ldap_options']['encode'] = 'md5crypt';
} }
switch($this -> config['ldap_options']['encode']) { switch($this -> config['ldap_options']['encode']) {
case 'crypt': case 'crypt':
if ($this -> config['ldap_options']['no_random_crypt_salt']) { if ($this -> config['ldap_options']['no_random_crypt_salt']) {
return array('{CRYPT}' . crypt($this -> clearPassword,substr($this -> clearPassword,0,2))); return '{CRYPT}' . crypt($clearPassword,substr($clearPassword,0,2));
} }
else { else {
return array('{CRYPT}' . crypt($this -> clearPassword,$this -> getSalt(2))); return '{CRYPT}' . crypt($clearPassword,$this -> getSalt(2));
} }
break; break;
case 'ext_des': case 'ext_des':
@ -68,7 +86,7 @@ class LSattr_ldap_password extends LSattr_ldap {
LSerror :: addErrorCode('LSattr_ldap_password_01','ext_des'); LSerror :: addErrorCode('LSattr_ldap_password_01','ext_des');
} }
else { else {
return array('{CRYPT}' . crypt( $this -> clearPassword, '_' . $this -> getSalt(8) )); return '{CRYPT}' . crypt( $clearPassword, '_' . $this -> getSalt(8) );
} }
break; break;
case 'blowfish': case 'blowfish':
@ -76,15 +94,15 @@ class LSattr_ldap_password extends LSattr_ldap {
LSerror :: addErrorCode('LSattr_ldap_password_01','blowfish'); LSerror :: addErrorCode('LSattr_ldap_password_01','blowfish');
} }
else { else {
return array('{CRYPT}' . crypt( $this -> clearPassword, '$2a$12$' . $this -> getSalt(13) )); return '{CRYPT}' . crypt( $clearPassword, '$2a$12$' . $this -> getSalt(13) );
} }
break; break;
case 'sha': case 'sha':
if( function_exists('sha1') ) { if( function_exists('sha1') ) {
return array('{SHA}' . base64_encode( pack( 'H*' , sha1( $this -> clearPassword ) ) )); return '{SHA}' . base64_encode( pack( 'H*' , sha1( $clearPassword ) ) );
} }
elseif( function_exists( 'mhash' ) ) { elseif( function_exists( 'mhash' ) ) {
return array('{SHA}' . base64_encode( mhash( MHASH_SHA1, $this -> clearPassword ) )); return '{SHA}' . base64_encode( mhash( MHASH_SHA1, $clearPassword ) );
} else { } else {
LSerror :: addErrorCode('LSattr_ldap_password_01','sha'); LSerror :: addErrorCode('LSattr_ldap_password_01','sha');
} }
@ -92,8 +110,8 @@ class LSattr_ldap_password extends LSattr_ldap {
case 'ssha': case 'ssha':
if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) { if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) {
mt_srand( (double) microtime() * 1000000 ); mt_srand( (double) microtime() * 1000000 );
$salt = mhash_keygen_s2k( MHASH_SHA1, $this -> clearPassword, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 ); $salt = mhash_keygen_s2k( MHASH_SHA1, $clearPassword, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 );
return array("{SSHA}".base64_encode( mhash( MHASH_SHA1, $this -> clearPassword.$salt ).$salt )); return "{SSHA}".base64_encode( mhash( MHASH_SHA1, $clearPassword.$salt ).$salt );
} }
else { else {
LSerror :: addErrorCode('LSattr_ldap_password_01','ssha'); LSerror :: addErrorCode('LSattr_ldap_password_01','ssha');
@ -103,29 +121,29 @@ class LSattr_ldap_password extends LSattr_ldap {
if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) { if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) {
mt_srand( (double) microtime() * 1000000 ); mt_srand( (double) microtime() * 1000000 );
$salt = mhash_keygen_s2k( MHASH_MD5, $password_clear, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 ); $salt = mhash_keygen_s2k( MHASH_MD5, $password_clear, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 );
return array("{SMD5}".base64_encode( mhash( MHASH_MD5, $password_clear.$salt ).$salt )); return "{SMD5}".base64_encode( mhash( MHASH_MD5, $password_clear.$salt ).$salt );
} }
else { else {
LSerror :: addErrorCode('LSattr_ldap_password_01','smd5'); LSerror :: addErrorCode('LSattr_ldap_password_01','smd5');
} }
break; break;
case 'md5': case 'md5':
return array('{MD5}' . base64_encode( pack( 'H*' , md5( $this -> clearPassword ) ) )); return '{MD5}' . base64_encode( pack( 'H*' , md5( $clearPassword ) ) );
break; break;
case 'md5crypt': case 'md5crypt':
if( ! defined( 'CRYPT_MD5' ) || CRYPT_MD5 == 0 ) { if( ! defined( 'CRYPT_MD5' ) || CRYPT_MD5 == 0 ) {
LSerror :: addErrorCode('LSattr_ldap_password_01','md5crypt'); LSerror :: addErrorCode('LSattr_ldap_password_01','md5crypt');
} }
else { else {
return array('{CRYPT}'.crypt($this -> clearPassword,'$1$'.$this -> getSalt().'$')); return '{CRYPT}'.crypt($clearPassword,'$1$'.$this -> getSalt().'$');
} }
break; break;
case 'clear': case 'clear':
return array($this -> clearPassword); return $clearPassword;
break; break;
} }
LSerror :: addErrorCode('LSattr_ldap_password_01',$this -> config['ldap_options']['encode']); LSerror :: addErrorCode('LSattr_ldap_password_01',$this -> config['ldap_options']['encode']);
return array($this -> clearPassword); return $clearPassword;
} }
/** /**