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