Skip to content

Commit

Permalink
Password: De-duplicate ldap drivers code
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed Mar 9, 2019
1 parent 2ea9d4f commit 0751c1c
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 250 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CHANGELOG Roundcube Webmail
===========================

- Password: Added ldap_exop driver (#4992)
- Elastic: Changed read/unread icons (#6636)
- Elastic: Changed "Move to..." icon (#6637)
- Elastic: Add hide/show for advanced preferences (#6632)
Expand Down
34 changes: 4 additions & 30 deletions plugins/password/drivers/ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public function save($curpass, $passwd)
{
$rcmail = rcmail::get_instance();
require_once 'Net/LDAP2.php';
require_once __DIR__ . '/ldap_simple.php';

// Building user DN
if ($userDN = $rcmail->config->get('password_ldap_userDN_mask')) {
$userDN = self::substitute_vars($userDN);
$userDN = rcube_ldap_simple_password::substitute_vars($userDN);
}
else {
$userDN = $this->search_userdn($rcmail);
Expand Down Expand Up @@ -179,8 +180,8 @@ function search_userdn($rcmail)
return '';
}

$base = self::substitute_vars($rcmail->config->get('password_ldap_search_base'));
$filter = self::substitute_vars($rcmail->config->get('password_ldap_search_filter'));
$base = rcube_ldap_simple_password::substitute_vars($rcmail->config->get('password_ldap_search_base'));
$filter = rcube_ldap_simple_password::substitute_vars($rcmail->config->get('password_ldap_search_filter'));
$options = array (
'scope' => 'sub',
'attributes' => array(),
Expand All @@ -196,31 +197,4 @@ function search_userdn($rcmail)

return $userDN;
}

/**
* Substitute %login, %name, %domain, %dc in $str
* See plugin config for details
*/
static function substitute_vars($str)
{
$str = str_replace('%login', $_SESSION['username'], $str);
$str = str_replace('%l', $_SESSION['username'], $str);

$parts = explode('@', $_SESSION['username']);

if (count($parts) == 2) {
$dc = 'dc='.strtr($parts[1], array('.' => ',dc=')); // hierarchal domain string

$str = str_replace('%name', $parts[0], $str);
$str = str_replace('%n', $parts[0], $str);
$str = str_replace('%dc', $dc, $str);
$str = str_replace('%domain', $parts[1], $str);
$str = str_replace('%d', $parts[1], $str);
} else if ( count($parts) == 1) {
$str = str_replace('%name', $parts[0], $str);
$str = str_replace('%n', $parts[0], $str);
}

return $str;
}
}
164 changes: 14 additions & 150 deletions plugins/password/drivers/ldap_exop.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

class rcube_ldap_exop_password
require_once __DIR__ . '/ldap_simple.php';

class rcube_ldap_exop_password extends rcube_ldap_simple_password
{
private $debug = false;

Expand All @@ -40,103 +42,24 @@ function save($curpass, $passwd)
'message' => "ldap_exop_passwd not supported"
),
true);
return PASSWORD_CONNECT_ERROR;
}

$rcmail = rcmail::get_instance();

$this->debug = $rcmail->config->get('ldap_debug');

$ldap_host = $rcmail->config->get('password_ldap_host', 'localhost');
$ldap_port = $rcmail->config->get('password_ldap_port', '389');

$this->_debug("C: Connect to $ldap_host:$ldap_port");

// Connect
if (!$ds = ldap_connect($ldap_host, $ldap_port)) {
$this->_debug("S: NOT OK");

rcube::raise_error(array(
'code' => 100, 'type' => 'ldap',
'file' => __FILE__, 'line' => __LINE__,
'message' => "Could not connect to LDAP server"
),
true);

return PASSWORD_CONNECT_ERROR;
return PASSWORD_ERROR;
}

$this->_debug("S: OK");

// Set protocol version
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION,
$rcmail->config->get('password_ldap_version', '3'));

// Start TLS
if ($rcmail->config->get('password_ldap_starttls')) {
if (!ldap_start_tls($ds)) {
ldap_unbind($ds);
return PASSWORD_CONNECT_ERROR;
}
// Connect and bind
$ret = $this->connect($curpass);
if ($ret !== true) {
return $ret;
}

// include 'ldap' driver, we share some static methods with it
require_once INSTALL_PATH . 'plugins/password/drivers/ldap.php';
if (!ldap_exop_passwd($this->conn, $this->user, $curpass, $passwd)) {
$this->_debug("S: ".ldap_error($this->conn));

// other plugins might want to modify user DN
$plugin = $rcmail->plugins->exec_hook('password_ldap_bind', array(
'user_dn' => '', 'conn' => $ds));
$errno = ldap_errno($this->conn);

// Build user DN
if (!empty($plugin['user_dn'])) {
$user_dn = $plugin['user_dn'];
}
else if ($user_dn = $rcmail->config->get('password_ldap_userDN_mask')) {
$user_dn = rcube_ldap_password::substitute_vars($user_dn);
}
else {
$user_dn = $this->search_userdn($rcmail, $ds);
}
ldap_unbind($this->conn);

if (empty($user_dn)) {
ldap_unbind($ds);
return PASSWORD_CONNECT_ERROR;
}

// Connection method
switch ($rcmail->config->get('password_ldap_method')) {
case 'admin':
$binddn = $rcmail->config->get('password_ldap_adminDN');
$bindpw = $rcmail->config->get('password_ldap_adminPW');
break;
case 'user':
default:
$binddn = $user_dn;
$bindpw = $curpass;
break;
}

$this->_debug("C: Bind $binddn, pass: **** [" . strlen($bindpw) . "]");

// Bind
if (!ldap_bind($ds, $binddn, $bindpw)) {
$this->_debug("S: ".ldap_error($ds));

ldap_unbind($ds);

return PASSWORD_CONNECT_ERROR;
}

$this->_debug("S: OK");

if (!ldap_exop_passwd($ds, $user_dn, $curpass, $passwd)) {
$this->_debug("S: ".ldap_error($ds));

$errno = ldap_errno($ds);

ldap_unbind($ds);

if ($errno == 0x13) { // LDAP_CONSTRAINT_VIOLATION
if ($errno == 0x13) {
return PASSWORD_CONSTRAINT_VIOLATION;
}

Expand All @@ -146,67 +69,8 @@ function save($curpass, $passwd)
$this->_debug("S: OK");

// All done, no error
ldap_unbind($ds);
ldap_unbind($this->conn);

return PASSWORD_SUCCESS;
}

/**
* Bind with searchDN and searchPW and search for the user's DN
* Use search_base and search_filter defined in config file
* Return the found DN
*/
function search_userdn($rcmail, $ds)
{
$search_user = $rcmail->config->get('password_ldap_searchDN');
$search_pass = $rcmail->config->get('password_ldap_searchPW');
$search_base = $rcmail->config->get('password_ldap_search_base');
$search_filter = $rcmail->config->get('password_ldap_search_filter');

if (empty($search_filter)) {
return false;
}

$this->_debug("C: Bind " . ($search_user ? $search_user : '[anonymous]'));

// Bind
if (!ldap_bind($ds, $search_user, $search_pass)) {
$this->_debug("S: ".ldap_error($ds));
return false;
}

$this->_debug("S: OK");

$search_base = rcube_ldap_password::substitute_vars($search_base);
$search_filter = rcube_ldap_password::substitute_vars($search_filter);

$this->_debug("C: Search $search_base for $search_filter");

// Search for the DN
if (!$sr = ldap_search($ds, $search_base, $search_filter)) {
$this->_debug("S: ".ldap_error($ds));
return false;
}

$found = ldap_count_entries($ds, $sr);

$this->_debug("S: OK [found $found records]");

// If no or more entries were found, return false
if ($found != 1) {
return false;
}

return ldap_get_dn($ds, ldap_first_entry($ds, $sr));
}

/**
* Prints debug info to the log
*/
private function _debug($str)
{
if ($this->debug) {
rcube::write_log('ldap', $str);
}
}
}
Loading

0 comments on commit 0751c1c

Please sign in to comment.