~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/php/phpBB3/includes/auth/auth_ldap.php

  • Committer: William Grant
  • Date: 2009-02-23 23:47:02 UTC
  • mfrom: (1099.1.211 new-dispatch)
  • Revision ID: grantw@unimelb.edu.au-20090223234702-db4b1llly46ignwo
Merge from lp:~ivle-dev/ivle/new-dispatch.

Pretty much everything changes. Reread the setup docs. Backup your databases.
Every file is now in a different installed location, the configuration system
is rewritten, the dispatch system is rewritten, URLs are different, the
database is different, worksheets and exercises are no longer on the
filesystem, we use a templating engine, jail service protocols are rewritten,
we don't repeat ourselves, we have authorization rewritten, phpBB is gone,
and probably lots of other things that I cannot remember.

This is certainly the biggest commit I have ever made, and hopefully
the largest I ever will.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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,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
12
 
*
13
 
*/
14
 
 
15
 
/**
16
 
* @ignore
17
 
*/
18
 
if (!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
 
*/
28
 
function 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
 
*/
99
 
function 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_BREAK,
108
 
                        'error_msg'     => 'NO_PASSWORD_SUPPLIED',
109
 
                );
110
 
        }
111
 
 
112
 
        if (!@extension_loaded('ldap'))
113
 
        {
114
 
                return array(
115
 
                        'status'                => LOGIN_ERROR_EXTERNAL_AUTH,
116
 
                        'error_msg'             => 'LDAP_NO_LDAP_EXTENSION',
117
 
                        'user_row'              => array('user_id' => ANONYMOUS),
118
 
                );
119
 
        }
120
 
 
121
 
        $config['ldap_port'] = (int) $config['ldap_port'];
122
 
        if ($config['ldap_port'])
123
 
        {
124
 
                $ldap = @ldap_connect($config['ldap_server'], $config['ldap_port']);
125
 
        }
126
 
        else
127
 
        {
128
 
                $ldap = @ldap_connect($config['ldap_server']);
129
 
        }
130
 
 
131
 
        if (!$ldap)
132
 
        {
133
 
                return array(
134
 
                        'status'                => LOGIN_ERROR_EXTERNAL_AUTH,
135
 
                        'error_msg'             => 'LDAP_NO_SERVER_CONNECTION',
136
 
                        'user_row'              => array('user_id' => ANONYMOUS),
137
 
                );
138
 
        }
139
 
 
140
 
        @ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
141
 
        @ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
142
 
 
143
 
        if ($config['ldap_user'] || $config['ldap_password'])
144
 
        {
145
 
                if (!@ldap_bind($ldap, $config['ldap_user'], htmlspecialchars_decode($config['ldap_password'])))
146
 
                {
147
 
                        return $user->lang['LDAP_NO_SERVER_CONNECTION'];
148
 
                }
149
 
        }
150
 
 
151
 
        $search = @ldap_search(
152
 
                $ldap,
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']),
156
 
                0,
157
 
                1
158
 
        );
159
 
 
160
 
        $ldap_result = @ldap_get_entries($ldap, $search);
161
 
 
162
 
        if (is_array($ldap_result) && sizeof($ldap_result) > 1)
163
 
        {
164
 
                if (@ldap_bind($ldap, $ldap_result[0]['dn'], htmlspecialchars_decode($password)))
165
 
                {
166
 
                        @ldap_close($ldap);
167
 
 
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);
174
 
 
175
 
                        if ($row)
176
 
                        {
177
 
                                unset($ldap_result);
178
 
 
179
 
                                // User inactive...
180
 
                                if ($row['user_type'] == USER_INACTIVE || $row['user_type'] == USER_IGNORE)
181
 
                                {
182
 
                                        return array(
183
 
                                                'status'                => LOGIN_ERROR_ACTIVE,
184
 
                                                'error_msg'             => 'ACTIVE_ERROR',
185
 
                                                'user_row'              => $row,
186
 
                                        );
187
 
                                }
188
 
 
189
 
                                // Successful login... set user_login_attempts to zero...
190
 
                                return array(
191
 
                                        'status'                => LOGIN_SUCCESS,
192
 
                                        'error_msg'             => false,
193
 
                                        'user_row'              => $row,
194
 
                                );
195
 
                        }
196
 
                        else
197
 
                        {
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);
206
 
 
207
 
                                if (!$row)
208
 
                                {
209
 
                                        trigger_error('NO_GROUP');
210
 
                                }
211
 
 
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,
220
 
                                );
221
 
 
222
 
                                unset($ldap_result);
223
 
 
224
 
                                // this is the user's first login so create an empty profile
225
 
                                return array(
226
 
                                        'status'                => LOGIN_SUCCESS_CREATE_PROFILE,
227
 
                                        'error_msg'             => false,
228
 
                                        'user_row'              => $ldap_user_row,
229
 
                                );
230
 
                        }
231
 
                }
232
 
                else
233
 
                {
234
 
                        unset($ldap_result);
235
 
                        @ldap_close($ldap);
236
 
 
237
 
                        // Give status about wrong password...
238
 
                        return array(
239
 
                                'status'                => LOGIN_ERROR_PASSWORD,
240
 
                                'error_msg'             => 'LOGIN_ERROR_PASSWORD',
241
 
                                'user_row'              => array('user_id' => ANONYMOUS),
242
 
                        );
243
 
                }
244
 
        }
245
 
 
246
 
        @ldap_close($ldap);
247
 
 
248
 
        return array(
249
 
                'status'        => LOGIN_ERROR_USERNAME,
250
 
                'error_msg'     => 'LOGIN_ERROR_USERNAME',
251
 
                'user_row'      => array('user_id' => ANONYMOUS),
252
 
        );
253
 
}
254
 
 
255
 
/**
256
 
* Generates a filter string for ldap_search to find a user
257
 
*
258
 
* @param        $username       string  Username identifying the searched user
259
 
*
260
 
* @return                               string  A filter string for ldap_search
261
 
*/
262
 
function ldap_user_filter($username)
263
 
{
264
 
        global $config;
265
 
 
266
 
        $filter = '(' . $config['ldap_uid'] . '=' . ldap_escape(htmlspecialchars_decode($username)) . ')';
267
 
        if ($config['ldap_user_filter'])
268
 
        {
269
 
                $filter = "(&$filter({$config['ldap_user_filter']}))";
270
 
        }
271
 
        return $filter;
272
 
}
273
 
 
274
 
/**
275
 
* Escapes an LDAP AttributeValue
276
 
*/
277
 
function ldap_escape($string)
278
 
{
279
 
        return str_replace(array('*', '\\', '(', ')'), array('\\*', '\\\\', '\\(', '\\)'), $string);
280
 
}
281
 
 
282
 
/**
283
 
* This function is used to output any required fields in the authentication
284
 
* admin panel. It also defines any required configuration table fields.
285
 
*/
286
 
function acp_ldap(&$new)
287
 
{
288
 
        global $user;
289
 
 
290
 
        $tpl = '
291
 
 
292
 
        <dl>
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>
295
 
        </dl>
296
 
        <dl>
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>
299
 
        </dl>
300
 
        <dl>
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>
303
 
        </dl>
304
 
        <dl>
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>
307
 
        </dl>
308
 
        <dl>
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>
311
 
        </dl>
312
 
        <dl>
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>
315
 
        </dl>
316
 
        <dl>
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>
319
 
        </dl>
320
 
        <dl>
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>
323
 
        </dl>
324
 
        ';
325
 
 
326
 
        // These are fields required in the config table
327
 
        return array(
328
 
                'tpl'           => $tpl,
329
 
                'config'        => array('ldap_server', 'ldap_port', 'ldap_base_dn', 'ldap_uid', 'ldap_user_filter', 'ldap_email', 'ldap_user', 'ldap_password')
330
 
        );
331
 
}
332
 
 
333
 
?>
 
 
b'\\ No newline at end of file'