PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/password/drivers/ldap.php

https://github.com/trimbakgopalghare/roundcubemail
PHP | 298 lines | 205 code | 35 blank | 58 comment | 45 complexity | a45aaaa7f525692c95d1733b9902a48b MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  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 2.0
  9. * @author Edouard MOREAU <edouard.moreau@ensma.fr>
  10. *
  11. * method hashPassword based on code from the phpLDAPadmin development team (http://phpldapadmin.sourceforge.net/).
  12. * method randomSalt based on code from the phpLDAPadmin development team (http://phpldapadmin.sourceforge.net/).
  13. *
  14. */
  15. class rcube_ldap_password
  16. {
  17. public function save($curpass, $passwd)
  18. {
  19. $rcmail = rcmail::get_instance();
  20. require_once 'Net/LDAP2.php';
  21. // Building user DN
  22. if ($userDN = $rcmail->config->get('password_ldap_userDN_mask')) {
  23. $userDN = self::substitute_vars($userDN);
  24. } else {
  25. $userDN = $this->search_userdn($rcmail);
  26. }
  27. if (empty($userDN)) {
  28. return PASSWORD_CONNECT_ERROR;
  29. }
  30. // Connection Method
  31. switch($rcmail->config->get('password_ldap_method')) {
  32. case 'admin':
  33. $binddn = $rcmail->config->get('password_ldap_adminDN');
  34. $bindpw = $rcmail->config->get('password_ldap_adminPW');
  35. break;
  36. case 'user':
  37. default:
  38. $binddn = $userDN;
  39. $bindpw = $curpass;
  40. break;
  41. }
  42. // Configuration array
  43. $ldapConfig = array (
  44. 'binddn' => $binddn,
  45. 'bindpw' => $bindpw,
  46. 'basedn' => $rcmail->config->get('password_ldap_basedn'),
  47. 'host' => $rcmail->config->get('password_ldap_host'),
  48. 'port' => $rcmail->config->get('password_ldap_port'),
  49. 'starttls' => $rcmail->config->get('password_ldap_starttls'),
  50. 'version' => $rcmail->config->get('password_ldap_version'),
  51. );
  52. // Connecting using the configuration array
  53. $ldap = Net_LDAP2::connect($ldapConfig);
  54. // Checking for connection error
  55. if (PEAR::isError($ldap)) {
  56. return PASSWORD_CONNECT_ERROR;
  57. }
  58. $crypted_pass = self::hash_password($passwd, $rcmail->config->get('password_ldap_encodage'));
  59. $force = $rcmail->config->get('password_ldap_force_replace');
  60. $pwattr = $rcmail->config->get('password_ldap_pwattr');
  61. $lchattr = $rcmail->config->get('password_ldap_lchattr');
  62. $smbpwattr = $rcmail->config->get('password_ldap_samba_pwattr');
  63. $smblchattr = $rcmail->config->get('password_ldap_samba_lchattr');
  64. $samba = $rcmail->config->get('password_ldap_samba');
  65. // Support password_ldap_samba option for backward compat.
  66. if ($samba && !$smbpwattr) {
  67. $smbpwattr = 'sambaNTPassword';
  68. $smblchattr = 'sambaPwdLastSet';
  69. }
  70. // Crypt new password
  71. if (!$crypted_pass) {
  72. return PASSWORD_CRYPT_ERROR;
  73. }
  74. // Crypt new samba password
  75. if ($smbpwattr && !($samba_pass = self::hash_password($passwd, 'samba'))) {
  76. return PASSWORD_CRYPT_ERROR;
  77. }
  78. // Writing new crypted password to LDAP
  79. $userEntry = $ldap->getEntry($userDN);
  80. if (Net_LDAP2::isError($userEntry)) {
  81. return PASSWORD_CONNECT_ERROR;
  82. }
  83. if (!$userEntry->replace(array($pwattr => $crypted_pass), $force)) {
  84. return PASSWORD_CONNECT_ERROR;
  85. }
  86. // Updating PasswordLastChange Attribute if desired
  87. if ($lchattr) {
  88. $current_day = (int)(time() / 86400);
  89. if (!$userEntry->replace(array($lchattr => $current_day), $force)) {
  90. return PASSWORD_CONNECT_ERROR;
  91. }
  92. }
  93. // Update Samba password and last change fields
  94. if ($smbpwattr) {
  95. $userEntry->replace(array($smbpwattr => $samba_pass), $force);
  96. }
  97. // Update Samba password last change field
  98. if ($smblchattr) {
  99. $userEntry->replace(array($smblchattr => time()), $force);
  100. }
  101. if (Net_LDAP2::isError($userEntry->update())) {
  102. return PASSWORD_CONNECT_ERROR;
  103. }
  104. // All done, no error
  105. return PASSWORD_SUCCESS;
  106. }
  107. /**
  108. * Bind with searchDN and searchPW and search for the user's DN.
  109. * Use search_base and search_filter defined in config file.
  110. * Return the found DN.
  111. */
  112. function search_userdn($rcmail)
  113. {
  114. $ldapConfig = array (
  115. 'binddn' => $rcmail->config->get('password_ldap_searchDN'),
  116. 'bindpw' => $rcmail->config->get('password_ldap_searchPW'),
  117. 'basedn' => $rcmail->config->get('password_ldap_basedn'),
  118. 'host' => $rcmail->config->get('password_ldap_host'),
  119. 'port' => $rcmail->config->get('password_ldap_port'),
  120. 'starttls' => $rcmail->config->get('password_ldap_starttls'),
  121. 'version' => $rcmail->config->get('password_ldap_version'),
  122. );
  123. $ldap = Net_LDAP2::connect($ldapConfig);
  124. if (PEAR::isError($ldap)) {
  125. return '';
  126. }
  127. $base = self::substitute_vars($rcmail->config->get('password_ldap_search_base'));
  128. $filter = self::substitute_vars($rcmail->config->get('password_ldap_search_filter'));
  129. $options = array (
  130. 'scope' => 'sub',
  131. 'attributes' => array(),
  132. );
  133. $result = $ldap->search($base, $filter, $options);
  134. $ldap->done();
  135. if (PEAR::isError($result) || ($result->count() != 1)) {
  136. return '';
  137. }
  138. return $result->current()->dn();
  139. }
  140. /**
  141. * Substitute %login, %name, %domain, %dc in $str
  142. * See plugin config for details
  143. */
  144. static function substitute_vars($str)
  145. {
  146. $str = str_replace('%login', $_SESSION['username'], $str);
  147. $str = str_replace('%l', $_SESSION['username'], $str);
  148. $parts = explode('@', $_SESSION['username']);
  149. if (count($parts) == 2) {
  150. $dc = 'dc='.strtr($parts[1], array('.' => ',dc=')); // hierarchal domain string
  151. $str = str_replace('%name', $parts[0], $str);
  152. $str = str_replace('%n', $parts[0], $str);
  153. $str = str_replace('%dc', $dc, $str);
  154. $str = str_replace('%domain', $parts[1], $str);
  155. $str = str_replace('%d', $parts[1], $str);
  156. }
  157. return $str;
  158. }
  159. /**
  160. * Code originaly from the phpLDAPadmin development team
  161. * http://phpldapadmin.sourceforge.net/
  162. *
  163. * Hashes a password and returns the hash based on the specified enc_type
  164. */
  165. static function hash_password($password_clear, $encodage_type)
  166. {
  167. $encodage_type = strtolower($encodage_type);
  168. switch ($encodage_type) {
  169. case 'crypt':
  170. $crypted_password = '{CRYPT}' . crypt($password_clear, self::random_salt(2));
  171. break;
  172. case 'ext_des':
  173. /* Extended DES crypt. see OpenBSD crypt man page */
  174. if (!defined('CRYPT_EXT_DES') || CRYPT_EXT_DES == 0) {
  175. /* Your system crypt library does not support extended DES encryption */
  176. return false;
  177. }
  178. $crypted_password = '{CRYPT}' . crypt($password_clear, '_' . self::random_salt(8));
  179. break;
  180. case 'md5crypt':
  181. if (!defined('CRYPT_MD5') || CRYPT_MD5 == 0) {
  182. /* Your system crypt library does not support md5crypt encryption */
  183. return false;
  184. }
  185. $crypted_password = '{CRYPT}' . crypt($password_clear, '$1$' . self::random_salt(9));
  186. break;
  187. case 'blowfish':
  188. if (!defined('CRYPT_BLOWFISH') || CRYPT_BLOWFISH == 0) {
  189. /* Your system crypt library does not support blowfish encryption */
  190. return false;
  191. }
  192. /* Hardcoded to second blowfish version and set number of rounds */
  193. $crypted_password = '{CRYPT}' . crypt($password_clear, '$2a$12$' . self::random_salt(13));
  194. break;
  195. case 'md5':
  196. $crypted_password = '{MD5}' . base64_encode(pack('H*', md5($password_clear)));
  197. break;
  198. case 'sha':
  199. if (function_exists('sha1')) {
  200. /* Use PHP 4.3.0+ sha1 function, if it is available */
  201. $crypted_password = '{SHA}' . base64_encode(pack('H*', sha1($password_clear)));
  202. } else if (function_exists('mhash')) {
  203. $crypted_password = '{SHA}' . base64_encode(mhash(MHASH_SHA1, $password_clear));
  204. } else {
  205. /* Your PHP install does not have the mhash() function */
  206. return false;
  207. }
  208. break;
  209. case 'ssha':
  210. if (function_exists('mhash') && function_exists('mhash_keygen_s2k')) {
  211. mt_srand((double) microtime() * 1000000 );
  212. $salt = mhash_keygen_s2k(MHASH_SHA1, $password_clear, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  213. $crypted_password = '{SSHA}' . base64_encode(mhash(MHASH_SHA1, $password_clear . $salt) . $salt);
  214. } else {
  215. /* Your PHP install does not have the mhash() function */
  216. return false;
  217. }
  218. break;
  219. case 'smd5':
  220. if (function_exists('mhash') && function_exists('mhash_keygen_s2k')) {
  221. mt_srand((double) microtime() * 1000000 );
  222. $salt = mhash_keygen_s2k(MHASH_MD5, $password_clear, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  223. $crypted_password = '{SMD5}' . base64_encode(mhash(MHASH_MD5, $password_clear . $salt) . $salt);
  224. } else {
  225. /* Your PHP install does not have the mhash() function */
  226. return false;
  227. }
  228. break;
  229. case 'samba':
  230. if (function_exists('hash')) {
  231. $crypted_password = hash('md4', rcube_charset::convert($password_clear, RCUBE_CHARSET, 'UTF-16LE'));
  232. $crypted_password = strtoupper($crypted_password);
  233. } else {
  234. /* Your PHP install does not have the hash() function */
  235. return false;
  236. }
  237. break;
  238. case 'ad':
  239. $crypted_password = rcube_charset::convert('"' . $password_clear . '"', RCUBE_CHARSET, 'UTF-16LE');
  240. break;
  241. case 'clear':
  242. default:
  243. $crypted_password = $password_clear;
  244. }
  245. return $crypted_password;
  246. }
  247. /**
  248. * Code originaly from the phpLDAPadmin development team
  249. * http://phpldapadmin.sourceforge.net/
  250. *
  251. * Used to generate a random salt for crypt-style passwords
  252. */
  253. static function random_salt($length)
  254. {
  255. $possible = '0123456789' . 'abcdefghijklmnopqrstuvwxyz' . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . './';
  256. $str = '';
  257. // mt_srand((double)microtime() * 1000000);
  258. while (strlen($str) < $length) {
  259. $str .= substr($possible, (rand() % strlen($possible)), 1);
  260. }
  261. return $str;
  262. }
  263. }