2 /*******************************************************************************
3 * Copyright (C) 2007 Easter-eggs
4 * http://ldapsaisie.labs.libre-entreprise.org
6 * Author: See AUTHORS file in top-level directory.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ******************************************************************************/
24 * Ldap attribute type password
27 class LSattr_ldap_password extends LSattr_ldap {
29 var $clearPassword = NULL;
32 * Return the display value of this attribute
34 * @param[in] $data mixed The value of this attribute
36 * @retval mixed The display value of this attribute
38 function getDisplayValue($data) {
43 * Return the value of this attribute to be stocked
45 * Note : Password encoding was strongly inspired of the project phpLdapAdmin.
46 * URL : http://phpldapadmin.sourceforge.net/
48 * @param[in] $data mixed The attribute value
50 * @retval mixed The value of this attribute to be stocked
52 function getUpdateData($data) {
53 $this -> clearPassword = $data[0];
54 if (!$this -> config['ldap_options']['encode']) {
55 $this -> config['ldap_options']['encode'] = 'md5crypt';
57 switch($this -> config['ldap_options']['encode']) {
59 if ($this -> config['ldap_options']['no_random_crypt_salt']) {
60 return array('{CRYPT}' . crypt($this -> clearPassword,substr($this -> clearPassword,0,2)));
63 return array('{CRYPT}' . crypt($this -> clearPassword,$this -> getSalt(2)));
67 if ( ! defined( 'CRYPT_EXT_DES' ) || CRYPT_EXT_DES == 0 ) {
68 LSerror :: addErrorCode('LSattr_ldap_password_01','ext_des');
71 return array('{CRYPT}' . crypt( $this -> clearPassword, '_' . $this -> getSalt(8) ));
75 if( ! defined( 'CRYPT_BLOWFISH' ) || CRYPT_BLOWFISH == 0 ) {
76 LSerror :: addErrorCode('LSattr_ldap_password_01','blowfish');
79 return array('{CRYPT}' . crypt( $this -> clearPassword, '$2a$12$' . $this -> getSalt(13) ));
83 if( function_exists('sha1') ) {
84 return array('{SHA}' . base64_encode( pack( 'H*' , sha1( $this -> clearPassword ) ) ));
86 elseif( function_exists( 'mhash' ) ) {
87 return array('{SHA}' . base64_encode( mhash( MHASH_SHA1, $this -> clearPassword ) ));
89 LSerror :: addErrorCode('LSattr_ldap_password_01','sha');
93 if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) {
94 mt_srand( (double) microtime() * 1000000 );
95 $salt = mhash_keygen_s2k( MHASH_SHA1, $this -> clearPassword, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 );
96 return array("{SSHA}".base64_encode( mhash( MHASH_SHA1, $this -> clearPassword.$salt ).$salt ));
99 LSerror :: addErrorCode('LSattr_ldap_password_01','ssha');
103 if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) {
104 mt_srand( (double) microtime() * 1000000 );
105 $salt = mhash_keygen_s2k( MHASH_MD5, $password_clear, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 );
106 return array("{SMD5}".base64_encode( mhash( MHASH_MD5, $password_clear.$salt ).$salt ));
109 LSerror :: addErrorCode('LSattr_ldap_password_01','smd5');
113 return array('{MD5}' . base64_encode( pack( 'H*' , md5( $this -> clearPassword ) ) ));
116 if( ! defined( 'CRYPT_MD5' ) || CRYPT_MD5 == 0 ) {
117 LSerror :: addErrorCode('LSattr_ldap_password_01','md5crypt');
120 return array('{CRYPT}'.crypt($this -> clearPassword,'$1$'.$this -> getSalt().'$'));
124 return array($this -> clearPassword);
127 LSerror :: addErrorCode('LSattr_ldap_password_01',$this -> config['ldap_options']['encode']);
128 return array($this -> clearPassword);
132 * Return salt (random string)
134 * @param[in] integer Number of caracters in this salt
136 * @retval string A salt
138 function getSalt($length=8) {
139 $pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
140 $key = $pattern{rand(0,35)};
141 for($i=1;$i<$length;$i++)
143 $key .= $pattern{rand(0,35)};
149 * Return the password in clear text
151 * @retval string The password in clear text
153 function getClearPassword() {
154 return $this -> clearPassword;
161 LSerror :: defineError('LSattr_ldap_password_01',
162 _("LSattr_ldap_password : Encoding type %{type} is not supported. This password will be stored in clear text.")