Skip to content
Snippets Groups Projects
Commit 0c3d72d4 authored by Andreas Åkre Solberg's avatar Andreas Åkre Solberg
Browse files

Added suport for searching on mulitple attribute names. improved Start_tls.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@397 44740490-163a-0410-bde0-09ae8108e29a
parent adb3c87b
No related branches found
No related tags found
No related merge requests found
...@@ -23,18 +23,19 @@ class SimpleSAML_Auth_LDAP { ...@@ -23,18 +23,19 @@ class SimpleSAML_Auth_LDAP {
/** /**
* private constructor restricts instantiaton to getInstance() * private constructor restricts instantiaton to getInstance()
*/ */
public function __construct($hostname,$enable_tls=true) { public function __construct($hostname, $enable_tls=true) {
$this->ldap = @ldap_connect($hostname); $this->ldap = @ldap_connect($hostname);
if (empty($this->ldap)) if (empty($this->ldap))
throw new Exception('Could not connect to LDAP server. Please try again, and if the problem persists, please report the error.'); throw new Exception('Error initializing LDAP connection with PHP LDAP library.');
if (!preg_match("/ldaps:/i",$hostname) and $enable_tls) { $this->setV3();
if ($enable_tls) {
if (!ldap_start_tls($this->ldap)) { if (!ldap_start_tls($this->ldap)) {
throw new Exception('Could not force LDAP into TLS-session. Please verify certificates and configuration'); throw new Exception('Could not force LDAP into TLS-session. Please verify certificates and configuration');
} }
} }
$this->setV3();
} }
...@@ -53,12 +54,12 @@ class SimpleSAML_Auth_LDAP { ...@@ -53,12 +54,12 @@ class SimpleSAML_Auth_LDAP {
*/ */
public function searchfordn($searchbase, $searchattr, $searchvalue) { public function searchfordn($searchbase, $searchattr, $searchvalue) {
SimpleSAML_Logger::debug('Library - LDAP: Search for DN (base:' .
$searchbase . ' attr:' . $searchattr . ' value:' . $searchvalue . ')');
// Search for ePPN // Search for ePPN
$search = '(' . $searchattr . '=' . $searchvalue. ')'; $search = $this->generateSearchFilter($searchattr, $searchvalue);
$search_result = @ldap_search($this->ldap, $searchbase, $search);
SimpleSAML_Logger::debug('Library - LDAP: Search for DN base:' . $searchbase . ' search: ' . $search);
$search_result = @ldap_search($this->ldap, $searchbase, $search, array() );
if ($search_result === false) { if ($search_result === false) {
throw new Exception('Failed performing a LDAP search: ' . ldap_error($this->ldap) . ' search:' . $search); throw new Exception('Failed performing a LDAP search: ' . ldap_error($this->ldap) . ' search:' . $search);
...@@ -86,6 +87,31 @@ class SimpleSAML_Auth_LDAP { ...@@ -86,6 +87,31 @@ class SimpleSAML_Auth_LDAP {
} }
/**
* Generate a search filter for one or more attribute names to match
* one attribute value.
*
* @param $searchattr Can be either an array or a string. Attribute name.
* @param $searchvalue Attribute value to match
* @return A LDAP search filter.
*/
private function generateSearchFilter($searchattr, $searchvalue) {
if (is_array($searchattr)) {
$search = '';
foreach ($searchattr AS $attr) {
$search .= '(' . $attr . '=' . $searchvalue. ')';
}
return '(|' . $search . ')';
} elseif (is_string($searchattr)) {
return '(' . $searchattr . '=' . $searchvalue. ')';
} else {
throw Exception('Search attribute is required to be an array or a string.');
}
}
/** /**
* Bind to LDAP with a specific DN and password. * Bind to LDAP with a specific DN and password.
*/ */
...@@ -115,21 +141,21 @@ class SimpleSAML_Auth_LDAP { ...@@ -115,21 +141,21 @@ class SimpleSAML_Auth_LDAP {
if ($sr === false) if ($sr === false)
throw new Exception('Could not retrieve attributes for user: ' . ldap_error($this->ldap)); throw new Exception('Could not retrieve attributes for user: ' . ldap_error($this->ldap));
$ldapentries = @ldap_get_entries($this->ldap, $sr); $ldapentry = @ldap_get_entries($this->ldap, $sr);
if ($ldapentries === false) if ($ldapentry === false)
throw new Exception('Could not retrieve results from attribute retrieval for user:' . ldap_error($this->ldap)); throw new Exception('Could not retrieve results from attribute retrieval for user:' . ldap_error($this->ldap));
$attributes = array(); $attributes = array();
for ($i = 0; $i < $ldapentries[0]['count']; $i++) { for ($i = 0; $i < $ldapentry[0]['count']; $i++) {
$values = array(); $values = array();
if ($ldapentries[0][$i] == 'jpegphoto') continue; if ($ldapentry[0][$i] == 'jpegphoto') continue;
for ($j = 0; $j < $ldapentries[0][$ldapentries[0][$i]]['count']; $j++) { for ($j = 0; $j < $ldapentry[0][$ldapentry[0][$i]]['count']; $j++) {
$values[] = $ldapentries[0][$ldapentries[0][$i]][$j]; $values[] = $ldapentry[0][$ldapentry[0][$i]][$j];
} }
$attributes[$ldapentries[0][$i]] = $values; $attributes[$ldapentry[0][$i]] = $values;
} }
SimpleSAML_Logger::debug('Library - LDAP: Found attributes (' . join(',', array_keys($attributes)) . ')'); SimpleSAML_Logger::debug('Library - LDAP: Found attributes (' . join(',', array_keys($attributes)) . ')');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment