source: branches/old/forum/includes/auth/auth_ldap.php@ 480

Last change on this file since 480 was 400, checked in by george, 16 years ago
  • Přidáno: Nové forum phpBB 3.
File size: 9.2 KB
Line 
1<?php
2/**
3*
4* LDAP auth plug-in for phpBB3
5*
6* Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
7*
8* @package login
9* @version $Id: auth_ldap.php 8479 2008-03-29 00:22:48Z naderman $
10* @copyright (c) 2005 phpBB Group
11* @license http://opensource.org/licenses/gpl-license.php GNU Public License
12*
13*/
14
15/**
16* @ignore
17*/
18if (!defined('IN_PHPBB'))
19{
20 exit;
21}
22
23/**
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
27*/
28function init_ldap()
29{
30 global $config, $user;
31
32 if (!@extension_loaded('ldap'))
33 {
34 return $user->lang['LDAP_NO_LDAP_EXTENSION'];
35 }
36
37 $config['ldap_port'] = (int) $config['ldap_port'];
38 if ($config['ldap_port'])
39 {
40 $ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
41 }
42 else
43 {
44 $ldap = @ldap_connect($config['ldap_server']);
45 }
46
47 if (!$ldap)
48 {
49 return $user->lang['LDAP_NO_SERVER_CONNECTION'];
50 }
51
52 @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
53 @ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
54
55 if ($config['ldap_user'] || $config['ldap_password'])
56 {
57 if (!@ldap_bind($ldap, htmlspecialchars_decode($config['ldap_user']), htmlspecialchars_decode($config['ldap_password'])))
58 {
59 return $user->lang['LDAP_INCORRECT_USER_PASSWORD'];
60 }
61 }
62
63 // ldap_connect only checks whether the specified server is valid, so the connection might still fail
64 $search = @ldap_search(
65 $ldap,
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']),
69 0,
70 1
71 );
72
73 if ($search === false)
74 {
75 return $user->lang['LDAP_NO_SERVER_CONNECTION'];
76 }
77
78 $result = @ldap_get_entries($ldap, $search);
79
80 @ldap_close($ldap);
81
82
83 if (!is_array($result) || sizeof($result) < 2)
84 {
85 return sprintf($user->lang['LDAP_NO_IDENTITY'], $user->data['username']);
86 }
87
88 if (!empty($config['ldap_email']) && !isset($result[0][$config['ldap_email']]))
89 {
90 return $user->lang['LDAP_NO_EMAIL'];
91 }
92
93 return false;
94}
95
96/**
97* Login function
98*/
99function login_ldap(&$username, &$password)
100{
101 global $db, $config, $user;
102
103 // do not allow empty password
104 if (!$password)
105 {
106 return array(
107 'status' => LOGIN_ERROR_PASSWORD,
108 'error_msg' => 'NO_PASSWORD_SUPPLIED',
109 'user_row' => array('user_id' => ANONYMOUS),
110 );
111 }
112
113 if (!$username)
114 {
115 return array(
116 'status' => LOGIN_ERROR_USERNAME,
117 'error_msg' => 'LOGIN_ERROR_USERNAME',
118 'user_row' => array('user_id' => ANONYMOUS),
119 );
120 }
121
122 if (!@extension_loaded('ldap'))
123 {
124 return array(
125 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
126 'error_msg' => 'LDAP_NO_LDAP_EXTENSION',
127 'user_row' => array('user_id' => ANONYMOUS),
128 );
129 }
130
131 $config['ldap_port'] = (int) $config['ldap_port'];
132 if ($config['ldap_port'])
133 {
134 $ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
135 }
136 else
137 {
138 $ldap = @ldap_connect($config['ldap_server']);
139 }
140
141 if (!$ldap)
142 {
143 return array(
144 'status' => LOGIN_ERROR_EXTERNAL_AUTH,
145 'error_msg' => 'LDAP_NO_SERVER_CONNECTION',
146 'user_row' => array('user_id' => ANONYMOUS),
147 );
148 }
149
150 @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
151 @ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
152
153 if ($config['ldap_user'] || $config['ldap_password'])
154 {
155 if (!@ldap_bind($ldap, $config['ldap_user'], htmlspecialchars_decode($config['ldap_password'])))
156 {
157 return $user->lang['LDAP_NO_SERVER_CONNECTION'];
158 }
159 }
160
161 $search = @ldap_search(
162 $ldap,
163 $config['ldap_base_dn'],
164 ldap_user_filter($username),
165 (empty($config['ldap_email'])) ? array($config['ldap_uid']) : array($config['ldap_uid'], $config['ldap_email']),
166 0,
167 1
168 );
169
170 $ldap_result = @ldap_get_entries($ldap, $search);
171
172 if (is_array($ldap_result) && sizeof($ldap_result) > 1)
173 {
174 if (@ldap_bind($ldap, $ldap_result[0]['dn'], htmlspecialchars_decode($password)))
175 {
176 @ldap_close($ldap);
177
178 $sql ='SELECT user_id, username, user_password, user_passchg, user_email, user_type
179 FROM ' . USERS_TABLE . "
180 WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
181 $result = $db->sql_query($sql);
182 $row = $db->sql_fetchrow($result);
183 $db->sql_freeresult($result);
184
185 if ($row)
186 {
187 unset($ldap_result);
188
189 // User inactive...
190 if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
191 {
192 return array(
193 'status' => LOGIN_ERROR_ACTIVE,
194 'error_msg' => 'ACTIVE_ERROR',
195 'user_row' => $row,
196 );
197 }
198
199 // Successful login... set user_login_attempts to zero...
200 return array(
201 'status' => LOGIN_SUCCESS,
202 'error_msg' => false,
203 'user_row' => $row,
204 );
205 }
206 else
207 {
208 // retrieve default group id
209 $sql = 'SELECT group_id
210 FROM ' . GROUPS_TABLE . "
211 WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
212 AND group_type = " . GROUP_SPECIAL;
213 $result = $db->sql_query($sql);
214 $row = $db->sql_fetchrow($result);
215 $db->sql_freeresult($result);
216
217 if (!$row)
218 {
219 trigger_error('NO_GROUP');
220 }
221
222 // generate user account data
223 $ldap_user_row = array(
224 'username' => $username,
225 'user_password' => phpbb_hash($password),
226 'user_email' => (!empty($config['ldap_email'])) ? $ldap_result[0][$config['ldap_email']][0] : '',
227 'group_id' => (int) $row['group_id'],
228 'user_type' => USER_NORMAL,
229 'user_ip' => $user->ip,
230 );
231
232 unset($ldap_result);
233
234 // this is the user's first login so create an empty profile
235 return array(
236 'status' => LOGIN_SUCCESS_CREATE_PROFILE,
237 'error_msg' => false,
238 'user_row' => $ldap_user_row,
239 );
240 }
241 }
242 else
243 {
244 unset($ldap_result);
245 @ldap_close($ldap);
246
247 // Give status about wrong password...
248 return array(
249 'status' => LOGIN_ERROR_PASSWORD,
250 'error_msg' => 'LOGIN_ERROR_PASSWORD',
251 'user_row' => array('user_id' => ANONYMOUS),
252 );
253 }
254 }
255
256 @ldap_close($ldap);
257
258 return array(
259 'status' => LOGIN_ERROR_USERNAME,
260 'error_msg' => 'LOGIN_ERROR_USERNAME',
261 'user_row' => array('user_id' => ANONYMOUS),
262 );
263}
264
265/**
266* Generates a filter string for ldap_search to find a user
267*
268* @param $username string Username identifying the searched user
269*
270* @return string A filter string for ldap_search
271*/
272function ldap_user_filter($username)
273{
274 global $config;
275
276 $filter = '(' . $config['ldap_uid'] . '=' . ldap_escape(htmlspecialchars_decode($username)) . ')';
277 if ($config['ldap_user_filter'])
278 {
279 $filter = "(&$filter({$config['ldap_user_filter']}))";
280 }
281 return $filter;
282}
283
284/**
285* Escapes an LDAP AttributeValue
286*/
287function ldap_escape($string)
288{
289 return str_replace(array('*', '\\', '(', ')'), array('\\*', '\\\\', '\\(', '\\)'), $string);
290}
291
292/**
293* This function is used to output any required fields in the authentication
294* admin panel. It also defines any required configuration table fields.
295*/
296function acp_ldap(&$new)
297{
298 global $user;
299
300 $tpl = '
301
302 <dl>
303 <dt><label for="ldap_server">' . $user->lang['LDAP_SERVER'] . ':</label><br /><span>' . $user->lang['LDAP_SERVER_EXPLAIN'] . '</span></dt>
304 <dd><input type="text" id="ldap_server" size="40" name="config[ldap_server]" value="' . $new['ldap_server'] . '" /></dd>
305 </dl>
306 <dl>
307 <dt><label for="ldap_port">' . $user->lang['LDAP_PORT'] . ':</label><br /><span>' . $user->lang['LDAP_PORT_EXPLAIN'] . '</span></dt>
308 <dd><input type="text" id="ldap_port" size="40" name="config[ldap_port]" value="' . $new['ldap_port'] . '" /></dd>
309 </dl>
310 <dl>
311 <dt><label for="ldap_dn">' . $user->lang['LDAP_DN'] . ':</label><br /><span>' . $user->lang['LDAP_DN_EXPLAIN'] . '</span></dt>
312 <dd><input type="text" id="ldap_dn" size="40" name="config[ldap_base_dn]" value="' . $new['ldap_base_dn'] . '" /></dd>
313 </dl>
314 <dl>
315 <dt><label for="ldap_uid">' . $user->lang['LDAP_UID'] . ':</label><br /><span>' . $user->lang['LDAP_UID_EXPLAIN'] . '</span></dt>
316 <dd><input type="text" id="ldap_uid" size="40" name="config[ldap_uid]" value="' . $new['ldap_uid'] . '" /></dd>
317 </dl>
318 <dl>
319 <dt><label for="ldap_user_filter">' . $user->lang['LDAP_USER_FILTER'] . ':</label><br /><span>' . $user->lang['LDAP_USER_FILTER_EXPLAIN'] . '</span></dt>
320 <dd><input type="text" id="ldap_user_filter" size="40" name="config[ldap_user_filter]" value="' . $new['ldap_user_filter'] . '" /></dd>
321 </dl>
322 <dl>
323 <dt><label for="ldap_email">' . $user->lang['LDAP_EMAIL'] . ':</label><br /><span>' . $user->lang['LDAP_EMAIL_EXPLAIN'] . '</span></dt>
324 <dd><input type="text" id="ldap_email" size="40" name="config[ldap_email]" value="' . $new['ldap_email'] . '" /></dd>
325 </dl>
326 <dl>
327 <dt><label for="ldap_user">' . $user->lang['LDAP_USER'] . ':</label><br /><span>' . $user->lang['LDAP_USER_EXPLAIN'] . '</span></dt>
328 <dd><input type="text" id="ldap_user" size="40" name="config[ldap_user]" value="' . $new['ldap_user'] . '" /></dd>
329 </dl>
330 <dl>
331 <dt><label for="ldap_password">' . $user->lang['LDAP_PASSWORD'] . ':</label><br /><span>' . $user->lang['LDAP_PASSWORD_EXPLAIN'] . '</span></dt>
332 <dd><input type="password" id="ldap_password" size="40" name="config[ldap_password]" value="' . $new['ldap_password'] . '" /></dd>
333 </dl>
334 ';
335
336 // These are fields required in the config table
337 return array(
338 'tpl' => $tpl,
339 'config' => array('ldap_server', 'ldap_port', 'ldap_base_dn', 'ldap_uid', 'ldap_user_filter', 'ldap_email', 'ldap_user', 'ldap_password')
340 );
341}
342
343?>
Note: See TracBrowser for help on using the repository browser.