PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/pf_password/drivers/ldap.php

https://github.com/Studio182/Crystal-Mail
PHP | 186 lines | 105 code | 31 blank | 50 comment | 25 complexity | 2f4d5cdfb3519749ba1c6daccd359089 MD5 | raw file
  1. <?php
  2. /**
  3. * LDAP Password Driver
  4. *
  5. * Driver for passwords stored in LDAP
  6. * This driver use the PEAR Net_LDAP2 class (http://pear.php.net/package/Net_LDAP2).
  7. *
  8. * @version 1.0 (2009-06-24)
  9. * @author Edouard MOREAU <edouard.moreau@ensma.fr>
  10. *
  11. * function hashPassword based on code from the phpLDAPadmin development team (http://phpldapadmin.sourceforge.net/).
  12. * function randomSalt based on code from the phpLDAPadmin development team (http://phpldapadmin.sourceforge.net/).
  13. *
  14. */
  15. function password_save($curpass, $passwd)
  16. {
  17. $cmail = cmail::get_instance();
  18. require_once ('Net/LDAP2.php');
  19. // Building user DN
  20. $userDN = str_replace('%login', $_SESSION['username'], $cmail->config->get('password_ldap_userDN_mask'));
  21. $parts = explode('@', $_SESSION['username']);
  22. if (count($parts) == 2)
  23. {
  24. $userDN = str_replace('%name', $parts[0], $userDN);
  25. $userDN = str_replace('%domain', $parts[1], $userDN);
  26. }
  27. if (empty($userDN)) {return PASSWORD_CONNECT_ERROR;}
  28. // Connection Method
  29. switch($cmail->config->get('password_ldap_method')) {
  30. case 'user': $binddn = $userDN; $bindpw = $curpass; break;
  31. case 'admin': $binddn = $cmail->config->get('password_ldap_adminDN'); $bindpw = $cmail->config->get('password_ldap_adminPW'); break;
  32. default: $binddn = $userDN; $bindpw = $curpass; break; // default is user mode
  33. }
  34. // Configuration array
  35. $ldapConfig = array (
  36. 'binddn' => $binddn,
  37. 'bindpw' => $bindpw,
  38. 'basedn' => $cmail->config->get('password_ldap_basedn'),
  39. 'host' => $cmail->config->get('password_ldap_host'),
  40. 'port' => $cmail->config->get('password_ldap_port'),
  41. 'starttls' => $cmail->config->get('password_ldap_starttls'),
  42. 'version' => $cmail->config->get('password_ldap_version'),
  43. );
  44. // Connecting using the configuration array
  45. $ldap = Net_LDAP2::connect($ldapConfig);
  46. // Checking for connection error
  47. if (PEAR::isError($ldap)) {return PASSWORD_CONNECT_ERROR;}
  48. // Crypting new password
  49. $newCryptedPassword = hashPassword($passwd, $cmail->config->get('password_ldap_encodage'));
  50. if (!$newCryptedPassword) {return PASSWORD_CRYPT_ERROR;}
  51. // Writing new crypted password to LDAP
  52. $userEntry = $ldap->getEntry($userDN);
  53. if (Net_LDAP2::isError($userEntry)) {return PASSWORD_CONNECT_ERROR;}
  54. if (!$userEntry->replace(array($cmail->config->get('password_ldap_pwattr') => $newCryptedPassword),$cmail->config->get('password_ldap_force_replace'))) {return PASSWORD_CONNECT_ERROR;}
  55. if (Net_LDAP2::isError($userEntry->update())) {return PASSWORD_CONNECT_ERROR;}
  56. // All done, no error
  57. return PASSWORD_SUCCESS;
  58. }
  59. /**
  60. * Code originaly from the phpLDAPadmin development team
  61. * http://phpldapadmin.sourceforge.net/
  62. *
  63. * Hashes a password and returns the hash based on the specified enc_type.
  64. *
  65. * @param string $passwordClear The password to hash in clear text.
  66. * @param string $encodageType Standard LDAP encryption type which must be one of
  67. * crypt, ext_des, md5crypt, blowfish, md5, sha, smd5, ssha, or clear.
  68. * @return string The hashed password.
  69. *
  70. */
  71. function hashPassword( $passwordClear, $encodageType )
  72. {
  73. $encodageType = strtolower( $encodageType );
  74. switch( $encodageType ) {
  75. case 'crypt':
  76. $cryptedPassword = '{CRYPT}' . crypt($passwordClear,randomSalt(2));
  77. break;
  78. case 'ext_des':
  79. // extended des crypt. see OpenBSD crypt man page.
  80. if ( ! defined( 'CRYPT_EXT_DES' ) || CRYPT_EXT_DES == 0 ) {return FALSE;} //Your system crypt library does not support extended DES encryption.
  81. $cryptedPassword = '{CRYPT}' . crypt( $passwordClear, '_' . randomSalt(8) );
  82. break;
  83. case 'md5crypt':
  84. if( ! defined( 'CRYPT_MD5' ) || CRYPT_MD5 == 0 ) {return FALSE;} //Your system crypt library does not support md5crypt encryption.
  85. $cryptedPassword = '{CRYPT}' . crypt( $passwordClear , '$1$' . randomSalt(9) );
  86. break;
  87. case 'blowfish':
  88. if( ! defined( 'CRYPT_BLOWFISH' ) || CRYPT_BLOWFISH == 0 ) {return FALSE;} //Your system crypt library does not support blowfish encryption.
  89. $cryptedPassword = '{CRYPT}' . crypt( $passwordClear , '$2a$12$' . randomSalt(13) ); // hardcoded to second blowfish version and set number of rounds
  90. break;
  91. case 'md5':
  92. $cryptedPassword = '{MD5}' . base64_encode( pack( 'H*' , md5( $passwordClear) ) );
  93. break;
  94. case 'sha':
  95. if( function_exists('sha1') ) {
  96. // use php 4.3.0+ sha1 function, if it is available.
  97. $cryptedPassword = '{SHA}' . base64_encode( pack( 'H*' , sha1( $passwordClear) ) );
  98. } elseif( function_exists( 'mhash' ) ) {
  99. $cryptedPassword = '{SHA}' . base64_encode( mhash( MHASH_SHA1, $passwordClear) );
  100. } else {
  101. return FALSE; //Your PHP install does not have the mhash() function. Cannot do SHA hashes.
  102. }
  103. break;
  104. case 'ssha':
  105. if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) {
  106. mt_srand( (double) microtime() * 1000000 );
  107. $salt = mhash_keygen_s2k( MHASH_SHA1, $passwordClear, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 );
  108. $cryptedPassword = "{SSHA}".base64_encode( mhash( MHASH_SHA1, $passwordClear.$salt ).$salt );
  109. } else {
  110. return FALSE; //Your PHP install does not have the mhash() function. Cannot do SHA hashes.
  111. }
  112. break;
  113. case 'smd5':
  114. if( function_exists( 'mhash' ) && function_exists( 'mhash_keygen_s2k' ) ) {
  115. mt_srand( (double) microtime() * 1000000 );
  116. $salt = mhash_keygen_s2k( MHASH_MD5, $passwordClear, substr( pack( "h*", md5( mt_rand() ) ), 0, 8 ), 4 );
  117. $cryptedPassword = "{SMD5}".base64_encode( mhash( MHASH_MD5, $passwordClear.$salt ).$salt );
  118. } else {
  119. return FALSE; //Your PHP install does not have the mhash() function. Cannot do SHA hashes.
  120. }
  121. break;
  122. case 'clear':
  123. default:
  124. $cryptedPassword = $passwordClear;
  125. }
  126. return $cryptedPassword;
  127. }
  128. /**
  129. * Code originaly from the phpLDAPadmin development team
  130. * http://phpldapadmin.sourceforge.net/
  131. *
  132. * Used to generate a random salt for crypt-style passwords. Salt strings are used
  133. * to make pre-built hash cracking dictionaries difficult to use as the hash algorithm uses
  134. * not only the user's password but also a randomly generated string. The string is
  135. * stored as the first N characters of the hash for reference of hashing algorithms later.
  136. *
  137. * --- added 20021125 by bayu irawan <bayuir@divnet.telkom.co.id> ---
  138. * --- ammended 20030625 by S C Rigler <srigler@houston.rr.com> ---
  139. *
  140. * @param int $length The length of the salt string to generate.
  141. * @return string The generated salt string.
  142. */
  143. function randomSalt( $length )
  144. {
  145. $possible = '0123456789'.
  146. 'abcdefghijklmnopqrstuvwxyz'.
  147. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
  148. './';
  149. $str = "";
  150. mt_srand((double)microtime() * 1000000);
  151. while( strlen( $str ) < $length )
  152. $str .= substr( $possible, ( rand() % strlen( $possible ) ), 1 );
  153. return $str;
  154. }
  155. ?>