1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/auth/auth_ldap.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,349 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* LDAP auth plug-in for phpBB3
1.8 +*
1.9 +* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
1.10 +*
1.11 +* @package login
1.12 +* @version $Id$
1.13 +* @copyright (c) 2005 phpBB Group
1.14 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.15 +*
1.16 +*/
1.17 +
1.18 +/**
1.19 +* @ignore
1.20 +*/
1.21 +if (!defined('IN_PHPBB'))
1.22 +{
1.23 + exit;
1.24 +}
1.25 +
1.26 +/**
1.27 +* Connect to ldap server
1.28 +* Only allow changing authentication to ldap if we can connect to the ldap server
1.29 +* Called in acp_board while setting authentication plugins
1.30 +*/
1.31 +function init_ldap()
1.32 +{
1.33 + global $config, $user;
1.34 +
1.35 + if (!@extension_loaded('ldap'))
1.36 + {
1.37 + return $user->lang['LDAP_NO_LDAP_EXTENSION'];
1.38 + }
1.39 +
1.40 + $config['ldap_port'] = (int) $config['ldap_port'];
1.41 + if ($config['ldap_port'])
1.42 + {
1.43 + $ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
1.44 + }
1.45 + else
1.46 + {
1.47 + $ldap = @ldap_connect($config['ldap_server']);
1.48 + }
1.49 +
1.50 + if (!$ldap)
1.51 + {
1.52 + return $user->lang['LDAP_NO_SERVER_CONNECTION'];
1.53 + }
1.54 +
1.55 + @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
1.56 + @ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
1.57 +
1.58 + if ($config['ldap_user'] || $config['ldap_password'])
1.59 + {
1.60 + if (!@ldap_bind($ldap, htmlspecialchars_decode($config['ldap_user']), htmlspecialchars_decode($config['ldap_password'])))
1.61 + {
1.62 + return $user->lang['LDAP_INCORRECT_USER_PASSWORD'];
1.63 + }
1.64 + }
1.65 +
1.66 + // ldap_connect only checks whether the specified server is valid, so the connection might still fail
1.67 + $search = @ldap_search(
1.68 + $ldap,
1.69 + htmlspecialchars_decode($config['ldap_base_dn']),
1.70 + ldap_user_filter($user->data['username']),
1.71 + (empty($config['ldap_email'])) ?
1.72 + array(htmlspecialchars_decode($config['ldap_uid'])) :
1.73 + array(htmlspecialchars_decode($config['ldap_uid']), htmlspecialchars_decode($config['ldap_email'])),
1.74 + 0,
1.75 + 1
1.76 + );
1.77 +
1.78 + if ($search === false)
1.79 + {
1.80 + return $user->lang['LDAP_SEARCH_FAILED'];
1.81 + }
1.82 +
1.83 + $result = @ldap_get_entries($ldap, $search);
1.84 +
1.85 + @ldap_close($ldap);
1.86 +
1.87 +
1.88 + if (!is_array($result) || sizeof($result) < 2)
1.89 + {
1.90 + return sprintf($user->lang['LDAP_NO_IDENTITY'], $user->data['username']);
1.91 + }
1.92 +
1.93 + if (!empty($config['ldap_email']) && !isset($result[0][htmlspecialchars_decode($config['ldap_email'])]))
1.94 + {
1.95 + return $user->lang['LDAP_NO_EMAIL'];
1.96 + }
1.97 +
1.98 + return false;
1.99 +}
1.100 +
1.101 +/**
1.102 +* Login function
1.103 +*/
1.104 +function login_ldap(&$username, &$password)
1.105 +{
1.106 + global $db, $config, $user;
1.107 +
1.108 + // do not allow empty password
1.109 + if (!$password)
1.110 + {
1.111 + return array(
1.112 + 'status' => LOGIN_ERROR_PASSWORD,
1.113 + 'error_msg' => 'NO_PASSWORD_SUPPLIED',
1.114 + 'user_row' => array('user_id' => ANONYMOUS),
1.115 + );
1.116 + }
1.117 +
1.118 + if (!$username)
1.119 + {
1.120 + return array(
1.121 + 'status' => LOGIN_ERROR_USERNAME,
1.122 + 'error_msg' => 'LOGIN_ERROR_USERNAME',
1.123 + 'user_row' => array('user_id' => ANONYMOUS),
1.124 + );
1.125 + }
1.126 +
1.127 + if (!@extension_loaded('ldap'))
1.128 + {
1.129 + return array(
1.130 + 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
1.131 + 'error_msg' => 'LDAP_NO_LDAP_EXTENSION',
1.132 + 'user_row' => array('user_id' => ANONYMOUS),
1.133 + );
1.134 + }
1.135 +
1.136 + $config['ldap_port'] = (int) $config['ldap_port'];
1.137 + if ($config['ldap_port'])
1.138 + {
1.139 + $ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
1.140 + }
1.141 + else
1.142 + {
1.143 + $ldap = @ldap_connect($config['ldap_server']);
1.144 + }
1.145 +
1.146 + if (!$ldap)
1.147 + {
1.148 + return array(
1.149 + 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
1.150 + 'error_msg' => 'LDAP_NO_SERVER_CONNECTION',
1.151 + 'user_row' => array('user_id' => ANONYMOUS),
1.152 + );
1.153 + }
1.154 +
1.155 + @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
1.156 + @ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
1.157 +
1.158 + if ($config['ldap_user'] || $config['ldap_password'])
1.159 + {
1.160 + if (!@ldap_bind($ldap, htmlspecialchars_decode($config['ldap_user']), htmlspecialchars_decode($config['ldap_password'])))
1.161 + {
1.162 + return $user->lang['LDAP_NO_SERVER_CONNECTION'];
1.163 + }
1.164 + }
1.165 +
1.166 + $search = @ldap_search(
1.167 + $ldap,
1.168 + htmlspecialchars_decode($config['ldap_base_dn']),
1.169 + ldap_user_filter($username),
1.170 + (empty($config['ldap_email'])) ?
1.171 + array(htmlspecialchars_decode($config['ldap_uid'])) :
1.172 + array(htmlspecialchars_decode($config['ldap_uid']), htmlspecialchars_decode($config['ldap_email'])),
1.173 + 0,
1.174 + 1
1.175 + );
1.176 +
1.177 + $ldap_result = @ldap_get_entries($ldap, $search);
1.178 +
1.179 + if (is_array($ldap_result) && sizeof($ldap_result) > 1)
1.180 + {
1.181 + if (@ldap_bind($ldap, $ldap_result[0]['dn'], htmlspecialchars_decode($password)))
1.182 + {
1.183 + @ldap_close($ldap);
1.184 +
1.185 + $sql ='SELECT user_id, username, user_password, user_passchg, user_email, user_type
1.186 + FROM ' . USERS_TABLE . "
1.187 + WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
1.188 + $result = $db->sql_query($sql);
1.189 + $row = $db->sql_fetchrow($result);
1.190 + $db->sql_freeresult($result);
1.191 +
1.192 + if ($row)
1.193 + {
1.194 + unset($ldap_result);
1.195 +
1.196 + // User inactive...
1.197 + if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
1.198 + {
1.199 + return array(
1.200 + 'status' => LOGIN_ERROR_ACTIVE,
1.201 + 'error_msg' => 'ACTIVE_ERROR',
1.202 + 'user_row' => $row,
1.203 + );
1.204 + }
1.205 +
1.206 + // Successful login... set user_login_attempts to zero...
1.207 + return array(
1.208 + 'status' => LOGIN_SUCCESS,
1.209 + 'error_msg' => false,
1.210 + 'user_row' => $row,
1.211 + );
1.212 + }
1.213 + else
1.214 + {
1.215 + // retrieve default group id
1.216 + $sql = 'SELECT group_id
1.217 + FROM ' . GROUPS_TABLE . "
1.218 + WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
1.219 + AND group_type = " . GROUP_SPECIAL;
1.220 + $result = $db->sql_query($sql);
1.221 + $row = $db->sql_fetchrow($result);
1.222 + $db->sql_freeresult($result);
1.223 +
1.224 + if (!$row)
1.225 + {
1.226 + trigger_error('NO_GROUP');
1.227 + }
1.228 +
1.229 + // generate user account data
1.230 + $ldap_user_row = array(
1.231 + 'username' => $username,
1.232 + 'user_password' => phpbb_hash($password),
1.233 + 'user_email' => (!empty($config['ldap_email'])) ? utf8_htmlspecialchars($ldap_result[0][htmlspecialchars_decode($config['ldap_email'])][0]) : '',
1.234 + 'group_id' => (int) $row['group_id'],
1.235 + 'user_type' => USER_NORMAL,
1.236 + 'user_ip' => $user->ip,
1.237 + 'user_new' => ($config['new_member_post_limit']) ? 1 : 0,
1.238 + );
1.239 +
1.240 + unset($ldap_result);
1.241 +
1.242 + // this is the user's first login so create an empty profile
1.243 + return array(
1.244 + 'status' => LOGIN_SUCCESS_CREATE_PROFILE,
1.245 + 'error_msg' => false,
1.246 + 'user_row' => $ldap_user_row,
1.247 + );
1.248 + }
1.249 + }
1.250 + else
1.251 + {
1.252 + unset($ldap_result);
1.253 + @ldap_close($ldap);
1.254 +
1.255 + // Give status about wrong password...
1.256 + return array(
1.257 + 'status' => LOGIN_ERROR_PASSWORD,
1.258 + 'error_msg' => 'LOGIN_ERROR_PASSWORD',
1.259 + 'user_row' => array('user_id' => ANONYMOUS),
1.260 + );
1.261 + }
1.262 + }
1.263 +
1.264 + @ldap_close($ldap);
1.265 +
1.266 + return array(
1.267 + 'status' => LOGIN_ERROR_USERNAME,
1.268 + 'error_msg' => 'LOGIN_ERROR_USERNAME',
1.269 + 'user_row' => array('user_id' => ANONYMOUS),
1.270 + );
1.271 +}
1.272 +
1.273 +/**
1.274 +* Generates a filter string for ldap_search to find a user
1.275 +*
1.276 +* @param $username string Username identifying the searched user
1.277 +*
1.278 +* @return string A filter string for ldap_search
1.279 +*/
1.280 +function ldap_user_filter($username)
1.281 +{
1.282 + global $config;
1.283 +
1.284 + $filter = '(' . $config['ldap_uid'] . '=' . ldap_escape(htmlspecialchars_decode($username)) . ')';
1.285 + if ($config['ldap_user_filter'])
1.286 + {
1.287 + $_filter = ($config['ldap_user_filter'][0] == '(' && substr($config['ldap_user_filter'], -1) == ')') ? $config['ldap_user_filter'] : "({$config['ldap_user_filter']})";
1.288 + $filter = "(&{$filter}{$_filter})";
1.289 + }
1.290 + return $filter;
1.291 +}
1.292 +
1.293 +/**
1.294 +* Escapes an LDAP AttributeValue
1.295 +*/
1.296 +function ldap_escape($string)
1.297 +{
1.298 + return str_replace(array('*', '\\', '(', ')'), array('\\*', '\\\\', '\\(', '\\)'), $string);
1.299 +}
1.300 +
1.301 +/**
1.302 +* This function is used to output any required fields in the authentication
1.303 +* admin panel. It also defines any required configuration table fields.
1.304 +*/
1.305 +function acp_ldap(&$new)
1.306 +{
1.307 + global $user;
1.308 +
1.309 + $tpl = '
1.310 +
1.311 + <dl>
1.312 + <dt><label for="ldap_server">' . $user->lang['LDAP_SERVER'] . ':</label><br /><span>' . $user->lang['LDAP_SERVER_EXPLAIN'] . '</span></dt>
1.313 + <dd><input type="text" id="ldap_server" size="40" name="config[ldap_server]" value="' . $new['ldap_server'] . '" /></dd>
1.314 + </dl>
1.315 + <dl>
1.316 + <dt><label for="ldap_port">' . $user->lang['LDAP_PORT'] . ':</label><br /><span>' . $user->lang['LDAP_PORT_EXPLAIN'] . '</span></dt>
1.317 + <dd><input type="text" id="ldap_port" size="40" name="config[ldap_port]" value="' . $new['ldap_port'] . '" /></dd>
1.318 + </dl>
1.319 + <dl>
1.320 + <dt><label for="ldap_dn">' . $user->lang['LDAP_DN'] . ':</label><br /><span>' . $user->lang['LDAP_DN_EXPLAIN'] . '</span></dt>
1.321 + <dd><input type="text" id="ldap_dn" size="40" name="config[ldap_base_dn]" value="' . $new['ldap_base_dn'] . '" /></dd>
1.322 + </dl>
1.323 + <dl>
1.324 + <dt><label for="ldap_uid">' . $user->lang['LDAP_UID'] . ':</label><br /><span>' . $user->lang['LDAP_UID_EXPLAIN'] . '</span></dt>
1.325 + <dd><input type="text" id="ldap_uid" size="40" name="config[ldap_uid]" value="' . $new['ldap_uid'] . '" /></dd>
1.326 + </dl>
1.327 + <dl>
1.328 + <dt><label for="ldap_user_filter">' . $user->lang['LDAP_USER_FILTER'] . ':</label><br /><span>' . $user->lang['LDAP_USER_FILTER_EXPLAIN'] . '</span></dt>
1.329 + <dd><input type="text" id="ldap_user_filter" size="40" name="config[ldap_user_filter]" value="' . $new['ldap_user_filter'] . '" /></dd>
1.330 + </dl>
1.331 + <dl>
1.332 + <dt><label for="ldap_email">' . $user->lang['LDAP_EMAIL'] . ':</label><br /><span>' . $user->lang['LDAP_EMAIL_EXPLAIN'] . '</span></dt>
1.333 + <dd><input type="text" id="ldap_email" size="40" name="config[ldap_email]" value="' . $new['ldap_email'] . '" /></dd>
1.334 + </dl>
1.335 + <dl>
1.336 + <dt><label for="ldap_user">' . $user->lang['LDAP_USER'] . ':</label><br /><span>' . $user->lang['LDAP_USER_EXPLAIN'] . '</span></dt>
1.337 + <dd><input type="text" id="ldap_user" size="40" name="config[ldap_user]" value="' . $new['ldap_user'] . '" /></dd>
1.338 + </dl>
1.339 + <dl>
1.340 + <dt><label for="ldap_password">' . $user->lang['LDAP_PASSWORD'] . ':</label><br /><span>' . $user->lang['LDAP_PASSWORD_EXPLAIN'] . '</span></dt>
1.341 + <dd><input type="password" id="ldap_password" size="40" name="config[ldap_password]" value="' . $new['ldap_password'] . '" autocomplete="off" /></dd>
1.342 + </dl>
1.343 + ';
1.344 +
1.345 + // These are fields required in the config table
1.346 + return array(
1.347 + 'tpl' => $tpl,
1.348 + 'config' => array('ldap_server', 'ldap_port', 'ldap_base_dn', 'ldap_uid', 'ldap_user_filter', 'ldap_email', 'ldap_user', 'ldap_password')
1.349 + );
1.350 +}
1.351 +
1.352 +?>
1.353 \ No newline at end of file