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

Possibility to debug just one host at a time...

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1232 44740490-163a-0410-bde0-09ae8108e29a
parent c9006018
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Test LDAP connection...
*
* @author Andreas Åkre Solberg, UNINETT AS.
* @package simpleSAMLphp
* @version $Id$
*/
class sspmod_ldapstatus_LDAPTester {
private $orgconfig;
private $debug;
private $debugOutput;
public function __construct($orgconfig, $debug, $output = FALSE) {
$this->orgconfig = $orgconfig;
$this->debug = $debug;
$this->debugOutput = $output;
}
private function is_in_array($needles, $haystack) {
$needles = SimpleSAML_Utilities::arrayize($needles);
foreach($needles AS $needle) {
if (array_key_exists($needle, $haystack) && !empty($haystack[$needle])) return TRUE;
}
return FALSE;
}
private function checkConfig($conf, $req) {
$err = array();
foreach($req AS $r) {
if (!$this->is_in_array($r, $conf)) {
$err[] = 'missing or empty: ' . join(', ', SimpleSAML_Utilities::arrayize($r));
}
}
if (count($err) > 0) {
return array(FALSE, 'Missing: ' . join(', ', $err));
}
return array(TRUE, NULL);
}
private function log($str) {
if ($this->debugOutput) {
echo '<p>' . $str;
} else {
SimpleSAML_Logger::debug('ldapstatus phpping(): ping [' . $host . ':' . $port . ']' );
}
}
private function phpping($host, $port) {
$this->log('ldapstatus phpping(): ping [' . $host . ':' . $port . ']' );
$timeout = 1.0;
$socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
@fclose($socket);
if ($errno) {
return array(FALSE, $errno . ':' . $errstr . ' [' . $host . ':' . $port . ']');
} else {
return array(TRUE,NULL);
}
}
public function test() {
$start = microtime(TRUE);
$result = array();
$this->log('Testing config');
$result['config'] = $this->checkConfig($this->orgconfig, array('description', 'searchbase', 'hostname'));
$this->log('Testing config meta');
$result['configMeta'] = $this->checkConfig($this->orgconfig, array(array('contactMail', 'contactURL')));
$this->log('Testing config testuser');
$result['configTest'] = $this->checkConfig($this->orgconfig, array('testUser', 'testPassword'));
if (!$result['config'][0]) {
$this->log('Skipping because of no configuration');
$result['time'] = microtime(TRUE) - $start;
return $result;
}
$urldef = explode(' ', $this->orgconfig['hostname']);
$url = parse_url($urldef[0]);
$port = 389;
if (!empty($url['scheme']) && $url['scheme'] === 'ldaps') $port = 636;
if (!empty($url['port'])) $port = $url['port'];
$this->log('ldapstatus Url parse [' . $this->orgconfig['hostname'] . '] => [' . $url['host'] . ']:[' . $port . ']' );
$result['ping'] = $this->phpping($url['host'], $port);
if (!$result['ping'][0]) {
$result['time'] = microtime(TRUE) - $start;
$this->log('Skipping because of no ping');
return $result;
}
// LDAP Connect
try {
$ldap = new SimpleSAML_Auth_LDAP($this->orgconfig['hostname'],
(array_key_exists('enable_tls', $this->orgconfig) ? $this->orgconfig['enable_tls'] : FALSE),
$this->debug);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
$result['connect'] = array(TRUE,NULL);
} catch (Exception $e) {
$this->log('ldapstatus: Connect error() [' .$orgkey . ']: ' . $e->getMessage());
$result['connect'] = array(FALSE,$e->getMessage());
$result['time'] = microtime(TRUE) - $start;
return $result;
}
// Bind as admin user
if (isset($this->orgconfig['adminUser'])) {
try {
$this->log('ldapstatus: Admin bind() [' .$orgkey . ']');
$success = $ldap->bind($this->orgconfig['adminUser'], $this->orgconfig['adminPassword']);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
if ($success) {
$result['adminBind'] = array(TRUE,NULL);
} else {
$result['adminBind'] = array(FALSE,'Could not bind()' );
}
} catch (Exception $e) {
$this->log('admin Bind() error:' . $e->getMessage());
$result['adminBind'] = array(FALSE,$e->getMessage());
$result['time'] = microtime(TRUE) - $start;
return $result;
}
}
$eppn = 'asdasdasdasd@feide.no';
// Search for bogus user
try {
$dn = $ldap->searchfordn($this->orgconfig['searchbase'], 'eduPersonPrincipalName', $eppn, TRUE);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
$result['ldapSearchBogus'] = array(TRUE,NULL);
} catch (Exception $e) {
$this->log('LDAP Search bogus:' . $e->getMessage());
$result['ldapSearchBogus'] = array(FALSE,$e->getMessage());
$result['time'] = microtime(TRUE) - $start;
return $result;
}
// If test user is available
if (array_key_exists('testUser', $this->orgconfig)) {
$this->log('Testuser found in config. Performing test with test user.');
// Try to search for DN of test account
try {
$dn = $ldap->searchfordn($this->orgconfig['searchbase'], 'eduPersonPrincipalName', $this->orgconfig['testUser']);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
$result['ldapSearchTestUser'] = array(TRUE,NULL);
} catch (Exception $e) {
$this->log('LDAP Search test account:' . $e->getMessage());
$result['ldapSearchTestUser'] = array(FALSE,$e->getMessage());
$result['time'] = microtime(TRUE) - $start;
return $result;
}
if ($ldap->bind($dn, $this->orgconfig['testPassword'])) {
$result['ldapBindTestUser'] = array(TRUE,NULL);
} else {
$this->log('LDAP Test user bind() failed...');
$result['ldapBindTestUser'] = array(FALSE,NULL);
$result['time'] = microtime(TRUE) - $start;
return $result;
}
try {
$attributes = $ldap->getAttributes($dn, $this->orgconfig['attributes']);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
$result['ldapGetAttributesTestUser'] = array(TRUE,NULL);
} catch(Exception $e) {
$this->log('LDAP Test user attributes failed:' . $e->getMessage());
$result['ldapGetAttributesTestUser'] = array(FALSE,$e->getMessage());
}
}
$result['time'] = microtime(TRUE) - $start;
return $result;
}
}
?>
\ No newline at end of file
...@@ -4,7 +4,14 @@ $this->data['head'] = '<style> ...@@ -4,7 +4,14 @@ $this->data['head'] = '<style>
table.statustable td { table.statustable td {
border-bottom: 1px solid #eee; border-bottom: 1px solid #eee;
} }
a {
color: #333;
text-decoration: none;
border-bottom: 1px dotted #aaa;
}
a:hover {
border-bottom: 1px solid #aaa;
}
</style>'; </style>';
$this->includeAtTemplateBase('includes/header.php'); $this->includeAtTemplateBase('includes/header.php');
...@@ -60,11 +67,13 @@ foreach($this->data['sortedOrgIndex'] as $orgkey) { ...@@ -60,11 +67,13 @@ foreach($this->data['sortedOrgIndex'] as $orgkey) {
$res = $this->data['results'][$orgkey]; $res = $this->data['results'][$orgkey];
echo('<tr class="' . ($classes[($i++ % 2)]) . '">'); echo('<tr class="' . ($classes[($i++ % 2)]) . '">');
if (array_key_exists('description', $this->data['orgconfig'][$orgkey])) { if (array_key_exists('description', $this->data['orgconfig'][$orgkey])) {
echo('<td>' . htmlspecialchars( echo('<td><a href="?orgtest=' . htmlentities($orgkey) . '">');
echo htmlspecialchars(
$this->getTranslation( $this->getTranslation(
SimpleSAML_Utilities::arrayize($this->data['orgconfig'][$orgkey]['description'], 'en') SimpleSAML_Utilities::arrayize($this->data['orgconfig'][$orgkey]['description'], 'en')
) )
) . '</td>'); );
echo('</a></td>');
} else { } else {
echo('<td><span style="color: #b4b4b4; font-size: x-small">NA</span> <tt>' . $orgkey . '</tt></td>'); echo('<td><span style="color: #b4b4b4; font-size: x-small">NA</span> <tt>' . $orgkey . '</tt></td>');
} }
......
...@@ -15,75 +15,42 @@ function myErrorHandler($errno, $errstr, $errfile, $errline) { ...@@ -15,75 +15,42 @@ function myErrorHandler($errno, $errstr, $errfile, $errline) {
switch ($errno) { switch ($errno) {
case E_USER_ERROR: case E_USER_ERROR:
SimpleSAML_Logger::error('PHP_ERROR : [' . $errno . '] ' . $errstr . '. Fatal error on line ' . $errline . ' in file ' . $errfile); echo('<p>PHP_ERROR : [' . $errno . '] ' . $errstr . '. Fatal error on line ' . $errline . ' in file ' . $errfile);
break; break;
case E_USER_WARNING: case E_USER_WARNING:
SimpleSAML_Logger::error('PHP_WARNING : [' . $errno . '] ' . $errstr . '. Warning on line ' . $errline . ' in file ' . $errfile); echo('<p>PHP_WARNING : [' . $errno . '] ' . $errstr . '. Warning on line ' . $errline . ' in file ' . $errfile);
break; break;
case E_USER_NOTICE: case E_USER_NOTICE:
SimpleSAML_Logger::error('PHP_WARNING : [' . $errno . '] ' . $errstr . '. Warning on line ' . $errline . ' in file ' . $errfile); echo('<p>PHP_WARNING : [' . $errno . '] ' . $errstr . '. Warning on line ' . $errline . ' in file ' . $errfile);
break; break;
default: default:
SimpleSAML_Logger::error('PHP_UNKNOWN : [' . $errno . '] ' . $errstr . '. Unknown error on line ' . $errline . ' in file ' . $errfile); echo('<p>PHP_UNKNOWN : [' . $errno . '] ' . $errstr . '. Unknown error on line ' . $errline . ' in file ' . $errfile);
break; break;
} }
/* Don't execute PHP internal error handler */ /* Don't execute PHP internal error handler */
return true; return true;
} }
$old_error_handler = set_error_handler("myErrorHandler");
$ldapconfig = $config->copyFromBase('loginfeide', 'config-login-feide.php'); $ldapconfig = $config->copyFromBase('loginfeide', 'config-login-feide.php');
$ldapStatusConfig = $config->copyFromBase('ldapstatus', 'module_ldapstatus.php'); $ldapStatusConfig = $config->copyFromBase('ldapstatus', 'module_ldapstatus.php');
$pingcommand = $ldapStatusConfig->getValue('ping');
$debug = $ldapconfig->getValue('ldapDebug', FALSE); $debug = $ldapconfig->getValue('ldapDebug', FALSE);
$orgs = $ldapconfig->getValue('orgldapconfig'); $orgs = $ldapconfig->getValue('orgldapconfig');
#echo '<pre>'; print_r($orgs); exit; #echo '<pre>'; print_r($orgs); exit;
function phpping($host, $port) {
SimpleSAML_Logger::debug('ldapstatus phpping(): ping [' . $host . ':' . $port . ']' );
$timeout = 1.0;
$socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
@fclose($socket);
if ($errno) {
return array(FALSE, $errno . ':' . $errstr . ' [' . $host . ':' . $port . ']');
} else {
return array(TRUE,NULL);
}
}
function is_in_array($needles, $haystack) {
$needles = SimpleSAML_Utilities::arrayize($needles);
foreach($needles AS $needle) {
if (array_key_exists($needle, $haystack) && !empty($haystack[$needle])) return TRUE;
}
return FALSE;
}
function checkConfig($conf, $req) {
$err = array();
foreach($req AS $r) {
if (!is_in_array($r, $conf)) {
$err[] = 'missing or empty: ' . join(', ', SimpleSAML_Utilities::arrayize($r));
}
}
if (count($err) > 0) {
return array(FALSE, 'Missing: ' . join(', ', $err));
}
return array(TRUE, NULL);
}
$results = NULL; $results = NULL;
...@@ -103,131 +70,40 @@ $previous = microtime(TRUE); ...@@ -103,131 +70,40 @@ $previous = microtime(TRUE);
$maxtime = $ldapStatusConfig->getValue('maxExecutionTime', 15); $maxtime = $ldapStatusConfig->getValue('maxExecutionTime', 15);
foreach ($orgs AS $orgkey => $orgconfig) { if (array_key_exists('orgtest', $_REQUEST)) {
$old_error_handler = set_error_handler("myErrorHandler");
$previous = microtime(TRUE);
if ((microtime(TRUE) - $start) > $maxtime) { echo('<html><head><style>
SimpleSAML_Logger::debug('ldapstatus: Completing execution after maxtime [' .(microtime(TRUE) - $start) . ' of maxtime ' . $maxtime . ']'); p {
break; font-family: monospace; color: #333;
}
if (array_key_exists($orgkey, $results)) {
SimpleSAML_Logger::debug('ldapstatus: Skipping org already tested [' .$orgkey. ']');
continue;
} else {
SimpleSAML_Logger::debug('ldapstatus: Not Skipping org: [' .$orgkey. ']');
} }
SimpleSAML_Logger::debug('ldapstatus: Executing test on [' .$orgkey . ']');
$results[$orgkey] = array();
$results[$orgkey]['config'] = checkConfig($orgconfig, array('description', 'searchbase', 'hostname'));
$results[$orgkey]['configMeta'] = checkConfig($orgconfig, array(array('contactMail', 'contactURL')));
$results[$orgkey]['configTest'] = checkConfig($orgconfig, array('testUser', 'testPassword'));
if (!$results[$orgkey]['config'][0]) {
$results[$orgkey]['time'] = microtime(TRUE) - $previous;
continue;
}
$urldef = explode(' ', $orgconfig['hostname']);
$url = parse_url($urldef[0]);
$port = 389;
if (!empty($url['scheme']) && $url['scheme'] === 'ldaps') $port = 636;
if (!empty($url['port'])) $port = $url['port'];
SimpleSAML_Logger::debug('ldapstatus Url parse [' . $orgconfig['hostname'] . '] => [' . $url['host'] . ']:[' . $port . ']' ); </style></head><body><h1>Test connection to [' . $_REQUEST['orgtest'] . ']</h1>');
$tester = new sspmod_ldapstatus_LDAPTester($orgs[$_REQUEST['orgtest']], $debug, TRUE);
$res = $tester->test();
echo('<pre>');
print_r($res);
echo('</p>');
echo('</body>');
exit;
}
$results[$orgkey]['ping'] = phpping($url['host'], $port);
if (!$results[$orgkey]['ping'][0]) { // Traverse and execute tests for each entry...
$results[$orgkey]['time'] = microtime(TRUE) - $previous; foreach ($orgs AS $orgkey => $orgconfig) {
continue; if (array_key_exists($orgkey, $results)) continue;
}
// LDAP Connect
try {
$ldap = new SimpleSAML_Auth_LDAP($orgconfig['hostname'], (array_key_exists('enable_tls', $orgconfig) ? $orgconfig['enable_tls'] : FALSE), $debug);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
$results[$orgkey]['connect'] = array(TRUE,NULL);
} catch (Exception $e) {
SimpleSAML_Logger::debug('ldapstatus: Connect error() [' .$orgkey . ']: ' . $e->getMessage());
$results[$orgkey]['connect'] = array(FALSE,$e->getMessage());
$results[$orgkey]['time'] = microtime(TRUE) - $previous;
continue;
}
// Bind as admin user SimpleSAML_Logger::debug('ldapstatus: Executing test on ' . $orgkey);
if (isset($orgconfig['adminUser'])) {
try {
SimpleSAML_Logger::debug('ldapstatus: Admin bind() [' .$orgkey . ']');
$success = $ldap->bind($orgconfig['adminUser'], $orgconfig['adminPassword']);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
if ($success) {
$results[$orgkey]['adminBind'] = array(TRUE,NULL);
} else {
$results[$orgkey]['adminBind'] = array(FALSE,'Could not bind()' );
}
} catch (Exception $e) {
$results[$orgkey]['adminBind'] = array(FALSE,$e->getMessage());
$results[$orgkey]['time'] = microtime(TRUE) - $previous;
continue;
}
}
$tester = new sspmod_ldapstatus_LDAPTester($orgconfig, $debug);
$results[$orgkey] = $tester->test();
$eppn = 'asdasdasdasd@feide.no'; if ((microtime(TRUE) - $start) > $maxtime) {
// Search for bogus user SimpleSAML_Logger::debug('ldapstatus: Completing execution after maxtime [' .(microtime(TRUE) - $start) . ' of maxtime ' . $maxtime . ']');
try { break;
$dn = $ldap->searchfordn($orgconfig['searchbase'], 'eduPersonPrincipalName', $eppn, TRUE);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
$results[$orgkey]['ldapSearchBogus'] = array(TRUE,NULL);
} catch (Exception $e) {
$results[$orgkey]['ldapSearchBogus'] = array(FALSE,$e->getMessage());
$results[$orgkey]['time'] = microtime(TRUE) - $previous;
continue;
}
// If test user is available
if (array_key_exists('testUser', $orgconfig)) {
// Try to search for DN of test account
try {
$dn = $ldap->searchfordn($orgconfig['searchbase'], 'eduPersonPrincipalName', $orgconfig['testUser']);
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
$results[$orgkey]['ldapSearchTestUser'] = array(TRUE,NULL);
} catch (Exception $e) {
$results[$orgkey]['ldapSearchTestUser'] = array(FALSE,$e->getMessage());
$results[$orgkey]['time'] = microtime(TRUE) - $previous;
continue;
}
if ($ldap->bind($dn, $orgconfig['testPassword'])) {
$results[$orgkey]['ldapBindTestUser'] = array(TRUE,NULL);
} else {
$results[$orgkey]['ldapBindTestUser'] = array(FALSE,NULL);
$results[$orgkey]['time'] = microtime(TRUE) - $previous;
continue;
}
try {
$attributes = $ldap->getAttributes($dn, $orgconfig['attributes'], $ldapconfig->getValue('attributesize.max', NULL));
if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
$results[$orgkey]['ldapGetAttributesTestUser'] = array(TRUE,NULL);
} catch(Exception $e) {
$results[$orgkey]['ldapGetAttributesTestUser'] = array(FALSE,$e->getMessage());
}
} }
$results[$orgkey]['time'] = microtime(TRUE) - $previous;
} }
$_SESSION['_ldapstatus_results'] = $results;
$session->setData('module:ldapstatus', 'results', $results); $session->setData('module:ldapstatus', 'results', $results);
#echo '<pre>'; print_r($results); exit; #echo '<pre>'; print_r($results); exit;
...@@ -255,9 +131,6 @@ function resultCode($res) { ...@@ -255,9 +131,6 @@ function resultCode($res) {
} }
return $code; return $code;
} }
$ressortable = array(); $ressortable = array();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment