You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ldap_simple.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * Simple LDAP Password Driver
  4. *
  5. * Driver for passwords stored in LDAP
  6. * This driver is based on Edouard's LDAP Password Driver, but does not
  7. * require PEAR's Net_LDAP2 to be installed
  8. *
  9. * @version 2.0
  10. * @author Wout Decre <wout@canodus.be>
  11. * @author Aleksander Machniak <machniak@kolabsys.com>
  12. *
  13. * Copyright (C) 2005-2014, The Roundcube Dev Team
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see http://www.gnu.org/licenses/.
  27. */
  28. class rcube_ldap_simple_password
  29. {
  30. private $debug = false;
  31. function save($curpass, $passwd)
  32. {
  33. $rcmail = rcmail::get_instance();
  34. $this->debug = $rcmail->config->get('ldap_debug');
  35. $ldap_host = $rcmail->config->get('password_ldap_host');
  36. $ldap_port = $rcmail->config->get('password_ldap_port');
  37. $this->_debug("C: Connect to $ldap_host:$ldap_port");
  38. // Connect
  39. if (!$ds = ldap_connect($ldap_host, $ldap_port)) {
  40. $this->_debug("S: NOT OK");
  41. rcube::raise_error(array(
  42. 'code' => 100, 'type' => 'ldap',
  43. 'file' => __FILE__, 'line' => __LINE__,
  44. 'message' => "Could not connect to LDAP server"
  45. ),
  46. true);
  47. return PASSWORD_CONNECT_ERROR;
  48. }
  49. $this->_debug("S: OK");
  50. // Set protocol version
  51. ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $rcmail->config->get('password_ldap_version'));
  52. // Start TLS
  53. if ($rcmail->config->get('password_ldap_starttls')) {
  54. if (!ldap_start_tls($ds)) {
  55. ldap_unbind($ds);
  56. return PASSWORD_CONNECT_ERROR;
  57. }
  58. }
  59. // include 'ldap' driver, we share some static methods with it
  60. require_once INSTALL_PATH . 'plugins/password/drivers/ldap.php';
  61. // other plugins might want to modify user DN
  62. $plugin = $rcmail->plugins->exec_hook('password_ldap_bind', array(
  63. 'user_dn' => '', 'conn' => $ds));
  64. // Build user DN
  65. if (!empty($plugin['user_dn'])) {
  66. $user_dn = $plugin['user_dn'];
  67. }
  68. else if ($user_dn = $rcmail->config->get('password_ldap_userDN_mask')) {
  69. $user_dn = rcube_ldap_password::substitute_vars($user_dn);
  70. }
  71. else {
  72. $user_dn = $this->search_userdn($rcmail, $ds);
  73. }
  74. if (empty($user_dn)) {
  75. ldap_unbind($ds);
  76. return PASSWORD_CONNECT_ERROR;
  77. }
  78. // Connection method
  79. switch ($rcmail->config->get('password_ldap_method')) {
  80. case 'admin':
  81. $binddn = $rcmail->config->get('password_ldap_adminDN');
  82. $bindpw = $rcmail->config->get('password_ldap_adminPW');
  83. break;
  84. case 'user':
  85. default:
  86. $binddn = $user_dn;
  87. $bindpw = $curpass;
  88. break;
  89. }
  90. $lchattr = $rcmail->config->get('password_ldap_lchattr');
  91. $pwattr = $rcmail->config->get('password_ldap_pwattr');
  92. $smbpwattr = $rcmail->config->get('password_ldap_samba_pwattr');
  93. $smblchattr = $rcmail->config->get('password_ldap_samba_lchattr');
  94. $samba = $rcmail->config->get('password_ldap_samba');
  95. $pass_mode = $rcmail->config->get('password_ldap_encodage');
  96. $crypted_pass = password::hash_password($passwd, $pass_mode);
  97. // Support password_ldap_samba option for backward compat.
  98. if ($samba && !$smbpwattr) {
  99. $smbpwattr = 'sambaNTPassword';
  100. $smblchattr = 'sambaPwdLastSet';
  101. }
  102. // Crypt new password
  103. if (!$crypted_pass) {
  104. return PASSWORD_CRYPT_ERROR;
  105. }
  106. // Crypt new Samba password
  107. if ($smbpwattr && !($samba_pass = password::hash_password($passwd, 'samba'))) {
  108. return PASSWORD_CRYPT_ERROR;
  109. }
  110. $this->_debug("C: Bind $binddn, pass: **** [" . strlen($bindpw) . "]");
  111. // Bind
  112. if (!ldap_bind($ds, $binddn, $bindpw)) {
  113. $this->_debug("S: ".ldap_error($ds));
  114. ldap_unbind($ds);
  115. return PASSWORD_CONNECT_ERROR;
  116. }
  117. $this->_debug("S: OK");
  118. $entry[$pwattr] = $crypted_pass;
  119. // Update PasswordLastChange Attribute if desired
  120. if ($lchattr) {
  121. $entry[$lchattr] = (int)(time() / 86400);
  122. }
  123. // Update Samba password
  124. if ($smbpwattr) {
  125. $entry[$smbpwattr] = $samba_pass;
  126. }
  127. // Update Samba password last change
  128. if ($smblchattr) {
  129. $entry[$smblchattr] = time();
  130. }
  131. $this->_debug("C: Modify $user_dn: " . print_r($entry, true));
  132. if (!ldap_modify($ds, $user_dn, $entry)) {
  133. $this->_debug("S: ".ldap_error($ds));
  134. ldap_unbind($ds);
  135. return PASSWORD_CONNECT_ERROR;
  136. }
  137. $this->_debug("S: OK");
  138. // All done, no error
  139. ldap_unbind($ds);
  140. return PASSWORD_SUCCESS;
  141. }
  142. /**
  143. * Bind with searchDN and searchPW and search for the user's DN
  144. * Use search_base and search_filter defined in config file
  145. * Return the found DN
  146. */
  147. function search_userdn($rcmail, $ds)
  148. {
  149. $search_user = $rcmail->config->get('password_ldap_searchDN');
  150. $search_pass = $rcmail->config->get('password_ldap_searchPW');
  151. $search_base = $rcmail->config->get('password_ldap_search_base');
  152. $search_filter = $rcmail->config->get('password_ldap_search_filter');
  153. if (empty($search_filter)) {
  154. return false;
  155. }
  156. $this->_debug("C: Bind " . ($search_user ? $search_user : '[anonymous]'));
  157. // Bind
  158. if (!ldap_bind($ds, $search_user, $search_pass)) {
  159. $this->_debug("S: ".ldap_error($ds));
  160. return false;
  161. }
  162. $this->_debug("S: OK");
  163. $search_base = rcube_ldap_password::substitute_vars($search_base);
  164. $search_filter = rcube_ldap_password::substitute_vars($search_filter);
  165. $this->_debug("C: Search $search_base for $search_filter");
  166. // Search for the DN
  167. if (!$sr = ldap_search($ds, $search_base, $search_filter)) {
  168. $this->_debug("S: ".ldap_error($ds));
  169. return false;
  170. }
  171. $found = ldap_count_entries($ds, $sr);
  172. $this->_debug("S: OK [found $found records]");
  173. // If no or more entries were found, return false
  174. if ($found != 1) {
  175. return false;
  176. }
  177. return ldap_get_dn($ds, ldap_first_entry($ds, $sr));
  178. }
  179. /**
  180. * Prints debug info to the log
  181. */
  182. private function _debug($str)
  183. {
  184. if ($this->debug) {
  185. rcube::write_log('ldap', $str);
  186. }
  187. }
  188. }