phpldapadmin/htdocs/login.php

177 lines
5.1 KiB
PHP
Raw Normal View History

2009-06-30 09:22:30 +00:00
<?php
2009-06-30 10:26:08 +00:00
// $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/login.php,v 1.51.2.9 2005/12/31 03:13:48 wurley Exp $
2009-06-30 08:05:37 +00:00
2009-06-30 09:22:30 +00:00
/**
2009-06-30 10:26:08 +00:00
* For servers whose auth_type is set to 'cookie' or 'session'. Pass me the
* login info and I'll write two cookies, pla_login_dn_X and pla_pass_X where X
* is the server_id. The cookie_time comes from config.php
2009-06-30 08:05:37 +00:00
*
2009-06-30 09:29:51 +00:00
* Variables that come in via common.php
* - server_id
2009-06-30 08:05:37 +00:00
* Variables that come in as POST vars:
* - login_dn
2009-06-30 09:29:51 +00:00
* - login_pass
*
* @package phpLDAPadmin
*/
/**
2009-06-30 08:05:37 +00:00
*/
2009-06-30 09:22:30 +00:00
require './common.php';
2009-06-30 09:29:51 +00:00
# Prevents users from coming here without going through the proper channels
if (! isset($ldapserver))
2009-06-30 10:26:08 +00:00
header('Location: index.php');
$dn = isset($_POST['login_dn']) ? $_POST['login_dn'] : null;
$pass = isset($_POST['login_pass']) ? $_POST['login_pass'] : null;
$uid = isset($_POST['uid']) ? $_POST['uid'] : null;
2009-06-30 08:05:37 +00:00
2009-06-30 09:29:51 +00:00
if ($ldapserver->isAnonBindAllowed())
2009-06-30 10:26:08 +00:00
$anon_bind = isset($_POST['anonymous_bind']) && $_POST['anonymous_bind'] == 'on' ? true : false;
2009-06-30 09:29:51 +00:00
else
$anon_bind = false;
2009-06-30 08:05:37 +00:00
2009-06-30 10:26:08 +00:00
if (! $anon_bind)
strlen($pass) or pla_error(_('You left the password blank.'));
2009-06-30 08:05:37 +00:00
2009-06-30 09:29:51 +00:00
$save_auth_type = $ldapserver->auth_type;
2009-06-30 09:22:30 +00:00
2009-06-30 09:29:51 +00:00
if ($anon_bind) {
2009-06-30 09:40:37 +00:00
if (DEBUG_ENABLED)
2009-06-30 10:26:08 +00:00
debug_log('Anonymous Login was posted [%s].',64,$anon_bind);
2009-06-30 09:40:37 +00:00
2009-06-30 08:05:37 +00:00
$dn = null;
$pass = null;
2009-06-30 09:29:51 +00:00
/* Checks if the login_attr option is enabled for this host,
which allows users to login with a simple username like 'jdoe' rather
than the fully qualified DN, 'uid=jdoe,ou=people,,dc=example,dc=com'. */
} elseif ($ldapserver->isLoginAttrEnabled()) {
# Is this a login string (printf-style)
2009-06-30 10:26:08 +00:00
if ($ldapserver->isLoginStringEnabled()) {
$dn = str_replace('<username>',$uid,$ldapserver->getLoginString());
2009-06-30 09:40:37 +00:00
if (DEBUG_ENABLED)
2009-06-30 10:26:08 +00:00
debug_log('LoginStringDN: [%s]',64,$dn);
2009-06-30 09:29:51 +00:00
} else {
# This is a standard login_attr
/* Fake the auth_type of config to do searching. This way, the admin can specify
the DN to use when searching for the login_attr user. */
$ldapserver->auth_type = 'config';
2009-06-30 10:26:08 +00:00
set_error_handler('temp_login_error_handler');
2009-06-30 09:29:51 +00:00
if ($ldapserver->login_dn)
2009-06-30 10:26:08 +00:00
$ldapserver->connect(true,'user');
2009-06-30 09:29:51 +00:00
else
2009-06-30 10:26:08 +00:00
$ldapserver->connect(true,'anonymous');
2009-06-30 09:29:51 +00:00
restore_error_handler();
if (! empty($ldapserver->login_class))
$filter = sprintf('(&(objectClass=%s)(%s=%s))',$ldapserver->login_class,$ldapserver->login_attr,$uid);
else
$filter = sprintf('%s=%s',$ldapserver->login_attr,$uid);
# Got through each of the BASE DNs and test the login.
foreach ($ldapserver->getBaseDN() as $base_dn) {
2009-06-30 09:40:37 +00:00
if (DEBUG_ENABLED)
2009-06-30 10:26:08 +00:00
debug_log('Searching LDAP with base [%s]',64,$base_dn);
2009-06-30 09:29:51 +00:00
2009-06-30 10:26:08 +00:00
$result = array_pop($ldapserver->search(null,$base_dn,$filter,array('dn')));
$dn = $result['dn'];
2009-06-30 09:29:51 +00:00
if ($dn) {
2009-06-30 09:40:37 +00:00
if (DEBUG_ENABLED)
2009-06-30 10:26:08 +00:00
debug_log('Got DN [%s] for user ID [%s]',64,$dn,$uid);
2009-06-30 09:29:51 +00:00
break;
}
}
# If we got here then we werent able to find a DN for the login filter.
if (! $dn)
2009-06-30 10:26:08 +00:00
pla_error(_('Bad username or password. Please try again.'));
2009-06-30 09:29:51 +00:00
# restore the original auth_type
$ldapserver->auth_type = $save_auth_type;
}
2009-06-30 08:05:37 +00:00
}
2009-06-30 08:07:14 +00:00
2009-06-30 09:29:51 +00:00
# We fake a 'config' server auth_type to omit duplicated code
2009-06-30 09:40:37 +00:00
if (DEBUG_ENABLED)
2009-06-30 10:26:08 +00:00
debug_log('Setting login type to CONFIG with DN [%s]',64,$dn);
2009-06-30 09:29:51 +00:00
$save_auth_type = $ldapserver->auth_type;
$ldapserver->auth_type = 'config';
$ldapserver->login_dn = $dn;
$ldapserver->login_pass = $pass;
# Verify that dn is allowed to login
2009-06-30 10:26:08 +00:00
if (! $ldapserver->userIsAllowedLogin($dn))
pla_error(_('Sorry, you are not allowed to use phpLDAPadmin with this LDAP server.'));
2009-06-30 08:05:37 +00:00
2009-06-30 09:40:37 +00:00
if (DEBUG_ENABLED)
2009-06-30 10:26:08 +00:00
debug_log('User is not prohibited from logging in - now bind with DN [%s]',64,$dn);
2009-06-30 09:29:51 +00:00
# verify that the login is good
2009-06-30 10:26:08 +00:00
if (is_null($dn) && is_null($pass))
$ds = $ldapserver->connect(true,'anonymous',true);
2009-06-30 08:10:17 +00:00
else
2009-06-30 10:26:08 +00:00
$ds = $ldapserver->connect(true,'user',true);
2009-06-30 09:29:51 +00:00
2009-06-30 09:40:37 +00:00
if (DEBUG_ENABLED)
2009-06-30 10:26:08 +00:00
debug_log('Connection returned [%s]',64,$ds);
2009-06-30 08:05:37 +00:00
2009-06-30 09:29:51 +00:00
if (! is_resource($ds)) {
if ($anon_bind)
2009-06-30 10:26:08 +00:00
pla_error(_('Could not bind anonymously to server.'),null,null,true);
2009-06-30 08:10:17 +00:00
else
2009-06-30 10:26:08 +00:00
pla_error(_('Bad username or password. Please try again.'),null,null,true);
2009-06-30 09:29:51 +00:00
syslog_notice("Authentification FAILED for $dn");
}
2009-06-30 08:05:37 +00:00
2009-06-30 09:29:51 +00:00
$ldapserver->auth_type = $save_auth_type;
2009-06-30 10:26:08 +00:00
$ldapserver->setLoginDN($dn,$pass,$anon_bind) or pla_error(_('Could not set cookie.'));
2009-06-30 09:29:51 +00:00
set_lastactivity($ldapserver);
2009-06-30 09:22:30 +00:00
2009-06-30 10:26:08 +00:00
if (! $anon_bind) {
2009-06-30 09:29:51 +00:00
syslog_notice("Authentification successful for $dn");
}
2009-06-30 08:09:20 +00:00
2009-06-30 10:26:08 +00:00
pla_session_close();
2009-06-30 08:09:20 +00:00
2009-06-30 09:29:51 +00:00
include './header.php';
2009-06-30 10:26:08 +00:00
echo '<body>';
2009-06-30 08:05:37 +00:00
2009-06-30 10:26:08 +00:00
echo '<script type="text/javascript" language="javascript">';
if ($anon_bind && $config->GetValue('appearance','anonymous_bind_redirect_no_tree'))
printf("parent.location.href='search.php?server_id=%s'",$ldapserver->server_id);
else
echo 'parent.left_frame.location.reload();';
echo '</script>';
echo '<center><br /><br /><br />';
printf(_('Successfully logged into server <b>%s</b>').'<br />',htmlspecialchars($ldapserver->name));
if ($anon_bind)
printf('(%s)',_('Anonymous Bind'));
echo '<br /></center>';
echo '</body></html>';
2009-06-30 08:05:37 +00:00
2009-06-30 09:22:30 +00:00
/**
* Only gets called when we fail to login.
*/
2009-06-30 09:29:51 +00:00
function temp_login_error_handler($errno,$errstr,$file,$lineno) {
if (ini_get('error_reporting') == 0 || error_reporting() == 0)
return;
2009-06-30 10:26:08 +00:00
pla_error(_('Could not connect to LDAP server.').'<br /><br />'.htmlspecialchars($errstr));
2009-06-30 09:22:30 +00:00
}
?>