|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* LDAP auth plug-in for phpBB3
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package login
|
|
|
4c79b5 |
* @version $Id: auth_ldap.php 8479 2008-03-29 00:22:48Z naderman $
|
|
|
4c79b5 |
* @copyright (c) 2005 phpBB Group
|
|
|
4c79b5 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @ignore
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
if (!defined('IN_PHPBB'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
exit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Connect to ldap server
|
|
|
4c79b5 |
* Only allow changing authentication to ldap if we can connect to the ldap server
|
|
|
4c79b5 |
* Called in acp_board while setting authentication plugins
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function init_ldap()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $config, $user;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!@extension_loaded('ldap'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $user->lang['LDAP_NO_LDAP_EXTENSION'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$config['ldap_port'] = (int) $config['ldap_port'];
|
|
|
4c79b5 |
if ($config['ldap_port'])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$ldap = @ldap_connect($config['ldap_server']);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$ldap)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $user->lang['LDAP_NO_SERVER_CONNECTION'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
@ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
|
4c79b5 |
@ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($config['ldap_user'] || $config['ldap_password'])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!@ldap_bind($ldap, htmlspecialchars_decode($config['ldap_user']), htmlspecialchars_decode($config['ldap_password'])))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $user->lang['LDAP_INCORRECT_USER_PASSWORD'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// ldap_connect only checks whether the specified server is valid, so the connection might still fail
|
|
|
4c79b5 |
$search = @ldap_search(
|
|
|
4c79b5 |
$ldap,
|
|
|
4c79b5 |
$config['ldap_base_dn'],
|
|
|
4c79b5 |
ldap_user_filter($user->data['username']),
|
|
|
4c79b5 |
(empty($config['ldap_email'])) ? array($config['ldap_uid']) : array($config['ldap_uid'], $config['ldap_email']),
|
|
|
4c79b5 |
0,
|
|
|
4c79b5 |
1
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($search === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $user->lang['LDAP_NO_SERVER_CONNECTION'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result = @ldap_get_entries($ldap, $search);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
@ldap_close($ldap);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!is_array($result) || sizeof($result) < 2)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return sprintf($user->lang['LDAP_NO_IDENTITY'], $user->data['username']);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!empty($config['ldap_email']) && !isset($result[0][$config['ldap_email']]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $user->lang['LDAP_NO_EMAIL'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Login function
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function login_ldap(&$username, &$password)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $db, $config, $user;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// do not allow empty password
|
|
|
4c79b5 |
if (!$password)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_ERROR_PASSWORD,
|
|
|
4c79b5 |
'error_msg' => 'NO_PASSWORD_SUPPLIED',
|
|
|
4c79b5 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$username)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_ERROR_USERNAME,
|
|
|
4c79b5 |
'error_msg' => 'LOGIN_ERROR_USERNAME',
|
|
|
4c79b5 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!@extension_loaded('ldap'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_ERROR_EXTERNAL_AUTH,
|
|
|
4c79b5 |
'error_msg' => 'LDAP_NO_LDAP_EXTENSION',
|
|
|
4c79b5 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$config['ldap_port'] = (int) $config['ldap_port'];
|
|
|
4c79b5 |
if ($config['ldap_port'])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$ldap = @ldap_connect($config['ldap_server']);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$ldap)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_ERROR_EXTERNAL_AUTH,
|
|
|
4c79b5 |
'error_msg' => 'LDAP_NO_SERVER_CONNECTION',
|
|
|
4c79b5 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
@ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
|
4c79b5 |
@ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($config['ldap_user'] || $config['ldap_password'])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!@ldap_bind($ldap, $config['ldap_user'], htmlspecialchars_decode($config['ldap_password'])))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $user->lang['LDAP_NO_SERVER_CONNECTION'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$search = @ldap_search(
|
|
|
4c79b5 |
$ldap,
|
|
|
4c79b5 |
$config['ldap_base_dn'],
|
|
|
4c79b5 |
ldap_user_filter($username),
|
|
|
4c79b5 |
(empty($config['ldap_email'])) ? array($config['ldap_uid']) : array($config['ldap_uid'], $config['ldap_email']),
|
|
|
4c79b5 |
0,
|
|
|
4c79b5 |
1
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$ldap_result = @ldap_get_entries($ldap, $search);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (is_array($ldap_result) && sizeof($ldap_result) > 1)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (@ldap_bind($ldap, $ldap_result[0]['dn'], htmlspecialchars_decode($password)))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
@ldap_close($ldap);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql ='SELECT user_id, username, user_password, user_passchg, user_email, user_type
|
|
|
4c79b5 |
FROM ' . USERS_TABLE . "
|
|
|
4c79b5 |
WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
|
|
|
4c79b5 |
$result = $db->sql_query($sql);
|
|
|
4c79b5 |
$row = $db->sql_fetchrow($result);
|
|
|
4c79b5 |
$db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($row)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
unset($ldap_result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// User inactive...
|
|
|
4c79b5 |
if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_ERROR_ACTIVE,
|
|
|
4c79b5 |
'error_msg' => 'ACTIVE_ERROR',
|
|
|
4c79b5 |
'user_row' => $row,
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Successful login... set user_login_attempts to zero...
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_SUCCESS,
|
|
|
4c79b5 |
'error_msg' => false,
|
|
|
4c79b5 |
'user_row' => $row,
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// retrieve default group id
|
|
|
4c79b5 |
$sql = 'SELECT group_id
|
|
|
4c79b5 |
FROM ' . GROUPS_TABLE . "
|
|
|
4c79b5 |
WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
|
|
|
4c79b5 |
AND group_type = " . GROUP_SPECIAL;
|
|
|
4c79b5 |
$result = $db->sql_query($sql);
|
|
|
4c79b5 |
$row = $db->sql_fetchrow($result);
|
|
|
4c79b5 |
$db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$row)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
trigger_error('NO_GROUP');
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// generate user account data
|
|
|
4c79b5 |
$ldap_user_row = array(
|
|
|
4c79b5 |
'username' => $username,
|
|
|
4c79b5 |
'user_password' => phpbb_hash($password),
|
|
|
4c79b5 |
'user_email' => (!empty($config['ldap_email'])) ? $ldap_result[0][$config['ldap_email']][0] : '',
|
|
|
4c79b5 |
'group_id' => (int) $row['group_id'],
|
|
|
4c79b5 |
'user_type' => USER_NORMAL,
|
|
|
4c79b5 |
'user_ip' => $user->ip,
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
unset($ldap_result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// this is the user's first login so create an empty profile
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_SUCCESS_CREATE_PROFILE,
|
|
|
4c79b5 |
'error_msg' => false,
|
|
|
4c79b5 |
'user_row' => $ldap_user_row,
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
unset($ldap_result);
|
|
|
4c79b5 |
@ldap_close($ldap);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Give status about wrong password...
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_ERROR_PASSWORD,
|
|
|
4c79b5 |
'error_msg' => 'LOGIN_ERROR_PASSWORD',
|
|
|
4c79b5 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
@ldap_close($ldap);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'status' => LOGIN_ERROR_USERNAME,
|
|
|
4c79b5 |
'error_msg' => 'LOGIN_ERROR_USERNAME',
|
|
|
4c79b5 |
'user_row' => array('user_id' => ANONYMOUS),
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Generates a filter string for ldap_search to find a user
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param $username string Username identifying the searched user
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @return string A filter string for ldap_search
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function ldap_user_filter($username)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $config;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$filter = '(' . $config['ldap_uid'] . '=' . ldap_escape(htmlspecialchars_decode($username)) . ')';
|
|
|
4c79b5 |
if ($config['ldap_user_filter'])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$filter = "(&$filter({$config['ldap_user_filter']}))";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
return $filter;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Escapes an LDAP AttributeValue
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function ldap_escape($string)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return str_replace(array('*', '\\', '(', ')'), array('\\*', '\\\\', '\\(', '\\)'), $string);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* This function is used to output any required fields in the authentication
|
|
|
4c79b5 |
* admin panel. It also defines any required configuration table fields.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function acp_ldap(&$new)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $user;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$tpl = '
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
<label for="ldap_server">' . $user->lang['LDAP_SERVER'] . ':</label> ' . $user->lang['LDAP_SERVER_EXPLAIN'] . '
|
|
|
4c79b5 |
<input type="text" id="ldap_server" size="40" name="config[ldap_server]" value="' . $new['ldap_server'] . '" />
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
<label for="ldap_port">' . $user->lang['LDAP_PORT'] . ':</label> ' . $user->lang['LDAP_PORT_EXPLAIN'] . '
|
|
|
4c79b5 |
<input type="text" id="ldap_port" size="40" name="config[ldap_port]" value="' . $new['ldap_port'] . '" />
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
<label for="ldap_dn">' . $user->lang['LDAP_DN'] . ':</label> ' . $user->lang['LDAP_DN_EXPLAIN'] . '
|
|
|
4c79b5 |
<input type="text" id="ldap_dn" size="40" name="config[ldap_base_dn]" value="' . $new['ldap_base_dn'] . '" />
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
<label for="ldap_uid">' . $user->lang['LDAP_UID'] . ':</label> ' . $user->lang['LDAP_UID_EXPLAIN'] . '
|
|
|
4c79b5 |
<input type="text" id="ldap_uid" size="40" name="config[ldap_uid]" value="' . $new['ldap_uid'] . '" />
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
<label for="ldap_user_filter">' . $user->lang['LDAP_USER_FILTER'] . ':</label> ' . $user->lang['LDAP_USER_FILTER_EXPLAIN'] . '
|
|
|
4c79b5 |
<input type="text" id="ldap_user_filter" size="40" name="config[ldap_user_filter]" value="' . $new['ldap_user_filter'] . '" />
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
<label for="ldap_email">' . $user->lang['LDAP_EMAIL'] . ':</label> ' . $user->lang['LDAP_EMAIL_EXPLAIN'] . '
|
|
|
4c79b5 |
<input type="text" id="ldap_email" size="40" name="config[ldap_email]" value="' . $new['ldap_email'] . '" />
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
<label for="ldap_user">' . $user->lang['LDAP_USER'] . ':</label> ' . $user->lang['LDAP_USER_EXPLAIN'] . '
|
|
|
4c79b5 |
<input type="text" id="ldap_user" size="40" name="config[ldap_user]" value="' . $new['ldap_user'] . '" />
|
|
|
4c79b5 |
|
|
|
4c79b5 |
|
|
|
4c79b5 |
<label for="ldap_password">' . $user->lang['LDAP_PASSWORD'] . ':</label> ' . $user->lang['LDAP_PASSWORD_EXPLAIN'] . '
|
|
|
4c79b5 |
<input type="password" id="ldap_password" size="40" name="config[ldap_password]" value="' . $new['ldap_password'] . '" />
|
|
|
4c79b5 |
|
|
|
4c79b5 |
';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// These are fields required in the config table
|
|
|
4c79b5 |
return array(
|
|
|
4c79b5 |
'tpl' => $tpl,
|
|
|
4c79b5 |
'config' => array('ldap_server', 'ldap_port', 'ldap_base_dn', 'ldap_uid', 'ldap_user_filter', 'ldap_email', 'ldap_user', 'ldap_password')
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
?>
|