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