2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Authentication Plugin: LDAP Authentication
19 * Authentication using LDAP (Lightweight Directory Access Protocol).
22 * @author Martin Dougiamas
23 * @author IƱaki Arenaza
24 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
27 defined('MOODLE_INTERNAL') || die();
29 // See http://support.microsoft.com/kb/305144 to interprete these values.
30 if (!defined('AUTH_AD_ACCOUNTDISABLE')) {
31 define('AUTH_AD_ACCOUNTDISABLE', 0x0002);
33 if (!defined('AUTH_AD_NORMAL_ACCOUNT')) {
34 define('AUTH_AD_NORMAL_ACCOUNT', 0x0200);
36 if (!defined('AUTH_NTLMTIMEOUT')) { // timewindow for the NTLM SSO process, in secs...
37 define('AUTH_NTLMTIMEOUT', 10);
40 // UF_DONT_EXPIRE_PASSWD value taken from MSDN directly
41 if (!defined('UF_DONT_EXPIRE_PASSWD')) {
42 define ('UF_DONT_EXPIRE_PASSWD', 0x00010000);
45 // The Posix uid and gid of the 'nobody' account and 'nogroup' group.
46 if (!defined('AUTH_UID_NOBODY')) {
47 define('AUTH_UID_NOBODY', -2);
49 if (!defined('AUTH_GID_NOGROUP')) {
50 define('AUTH_GID_NOGROUP', -2);
53 // Regular expressions for a valid NTLM username and domain name.
54 if (!defined('AUTH_NTLM_VALID_USERNAME')) {
55 define('AUTH_NTLM_VALID_USERNAME', '[^/\\\\\\\\\[\]:;|=,+*?<>@"]+');
57 if (!defined('AUTH_NTLM_VALID_DOMAINNAME')) {
58 define('AUTH_NTLM_VALID_DOMAINNAME', '[^\\\\\\\\\/:*?"<>|]+');
60 // Default format for remote users if using NTLM SSO
61 if (!defined('AUTH_NTLM_DEFAULT_FORMAT')) {
62 define('AUTH_NTLM_DEFAULT_FORMAT', '%domain%\\%username%');
64 if (!defined('AUTH_NTLM_FASTPATH_ATTEMPT')) {
65 define('AUTH_NTLM_FASTPATH_ATTEMPT', 0);
67 if (!defined('AUTH_NTLM_FASTPATH_YESFORM')) {
68 define('AUTH_NTLM_FASTPATH_YESFORM', 1);
70 if (!defined('AUTH_NTLM_FASTPATH_YESATTEMPT')) {
71 define('AUTH_NTLM_FASTPATH_YESATTEMPT', 2);
74 // Allows us to retrieve a diagnostic message in case of LDAP operation error
75 if (!defined('LDAP_OPT_DIAGNOSTIC_MESSAGE')) {
76 define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032);
79 require_once($CFG->libdir.'/authlib.php');
80 require_once($CFG->libdir.'/ldaplib.php');
81 require_once($CFG->dirroot.'/user/lib.php');
84 * LDAP authentication plugin.
86 class auth_plugin_ldap extends auth_plugin_base {
89 * Init plugin config from database settings depending on the plugin auth type.
91 function init_plugin($authtype) {
92 $this->pluginconfig = 'auth_'.$authtype;
93 $this->config = get_config($this->pluginconfig);
94 if (empty($this->config->ldapencoding)) {
95 $this->config->ldapencoding = 'utf-8';
97 if (empty($this->config->user_type)) {
98 $this->config->user_type = 'default';
101 $ldap_usertypes = ldap_supported_usertypes();
102 $this->config->user_type_name = $ldap_usertypes[$this->config->user_type];
103 unset($ldap_usertypes);
105 $default = ldap_getdefaults();
107 // Use defaults if values not given
108 foreach ($default as $key => $value) {
109 // watch out - 0, false are correct values too
110 if (!isset($this->config->{$key}) or $this->config->{$key} == '') {
111 $this->config->{$key} = $value[$this->config->user_type];
115 // Hack prefix to objectclass
116 $this->config->objectclass = ldap_normalise_objectclass($this->config->objectclass);
120 * Constructor with initialisation.
122 public function __construct() {
123 $this->authtype = 'ldap';
124 $this->roleauth = 'auth_ldap';
125 $this->errorlogtag = '[AUTH LDAP] ';
126 $this->init_plugin($this->authtype);
130 * Old syntax of class constructor. Deprecated in PHP7.
132 * @deprecated since Moodle 3.1
134 public function auth_plugin_ldap() {
135 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
140 * Returns true if the username and password work and false if they are
141 * wrong or don't exist.
143 * @param string $username The username (without system magic quotes)
144 * @param string $password The password (without system magic quotes)
146 * @return bool Authentication success or failure.
148 function user_login($username, $password) {
149 if (! function_exists('ldap_bind')) {
150 print_error('auth_ldapnotinstalled', 'auth_ldap');
154 if (!$username or !$password) { // Don't allow blank usernames or passwords
158 $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
159 $extpassword = core_text::convert($password, 'utf-8', $this->config->ldapencoding);
161 // Before we connect to LDAP, check if this is an AD SSO login
162 // if we succeed in this block, we'll return success early.
165 if (!empty($this->config->ntlmsso_enabled) && $key === $password) {
166 $cf = get_cache_flags($this->pluginconfig.'/ntlmsess');
167 // We only get the cache flag if we retrieve it before
168 // it expires (AUTH_NTLMTIMEOUT seconds).
169 if (!isset($cf[$key]) || $cf[$key] === '') {
173 $sessusername = $cf[$key];
174 if ($username === $sessusername) {
175 unset($sessusername);
178 // Check that the user is inside one of the configured LDAP contexts
180 $ldapconnection = $this->ldap_connect();
181 // if the user is not inside the configured contexts,
182 // ldap_find_userdn returns false.
183 if ($this->ldap_find_userdn($ldapconnection, $extusername)) {
188 // Shortcut here - SSO confirmed
191 } // End SSO processing
194 $ldapconnection = $this->ldap_connect();
195 $ldap_user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
197 // If ldap_user_dn is empty, user does not exist
198 if (!$ldap_user_dn) {
203 // Try to bind with current username and password
204 $ldap_login = @ldap_bind($ldapconnection, $ldap_user_dn, $extpassword);
206 // If login fails and we are using MS Active Directory, retrieve the diagnostic
207 // message to see if this is due to an expired password, or that the user is forced to
208 // change the password on first login. If it is, only proceed if we can change
209 // password from Moodle (otherwise we'll get stuck later in the login process).
210 if (!$ldap_login && ($this->config->user_type == 'ad')
211 && $this->can_change_password()
212 && (!empty($this->config->expiration) and ($this->config->expiration == 1))) {
214 // We need to get the diagnostic message right after the call to ldap_bind(),
215 // before any other LDAP operation.
216 ldap_get_option($ldapconnection, LDAP_OPT_DIAGNOSTIC_MESSAGE, $diagmsg);
218 if ($this->ldap_ad_pwdexpired_from_diagmsg($diagmsg)) {
219 // If login failed because user must change the password now or the
220 // password has expired, let the user in. We'll catch this later in the
221 // login process when we explicitly check for expired passwords.
230 * Reads user information from ldap and returns it in array()
232 * Function should return all information available. If you are saving
233 * this information to moodle user-table you should honor syncronization flags
235 * @param string $username username
237 * @return mixed array with no magic quotes or false on error
239 function get_userinfo($username) {
240 $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
242 $ldapconnection = $this->ldap_connect();
243 if(!($user_dn = $this->ldap_find_userdn($ldapconnection, $extusername))) {
248 $search_attribs = array();
249 $attrmap = $this->ldap_attributes();
250 foreach ($attrmap as $key => $values) {
251 if (!is_array($values)) {
252 $values = array($values);
254 foreach ($values as $value) {
255 if (!in_array($value, $search_attribs)) {
256 array_push($search_attribs, $value);
261 if (!$user_info_result = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs)) {
263 return false; // error!
266 $user_entry = ldap_get_entries_moodle($ldapconnection, $user_info_result);
267 if (empty($user_entry)) {
269 return false; // entry not found
273 foreach ($attrmap as $key => $values) {
274 if (!is_array($values)) {
275 $values = array($values);
278 foreach ($values as $value) {
279 $entry = $user_entry[0];
280 if (($value == 'dn') || ($value == 'distinguishedname')) {
281 $result[$key] = $user_dn;
284 if (!array_key_exists($value, $entry)) {
285 continue; // wrong data mapping!
287 if (is_array($entry[$value])) {
288 $newval = core_text::convert($entry[$value][0], $this->config->ldapencoding, 'utf-8');
290 $newval = core_text::convert($entry[$value], $this->config->ldapencoding, 'utf-8');
292 if (!empty($newval)) { // favour ldap entries that are set
296 if (!is_null($ldapval)) {
297 $result[$key] = $ldapval;
306 * Reads user information from ldap and returns it in an object
308 * @param string $username username (with system magic quotes)
309 * @return mixed object or false on error
311 function get_userinfo_asobj($username) {
312 $user_array = $this->get_userinfo($username);
313 if ($user_array == false) {
314 return false; //error or not found
316 $user_array = truncate_userinfo($user_array);
317 $user = new stdClass();
318 foreach ($user_array as $key=>$value) {
319 $user->{$key} = $value;
325 * Returns all usernames from LDAP
327 * get_userlist returns all usernames from LDAP
331 function get_userlist() {
332 return $this->ldap_get_userlist("({$this->config->user_attribute}=*)");
336 * Checks if user exists on LDAP
338 * @param string $username
340 function user_exists($username) {
341 $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
343 // Returns true if given username exists on ldap
344 $users = $this->ldap_get_userlist('('.$this->config->user_attribute.'='.ldap_filter_addslashes($extusername).')');
345 return count($users);
349 * Creates a new user on LDAP.
350 * By using information in userobject
351 * Use user_exists to prevent duplicate usernames
353 * @param mixed $userobject Moodle userobject
354 * @param mixed $plainpass Plaintext password
356 function user_create($userobject, $plainpass) {
357 $extusername = core_text::convert($userobject->username, 'utf-8', $this->config->ldapencoding);
358 $extpassword = core_text::convert($plainpass, 'utf-8', $this->config->ldapencoding);
360 switch ($this->config->passtype) {
362 $extpassword = '{MD5}' . base64_encode(pack('H*', md5($extpassword)));
365 $extpassword = '{SHA}' . base64_encode(pack('H*', sha1($extpassword)));
372 $ldapconnection = $this->ldap_connect();
373 $attrmap = $this->ldap_attributes();
377 foreach ($attrmap as $key => $values) {
378 if (!is_array($values)) {
379 $values = array($values);
381 foreach ($values as $value) {
382 if (!empty($userobject->$key) ) {
383 $newuser[$value] = core_text::convert($userobject->$key, 'utf-8', $this->config->ldapencoding);
388 //Following sets all mandatory and other forced attribute values
389 //User should be creted as login disabled untill email confirmation is processed
390 //Feel free to add your user type and send patches to paca@sci.fi to add them
391 //Moodle distribution
393 switch ($this->config->user_type) {
395 $newuser['objectClass'] = array('inetOrgPerson', 'organizationalPerson', 'person', 'top');
396 $newuser['uniqueId'] = $extusername;
397 $newuser['logindisabled'] = 'TRUE';
398 $newuser['userpassword'] = $extpassword;
399 $uadd = ldap_add($ldapconnection, $this->config->user_attribute.'='.ldap_addslashes($extusername).','.$this->config->create_context, $newuser);
403 // posixAccount object class forces us to specify a uidNumber
404 // and a gidNumber. That is quite complicated to generate from
405 // Moodle without colliding with existing numbers and without
406 // race conditions. As this user is supposed to be only used
407 // with Moodle (otherwise the user would exist beforehand) and
408 // doesn't need to login into a operating system, we assign the
409 // user the uid of user 'nobody' and gid of group 'nogroup'. In
410 // addition to that, we need to specify a home directory. We
411 // use the root directory ('/') as the home directory, as this
412 // is the only one can always be sure exists. Finally, even if
413 // it's not mandatory, we specify '/bin/false' as the login
414 // shell, to prevent the user from login in at the operating
415 // system level (Moodle ignores this).
417 $newuser['objectClass'] = array('posixAccount', 'inetOrgPerson', 'organizationalPerson', 'person', 'top');
418 $newuser['cn'] = $extusername;
419 $newuser['uid'] = $extusername;
420 $newuser['uidNumber'] = AUTH_UID_NOBODY;
421 $newuser['gidNumber'] = AUTH_GID_NOGROUP;
422 $newuser['homeDirectory'] = '/';
423 $newuser['loginShell'] = '/bin/false';
426 // We have to create the account locked, but posixAccount has
427 // no attribute to achive this reliably. So we are going to
428 // modify the password in a reversable way that we can later
429 // revert in user_activate().
431 // Beware that this can be defeated by the user if we are not
432 // using MD5 or SHA-1 passwords. After all, the source code of
433 // Moodle is available, and the user can see the kind of
434 // modification we are doing and 'undo' it by hand (but only
435 // if we are using plain text passwords).
437 // Also bear in mind that you need to use a binding user that
438 // can create accounts and has read/write privileges on the
439 // 'userPassword' attribute for this to work.
441 $newuser['userPassword'] = '*'.$extpassword;
442 $uadd = ldap_add($ldapconnection, $this->config->user_attribute.'='.ldap_addslashes($extusername).','.$this->config->create_context, $newuser);
445 // User account creation is a two step process with AD. First you
446 // create the user object, then you set the password. If you try
447 // to set the password while creating the user, the operation
450 // Passwords in Active Directory must be encoded as Unicode
451 // strings (UCS-2 Little Endian format) and surrounded with
452 // double quotes. See http://support.microsoft.com/?kbid=269190
453 if (!function_exists('mb_convert_encoding')) {
454 print_error('auth_ldap_no_mbstring', 'auth_ldap');
457 // Check for invalid sAMAccountName characters.
458 if (preg_match('#[/\\[\]:;|=,+*?<>@"]#', $extusername)) {
459 print_error ('auth_ldap_ad_invalidchars', 'auth_ldap');
462 // First create the user account, and mark it as disabled.
463 $newuser['objectClass'] = array('top', 'person', 'user', 'organizationalPerson');
464 $newuser['sAMAccountName'] = $extusername;
465 $newuser['userAccountControl'] = AUTH_AD_NORMAL_ACCOUNT |
466 AUTH_AD_ACCOUNTDISABLE;
467 $userdn = 'cn='.ldap_addslashes($extusername).','.$this->config->create_context;
468 if (!ldap_add($ldapconnection, $userdn, $newuser)) {
469 print_error('auth_ldap_ad_create_req', 'auth_ldap');
472 // Now set the password
474 $newuser['unicodePwd'] = mb_convert_encoding('"' . $extpassword . '"',
476 if(!ldap_modify($ldapconnection, $userdn, $newuser)) {
477 // Something went wrong: delete the user account and error out
478 ldap_delete ($ldapconnection, $userdn);
479 print_error('auth_ldap_ad_create_req', 'auth_ldap');
484 print_error('auth_ldap_unsupportedusertype', 'auth_ldap', '', $this->config->user_type_name);
491 * Returns true if plugin allows resetting of password from moodle.
495 function can_reset_password() {
496 return !empty($this->config->stdchangepassword);
500 * Returns true if plugin can be manually set.
504 function can_be_manually_set() {
509 * Returns true if plugin allows signup and user creation.
513 function can_signup() {
514 return (!empty($this->config->auth_user_create) and !empty($this->config->create_context));
518 * Sign up a new user ready for confirmation.
519 * Password is passed in plaintext.
521 * @param object $user new user object
522 * @param boolean $notify print notice with link and terminate
523 * @return boolean success
525 function user_signup($user, $notify=true) {
526 global $CFG, $DB, $PAGE, $OUTPUT;
528 require_once($CFG->dirroot.'/user/profile/lib.php');
529 require_once($CFG->dirroot.'/user/lib.php');
531 if ($this->user_exists($user->username)) {
532 print_error('auth_ldap_user_exists', 'auth_ldap');
535 $plainslashedpassword = $user->password;
536 unset($user->password);
538 if (! $this->user_create($user, $plainslashedpassword)) {
539 print_error('auth_ldap_create_error', 'auth_ldap');
542 $user->id = user_create_user($user, false, false);
544 user_add_password_history($user->id, $plainslashedpassword);
546 // Save any custom profile field information
547 profile_save_data($user);
549 $this->update_user_record($user->username);
550 // This will also update the stored hash to the latest algorithm
551 // if the existing hash is using an out-of-date algorithm (or the
552 // legacy md5 algorithm).
553 update_internal_user_password($user, $plainslashedpassword);
555 $user = $DB->get_record('user', array('id'=>$user->id));
557 \core\event\user_created::create_from_userid($user->id)->trigger();
559 if (! send_confirmation_email($user)) {
560 print_error('noemail', 'auth_ldap');
564 $emailconfirm = get_string('emailconfirm');
565 $PAGE->set_url('/auth/ldap/auth.php');
566 $PAGE->navbar->add($emailconfirm);
567 $PAGE->set_title($emailconfirm);
568 $PAGE->set_heading($emailconfirm);
569 echo $OUTPUT->header();
570 notice(get_string('emailconfirmsent', '', $user->email), "{$CFG->wwwroot}/index.php");
577 * Returns true if plugin allows confirming of new users.
581 function can_confirm() {
582 return $this->can_signup();
586 * Confirm the new user as registered.
588 * @param string $username
589 * @param string $confirmsecret
591 function user_confirm($username, $confirmsecret) {
594 $user = get_complete_user_data('username', $username);
597 if ($user->auth != $this->authtype) {
598 return AUTH_CONFIRM_ERROR;
600 } else if ($user->secret == $confirmsecret && $user->confirmed) {
601 return AUTH_CONFIRM_ALREADY;
603 } else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
604 if (!$this->user_activate($username)) {
605 return AUTH_CONFIRM_FAIL;
607 $user->confirmed = 1;
608 user_update_user($user, false);
609 return AUTH_CONFIRM_OK;
612 return AUTH_CONFIRM_ERROR;
617 * Return number of days to user password expires
619 * If userpassword does not expire it should return 0. If password is already expired
620 * it should return negative value.
622 * @param mixed $username username
625 function password_expire($username) {
628 $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
630 $ldapconnection = $this->ldap_connect();
631 $user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
632 $search_attribs = array($this->config->expireattr);
633 $sr = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs);
635 $info = ldap_get_entries_moodle($ldapconnection, $sr);
636 if (!empty ($info)) {
638 if (isset($info[$this->config->expireattr][0])) {
639 $expiretime = $this->ldap_expirationtime2unix($info[$this->config->expireattr][0], $ldapconnection, $user_dn);
640 if ($expiretime != 0) {
642 if ($expiretime > $now) {
643 $result = ceil(($expiretime - $now) / DAYSECS);
645 $result = floor(($expiretime - $now) / DAYSECS);
651 error_log($this->errorlogtag.get_string('didtfindexpiretime', 'auth_ldap'));
658 * Syncronizes user fron external LDAP server to moodle user table
660 * Sync is now using username attribute.
662 * Syncing users removes or suspends users that dont exists anymore in external LDAP.
663 * Creates new users and updates coursecreator status of users.
665 * @param bool $do_updates will do pull in data updates from LDAP if relevant
667 function sync_users($do_updates=true) {
670 print_string('connectingldap', 'auth_ldap');
671 $ldapconnection = $this->ldap_connect();
673 $dbman = $DB->get_manager();
675 /// Define table user to be created
676 $table = new xmldb_table('tmp_extuser');
677 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
678 $table->add_field('username', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
679 $table->add_field('mnethostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
680 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
681 $table->add_index('username', XMLDB_INDEX_UNIQUE, array('mnethostid', 'username'));
683 print_string('creatingtemptable', 'auth_ldap', 'tmp_extuser');
684 $dbman->create_temp_table($table);
687 //// get user's list from ldap to sql in a scalable fashion
689 // prepare some data we'll need
690 $filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')';
692 $contexts = explode(';', $this->config->contexts);
694 if (!empty($this->config->create_context)) {
695 array_push($contexts, $this->config->create_context);
698 $ldap_pagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
700 foreach ($contexts as $context) {
701 $context = trim($context);
702 if (empty($context)) {
707 if ($ldap_pagedresults) {
708 ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
710 if ($this->config->search_sub) {
711 // Use ldap_search to find first user from subtree.
712 $ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
714 // Search only in this context.
715 $ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
720 if ($ldap_pagedresults) {
721 ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
723 if ($entry = @ldap_first_entry($ldapconnection, $ldap_result)) {
725 $value = ldap_get_values_len($ldapconnection, $entry, $this->config->user_attribute);
726 $value = core_text::convert($value[0], $this->config->ldapencoding, 'utf-8');
727 $value = trim($value);
728 $this->ldap_bulk_insert($value);
729 } while ($entry = ldap_next_entry($ldapconnection, $entry));
731 unset($ldap_result); // Free mem.
732 } while ($ldap_pagedresults && $ldap_cookie !== null && $ldap_cookie != '');
735 // If LDAP paged results were used, the current connection must be completely
736 // closed and a new one created, to work without paged results from here on.
737 if ($ldap_pagedresults) {
738 $this->ldap_close(true);
739 $ldapconnection = $this->ldap_connect();
742 /// preserve our user database
743 /// if the temp table is empty, it probably means that something went wrong, exit
744 /// so as to avoid mass deletion of users; which is hard to undo
745 $count = $DB->count_records_sql('SELECT COUNT(username) AS count, 1 FROM {tmp_extuser}');
747 print_string('didntgetusersfromldap', 'auth_ldap');
750 print_string('gotcountrecordsfromldap', 'auth_ldap', $count);
755 // Find users in DB that aren't in ldap -- to be removed!
756 // this is still not as scalable (but how often do we mass delete?)
758 if ($this->config->removeuser == AUTH_REMOVEUSER_FULLDELETE) {
761 LEFT JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
764 AND e.username IS NULL";
765 $remove_users = $DB->get_records_sql($sql, array('auth'=>$this->authtype));
767 if (!empty($remove_users)) {
768 print_string('userentriestoremove', 'auth_ldap', count($remove_users));
769 foreach ($remove_users as $user) {
770 if (delete_user($user)) {
771 echo "\t"; print_string('auth_dbdeleteuser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
773 echo "\t"; print_string('auth_dbdeleteusererror', 'auth_db', $user->username); echo "\n";
777 print_string('nouserentriestoremove', 'auth_ldap');
779 unset($remove_users); // Free mem!
781 } else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
784 LEFT JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
788 AND e.username IS NULL";
789 $remove_users = $DB->get_records_sql($sql, array('auth'=>$this->authtype));
791 if (!empty($remove_users)) {
792 print_string('userentriestoremove', 'auth_ldap', count($remove_users));
794 foreach ($remove_users as $user) {
795 $updateuser = new stdClass();
796 $updateuser->id = $user->id;
797 $updateuser->suspended = 1;
798 user_update_user($updateuser, false);
799 echo "\t"; print_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
800 \core\session\manager::kill_user_sessions($user->id);
803 print_string('nouserentriestoremove', 'auth_ldap');
805 unset($remove_users); // Free mem!
808 /// Revive suspended users
809 if (!empty($this->config->removeuser) and $this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
810 $sql = "SELECT u.id, u.username
812 JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
813 WHERE (u.auth = 'nologin' OR (u.auth = ? AND u.suspended = 1)) AND u.deleted = 0";
814 // Note: 'nologin' is there for backwards compatibility.
815 $revive_users = $DB->get_records_sql($sql, array($this->authtype));
817 if (!empty($revive_users)) {
818 print_string('userentriestorevive', 'auth_ldap', count($revive_users));
820 foreach ($revive_users as $user) {
821 $updateuser = new stdClass();
822 $updateuser->id = $user->id;
823 $updateuser->auth = $this->authtype;
824 $updateuser->suspended = 0;
825 user_update_user($updateuser, false);
826 echo "\t"; print_string('auth_dbreviveduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
829 print_string('nouserentriestorevive', 'auth_ldap');
832 unset($revive_users);
836 /// User Updates - time-consuming (optional)
838 // Narrow down what fields we need to update
839 $all_keys = array_keys(get_object_vars($this->config));
840 $updatekeys = array();
841 foreach ($all_keys as $key) {
842 if (preg_match('/^field_updatelocal_(.+)$/', $key, $match)) {
843 // If we have a field to update it from
844 // and it must be updated 'onlogin' we
846 if (!empty($this->config->{'field_map_'.$match[1]})
847 and $this->config->{$match[0]} === 'onlogin') {
848 array_push($updatekeys, $match[1]); // the actual key name
852 if ($this->config->suspended_attribute && $this->config->sync_suspended) {
853 $updatekeys[] = 'suspended';
855 unset($all_keys); unset($key);
858 print_string('noupdatestobedone', 'auth_ldap');
860 if ($do_updates and !empty($updatekeys)) { // run updates only if relevant
861 $users = $DB->get_records_sql('SELECT u.username, u.id
863 WHERE u.deleted = 0 AND u.auth = ? AND u.mnethostid = ?',
864 array($this->authtype, $CFG->mnet_localhost_id));
865 if (!empty($users)) {
866 print_string('userentriestoupdate', 'auth_ldap', count($users));
868 $sitecontext = context_system::instance();
869 if (!empty($this->config->creators) and !empty($this->config->memberattribute)
870 and $roles = get_archetype_roles('coursecreator')) {
871 $creatorrole = array_shift($roles); // We can only use one, let's use the first one
873 $creatorrole = false;
876 $transaction = $DB->start_delegated_transaction();
880 foreach ($users as $user) {
881 echo "\t"; print_string('auth_dbupdatinguser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id));
882 if (!$this->update_user_record($user->username, $updatekeys, true)) {
883 echo ' - '.get_string('skipped');
888 // Update course creators if needed
889 if ($creatorrole !== false) {
890 if ($this->iscreator($user->username)) {
891 role_assign($creatorrole->id, $user->id, $sitecontext->id, $this->roleauth);
893 role_unassign($creatorrole->id, $user->id, $sitecontext->id, $this->roleauth);
897 $transaction->allow_commit();
898 unset($users); // free mem
900 } else { // end do updates
901 print_string('noupdatestobedone', 'auth_ldap');
905 // Find users missing in DB that are in LDAP
906 // and gives me a nifty object I don't want.
907 // note: we do not care about deleted accounts anymore, this feature was replaced by suspending to nologin auth plugin
908 $sql = 'SELECT e.id, e.username
910 LEFT JOIN {user} u ON (e.username = u.username AND e.mnethostid = u.mnethostid)
912 $add_users = $DB->get_records_sql($sql);
914 if (!empty($add_users)) {
915 print_string('userentriestoadd', 'auth_ldap', count($add_users));
917 $sitecontext = context_system::instance();
918 if (!empty($this->config->creators) and !empty($this->config->memberattribute)
919 and $roles = get_archetype_roles('coursecreator')) {
920 $creatorrole = array_shift($roles); // We can only use one, let's use the first one
922 $creatorrole = false;
925 $transaction = $DB->start_delegated_transaction();
926 foreach ($add_users as $user) {
927 $user = $this->get_userinfo_asobj($user->username);
930 $user->modified = time();
931 $user->confirmed = 1;
932 $user->auth = $this->authtype;
933 $user->mnethostid = $CFG->mnet_localhost_id;
934 // get_userinfo_asobj() might have replaced $user->username with the value
935 // from the LDAP server (which can be mixed-case). Make sure it's lowercase
936 $user->username = trim(core_text::strtolower($user->username));
937 // It isn't possible to just rely on the configured suspension attribute since
938 // things like active directory use bit masks, other things using LDAP might
939 // do different stuff as well.
941 // The cast to int is a workaround for MDL-53959.
942 $user->suspended = (int)$this->is_user_suspended($user);
943 if (empty($user->lang)) {
944 $user->lang = $CFG->lang;
946 if (empty($user->calendartype)) {
947 $user->calendartype = $CFG->calendartype;
950 $id = user_create_user($user, false);
951 echo "\t"; print_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)); echo "\n";
952 $euser = $DB->get_record('user', array('id' => $id));
954 if (!empty($this->config->forcechangepassword)) {
955 set_user_preference('auth_forcepasswordchange', 1, $id);
958 // Add course creators if needed
959 if ($creatorrole !== false and $this->iscreator($user->username)) {
960 role_assign($creatorrole->id, $id, $sitecontext->id, $this->roleauth);
964 $transaction->allow_commit();
965 unset($add_users); // free mem
967 print_string('nouserstobeadded', 'auth_ldap');
970 $dbman->drop_table($table);
977 * Update a local user record from an external source.
978 * This is a lighter version of the one in moodlelib -- won't do
979 * expensive ops such as enrolment.
981 * If you don't pass $updatekeys, there is a performance hit and
982 * values removed from LDAP won't be removed from moodle.
984 * @param string $username username
985 * @param boolean $updatekeys true to update the local record with the external LDAP values.
986 * @param bool $triggerevent set false if user_updated event should not be triggered.
987 * This will not affect user_password_updated event triggering.
988 * @return stdClass|bool updated user record or false if there is no new info to update.
990 function update_user_record($username, $updatekeys = false, $triggerevent = false) {
993 // Just in case check text case
994 $username = trim(core_text::strtolower($username));
996 // Get the current user record
997 $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id));
998 if (empty($user)) { // trouble
999 error_log($this->errorlogtag.get_string('auth_dbusernotexist', 'auth_db', '', $username));
1000 print_error('auth_dbusernotexist', 'auth_db', '', $username);
1004 // Protect the userid from being overwritten
1005 $userid = $user->id;
1007 if ($newinfo = $this->get_userinfo($username)) {
1008 $newinfo = truncate_userinfo($newinfo);
1010 if (empty($updatekeys)) { // all keys? this does not support removing values
1011 $updatekeys = array_keys($newinfo);
1014 if (!empty($updatekeys)) {
1015 $newuser = new stdClass();
1016 $newuser->id = $userid;
1017 // The cast to int is a workaround for MDL-53959.
1018 $newuser->suspended = (int)$this->is_user_suspended((object) $newinfo);
1020 foreach ($updatekeys as $key) {
1021 if (isset($newinfo[$key])) {
1022 $value = $newinfo[$key];
1027 if (!empty($this->config->{'field_updatelocal_' . $key})) {
1028 // Only update if it's changed.
1029 if ($user->{$key} != $value) {
1030 $newuser->$key = $value;
1034 user_update_user($newuser, false, $triggerevent);
1039 return $DB->get_record('user', array('id'=>$userid, 'deleted'=>0));
1043 * Bulk insert in SQL's temp table
1045 function ldap_bulk_insert($username) {
1048 $username = core_text::strtolower($username); // usernames are __always__ lowercase.
1049 $DB->insert_record_raw('tmp_extuser', array('username'=>$username,
1050 'mnethostid'=>$CFG->mnet_localhost_id), false, true);
1055 * Activates (enables) user in external LDAP so user can login
1057 * @param mixed $username
1058 * @return boolean result
1060 function user_activate($username) {
1061 $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
1063 $ldapconnection = $this->ldap_connect();
1065 $userdn = $this->ldap_find_userdn($ldapconnection, $extusername);
1066 switch ($this->config->user_type) {
1068 $newinfo['loginDisabled'] = 'FALSE';
1072 // Remember that we add a '*' character in front of the
1073 // external password string to 'disable' the account. We just
1074 // need to remove it.
1075 $sr = ldap_read($ldapconnection, $userdn, '(objectClass=*)',
1076 array('userPassword'));
1077 $info = ldap_get_entries($ldapconnection, $sr);
1078 $info[0] = array_change_key_case($info[0], CASE_LOWER);
1079 $newinfo['userPassword'] = ltrim($info[0]['userpassword'][0], '*');
1082 // We need to unset the ACCOUNTDISABLE bit in the
1083 // userAccountControl attribute ( see
1084 // http://support.microsoft.com/kb/305144 )
1085 $sr = ldap_read($ldapconnection, $userdn, '(objectClass=*)',
1086 array('userAccountControl'));
1087 $info = ldap_get_entries($ldapconnection, $sr);
1088 $info[0] = array_change_key_case($info[0], CASE_LOWER);
1089 $newinfo['userAccountControl'] = $info[0]['useraccountcontrol'][0]
1090 & (~AUTH_AD_ACCOUNTDISABLE);
1093 print_error('user_activatenotsupportusertype', 'auth_ldap', '', $this->config->user_type_name);
1095 $result = ldap_modify($ldapconnection, $userdn, $newinfo);
1096 $this->ldap_close();
1101 * Returns true if user should be coursecreator.
1103 * @param mixed $username username (without system magic quotes)
1104 * @return mixed result null if course creators is not configured, boolean otherwise.
1106 function iscreator($username) {
1107 if (empty($this->config->creators) or empty($this->config->memberattribute)) {
1111 $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
1113 $ldapconnection = $this->ldap_connect();
1115 if ($this->config->memberattribute_isdn) {
1116 if(!($userid = $this->ldap_find_userdn($ldapconnection, $extusername))) {
1120 $userid = $extusername;
1123 $group_dns = explode(';', $this->config->creators);
1124 $creator = ldap_isgroupmember($ldapconnection, $userid, $group_dns, $this->config->memberattribute);
1126 $this->ldap_close();
1132 * Called when the user record is updated.
1134 * Modifies user in external LDAP server. It takes olduser (before
1135 * changes) and newuser (after changes) compares information and
1136 * saves modified information to external LDAP server.
1138 * @param mixed $olduser Userobject before modifications (without system magic quotes)
1139 * @param mixed $newuser Userobject new modified userobject (without system magic quotes)
1140 * @return boolean result
1143 function user_update($olduser, $newuser) {
1146 if (isset($olduser->username) and isset($newuser->username) and $olduser->username != $newuser->username) {
1147 error_log($this->errorlogtag.get_string('renamingnotallowed', 'auth_ldap'));
1151 if (isset($olduser->auth) and $olduser->auth != $this->authtype) {
1152 return true; // just change auth and skip update
1155 $attrmap = $this->ldap_attributes();
1156 // Before doing anything else, make sure we really need to update anything
1157 // in the external LDAP server.
1158 $update_external = false;
1159 foreach ($attrmap as $key => $ldapkeys) {
1160 if (!empty($this->config->{'field_updateremote_'.$key})) {
1161 $update_external = true;
1165 if (!$update_external) {
1169 $extoldusername = core_text::convert($olduser->username, 'utf-8', $this->config->ldapencoding);
1171 $ldapconnection = $this->ldap_connect();
1173 $search_attribs = array();
1174 foreach ($attrmap as $key => $values) {
1175 if (!is_array($values)) {
1176 $values = array($values);
1178 foreach ($values as $value) {
1179 if (!in_array($value, $search_attribs)) {
1180 array_push($search_attribs, $value);
1185 if(!($user_dn = $this->ldap_find_userdn($ldapconnection, $extoldusername))) {
1190 $user_info_result = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs);
1191 if ($user_info_result) {
1192 $user_entry = ldap_get_entries_moodle($ldapconnection, $user_info_result);
1193 if (empty($user_entry)) {
1194 $attribs = join (', ', $search_attribs);
1195 error_log($this->errorlogtag.get_string('updateusernotfound', 'auth_ldap',
1196 array('userdn'=>$user_dn,
1197 'attribs'=>$attribs)));
1198 return false; // old user not found!
1199 } else if (count($user_entry) > 1) {
1200 error_log($this->errorlogtag.get_string('morethanoneuser', 'auth_ldap'));
1204 $user_entry = $user_entry[0];
1206 foreach ($attrmap as $key => $ldapkeys) {
1208 // Only process if the moodle field ($key) has changed and we
1209 // are set to update LDAP with it
1210 $customprofilefield = 'profile_field_' . $key;
1211 if (isset($olduser->$key) and isset($newuser->$key)
1212 and ($olduser->$key !== $newuser->$key)) {
1213 $profilefield = $key;
1214 } else if (isset($olduser->$customprofilefield) && isset($newuser->$customprofilefield)
1215 && $olduser->$customprofilefield !== $newuser->$customprofilefield) {
1216 $profilefield = $customprofilefield;
1219 if (!empty($profilefield) && !empty($this->config->{'field_updateremote_' . $key})) {
1220 // For ldap values that could be in more than one
1221 // ldap key, we will do our best to match
1222 // where they came from
1225 if (!is_array($ldapkeys)) {
1226 $ldapkeys = array($ldapkeys);
1228 if (count($ldapkeys) < 2) {
1232 $nuvalue = core_text::convert($newuser->$profilefield, 'utf-8', $this->config->ldapencoding);
1233 empty($nuvalue) ? $nuvalue = array() : $nuvalue;
1234 $ouvalue = core_text::convert($olduser->$profilefield, 'utf-8', $this->config->ldapencoding);
1236 foreach ($ldapkeys as $ldapkey) {
1237 $ldapkey = $ldapkey;
1238 $ldapvalue = $user_entry[$ldapkey][0];
1240 // Skip update if the values already match
1241 if ($nuvalue !== $ldapvalue) {
1242 // This might fail due to schema validation
1243 if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
1248 error_log($this->errorlogtag.get_string ('updateremfail', 'auth_ldap',
1249 array('errno'=>ldap_errno($ldapconnection),
1250 'errstring'=>ldap_err2str(ldap_errno($ldapconnection)),
1252 'ouvalue'=>$ouvalue,
1253 'nuvalue'=>$nuvalue)));
1258 // Ambiguous. Value empty before in Moodle (and LDAP) - use
1259 // 1st ldap candidate field, no need to guess
1260 if ($ouvalue === '') { // value empty before - use 1st ldap candidate
1261 // This might fail due to schema validation
1262 if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
1267 error_log($this->errorlogtag.get_string ('updateremfail', 'auth_ldap',
1268 array('errno'=>ldap_errno($ldapconnection),
1269 'errstring'=>ldap_err2str(ldap_errno($ldapconnection)),
1271 'ouvalue'=>$ouvalue,
1272 'nuvalue'=>$nuvalue)));
1277 // We found which ldap key to update!
1278 if ($ouvalue !== '' and $ouvalue === $ldapvalue ) {
1279 // This might fail due to schema validation
1280 if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
1285 error_log($this->errorlogtag.get_string ('updateremfail', 'auth_ldap',
1286 array('errno'=>ldap_errno($ldapconnection),
1287 'errstring'=>ldap_err2str(ldap_errno($ldapconnection)),
1289 'ouvalue'=>$ouvalue,
1290 'nuvalue'=>$nuvalue)));
1297 if ($ambiguous and !$changed) {
1299 error_log($this->errorlogtag.get_string ('updateremfailamb', 'auth_ldap',
1301 'ouvalue'=>$ouvalue,
1302 'nuvalue'=>$nuvalue)));
1307 error_log($this->errorlogtag.get_string ('usernotfound', 'auth_ldap'));
1311 $this->ldap_close();
1317 * Changes userpassword in LDAP
1319 * Called when the user password is updated. It assumes it is
1320 * called by an admin or that you've otherwise checked the user's
1323 * @param object $user User table object
1324 * @param string $newpassword Plaintext password (not crypted/md5'ed)
1325 * @return boolean result
1328 function user_update_password($user, $newpassword) {
1332 $username = $user->username;
1334 $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
1335 $extpassword = core_text::convert($newpassword, 'utf-8', $this->config->ldapencoding);
1337 switch ($this->config->passtype) {
1339 $extpassword = '{MD5}' . base64_encode(pack('H*', md5($extpassword)));
1342 $extpassword = '{SHA}' . base64_encode(pack('H*', sha1($extpassword)));
1349 $ldapconnection = $this->ldap_connect();
1351 $user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
1354 error_log($this->errorlogtag.get_string ('nodnforusername', 'auth_ldap', $user->username));
1358 switch ($this->config->user_type) {
1361 $result = ldap_modify($ldapconnection, $user_dn, array('userPassword' => $extpassword));
1363 error_log($this->errorlogtag.get_string ('updatepasserror', 'auth_ldap',
1364 array('errno'=>ldap_errno($ldapconnection),
1365 'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1367 // Update password expiration time, grace logins count
1368 $search_attribs = array($this->config->expireattr, 'passwordExpirationInterval', 'loginGraceLimit');
1369 $sr = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs);
1371 $entry = ldap_get_entries_moodle($ldapconnection, $sr);
1373 $newattrs = array();
1374 if (!empty($info[$this->config->expireattr][0])) {
1375 // Set expiration time only if passwordExpirationInterval is defined
1376 if (!empty($info['passwordexpirationinterval'][0])) {
1377 $expirationtime = time() + $info['passwordexpirationinterval'][0];
1378 $ldapexpirationtime = $this->ldap_unix2expirationtime($expirationtime);
1379 $newattrs['passwordExpirationTime'] = $ldapexpirationtime;
1382 // Set gracelogin count
1383 if (!empty($info['logingracelimit'][0])) {
1384 $newattrs['loginGraceRemaining']= $info['logingracelimit'][0];
1387 // Store attribute changes in LDAP
1388 $result = ldap_modify($ldapconnection, $user_dn, $newattrs);
1390 error_log($this->errorlogtag.get_string ('updatepasserrorexpiregrace', 'auth_ldap',
1391 array('errno'=>ldap_errno($ldapconnection),
1392 'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1397 error_log($this->errorlogtag.get_string ('updatepasserrorexpire', 'auth_ldap',
1398 array('errno'=>ldap_errno($ldapconnection),
1399 'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1404 // Passwords in Active Directory must be encoded as Unicode
1405 // strings (UCS-2 Little Endian format) and surrounded with
1406 // double quotes. See http://support.microsoft.com/?kbid=269190
1407 if (!function_exists('mb_convert_encoding')) {
1408 error_log($this->errorlogtag.get_string ('needmbstring', 'auth_ldap'));
1411 $extpassword = mb_convert_encoding('"'.$extpassword.'"', "UCS-2LE", $this->config->ldapencoding);
1412 $result = ldap_modify($ldapconnection, $user_dn, array('unicodePwd' => $extpassword));
1414 error_log($this->errorlogtag.get_string ('updatepasserror', 'auth_ldap',
1415 array('errno'=>ldap_errno($ldapconnection),
1416 'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1421 // Send LDAP the password in cleartext, it will md5 it itself
1422 $result = ldap_modify($ldapconnection, $user_dn, array('userPassword' => $extpassword));
1424 error_log($this->errorlogtag.get_string ('updatepasserror', 'auth_ldap',
1425 array('errno'=>ldap_errno($ldapconnection),
1426 'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1431 $this->ldap_close();
1436 * Take expirationtime and return it as unix timestamp in seconds
1438 * Takes expiration timestamp as read from LDAP and returns it as unix timestamp in seconds
1439 * Depends on $this->config->user_type variable
1441 * @param mixed time Time stamp read from LDAP as it is.
1442 * @param string $ldapconnection Only needed for Active Directory.
1443 * @param string $user_dn User distinguished name for the user we are checking password expiration (only needed for Active Directory).
1446 function ldap_expirationtime2unix ($time, $ldapconnection, $user_dn) {
1448 switch ($this->config->user_type) {
1450 $yr=substr($time, 0, 4);
1451 $mo=substr($time, 4, 2);
1452 $dt=substr($time, 6, 2);
1453 $hr=substr($time, 8, 2);
1454 $min=substr($time, 10, 2);
1455 $sec=substr($time, 12, 2);
1456 $result = mktime($hr, $min, $sec, $mo, $dt, $yr);
1460 $result = $time * DAYSECS; // The shadowExpire contains the number of DAYS between 01/01/1970 and the actual expiration date
1463 $result = $this->ldap_get_ad_pwdexpire($time, $ldapconnection, $user_dn);
1466 print_error('auth_ldap_usertypeundefined', 'auth_ldap');
1472 * Takes unix timestamp and returns it formated for storing in LDAP
1474 * @param integer unix time stamp
1476 function ldap_unix2expirationtime($time) {
1478 switch ($this->config->user_type) {
1480 $result=date('YmdHis', $time).'Z';
1484 $result = $time ; // Already in correct format
1487 print_error('auth_ldap_usertypeundefined2', 'auth_ldap');
1494 * Returns user attribute mappings between moodle and LDAP
1499 function ldap_attributes () {
1500 $moodleattributes = array();
1501 // If we have custom fields then merge them with user fields.
1502 $customfields = $this->get_custom_user_profile_fields();
1503 if (!empty($customfields) && !empty($this->userfields)) {
1504 $userfields = array_merge($this->userfields, $customfields);
1506 $userfields = $this->userfields;
1509 foreach ($userfields as $field) {
1510 if (!empty($this->config->{"field_map_$field"})) {
1511 $moodleattributes[$field] = core_text::strtolower(trim($this->config->{"field_map_$field"}));
1512 if (preg_match('/,/', $moodleattributes[$field])) {
1513 $moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ?
1517 $moodleattributes['username'] = core_text::strtolower(trim($this->config->user_attribute));
1518 $moodleattributes['suspended'] = core_text::strtolower(trim($this->config->suspended_attribute));
1519 return $moodleattributes;
1523 * Returns all usernames from LDAP
1525 * @param $filter An LDAP search filter to select desired users
1526 * @return array of LDAP user names converted to UTF-8
1528 function ldap_get_userlist($filter='*') {
1531 $ldapconnection = $this->ldap_connect();
1533 if ($filter == '*') {
1534 $filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')';
1537 $contexts = explode(';', $this->config->contexts);
1538 if (!empty($this->config->create_context)) {
1539 array_push($contexts, $this->config->create_context);
1543 $ldap_pagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
1544 foreach ($contexts as $context) {
1545 $context = trim($context);
1546 if (empty($context)) {
1551 if ($ldap_pagedresults) {
1552 ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
1554 if ($this->config->search_sub) {
1555 // Use ldap_search to find first user from subtree.
1556 $ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
1558 // Search only in this context.
1559 $ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
1564 if ($ldap_pagedresults) {
1565 ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
1567 $users = ldap_get_entries_moodle($ldapconnection, $ldap_result);
1568 // Add found users to list.
1569 for ($i = 0; $i < count($users); $i++) {
1570 $extuser = core_text::convert($users[$i][$this->config->user_attribute][0],
1571 $this->config->ldapencoding, 'utf-8');
1572 array_push($fresult, $extuser);
1574 unset($ldap_result); // Free mem.
1575 } while ($ldap_pagedresults && !empty($ldap_cookie));
1578 // If paged results were used, make sure the current connection is completely closed
1579 $this->ldap_close($ldap_pagedresults);
1584 * Indicates if password hashes should be stored in local moodle database.
1586 * @return bool true means flag 'not_cached' stored instead of password hash
1588 function prevent_local_passwords() {
1589 return !empty($this->config->preventpassindb);
1593 * Returns true if this authentication plugin is 'internal'.
1597 function is_internal() {
1602 * Returns true if this authentication plugin can change the user's
1607 function can_change_password() {
1608 return !empty($this->config->stdchangepassword) or !empty($this->config->changepasswordurl);
1612 * Returns the URL for changing the user's password, or empty if the default can
1615 * @return moodle_url
1617 function change_password_url() {
1618 if (empty($this->config->stdchangepassword)) {
1619 if (!empty($this->config->changepasswordurl)) {
1620 return new moodle_url($this->config->changepasswordurl);
1630 * Will get called before the login page is shownr. Ff NTLM SSO
1631 * is enabled, and the user is in the right network, we'll redirect
1632 * to the magic NTLM page for SSO...
1635 function loginpage_hook() {
1636 global $CFG, $SESSION;
1638 // HTTPS is potentially required
1639 //httpsrequired(); - this must be used before setting the URL, it is already done on the login/index.php
1641 if (($_SERVER['REQUEST_METHOD'] === 'GET' // Only on initial GET of loginpage
1642 || ($_SERVER['REQUEST_METHOD'] === 'POST'
1643 && (get_local_referer() != strip_querystring(qualified_me()))))
1644 // Or when POSTed from another place
1646 && !empty($this->config->ntlmsso_enabled) // SSO enabled
1647 && !empty($this->config->ntlmsso_subnet) // have a subnet to test for
1648 && empty($_GET['authldap_skipntlmsso']) // haven't failed it yet
1649 && (isguestuser() || !isloggedin()) // guestuser or not-logged-in users
1650 && address_in_subnet(getremoteaddr(), $this->config->ntlmsso_subnet)) {
1652 // First, let's remember where we were trying to get to before we got here
1653 if (empty($SESSION->wantsurl)) {
1654 $SESSION->wantsurl = null;
1655 $referer = get_local_referer(false);
1657 $referer != $CFG->wwwroot &&
1658 $referer != $CFG->wwwroot . '/' &&
1659 $referer != $CFG->httpswwwroot . '/login/' &&
1660 $referer != $CFG->httpswwwroot . '/login/index.php') {
1661 $SESSION->wantsurl = $referer;
1665 // Now start the whole NTLM machinery.
1666 if($this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESATTEMPT ||
1667 $this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESFORM) {
1668 if (core_useragent::is_ie()) {
1669 $sesskey = sesskey();
1670 redirect($CFG->wwwroot.'/auth/ldap/ntlmsso_magic.php?sesskey='.$sesskey);
1671 } else if ($this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESFORM) {
1672 redirect($CFG->httpswwwroot.'/login/index.php?authldap_skipntlmsso=1');
1675 redirect($CFG->wwwroot.'/auth/ldap/ntlmsso_attempt.php');
1678 // No NTLM SSO, Use the normal login page instead.
1680 // If $SESSION->wantsurl is empty and we have a 'Referer:' header, the login
1681 // page insists on redirecting us to that page after user validation. If
1682 // we clicked on the redirect link at the ntlmsso_finish.php page (instead
1683 // of waiting for the redirection to happen) then we have a 'Referer:' header
1684 // we don't want to use at all. As we can't get rid of it, just point
1685 // $SESSION->wantsurl to $CFG->wwwroot (after all, we came from there).
1686 if (empty($SESSION->wantsurl)
1687 && (get_local_referer() == $CFG->httpswwwroot.'/auth/ldap/ntlmsso_finish.php')) {
1689 $SESSION->wantsurl = $CFG->wwwroot;
1694 * To be called from a page running under NTLM's
1695 * "Integrated Windows Authentication".
1697 * If successful, it will set a special "cookie" (not an HTTP cookie!)
1698 * in cache_flags under the $this->pluginconfig/ntlmsess "plugin" and return true.
1699 * The "cookie" will be picked up by ntlmsso_finish() to complete the
1702 * On failure it will return false for the caller to display an appropriate
1703 * error message (probably saying that Integrated Windows Auth isn't enabled!)
1705 * NOTE that this code will execute under the OS user credentials,
1706 * so we MUST avoid dealing with files -- such as session files.
1707 * (The caller should define('NO_MOODLE_COOKIES', true) before including config.php)
1710 function ntlmsso_magic($sesskey) {
1711 if (isset($_SERVER['REMOTE_USER']) && !empty($_SERVER['REMOTE_USER'])) {
1713 // HTTP __headers__ seem to be sent in ISO-8859-1 encoding
1714 // (according to my reading of RFC-1945, RFC-2616 and RFC-2617 and
1715 // my local tests), so we need to convert the REMOTE_USER value
1716 // (i.e., what we got from the HTTP WWW-Authenticate header) into UTF-8
1717 $username = core_text::convert($_SERVER['REMOTE_USER'], 'iso-8859-1', 'utf-8');
1719 switch ($this->config->ntlmsso_type) {
1721 // The format is now configurable, so try to extract the username
1722 $username = $this->get_ntlm_remote_user($username);
1723 if (empty($username)) {
1728 // Format is username@DOMAIN
1729 $username = substr($username, 0, strpos($username, '@'));
1732 error_log($this->errorlogtag.get_string ('ntlmsso_unknowntype', 'auth_ldap'));
1733 return false; // Should never happen!
1736 $username = core_text::strtolower($username); // Compatibility hack
1737 set_cache_flag($this->pluginconfig.'/ntlmsess', $sesskey, $username, AUTH_NTLMTIMEOUT);
1744 * Find the session set by ntlmsso_magic(), validate it and
1745 * call authenticate_user_login() to authenticate the user through
1746 * the auth machinery.
1748 * It is complemented by a similar check in user_login().
1750 * If it succeeds, it never returns.
1753 function ntlmsso_finish() {
1754 global $CFG, $USER, $SESSION;
1757 $cf = get_cache_flags($this->pluginconfig.'/ntlmsess');
1758 if (!isset($cf[$key]) || $cf[$key] === '') {
1761 $username = $cf[$key];
1763 // Here we want to trigger the whole authentication machinery
1764 // to make sure no step is bypassed...
1765 $user = authenticate_user_login($username, $key);
1767 complete_user_login($user);
1769 // Cleanup the key to prevent reuse...
1770 // and to allow re-logins with normal credentials
1771 unset_cache_flag($this->pluginconfig.'/ntlmsess', $key);
1774 if (user_not_fully_set_up($USER, true)) {
1775 $urltogo = $CFG->wwwroot.'/user/edit.php';
1776 // We don't delete $SESSION->wantsurl yet, so we get there later
1777 } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
1778 $urltogo = $SESSION->wantsurl; // Because it's an address in this site
1779 unset($SESSION->wantsurl);
1781 // No wantsurl stored or external - go to homepage
1782 $urltogo = $CFG->wwwroot.'/';
1783 unset($SESSION->wantsurl);
1785 // We do not want to redirect if we are in a PHPUnit test.
1786 if (!PHPUNIT_TEST) {
1790 // Should never reach here.
1795 * Sync roles for this user
1797 * @param $user object user object (without system magic quotes)
1799 function sync_roles($user) {
1800 $iscreator = $this->iscreator($user->username);
1801 if ($iscreator === null) {
1802 return; // Nothing to sync - creators not configured
1805 if ($roles = get_archetype_roles('coursecreator')) {
1806 $creatorrole = array_shift($roles); // We can only use one, let's use the first one
1807 $systemcontext = context_system::instance();
1809 if ($iscreator) { // Following calls will not create duplicates
1810 role_assign($creatorrole->id, $user->id, $systemcontext->id, $this->roleauth);
1812 // Unassign only if previously assigned by this plugin!
1813 role_unassign($creatorrole->id, $user->id, $systemcontext->id, $this->roleauth);
1819 * Get password expiration time for a given user from Active Directory
1821 * @param string $pwdlastset The time last time we changed the password.
1822 * @param resource $lcapconn The open LDAP connection.
1823 * @param string $user_dn The distinguished name of the user we are checking.
1825 * @return string $unixtime
1827 function ldap_get_ad_pwdexpire($pwdlastset, $ldapconn, $user_dn){
1830 if (!function_exists('bcsub')) {
1831 error_log($this->errorlogtag.get_string ('needbcmath', 'auth_ldap'));
1835 // If UF_DONT_EXPIRE_PASSWD flag is set in user's
1836 // userAccountControl attribute, the password doesn't expire.
1837 $sr = ldap_read($ldapconn, $user_dn, '(objectClass=*)',
1838 array('userAccountControl'));
1840 error_log($this->errorlogtag.get_string ('useracctctrlerror', 'auth_ldap', $user_dn));
1841 // Don't expire password, as we are not sure if it has to be
1846 $entry = ldap_get_entries_moodle($ldapconn, $sr);
1848 $useraccountcontrol = $info['useraccountcontrol'][0];
1849 if ($useraccountcontrol & UF_DONT_EXPIRE_PASSWD) {
1850 // Password doesn't expire.
1854 // If pwdLastSet is zero, the user must change his/her password now
1855 // (unless UF_DONT_EXPIRE_PASSWD flag is set, but we already
1856 // tested this above)
1857 if ($pwdlastset === '0') {
1858 // Password has expired
1862 // ----------------------------------------------------------------
1863 // Password expiration time in Active Directory is the composition of
1866 // - User's pwdLastSet attribute, that stores the last time
1867 // the password was changed.
1869 // - Domain's maxPwdAge attribute, that sets how long
1870 // passwords last in this domain.
1872 // We already have the first value (passed in as a parameter). We
1873 // need to get the second one. As we don't know the domain DN, we
1874 // have to query rootDSE's defaultNamingContext attribute to get
1875 // it. Then we have to query that DN's maxPwdAge attribute to get
1878 // Once we have both values, we just need to combine them. But MS
1879 // chose to use a different base and unit for time measurements.
1880 // So we need to convert the values to Unix timestamps (see
1882 // ----------------------------------------------------------------
1884 $sr = ldap_read($ldapconn, ROOTDSE, '(objectClass=*)',
1885 array('defaultNamingContext'));
1887 error_log($this->errorlogtag.get_string ('rootdseerror', 'auth_ldap'));
1891 $entry = ldap_get_entries_moodle($ldapconn, $sr);
1893 $domaindn = $info['defaultnamingcontext'][0];
1895 $sr = ldap_read ($ldapconn, $domaindn, '(objectClass=*)',
1896 array('maxPwdAge'));
1897 $entry = ldap_get_entries_moodle($ldapconn, $sr);
1899 $maxpwdage = $info['maxpwdage'][0];
1900 if ($sr = ldap_read($ldapconn, $user_dn, '(objectClass=*)', array('msDS-ResultantPSO'))) {
1901 if ($entry = ldap_get_entries_moodle($ldapconn, $sr)) {
1903 $userpso = $info['msds-resultantpso'][0];
1905 // If a PSO exists, FGPP is being utilized.
1906 // Grab the new maxpwdage from the msDS-MaximumPasswordAge attribute of the PSO.
1907 if (!empty($userpso)) {
1908 $sr = ldap_read($ldapconn, $userpso, '(objectClass=*)', array('msDS-MaximumPasswordAge'));
1909 if ($entry = ldap_get_entries_moodle($ldapconn, $sr)) {
1911 // Default value of msds-maximumpasswordage is 42 and is always set.
1912 $maxpwdage = $info['msds-maximumpasswordage'][0];
1917 // ----------------------------------------------------------------
1918 // MSDN says that "pwdLastSet contains the number of 100 nanosecond
1919 // intervals since January 1, 1601 (UTC), stored in a 64 bit integer".
1921 // According to Perl's Date::Manip, the number of seconds between
1922 // this date and Unix epoch is 11644473600. So we have to
1923 // substract this value to calculate a Unix time, once we have
1924 // scaled pwdLastSet to seconds. This is the script used to
1925 // calculate the value shown above:
1927 // #!/usr/bin/perl -w
1931 // $date1 = ParseDate ("160101010000 UTC");
1932 // $date2 = ParseDate ("197001010000 UTC");
1933 // $delta = DateCalc($date1, $date2, \$err);
1934 // $secs = Delta_Format($delta, 0, "%st");
1935 // print "$secs \n";
1937 // MSDN also says that "maxPwdAge is stored as a large integer that
1938 // represents the number of 100 nanosecond intervals from the time
1939 // the password was set before the password expires." We also need
1940 // to scale this to seconds. Bear in mind that this value is stored
1941 // as a _negative_ quantity (at least in my AD domain).
1943 // As a last remark, if the low 32 bits of maxPwdAge are equal to 0,
1944 // the maximum password age in the domain is set to 0, which means
1945 // passwords do not expire (see
1946 // http://msdn2.microsoft.com/en-us/library/ms974598.aspx)
1948 // As the quantities involved are too big for PHP integers, we
1949 // need to use BCMath functions to work with arbitrary precision
1951 // ----------------------------------------------------------------
1953 // If the low order 32 bits are 0, then passwords do not expire in
1954 // the domain. Just do '$maxpwdage mod 2^32' and check the result
1955 // (2^32 = 4294967296)
1956 if (bcmod ($maxpwdage, 4294967296) === '0') {
1960 // Add up pwdLastSet and maxPwdAge to get password expiration
1961 // time, in MS time units. Remember maxPwdAge is stored as a
1962 // _negative_ quantity, so we need to substract it in fact.
1963 $pwdexpire = bcsub ($pwdlastset, $maxpwdage);
1965 // Scale the result to convert it to Unix time units and return
1967 return bcsub( bcdiv($pwdexpire, '10000000'), '11644473600');
1971 * Connect to the LDAP server, using the plugin configured
1972 * settings. It's actually a wrapper around ldap_connect_moodle()
1974 * @return resource A valid LDAP connection (or dies if it can't connect)
1976 function ldap_connect() {
1977 // Cache ldap connections. They are expensive to set up
1978 // and can drain the TCP/IP ressources on the server if we
1979 // are syncing a lot of users (as we try to open a new connection
1980 // to get the user details). This is the least invasive way
1981 // to reuse existing connections without greater code surgery.
1982 if(!empty($this->ldapconnection)) {
1984 return $this->ldapconnection;
1987 if($ldapconnection = ldap_connect_moodle($this->config->host_url, $this->config->ldap_version,
1988 $this->config->user_type, $this->config->bind_dn,
1989 $this->config->bind_pw, $this->config->opt_deref,
1990 $debuginfo, $this->config->start_tls)) {
1991 $this->ldapconns = 1;
1992 $this->ldapconnection = $ldapconnection;
1993 return $ldapconnection;
1996 print_error('auth_ldap_noconnect_all', 'auth_ldap', '', $debuginfo);
2000 * Disconnects from a LDAP server
2002 * @param force boolean Forces closing the real connection to the LDAP server, ignoring any
2003 * cached connections. This is needed when we've used paged results
2004 * and want to use normal results again.
2006 function ldap_close($force=false) {
2008 if (($this->ldapconns == 0) || ($force)) {
2009 $this->ldapconns = 0;
2010 @ldap_close($this->ldapconnection);
2011 unset($this->ldapconnection);
2016 * Search specified contexts for username and return the user dn
2017 * like: cn=username,ou=suborg,o=org. It's actually a wrapper
2018 * around ldap_find_userdn().
2020 * @param resource $ldapconnection a valid LDAP connection
2021 * @param string $extusername the username to search (in external LDAP encoding, no db slashes)
2022 * @return mixed the user dn (external LDAP encoding) or false
2024 function ldap_find_userdn($ldapconnection, $extusername) {
2025 $ldap_contexts = explode(';', $this->config->contexts);
2026 if (!empty($this->config->create_context)) {
2027 array_push($ldap_contexts, $this->config->create_context);
2030 return ldap_find_userdn($ldapconnection, $extusername, $ldap_contexts, $this->config->objectclass,
2031 $this->config->user_attribute, $this->config->search_sub);
2035 * When using NTLM SSO, the format of the remote username we get in
2036 * $_SERVER['REMOTE_USER'] may vary, depending on where from and how the web
2037 * server gets the data. So we let the admin configure the format using two
2038 * place holders (%domain% and %username%). This function tries to extract
2039 * the username (stripping the domain part and any separators if they are
2040 * present) from the value present in $_SERVER['REMOTE_USER'], using the
2041 * configured format.
2043 * @param string $remoteuser The value from $_SERVER['REMOTE_USER'] (converted to UTF-8)
2045 * @return string The remote username (without domain part or
2046 * separators). Empty string if we can't extract the username.
2048 protected function get_ntlm_remote_user($remoteuser) {
2049 if (empty($this->config->ntlmsso_remoteuserformat)) {
2050 $format = AUTH_NTLM_DEFAULT_FORMAT;
2052 $format = $this->config->ntlmsso_remoteuserformat;
2055 $format = preg_quote($format);
2056 $formatregex = preg_replace(array('#%domain%#', '#%username%#'),
2057 array('('.AUTH_NTLM_VALID_DOMAINNAME.')', '('.AUTH_NTLM_VALID_USERNAME.')'),
2059 if (preg_match('#^'.$formatregex.'$#', $remoteuser, $matches)) {
2060 $user = end($matches);
2064 /* We are unable to extract the username with the configured format. Probably
2065 * the format specified is wrong, so log a warning for the admin and return
2066 * an empty username.
2068 error_log($this->errorlogtag.get_string ('auth_ntlmsso_maybeinvalidformat', 'auth_ldap'));
2073 * Check if the diagnostic message for the LDAP login error tells us that the
2074 * login is denied because the user password has expired or the password needs
2075 * to be changed on first login (using interactive SMB/Windows logins, not
2078 * @param string the diagnostic message for the LDAP login error
2079 * @return bool true if the password has expired or the password must be changed on first login
2081 protected function ldap_ad_pwdexpired_from_diagmsg($diagmsg) {
2082 // The format of the diagnostic message is (actual examples from W2003 and W2008):
2083 // "80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece" (W2003)
2084 // "80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 773, vece" (W2003)
2085 // "80090308: LdapErr: DSID-0C0903AA, comment: AcceptSecurityContext error, data 52e, v1771" (W2008)
2086 // "80090308: LdapErr: DSID-0C0903AA, comment: AcceptSecurityContext error, data 773, v1771" (W2008)
2087 // We are interested in the 'data nnn' part.
2088 // if nnn == 773 then user must change password on first login
2089 // if nnn == 532 then user password has expired
2090 $diagmsg = explode(',', $diagmsg);
2091 if (preg_match('/data (773|532)/i', trim($diagmsg[2]))) {
2098 * Check if a user is suspended. This function is intended to be used after calling
2099 * get_userinfo_asobj. This is needed because LDAP doesn't have a notion of disabled
2100 * users, however things like MS Active Directory support it and expose information
2103 * @param object $user the user object returned by get_userinfo_asobj
2106 protected function is_user_suspended($user) {
2107 if (!$this->config->suspended_attribute || !isset($user->suspended)) {
2110 if ($this->config->suspended_attribute == 'useraccountcontrol' && $this->config->user_type == 'ad') {
2111 return (bool)($user->suspended & AUTH_AD_ACCOUNTDISABLE);
2114 return (bool)$user->suspended;
2118 * Test if settings are correct, print info to output.
2120 public function test_settings() {
2123 if (!function_exists('ldap_connect')) { // Is php-ldap really there?
2124 echo $OUTPUT->notification(get_string('auth_ldap_noextension', 'auth_ldap'));
2128 // Check to see if this is actually configured.
2129 if ((isset($this->config->host_url)) && ($this->config->host_url !== '')) {
2132 $ldapconn = $this->ldap_connect();
2133 // Try to connect to the LDAP server. See if the page size setting is supported on this server.
2134 $pagedresultssupported = ldap_paged_results_supported($this->config->ldap_version, $ldapconn);
2135 } catch (Exception $e) {
2137 // If we couldn't connect and get the supported options, we can only assume we don't support paged results.
2138 $pagedresultssupported = false;
2141 // Display paged file results.
2142 if ((!$pagedresultssupported)) {
2143 echo $OUTPUT->notification(get_string('pagedresultsnotsupp', 'auth_ldap'), \core\output\notification::NOTIFY_INFO);
2144 } else if ($ldapconn) {
2145 // We were able to connect successfuly.
2146 echo $OUTPUT->notification(get_string('connectingldapsuccess', 'auth_ldap'), \core\output\notification::NOTIFY_SUCCESS);
2150 // LDAP is not even configured.
2151 echo $OUTPUT->notification(get_string('ldapnotconfigured', 'auth_ldap'), \core\output\notification::NOTIFY_INFO);
2154 } // End of the class