<?php
/*
log onto the directory
host & port are check using a ldap_connect()
register conexion parameters in the session array $ldapmanager
*/
if( isset($_POST) && is_array($_POST) ) {
$form = $_POST;
} else {
$form = $HTTP_POST_VARS;
}
session_unregister( "ldapmanager" );
while( 1 )
{
if( empty($form['host']) or empty($form['port']) ) {
$errorMsg = "Host and port required";
break;
}
$ds = ldap_connect( $form['host'], $form['port'] );
if( ! $ds ) {
$errorMsg = "Bad host or port: $form[host] , $form[port]";
break;
}
if( isset($form['binddn']) && $form['binddn'] != "" ) {
$binddn = $form['binddn'];
$bindpw = $form['bindpw'];
// simple uid given, we search for the DN
if( ! strstr( $binddn, "=" ) ) {
$sr = ldap_search( $ds, $basedn, "uid=$binddn" );
$tmp = ldap_get_entries( $ds, $sr );
if( is_array($tmp) && $tmp['count'] == 1 ) {
$binddn = $tmp[0]['dn'];
} else {
$errorMsg = "Bad username";
break;
}
}
} else {
$binddn = "";
$bindpw = "";
}
// try to bind
if( $binddn != "" ) {
$ret = ldap_bind( $ds, $binddn, $bindpw );
if( ! $ret ) {
$errorMsg = "Bad password ";
break;
}
}
// get available baseDNs from Server
if( isset( $LookupDN ) && isset( $host) && isset($port)) {
$sr = ldap_read( $ds, "", "objectclass=*" );
$entries = ldap_get_entries($ds,$sr );
// echo "<pre>"; print_r( $entries);
if( $entries['count'] == 1 ) {
$contexts = $entries[0]['namingcontexts'];
unset( $contexts['count'] );
$config->basedn = $contexts;
}
$errorMsg = "Available Base DN displayed in baseDN field";
}
ldap_close($ds);
// silently ignore bad input
( $maxTreeItems <= 0 ) && $maxTreeItems = $config->maxtreeitems;
// save connexion params in session
$ldapmanager = array(
"basedn" => $basedn,
"host" => $form['host'],
"port" => $form['port'],
"filter" => 'objectclass=*',
"binddn" => $binddn,
"bindpw" => $bindpw,
"convertCharset" => $form['convertCharset'],
"maxTreeItems" => $form['maxTreeItems'],
);
session_register( "ldapmanager" );
$statusMsg = "Welcome to LDAP Navigator ! You are logged on the LDAP server <b>$ldapmanager[host]:$ldapmanager[port]</b>";
break;
}
if( isset($errorMsg) ) {
include_once( "./modules/authent/login.php" );
exit;
}
// echo "<pre>"; print_r( $ldapmanager ); print_r( $_SESSION );
?>