/
AuthLDAP.php
323 lines (290 loc) · 12.3 KB
/
AuthLDAP.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
class AuthLDAP extends AuthPluginBase
{
protected $storage = 'DbStorage';
static protected $description = 'Core: LDAP authentication';
static protected $name = 'LDAP';
/**
* Can we autocreate users? For the moment this is disabled, will be moved
* to a setting when we have more robust user creation system.
*
* @var boolean
*/
protected $autoCreate = false;
protected $settings = array(
'server' => array(
'type' => 'string',
'label' => 'Ldap server e.g. ldap://ldap.mydomain.com or ldaps://ldap.mydomain.com'
),
'ldapport' => array(
'type' => 'string',
'label' => 'Port number (default when omitted is 389)'
),
'ldapversion' => array(
'type' => 'select',
'label' => 'LDAP version',
'options' => array('2' => 'LDAPv2', '3' => 'LDAPv3'),
'default' => '2',
'submitonchange'=> true
),
'ldapoptreferrals' => array(
'type' => 'boolean',
'label' => 'Select true if referrals must be followed (use false for ActiveDirectory)',
'default' => '0'
),
'ldaptls' => array(
'type' => 'boolean',
'label' => 'Check to enable Start-TLS encryption When using LDAPv3',
'default' => '0'
),
'ldapmode' => array(
'type' => 'select',
'label' => 'Select how to perform authentication.',
'options' => array("simplebind" => "Simple bind", "searchandbind" => "Search and bind"),
'default' => "simplebind",
'submitonchange'=> true
),
'userprefix' => array(
'type' => 'string',
'label' => 'Username prefix cn= or uid=',
),
'domainsuffix' => array(
'type' => 'string',
'label' => 'Username suffix e.g. @mydomain.com or remaining part of ldap query'
),
'searchuserattribute' => array(
'type' => 'string',
'label' => 'Attribute to compare to the given login can be uid, cn, mail, ...'
),
'usersearchbase' => array(
'type' => 'string',
'label' => 'Base DN for the user search operation'
),
'extrauserfilter' => array(
'type' => 'string',
'label' => 'Optional extra LDAP filter to be ANDed to the basic (searchuserattribute=username) filter. Don\'t forget the outmost enclosing parentheses'
),
'binddn' => array(
'type' => 'string',
'label' => 'Optional DN of the LDAP account used to search for the end-user\'s DN. An anonymous bind is performed if empty.'
),
'bindpwd' => array(
'type' => 'string',
'label' => 'Password of the LDAP account used to search for the end-user\'s DN if previoulsy set.'
),
'is_default' => array(
'type' => 'checkbox',
'label' => 'Check to make default authentication method'
)
);
public function __construct(PluginManager $manager, $id) {
parent::__construct($manager, $id);
/**
* Here you should handle subscribing to the events your plugin will handle
*/
$this->subscribe('beforeLogin');
$this->subscribe('newLoginForm');
$this->subscribe('afterLoginFormSubmit');
$this->subscribe('newUserSession');
}
public function beforeLogin()
{
if ($this->get('is_default', null, null, false) == true) {
// This is configured to be the default login method
$this->getEvent()->set('default', get_class($this));
}
}
public function newLoginForm()
{
$this->getEvent()->getContent($this)
->addContent(CHtml::tag('li', array(), "<label for='user'>" . gT("Username") . "</label><input name='user' id='user' type='text' size='40' maxlength='40' value='' />"))
->addContent(CHtml::tag('li', array(), "<label for='password'>" . gT("Password") . "</label><input name='password' id='password' type='password' size='40' maxlength='40' value='' />"));
}
/**
* Modified getPluginSettings since we have a select box that autosubmits
* and we only want to show the relevant options.
*
* @param boolean $getValues
* @return array
*/
public function getPluginSettings($getValues = true)
{
$aPluginSettings = parent::getPluginSettings($getValues);
if ($getValues) {
$ldapmode = $aPluginSettings['ldapmode']['current'];
$ldapver = $aPluginSettings['ldapversion']['current'];
// If it is a post request, it could be an autosubmit so read posted
// value over the saved value
if (App()->request->isPostRequest) {
$ldapmode = App()->request->getPost('ldapmode', $ldapmode);
$aPluginSettings['ldapmode']['current'] = $ldapmode;
$ldapver = App()->request->getPost('ldapversion', $ldapver);
$aPluginSettings['ldapversion']['current'] = $ldapver;
}
if ($ldapver == '2' ) {
unset($aPluginSettings['ldaptls']);
}
if ($ldapmode == 'searchandbind') {
// Hide simple settings
unset($aPluginSettings['userprefix']);
unset($aPluginSettings['domainsuffix']);
} else {
// Hide searchandbind settings
unset($aPluginSettings['searchuserattribute']);
unset($aPluginSettings['usersearchbase']);
unset($aPluginSettings['extrauserfilter']);
unset($aPluginSettings['binddn']);
unset($aPluginSettings['bindpwd']);
unset($aPluginSettings['ldapoptreferrals']);
}
}
return $aPluginSettings;
}
public function newUserSession()
{
// Do nothing if this user is not Authdb type
$identity = $this->getEvent()->get('identity');
if ($identity->plugin != 'AuthLDAP')
{
return;
}
// Here we do the actual authentication
$username = $this->getUsername();
$password = $this->getPassword();
$user = $this->api->getUserByName($username);
if ($user === null && $this->autoCreate === false)
{
// If the user doesnt exist ín the LS database, he can not login
$this->setAuthFailure(self::ERROR_USERNAME_INVALID);
return;
}
if (empty($password))
{
// If password is null or blank reject login
// This is necessary because in simple bind ldap server authenticates with blank password
$this->setAuthFailure(self::ERROR_PASSWORD_INVALID);
return;
}
// Get configuration settings:
$ldapserver = $this->get('server');
$ldapport = $this->get('ldapport');
$ldapver = $this->get('ldapversion');
$ldaptls = $this->get('ldaptls');
$ldapoptreferrals = $this->get('ldapoptreferrals');
$ldapmode = $this->get('ldapmode');
$suffix = $this->get('domainsuffix');
$prefix = $this->get('userprefix');
$searchuserattribute = $this->get('searchuserattribute');
$extrauserfilter = $this->get('extrauserfilter');
$usersearchbase = $this->get('usersearchbase');
$binddn = $this->get('binddn');
$bindpwd = $this->get('bindpwd');
if (empty($ldapport)) {
$ldapport = 389;
}
// Try to connect
$ldapconn = ldap_connect($ldapserver, (int) $ldapport);
if (false == $ldapconn) {
$this->setAuthFailure(1, gT('Could not connect to LDAP server.'));
return;
}
// using LDAP version
if ($ldapver === null)
{
// If the version hasn't been set, default = 2
$ldapver = 2;
}
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, $ldapver);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, $ldapoptreferrals);
if (!empty($ldaptls) && $ldaptls == '1' && $ldapver == 3 && preg_match("/^ldaps:\/\//", $ldapserver) == 0 )
{
// starting TLS secure layer
if(!ldap_start_tls($ldapconn))
{
$this->setAuthFailure(100, ldap_error($ldapconn));
ldap_close($ldapconn); // all done? close connection
return;
}
}
if (empty($ldapmode) || $ldapmode=='simplebind')
{
// in simple bind mode we know how to construct the userDN from the username
$ldapbind = @ldap_bind($ldapconn, $prefix . $username . $suffix, $password);
}
else
{
// in search and bind mode we first do a LDAP search from the username given
// to foind the userDN and then we procced to the bind operation
if (empty($binddn))
{
// There is no account defined to do the LDAP search,
// let's use anonymous bind instead
$ldapbindsearch = @ldap_bind($ldapconn);
}
else
{
// An account is defined to do the LDAP search, let's use it
$ldapbindsearch = @ldap_bind($ldapconn, $binddn, $bindpwd);
}
if (!$ldapbindsearch) {
$this->setAuthFailure(100, ldap_error($ldapconn));
ldap_close($ldapconn); // all done? close connection
return;
}
// Now prepare the search fitler
if ( $extrauserfilter != "")
{
$usersearchfilter = "(&($searchuserattribute=$username)$extrauserfilter)";
}
else
{
$usersearchfilter = "($searchuserattribute=$username)";
}
// Search for the user
$dnsearchres = ldap_search($ldapconn, $usersearchbase, $usersearchfilter, array($searchuserattribute));
$rescount=ldap_count_entries($ldapconn,$dnsearchres);
if ($rescount == 1)
{
$userentry=ldap_get_entries($ldapconn, $dnsearchres);
$userdn = $userentry[0]["dn"];
}
else
{
// if no entry or more than one entry returned
// then deny authentication
$this->setAuthFailure(100, ldap_error($ldapconn));
ldap_close($ldapconn); // all done? close connection
return;
}
// binding to ldap server with the userDN and privided credentials
$ldapbind = @ldap_bind($ldapconn, $userdn, $password);
}
// verify user binding
if (!$ldapbind) {
$this->setAuthFailure(100, ldap_error($ldapconn));
ldap_close($ldapconn); // all done? close connection
return;
}
// Authentication was successful, now see if we have a user or that we should create one
if (is_null($user)) {
if ($this->autoCreate === true) {
/*
* Dispatch the newUserLogin event, and hope that after this we can find the user
* this allows users to create their own plugin for handling the user creation
* we will need more methods to pass username, rdn and ldap connection.
*/
$this->pluginManager->dispatchEvent(new PluginEvent('newUserLogin', $this));
// Check ourselves, we do not want fake responses from a plugin
$user = $this->api->getUserByName($username);
}
if (is_null($user)) {
$this->setAuthFailure(self::ERROR_USERNAME_INVALID);
ldap_close($ldapconn); // all done? close connection
return;
}
}
ldap_close($ldapconn); // all done? close connection
// If we made it here, authentication was a success and we do have a valid user
$this->setAuthSuccess($user);
}
}