2007-11-15 19:07:24 +01:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
|
|
|
* http://ldapsaisie.labs.libre-entreprise.org
|
|
|
|
*
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2009-01-02 17:00:25 +01:00
|
|
|
* Ldap attribute type password
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class LSattr_ldap_password extends LSattr_ldap {
|
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
var $clearPassword = NULL;
|
2007-11-15 19:07:24 +01:00
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
/**
|
2009-01-02 17:00:25 +01:00
|
|
|
* Return the display value of this attribute
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2009-01-02 17:00:25 +01:00
|
|
|
* @param[in] $data mixed The value of this attribute
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2009-01-02 17:00:25 +01:00
|
|
|
* @retval mixed The display value of this attribute
|
2008-02-12 18:59:44 +01:00
|
|
|
*/
|
2007-11-15 19:07:24 +01:00
|
|
|
function getDisplayValue($data) {
|
2013-06-26 11:31:21 +02:00
|
|
|
if ($this -> config['ldap_options']['displayClearValue']) {
|
|
|
|
if (is_array($data)) {
|
|
|
|
$ret=array();
|
|
|
|
foreach($data as $p) {
|
|
|
|
if ($p==$this -> config['ldap_options']['wildcardPassword'] || $p==$this -> config['ldap_options']['encodedWildcardPassword']) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$ret[]=$p;
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return '********';
|
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
/**
|
2009-01-02 17:00:25 +01:00
|
|
|
* Return the value of this attribute to be stocked
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2009-01-02 17:00:25 +01:00
|
|
|
* Note : Password encoding was strongly inspired of the project phpLdapAdmin.
|
|
|
|
* URL : http://phpldapadmin.sourceforge.net/
|
|
|
|
*
|
|
|
|
* @param[in] $data mixed The attribute value
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2009-01-02 17:00:25 +01:00
|
|
|
* @retval mixed The value of this attribute to be stocked
|
2008-02-12 18:59:44 +01:00
|
|
|
*/
|
2007-11-15 19:07:24 +01:00
|
|
|
function getUpdateData($data) {
|
2008-02-12 18:59:44 +01:00
|
|
|
$this -> clearPassword = $data[0];
|
2010-11-09 16:47:29 +01:00
|
|
|
$data=array();
|
|
|
|
|
|
|
|
$data[]=$this -> encodePassword($this -> clearPassword);
|
|
|
|
|
2010-11-09 16:50:24 +01:00
|
|
|
// Wildcard Password
|
|
|
|
if (isset($this -> config['ldap_options']['wildcardPassword'])) {
|
|
|
|
if(!is_array($this -> config['ldap_options']['wildcardPassword'])) {
|
|
|
|
$data[]=$this -> encodePassword($this -> config['ldap_options']['wildcardPassword']);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach($this -> config['ldap_options']['wildcardPassword'] as $pwd) {
|
|
|
|
$data[]=$this -> encodePassword($pwd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wildcard Password already encoded
|
|
|
|
if (isset($this -> config['ldap_options']['encodedWildcardPassword'])) {
|
|
|
|
if(!is_array($this -> config['ldap_options']['encodedWildcardPassword'])) {
|
|
|
|
$data[]=$this -> config['ldap_options']['encodedWildcardPassword'];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$data=array_merge($data,$this -> config['ldap_options']['encodedWildcardPassword']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 16:47:29 +01:00
|
|
|
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) {
|
2011-04-08 17:07:43 +02:00
|
|
|
if (isset($this -> config['ldap_options']['encode_function']) || $this -> config['ldap_options']['encode']=='function') {
|
|
|
|
if (!is_callable($this -> config['ldap_options']['encode_function'])) {
|
|
|
|
$this -> config['ldap_options']['encode'] = 'clear';
|
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_02',$this -> config['ldap_options']['encode_function']);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this -> config['ldap_options']['encode'] = 'function';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (!$this -> config['ldap_options']['encode']) {
|
2009-01-02 17:00:25 +01:00
|
|
|
$this -> config['ldap_options']['encode'] = 'md5crypt';
|
|
|
|
}
|
|
|
|
switch($this -> config['ldap_options']['encode']) {
|
|
|
|
case 'crypt':
|
|
|
|
if ($this -> config['ldap_options']['no_random_crypt_salt']) {
|
2010-11-09 16:47:29 +01:00
|
|
|
return '{CRYPT}' . crypt($clearPassword,substr($clearPassword,0,2));
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-11-09 16:47:29 +01:00
|
|
|
return '{CRYPT}' . crypt($clearPassword,$this -> getSalt(2));
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'ext_des':
|
|
|
|
if ( ! defined( 'CRYPT_EXT_DES' ) || CRYPT_EXT_DES == 0 ) {
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_01','ext_des');
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-11-09 16:47:29 +01:00
|
|
|
return '{CRYPT}' . crypt( $clearPassword, '_' . $this -> getSalt(8) );
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'blowfish':
|
|
|
|
if( ! defined( 'CRYPT_BLOWFISH' ) || CRYPT_BLOWFISH == 0 ) {
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_01','blowfish');
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-11-09 16:47:29 +01:00
|
|
|
return '{CRYPT}' . crypt( $clearPassword, '$2a$12$' . $this -> getSalt(13) );
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'sha':
|
|
|
|
if( function_exists('sha1') ) {
|
2010-11-09 16:47:29 +01:00
|
|
|
return '{SHA}' . base64_encode( pack( 'H*' , sha1( $clearPassword ) ) );
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
elseif( function_exists( 'mhash' ) ) {
|
2010-11-09 16:47:29 +01:00
|
|
|
return '{SHA}' . base64_encode( mhash( MHASH_SHA1, $clearPassword ) );
|
2009-01-02 17:00:25 +01:00
|
|
|
} else {
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_01','sha');
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
break;
|
2019-02-01 10:26:05 +01:00
|
|
|
case 'sha256':
|
|
|
|
case 'sha512':
|
|
|
|
switch($this -> config['ldap_options']['encode']) {
|
|
|
|
case 'sha256':
|
|
|
|
$mhash_type = MHASH_SHA256;
|
|
|
|
break;
|
|
|
|
case 'sha512':
|
|
|
|
$mhash_type = MHASH_SHA512;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( function_exists( 'mhash' ) ) {
|
|
|
|
return '{'.strtoupper($this -> config['ldap_options']['encode']).'}' . base64_encode( mhash( $mhash_type, $clearPassword ) );
|
|
|
|
} else {
|
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_01', $this -> config['ldap_options']['encode']);
|
|
|
|
}
|
|
|
|
break;
|
2009-01-02 17:00:25 +01:00
|
|
|
case 'ssha':
|
2019-02-01 10:26:05 +01:00
|
|
|
case 'ssha256':
|
|
|
|
case 'ssha512':
|
|
|
|
switch($this -> config['ldap_options']['encode']) {
|
|
|
|
case 'ssha':
|
|
|
|
$mhash_type = MHASH_SHA1;
|
|
|
|
break;
|
|
|
|
case 'ssha256':
|
|
|
|
$mhash_type = MHASH_SHA256;
|
|
|
|
break;
|
|
|
|
case 'ssha512':
|
|
|
|
$mhash_type = MHASH_SHA512;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-02 17:00:25 +01:00
|
|
|
if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) {
|
|
|
|
mt_srand( (double) microtime() * 1000000 );
|
2019-02-01 10:26:05 +01:00
|
|
|
$salt = mhash_keygen_s2k( $mhash_type, $clearPassword, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 );
|
|
|
|
return "{".strtoupper($this -> config['ldap_options']['encode'])."}".base64_encode( mhash( $mhash_type, $clearPassword.$salt ).$salt );
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-02-01 10:26:05 +01:00
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_01', $this -> config['ldap_options']['encode']);
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'smd5':
|
|
|
|
if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) {
|
|
|
|
mt_srand( (double) microtime() * 1000000 );
|
|
|
|
$salt = mhash_keygen_s2k( MHASH_MD5, $password_clear, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 );
|
2010-11-09 16:47:29 +01:00
|
|
|
return "{SMD5}".base64_encode( mhash( MHASH_MD5, $password_clear.$salt ).$salt );
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_01','smd5');
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'md5':
|
2010-11-09 16:47:29 +01:00
|
|
|
return '{MD5}' . base64_encode( pack( 'H*' , md5( $clearPassword ) ) );
|
2009-01-02 17:00:25 +01:00
|
|
|
break;
|
|
|
|
case 'md5crypt':
|
|
|
|
if( ! defined( 'CRYPT_MD5' ) || CRYPT_MD5 == 0 ) {
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_01','md5crypt');
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2010-11-09 16:47:29 +01:00
|
|
|
return '{CRYPT}'.crypt($clearPassword,'$1$'.$this -> getSalt().'$');
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
|
|
|
break;
|
2009-02-11 16:58:35 +01:00
|
|
|
case 'clear':
|
2010-11-09 16:47:29 +01:00
|
|
|
return $clearPassword;
|
2009-02-11 16:58:35 +01:00
|
|
|
break;
|
2011-04-08 17:07:43 +02:00
|
|
|
case 'function':
|
2017-04-28 10:22:01 +02:00
|
|
|
return call_user_func_array($this -> config['ldap_options']['encode_function'], array(&$this -> attribute -> ldapObject, $clearPassword));
|
2011-04-08 17:07:43 +02:00
|
|
|
break;
|
2009-01-02 17:00:25 +01:00
|
|
|
}
|
2009-02-11 16:58:35 +01:00
|
|
|
LSerror :: addErrorCode('LSattr_ldap_password_01',$this -> config['ldap_options']['encode']);
|
2010-11-09 16:47:29 +01:00
|
|
|
return $clearPassword;
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
/**
|
2009-01-02 17:00:25 +01:00
|
|
|
* Return salt (random string)
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2009-01-02 17:00:25 +01:00
|
|
|
* @param[in] integer Number of caracters in this salt
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2009-01-02 17:00:25 +01:00
|
|
|
* @retval string A salt
|
2008-02-12 18:59:44 +01:00
|
|
|
*/
|
2007-11-15 19:07:24 +01:00
|
|
|
function getSalt($length=8) {
|
|
|
|
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
|
|
|
|
$key = $pattern{rand(0,35)};
|
|
|
|
for($i=1;$i<$length;$i++)
|
|
|
|
{
|
|
|
|
$key .= $pattern{rand(0,35)};
|
|
|
|
}
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
/**
|
2009-01-02 17:00:25 +01:00
|
|
|
* Return the password in clear text
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2009-01-02 17:00:25 +01:00
|
|
|
* @retval string The password in clear text
|
2008-02-12 18:59:44 +01:00
|
|
|
*/
|
|
|
|
function getClearPassword() {
|
|
|
|
return $this -> clearPassword;
|
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
2009-01-02 17:00:25 +01:00
|
|
|
/**
|
|
|
|
* Error Codes
|
|
|
|
**/
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: defineError('LSattr_ldap_password_01',
|
2009-02-14 00:06:58 +01:00
|
|
|
_("LSattr_ldap_password : Encoding type %{type} is not supported. This password will be stored in clear text.")
|
2009-01-02 17:00:25 +01:00
|
|
|
);
|
2011-04-08 17:07:43 +02:00
|
|
|
LSerror :: defineError('LSattr_ldap_password_02',
|
|
|
|
_("LSattr_ldap_password : Encoding function %{function} is not callable. This password will be stored in clear text.")
|
|
|
|
);
|
|
|
|
|
2007-11-15 19:07:24 +01:00
|
|
|
?>
|