4
* LDAP auth plug-in for phpBB3
6
* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
9
* @version $Id: auth_ldap.php,v 1.30 2007/10/05 12:42:06 acydburn Exp $
10
* @copyright (c) 2005 phpBB Group
11
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
18
if (!defined('IN_PHPBB'))
24
* Connect to ldap server
25
* Only allow changing authentication to ldap if we can connect to the ldap server
26
* Called in acp_board while setting authentication plugins
30
global $config, $user;
32
if (!@extension_loaded('ldap'))
34
return $user->lang['LDAP_NO_LDAP_EXTENSION'];
37
$config['ldap_port'] = (int) $config['ldap_port'];
38
if ($config['ldap_port'])
40
$ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
44
$ldap = @ldap_connect($config['ldap_server']);
49
return $user->lang['LDAP_NO_SERVER_CONNECTION'];
52
@ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
53
@ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
55
if ($config['ldap_user'] || $config['ldap_password'])
57
if (!@ldap_bind($ldap, htmlspecialchars_decode($config['ldap_user']), htmlspecialchars_decode($config['ldap_password'])))
59
return $user->lang['LDAP_INCORRECT_USER_PASSWORD'];
63
// ldap_connect only checks whether the specified server is valid, so the connection might still fail
64
$search = @ldap_search(
66
$config['ldap_base_dn'],
67
ldap_user_filter($user->data['username']),
68
(empty($config['ldap_email'])) ? array($config['ldap_uid']) : array($config['ldap_uid'], $config['ldap_email']),
73
if ($search === false)
75
return $user->lang['LDAP_NO_SERVER_CONNECTION'];
78
$result = @ldap_get_entries($ldap, $search);
83
if (!is_array($result) || sizeof($result) < 2)
85
return sprintf($user->lang['LDAP_NO_IDENTITY'], $user->data['username']);
88
if (!empty($config['ldap_email']) && !isset($result[0][$config['ldap_email']]))
90
return $user->lang['LDAP_NO_EMAIL'];
99
function login_ldap(&$username, &$password)
101
global $db, $config, $user;
103
// do not allow empty password
107
'status' => LOGIN_BREAK,
108
'error_msg' => 'NO_PASSWORD_SUPPLIED',
112
if (!@extension_loaded('ldap'))
115
'status' => LOGIN_ERROR_EXTERNAL_AUTH,
116
'error_msg' => 'LDAP_NO_LDAP_EXTENSION',
117
'user_row' => array('user_id' => ANONYMOUS),
121
$config['ldap_port'] = (int) $config['ldap_port'];
122
if ($config['ldap_port'])
124
$ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
128
$ldap = @ldap_connect($config['ldap_server']);
134
'status' => LOGIN_ERROR_EXTERNAL_AUTH,
135
'error_msg' => 'LDAP_NO_SERVER_CONNECTION',
136
'user_row' => array('user_id' => ANONYMOUS),
140
@ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
141
@ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
143
if ($config['ldap_user'] || $config['ldap_password'])
145
if (!@ldap_bind($ldap, $config['ldap_user'], htmlspecialchars_decode($config['ldap_password'])))
147
return $user->lang['LDAP_NO_SERVER_CONNECTION'];
151
$search = @ldap_search(
153
$config['ldap_base_dn'],
154
ldap_user_filter($username),
155
(empty($config['ldap_email'])) ? array($config['ldap_uid']) : array($config['ldap_uid'], $config['ldap_email']),
160
$ldap_result = @ldap_get_entries($ldap, $search);
162
if (is_array($ldap_result) && sizeof($ldap_result) > 1)
164
if (@ldap_bind($ldap, $ldap_result[0]['dn'], htmlspecialchars_decode($password)))
168
$sql ='SELECT user_id, username, user_password, user_passchg, user_email, user_type
169
FROM ' . USERS_TABLE . "
170
WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
171
$result = $db->sql_query($sql);
172
$row = $db->sql_fetchrow($result);
173
$db->sql_freeresult($result);
180
if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
183
'status' => LOGIN_ERROR_ACTIVE,
184
'error_msg' => 'ACTIVE_ERROR',
189
// Successful login... set user_login_attempts to zero...
191
'status' => LOGIN_SUCCESS,
192
'error_msg' => false,
198
// retrieve default group id
199
$sql = 'SELECT group_id
200
FROM ' . GROUPS_TABLE . "
201
WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
202
AND group_type = " . GROUP_SPECIAL;
203
$result = $db->sql_query($sql);
204
$row = $db->sql_fetchrow($result);
205
$db->sql_freeresult($result);
209
trigger_error('NO_GROUP');
212
// generate user account data
213
$ldap_user_row = array(
214
'username' => $username,
215
'user_password' => phpbb_hash($password),
216
'user_email' => (!empty($config['ldap_email'])) ? $ldap_result[0][$config['ldap_email']][0] : '',
217
'group_id' => (int) $row['group_id'],
218
'user_type' => USER_NORMAL,
219
'user_ip' => $user->ip,
224
// this is the user's first login so create an empty profile
226
'status' => LOGIN_SUCCESS_CREATE_PROFILE,
227
'error_msg' => false,
228
'user_row' => $ldap_user_row,
237
// Give status about wrong password...
239
'status' => LOGIN_ERROR_PASSWORD,
240
'error_msg' => 'LOGIN_ERROR_PASSWORD',
241
'user_row' => array('user_id' => ANONYMOUS),
249
'status' => LOGIN_ERROR_USERNAME,
250
'error_msg' => 'LOGIN_ERROR_USERNAME',
251
'user_row' => array('user_id' => ANONYMOUS),
256
* Generates a filter string for ldap_search to find a user
258
* @param $username string Username identifying the searched user
260
* @return string A filter string for ldap_search
262
function ldap_user_filter($username)
266
$filter = '(' . $config['ldap_uid'] . '=' . ldap_escape(htmlspecialchars_decode($username)) . ')';
267
if ($config['ldap_user_filter'])
269
$filter = "(&$filter({$config['ldap_user_filter']}))";
275
* Escapes an LDAP AttributeValue
277
function ldap_escape($string)
279
return str_replace(array('*', '\\', '(', ')'), array('\\*', '\\\\', '\\(', '\\)'), $string);
283
* This function is used to output any required fields in the authentication
284
* admin panel. It also defines any required configuration table fields.
286
function acp_ldap(&$new)
293
<dt><label for="ldap_server">' . $user->lang['LDAP_SERVER'] . ':</label><br /><span>' . $user->lang['LDAP_SERVER_EXPLAIN'] . '</span></dt>
294
<dd><input type="text" id="ldap_server" size="40" name="config[ldap_server]" value="' . $new['ldap_server'] . '" /></dd>
297
<dt><label for="ldap_port">' . $user->lang['LDAP_PORT'] . ':</label><br /><span>' . $user->lang['LDAP_PORT_EXPLAIN'] . '</span></dt>
298
<dd><input type="text" id="ldap_port" size="40" name="config[ldap_port]" value="' . $new['ldap_port'] . '" /></dd>
301
<dt><label for="ldap_dn">' . $user->lang['LDAP_DN'] . ':</label><br /><span>' . $user->lang['LDAP_DN_EXPLAIN'] . '</span></dt>
302
<dd><input type="text" id="ldap_dn" size="40" name="config[ldap_base_dn]" value="' . $new['ldap_base_dn'] . '" /></dd>
305
<dt><label for="ldap_uid">' . $user->lang['LDAP_UID'] . ':</label><br /><span>' . $user->lang['LDAP_UID_EXPLAIN'] . '</span></dt>
306
<dd><input type="text" id="ldap_uid" size="40" name="config[ldap_uid]" value="' . $new['ldap_uid'] . '" /></dd>
309
<dt><label for="ldap_user_filter">' . $user->lang['LDAP_USER_FILTER'] . ':</label><br /><span>' . $user->lang['LDAP_USER_FILTER_EXPLAIN'] . '</span></dt>
310
<dd><input type="text" id="ldap_user_filter" size="40" name="config[ldap_user_filter]" value="' . $new['ldap_user_filter'] . '" /></dd>
313
<dt><label for="ldap_email">' . $user->lang['LDAP_EMAIL'] . ':</label><br /><span>' . $user->lang['LDAP_EMAIL_EXPLAIN'] . '</span></dt>
314
<dd><input type="text" id="ldap_email" size="40" name="config[ldap_email]" value="' . $new['ldap_email'] . '" /></dd>
317
<dt><label for="ldap_user">' . $user->lang['LDAP_USER'] . ':</label><br /><span>' . $user->lang['LDAP_USER_EXPLAIN'] . '</span></dt>
318
<dd><input type="text" id="ldap_user" size="40" name="config[ldap_user]" value="' . $new['ldap_user'] . '" /></dd>
321
<dt><label for="ldap_password">' . $user->lang['LDAP_PASSWORD'] . ':</label><br /><span>' . $user->lang['LDAP_PASSWORD_EXPLAIN'] . '</span></dt>
322
<dd><input type="password" id="ldap_password" size="40" name="config[ldap_password]" value="' . $new['ldap_password'] . '" /></dd>
326
// These are fields required in the config table
329
'config' => array('ldap_server', 'ldap_port', 'ldap_base_dn', 'ldap_uid', 'ldap_user_filter', 'ldap_email', 'ldap_user', 'ldap_password')
b'\\ No newline at end of file'