6cf1f2819ec6d8174ee4b99a91e0bf3c678c16c0
[moodle.git] / auth / ldap / auth.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Authentication Plugin: LDAP Authentication
19  * Authentication using LDAP (Lightweight Directory Access Protocol).
20  *
21  * @package auth_ldap
22  * @author Martin Dougiamas
23  * @author IƱaki Arenaza
24  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
25  */
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);
32 }
33 if (!defined('AUTH_AD_NORMAL_ACCOUNT')) {
34     define('AUTH_AD_NORMAL_ACCOUNT', 0x0200);
35 }
36 if (!defined('AUTH_NTLMTIMEOUT')) {  // timewindow for the NTLM SSO process, in secs...
37     define('AUTH_NTLMTIMEOUT', 10);
38 }
40 // UF_DONT_EXPIRE_PASSWD value taken from MSDN directly
41 if (!defined('UF_DONT_EXPIRE_PASSWD')) {
42     define ('UF_DONT_EXPIRE_PASSWD', 0x00010000);
43 }
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);
48 }
49 if (!defined('AUTH_GID_NOGROUP')) {
50     define('AUTH_GID_NOGROUP', -2);
51 }
53 // Regular expressions for a valid NTLM username and domain name.
54 if (!defined('AUTH_NTLM_VALID_USERNAME')) {
55     define('AUTH_NTLM_VALID_USERNAME', '[^/\\\\\\\\\[\]:;|=,+*?<>@"]+');
56 }
57 if (!defined('AUTH_NTLM_VALID_DOMAINNAME')) {
58     define('AUTH_NTLM_VALID_DOMAINNAME', '[^\\\\\\\\\/:*?"<>|]+');
59 }
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%');
63 }
64 if (!defined('AUTH_NTLM_FASTPATH_ATTEMPT')) {
65     define('AUTH_NTLM_FASTPATH_ATTEMPT', 0);
66 }
67 if (!defined('AUTH_NTLM_FASTPATH_YESFORM')) {
68     define('AUTH_NTLM_FASTPATH_YESFORM', 1);
69 }
70 if (!defined('AUTH_NTLM_FASTPATH_YESATTEMPT')) {
71     define('AUTH_NTLM_FASTPATH_YESATTEMPT', 2);
72 }
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);
77 }
79 require_once($CFG->libdir.'/authlib.php');
80 require_once($CFG->libdir.'/ldaplib.php');
81 require_once($CFG->dirroot.'/user/lib.php');
82 require_once($CFG->dirroot.'/auth/ldap/locallib.php');
84 /**
85  * LDAP authentication plugin.
86  */
87 class auth_plugin_ldap extends auth_plugin_base {
89     /**
90      * Init plugin config from database settings depending on the plugin auth type.
91      */
92     function init_plugin($authtype) {
93         $this->pluginconfig = 'auth_'.$authtype;
94         $this->config = get_config($this->pluginconfig);
95         if (empty($this->config->ldapencoding)) {
96             $this->config->ldapencoding = 'utf-8';
97         }
98         if (empty($this->config->user_type)) {
99             $this->config->user_type = 'default';
100         }
102         $ldap_usertypes = ldap_supported_usertypes();
103         $this->config->user_type_name = $ldap_usertypes[$this->config->user_type];
104         unset($ldap_usertypes);
106         $default = ldap_getdefaults();
108         // Use defaults if values not given
109         foreach ($default as $key => $value) {
110             // watch out - 0, false are correct values too
111             if (!isset($this->config->{$key}) or $this->config->{$key} == '') {
112                 $this->config->{$key} = $value[$this->config->user_type];
113             }
114         }
116         // Hack prefix to objectclass
117         $this->config->objectclass = ldap_normalise_objectclass($this->config->objectclass);
118     }
120     /**
121      * Constructor with initialisation.
122      */
123     public function __construct() {
124         $this->authtype = 'ldap';
125         $this->roleauth = 'auth_ldap';
126         $this->errorlogtag = '[AUTH LDAP] ';
127         $this->init_plugin($this->authtype);
128     }
130     /**
131      * Old syntax of class constructor. Deprecated in PHP7.
132      *
133      * @deprecated since Moodle 3.1
134      */
135     public function auth_plugin_ldap() {
136         debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
137         self::__construct();
138     }
140     /**
141      * Returns true if the username and password work and false if they are
142      * wrong or don't exist.
143      *
144      * @param string $username The username (without system magic quotes)
145      * @param string $password The password (without system magic quotes)
146      *
147      * @return bool Authentication success or failure.
148      */
149     function user_login($username, $password) {
150         if (! function_exists('ldap_bind')) {
151             print_error('auth_ldapnotinstalled', 'auth_ldap');
152             return false;
153         }
155         if (!$username or !$password) {    // Don't allow blank usernames or passwords
156             return false;
157         }
159         $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
160         $extpassword = core_text::convert($password, 'utf-8', $this->config->ldapencoding);
162         // Before we connect to LDAP, check if this is an AD SSO login
163         // if we succeed in this block, we'll return success early.
164         //
165         $key = sesskey();
166         if (!empty($this->config->ntlmsso_enabled) && $key === $password) {
167             $cf = get_cache_flags($this->pluginconfig.'/ntlmsess');
168             // We only get the cache flag if we retrieve it before
169             // it expires (AUTH_NTLMTIMEOUT seconds).
170             if (!isset($cf[$key]) || $cf[$key] === '') {
171                 return false;
172             }
174             $sessusername = $cf[$key];
175             if ($username === $sessusername) {
176                 unset($sessusername);
177                 unset($cf);
179                 // Check that the user is inside one of the configured LDAP contexts
180                 $validuser = false;
181                 $ldapconnection = $this->ldap_connect();
182                 // if the user is not inside the configured contexts,
183                 // ldap_find_userdn returns false.
184                 if ($this->ldap_find_userdn($ldapconnection, $extusername)) {
185                     $validuser = true;
186                 }
187                 $this->ldap_close();
189                 // Shortcut here - SSO confirmed
190                 return $validuser;
191             }
192         } // End SSO processing
193         unset($key);
195         $ldapconnection = $this->ldap_connect();
196         $ldap_user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
198         // If ldap_user_dn is empty, user does not exist
199         if (!$ldap_user_dn) {
200             $this->ldap_close();
201             return false;
202         }
204         // Try to bind with current username and password
205         $ldap_login = @ldap_bind($ldapconnection, $ldap_user_dn, $extpassword);
207         // If login fails and we are using MS Active Directory, retrieve the diagnostic
208         // message to see if this is due to an expired password, or that the user is forced to
209         // change the password on first login. If it is, only proceed if we can change
210         // password from Moodle (otherwise we'll get stuck later in the login process).
211         if (!$ldap_login && ($this->config->user_type == 'ad')
212             && $this->can_change_password()
213             && (!empty($this->config->expiration) and ($this->config->expiration == 1))) {
215             // We need to get the diagnostic message right after the call to ldap_bind(),
216             // before any other LDAP operation.
217             ldap_get_option($ldapconnection, LDAP_OPT_DIAGNOSTIC_MESSAGE, $diagmsg);
219             if ($this->ldap_ad_pwdexpired_from_diagmsg($diagmsg)) {
220                 // If login failed because user must change the password now or the
221                 // password has expired, let the user in. We'll catch this later in the
222                 // login process when we explicitly check for expired passwords.
223                 $ldap_login = true;
224             }
225         }
226         $this->ldap_close();
227         return $ldap_login;
228     }
230     /**
231      * Reads user information from ldap and returns it in array()
232      *
233      * Function should return all information available. If you are saving
234      * this information to moodle user-table you should honor syncronization flags
235      *
236      * @param string $username username
237      *
238      * @return mixed array with no magic quotes or false on error
239      */
240     function get_userinfo($username) {
241         $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
243         $ldapconnection = $this->ldap_connect();
244         if(!($user_dn = $this->ldap_find_userdn($ldapconnection, $extusername))) {
245             $this->ldap_close();
246             return false;
247         }
249         $search_attribs = array();
250         $attrmap = $this->ldap_attributes();
251         foreach ($attrmap as $key => $values) {
252             if (!is_array($values)) {
253                 $values = array($values);
254             }
255             foreach ($values as $value) {
256                 if (!in_array($value, $search_attribs)) {
257                     array_push($search_attribs, $value);
258                 }
259             }
260         }
262         if (!$user_info_result = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs)) {
263             $this->ldap_close();
264             return false; // error!
265         }
267         $user_entry = ldap_get_entries_moodle($ldapconnection, $user_info_result);
268         if (empty($user_entry)) {
269             $this->ldap_close();
270             return false; // entry not found
271         }
273         $result = array();
274         foreach ($attrmap as $key => $values) {
275             if (!is_array($values)) {
276                 $values = array($values);
277             }
278             $ldapval = NULL;
279             foreach ($values as $value) {
280                 $entry = $user_entry[0];
281                 if (($value == 'dn') || ($value == 'distinguishedname')) {
282                     $result[$key] = $user_dn;
283                     continue;
284                 }
285                 if (!array_key_exists($value, $entry)) {
286                     continue; // wrong data mapping!
287                 }
288                 if (is_array($entry[$value])) {
289                     $newval = core_text::convert($entry[$value][0], $this->config->ldapencoding, 'utf-8');
290                 } else {
291                     $newval = core_text::convert($entry[$value], $this->config->ldapencoding, 'utf-8');
292                 }
293                 if (!empty($newval)) { // favour ldap entries that are set
294                     $ldapval = $newval;
295                 }
296             }
297             if (!is_null($ldapval)) {
298                 $result[$key] = $ldapval;
299             }
300         }
302         $this->ldap_close();
303         return $result;
304     }
306     /**
307      * Reads user information from ldap and returns it in an object
308      *
309      * @param string $username username (with system magic quotes)
310      * @return mixed object or false on error
311      */
312     function get_userinfo_asobj($username) {
313         $user_array = $this->get_userinfo($username);
314         if ($user_array == false) {
315             return false; //error or not found
316         }
317         $user_array = truncate_userinfo($user_array);
318         $user = new stdClass();
319         foreach ($user_array as $key=>$value) {
320             $user->{$key} = $value;
321         }
322         return $user;
323     }
325     /**
326      * Returns all usernames from LDAP
327      *
328      * get_userlist returns all usernames from LDAP
329      *
330      * @return array
331      */
332     function get_userlist() {
333         return $this->ldap_get_userlist("({$this->config->user_attribute}=*)");
334     }
336     /**
337      * Checks if user exists on LDAP
338      *
339      * @param string $username
340      */
341     function user_exists($username) {
342         $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
344         // Returns true if given username exists on ldap
345         $users = $this->ldap_get_userlist('('.$this->config->user_attribute.'='.ldap_filter_addslashes($extusername).')');
346         return count($users);
347     }
349     /**
350      * Creates a new user on LDAP.
351      * By using information in userobject
352      * Use user_exists to prevent duplicate usernames
353      *
354      * @param mixed $userobject  Moodle userobject
355      * @param mixed $plainpass   Plaintext password
356      */
357     function user_create($userobject, $plainpass) {
358         $extusername = core_text::convert($userobject->username, 'utf-8', $this->config->ldapencoding);
359         $extpassword = core_text::convert($plainpass, 'utf-8', $this->config->ldapencoding);
361         switch ($this->config->passtype) {
362             case 'md5':
363                 $extpassword = '{MD5}' . base64_encode(pack('H*', md5($extpassword)));
364                 break;
365             case 'sha1':
366                 $extpassword = '{SHA}' . base64_encode(pack('H*', sha1($extpassword)));
367                 break;
368             case 'plaintext':
369             default:
370                 break; // plaintext
371         }
373         $ldapconnection = $this->ldap_connect();
374         $attrmap = $this->ldap_attributes();
376         $newuser = array();
378         foreach ($attrmap as $key => $values) {
379             if (!is_array($values)) {
380                 $values = array($values);
381             }
382             foreach ($values as $value) {
383                 if (!empty($userobject->$key) ) {
384                     $newuser[$value] = core_text::convert($userobject->$key, 'utf-8', $this->config->ldapencoding);
385                 }
386             }
387         }
389         //Following sets all mandatory and other forced attribute values
390         //User should be creted as login disabled untill email confirmation is processed
391         //Feel free to add your user type and send patches to paca@sci.fi to add them
392         //Moodle distribution
394         switch ($this->config->user_type)  {
395             case 'edir':
396                 $newuser['objectClass']   = array('inetOrgPerson', 'organizationalPerson', 'person', 'top');
397                 $newuser['uniqueId']      = $extusername;
398                 $newuser['logindisabled'] = 'TRUE';
399                 $newuser['userpassword']  = $extpassword;
400                 $uadd = ldap_add($ldapconnection, $this->config->user_attribute.'='.ldap_addslashes($extusername).','.$this->config->create_context, $newuser);
401                 break;
402             case 'rfc2307':
403             case 'rfc2307bis':
404                 // posixAccount object class forces us to specify a uidNumber
405                 // and a gidNumber. That is quite complicated to generate from
406                 // Moodle without colliding with existing numbers and without
407                 // race conditions. As this user is supposed to be only used
408                 // with Moodle (otherwise the user would exist beforehand) and
409                 // doesn't need to login into a operating system, we assign the
410                 // user the uid of user 'nobody' and gid of group 'nogroup'. In
411                 // addition to that, we need to specify a home directory. We
412                 // use the root directory ('/') as the home directory, as this
413                 // is the only one can always be sure exists. Finally, even if
414                 // it's not mandatory, we specify '/bin/false' as the login
415                 // shell, to prevent the user from login in at the operating
416                 // system level (Moodle ignores this).
418                 $newuser['objectClass']   = array('posixAccount', 'inetOrgPerson', 'organizationalPerson', 'person', 'top');
419                 $newuser['cn']            = $extusername;
420                 $newuser['uid']           = $extusername;
421                 $newuser['uidNumber']     = AUTH_UID_NOBODY;
422                 $newuser['gidNumber']     = AUTH_GID_NOGROUP;
423                 $newuser['homeDirectory'] = '/';
424                 $newuser['loginShell']    = '/bin/false';
426                 // IMPORTANT:
427                 // We have to create the account locked, but posixAccount has
428                 // no attribute to achive this reliably. So we are going to
429                 // modify the password in a reversable way that we can later
430                 // revert in user_activate().
431                 //
432                 // Beware that this can be defeated by the user if we are not
433                 // using MD5 or SHA-1 passwords. After all, the source code of
434                 // Moodle is available, and the user can see the kind of
435                 // modification we are doing and 'undo' it by hand (but only
436                 // if we are using plain text passwords).
437                 //
438                 // Also bear in mind that you need to use a binding user that
439                 // can create accounts and has read/write privileges on the
440                 // 'userPassword' attribute for this to work.
442                 $newuser['userPassword']  = '*'.$extpassword;
443                 $uadd = ldap_add($ldapconnection, $this->config->user_attribute.'='.ldap_addslashes($extusername).','.$this->config->create_context, $newuser);
444                 break;
445             case 'ad':
446                 // User account creation is a two step process with AD. First you
447                 // create the user object, then you set the password. If you try
448                 // to set the password while creating the user, the operation
449                 // fails.
451                 // Passwords in Active Directory must be encoded as Unicode
452                 // strings (UCS-2 Little Endian format) and surrounded with
453                 // double quotes. See http://support.microsoft.com/?kbid=269190
454                 if (!function_exists('mb_convert_encoding')) {
455                     print_error('auth_ldap_no_mbstring', 'auth_ldap');
456                 }
458                 // Check for invalid sAMAccountName characters.
459                 if (preg_match('#[/\\[\]:;|=,+*?<>@"]#', $extusername)) {
460                     print_error ('auth_ldap_ad_invalidchars', 'auth_ldap');
461                 }
463                 // First create the user account, and mark it as disabled.
464                 $newuser['objectClass'] = array('top', 'person', 'user', 'organizationalPerson');
465                 $newuser['sAMAccountName'] = $extusername;
466                 $newuser['userAccountControl'] = AUTH_AD_NORMAL_ACCOUNT |
467                                                  AUTH_AD_ACCOUNTDISABLE;
468                 $userdn = 'cn='.ldap_addslashes($extusername).','.$this->config->create_context;
469                 if (!ldap_add($ldapconnection, $userdn, $newuser)) {
470                     print_error('auth_ldap_ad_create_req', 'auth_ldap');
471                 }
473                 // Now set the password
474                 unset($newuser);
475                 $newuser['unicodePwd'] = mb_convert_encoding('"' . $extpassword . '"',
476                                                              'UCS-2LE', 'UTF-8');
477                 if(!ldap_modify($ldapconnection, $userdn, $newuser)) {
478                     // Something went wrong: delete the user account and error out
479                     ldap_delete ($ldapconnection, $userdn);
480                     print_error('auth_ldap_ad_create_req', 'auth_ldap');
481                 }
482                 $uadd = true;
483                 break;
484             default:
485                print_error('auth_ldap_unsupportedusertype', 'auth_ldap', '', $this->config->user_type_name);
486         }
487         $this->ldap_close();
488         return $uadd;
489     }
491     /**
492      * Returns true if plugin allows resetting of password from moodle.
493      *
494      * @return bool
495      */
496     function can_reset_password() {
497         return !empty($this->config->stdchangepassword);
498     }
500     /**
501      * Returns true if plugin can be manually set.
502      *
503      * @return bool
504      */
505     function can_be_manually_set() {
506         return true;
507     }
509     /**
510      * Returns true if plugin allows signup and user creation.
511      *
512      * @return bool
513      */
514     function can_signup() {
515         return (!empty($this->config->auth_user_create) and !empty($this->config->create_context));
516     }
518     /**
519      * Sign up a new user ready for confirmation.
520      * Password is passed in plaintext.
521      *
522      * @param object $user new user object
523      * @param boolean $notify print notice with link and terminate
524      * @return boolean success
525      */
526     function user_signup($user, $notify=true) {
527         global $CFG, $DB, $PAGE, $OUTPUT;
529         require_once($CFG->dirroot.'/user/profile/lib.php');
530         require_once($CFG->dirroot.'/user/lib.php');
532         if ($this->user_exists($user->username)) {
533             print_error('auth_ldap_user_exists', 'auth_ldap');
534         }
536         $plainslashedpassword = $user->password;
537         unset($user->password);
539         if (! $this->user_create($user, $plainslashedpassword)) {
540             print_error('auth_ldap_create_error', 'auth_ldap');
541         }
543         $user->id = user_create_user($user, false, false);
545         user_add_password_history($user->id, $plainslashedpassword);
547         // Save any custom profile field information
548         profile_save_data($user);
550         $this->update_user_record($user->username);
551         // This will also update the stored hash to the latest algorithm
552         // if the existing hash is using an out-of-date algorithm (or the
553         // legacy md5 algorithm).
554         update_internal_user_password($user, $plainslashedpassword);
556         $user = $DB->get_record('user', array('id'=>$user->id));
558         \core\event\user_created::create_from_userid($user->id)->trigger();
560         if (! send_confirmation_email($user)) {
561             print_error('noemail', 'auth_ldap');
562         }
564         if ($notify) {
565             $emailconfirm = get_string('emailconfirm');
566             $PAGE->set_url('/auth/ldap/auth.php');
567             $PAGE->navbar->add($emailconfirm);
568             $PAGE->set_title($emailconfirm);
569             $PAGE->set_heading($emailconfirm);
570             echo $OUTPUT->header();
571             notice(get_string('emailconfirmsent', '', $user->email), "{$CFG->wwwroot}/index.php");
572         } else {
573             return true;
574         }
575     }
577     /**
578      * Returns true if plugin allows confirming of new users.
579      *
580      * @return bool
581      */
582     function can_confirm() {
583         return $this->can_signup();
584     }
586     /**
587      * Confirm the new user as registered.
588      *
589      * @param string $username
590      * @param string $confirmsecret
591      */
592     function user_confirm($username, $confirmsecret) {
593         global $DB;
595         $user = get_complete_user_data('username', $username);
597         if (!empty($user)) {
598             if ($user->auth != $this->authtype) {
599                 return AUTH_CONFIRM_ERROR;
601             } else if ($user->secret == $confirmsecret && $user->confirmed) {
602                 return AUTH_CONFIRM_ALREADY;
604             } else if ($user->secret == $confirmsecret) {   // They have provided the secret key to get in
605                 if (!$this->user_activate($username)) {
606                     return AUTH_CONFIRM_FAIL;
607                 }
608                 $user->confirmed = 1;
609                 user_update_user($user, false);
610                 return AUTH_CONFIRM_OK;
611             }
612         } else {
613             return AUTH_CONFIRM_ERROR;
614         }
615     }
617     /**
618      * Return number of days to user password expires
619      *
620      * If userpassword does not expire it should return 0. If password is already expired
621      * it should return negative value.
622      *
623      * @param mixed $username username
624      * @return integer
625      */
626     function password_expire($username) {
627         $result = 0;
629         $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
631         $ldapconnection = $this->ldap_connect();
632         $user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
633         $search_attribs = array($this->config->expireattr);
634         $sr = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs);
635         if ($sr)  {
636             $info = ldap_get_entries_moodle($ldapconnection, $sr);
637             if (!empty ($info)) {
638                 $info = $info[0];
639                 if (isset($info[$this->config->expireattr][0])) {
640                     $expiretime = $this->ldap_expirationtime2unix($info[$this->config->expireattr][0], $ldapconnection, $user_dn);
641                     if ($expiretime != 0) {
642                         $now = time();
643                         if ($expiretime > $now) {
644                             $result = ceil(($expiretime - $now) / DAYSECS);
645                         } else {
646                             $result = floor(($expiretime - $now) / DAYSECS);
647                         }
648                     }
649                 }
650             }
651         } else {
652             error_log($this->errorlogtag.get_string('didtfindexpiretime', 'auth_ldap'));
653         }
655         return $result;
656     }
658     /**
659      * Syncronizes user fron external LDAP server to moodle user table
660      *
661      * Sync is now using username attribute.
662      *
663      * Syncing users removes or suspends users that dont exists anymore in external LDAP.
664      * Creates new users and updates coursecreator status of users.
665      *
666      * @param bool $do_updates will do pull in data updates from LDAP if relevant
667      */
668     function sync_users($do_updates=true) {
669         global $CFG, $DB;
671         print_string('connectingldap', 'auth_ldap');
672         $ldapconnection = $this->ldap_connect();
674         $dbman = $DB->get_manager();
676     /// Define table user to be created
677         $table = new xmldb_table('tmp_extuser');
678         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
679         $table->add_field('username', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
680         $table->add_field('mnethostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
681         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
682         $table->add_index('username', XMLDB_INDEX_UNIQUE, array('mnethostid', 'username'));
684         print_string('creatingtemptable', 'auth_ldap', 'tmp_extuser');
685         $dbman->create_temp_table($table);
687         ////
688         //// get user's list from ldap to sql in a scalable fashion
689         ////
690         // prepare some data we'll need
691         $filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')';
693         $contexts = explode(';', $this->config->contexts);
695         if (!empty($this->config->create_context)) {
696             array_push($contexts, $this->config->create_context);
697         }
699         $ldap_pagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
700         $ldap_cookie = '';
701         foreach ($contexts as $context) {
702             $context = trim($context);
703             if (empty($context)) {
704                 continue;
705             }
707             do {
708                 if ($ldap_pagedresults) {
709                     ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
710                 }
711                 if ($this->config->search_sub) {
712                     // Use ldap_search to find first user from subtree.
713                     $ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
714                 } else {
715                     // Search only in this context.
716                     $ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
717                 }
718                 if(!$ldap_result) {
719                     continue;
720                 }
721                 if ($ldap_pagedresults) {
722                     ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
723                 }
724                 if ($entry = @ldap_first_entry($ldapconnection, $ldap_result)) {
725                     do {
726                         $value = ldap_get_values_len($ldapconnection, $entry, $this->config->user_attribute);
727                         $value = core_text::convert($value[0], $this->config->ldapencoding, 'utf-8');
728                         $value = trim($value);
729                         $this->ldap_bulk_insert($value);
730                     } while ($entry = ldap_next_entry($ldapconnection, $entry));
731                 }
732                 unset($ldap_result); // Free mem.
733             } while ($ldap_pagedresults && $ldap_cookie !== null && $ldap_cookie != '');
734         }
736         // If LDAP paged results were used, the current connection must be completely
737         // closed and a new one created, to work without paged results from here on.
738         if ($ldap_pagedresults) {
739             $this->ldap_close(true);
740             $ldapconnection = $this->ldap_connect();
741         }
743         /// preserve our user database
744         /// if the temp table is empty, it probably means that something went wrong, exit
745         /// so as to avoid mass deletion of users; which is hard to undo
746         $count = $DB->count_records_sql('SELECT COUNT(username) AS count, 1 FROM {tmp_extuser}');
747         if ($count < 1) {
748             print_string('didntgetusersfromldap', 'auth_ldap');
749             exit;
750         } else {
751             print_string('gotcountrecordsfromldap', 'auth_ldap', $count);
752         }
755 /// User removal
756         // Find users in DB that aren't in ldap -- to be removed!
757         // this is still not as scalable (but how often do we mass delete?)
759         if ($this->config->removeuser == AUTH_REMOVEUSER_FULLDELETE) {
760             $sql = "SELECT u.*
761                       FROM {user} u
762                  LEFT JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
763                      WHERE u.auth = :auth
764                            AND u.deleted = 0
765                            AND e.username IS NULL";
766             $remove_users = $DB->get_records_sql($sql, array('auth'=>$this->authtype));
768             if (!empty($remove_users)) {
769                 print_string('userentriestoremove', 'auth_ldap', count($remove_users));
770                 foreach ($remove_users as $user) {
771                     if (delete_user($user)) {
772                         echo "\t"; print_string('auth_dbdeleteuser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
773                     } else {
774                         echo "\t"; print_string('auth_dbdeleteusererror', 'auth_db', $user->username); echo "\n";
775                     }
776                 }
777             } else {
778                 print_string('nouserentriestoremove', 'auth_ldap');
779             }
780             unset($remove_users); // Free mem!
782         } else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
783             $sql = "SELECT u.*
784                       FROM {user} u
785                  LEFT JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
786                      WHERE u.auth = :auth
787                            AND u.deleted = 0
788                            AND u.suspended = 0
789                            AND e.username IS NULL";
790             $remove_users = $DB->get_records_sql($sql, array('auth'=>$this->authtype));
792             if (!empty($remove_users)) {
793                 print_string('userentriestoremove', 'auth_ldap', count($remove_users));
795                 foreach ($remove_users as $user) {
796                     $updateuser = new stdClass();
797                     $updateuser->id = $user->id;
798                     $updateuser->suspended = 1;
799                     user_update_user($updateuser, false);
800                     echo "\t"; print_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
801                     \core\session\manager::kill_user_sessions($user->id);
802                 }
803             } else {
804                 print_string('nouserentriestoremove', 'auth_ldap');
805             }
806             unset($remove_users); // Free mem!
807         }
809 /// Revive suspended users
810         if (!empty($this->config->removeuser) and $this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
811             $sql = "SELECT u.id, u.username
812                       FROM {user} u
813                       JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = e.mnethostid)
814                      WHERE (u.auth = 'nologin' OR (u.auth = ? AND u.suspended = 1)) AND u.deleted = 0";
815             // Note: 'nologin' is there for backwards compatibility.
816             $revive_users = $DB->get_records_sql($sql, array($this->authtype));
818             if (!empty($revive_users)) {
819                 print_string('userentriestorevive', 'auth_ldap', count($revive_users));
821                 foreach ($revive_users as $user) {
822                     $updateuser = new stdClass();
823                     $updateuser->id = $user->id;
824                     $updateuser->auth = $this->authtype;
825                     $updateuser->suspended = 0;
826                     user_update_user($updateuser, false);
827                     echo "\t"; print_string('auth_dbreviveduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
828                 }
829             } else {
830                 print_string('nouserentriestorevive', 'auth_ldap');
831             }
833             unset($revive_users);
834         }
837 /// User Updates - time-consuming (optional)
838         if ($do_updates) {
839             // Narrow down what fields we need to update
840             $all_keys = array_keys(get_object_vars($this->config));
841             $updatekeys = array();
842             foreach ($all_keys as $key) {
843                 if (preg_match('/^field_updatelocal_(.+)$/', $key, $match)) {
844                     // If we have a field to update it from
845                     // and it must be updated 'onlogin' we
846                     // update it on cron
847                     if (!empty($this->config->{'field_map_'.$match[1]})
848                          and $this->config->{$match[0]} === 'onlogin') {
849                         array_push($updatekeys, $match[1]); // the actual key name
850                     }
851                 }
852             }
853             if ($this->config->suspended_attribute && $this->config->sync_suspended) {
854                 $updatekeys[] = 'suspended';
855             }
856             unset($all_keys); unset($key);
858         } else {
859             print_string('noupdatestobedone', 'auth_ldap');
860         }
861         if ($do_updates and !empty($updatekeys)) { // run updates only if relevant
862             $users = $DB->get_records_sql('SELECT u.username, u.id
863                                              FROM {user} u
864                                             WHERE u.deleted = 0 AND u.auth = ? AND u.mnethostid = ?',
865                                           array($this->authtype, $CFG->mnet_localhost_id));
866             if (!empty($users)) {
867                 print_string('userentriestoupdate', 'auth_ldap', count($users));
869                 $transaction = $DB->start_delegated_transaction();
870                 $xcount = 0;
871                 $maxxcount = 100;
873                 foreach ($users as $user) {
874                     echo "\t"; print_string('auth_dbupdatinguser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id));
875                     if (!$this->update_user_record($user->username, $updatekeys, true)) {
876                         echo ' - '.get_string('skipped');
877                     }
878                     echo "\n";
879                     $xcount++;
881                     // Update system roles, if needed.
882                     $this->sync_roles($user);
883                 }
884                 $transaction->allow_commit();
885                 unset($users); // free mem
886             }
887         } else { // end do updates
888             print_string('noupdatestobedone', 'auth_ldap');
889         }
891 /// User Additions
892         // Find users missing in DB that are in LDAP
893         // and gives me a nifty object I don't want.
894         // note: we do not care about deleted accounts anymore, this feature was replaced by suspending to nologin auth plugin
895         $sql = 'SELECT e.id, e.username
896                   FROM {tmp_extuser} e
897                   LEFT JOIN {user} u ON (e.username = u.username AND e.mnethostid = u.mnethostid)
898                  WHERE u.id IS NULL';
899         $add_users = $DB->get_records_sql($sql);
901         if (!empty($add_users)) {
902             print_string('userentriestoadd', 'auth_ldap', count($add_users));
904             $transaction = $DB->start_delegated_transaction();
905             foreach ($add_users as $user) {
906                 $user = $this->get_userinfo_asobj($user->username);
908                 // Prep a few params
909                 $user->modified   = time();
910                 $user->confirmed  = 1;
911                 $user->auth       = $this->authtype;
912                 $user->mnethostid = $CFG->mnet_localhost_id;
913                 // get_userinfo_asobj() might have replaced $user->username with the value
914                 // from the LDAP server (which can be mixed-case). Make sure it's lowercase
915                 $user->username = trim(core_text::strtolower($user->username));
916                 // It isn't possible to just rely on the configured suspension attribute since
917                 // things like active directory use bit masks, other things using LDAP might
918                 // do different stuff as well.
919                 //
920                 // The cast to int is a workaround for MDL-53959.
921                 $user->suspended = (int)$this->is_user_suspended($user);
922                 if (empty($user->lang)) {
923                     $user->lang = $CFG->lang;
924                 }
925                 if (empty($user->calendartype)) {
926                     $user->calendartype = $CFG->calendartype;
927                 }
929                 $id = user_create_user($user, false);
930                 echo "\t"; print_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)); echo "\n";
931                 $user = $DB->get_record('user', array('id' => $id));
933                 if (!empty($this->config->forcechangepassword)) {
934                     set_user_preference('auth_forcepasswordchange', 1, $id);
935                 }
937                 // Add roles if needed.
938                 $this->sync_roles($user);
940             }
941             $transaction->allow_commit();
942             unset($add_users); // free mem
943         } else {
944             print_string('nouserstobeadded', 'auth_ldap');
945         }
947         $dbman->drop_table($table);
948         $this->ldap_close();
950         return true;
951     }
953     /**
954      * Update a local user record from an external source.
955      * This is a lighter version of the one in moodlelib -- won't do
956      * expensive ops such as enrolment.
957      *
958      * If you don't pass $updatekeys, there is a performance hit and
959      * values removed from LDAP won't be removed from moodle.
960      *
961      * @param string $username username
962      * @param boolean $updatekeys true to update the local record with the external LDAP values.
963      * @param bool $triggerevent set false if user_updated event should not be triggered.
964      *             This will not affect user_password_updated event triggering.
965      * @return stdClass|bool updated user record or false if there is no new info to update.
966      */
967     function update_user_record($username, $updatekeys = false, $triggerevent = false) {
968         global $CFG, $DB;
970         // Just in case check text case
971         $username = trim(core_text::strtolower($username));
973         // Get the current user record
974         $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id));
975         if (empty($user)) { // trouble
976             error_log($this->errorlogtag.get_string('auth_dbusernotexist', 'auth_db', '', $username));
977             print_error('auth_dbusernotexist', 'auth_db', '', $username);
978             die;
979         }
981         // Protect the userid from being overwritten
982         $userid = $user->id;
984         if ($newinfo = $this->get_userinfo($username)) {
985             $newinfo = truncate_userinfo($newinfo);
987             if (empty($updatekeys)) { // all keys? this does not support removing values
988                 $updatekeys = array_keys($newinfo);
989             }
991             if (!empty($updatekeys)) {
992                 $newuser = new stdClass();
993                 $newuser->id = $userid;
994                 // The cast to int is a workaround for MDL-53959.
995                 $newuser->suspended = (int)$this->is_user_suspended((object) $newinfo);
997                 foreach ($updatekeys as $key) {
998                     if (isset($newinfo[$key])) {
999                         $value = $newinfo[$key];
1000                     } else {
1001                         $value = '';
1002                     }
1004                     if (!empty($this->config->{'field_updatelocal_' . $key})) {
1005                         // Only update if it's changed.
1006                         if ($user->{$key} != $value) {
1007                             $newuser->$key = $value;
1008                         }
1009                     }
1010                 }
1011                 user_update_user($newuser, false, $triggerevent);
1012             }
1013         } else {
1014             return false;
1015         }
1016         return $DB->get_record('user', array('id'=>$userid, 'deleted'=>0));
1017     }
1019     /**
1020      * Bulk insert in SQL's temp table
1021      */
1022     function ldap_bulk_insert($username) {
1023         global $DB, $CFG;
1025         $username = core_text::strtolower($username); // usernames are __always__ lowercase.
1026         $DB->insert_record_raw('tmp_extuser', array('username'=>$username,
1027                                                     'mnethostid'=>$CFG->mnet_localhost_id), false, true);
1028         echo '.';
1029     }
1031     /**
1032      * Activates (enables) user in external LDAP so user can login
1033      *
1034      * @param mixed $username
1035      * @return boolean result
1036      */
1037     function user_activate($username) {
1038         $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
1040         $ldapconnection = $this->ldap_connect();
1042         $userdn = $this->ldap_find_userdn($ldapconnection, $extusername);
1043         switch ($this->config->user_type)  {
1044             case 'edir':
1045                 $newinfo['loginDisabled'] = 'FALSE';
1046                 break;
1047             case 'rfc2307':
1048             case 'rfc2307bis':
1049                 // Remember that we add a '*' character in front of the
1050                 // external password string to 'disable' the account. We just
1051                 // need to remove it.
1052                 $sr = ldap_read($ldapconnection, $userdn, '(objectClass=*)',
1053                                 array('userPassword'));
1054                 $info = ldap_get_entries($ldapconnection, $sr);
1055                 $info[0] = array_change_key_case($info[0], CASE_LOWER);
1056                 $newinfo['userPassword'] = ltrim($info[0]['userpassword'][0], '*');
1057                 break;
1058             case 'ad':
1059                 // We need to unset the ACCOUNTDISABLE bit in the
1060                 // userAccountControl attribute ( see
1061                 // http://support.microsoft.com/kb/305144 )
1062                 $sr = ldap_read($ldapconnection, $userdn, '(objectClass=*)',
1063                                 array('userAccountControl'));
1064                 $info = ldap_get_entries($ldapconnection, $sr);
1065                 $info[0] = array_change_key_case($info[0], CASE_LOWER);
1066                 $newinfo['userAccountControl'] = $info[0]['useraccountcontrol'][0]
1067                                                  & (~AUTH_AD_ACCOUNTDISABLE);
1068                 break;
1069             default:
1070                 print_error('user_activatenotsupportusertype', 'auth_ldap', '', $this->config->user_type_name);
1071         }
1072         $result = ldap_modify($ldapconnection, $userdn, $newinfo);
1073         $this->ldap_close();
1074         return $result;
1075     }
1077     /**
1078      * Returns true if user should be coursecreator.
1079      *
1080      * @param mixed $username    username (without system magic quotes)
1081      * @return mixed result      null if course creators is not configured, boolean otherwise.
1082      *
1083      * @deprecated since Moodle 3.4 MDL-30634 - please do not use this function any more.
1084      */
1085     function iscreator($username) {
1086         debugging('iscreator() is deprecated. Please use auth_plugin_ldap::is_role() instead.', DEBUG_DEVELOPER);
1088         if (empty($this->config->creators) or empty($this->config->memberattribute)) {
1089             return null;
1090         }
1092         $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
1094         $ldapconnection = $this->ldap_connect();
1096         if ($this->config->memberattribute_isdn) {
1097             if(!($userid = $this->ldap_find_userdn($ldapconnection, $extusername))) {
1098                 return false;
1099             }
1100         } else {
1101             $userid = $extusername;
1102         }
1104         $group_dns = explode(';', $this->config->creators);
1105         $creator = ldap_isgroupmember($ldapconnection, $userid, $group_dns, $this->config->memberattribute);
1107         $this->ldap_close();
1109         return $creator;
1110     }
1112     /**
1113      * Check if user has LDAP group membership.
1114      *
1115      * Returns true if user should be assigned role.
1116      *
1117      * @param mixed $username username (without system magic quotes).
1118      * @param array $role Array of role's shortname, localname, and settingname for the config value.
1119      * @return mixed result null if role/LDAP context is not configured, boolean otherwise.
1120      */
1121     private function is_role($username, $role) {
1122         if (empty($this->config->{$role['settingname']}) or empty($this->config->memberattribute)) {
1123             return null;
1124         }
1126         $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
1128         $ldapconnection = $this->ldap_connect();
1130         if ($this->config->memberattribute_isdn) {
1131             if (!($userid = $this->ldap_find_userdn($ldapconnection, $extusername))) {
1132                 return false;
1133             }
1134         } else {
1135             $userid = $extusername;
1136         }
1138         $groupdns = explode(';', $this->config->{$role['settingname']});
1139         $isrole = ldap_isgroupmember($ldapconnection, $userid, $groupdns, $this->config->memberattribute);
1141         $this->ldap_close();
1143         return $isrole;
1144     }
1146     /**
1147      * Called when the user record is updated.
1148      *
1149      * Modifies user in external LDAP server. It takes olduser (before
1150      * changes) and newuser (after changes) compares information and
1151      * saves modified information to external LDAP server.
1152      *
1153      * @param mixed $olduser     Userobject before modifications    (without system magic quotes)
1154      * @param mixed $newuser     Userobject new modified userobject (without system magic quotes)
1155      * @return boolean result
1156      *
1157      */
1158     function user_update($olduser, $newuser) {
1159         global $USER;
1161         if (isset($olduser->username) and isset($newuser->username) and $olduser->username != $newuser->username) {
1162             error_log($this->errorlogtag.get_string('renamingnotallowed', 'auth_ldap'));
1163             return false;
1164         }
1166         if (isset($olduser->auth) and $olduser->auth != $this->authtype) {
1167             return true; // just change auth and skip update
1168         }
1170         $attrmap = $this->ldap_attributes();
1171         // Before doing anything else, make sure we really need to update anything
1172         // in the external LDAP server.
1173         $update_external = false;
1174         foreach ($attrmap as $key => $ldapkeys) {
1175             if (!empty($this->config->{'field_updateremote_'.$key})) {
1176                 $update_external = true;
1177                 break;
1178             }
1179         }
1180         if (!$update_external) {
1181             return true;
1182         }
1184         $extoldusername = core_text::convert($olduser->username, 'utf-8', $this->config->ldapencoding);
1186         $ldapconnection = $this->ldap_connect();
1188         $search_attribs = array();
1189         foreach ($attrmap as $key => $values) {
1190             if (!is_array($values)) {
1191                 $values = array($values);
1192             }
1193             foreach ($values as $value) {
1194                 if (!in_array($value, $search_attribs)) {
1195                     array_push($search_attribs, $value);
1196                 }
1197             }
1198         }
1200         if(!($user_dn = $this->ldap_find_userdn($ldapconnection, $extoldusername))) {
1201             return false;
1202         }
1204         $success = true;
1205         $user_info_result = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs);
1206         if ($user_info_result) {
1207             $user_entry = ldap_get_entries_moodle($ldapconnection, $user_info_result);
1208             if (empty($user_entry)) {
1209                 $attribs = join (', ', $search_attribs);
1210                 error_log($this->errorlogtag.get_string('updateusernotfound', 'auth_ldap',
1211                                                           array('userdn'=>$user_dn,
1212                                                                 'attribs'=>$attribs)));
1213                 return false; // old user not found!
1214             } else if (count($user_entry) > 1) {
1215                 error_log($this->errorlogtag.get_string('morethanoneuser', 'auth_ldap'));
1216                 return false;
1217             }
1219             $user_entry = $user_entry[0];
1221             foreach ($attrmap as $key => $ldapkeys) {
1222                 $profilefield = '';
1223                 // Only process if the moodle field ($key) has changed and we
1224                 // are set to update LDAP with it
1225                 $customprofilefield = 'profile_field_' . $key;
1226                 if (isset($olduser->$key) and isset($newuser->$key)
1227                     and ($olduser->$key !== $newuser->$key)) {
1228                     $profilefield = $key;
1229                 } else if (isset($olduser->$customprofilefield) && isset($newuser->$customprofilefield)
1230                     && $olduser->$customprofilefield !== $newuser->$customprofilefield) {
1231                     $profilefield = $customprofilefield;
1232                 }
1234                 if (!empty($profilefield) && !empty($this->config->{'field_updateremote_' . $key})) {
1235                     // For ldap values that could be in more than one
1236                     // ldap key, we will do our best to match
1237                     // where they came from
1238                     $ambiguous = true;
1239                     $changed   = false;
1240                     if (!is_array($ldapkeys)) {
1241                         $ldapkeys = array($ldapkeys);
1242                     }
1243                     if (count($ldapkeys) < 2) {
1244                         $ambiguous = false;
1245                     }
1247                     $nuvalue = core_text::convert($newuser->$profilefield, 'utf-8', $this->config->ldapencoding);
1248                     empty($nuvalue) ? $nuvalue = array() : $nuvalue;
1249                     $ouvalue = core_text::convert($olduser->$profilefield, 'utf-8', $this->config->ldapencoding);
1251                     foreach ($ldapkeys as $ldapkey) {
1252                         $ldapkey   = $ldapkey;
1253                         $ldapvalue = $user_entry[$ldapkey][0];
1254                         if (!$ambiguous) {
1255                             // Skip update if the values already match
1256                             if ($nuvalue !== $ldapvalue) {
1257                                 // This might fail due to schema validation
1258                                 if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
1259                                     $changed = true;
1260                                     continue;
1261                                 } else {
1262                                     $success = false;
1263                                     error_log($this->errorlogtag.get_string ('updateremfail', 'auth_ldap',
1264                                                                              array('errno'=>ldap_errno($ldapconnection),
1265                                                                                    'errstring'=>ldap_err2str(ldap_errno($ldapconnection)),
1266                                                                                    'key'=>$key,
1267                                                                                    'ouvalue'=>$ouvalue,
1268                                                                                    'nuvalue'=>$nuvalue)));
1269                                     continue;
1270                                 }
1271                             }
1272                         } else {
1273                             // Ambiguous. Value empty before in Moodle (and LDAP) - use
1274                             // 1st ldap candidate field, no need to guess
1275                             if ($ouvalue === '') { // value empty before - use 1st ldap candidate
1276                                 // This might fail due to schema validation
1277                                 if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
1278                                     $changed = true;
1279                                     continue;
1280                                 } else {
1281                                     $success = false;
1282                                     error_log($this->errorlogtag.get_string ('updateremfail', 'auth_ldap',
1283                                                                              array('errno'=>ldap_errno($ldapconnection),
1284                                                                                    'errstring'=>ldap_err2str(ldap_errno($ldapconnection)),
1285                                                                                    'key'=>$key,
1286                                                                                    'ouvalue'=>$ouvalue,
1287                                                                                    'nuvalue'=>$nuvalue)));
1288                                     continue;
1289                                 }
1290                             }
1292                             // We found which ldap key to update!
1293                             if ($ouvalue !== '' and $ouvalue === $ldapvalue ) {
1294                                 // This might fail due to schema validation
1295                                 if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
1296                                     $changed = true;
1297                                     continue;
1298                                 } else {
1299                                     $success = false;
1300                                     error_log($this->errorlogtag.get_string ('updateremfail', 'auth_ldap',
1301                                                                              array('errno'=>ldap_errno($ldapconnection),
1302                                                                                    'errstring'=>ldap_err2str(ldap_errno($ldapconnection)),
1303                                                                                    'key'=>$key,
1304                                                                                    'ouvalue'=>$ouvalue,
1305                                                                                    'nuvalue'=>$nuvalue)));
1306                                     continue;
1307                                 }
1308                             }
1309                         }
1310                     }
1312                     if ($ambiguous and !$changed) {
1313                         $success = false;
1314                         error_log($this->errorlogtag.get_string ('updateremfailamb', 'auth_ldap',
1315                                                                  array('key'=>$key,
1316                                                                        'ouvalue'=>$ouvalue,
1317                                                                        'nuvalue'=>$nuvalue)));
1318                     }
1319                 }
1320             }
1321         } else {
1322             error_log($this->errorlogtag.get_string ('usernotfound', 'auth_ldap'));
1323             $success = false;
1324         }
1326         $this->ldap_close();
1327         return $success;
1329     }
1331     /**
1332      * Changes userpassword in LDAP
1333      *
1334      * Called when the user password is updated. It assumes it is
1335      * called by an admin or that you've otherwise checked the user's
1336      * credentials
1337      *
1338      * @param  object  $user        User table object
1339      * @param  string  $newpassword Plaintext password (not crypted/md5'ed)
1340      * @return boolean result
1341      *
1342      */
1343     function user_update_password($user, $newpassword) {
1344         global $USER;
1346         $result = false;
1347         $username = $user->username;
1349         $extusername = core_text::convert($username, 'utf-8', $this->config->ldapencoding);
1350         $extpassword = core_text::convert($newpassword, 'utf-8', $this->config->ldapencoding);
1352         switch ($this->config->passtype) {
1353             case 'md5':
1354                 $extpassword = '{MD5}' . base64_encode(pack('H*', md5($extpassword)));
1355                 break;
1356             case 'sha1':
1357                 $extpassword = '{SHA}' . base64_encode(pack('H*', sha1($extpassword)));
1358                 break;
1359             case 'plaintext':
1360             default:
1361                 break; // plaintext
1362         }
1364         $ldapconnection = $this->ldap_connect();
1366         $user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
1368         if (!$user_dn) {
1369             error_log($this->errorlogtag.get_string ('nodnforusername', 'auth_ldap', $user->username));
1370             return false;
1371         }
1373         switch ($this->config->user_type) {
1374             case 'edir':
1375                 // Change password
1376                 $result = ldap_modify($ldapconnection, $user_dn, array('userPassword' => $extpassword));
1377                 if (!$result) {
1378                     error_log($this->errorlogtag.get_string ('updatepasserror', 'auth_ldap',
1379                                                                array('errno'=>ldap_errno($ldapconnection),
1380                                                                      'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1381                 }
1382                 // Update password expiration time, grace logins count
1383                 $search_attribs = array($this->config->expireattr, 'passwordExpirationInterval', 'loginGraceLimit');
1384                 $sr = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs);
1385                 if ($sr) {
1386                     $entry = ldap_get_entries_moodle($ldapconnection, $sr);
1387                     $info = $entry[0];
1388                     $newattrs = array();
1389                     if (!empty($info[$this->config->expireattr][0])) {
1390                         // Set expiration time only if passwordExpirationInterval is defined
1391                         if (!empty($info['passwordexpirationinterval'][0])) {
1392                            $expirationtime = time() + $info['passwordexpirationinterval'][0];
1393                            $ldapexpirationtime = $this->ldap_unix2expirationtime($expirationtime);
1394                            $newattrs['passwordExpirationTime'] = $ldapexpirationtime;
1395                         }
1397                         // Set gracelogin count
1398                         if (!empty($info['logingracelimit'][0])) {
1399                            $newattrs['loginGraceRemaining']= $info['logingracelimit'][0];
1400                         }
1402                         // Store attribute changes in LDAP
1403                         $result = ldap_modify($ldapconnection, $user_dn, $newattrs);
1404                         if (!$result) {
1405                             error_log($this->errorlogtag.get_string ('updatepasserrorexpiregrace', 'auth_ldap',
1406                                                                        array('errno'=>ldap_errno($ldapconnection),
1407                                                                              'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1408                         }
1409                     }
1410                 }
1411                 else {
1412                     error_log($this->errorlogtag.get_string ('updatepasserrorexpire', 'auth_ldap',
1413                                                              array('errno'=>ldap_errno($ldapconnection),
1414                                                                    'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1415                 }
1416                 break;
1418             case 'ad':
1419                 // Passwords in Active Directory must be encoded as Unicode
1420                 // strings (UCS-2 Little Endian format) and surrounded with
1421                 // double quotes. See http://support.microsoft.com/?kbid=269190
1422                 if (!function_exists('mb_convert_encoding')) {
1423                     error_log($this->errorlogtag.get_string ('needmbstring', 'auth_ldap'));
1424                     return false;
1425                 }
1426                 $extpassword = mb_convert_encoding('"'.$extpassword.'"', "UCS-2LE", $this->config->ldapencoding);
1427                 $result = ldap_modify($ldapconnection, $user_dn, array('unicodePwd' => $extpassword));
1428                 if (!$result) {
1429                     error_log($this->errorlogtag.get_string ('updatepasserror', 'auth_ldap',
1430                                                              array('errno'=>ldap_errno($ldapconnection),
1431                                                                    'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1432                 }
1433                 break;
1435             default:
1436                 // Send LDAP the password in cleartext, it will md5 it itself
1437                 $result = ldap_modify($ldapconnection, $user_dn, array('userPassword' => $extpassword));
1438                 if (!$result) {
1439                     error_log($this->errorlogtag.get_string ('updatepasserror', 'auth_ldap',
1440                                                              array('errno'=>ldap_errno($ldapconnection),
1441                                                                    'errstring'=>ldap_err2str(ldap_errno($ldapconnection)))));
1442                 }
1444         }
1446         $this->ldap_close();
1447         return $result;
1448     }
1450     /**
1451      * Take expirationtime and return it as unix timestamp in seconds
1452      *
1453      * Takes expiration timestamp as read from LDAP and returns it as unix timestamp in seconds
1454      * Depends on $this->config->user_type variable
1455      *
1456      * @param mixed time   Time stamp read from LDAP as it is.
1457      * @param string $ldapconnection Only needed for Active Directory.
1458      * @param string $user_dn User distinguished name for the user we are checking password expiration (only needed for Active Directory).
1459      * @return timestamp
1460      */
1461     function ldap_expirationtime2unix ($time, $ldapconnection, $user_dn) {
1462         $result = false;
1463         switch ($this->config->user_type) {
1464             case 'edir':
1465                 $yr=substr($time, 0, 4);
1466                 $mo=substr($time, 4, 2);
1467                 $dt=substr($time, 6, 2);
1468                 $hr=substr($time, 8, 2);
1469                 $min=substr($time, 10, 2);
1470                 $sec=substr($time, 12, 2);
1471                 $result = mktime($hr, $min, $sec, $mo, $dt, $yr);
1472                 break;
1473             case 'rfc2307':
1474             case 'rfc2307bis':
1475                 $result = $time * DAYSECS; // The shadowExpire contains the number of DAYS between 01/01/1970 and the actual expiration date
1476                 break;
1477             case 'ad':
1478                 $result = $this->ldap_get_ad_pwdexpire($time, $ldapconnection, $user_dn);
1479                 break;
1480             default:
1481                 print_error('auth_ldap_usertypeundefined', 'auth_ldap');
1482         }
1483         return $result;
1484     }
1486     /**
1487      * Takes unix timestamp and returns it formated for storing in LDAP
1488      *
1489      * @param integer unix time stamp
1490      */
1491     function ldap_unix2expirationtime($time) {
1492         $result = false;
1493         switch ($this->config->user_type) {
1494             case 'edir':
1495                 $result=date('YmdHis', $time).'Z';
1496                 break;
1497             case 'rfc2307':
1498             case 'rfc2307bis':
1499                 $result = $time ; // Already in correct format
1500                 break;
1501             default:
1502                 print_error('auth_ldap_usertypeundefined2', 'auth_ldap');
1503         }
1504         return $result;
1506     }
1508     /**
1509      * Returns user attribute mappings between moodle and LDAP
1510      *
1511      * @return array
1512      */
1514     function ldap_attributes () {
1515         $moodleattributes = array();
1516         // If we have custom fields then merge them with user fields.
1517         $customfields = $this->get_custom_user_profile_fields();
1518         if (!empty($customfields) && !empty($this->userfields)) {
1519             $userfields = array_merge($this->userfields, $customfields);
1520         } else {
1521             $userfields = $this->userfields;
1522         }
1524         foreach ($userfields as $field) {
1525             if (!empty($this->config->{"field_map_$field"})) {
1526                 $moodleattributes[$field] = core_text::strtolower(trim($this->config->{"field_map_$field"}));
1527                 if (preg_match('/,/', $moodleattributes[$field])) {
1528                     $moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ?
1529                 }
1530             }
1531         }
1532         $moodleattributes['username'] = core_text::strtolower(trim($this->config->user_attribute));
1533         $moodleattributes['suspended'] = core_text::strtolower(trim($this->config->suspended_attribute));
1534         return $moodleattributes;
1535     }
1537     /**
1538      * Returns all usernames from LDAP
1539      *
1540      * @param $filter An LDAP search filter to select desired users
1541      * @return array of LDAP user names converted to UTF-8
1542      */
1543     function ldap_get_userlist($filter='*') {
1544         $fresult = array();
1546         $ldapconnection = $this->ldap_connect();
1548         if ($filter == '*') {
1549            $filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')';
1550         }
1552         $contexts = explode(';', $this->config->contexts);
1553         if (!empty($this->config->create_context)) {
1554             array_push($contexts, $this->config->create_context);
1555         }
1557         $ldap_cookie = '';
1558         $ldap_pagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
1559         foreach ($contexts as $context) {
1560             $context = trim($context);
1561             if (empty($context)) {
1562                 continue;
1563             }
1565             do {
1566                 if ($ldap_pagedresults) {
1567                     ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
1568                 }
1569                 if ($this->config->search_sub) {
1570                     // Use ldap_search to find first user from subtree.
1571                     $ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
1572                 } else {
1573                     // Search only in this context.
1574                     $ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
1575                 }
1576                 if(!$ldap_result) {
1577                     continue;
1578                 }
1579                 if ($ldap_pagedresults) {
1580                     ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
1581                 }
1582                 $users = ldap_get_entries_moodle($ldapconnection, $ldap_result);
1583                 // Add found users to list.
1584                 for ($i = 0; $i < count($users); $i++) {
1585                     $extuser = core_text::convert($users[$i][$this->config->user_attribute][0],
1586                                                 $this->config->ldapencoding, 'utf-8');
1587                     array_push($fresult, $extuser);
1588                 }
1589                 unset($ldap_result); // Free mem.
1590             } while ($ldap_pagedresults && !empty($ldap_cookie));
1591         }
1593         // If paged results were used, make sure the current connection is completely closed
1594         $this->ldap_close($ldap_pagedresults);
1595         return $fresult;
1596     }
1598     /**
1599      * Indicates if password hashes should be stored in local moodle database.
1600      *
1601      * @return bool true means flag 'not_cached' stored instead of password hash
1602      */
1603     function prevent_local_passwords() {
1604         return !empty($this->config->preventpassindb);
1605     }
1607     /**
1608      * Returns true if this authentication plugin is 'internal'.
1609      *
1610      * @return bool
1611      */
1612     function is_internal() {
1613         return false;
1614     }
1616     /**
1617      * Returns true if this authentication plugin can change the user's
1618      * password.
1619      *
1620      * @return bool
1621      */
1622     function can_change_password() {
1623         return !empty($this->config->stdchangepassword) or !empty($this->config->changepasswordurl);
1624     }
1626     /**
1627      * Returns the URL for changing the user's password, or empty if the default can
1628      * be used.
1629      *
1630      * @return moodle_url
1631      */
1632     function change_password_url() {
1633         if (empty($this->config->stdchangepassword)) {
1634             if (!empty($this->config->changepasswordurl)) {
1635                 return new moodle_url($this->config->changepasswordurl);
1636             } else {
1637                 return null;
1638             }
1639         } else {
1640             return null;
1641         }
1642     }
1644     /**
1645      * Will get called before the login page is shownr. Ff NTLM SSO
1646      * is enabled, and the user is in the right network, we'll redirect
1647      * to the magic NTLM page for SSO...
1648      *
1649      */
1650     function loginpage_hook() {
1651         global $CFG, $SESSION;
1653         // HTTPS is potentially required
1654         //httpsrequired(); - this must be used before setting the URL, it is already done on the login/index.php
1656         if (($_SERVER['REQUEST_METHOD'] === 'GET'         // Only on initial GET of loginpage
1657              || ($_SERVER['REQUEST_METHOD'] === 'POST'
1658                  && (get_local_referer() != strip_querystring(qualified_me()))))
1659                                                           // Or when POSTed from another place
1660                                                           // See MDL-14071
1661             && !empty($this->config->ntlmsso_enabled)     // SSO enabled
1662             && !empty($this->config->ntlmsso_subnet)      // have a subnet to test for
1663             && empty($_GET['authldap_skipntlmsso'])       // haven't failed it yet
1664             && (isguestuser() || !isloggedin())           // guestuser or not-logged-in users
1665             && address_in_subnet(getremoteaddr(), $this->config->ntlmsso_subnet)) {
1667             // First, let's remember where we were trying to get to before we got here
1668             if (empty($SESSION->wantsurl)) {
1669                 $SESSION->wantsurl = null;
1670                 $referer = get_local_referer(false);
1671                 if ($referer &&
1672                         $referer != $CFG->wwwroot &&
1673                         $referer != $CFG->wwwroot . '/' &&
1674                         $referer != $CFG->httpswwwroot . '/login/' &&
1675                         $referer != $CFG->httpswwwroot . '/login/index.php') {
1676                     $SESSION->wantsurl = $referer;
1677                 }
1678             }
1680             // Now start the whole NTLM machinery.
1681             if($this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESATTEMPT ||
1682                 $this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESFORM) {
1683                 if (core_useragent::is_ie()) {
1684                     $sesskey = sesskey();
1685                     redirect($CFG->wwwroot.'/auth/ldap/ntlmsso_magic.php?sesskey='.$sesskey);
1686                 } else if ($this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESFORM) {
1687                     redirect($CFG->httpswwwroot.'/login/index.php?authldap_skipntlmsso=1');
1688                 }
1689             }
1690             redirect($CFG->wwwroot.'/auth/ldap/ntlmsso_attempt.php');
1691         }
1693         // No NTLM SSO, Use the normal login page instead.
1695         // If $SESSION->wantsurl is empty and we have a 'Referer:' header, the login
1696         // page insists on redirecting us to that page after user validation. If
1697         // we clicked on the redirect link at the ntlmsso_finish.php page (instead
1698         // of waiting for the redirection to happen) then we have a 'Referer:' header
1699         // we don't want to use at all. As we can't get rid of it, just point
1700         // $SESSION->wantsurl to $CFG->wwwroot (after all, we came from there).
1701         if (empty($SESSION->wantsurl)
1702             && (get_local_referer() == $CFG->httpswwwroot.'/auth/ldap/ntlmsso_finish.php')) {
1704             $SESSION->wantsurl = $CFG->wwwroot;
1705         }
1706     }
1708     /**
1709      * To be called from a page running under NTLM's
1710      * "Integrated Windows Authentication".
1711      *
1712      * If successful, it will set a special "cookie" (not an HTTP cookie!)
1713      * in cache_flags under the $this->pluginconfig/ntlmsess "plugin" and return true.
1714      * The "cookie" will be picked up by ntlmsso_finish() to complete the
1715      * process.
1716      *
1717      * On failure it will return false for the caller to display an appropriate
1718      * error message (probably saying that Integrated Windows Auth isn't enabled!)
1719      *
1720      * NOTE that this code will execute under the OS user credentials,
1721      * so we MUST avoid dealing with files -- such as session files.
1722      * (The caller should define('NO_MOODLE_COOKIES', true) before including config.php)
1723      *
1724      */
1725     function ntlmsso_magic($sesskey) {
1726         if (isset($_SERVER['REMOTE_USER']) && !empty($_SERVER['REMOTE_USER'])) {
1728             // HTTP __headers__ seem to be sent in ISO-8859-1 encoding
1729             // (according to my reading of RFC-1945, RFC-2616 and RFC-2617 and
1730             // my local tests), so we need to convert the REMOTE_USER value
1731             // (i.e., what we got from the HTTP WWW-Authenticate header) into UTF-8
1732             $username = core_text::convert($_SERVER['REMOTE_USER'], 'iso-8859-1', 'utf-8');
1734             switch ($this->config->ntlmsso_type) {
1735                 case 'ntlm':
1736                     // The format is now configurable, so try to extract the username
1737                     $username = $this->get_ntlm_remote_user($username);
1738                     if (empty($username)) {
1739                         return false;
1740                     }
1741                     break;
1742                 case 'kerberos':
1743                     // Format is username@DOMAIN
1744                     $username = substr($username, 0, strpos($username, '@'));
1745                     break;
1746                 default:
1747                     error_log($this->errorlogtag.get_string ('ntlmsso_unknowntype', 'auth_ldap'));
1748                     return false; // Should never happen!
1749             }
1751             $username = core_text::strtolower($username); // Compatibility hack
1752             set_cache_flag($this->pluginconfig.'/ntlmsess', $sesskey, $username, AUTH_NTLMTIMEOUT);
1753             return true;
1754         }
1755         return false;
1756     }
1758     /**
1759      * Find the session set by ntlmsso_magic(), validate it and
1760      * call authenticate_user_login() to authenticate the user through
1761      * the auth machinery.
1762      *
1763      * It is complemented by a similar check in user_login().
1764      *
1765      * If it succeeds, it never returns.
1766      *
1767      */
1768     function ntlmsso_finish() {
1769         global $CFG, $USER, $SESSION;
1771         $key = sesskey();
1772         $cf = get_cache_flags($this->pluginconfig.'/ntlmsess');
1773         if (!isset($cf[$key]) || $cf[$key] === '') {
1774             return false;
1775         }
1776         $username   = $cf[$key];
1778         // Here we want to trigger the whole authentication machinery
1779         // to make sure no step is bypassed...
1780         $user = authenticate_user_login($username, $key);
1781         if ($user) {
1782             complete_user_login($user);
1784             // Cleanup the key to prevent reuse...
1785             // and to allow re-logins with normal credentials
1786             unset_cache_flag($this->pluginconfig.'/ntlmsess', $key);
1788             // Redirection
1789             if (user_not_fully_set_up($USER, true)) {
1790                 $urltogo = $CFG->wwwroot.'/user/edit.php';
1791                 // We don't delete $SESSION->wantsurl yet, so we get there later
1792             } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
1793                 $urltogo = $SESSION->wantsurl;    // Because it's an address in this site
1794                 unset($SESSION->wantsurl);
1795             } else {
1796                 // No wantsurl stored or external - go to homepage
1797                 $urltogo = $CFG->wwwroot.'/';
1798                 unset($SESSION->wantsurl);
1799             }
1800             // We do not want to redirect if we are in a PHPUnit test.
1801             if (!PHPUNIT_TEST) {
1802                 redirect($urltogo);
1803             }
1804         }
1805         // Should never reach here.
1806         return false;
1807     }
1809     /**
1810      * Sync roles for this user.
1811      *
1812      * @param object $user The user to sync (without system magic quotes).
1813      */
1814     function sync_roles($user) {
1815         global $DB;
1817         $roles = get_ldap_assignable_role_names(2); // Admin user.
1819         foreach ($roles as $role) {
1820             $isrole = $this->is_role($user->username, $role);
1821             if ($isrole === null) {
1822                 continue; // Nothing to sync - role/LDAP contexts not configured.
1823             }
1825             // Sync user.
1826             $systemcontext = context_system::instance();
1827             if ($isrole) {
1828                 // Following calls will not create duplicates.
1829                 role_assign($role['id'], $user->id, $systemcontext->id, $this->roleauth);
1830             } else {
1831                 // Unassign only if previously assigned by this plugin.
1832                 role_unassign($role['id'], $user->id, $systemcontext->id, $this->roleauth);
1833             }
1834         }
1835     }
1837     /**
1838      * Get password expiration time for a given user from Active Directory
1839      *
1840      * @param string $pwdlastset The time last time we changed the password.
1841      * @param resource $lcapconn The open LDAP connection.
1842      * @param string $user_dn The distinguished name of the user we are checking.
1843      *
1844      * @return string $unixtime
1845      */
1846     function ldap_get_ad_pwdexpire($pwdlastset, $ldapconn, $user_dn){
1847         global $CFG;
1849         if (!function_exists('bcsub')) {
1850             error_log($this->errorlogtag.get_string ('needbcmath', 'auth_ldap'));
1851             return 0;
1852         }
1854         // If UF_DONT_EXPIRE_PASSWD flag is set in user's
1855         // userAccountControl attribute, the password doesn't expire.
1856         $sr = ldap_read($ldapconn, $user_dn, '(objectClass=*)',
1857                         array('userAccountControl'));
1858         if (!$sr) {
1859             error_log($this->errorlogtag.get_string ('useracctctrlerror', 'auth_ldap', $user_dn));
1860             // Don't expire password, as we are not sure if it has to be
1861             // expired or not.
1862             return 0;
1863         }
1865         $entry = ldap_get_entries_moodle($ldapconn, $sr);
1866         $info = $entry[0];
1867         $useraccountcontrol = $info['useraccountcontrol'][0];
1868         if ($useraccountcontrol & UF_DONT_EXPIRE_PASSWD) {
1869             // Password doesn't expire.
1870             return 0;
1871         }
1873         // If pwdLastSet is zero, the user must change his/her password now
1874         // (unless UF_DONT_EXPIRE_PASSWD flag is set, but we already
1875         // tested this above)
1876         if ($pwdlastset === '0') {
1877             // Password has expired
1878             return -1;
1879         }
1881         // ----------------------------------------------------------------
1882         // Password expiration time in Active Directory is the composition of
1883         // two values:
1884         //
1885         //   - User's pwdLastSet attribute, that stores the last time
1886         //     the password was changed.
1887         //
1888         //   - Domain's maxPwdAge attribute, that sets how long
1889         //     passwords last in this domain.
1890         //
1891         // We already have the first value (passed in as a parameter). We
1892         // need to get the second one. As we don't know the domain DN, we
1893         // have to query rootDSE's defaultNamingContext attribute to get
1894         // it. Then we have to query that DN's maxPwdAge attribute to get
1895         // the real value.
1896         //
1897         // Once we have both values, we just need to combine them. But MS
1898         // chose to use a different base and unit for time measurements.
1899         // So we need to convert the values to Unix timestamps (see
1900         // details below).
1901         // ----------------------------------------------------------------
1903         $sr = ldap_read($ldapconn, ROOTDSE, '(objectClass=*)',
1904                         array('defaultNamingContext'));
1905         if (!$sr) {
1906             error_log($this->errorlogtag.get_string ('rootdseerror', 'auth_ldap'));
1907             return 0;
1908         }
1910         $entry = ldap_get_entries_moodle($ldapconn, $sr);
1911         $info = $entry[0];
1912         $domaindn = $info['defaultnamingcontext'][0];
1914         $sr = ldap_read ($ldapconn, $domaindn, '(objectClass=*)',
1915                          array('maxPwdAge'));
1916         $entry = ldap_get_entries_moodle($ldapconn, $sr);
1917         $info = $entry[0];
1918         $maxpwdage = $info['maxpwdage'][0];
1919         if ($sr = ldap_read($ldapconn, $user_dn, '(objectClass=*)', array('msDS-ResultantPSO'))) {
1920             if ($entry = ldap_get_entries_moodle($ldapconn, $sr)) {
1921                 $info = $entry[0];
1922                 $userpso = $info['msds-resultantpso'][0];
1924                 // If a PSO exists, FGPP is being utilized.
1925                 // Grab the new maxpwdage from the msDS-MaximumPasswordAge attribute of the PSO.
1926                 if (!empty($userpso)) {
1927                     $sr = ldap_read($ldapconn, $userpso, '(objectClass=*)', array('msDS-MaximumPasswordAge'));
1928                     if ($entry = ldap_get_entries_moodle($ldapconn, $sr)) {
1929                         $info = $entry[0];
1930                         // Default value of msds-maximumpasswordage is 42 and is always set.
1931                         $maxpwdage = $info['msds-maximumpasswordage'][0];
1932                     }
1933                 }
1934             }
1935         }
1936         // ----------------------------------------------------------------
1937         // MSDN says that "pwdLastSet contains the number of 100 nanosecond
1938         // intervals since January 1, 1601 (UTC), stored in a 64 bit integer".
1939         //
1940         // According to Perl's Date::Manip, the number of seconds between
1941         // this date and Unix epoch is 11644473600. So we have to
1942         // substract this value to calculate a Unix time, once we have
1943         // scaled pwdLastSet to seconds. This is the script used to
1944         // calculate the value shown above:
1945         //
1946         //    #!/usr/bin/perl -w
1947         //
1948         //    use Date::Manip;
1949         //
1950         //    $date1 = ParseDate ("160101010000 UTC");
1951         //    $date2 = ParseDate ("197001010000 UTC");
1952         //    $delta = DateCalc($date1, $date2, \$err);
1953         //    $secs = Delta_Format($delta, 0, "%st");
1954         //    print "$secs \n";
1955         //
1956         // MSDN also says that "maxPwdAge is stored as a large integer that
1957         // represents the number of 100 nanosecond intervals from the time
1958         // the password was set before the password expires." We also need
1959         // to scale this to seconds. Bear in mind that this value is stored
1960         // as a _negative_ quantity (at least in my AD domain).
1961         //
1962         // As a last remark, if the low 32 bits of maxPwdAge are equal to 0,
1963         // the maximum password age in the domain is set to 0, which means
1964         // passwords do not expire (see
1965         // http://msdn2.microsoft.com/en-us/library/ms974598.aspx)
1966         //
1967         // As the quantities involved are too big for PHP integers, we
1968         // need to use BCMath functions to work with arbitrary precision
1969         // numbers.
1970         // ----------------------------------------------------------------
1972         // If the low order 32 bits are 0, then passwords do not expire in
1973         // the domain. Just do '$maxpwdage mod 2^32' and check the result
1974         // (2^32 = 4294967296)
1975         if (bcmod ($maxpwdage, 4294967296) === '0') {
1976             return 0;
1977         }
1979         // Add up pwdLastSet and maxPwdAge to get password expiration
1980         // time, in MS time units. Remember maxPwdAge is stored as a
1981         // _negative_ quantity, so we need to substract it in fact.
1982         $pwdexpire = bcsub ($pwdlastset, $maxpwdage);
1984         // Scale the result to convert it to Unix time units and return
1985         // that value.
1986         return bcsub( bcdiv($pwdexpire, '10000000'), '11644473600');
1987     }
1989     /**
1990      * Connect to the LDAP server, using the plugin configured
1991      * settings. It's actually a wrapper around ldap_connect_moodle()
1992      *
1993      * @return resource A valid LDAP connection (or dies if it can't connect)
1994      */
1995     function ldap_connect() {
1996         // Cache ldap connections. They are expensive to set up
1997         // and can drain the TCP/IP ressources on the server if we
1998         // are syncing a lot of users (as we try to open a new connection
1999         // to get the user details). This is the least invasive way
2000         // to reuse existing connections without greater code surgery.
2001         if(!empty($this->ldapconnection)) {
2002             $this->ldapconns++;
2003             return $this->ldapconnection;
2004         }
2006         if($ldapconnection = ldap_connect_moodle($this->config->host_url, $this->config->ldap_version,
2007                                                  $this->config->user_type, $this->config->bind_dn,
2008                                                  $this->config->bind_pw, $this->config->opt_deref,
2009                                                  $debuginfo, $this->config->start_tls)) {
2010             $this->ldapconns = 1;
2011             $this->ldapconnection = $ldapconnection;
2012             return $ldapconnection;
2013         }
2015         print_error('auth_ldap_noconnect_all', 'auth_ldap', '', $debuginfo);
2016     }
2018     /**
2019      * Disconnects from a LDAP server
2020      *
2021      * @param force boolean Forces closing the real connection to the LDAP server, ignoring any
2022      *                      cached connections. This is needed when we've used paged results
2023      *                      and want to use normal results again.
2024      */
2025     function ldap_close($force=false) {
2026         $this->ldapconns--;
2027         if (($this->ldapconns == 0) || ($force)) {
2028             $this->ldapconns = 0;
2029             @ldap_close($this->ldapconnection);
2030             unset($this->ldapconnection);
2031         }
2032     }
2034     /**
2035      * Search specified contexts for username and return the user dn
2036      * like: cn=username,ou=suborg,o=org. It's actually a wrapper
2037      * around ldap_find_userdn().
2038      *
2039      * @param resource $ldapconnection a valid LDAP connection
2040      * @param string $extusername the username to search (in external LDAP encoding, no db slashes)
2041      * @return mixed the user dn (external LDAP encoding) or false
2042      */
2043     function ldap_find_userdn($ldapconnection, $extusername) {
2044         $ldap_contexts = explode(';', $this->config->contexts);
2045         if (!empty($this->config->create_context)) {
2046             array_push($ldap_contexts, $this->config->create_context);
2047         }
2049         return ldap_find_userdn($ldapconnection, $extusername, $ldap_contexts, $this->config->objectclass,
2050                                 $this->config->user_attribute, $this->config->search_sub);
2051     }
2053     /**
2054      * When using NTLM SSO, the format of the remote username we get in
2055      * $_SERVER['REMOTE_USER'] may vary, depending on where from and how the web
2056      * server gets the data. So we let the admin configure the format using two
2057      * place holders (%domain% and %username%). This function tries to extract
2058      * the username (stripping the domain part and any separators if they are
2059      * present) from the value present in $_SERVER['REMOTE_USER'], using the
2060      * configured format.
2061      *
2062      * @param string $remoteuser The value from $_SERVER['REMOTE_USER'] (converted to UTF-8)
2063      *
2064      * @return string The remote username (without domain part or
2065      *                separators). Empty string if we can't extract the username.
2066      */
2067     protected function get_ntlm_remote_user($remoteuser) {
2068         if (empty($this->config->ntlmsso_remoteuserformat)) {
2069             $format = AUTH_NTLM_DEFAULT_FORMAT;
2070         } else {
2071             $format = $this->config->ntlmsso_remoteuserformat;
2072         }
2074         $format = preg_quote($format);
2075         $formatregex = preg_replace(array('#%domain%#', '#%username%#'),
2076                                     array('('.AUTH_NTLM_VALID_DOMAINNAME.')', '('.AUTH_NTLM_VALID_USERNAME.')'),
2077                                     $format);
2078         if (preg_match('#^'.$formatregex.'$#', $remoteuser, $matches)) {
2079             $user = end($matches);
2080             return $user;
2081         }
2083         /* We are unable to extract the username with the configured format. Probably
2084          * the format specified is wrong, so log a warning for the admin and return
2085          * an empty username.
2086          */
2087         error_log($this->errorlogtag.get_string ('auth_ntlmsso_maybeinvalidformat', 'auth_ldap'));
2088         return '';
2089     }
2091     /**
2092      * Check if the diagnostic message for the LDAP login error tells us that the
2093      * login is denied because the user password has expired or the password needs
2094      * to be changed on first login (using interactive SMB/Windows logins, not
2095      * LDAP logins).
2096      *
2097      * @param string the diagnostic message for the LDAP login error
2098      * @return bool true if the password has expired or the password must be changed on first login
2099      */
2100     protected function ldap_ad_pwdexpired_from_diagmsg($diagmsg) {
2101         // The format of the diagnostic message is (actual examples from W2003 and W2008):
2102         // "80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece"  (W2003)
2103         // "80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 773, vece"  (W2003)
2104         // "80090308: LdapErr: DSID-0C0903AA, comment: AcceptSecurityContext error, data 52e, v1771" (W2008)
2105         // "80090308: LdapErr: DSID-0C0903AA, comment: AcceptSecurityContext error, data 773, v1771" (W2008)
2106         // We are interested in the 'data nnn' part.
2107         //   if nnn == 773 then user must change password on first login
2108         //   if nnn == 532 then user password has expired
2109         $diagmsg = explode(',', $diagmsg);
2110         if (preg_match('/data (773|532)/i', trim($diagmsg[2]))) {
2111             return true;
2112         }
2113         return false;
2114     }
2116     /**
2117      * Check if a user is suspended. This function is intended to be used after calling
2118      * get_userinfo_asobj. This is needed because LDAP doesn't have a notion of disabled
2119      * users, however things like MS Active Directory support it and expose information
2120      * through a field.
2121      *
2122      * @param object $user the user object returned by get_userinfo_asobj
2123      * @return boolean
2124      */
2125     protected function is_user_suspended($user) {
2126         if (!$this->config->suspended_attribute || !isset($user->suspended)) {
2127             return false;
2128         }
2129         if ($this->config->suspended_attribute == 'useraccountcontrol' && $this->config->user_type == 'ad') {
2130             return (bool)($user->suspended & AUTH_AD_ACCOUNTDISABLE);
2131         }
2133         return (bool)$user->suspended;
2134     }
2136     /**
2137      * Test if settings are correct, print info to output.
2138      */
2139     public function test_settings() {
2140         global $OUTPUT;
2142         if (!function_exists('ldap_connect')) { // Is php-ldap really there?
2143             echo $OUTPUT->notification(get_string('auth_ldap_noextension', 'auth_ldap'));
2144             return;
2145         }
2147         // Check to see if this is actually configured.
2148         if ((isset($this->config->host_url)) && ($this->config->host_url !== '')) {
2150             try {
2151                 $ldapconn = $this->ldap_connect();
2152                 // Try to connect to the LDAP server.  See if the page size setting is supported on this server.
2153                 $pagedresultssupported = ldap_paged_results_supported($this->config->ldap_version, $ldapconn);
2154             } catch (Exception $e) {
2156                 // If we couldn't connect and get the supported options, we can only assume we don't support paged results.
2157                 $pagedresultssupported = false;
2158             }
2160             // Display paged file results.
2161             if ((!$pagedresultssupported)) {
2162                 echo $OUTPUT->notification(get_string('pagedresultsnotsupp', 'auth_ldap'), \core\output\notification::NOTIFY_INFO);
2163             } else if ($ldapconn) {
2164                 // We were able to connect successfuly.
2165                 echo $OUTPUT->notification(get_string('connectingldapsuccess', 'auth_ldap'), \core\output\notification::NOTIFY_SUCCESS);
2166             }
2168         } else {
2169             // LDAP is not even configured.
2170             echo $OUTPUT->notification(get_string('ldapnotconfigured', 'auth_ldap'), \core\output\notification::NOTIFY_INFO);
2171         }
2172     }
2173 } // End of the class