1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* This module injects itself into Drupal's Authentication stack.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* @todo fix advanced help for ../ldap/authentication settings page
|
10
|
*/
|
11
|
define('LDAP_AUTHENTICATION_PROJECT_TAG', 'ldap');
|
12
|
|
13
|
define('LDAP_AUTHENTICATION_MIXED', 1);
|
14
|
define('LDAP_AUTHENTICATION_EXCLUSIVE', 2);
|
15
|
define('LDAP_AUTHENTICATION_MODE_DEFAULT', 1);
|
16
|
|
17
|
define('LDAP_AUTHENTICATION_EXCL_IF_NO_AUTHZ_DEFAULT', 0);
|
18
|
define('LDAP_AUTHENTICATION_CONFLICT_LOG', 1);
|
19
|
define('LDAP_AUTHENTICATION_CONFLICT_RESOLVE', 2);
|
20
|
define('LDAP_AUTHENTICATION_CONFLICT_RESOLVE_DEFAULT', 2);
|
21
|
|
22
|
define('LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE_NOTIFY', 1);
|
23
|
define('LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_ENABLE', 2);
|
24
|
define('LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_DISABLE', 3);
|
25
|
define('LDAP_AUTHENTICATION_EMAIL_UPDATE_ON_LDAP_CHANGE_DEFAULT', 1);
|
26
|
|
27
|
define('LDAP_AUTHENTICATION_EMAIL_FIELD_REMOVE', 2);
|
28
|
define('LDAP_AUTHENTICATION_EMAIL_FIELD_DISABLE', 3);
|
29
|
define('LDAP_AUTHENTICATION_EMAIL_FIELD_ALLOW', 4);
|
30
|
define('LDAP_AUTHENTICATION_EMAIL_FIELD_DEFAULT', 3);
|
31
|
|
32
|
define('LDAP_AUTHENTICATION_EMAIL_TEMPLATE_NONE', 1);
|
33
|
define('LDAP_AUTHENTICATION_EMAIL_TEMPLATE_ALWAYS', 2);
|
34
|
define('LDAP_AUTHENTICATION_EMAIL_TEMPLATE_IF_EMPTY', 3);
|
35
|
define('LDAP_AUTHENTICATION_EMAIL_TEMPLATE_DEFAULT', 3);
|
36
|
|
37
|
define('LDAP_AUTHENTICATION_DEFAULT_TEMPLATE', '@username@fake-domain.com');
|
38
|
|
39
|
define('LDAP_AUTHENTICATION_DEFAULT_TEMPLATE_REGEX', '.*@fake-domain\\.com');
|
40
|
|
41
|
define('LDAP_AUTHENTICATION_TEMPLATE_USAGE_NEVER_UPDATE_DEFAULT', 0);
|
42
|
define('LDAP_AUTHENTICATION_TEMPLATE_USAGE_RESOLVE_CONFLICT_DEFAULT', 0);
|
43
|
define('LDAP_AUTHENTICATION_TEMPLATE_USAGE_PROMPT_USER_DEFAULT', 0);
|
44
|
|
45
|
define('LDAP_AUTHENTICATION_PASSWORD_FIELD_SHOW', 2);
|
46
|
define('LDAP_AUTHENTICATION_PASSWORD_FIELD_HIDE', 3);
|
47
|
define('LDAP_AUTHENTICATION_PASSWORD_FIELD_ALLOW', 4);
|
48
|
define('LDAP_AUTHENTICATION_PASSWORD_FIELD_DEFAULT', 2);
|
49
|
|
50
|
define('LDAP_AUTHENTICATION_RESULT_FAIL_CONNECT', 1);
|
51
|
define('LDAP_AUTHENTICATION_RESULT_FAIL_BIND', 2);
|
52
|
define('LDAP_AUTHENTICATION_RESULT_FAIL_FIND', 3);
|
53
|
define('LDAP_AUTHENTICATION_RESULT_FAIL_DISALLOWED', 4);
|
54
|
define('LDAP_AUTHENTICATION_RESULT_FAIL_CREDENTIALS', 5);
|
55
|
define('LDAP_AUTHENTICATION_RESULT_SUCCESS', 6);
|
56
|
define('LDAP_AUTHENTICATION_RESULT_FAIL_GENERIC', 7);
|
57
|
define('LDAP_AUTHENTICATION_RESULT_FAIL_SERVER' , 8);
|
58
|
|
59
|
define('LDAP_AUTHENTICATION_ACCT_CREATION_DEFAULT', 4);
|
60
|
define('LDAP_AUTHENTICATION_ACCT_CREATION_USER_SETTINGS_FOR_LDAP', 1);
|
61
|
define('LDAP_AUTHENTICATION_ACCT_CREATION_LDAP_BEHAVIOR', 4);
|
62
|
|
63
|
define('LDAP_AUTHENTICATION_HELP_LINK_TEXT_DEFAULT', 'Logon Help');
|
64
|
|
65
|
define('LDAP_AUTHENTICATION_DISABLED_FOR_BAD_CONF_MSG' , 'The site logon is currently not working due to a configuration error. Please see logs for additional details.');
|
66
|
define('LDAP_AUTHENTICATION_COOKIE_EXPIRE', 0);
|
67
|
|
68
|
define('LDAP_AUTHENTICATION_REDIRECT_ON_LOGIN_DEFAULT', 0);
|
69
|
|
70
|
/**
|
71
|
* Implements hook_menu().
|
72
|
*/
|
73
|
function ldap_authentication_menu() {
|
74
|
$items = array();
|
75
|
|
76
|
$items['user/ldap-profile-update'] = array(
|
77
|
'title' => 'Update Profile',
|
78
|
'page callback' => 'drupal_get_form',
|
79
|
'page arguments' => array('ldap_authentication_profile_update_form'),
|
80
|
'access callback' => TRUE,
|
81
|
'type' => MENU_CALLBACK,
|
82
|
'file' => 'ldap_authentication.pages.inc',
|
83
|
);
|
84
|
|
85
|
$items['admin/config/people/ldap/authentication'] = array(
|
86
|
'title' => 'Authentication',
|
87
|
'description' => 'Configure LDAP Authentication',
|
88
|
'page callback' => 'drupal_get_form',
|
89
|
'page arguments' => array('ldap_authentication_admin_form'),
|
90
|
'access arguments' => array('administer site configuration'),
|
91
|
'type' => MENU_LOCAL_TASK,
|
92
|
'weight' => 3,
|
93
|
'file' => 'ldap_authentication.admin.inc',
|
94
|
);
|
95
|
|
96
|
return $items;
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Implements hook_menu_alter().
|
101
|
* since menu items are cached, only useful to add or alter callbacks
|
102
|
* for ldap authentication driven menu items.
|
103
|
*
|
104
|
*/
|
105
|
function ldap_authentication_menu_alter(&$items) {
|
106
|
ldap_servers_module_load_include('inc', 'ldap_authentication', 'ldap_authentication');
|
107
|
$items['user/password']['access callback'] = 'ldap_authentication_show_reset_pwd';
|
108
|
$auth_conf = ldap_authentication_get_valid_conf();
|
109
|
if (@$auth_conf->ldapUserHelpLinkUrl) {
|
110
|
$items['user/ldaphelp'] = array(
|
111
|
'title' => $auth_conf->ldapUserHelpLinkText,
|
112
|
'page callback' => 'drupal_goto',
|
113
|
'page arguments' => array($auth_conf->ldapUserHelpLinkUrl),
|
114
|
'access callback' => 'ldap_authentication_show_ldap_help_link',
|
115
|
'type' => MENU_LOCAL_TASK,
|
116
|
);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Implements hook_theme().
|
122
|
*/
|
123
|
function ldap_authentication_theme() {
|
124
|
return array(
|
125
|
'ldap_authentication_user_login_block_links' => array(
|
126
|
'variables' => array('ldap_user_help_link' => NULL, 'user_register' => TRUE),
|
127
|
'render element' => 'element',
|
128
|
'file' => 'ldap_authentication.theme.inc'
|
129
|
),
|
130
|
'ldap_authentication_user_pass_message' => array(
|
131
|
'variables' => array('show_reset_pwd' => NULL, 'auth_conf' => TRUE),
|
132
|
'render element' => 'element',
|
133
|
'file' => 'ldap_authentication.theme.inc'
|
134
|
),
|
135
|
'ldap_authentication_user_pass_validate_ldap_authenticated' => array(
|
136
|
'variables' => array('account' => NULL, 'auth_conf' => TRUE),
|
137
|
'render element' => 'element',
|
138
|
'file' => 'ldap_authentication.theme.inc'
|
139
|
),
|
140
|
'ldap_authentication_login_message' => array(
|
141
|
'render element' => 'element',
|
142
|
'variables' => array('message' => NULL),
|
143
|
'file' => 'ldap_authentication.theme.inc'
|
144
|
),
|
145
|
'ldap_authentication_message_not_found' => array(
|
146
|
'render element' => 'element',
|
147
|
'variables' => array('message' => NULL),
|
148
|
'file' => 'ldap_authentication.theme.inc'
|
149
|
),
|
150
|
'ldap_authentication_message_not_authenticated' => array(
|
151
|
'render element' => 'element',
|
152
|
'variables' => array('message' => NULL),
|
153
|
'file' => 'ldap_authentication.theme.inc'
|
154
|
),
|
155
|
);
|
156
|
}
|
157
|
|
158
|
|
159
|
/**
|
160
|
* Implements hook_help().
|
161
|
*/
|
162
|
|
163
|
function ldap_authentication_help($path, $arg) {
|
164
|
|
165
|
$authentication_help = t('LDAP authentication allows authentication against an LDAP server. It
|
166
|
may be used alongside other authentication means such as built in Drupal authentication,
|
167
|
open id, etc. More detailed help is available on drupal.org at !helplink.',
|
168
|
array(
|
169
|
'!helplink' => l(LDAP_SERVERS_DRUPAL_HELP_URL, LDAP_SERVERS_DRUPAL_HELP_URL),
|
170
|
));
|
171
|
|
172
|
switch ($path) {
|
173
|
case 'admin/config/people/ldap/authentication':
|
174
|
$output = '<p>' . $authentication_help . '</p>';
|
175
|
return $output;
|
176
|
|
177
|
case 'admin/help#ldap_authentication':
|
178
|
$output = '<p>' . $authentication_help . '</p>';
|
179
|
return $output;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Helper function that determines whether or not the user's profile
|
185
|
* is valid or needs to be updated on login.
|
186
|
*
|
187
|
* Currently this only checks if mail is valid or not according to the
|
188
|
* authentication settings.
|
189
|
*
|
190
|
* @return boolean
|
191
|
* TRUE if the user's profile is valid, otherwise FALSE.
|
192
|
*
|
193
|
*/
|
194
|
function _ldap_authentication_verify_user_profile() {
|
195
|
global $user;
|
196
|
$result = TRUE;
|
197
|
// We only want non-anonymous and non-1 users.
|
198
|
if ($user->uid > 1) {
|
199
|
// we store the value in the session for speed.
|
200
|
if (isset($_SESSION['ldap_authentication_template']) &&
|
201
|
isset($_SESSION['ldap_authentication_template']['verify_user_profile'])) {
|
202
|
return $_SESSION['ldap_authentication_template']['verify_user_profile'];
|
203
|
}
|
204
|
if (ldap_authentication_ldap_authenticated($user)) {
|
205
|
$auth_conf = ldap_authentication_get_valid_conf();
|
206
|
$regex = '`' . $auth_conf->templateUsagePromptRegex . '`i';
|
207
|
if (preg_match($regex, $user->mail)) {
|
208
|
$result = FALSE;
|
209
|
}
|
210
|
$_SESSION['ldap_authentication_template'] = array(
|
211
|
'verify_user_profile' => $result,
|
212
|
);
|
213
|
}
|
214
|
}
|
215
|
return TRUE;
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
* Implements hook_init().
|
220
|
*/
|
221
|
function ldap_authentication_init() {
|
222
|
$auth_conf = ldap_authentication_get_valid_conf();
|
223
|
if ($auth_conf->templateUsagePromptUser) {
|
224
|
ldap_authentication_check_for_email_template();
|
225
|
}
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* Form submit callback to check for an email template and redirect if needed.
|
230
|
*/
|
231
|
function ldap_authentication_check_for_email_template() {
|
232
|
if (!_ldap_authentication_verify_user_profile()) {
|
233
|
$url = 'user/ldap-profile-update';
|
234
|
$cp = current_path();
|
235
|
// avoid redirects on these two pages.
|
236
|
if ($cp != $url && $cp != 'user/logout') {
|
237
|
if (isset($_GET['destination'])) {
|
238
|
unset($_GET['destination']);
|
239
|
}
|
240
|
drupal_goto($url, array(
|
241
|
'query' => array(
|
242
|
'next' => current_path(),
|
243
|
),
|
244
|
));
|
245
|
}
|
246
|
}
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* Implements hook_info().
|
251
|
*/
|
252
|
function ldap_authentication_info($field = 0) {
|
253
|
$info['name']= 'ldap_authentication';
|
254
|
$info['protocol'] = 'LDAP';
|
255
|
|
256
|
if ($field) {
|
257
|
return $info[$field];
|
258
|
}
|
259
|
|
260
|
return $info;
|
261
|
}
|
262
|
|
263
|
|
264
|
/**
|
265
|
*
|
266
|
* @param object $user
|
267
|
* @return boolean
|
268
|
* true if user is recorded as ldap authenticated and identified (ldap_authentified)
|
269
|
*/
|
270
|
|
271
|
function ldap_authentication_ldap_authenticated($user) {
|
272
|
|
273
|
if (is_numeric($user)) {
|
274
|
$user = @user_load((int)$user);
|
275
|
}
|
276
|
if (!is_object($user) || $user->uid == 0) {
|
277
|
return FALSE;
|
278
|
}
|
279
|
|
280
|
$authname = ldap_user_get_authname($user);
|
281
|
return !empty($authname);
|
282
|
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* A user access callback for using the single sign-on URL, denying access to
|
287
|
* authenticated users, and granting access to anonymous users and menu
|
288
|
* administrators viewing the menu item.
|
289
|
*
|
290
|
*/
|
291
|
function _ldap_authentication_user_access() {
|
292
|
return (boolean)(!$GLOBALS['user']->uid || !empty($GLOBALS['menu_admin']));
|
293
|
}
|
294
|
|
295
|
|
296
|
|
297
|
/**
|
298
|
* get LdapAuthenticationConf object
|
299
|
*
|
300
|
* @return object LdapAuthenticationConf object if configured, otherwise FALSE
|
301
|
*
|
302
|
*/
|
303
|
|
304
|
function ldap_authentication_get_valid_conf($reset = FALSE) {
|
305
|
|
306
|
static $auth_conf;
|
307
|
if (!$reset && is_object($auth_conf)) {
|
308
|
return $auth_conf;
|
309
|
}
|
310
|
ldap_servers_module_load_include('php', 'ldap_authentication', 'LdapAuthenticationConf.class');
|
311
|
|
312
|
$auth_conf = new LdapAuthenticationConf();
|
313
|
return ($auth_conf->inDatabase) ? $auth_conf : FALSE;
|
314
|
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* Implements hook_ldap_ldap_server_in_use().
|
319
|
*/
|
320
|
function ldap_authentication_ldap_server_in_use($sid, $server_name) {
|
321
|
|
322
|
$use_warnings = array();
|
323
|
$auth_conf = ldap_authentication_get_valid_conf();
|
324
|
if ($auth_conf && in_array($sid, array_keys($auth_conf->sids))) {
|
325
|
$use_warnings[] = t('This server (%server_name) may not be deleted or
|
326
|
disabled because it is being used for ldap authentication.',
|
327
|
array('%server_name' => $server_name));
|
328
|
}
|
329
|
return $use_warnings;
|
330
|
}
|
331
|
|
332
|
function ldap_authentication_show_reset_pwd($user = NULL) {
|
333
|
|
334
|
if (!$user) {
|
335
|
global $user;
|
336
|
}
|
337
|
$auth_conf = ldap_authentication_get_valid_conf();
|
338
|
if (current_path() == 'user/password' || $user->uid == 1 || !$auth_conf) {
|
339
|
return TRUE;
|
340
|
// always show at user/passwordurl. otherwise user 1 will not be able to reset password.
|
341
|
}
|
342
|
|
343
|
if ($user->uid == 0) {
|
344
|
// hide reset password for anonymous users if ldap only authentication and password updates are disabled, otherwise show
|
345
|
if ($auth_conf->authenticationMode != LDAP_AUTHENTICATION_EXCLUSIVE) {
|
346
|
if ($auth_conf->passwordOption == LDAP_AUTHENTICATION_PASSWORD_FIELD_ALLOW) {
|
347
|
return TRUE;
|
348
|
}
|
349
|
return FALSE;
|
350
|
}
|
351
|
return TRUE;
|
352
|
}
|
353
|
else {
|
354
|
// authenticated user. hide if ldap authenticated and updating password is
|
355
|
// not allowed, otherwise show.
|
356
|
if (ldap_authentication_ldap_authenticated($user)) {
|
357
|
if ($auth_conf->passwordOption == LDAP_AUTHENTICATION_PASSWORD_FIELD_ALLOW) {
|
358
|
return TRUE;
|
359
|
}
|
360
|
return FALSE;
|
361
|
}
|
362
|
return TRUE;
|
363
|
}
|
364
|
|
365
|
}
|
366
|
|
367
|
|
368
|
/**
|
369
|
* Implements hook_form_FORM_ID_alter().
|
370
|
*/
|
371
|
|
372
|
function ldap_authentication_form_user_pass_alter(&$form, $form_state) {
|
373
|
// the following could be in a theme preproces function
|
374
|
$auth_conf = ldap_authentication_get_valid_conf();
|
375
|
$form['ldap_warning'] = array(
|
376
|
'#type' => 'item',
|
377
|
'#markup' => theme('ldap_authentication_user_pass_message', array('auth_conf' => $auth_conf)),
|
378
|
'#weight' => 10,
|
379
|
);
|
380
|
|
381
|
// need to insert before user_pass_validate
|
382
|
array_unshift($form['#validate'], 'ldap_authentication_user_pass_validate');
|
383
|
}
|
384
|
|
385
|
|
386
|
/**
|
387
|
* A validate handler on the login form. Check supplied username/password
|
388
|
* against local users table. If successful, $form_state['uid']
|
389
|
* is set to the matching user ID.
|
390
|
*/
|
391
|
function ldap_authentication_core_override_user_login_authenticate_validate($form, &$form_state) {
|
392
|
// No additional validation of user credentials is needed when
|
393
|
// $form_state['uid'] is set.
|
394
|
if (!empty($form_state['uid'])) {
|
395
|
return;
|
396
|
}
|
397
|
user_login_authenticate_validate($form, $form_state);
|
398
|
}
|
399
|
|
400
|
|
401
|
function ldap_authentication_user_pass_validate(&$form_state) {
|
402
|
$name_or_mail = trim($form_state['name']['#value']);
|
403
|
if ($account = user_load_by_mail($name_or_mail)) {
|
404
|
|
405
|
}
|
406
|
else {
|
407
|
$account = user_load_by_name($name_or_mail);
|
408
|
}
|
409
|
|
410
|
if (ldap_authentication_ldap_authenticated($account)) {
|
411
|
$vars = array(
|
412
|
'account' => $account,
|
413
|
'auth_conf' => ldap_authentication_get_valid_conf(),
|
414
|
);
|
415
|
$error = TRUE;
|
416
|
if (is_object($vars['auth_conf'])) {
|
417
|
if ($vars['auth_conf']->passwordOption == LDAP_AUTHENTICATION_PASSWORD_FIELD_ALLOW) {
|
418
|
$error = FALSE;
|
419
|
}
|
420
|
}
|
421
|
if (!empty($error)) {
|
422
|
form_set_error('name', theme('ldap_authentication_user_pass_validate_ldap_authenticated', $vars));
|
423
|
}
|
424
|
}
|
425
|
}
|
426
|
|
427
|
|
428
|
/**
|
429
|
* Implements hook_form_FORM_ID_alter(). for user_profile_form
|
430
|
*/
|
431
|
function ldap_authentication_form_user_profile_form_alter(&$form, $form_state) {
|
432
|
ldap_servers_module_load_include('inc', 'ldap_authentication', 'ldap_authentication');
|
433
|
_ldap_authentication_form_user_profile_form_alter($form, $form_state, 'user_login');
|
434
|
|
435
|
}
|
436
|
|
437
|
|
438
|
/**
|
439
|
* Implements hook_form_FORM_ID_alter(). for user_login
|
440
|
*/
|
441
|
function ldap_authentication_form_user_login_alter(&$form, &$form_state) {
|
442
|
ldap_servers_module_load_include('inc', 'ldap_authentication', 'ldap_authentication');
|
443
|
_ldap_authentication_login_form_alter($form, $form_state, 'user_login');
|
444
|
|
445
|
}
|
446
|
|
447
|
/**
|
448
|
* Implements hook_form_FORM_ID_alter(). for user_login_block
|
449
|
*/
|
450
|
function ldap_authentication_form_user_login_block_alter(&$form, &$form_state) {
|
451
|
ldap_servers_module_load_include('inc', 'ldap_authentication', 'ldap_authentication');
|
452
|
_ldap_authentication_login_form_alter($form, $form_state, 'user_login_block');
|
453
|
|
454
|
}
|
455
|
|
456
|
/**
|
457
|
* validate function for user logon forms.
|
458
|
*/
|
459
|
function ldap_authentication_user_login_authenticate_validate($form, &$form_state, $return_user = FALSE) {
|
460
|
ldap_servers_module_load_include('inc', 'ldap_authentication', 'ldap_authentication');
|
461
|
return _ldap_authentication_user_login_authenticate_validate($form_state, $return_user);
|
462
|
}
|
463
|
|
464
|
|
465
|
/**
|
466
|
* Implements hook_user_presave().
|
467
|
* A user account is about to be created or updated.
|
468
|
*/
|
469
|
|
470
|
function ldap_authentication_user_presave(&$edit, $account, $category = NULL) {
|
471
|
|
472
|
}
|
473
|
|
474
|
|
475
|
/**
|
476
|
* Implements hook_user_insert().
|
477
|
*
|
478
|
* A user account was created.
|
479
|
* The module should save its custom additions to the user object into the database.
|
480
|
*/
|
481
|
|
482
|
function ldap_authentication_user_insert(&$edit, $account, $category) {
|
483
|
|
484
|
}
|
485
|
|
486
|
|
487
|
/**
|
488
|
* Implements hook_user_update().
|
489
|
*
|
490
|
* A user account was updated.
|
491
|
* Modules may use this hook to update their user data in a custom storage after a user account has been updated.
|
492
|
*/
|
493
|
|
494
|
|
495
|
function ldap_authentication_user_update($edit, $user, $category) {
|
496
|
|
497
|
}
|
498
|
|
499
|
function ldap_authentication_show_ldap_help_link($user = NULL) {
|
500
|
global $user;
|
501
|
|
502
|
if (!$auth_conf = ldap_authentication_get_valid_conf()) {
|
503
|
return FALSE;
|
504
|
}
|
505
|
elseif ($auth_conf->authenticationMode == LDAP_AUTHENTICATION_MIXED) {
|
506
|
return (ldap_authentication_ldap_authenticated($user));
|
507
|
}
|
508
|
elseif ($auth_conf->authenticationMode == LDAP_AUTHENTICATION_EXCLUSIVE) {
|
509
|
if ($user->uid == 0 || ldap_authentication_ldap_authenticated($user)) {
|
510
|
return TRUE;
|
511
|
}
|
512
|
else {
|
513
|
return FALSE;
|
514
|
}
|
515
|
}
|
516
|
}
|