2007-01-04 04:52:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Martin Dougiamas
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
* @package moodle multiauth
|
|
|
|
*
|
|
|
|
* Authentication Plugin: LDAP Authentication
|
|
|
|
*
|
|
|
|
* Authentication using LDAP (Lightweight Directory Access Protocol).
|
|
|
|
*
|
|
|
|
* 2006-08-28 File created.
|
|
|
|
*/
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!defined('MOODLE_INTERNAL')) {
|
|
|
|
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
2007-05-30 08:47:00 +00:00
|
|
|
// See http://support.microsoft.com/kb/305144 to interprete these values.
|
|
|
|
if (!defined('AUTH_AD_ACCOUNTDISABLE')) {
|
|
|
|
define('AUTH_AD_ACCOUNTDISABLE', 0x0002);
|
|
|
|
}
|
|
|
|
if (!defined('AUTH_AD_NORMAL_ACCOUNT')) {
|
|
|
|
define('AUTH_AD_NORMAL_ACCOUNT', 0x0200);
|
|
|
|
}
|
2007-11-14 22:11:35 +00:00
|
|
|
if (!defined('AUTH_NTLMTIMEOUT')) { // timewindow for the NTLM SSO process, in secs...
|
|
|
|
define('AUTH_NTLMTIMEOUT', 10);
|
|
|
|
}
|
|
|
|
|
2007-05-30 08:47:00 +00:00
|
|
|
|
2007-03-22 12:27:52 +00:00
|
|
|
require_once($CFG->libdir.'/authlib.php');
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
/**
|
|
|
|
* LDAP authentication plugin.
|
|
|
|
*/
|
2007-03-22 12:27:52 +00:00
|
|
|
class auth_plugin_ldap extends auth_plugin_base {
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
/**
|
2007-02-20 17:03:36 +00:00
|
|
|
* Constructor with initialisation.
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function auth_plugin_ldap() {
|
2007-03-22 12:27:52 +00:00
|
|
|
$this->authtype = 'ldap';
|
2007-01-04 04:52:42 +00:00
|
|
|
$this->config = get_config('auth/ldap');
|
2007-02-20 17:03:36 +00:00
|
|
|
if (empty($this->config->ldapencoding)) {
|
|
|
|
$this->config->ldapencoding = 'utf-8';
|
|
|
|
}
|
|
|
|
if (empty($this->config->user_type)) {
|
|
|
|
$this->config->user_type = 'default';
|
|
|
|
}
|
|
|
|
|
|
|
|
$default = $this->ldap_getdefaults();
|
|
|
|
|
|
|
|
//use defaults if values not given
|
|
|
|
foreach ($default as $key => $value) {
|
|
|
|
// watch out - 0, false are correct values too
|
|
|
|
if (!isset($this->config->{$key}) or $this->config->{$key} == '') {
|
|
|
|
$this->config->{$key} = $value[$this->config->user_type];
|
|
|
|
}
|
|
|
|
}
|
2008-08-24 20:46:49 +00:00
|
|
|
|
|
|
|
// Hack prefix to objectclass
|
|
|
|
if (empty($this->config->objectclass)) {
|
|
|
|
// Can't send empty filter
|
|
|
|
$this->config->objectclass='(objectClass=*)';
|
|
|
|
} else if (stripos($this->config->objectclass, 'objectClass=') === 0) {
|
|
|
|
// Value is 'objectClass=some-string-here', so just add ()
|
|
|
|
// around the value (filter _must_ have them).
|
|
|
|
$this->config->objectclass = '('.$this->config->objectclass.')';
|
|
|
|
} else if (stripos($this->config->objectclass, '(') !== 0) {
|
|
|
|
// Value is 'some-string-not-starting-with-left-parentheses',
|
|
|
|
// which is assumed to be the objectClass matching value.
|
|
|
|
// So build a valid filter with it.
|
|
|
|
$this->config->objectclass = '(objectClass='.$this->config->objectclass.')';
|
|
|
|
} else {
|
|
|
|
// There is an additional possible value
|
|
|
|
// '(some-string-here)', that can be used to specify any
|
|
|
|
// valid filter string, to select subsets of users based
|
|
|
|
// on any criteria. For example, we could select the users
|
|
|
|
// whose objectClass is 'user' and have the
|
|
|
|
// 'enabledMoodleUser' attribute, with something like:
|
|
|
|
//
|
|
|
|
// (&(objectClass=user)(enabledMoodleUser=1))
|
|
|
|
//
|
|
|
|
// This is only used in the functions that deal with the
|
|
|
|
// whole potential set of users (currently sync_users()
|
|
|
|
// and get_user_list() only).
|
|
|
|
//
|
|
|
|
// In this particular case we don't need to do anything,
|
|
|
|
// so leave $this->config->objectclass as is.
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-02-21 21:42:10 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the username and password work and false if they are
|
|
|
|
* wrong or don't exist.
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* @param string $username The username (with system magic quotes)
|
|
|
|
* @param string $password The password (with system magic quotes)
|
|
|
|
*
|
|
|
|
* @return bool Authentication success or failure.
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function user_login($username, $password) {
|
2007-01-10 00:50:59 +00:00
|
|
|
if (! function_exists('ldap_bind')) {
|
2007-02-09 00:34:49 +00:00
|
|
|
print_error('auth_ldapnotinstalled','auth');
|
2007-01-10 00:50:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
if (!$username or !$password) { // Don't allow blank usernames or passwords
|
|
|
|
return false;
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-11-14 22:12:07 +00:00
|
|
|
$textlib = textlib_get_instance();
|
2008-05-30 20:54:19 +00:00
|
|
|
$extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
|
|
|
|
$extpassword = $textlib->convert($password, 'utf-8', $this->config->ldapencoding);
|
2007-11-14 22:12:07 +00:00
|
|
|
|
2007-11-14 22:08:11 +00:00
|
|
|
//
|
|
|
|
// Before we connect to LDAP, check if this is an AD SSO login
|
2007-11-14 22:11:35 +00:00
|
|
|
// if we succeed in this block, we'll return success early.
|
2007-11-14 22:08:11 +00:00
|
|
|
//
|
2007-11-14 22:11:48 +00:00
|
|
|
$key = sesskey();
|
|
|
|
if (!empty($this->config->ntlmsso_enabled) && $key === $password) {
|
2007-11-19 02:43:48 +00:00
|
|
|
$cf = get_cache_flags('auth/ldap/ntlmsess');
|
|
|
|
// We only get the cache flag if we retrieve it before
|
|
|
|
// it expires (AUTH_NTLMTIMEOUT seconds).
|
|
|
|
if (!isset($cf[$key]) || $cf[$key] === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sessusername = $cf[$key];
|
|
|
|
if ($username === $sessusername) {
|
|
|
|
unset($sessusername);
|
|
|
|
unset($cf);
|
|
|
|
|
|
|
|
// Check that the user is inside one of the configured LDAP contexts
|
|
|
|
$validuser = false;
|
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
if ($ldapconnection) {
|
|
|
|
// if the user is not inside the configured contexts,
|
|
|
|
// ldap_find_userdn returns false.
|
|
|
|
if ($this->ldap_find_userdn($ldapconnection, $extusername)) {
|
|
|
|
$validuser = true;
|
2007-11-14 22:08:11 +00:00
|
|
|
}
|
2007-11-19 02:43:48 +00:00
|
|
|
ldap_close($ldapconnection);
|
2007-11-14 22:08:11 +00:00
|
|
|
}
|
2007-11-19 02:43:48 +00:00
|
|
|
|
|
|
|
// Shortcut here - SSO confirmed
|
|
|
|
return $validuser;
|
2007-11-14 22:08:11 +00:00
|
|
|
}
|
2007-11-14 22:11:35 +00:00
|
|
|
} // End SSO processing
|
2007-11-14 22:11:48 +00:00
|
|
|
unset($key);
|
2007-11-14 22:08:11 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
if ($ldapconnection) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$ldap_user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
//if ldap_user_dn is empty, user does not exist
|
|
|
|
if (!$ldap_user_dn) {
|
|
|
|
ldap_close($ldapconnection);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to bind with current username and password
|
2007-02-20 17:03:36 +00:00
|
|
|
$ldap_login = @ldap_bind($ldapconnection, $ldap_user_dn, $extpassword);
|
2007-01-04 04:52:42 +00:00
|
|
|
ldap_close($ldapconnection);
|
|
|
|
if ($ldap_login) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
@ldap_close($ldapconnection);
|
2008-01-08 00:13:49 +00:00
|
|
|
print_error('auth_ldap_noconnect','auth','',$this->config->host_url);
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reads userinformation from ldap and return it in array()
|
|
|
|
*
|
|
|
|
* Read user information from external database and returns it as array().
|
|
|
|
* Function should return all information available. If you are saving
|
|
|
|
* this information to moodle user-table you should honor syncronization flags
|
|
|
|
*
|
2008-05-30 20:54:19 +00:00
|
|
|
* @param string $username username
|
2007-02-20 17:03:36 +00:00
|
|
|
*
|
|
|
|
* @return mixed array with no magic quotes or false on error
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function get_userinfo($username) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$textlib = textlib_get_instance();
|
2008-05-30 20:54:19 +00:00
|
|
|
$extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
$attrmap = $this->ldap_attributes();
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$result = array();
|
|
|
|
$search_attribs = array();
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
foreach ($attrmap as $key=>$values) {
|
|
|
|
if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if (!in_array($value, $search_attribs)) {
|
|
|
|
array_push($search_attribs, $value);
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
$user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
|
2007-01-04 04:52:42 +00:00
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!$user_info_result = ldap_read($ldapconnection, $user_dn, $this->config->objectclass, $search_attribs)) {
|
|
|
|
return false; // error!
|
|
|
|
}
|
|
|
|
$user_entry = $this->ldap_get_entries($ldapconnection, $user_info_result);
|
|
|
|
if (empty($user_entry)) {
|
|
|
|
return false; // entry not found
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($attrmap as $key=>$values) {
|
|
|
|
if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
$ldapval = NULL;
|
|
|
|
foreach ($values as $value) {
|
2007-03-29 08:42:07 +00:00
|
|
|
if ($value == 'dn') {
|
|
|
|
$result[$key] = $user_dn;
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!array_key_exists($value, $user_entry[0])) {
|
|
|
|
continue; // wrong data mapping!
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
if (is_array($user_entry[0][$value])) {
|
|
|
|
$newval = $textlib->convert($user_entry[0][$value][0], $this->config->ldapencoding, 'utf-8');
|
|
|
|
} else {
|
|
|
|
$newval = $textlib->convert($user_entry[0][$value], $this->config->ldapencoding, 'utf-8');
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!empty($newval)) { // favour ldap entries that are set
|
|
|
|
$ldapval = $newval;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!is_null($ldapval)) {
|
|
|
|
$result[$key] = $ldapval;
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@ldap_close($ldapconnection);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reads userinformation from ldap and return it in an object
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* @param string $username username (with system magic quotes)
|
|
|
|
* @return mixed object or false on error
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function get_userinfo_asobj($username) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$user_array = $this->get_userinfo($username);
|
|
|
|
if ($user_array == false) {
|
|
|
|
return false; //error or not found
|
|
|
|
}
|
|
|
|
$user_array = truncate_userinfo($user_array);
|
|
|
|
$user = new object();
|
2007-01-04 04:52:42 +00:00
|
|
|
foreach ($user_array as $key=>$value) {
|
|
|
|
$user->{$key} = $value;
|
|
|
|
}
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns all usernames from external database
|
|
|
|
*
|
|
|
|
* get_userlist returns all usernames from external database
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* @return array
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function get_userlist() {
|
|
|
|
return $this->ldap_get_userlist("({$this->config->user_attribute}=*)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if user exists on external db
|
2007-02-20 17:03:36 +00:00
|
|
|
*
|
2008-06-07 15:41:25 +00:00
|
|
|
* @param string $username
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function user_exists($username) {
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
$textlib = textlib_get_instance();
|
2008-06-07 15:41:25 +00:00
|
|
|
$extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
//returns true if given username exist on ldap
|
|
|
|
$users = $this->ldap_get_userlist("({$this->config->user_attribute}=".$this->filter_addslashes($extusername).")");
|
|
|
|
return count($users);
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-02-20 17:03:36 +00:00
|
|
|
* Creates a new user on external database.
|
2007-01-04 04:52:42 +00:00
|
|
|
* By using information in userobject
|
|
|
|
* Use user_exists to prevent dublicate usernames
|
|
|
|
*
|
2008-06-07 15:41:25 +00:00
|
|
|
* @param mixed $userobject Moodle userobject
|
|
|
|
* @param mixed $plainpass Plaintext password
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function user_create($userobject, $plainpass) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$textlib = textlib_get_instance();
|
2008-06-07 15:41:25 +00:00
|
|
|
$extusername = $textlib->convert($userobject->username, 'utf-8', $this->config->ldapencoding);
|
|
|
|
$extpassword = $textlib->convert($plainpass, 'utf-8', $this->config->ldapencoding);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-03-29 19:50:53 +00:00
|
|
|
switch ($this->config->passtype) {
|
|
|
|
case 'md5':
|
|
|
|
$extpassword = '{MD5}' . base64_encode(pack('H*', md5($extpassword)));
|
|
|
|
break;
|
|
|
|
case 'sha1':
|
|
|
|
$extpassword = '{SHA}' . base64_encode(pack('H*', sha1($extpassword)));
|
|
|
|
break;
|
|
|
|
case 'plaintext':
|
|
|
|
default:
|
|
|
|
break; // plaintext
|
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
$attrmap = $this->ldap_attributes();
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$newuser = array();
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
foreach ($attrmap as $key => $values) {
|
|
|
|
if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if (!empty($userobject->$key) ) {
|
2008-06-07 15:41:25 +00:00
|
|
|
$newuser[$value] = $textlib->convert($userobject->$key, 'utf-8', $this->config->ldapencoding);
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
//Following sets all mandatory and other forced attribute values
|
|
|
|
//User should be creted as login disabled untill email confirmation is processed
|
2007-02-20 17:03:36 +00:00
|
|
|
//Feel free to add your user type and send patches to paca@sci.fi to add them
|
2007-01-04 04:52:42 +00:00
|
|
|
//Moodle distribution
|
|
|
|
|
|
|
|
switch ($this->config->user_type) {
|
|
|
|
case 'edir':
|
2007-02-20 17:03:36 +00:00
|
|
|
$newuser['objectClass'] = array("inetOrgPerson","organizationalPerson","person","top");
|
|
|
|
$newuser['uniqueId'] = $extusername;
|
|
|
|
$newuser['logindisabled'] = "TRUE";
|
|
|
|
$newuser['userpassword'] = $extpassword;
|
2007-06-09 15:33:22 +00:00
|
|
|
$uadd = ldap_add($ldapconnection, $this->config->user_attribute.'="'.$this->ldap_addslashes($userobject->username).','.$this->config->create_context.'"', $newuser);
|
2007-05-30 08:47:00 +00:00
|
|
|
break;
|
|
|
|
case 'ad':
|
|
|
|
// User account creation is a two step process with AD. First you
|
|
|
|
// create the user object, then you set the password. If you try
|
|
|
|
// to set the password while creating the user, the operation
|
|
|
|
// fails.
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:47:00 +00:00
|
|
|
// Passwords in Active Directory must be encoded as Unicode
|
|
|
|
// strings (UCS-2 Little Endian format) and surrounded with
|
|
|
|
// double quotes. See http://support.microsoft.com/?kbid=269190
|
|
|
|
if (!function_exists('mb_convert_encoding')) {
|
|
|
|
print_error ('auth_ldap_no_mbstring', 'auth');
|
|
|
|
}
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:47:00 +00:00
|
|
|
// First create the user account, and mark it as disabled.
|
|
|
|
$newuser['objectClass'] = array('top','person','user','organizationalPerson');
|
|
|
|
$newuser['sAMAccountName'] = $extusername;
|
2007-08-17 11:18:58 +00:00
|
|
|
$newuser['userAccountControl'] = AUTH_AD_NORMAL_ACCOUNT |
|
2007-05-30 08:47:00 +00:00
|
|
|
AUTH_AD_ACCOUNTDISABLE;
|
|
|
|
$userdn = 'cn=' . $this->ldap_addslashes($extusername) .
|
|
|
|
',' . $this->config->create_context;
|
|
|
|
if (!ldap_add($ldapconnection, $userdn, $newuser)) {
|
|
|
|
print_error ('auth_ldap_ad_create_req', 'auth');
|
|
|
|
}
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:47:00 +00:00
|
|
|
// Now set the password
|
|
|
|
unset($newuser);
|
|
|
|
$newuser['unicodePwd'] = mb_convert_encoding('"' . $extpassword . '"',
|
|
|
|
"UCS-2LE", "UTF-8");
|
|
|
|
if(!ldap_modify($ldapconnection, $userdn, $newuser)) {
|
|
|
|
// Something went wrong: delete the user account and error out
|
|
|
|
ldap_delete ($ldapconnection, $userdn);
|
|
|
|
print_error ('auth_ldap_ad_create_req', 'auth');
|
|
|
|
}
|
|
|
|
$uadd = true;
|
2007-01-04 04:52:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-05-21 20:33:42 +00:00
|
|
|
print_error('auth_ldap_unsupportedusertype','auth','',$this->config->user_type);
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
ldap_close($ldapconnection);
|
|
|
|
return $uadd;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-10 19:26:12 +00:00
|
|
|
function can_reset_password() {
|
|
|
|
return !empty($this->config->stdchangepassword);
|
|
|
|
}
|
|
|
|
|
2007-05-21 20:33:42 +00:00
|
|
|
function can_signup() {
|
|
|
|
return (!empty($this->config->auth_user_create) and !empty($this->config->create_context));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sign up a new user ready for confirmation.
|
|
|
|
* Password is passed in plaintext.
|
|
|
|
*
|
2008-05-30 22:11:31 +00:00
|
|
|
* @param object $user new user object
|
2007-05-21 20:33:42 +00:00
|
|
|
* @param boolean $notify print notice with link and terminate
|
|
|
|
*/
|
|
|
|
function user_signup($user, $notify=true) {
|
2007-08-20 08:30:34 +00:00
|
|
|
global $CFG;
|
|
|
|
require_once($CFG->dirroot.'/user/profile/lib.php');
|
|
|
|
|
2007-05-21 20:33:42 +00:00
|
|
|
if ($this->user_exists($user->username)) {
|
|
|
|
print_error('auth_ldap_user_exists', 'auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
$plainslashedpassword = $user->password;
|
|
|
|
unset($user->password);
|
|
|
|
|
|
|
|
if (! $this->user_create($user, $plainslashedpassword)) {
|
|
|
|
print_error('auth_ldap_create_error', 'auth');
|
|
|
|
}
|
|
|
|
|
2008-05-30 22:11:31 +00:00
|
|
|
if (! ($user->id = $DB->insert_record('user', $user)) ) {
|
2007-05-21 20:33:42 +00:00
|
|
|
print_error('auth_emailnoinsert', 'auth');
|
|
|
|
}
|
|
|
|
|
2007-08-20 08:30:34 +00:00
|
|
|
/// Save any custom profile field information
|
|
|
|
profile_save_data($user);
|
|
|
|
|
2007-05-21 20:33:42 +00:00
|
|
|
$this->update_user_record($user->username);
|
|
|
|
update_internal_user_password($user, $plainslashedpassword);
|
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
$user = get_record('user', 'id', $user->id);
|
|
|
|
events_trigger('user_created', $user);
|
|
|
|
|
2007-05-21 20:33:42 +00:00
|
|
|
if (! send_confirmation_email($user)) {
|
|
|
|
print_error('auth_emailnoemail', 'auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($notify) {
|
|
|
|
global $CFG;
|
|
|
|
$emailconfirm = get_string('emailconfirm');
|
2007-08-17 11:18:58 +00:00
|
|
|
$navlinks = array();
|
|
|
|
$navlinks[] = array('name' => $emailconfirm, 'link' => null, 'type' => 'misc');
|
|
|
|
$navigation = build_navigation($navlinks);
|
|
|
|
|
|
|
|
print_header($emailconfirm, $emailconfirm, $navigation);
|
2007-05-21 20:33:42 +00:00
|
|
|
notice(get_string('emailconfirmsent', '', $user->email), "$CFG->wwwroot/index.php");
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if plugin allows confirming of new users.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function can_confirm() {
|
|
|
|
return $this->can_signup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Confirm the new user as registered.
|
|
|
|
*
|
2008-05-31 10:43:51 +00:00
|
|
|
* @param string $username
|
|
|
|
* @param string $confirmsecret
|
2007-05-21 20:33:42 +00:00
|
|
|
*/
|
|
|
|
function user_confirm($username, $confirmsecret) {
|
2008-06-07 15:41:25 +00:00
|
|
|
global $DB;
|
|
|
|
|
2007-05-21 20:33:42 +00:00
|
|
|
$user = get_complete_user_data('username', $username);
|
|
|
|
|
|
|
|
if (!empty($user)) {
|
|
|
|
if ($user->confirmed) {
|
|
|
|
return AUTH_CONFIRM_ALREADY;
|
|
|
|
|
|
|
|
} else if ($user->auth != 'ldap') {
|
|
|
|
return AUTH_CONFIRM_ERROR;
|
|
|
|
|
2008-05-31 10:43:51 +00:00
|
|
|
} else if ($user->secret == $confirmsecret) { // They have provided the secret key to get in
|
2007-05-21 20:33:42 +00:00
|
|
|
if (!$this->user_activate($username)) {
|
|
|
|
return AUTH_CONFIRM_FAIL;
|
|
|
|
}
|
2008-06-07 15:41:25 +00:00
|
|
|
if (!$DB->set_field("user", "confirmed", 1, array("id"=>$user->id))) {
|
2007-05-21 20:33:42 +00:00
|
|
|
return AUTH_CONFIRM_FAIL;
|
|
|
|
}
|
2008-06-07 15:41:25 +00:00
|
|
|
if (!$DB->set_field("user", "firstaccess", time(), array("id"=>$user->id))) {
|
2007-05-21 20:33:42 +00:00
|
|
|
return AUTH_CONFIRM_FAIL;
|
|
|
|
}
|
|
|
|
return AUTH_CONFIRM_OK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return AUTH_CONFIRM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
/**
|
|
|
|
* return number of days to user password expires
|
|
|
|
*
|
|
|
|
* If userpassword does not expire it should return 0. If password is already expired
|
|
|
|
* it should return negative value.
|
|
|
|
*
|
2008-06-07 15:41:25 +00:00
|
|
|
* @param mixed $username username
|
2007-01-04 04:52:42 +00:00
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
function password_expire($username) {
|
2007-05-21 20:08:45 +00:00
|
|
|
$result = 0;
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
$textlib = textlib_get_instance();
|
2008-06-07 15:41:25 +00:00
|
|
|
$extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapconnection = $this->ldap_connect();
|
2007-02-20 17:03:36 +00:00
|
|
|
$user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
|
2007-01-04 04:52:42 +00:00
|
|
|
$search_attribs = array($this->config->expireattr);
|
2008-08-24 20:46:49 +00:00
|
|
|
$sr = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs);
|
2007-01-04 04:52:42 +00:00
|
|
|
if ($sr) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$info = $this->ldap_get_entries($ldapconnection, $sr);
|
2007-05-21 20:08:45 +00:00
|
|
|
if (!empty ($info) and !empty($info[0][$this->config->expireattr][0])) {
|
|
|
|
$expiretime = $this->ldap_expirationtime2unix($info[0][$this->config->expireattr][0], $ldapconnection, $user_dn);
|
|
|
|
if ($expiretime != 0) {
|
|
|
|
$now = time();
|
|
|
|
if ($expiretime > $now) {
|
|
|
|
$result = ceil(($expiretime - $now) / DAYSECS);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$result = floor(($expiretime - $now) / DAYSECS);
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
2007-01-04 04:52:42 +00:00
|
|
|
error_log("ldap: password_expire did't find expiration time.");
|
|
|
|
}
|
|
|
|
|
|
|
|
//error_log("ldap: password_expire user $user_dn expires in $result days!");
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* syncronizes user fron external db to moodle user table
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* Sync is now using username attribute.
|
|
|
|
*
|
|
|
|
* Syncing users removes or suspends users that dont exists anymore in external db.
|
|
|
|
* Creates new users and updates coursecreator status of users.
|
|
|
|
*
|
|
|
|
* @param bool $do_updates will do pull in data updates from ldap if relevant
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
2008-06-07 15:41:25 +00:00
|
|
|
function sync_users($do_updates=true) {
|
|
|
|
global $CFG, $DB;
|
2007-02-17 01:26:43 +00:00
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
$textlib = textlib_get_instance();
|
2008-06-07 15:41:25 +00:00
|
|
|
$dbman = $DB->get_manager();
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2008-06-07 15:41:25 +00:00
|
|
|
/// Define table user to be created
|
|
|
|
$table = new xmldb_table('tmp_extuser');
|
|
|
|
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
|
|
|
|
$table->add_field('username', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null);
|
|
|
|
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
|
|
|
|
$table->add_index('username', XMLDB_INDEX_UNIQUE, array('mnethostid', 'username'));
|
2007-01-04 04:52:42 +00:00
|
|
|
|
2007-07-22 21:33:31 +00:00
|
|
|
echo "Creating temp table $temptable\n";
|
2008-06-07 15:41:25 +00:00
|
|
|
if (!$dbman->create_temp_table($table)) {
|
2007-07-22 21:33:31 +00:00
|
|
|
print "Failed to create temporary users table - aborting\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
print "Connecting to ldap...\n";
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
|
|
|
|
if (!$ldapconnection) {
|
|
|
|
@ldap_close($ldapconnection);
|
2007-02-20 17:03:36 +00:00
|
|
|
print get_string('auth_ldap_noconnect','auth',$this->config->host_url);
|
|
|
|
exit;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
//// get user's list from ldap to sql in a scalable fashion
|
|
|
|
////
|
|
|
|
// prepare some data we'll need
|
2008-08-24 20:46:49 +00:00
|
|
|
$filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')';
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
$contexts = explode(";",$this->config->contexts);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($this->config->create_context)) {
|
|
|
|
array_push($contexts, $this->config->create_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fresult = array();
|
|
|
|
foreach ($contexts as $context) {
|
|
|
|
$context = trim($context);
|
|
|
|
if (empty($context)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->config->search_sub) {
|
|
|
|
//use ldap_search to find first user from subtree
|
|
|
|
$ldap_result = ldap_search($ldapconnection, $context,
|
|
|
|
$filter,
|
|
|
|
array($this->config->user_attribute));
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
2007-01-04 04:52:42 +00:00
|
|
|
//search only in this context
|
|
|
|
$ldap_result = ldap_list($ldapconnection, $context,
|
|
|
|
$filter,
|
|
|
|
array($this->config->user_attribute));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($entry = ldap_first_entry($ldapconnection, $ldap_result)) {
|
|
|
|
do {
|
2007-02-20 17:03:36 +00:00
|
|
|
$value = ldap_get_values_len($ldapconnection, $entry, $this->config->user_attribute);
|
|
|
|
$value = $textlib->convert($value[0], $this->config->ldapencoding, 'utf-8');
|
2008-06-07 15:41:25 +00:00
|
|
|
$this->ldap_bulk_insert($value);
|
2007-02-20 17:03:36 +00:00
|
|
|
} while ($entry = ldap_next_entry($ldapconnection, $entry));
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
unset($ldap_result); // free mem
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// preserve our user database
|
|
|
|
/// if the temp table is empty, it probably means that something went wrong, exit
|
|
|
|
/// so as to avoid mass deletion of users; which is hard to undo
|
2008-06-07 15:41:25 +00:00
|
|
|
$count = $DB->count_records_sql('SELECT COUNT(username) AS count, 1 FROM {tmp_extuser}');
|
2007-01-04 04:52:42 +00:00
|
|
|
if ($count < 1) {
|
|
|
|
print "Did not get any users from LDAP -- error? -- exiting\n";
|
|
|
|
exit;
|
2007-02-17 01:26:43 +00:00
|
|
|
} else {
|
2007-02-20 17:03:36 +00:00
|
|
|
print "Got $count records from LDAP\n\n";
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/// User removal
|
|
|
|
// find users in DB that aren't in ldap -- to be removed!
|
|
|
|
// this is still not as scalable (but how often do we mass delete?)
|
|
|
|
if (!empty($this->config->removeuser)) {
|
2008-04-25 13:22:02 +00:00
|
|
|
$sql = "SELECT u.id, u.username, u.email, u.auth
|
2008-06-07 15:41:25 +00:00
|
|
|
FROM {user} u
|
|
|
|
LEFT JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = ?)
|
|
|
|
WHERE u.auth='ldap'
|
|
|
|
AND u.deleted=0
|
|
|
|
AND e.username IS NULL";
|
|
|
|
$remove_users = $DB->get_records_sql($sql, array($CFG->mnet_localhost_id));
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
if (!empty($remove_users)) {
|
|
|
|
print "User entries to remove: ". count($remove_users) . "\n";
|
|
|
|
|
|
|
|
foreach ($remove_users as $user) {
|
2008-03-26 01:35:04 +00:00
|
|
|
if ($this->config->removeuser == AUTH_REMOVEUSER_FULLDELETE) {
|
2007-08-21 20:52:36 +00:00
|
|
|
if (delete_user($user)) {
|
2007-02-20 17:03:36 +00:00
|
|
|
echo "\t"; print_string('auth_dbdeleteuser', 'auth', array($user->username, $user->id)); echo "\n";
|
|
|
|
} else {
|
|
|
|
echo "\t"; print_string('auth_dbdeleteusererror', 'auth', $user->username); echo "\n";
|
|
|
|
}
|
2008-03-26 01:35:04 +00:00
|
|
|
} else if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$updateuser = new object();
|
|
|
|
$updateuser->id = $user->id;
|
|
|
|
$updateuser->auth = 'nologin';
|
2008-06-07 15:41:25 +00:00
|
|
|
if ($DB->update_record('user', $updateuser)) {
|
2007-02-20 17:03:36 +00:00
|
|
|
echo "\t"; print_string('auth_dbsuspenduser', 'auth', array($user->username, $user->id)); echo "\n";
|
|
|
|
} else {
|
|
|
|
echo "\t"; print_string('auth_dbsuspendusererror', 'auth', $user->username); echo "\n";
|
|
|
|
}
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
|
|
|
print "No user entries to be removed\n";
|
|
|
|
}
|
|
|
|
unset($remove_users); // free mem!
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Revive suspended users
|
2008-03-26 01:35:04 +00:00
|
|
|
if (!empty($this->config->removeuser) and $this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$sql = "SELECT u.id, u.username
|
2008-06-07 15:41:25 +00:00
|
|
|
FROM {user} u
|
|
|
|
JOIN {tmp_extuser} e ON (u.username = e.username AND u.mnethostid = ?)
|
|
|
|
WHERE u.auth='nologin' AND u.deleted=0";
|
|
|
|
$revive_users = $DB->get_records_sql($sql, array($CFG->mnet_localhost_id));
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
if (!empty($revive_users)) {
|
|
|
|
print "User entries to be revived: ". count($revive_users) . "\n";
|
|
|
|
|
|
|
|
foreach ($revive_users as $user) {
|
|
|
|
$updateuser = new object();
|
|
|
|
$updateuser->id = $user->id;
|
|
|
|
$updateuser->auth = 'ldap';
|
2008-06-07 15:41:25 +00:00
|
|
|
if ($DB->pdate_record('user', $updateuser)) {
|
2007-02-20 17:03:36 +00:00
|
|
|
echo "\t"; print_string('auth_dbreviveser', 'auth', array($user->username, $user->id)); echo "\n";
|
|
|
|
} else {
|
|
|
|
echo "\t"; print_string('auth_dbreviveusererror', 'auth', $user->username); echo "\n";
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
|
|
|
print "No user entries to be revived\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($revive_users);
|
2007-02-17 01:26:43 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
/// User Updates - time-consuming (optional)
|
2007-01-04 04:52:42 +00:00
|
|
|
if ($do_updates) {
|
|
|
|
// narrow down what fields we need to update
|
|
|
|
$all_keys = array_keys(get_object_vars($this->config));
|
|
|
|
$updatekeys = array();
|
|
|
|
foreach ($all_keys as $key) {
|
|
|
|
if (preg_match('/^field_updatelocal_(.+)$/',$key, $match)) {
|
|
|
|
// if we have a field to update it from
|
2007-02-20 17:03:36 +00:00
|
|
|
// and it must be updated 'onlogin' we
|
2007-01-04 04:52:42 +00:00
|
|
|
// update it on cron
|
|
|
|
if ( !empty($this->config->{'field_map_'.$match[1]})
|
2007-02-20 17:03:36 +00:00
|
|
|
and $this->config->{$match[0]} === 'onlogin') {
|
2007-01-04 04:52:42 +00:00
|
|
|
array_push($updatekeys, $match[1]); // the actual key name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// print_r($all_keys); print_r($updatekeys);
|
|
|
|
unset($all_keys); unset($key);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-02-17 01:26:43 +00:00
|
|
|
} else {
|
|
|
|
print "No updates to be done\n";
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
if ( $do_updates and !empty($updatekeys) ) { // run updates only if relevant
|
2008-06-07 15:41:25 +00:00
|
|
|
$users = $DB->get_records_sql("SELECT u.username, u.id
|
|
|
|
FROM {user} u
|
|
|
|
WHERE u.deleted=0 AND u.auth='ldap' AND u.mnethostid = ?", array($CFG->mnet_localhost_id));
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($users)) {
|
|
|
|
print "User entries to update: ". count($users). "\n";
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!empty($this->config->creators) and !empty($this->config->memberattribute)
|
|
|
|
and $roles = get_roles_with_capability('moodle/legacy:coursecreator', CAP_ALLOW)) {
|
|
|
|
$creatorrole = array_shift($roles); // We can only use one, let's use the first one
|
|
|
|
} else {
|
|
|
|
$creatorrole = false;
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
|
2008-06-07 15:41:25 +00:00
|
|
|
$DB->begin_sql();
|
2007-02-20 17:03:36 +00:00
|
|
|
$xcount = 0;
|
|
|
|
$maxxcount = 100;
|
2007-01-04 04:52:42 +00:00
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
foreach ($users as $user) {
|
|
|
|
echo "\t"; print_string('auth_dbupdatinguser', 'auth', array($user->username, $user->id));
|
2008-05-31 13:57:49 +00:00
|
|
|
if (!$this->update_user_record($user->username, $updatekeys)) {
|
2007-02-20 17:03:36 +00:00
|
|
|
echo " - ".get_string('skipped');
|
|
|
|
}
|
|
|
|
echo "\n";
|
|
|
|
$xcount++;
|
|
|
|
|
|
|
|
// update course creators if needed
|
|
|
|
if ($creatorrole !== false) {
|
|
|
|
if ($this->iscreator($user->username)) {
|
|
|
|
role_assign($creatorrole->id, $user->id, 0, $sitecontext->id, 0, 0, 0, 'ldap');
|
|
|
|
} else {
|
2007-03-22 12:27:52 +00:00
|
|
|
role_unassign($creatorrole->id, $user->id, 0, $sitecontext->id, 'ldap');
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($xcount++ > $maxxcount) {
|
2008-06-07 15:41:25 +00:00
|
|
|
$DB->commit_sql();
|
|
|
|
$DB->begin_sql();
|
2007-02-20 17:03:36 +00:00
|
|
|
$xcount = 0;
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2008-06-07 15:41:25 +00:00
|
|
|
$DB->commit_sql();
|
2007-02-20 17:03:36 +00:00
|
|
|
unset($users); // free mem
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-17 01:26:43 +00:00
|
|
|
} else { // end do updates
|
|
|
|
print "No updates to be done\n";
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
/// User Additions
|
2007-01-04 04:52:42 +00:00
|
|
|
// find users missing in DB that are in LDAP
|
|
|
|
// and gives me a nifty object I don't want.
|
2007-02-20 17:03:36 +00:00
|
|
|
// note: we do not care about deleted accounts anymore, this feature was replaced by suspending to nologin auth plugin
|
2008-06-07 15:41:25 +00:00
|
|
|
// mnetid not used here in join because we can not add users with the same username
|
|
|
|
$sql = "SELECT e.id, e.username
|
|
|
|
FROM {user} u
|
|
|
|
LEFT JOIN {tmp_extuser} e ON u.username = e.username
|
|
|
|
WHERE u.id IS NULL";
|
|
|
|
$add_users = $DB->get_records_sql($sql); // get rid of the fat
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($add_users)) {
|
|
|
|
print "User entries to add: ". count($add_users). "\n";
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
|
|
|
|
if (!empty($this->config->creators) and !empty($this->config->memberattribute)
|
|
|
|
and $roles = get_roles_with_capability('moodle/legacy:coursecreator', CAP_ALLOW)) {
|
2007-01-04 04:52:42 +00:00
|
|
|
$creatorrole = array_shift($roles); // We can only use one, let's use the first one
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
|
|
|
$creatorrole = false;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
2008-06-07 15:41:25 +00:00
|
|
|
$DB->begin_sql();
|
2007-01-04 04:52:42 +00:00
|
|
|
foreach ($add_users as $user) {
|
2008-06-07 15:41:25 +00:00
|
|
|
$user = $this->get_userinfo_asobj($user->username);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
// prep a few params
|
2007-01-10 00:50:59 +00:00
|
|
|
$user->modified = time();
|
|
|
|
$user->confirmed = 1;
|
2007-02-20 17:03:36 +00:00
|
|
|
$user->auth = 'ldap';
|
2007-01-10 00:50:59 +00:00
|
|
|
$user->mnethostid = $CFG->mnet_localhost_id;
|
2007-02-20 17:03:36 +00:00
|
|
|
if (empty($user->lang)) {
|
|
|
|
$user->lang = $CFG->lang;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2008-06-07 15:41:25 +00:00
|
|
|
if ($id = $DB->insert_record('user', $user)) {
|
|
|
|
echo "\t"; print_string('auth_dbinsertuser', 'auth', array($user->username, $id)); echo "\n";
|
2007-02-20 17:03:36 +00:00
|
|
|
$userobj = $this->update_user_record($user->username);
|
|
|
|
if (!empty($this->config->forcechangepassword)) {
|
|
|
|
set_user_preference('auth_forcepasswordchange', 1, $userobj->id);
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
|
|
|
echo "\t"; print_string('auth_dbinsertusererror', 'auth', $user->username); echo "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// add course creators if needed
|
2008-06-07 15:41:25 +00:00
|
|
|
if ($creatorrole !== false and $this->iscreator($user->username)) {
|
2007-02-20 17:03:36 +00:00
|
|
|
role_assign($creatorrole->id, $user->id, 0, $sitecontext->id, 0, 0, 0, 'ldap');
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-07 15:41:25 +00:00
|
|
|
$DB->commit_sql();
|
2007-01-04 04:52:42 +00:00
|
|
|
unset($add_users); // free mem
|
2007-02-17 01:26:43 +00:00
|
|
|
} else {
|
|
|
|
print "No users to be added\n";
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2008-06-07 15:41:25 +00:00
|
|
|
|
|
|
|
$dbman->drop_temp_table($table);
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/**
|
|
|
|
* Update a local user record from an external source.
|
|
|
|
* This is a lighter version of the one in moodlelib -- won't do
|
2007-01-04 04:52:42 +00:00
|
|
|
* expensive ops such as enrolment.
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* If you don't pass $updatekeys, there is a performance hit and
|
|
|
|
* values removed from LDAP won't be removed from moodle.
|
|
|
|
*
|
2008-05-31 13:57:49 +00:00
|
|
|
* @param string $username username
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function update_user_record($username, $updatekeys = false) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
//just in case check text case
|
|
|
|
$username = trim(moodle_strtolower($username));
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
// get the current user record
|
2008-05-31 13:57:49 +00:00
|
|
|
$user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id));
|
2007-01-04 04:52:42 +00:00
|
|
|
if (empty($user)) { // trouble
|
2008-05-31 13:57:49 +00:00
|
|
|
error_log("Cannot update non-existent user: ".$username);
|
2008-01-08 00:13:49 +00:00
|
|
|
print_error('auth_dbusernotexist','auth','',$username);
|
2007-01-04 04:52:42 +00:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
2007-01-10 00:50:59 +00:00
|
|
|
// Protect the userid from being overwritten
|
|
|
|
$userid = $user->id;
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
if ($newinfo = $this->get_userinfo($username)) {
|
|
|
|
$newinfo = truncate_userinfo($newinfo);
|
|
|
|
|
|
|
|
if (empty($updatekeys)) { // all keys? this does not support removing values
|
|
|
|
$updatekeys = array_keys($newinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($updatekeys as $key) {
|
|
|
|
if (isset($newinfo[$key])) {
|
|
|
|
$value = $newinfo[$key];
|
|
|
|
} else {
|
|
|
|
$value = '';
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
if (!empty($this->config->{'field_updatelocal_' . $key})) {
|
|
|
|
if ($user->{$key} != $value) { // only update if it's changed
|
2008-05-31 13:57:49 +00:00
|
|
|
$DB->set_field('user', $key, $value, array('id'=>$userid));
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2008-05-31 13:57:49 +00:00
|
|
|
return $DB->get_record('user', array('id'=>$userid, 'deleted'=>0));
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/**
|
|
|
|
* Bulk insert in SQL's temp table
|
|
|
|
*/
|
2008-06-07 15:41:25 +00:00
|
|
|
function ldap_bulk_insert($username) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$username = moodle_strtolower($username); // usernames are __always__ lowercase.
|
|
|
|
$DB->insert_record_raw('tmp_extuser', array('username'=>$username), false, true);
|
|
|
|
print ".";
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/**
|
2007-01-04 04:52:42 +00:00
|
|
|
* Activates (enables) user in external db so user can login to external db
|
|
|
|
*
|
2008-06-07 15:41:25 +00:00
|
|
|
* @param mixed $username
|
2007-01-04 04:52:42 +00:00
|
|
|
* @return boolen result
|
|
|
|
*/
|
|
|
|
function user_activate($username) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$textlib = textlib_get_instance();
|
2008-06-07 15:41:25 +00:00
|
|
|
$extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
$userdn = $this->ldap_find_userdn($ldapconnection, $extusername);
|
2007-01-04 04:52:42 +00:00
|
|
|
switch ($this->config->user_type) {
|
|
|
|
case 'edir':
|
|
|
|
$newinfo['loginDisabled']="FALSE";
|
|
|
|
break;
|
2007-05-30 08:47:00 +00:00
|
|
|
case 'ad':
|
|
|
|
// We need to unset the ACCOUNTDISABLE bit in the
|
|
|
|
// userAccountControl attribute ( see
|
|
|
|
// http://support.microsoft.com/kb/305144 )
|
|
|
|
$sr = ldap_read($ldapconnection, $userdn, '(objectClass=*)',
|
|
|
|
array('userAccountControl'));
|
|
|
|
$info = ldap_get_entries($ldapconnection, $sr);
|
|
|
|
$newinfo['userAccountControl'] = $info[0]['userAccountControl'][0]
|
|
|
|
& (~AUTH_AD_ACCOUNTDISABLE);
|
|
|
|
break;
|
2007-01-04 04:52:42 +00:00
|
|
|
default:
|
2007-02-20 17:03:36 +00:00
|
|
|
error ('auth: ldap user_activate() does not support selected usertype:"'.$this->config->user_type.'" (..yet)');
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
$result = ldap_modify($ldapconnection, $userdn, $newinfo);
|
|
|
|
ldap_close($ldapconnection);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/**
|
2007-01-04 04:52:42 +00:00
|
|
|
* Disables user in external db so user can't login to external db
|
|
|
|
*
|
|
|
|
* @param mixed $username username
|
|
|
|
* @return boolean result
|
|
|
|
*/
|
2007-02-20 17:03:36 +00:00
|
|
|
/* function user_disable($username) {
|
|
|
|
$textlib = textlib_get_instance();
|
2008-06-07 15:41:25 +00:00
|
|
|
$extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
$userdn = $this->ldap_find_userdn($ldapconnection, $extusername);
|
2007-01-04 04:52:42 +00:00
|
|
|
switch ($this->config->user_type) {
|
|
|
|
case 'edir':
|
|
|
|
$newinfo['loginDisabled']="TRUE";
|
|
|
|
break;
|
2007-05-30 08:47:00 +00:00
|
|
|
case 'ad':
|
|
|
|
// We need to set the ACCOUNTDISABLE bit in the
|
|
|
|
// userAccountControl attribute ( see
|
|
|
|
// http://support.microsoft.com/kb/305144 )
|
|
|
|
$sr = ldap_read($ldapconnection, $userdn, '(objectClass=*)',
|
|
|
|
array('userAccountControl'));
|
|
|
|
$info = auth_ldap_get_entries($ldapconnection, $sr);
|
|
|
|
$newinfo['userAccountControl'] = $info[0]['userAccountControl'][0]
|
|
|
|
| AUTH_AD_ACCOUNTDISABLE;
|
|
|
|
break;
|
2007-01-04 04:52:42 +00:00
|
|
|
default:
|
2007-02-20 17:03:36 +00:00
|
|
|
error ('auth: ldap user_disable() does not support selected usertype (..yet)');
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
$result = ldap_modify($ldapconnection, $userdn, $newinfo);
|
|
|
|
ldap_close($ldapconnection);
|
|
|
|
return $result;
|
2007-02-20 17:03:36 +00:00
|
|
|
}*/
|
2007-01-04 04:52:42 +00:00
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/**
|
2007-01-04 04:52:42 +00:00
|
|
|
* Returns true if user should be coursecreator.
|
|
|
|
*
|
2007-03-22 12:27:52 +00:00
|
|
|
* @param mixed $username username (without system magic quotes)
|
2007-01-04 04:52:42 +00:00
|
|
|
* @return boolean result
|
|
|
|
*/
|
2007-03-22 12:27:52 +00:00
|
|
|
function iscreator($username) {
|
2007-02-20 17:03:36 +00:00
|
|
|
if (empty($this->config->creators) or empty($this->config->memberattribute)) {
|
2007-03-22 12:27:52 +00:00
|
|
|
return null;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
$textlib = textlib_get_instance();
|
|
|
|
$extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
|
|
|
|
|
2007-03-22 12:27:52 +00:00
|
|
|
return (boolean)$this->ldap_isgroupmember($extusername, $this->config->creators);
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/**
|
2007-01-04 04:52:42 +00:00
|
|
|
* Called when the user record is updated.
|
2007-02-20 17:03:36 +00:00
|
|
|
* Modifies user in external database. It takes olduser (before changes) and newuser (after changes)
|
2007-01-04 04:52:42 +00:00
|
|
|
* conpares information saved modified information to external db.
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* @param mixed $olduser Userobject before modifications (without system magic quotes)
|
|
|
|
* @param mixed $newuser Userobject new modified userobject (without system magic quotes)
|
2007-01-04 04:52:42 +00:00
|
|
|
* @return boolean result
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function user_update($olduser, $newuser) {
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (isset($olduser->username) and isset($newuser->username) and $olduser->username != $newuser->username) {
|
|
|
|
error_log("ERROR:User renaming not allowed in LDAP");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-22 12:27:52 +00:00
|
|
|
if (isset($olduser->auth) and $olduser->auth != 'ldap') {
|
2007-02-20 17:03:36 +00:00
|
|
|
return true; // just change auth and skip update
|
|
|
|
}
|
|
|
|
|
2007-12-28 12:06:22 +00:00
|
|
|
$attrmap = $this->ldap_attributes();
|
|
|
|
|
|
|
|
// Before doing anything else, make sure really need to update anything
|
|
|
|
// in the external LDAP server.
|
|
|
|
$update_external = false;
|
|
|
|
foreach ($attrmap as $key => $ldapkeys) {
|
|
|
|
if (!empty($this->config->{'field_updateremote_'.$key})) {
|
|
|
|
$update_external = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$update_external) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
$textlib = textlib_get_instance();
|
|
|
|
$extoldusername = $textlib->convert($olduser->username, 'utf-8', $this->config->ldapencoding);
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
$ldapconnection = $this->ldap_connect();
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$search_attribs = array();
|
|
|
|
|
|
|
|
foreach ($attrmap as $key => $values) {
|
|
|
|
if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
|
|
|
}
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if (!in_array($value, $search_attribs)) {
|
|
|
|
array_push($search_attribs, $value);
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
$user_dn = $this->ldap_find_userdn($ldapconnection, $extoldusername);
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
$user_info_result = ldap_read($ldapconnection, $user_dn,
|
|
|
|
$this->config->objectclass, $search_attribs);
|
|
|
|
|
|
|
|
if ($user_info_result) {
|
|
|
|
|
|
|
|
$user_entry = $this->ldap_get_entries($ldapconnection, $user_info_result);
|
2007-02-20 17:03:36 +00:00
|
|
|
if (empty($user_entry)) {
|
2007-12-28 12:06:22 +00:00
|
|
|
$error = 'ldap: Could not find user while updating externally. '.
|
|
|
|
'Details follow: search base: \''.$user_dn.'\'; search filter: \''.
|
|
|
|
$this->config->objectclass.'\'; search attributes: ';
|
|
|
|
foreach ($search_attribs as $attrib) {
|
|
|
|
$error .= $attrib.' ';
|
|
|
|
}
|
|
|
|
error_log($error);
|
2007-02-20 17:03:36 +00:00
|
|
|
return false; // old user not found!
|
|
|
|
} else if (count($user_entry) > 1) {
|
2007-12-28 12:06:22 +00:00
|
|
|
error_log('ldap: Strange! More than one user record found in ldap. Only using the first one.');
|
2007-02-20 17:03:36 +00:00
|
|
|
return false;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
$user_entry = $user_entry[0];
|
|
|
|
|
|
|
|
//error_log(var_export($user_entry) . 'fpp' );
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
foreach ($attrmap as $key => $ldapkeys) {
|
2007-01-04 04:52:42 +00:00
|
|
|
// only process if the moodle field ($key) has changed and we
|
|
|
|
// are set to update LDAP with it
|
2007-02-20 17:03:36 +00:00
|
|
|
if (isset($olduser->$key) and isset($newuser->$key)
|
|
|
|
and $olduser->$key !== $newuser->$key
|
|
|
|
and !empty($this->config->{'field_updateremote_'. $key})) {
|
|
|
|
// for ldap values that could be in more than one
|
|
|
|
// ldap key, we will do our best to match
|
2007-01-04 04:52:42 +00:00
|
|
|
// where they came from
|
|
|
|
$ambiguous = true;
|
|
|
|
$changed = false;
|
|
|
|
if (!is_array($ldapkeys)) {
|
|
|
|
$ldapkeys = array($ldapkeys);
|
|
|
|
}
|
|
|
|
if (count($ldapkeys) < 2) {
|
|
|
|
$ambiguous = false;
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
$nuvalue = $textlib->convert($newuser->$key, 'utf-8', $this->config->ldapencoding);
|
2008-05-31 15:00:42 +00:00
|
|
|
empty($nuvalue) ? $nuvalue = array() : $nuvalue;
|
2007-02-20 17:03:36 +00:00
|
|
|
$ouvalue = $textlib->convert($olduser->$key, 'utf-8', $this->config->ldapencoding);
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
foreach ($ldapkeys as $ldapkey) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$ldapkey = $ldapkey;
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapvalue = $user_entry[$ldapkey][0];
|
|
|
|
if (!$ambiguous) {
|
|
|
|
// skip update if the values already match
|
2007-02-20 17:03:36 +00:00
|
|
|
if ($nuvalue !== $ldapvalue) {
|
|
|
|
//this might fail due to schema validation
|
|
|
|
if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
error_log('Error updating LDAP record. Error code: '
|
|
|
|
. ldap_errno($ldapconnection) . '; Error string : '
|
|
|
|
. ldap_err2str(ldap_errno($ldapconnection))
|
|
|
|
. "\nKey ($key) - old moodle value: '$ouvalue' new value: '$nuvalue'");
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
2007-01-04 04:52:42 +00:00
|
|
|
// ambiguous
|
|
|
|
// value empty before in Moodle (and LDAP) - use 1st ldap candidate field
|
|
|
|
// no need to guess
|
2007-02-20 17:03:36 +00:00
|
|
|
if ($ouvalue === '') { // value empty before - use 1st ldap candidate
|
|
|
|
//this might fail due to schema validation
|
|
|
|
if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
|
2007-01-04 04:52:42 +00:00
|
|
|
$changed = true;
|
2007-02-20 17:03:36 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
error_log('Error updating LDAP record. Error code: '
|
|
|
|
. ldap_errno($ldapconnection) . '; Error string : '
|
|
|
|
. ldap_err2str(ldap_errno($ldapconnection))
|
|
|
|
. "\nKey ($key) - old moodle value: '$ouvalue' new value: '$nuvalue'");
|
|
|
|
continue;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
// we found which ldap key to update!
|
|
|
|
if ($ouvalue !== '' and $ouvalue === $ldapvalue ) {
|
|
|
|
//this might fail due to schema validation
|
|
|
|
if (@ldap_modify($ldapconnection, $user_dn, array($ldapkey => $nuvalue))) {
|
2007-01-04 04:52:42 +00:00
|
|
|
$changed = true;
|
2007-02-20 17:03:36 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
error_log('Error updating LDAP record. Error code: '
|
2007-01-04 04:52:42 +00:00
|
|
|
. ldap_errno($ldapconnection) . '; Error string : '
|
2007-02-20 17:03:36 +00:00
|
|
|
. ldap_err2str(ldap_errno($ldapconnection))
|
|
|
|
. "\nKey ($key) - old moodle value: '$ouvalue' new value: '$nuvalue'");
|
|
|
|
continue;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if ($ambiguous and !$changed) {
|
2007-02-20 17:03:36 +00:00
|
|
|
error_log("Failed to update LDAP with ambiguous field $key".
|
|
|
|
" old moodle value: '" . $ouvalue .
|
|
|
|
"' new value '" . $nuvalue );
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
} else {
|
2007-01-04 04:52:42 +00:00
|
|
|
error_log("ERROR:No user found in LDAP");
|
|
|
|
@ldap_close($ldapconnection);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ldap_close($ldapconnection);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-01-28 22:08:59 +00:00
|
|
|
/**
|
2007-01-04 04:52:42 +00:00
|
|
|
* changes userpassword in external db
|
|
|
|
*
|
|
|
|
* called when the user password is updated.
|
|
|
|
* changes userpassword in external db
|
|
|
|
*
|
2008-05-30 19:59:50 +00:00
|
|
|
* @param object $user User table object
|
|
|
|
* @param string $newpassword Plaintext password
|
2007-01-04 04:52:42 +00:00
|
|
|
* @return boolean result
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function user_update_password($user, $newpassword) {
|
|
|
|
/// called when the user password is updated -- it assumes it is called by an admin
|
|
|
|
/// or that you've otherwise checked the user's credentials
|
|
|
|
/// IMPORTANT: $newpassword must be cleartext, not crypted/md5'ed
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
global $USER;
|
2007-01-04 04:52:42 +00:00
|
|
|
$result = false;
|
|
|
|
$username = $user->username;
|
2007-02-20 17:03:36 +00:00
|
|
|
|
|
|
|
$textlib = textlib_get_instance();
|
2008-05-30 19:59:50 +00:00
|
|
|
$extusername = $textlib->convert($username, 'utf-8', $this->config->ldapencoding);
|
|
|
|
$extpassword = $textlib->convert($newpassword, 'utf-8', $this->config->ldapencoding);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-03-29 19:50:53 +00:00
|
|
|
switch ($this->config->passtype) {
|
|
|
|
case 'md5':
|
|
|
|
$extpassword = '{MD5}' . base64_encode(pack('H*', md5($extpassword)));
|
|
|
|
break;
|
|
|
|
case 'sha1':
|
|
|
|
$extpassword = '{SHA}' . base64_encode(pack('H*', sha1($extpassword)));
|
|
|
|
break;
|
|
|
|
case 'plaintext':
|
|
|
|
default:
|
|
|
|
break; // plaintext
|
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
$user_dn = $this->ldap_find_userdn($ldapconnection, $extusername);
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!$user_dn) {
|
2008-05-30 19:59:50 +00:00
|
|
|
error_log('LDAP Error in user_update_password(). No DN for: ' . $user->username);
|
2007-01-04 04:52:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($this->config->user_type) {
|
|
|
|
case 'edir':
|
|
|
|
//Change password
|
2007-02-20 17:03:36 +00:00
|
|
|
$result = ldap_modify($ldapconnection, $user_dn, array('userPassword' => $extpassword));
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!$result) {
|
|
|
|
error_log('LDAP Error in user_update_password(). Error code: '
|
|
|
|
. ldap_errno($ldapconnection) . '; Error string : '
|
|
|
|
. ldap_err2str(ldap_errno($ldapconnection)));
|
|
|
|
}
|
|
|
|
//Update password expiration time, grace logins count
|
|
|
|
$search_attribs = array($this->config->expireattr, 'passwordExpirationInterval','loginGraceLimit' );
|
2008-08-24 20:46:49 +00:00
|
|
|
$sr = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs);
|
2007-01-04 04:52:42 +00:00
|
|
|
if ($sr) {
|
|
|
|
$info=$this->ldap_get_entries($ldapconnection, $sr);
|
|
|
|
$newattrs = array();
|
|
|
|
if (!empty($info[0][$this->config->expireattr][0])) {
|
|
|
|
//Set expiration time only if passwordExpirationInterval is defined
|
|
|
|
if (!empty($info[0]['passwordExpirationInterval'][0])) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$expirationtime = time() + $info[0]['passwordExpirationInterval'][0];
|
2007-01-04 04:52:42 +00:00
|
|
|
$ldapexpirationtime = $this->ldap_unix2expirationtime($expirationtime);
|
|
|
|
$newattrs['passwordExpirationTime'] = $ldapexpirationtime;
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
//set gracelogin count
|
|
|
|
if (!empty($info[0]['loginGraceLimit'][0])) {
|
2007-02-20 17:03:36 +00:00
|
|
|
$newattrs['loginGraceRemaining']= $info[0]['loginGraceLimit'][0];
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
//Store attribute changes to ldap
|
|
|
|
$result = ldap_modify($ldapconnection, $user_dn, $newattrs);
|
|
|
|
if (!$result) {
|
|
|
|
error_log('LDAP Error in user_update_password() when modifying expirationtime and/or gracelogins. Error code: '
|
|
|
|
. ldap_errno($ldapconnection) . '; Error string : '
|
|
|
|
. ldap_err2str(ldap_errno($ldapconnection)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error_log('LDAP Error in user_update_password() when reading password expiration time. Error code: '
|
|
|
|
. ldap_errno($ldapconnection) . '; Error string : '
|
|
|
|
. ldap_err2str(ldap_errno($ldapconnection)));
|
2007-05-21 05:23:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ad':
|
|
|
|
// Passwords in Active Directory must be encoded as Unicode
|
|
|
|
// strings (UCS-2 Little Endian format) and surrounded with
|
|
|
|
// double quotes. See http://support.microsoft.com/?kbid=269190
|
|
|
|
if (!function_exists('mb_convert_encoding')) {
|
|
|
|
error_log ('You need the mbstring extension to change passwords in Active Directory');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$extpassword = mb_convert_encoding('"'.$extpassword.'"', "UCS-2LE", $this->config->ldapencoding);
|
|
|
|
$result = ldap_modify($ldapconnection, $user_dn, array('unicodePwd' => $extpassword));
|
|
|
|
if (!$result) {
|
2007-08-17 11:18:58 +00:00
|
|
|
error_log('LDAP Error in user_update_password(). Error code: '
|
2007-05-21 05:23:00 +00:00
|
|
|
. ldap_errno($ldapconnection) . '; Error string : '
|
|
|
|
. ldap_err2str(ldap_errno($ldapconnection)));
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
break;
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
default:
|
|
|
|
$usedconnection = &$ldapconnection;
|
|
|
|
// send ldap the password in cleartext, it will md5 it itself
|
2007-02-20 17:03:36 +00:00
|
|
|
$result = ldap_modify($ldapconnection, $user_dn, array('userPassword' => $extpassword));
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!$result) {
|
2007-02-20 17:03:36 +00:00
|
|
|
error_log('LDAP Error in user_update_password(). Error code: '
|
2007-01-04 04:52:42 +00:00
|
|
|
. ldap_errno($ldapconnection) . '; Error string : '
|
|
|
|
. ldap_err2str(ldap_errno($ldapconnection)));
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@ldap_close($ldapconnection);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//PRIVATE FUNCTIONS starts
|
|
|
|
//private functions are named as ldap_*
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns predefined usertypes
|
|
|
|
*
|
|
|
|
* @return array of predefined usertypes
|
|
|
|
*/
|
|
|
|
function ldap_suppported_usertypes() {
|
2007-02-20 17:03:36 +00:00
|
|
|
$types = array();
|
2007-01-04 04:52:42 +00:00
|
|
|
$types['edir']='Novell Edirectory';
|
|
|
|
$types['rfc2307']='posixAccount (rfc2307)';
|
|
|
|
$types['rfc2307bis']='posixAccount (rfc2307bis)';
|
|
|
|
$types['samba']='sambaSamAccount (v.3.0.7)';
|
2007-02-20 17:03:36 +00:00
|
|
|
$types['ad']='MS ActiveDirectory';
|
|
|
|
$types['default']=get_string('default');
|
2007-01-04 04:52:42 +00:00
|
|
|
return $types;
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
/**
|
2007-02-20 17:03:36 +00:00
|
|
|
* Initializes needed variables for ldap-module
|
2007-01-04 04:52:42 +00:00
|
|
|
*
|
|
|
|
* Uses names defined in ldap_supported_usertypes.
|
|
|
|
* $default is first defined as:
|
|
|
|
* $default['pseudoname'] = array(
|
|
|
|
* 'typename1' => 'value',
|
|
|
|
* 'typename2' => 'value'
|
|
|
|
* ....
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @return array of default values
|
|
|
|
*/
|
|
|
|
function ldap_getdefaults() {
|
|
|
|
$default['objectclass'] = array(
|
|
|
|
'edir' => 'User',
|
2007-02-20 17:03:36 +00:00
|
|
|
'rfc2307' => 'posixAccount',
|
|
|
|
'rfc2307bis' => 'posixAccount',
|
2007-01-04 04:52:42 +00:00
|
|
|
'samba' => 'sambaSamAccount',
|
|
|
|
'ad' => 'user',
|
|
|
|
'default' => '*'
|
|
|
|
);
|
|
|
|
$default['user_attribute'] = array(
|
|
|
|
'edir' => 'cn',
|
|
|
|
'rfc2307' => 'uid',
|
|
|
|
'rfc2307bis' => 'uid',
|
|
|
|
'samba' => 'uid',
|
|
|
|
'ad' => 'cn',
|
|
|
|
'default' => 'cn'
|
|
|
|
);
|
|
|
|
$default['memberattribute'] = array(
|
|
|
|
'edir' => 'member',
|
|
|
|
'rfc2307' => 'member',
|
|
|
|
'rfc2307bis' => 'member',
|
|
|
|
'samba' => 'member',
|
2007-02-20 17:03:36 +00:00
|
|
|
'ad' => 'member',
|
2007-01-04 04:52:42 +00:00
|
|
|
'default' => 'member'
|
|
|
|
);
|
|
|
|
$default['memberattribute_isdn'] = array(
|
|
|
|
'edir' => '1',
|
|
|
|
'rfc2307' => '0',
|
|
|
|
'rfc2307bis' => '1',
|
|
|
|
'samba' => '0', //is this right?
|
|
|
|
'ad' => '1',
|
|
|
|
'default' => '0'
|
|
|
|
);
|
|
|
|
$default['expireattr'] = array (
|
|
|
|
'edir' => 'passwordExpirationTime',
|
|
|
|
'rfc2307' => 'shadowExpire',
|
|
|
|
'rfc2307bis' => 'shadowExpire',
|
|
|
|
'samba' => '', //No support yet
|
2008-05-25 21:53:23 +00:00
|
|
|
'ad' => 'pwdLastSet',
|
2007-01-04 04:52:42 +00:00
|
|
|
'default' => ''
|
|
|
|
);
|
2007-02-20 17:03:36 +00:00
|
|
|
return $default;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return binaryfields of selected usertype
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function ldap_getbinaryfields () {
|
|
|
|
$binaryfields = array (
|
|
|
|
'edir' => array('guid'),
|
2007-02-20 17:03:36 +00:00
|
|
|
'rfc2307' => array(),
|
|
|
|
'rfc2307bis' => array(),
|
2007-01-04 04:52:42 +00:00
|
|
|
'samba' => array(),
|
|
|
|
'ad' => array(),
|
2007-02-20 17:03:36 +00:00
|
|
|
'default' => array()
|
2007-01-04 04:52:42 +00:00
|
|
|
);
|
|
|
|
if (!empty($this->config->user_type)) {
|
2007-02-20 17:03:36 +00:00
|
|
|
return $binaryfields[$this->config->user_type];
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $binaryfields['default'];
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ldap_isbinary ($field) {
|
2007-02-20 17:03:36 +00:00
|
|
|
if (empty($field)) {
|
|
|
|
return false;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
return array_search($field, $this->ldap_getbinaryfields());
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* take expirationtime and return it as unixseconds
|
2007-02-20 17:03:36 +00:00
|
|
|
*
|
2007-01-04 04:52:42 +00:00
|
|
|
* takes expriration timestamp as readed from ldap
|
|
|
|
* returns it as unix seconds
|
2007-02-20 17:03:36 +00:00
|
|
|
* depends on $this->config->user_type variable
|
2007-01-04 04:52:42 +00:00
|
|
|
*
|
|
|
|
* @param mixed time Time stamp readed from ldap as it is.
|
2007-05-30 08:54:52 +00:00
|
|
|
* @param string $ldapconnection Just needed for Active Directory.
|
|
|
|
* @param string $user_dn User distinguished name for the user we are checking password expiration (just needed for Active Directory).
|
2007-01-04 04:52:42 +00:00
|
|
|
* @return timestamp
|
|
|
|
*/
|
2007-05-30 08:54:52 +00:00
|
|
|
function ldap_expirationtime2unix ($time, $ldapconnection, $user_dn) {
|
2007-01-04 04:52:42 +00:00
|
|
|
$result = false;
|
|
|
|
switch ($this->config->user_type) {
|
|
|
|
case 'edir':
|
|
|
|
$yr=substr($time,0,4);
|
|
|
|
$mo=substr($time,4,2);
|
|
|
|
$dt=substr($time,6,2);
|
|
|
|
$hr=substr($time,8,2);
|
|
|
|
$min=substr($time,10,2);
|
|
|
|
$sec=substr($time,12,2);
|
2007-02-20 17:03:36 +00:00
|
|
|
$result = mktime($hr,$min,$sec,$mo,$dt,$yr);
|
2007-01-04 04:52:42 +00:00
|
|
|
break;
|
2007-07-01 15:42:36 +00:00
|
|
|
case 'rfc2307':
|
|
|
|
case 'rfc2307bis':
|
2007-01-04 04:52:42 +00:00
|
|
|
$result = $time * DAYSECS; //The shadowExpire contains the number of DAYS between 01/01/1970 and the actual expiration date
|
|
|
|
break;
|
2007-05-30 08:54:52 +00:00
|
|
|
case 'ad':
|
|
|
|
$result = $this->ldap_get_ad_pwdexpire($time, $ldapconnection, $user_dn);
|
|
|
|
break;
|
2007-02-20 17:03:36 +00:00
|
|
|
default:
|
2007-01-11 00:05:47 +00:00
|
|
|
print_error('auth_ldap_usertypeundefined', 'auth');
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* takes unixtime and return it formated for storing in ldap
|
|
|
|
*
|
|
|
|
* @param integer unix time stamp
|
|
|
|
*/
|
|
|
|
function ldap_unix2expirationtime($time) {
|
|
|
|
$result = false;
|
|
|
|
switch ($this->config->user_type) {
|
|
|
|
case 'edir':
|
2007-02-20 17:03:36 +00:00
|
|
|
$result=date('YmdHis', $time).'Z';
|
2007-01-04 04:52:42 +00:00
|
|
|
break;
|
2007-07-01 15:42:36 +00:00
|
|
|
case 'rfc2307':
|
|
|
|
case 'rfc2307bis':
|
2007-01-04 04:52:42 +00:00
|
|
|
$result = $time ; //Already in correct format
|
|
|
|
break;
|
2007-02-20 17:03:36 +00:00
|
|
|
default:
|
2007-01-11 00:05:47 +00:00
|
|
|
print_error('auth_ldap_usertypeundefined2', 'auth');
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/**
|
2007-01-04 04:52:42 +00:00
|
|
|
* checks if user belong to specific group(s)
|
2007-11-14 22:23:45 +00:00
|
|
|
* or is in a subtree.
|
2007-01-04 04:52:42 +00:00
|
|
|
*
|
2007-11-14 22:23:45 +00:00
|
|
|
* Returns true if user belongs group in grupdns string OR
|
|
|
|
* if the DN of the user is in a subtree pf the DN provided
|
|
|
|
* as "group"
|
2007-01-04 04:52:42 +00:00
|
|
|
*
|
|
|
|
* @param mixed $username username
|
|
|
|
* @param mixed $groupdns string of group dn separated by ;
|
|
|
|
*
|
|
|
|
*/
|
2007-02-20 17:03:36 +00:00
|
|
|
function ldap_isgroupmember($extusername='', $groupdns='') {
|
2007-01-04 04:52:42 +00:00
|
|
|
// Takes username and groupdn(s) , separated by ;
|
|
|
|
// Returns true if user is member of any given groups
|
|
|
|
|
|
|
|
$ldapconnection = $this->ldap_connect();
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-03-29 15:53:09 +00:00
|
|
|
if (empty($extusername) or empty($groupdns)) {
|
|
|
|
return false;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->config->memberattribute_isdn) {
|
2007-03-29 15:53:09 +00:00
|
|
|
$memberuser = $this->ldap_find_userdn($ldapconnection, $extusername);
|
|
|
|
} else {
|
|
|
|
$memberuser = $extusername;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-03-29 15:53:09 +00:00
|
|
|
|
|
|
|
if (empty($memberuser)) {
|
|
|
|
return false;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$groups = explode(";",$groupdns);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-03-29 15:53:09 +00:00
|
|
|
$result = false;
|
2007-01-04 04:52:42 +00:00
|
|
|
foreach ($groups as $group) {
|
|
|
|
$group = trim($group);
|
|
|
|
if (empty($group)) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-14 22:23:45 +00:00
|
|
|
|
|
|
|
// check cheaply if the user's DN sits in a subtree
|
|
|
|
// of the "group" DN provided. Granted, this isn't
|
|
|
|
// a proper LDAP group, but it's a popular usage.
|
|
|
|
if (strpos(strrev($memberuser), strrev($group))===0) {
|
|
|
|
$result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
//echo "Checking group $group for member $username\n";
|
2007-03-29 15:53:09 +00:00
|
|
|
$search = ldap_read($ldapconnection, $group, '('.$this->config->memberattribute.'='.$this->filter_addslashes($memberuser).')', array($this->config->memberattribute));
|
|
|
|
if (!empty($search) and ldap_count_entries($ldapconnection, $search)) {
|
|
|
|
$info = $this->ldap_get_entries($ldapconnection, $search);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (count($info) > 0 ) {
|
|
|
|
// user is member of group
|
|
|
|
$result = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-29 15:53:09 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* connects to ldap server
|
|
|
|
*
|
|
|
|
* Tries connect to specified ldap servers.
|
|
|
|
* Returns connection result or error.
|
|
|
|
*
|
|
|
|
* @return connection result
|
|
|
|
*/
|
|
|
|
function ldap_connect($binddn='',$bindpwd='') {
|
|
|
|
//Select bind password, With empty values use
|
|
|
|
//ldap_bind_* variables or anonymous bind if ldap_bind_* are empty
|
|
|
|
if ($binddn == '' and $bindpwd == '') {
|
|
|
|
if (!empty($this->config->bind_dn)) {
|
|
|
|
$binddn = $this->config->bind_dn;
|
|
|
|
}
|
|
|
|
if (!empty($this->config->bind_pw)) {
|
|
|
|
$bindpwd = $this->config->bind_pw;
|
|
|
|
}
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$urls = explode(";",$this->config->host_url);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
foreach ($urls as $server) {
|
|
|
|
$server = trim($server);
|
|
|
|
if (empty($server)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$connresult = ldap_connect($server);
|
|
|
|
//ldap_connect returns ALWAYS true
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($this->config->version)) {
|
|
|
|
ldap_set_option($connresult, LDAP_OPT_PROTOCOL_VERSION, $this->config->version);
|
|
|
|
}
|
|
|
|
|
2007-08-29 20:42:41 +00:00
|
|
|
// Fix MDL-10921
|
|
|
|
if ($this->config->user_type == 'ad') {
|
|
|
|
ldap_set_option($connresult, LDAP_OPT_REFERRALS, 0);
|
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($binddn)) {
|
|
|
|
//bind with search-user
|
2007-02-20 17:03:36 +00:00
|
|
|
//$debuginfo .= 'Using bind user'.$binddn.'and password:'.$bindpwd;
|
2007-01-04 04:52:42 +00:00
|
|
|
$bindresult=ldap_bind($connresult, $binddn,$bindpwd);
|
|
|
|
}
|
|
|
|
else {
|
2007-02-20 17:03:36 +00:00
|
|
|
//bind anonymously
|
2007-01-04 04:52:42 +00:00
|
|
|
$bindresult=@ldap_bind($connresult);
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($this->config->opt_deref)) {
|
|
|
|
ldap_set_option($connresult, LDAP_OPT_DEREF, $this->config->opt_deref);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($bindresult) {
|
|
|
|
return $connresult;
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$debuginfo .= "<br/>Server: '$server' <br/> Connection: '$connresult'<br/> Bind result: '$bindresult'</br>";
|
|
|
|
}
|
|
|
|
|
|
|
|
//If any of servers are alive we have already returned connection
|
2008-01-08 00:13:49 +00:00
|
|
|
print_error('auth_ldap_noconnect_all','auth','', $debuginfo);
|
2007-01-04 04:52:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retuns dn of username
|
|
|
|
*
|
|
|
|
* Search specified contexts for username and return user dn
|
|
|
|
* like: cn=username,ou=suborg,o=org
|
|
|
|
*
|
|
|
|
* @param mixed $ldapconnection $ldapconnection result
|
2007-02-20 17:03:36 +00:00
|
|
|
* @param mixed $username username (external encoding no slashes)
|
2007-01-04 04:52:42 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
function ldap_find_userdn ($ldapconnection, $extusername) {
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
//default return value
|
|
|
|
$ldap_user_dn = FALSE;
|
|
|
|
|
|
|
|
//get all contexts and look for first matching user
|
|
|
|
$ldap_contexts = explode(";",$this->config->contexts);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($this->config->create_context)) {
|
|
|
|
array_push($ldap_contexts, $this->config->create_context);
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
foreach ($ldap_contexts as $context) {
|
|
|
|
|
|
|
|
$context = trim($context);
|
|
|
|
if (empty($context)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->config->search_sub) {
|
|
|
|
//use ldap_search to find first user from subtree
|
2007-02-20 17:03:36 +00:00
|
|
|
$ldap_result = ldap_search($ldapconnection, $context, "(".$this->config->user_attribute."=".$this->filter_addslashes($extusername).")",array($this->config->user_attribute));
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//search only in this context
|
2007-02-20 17:03:36 +00:00
|
|
|
$ldap_result = ldap_list($ldapconnection, $context, "(".$this->config->user_attribute."=".$this->filter_addslashes($extusername).")",array($this->config->user_attribute));
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$entry = ldap_first_entry($ldapconnection,$ldap_result);
|
|
|
|
|
|
|
|
if ($entry) {
|
|
|
|
$ldap_user_dn = ldap_get_dn($ldapconnection, $entry);
|
|
|
|
break ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ldap_user_dn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retuns user attribute mappings between moodle and ldap
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ldap_attributes () {
|
|
|
|
$moodleattributes = array();
|
2008-04-08 05:53:12 +00:00
|
|
|
foreach ($this->userfields as $field) {
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($this->config->{"field_map_$field"})) {
|
|
|
|
$moodleattributes[$field] = $this->config->{"field_map_$field"};
|
|
|
|
if (preg_match('/,/',$moodleattributes[$field])) {
|
|
|
|
$moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-25 22:44:45 +00:00
|
|
|
$moodleattributes['username'] = $this->config->user_attribute;
|
2007-01-04 04:52:42 +00:00
|
|
|
return $moodleattributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return all usernames from ldap
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ldap_get_userlist($filter="*") {
|
|
|
|
/// returns all users from ldap servers
|
|
|
|
$fresult = array();
|
|
|
|
|
|
|
|
$ldapconnection = $this->ldap_connect();
|
|
|
|
|
|
|
|
if ($filter=="*") {
|
2008-08-24 20:46:49 +00:00
|
|
|
$filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')';
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$contexts = explode(";",$this->config->contexts);
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!empty($this->config->create_context)) {
|
|
|
|
array_push($contexts, $this->config->create_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($contexts as $context) {
|
|
|
|
|
|
|
|
$context = trim($context);
|
|
|
|
if (empty($context)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->config->search_sub) {
|
|
|
|
//use ldap_search to find first user from subtree
|
|
|
|
$ldap_result = ldap_search($ldapconnection, $context,$filter,array($this->config->user_attribute));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//search only in this context
|
|
|
|
$ldap_result = ldap_list($ldapconnection, $context,
|
|
|
|
$filter,
|
|
|
|
array($this->config->user_attribute));
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
$users = $this->ldap_get_entries($ldapconnection, $ldap_result);
|
|
|
|
|
|
|
|
//add found users to list
|
|
|
|
for ($i=0;$i<count($users);$i++) {
|
|
|
|
array_push($fresult, ($users[$i][$this->config->user_attribute][0]) );
|
|
|
|
}
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
return $fresult;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return entries from ldap
|
|
|
|
*
|
|
|
|
* Returns values like ldap_get_entries but is
|
|
|
|
* binary compatible and return all attributes as array
|
|
|
|
*
|
|
|
|
* @return array ldap-entries
|
|
|
|
*/
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
function ldap_get_entries($conn, $searchresult) {
|
|
|
|
//Returns values like ldap_get_entries but is
|
|
|
|
//binary compatible
|
|
|
|
$i=0;
|
|
|
|
$fresult=array();
|
|
|
|
$entry = ldap_first_entry($conn, $searchresult);
|
|
|
|
do {
|
|
|
|
$attributes = @ldap_get_attributes($conn, $entry);
|
|
|
|
for ($j=0; $j<$attributes['count']; $j++) {
|
|
|
|
$values = ldap_get_values_len($conn, $entry,$attributes[$j]);
|
|
|
|
if (is_array($values)) {
|
|
|
|
$fresult[$i][$attributes[$j]] = $values;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$fresult[$i][$attributes[$j]] = array($values);
|
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
|
|
|
$i++;
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
while ($entry = @ldap_next_entry($conn, $entry));
|
|
|
|
//were done
|
|
|
|
return ($fresult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this authentication plugin is 'internal'.
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* @return bool
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function is_internal() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this authentication plugin can change the user's
|
|
|
|
* password.
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* @return bool
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function can_change_password() {
|
2007-02-21 21:42:10 +00:00
|
|
|
return !empty($this->config->stdchangepassword) or !empty($this->config->changepasswordurl);
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
/**
|
2007-02-21 21:42:10 +00:00
|
|
|
* Returns the URL for changing the user's pw, or empty if the default can
|
2007-01-04 04:52:42 +00:00
|
|
|
* be used.
|
|
|
|
*
|
2007-02-20 17:03:36 +00:00
|
|
|
* @return string url
|
2007-01-04 04:52:42 +00:00
|
|
|
*/
|
|
|
|
function change_password_url() {
|
2007-02-20 17:03:36 +00:00
|
|
|
if (empty($this->config->stdchangepassword)) {
|
|
|
|
return $this->config->changepasswordurl;
|
|
|
|
} else {
|
2007-02-21 21:42:10 +00:00
|
|
|
return '';
|
2007-02-20 17:03:36 +00:00
|
|
|
}
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
2007-02-20 17:03:36 +00:00
|
|
|
|
2007-11-14 22:07:53 +00:00
|
|
|
/**
|
|
|
|
* Will get called before the login page is shown, if NTLM SSO
|
|
|
|
* is enabled, and the user is in the right network, we'll redirect
|
|
|
|
* to the magic NTLM page for SSO...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function loginpage_hook() {
|
2007-11-14 22:09:15 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2007-11-14 22:09:29 +00:00
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET' // Only on initial GET
|
|
|
|
// of loginpage
|
|
|
|
&&!empty($this->config->ntlmsso_enabled)// SSO enabled
|
2007-11-14 22:07:53 +00:00
|
|
|
&& !empty($this->config->ntlmsso_subnet)// have a subnet to test for
|
|
|
|
&& empty($_GET['authldap_skipntlmsso']) // haven't failed it yet
|
|
|
|
&& (isguestuser() || !isloggedin()) // guestuser or not-logged-in users
|
|
|
|
&& address_in_subnet($_SERVER['REMOTE_ADDR'],$this->config->ntlmsso_subnet)) {
|
|
|
|
redirect("{$CFG->wwwroot}/auth/ldap/ntlmsso_attempt.php");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To be called from a page running under NTLM's
|
|
|
|
* "Integrated Windows Authentication".
|
|
|
|
*
|
|
|
|
* If successful, it will set a special "cookie" (not an HTTP cookie!)
|
2007-11-14 22:12:57 +00:00
|
|
|
* in cache_flags under the "auth/ldap/ntlmsess" "plugin" and return true.
|
2007-11-14 22:07:53 +00:00
|
|
|
* The "cookie" will be picked up by ntlmsso_finish() to complete the
|
|
|
|
* process.
|
|
|
|
*
|
|
|
|
* On failure it will return false for the caller to display an appropriate
|
2007-11-14 22:08:11 +00:00
|
|
|
* error message (probably saying that Integrated Windows Auth isn't enabled!)
|
2007-11-14 22:07:53 +00:00
|
|
|
*
|
|
|
|
* NOTE that this code will execute under the OS user credentials,
|
|
|
|
* so we MUST avoid dealing with files -- such as session files.
|
2008-06-25 17:31:23 +00:00
|
|
|
* (The caller should define('NO_MOODLE_COOKIES', true) before including config.php)
|
2007-11-14 22:07:53 +00:00
|
|
|
*
|
|
|
|
*/
|
2007-11-14 22:08:11 +00:00
|
|
|
function ntlmsso_magic($sesskey) {
|
|
|
|
if (isset($_SERVER['REMOTE_USER']) && !empty($_SERVER['REMOTE_USER'])) {
|
|
|
|
$username = $_SERVER['REMOTE_USER'];
|
|
|
|
$username = substr(strrchr($username, '\\'), 1); //strip domain info
|
2007-11-14 22:11:35 +00:00
|
|
|
$username = moodle_strtolower($username); //compatibility hack
|
2007-11-14 22:12:57 +00:00
|
|
|
set_cache_flag('auth/ldap/ntlmsess', $sesskey, $username, AUTH_NTLMTIMEOUT);
|
|
|
|
return true;
|
2007-11-14 22:08:11 +00:00
|
|
|
}
|
|
|
|
return false;
|
2007-11-14 22:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-14 22:08:11 +00:00
|
|
|
* Find the session set by ntlmsso_magic(), validate it and
|
|
|
|
* call authenticate_user_login() to authenticate the user through
|
|
|
|
* the auth machinery.
|
|
|
|
*
|
|
|
|
* It is complemented by a similar check in user_login().
|
2007-11-14 22:07:53 +00:00
|
|
|
*
|
2007-11-14 22:08:11 +00:00
|
|
|
* If it succeeds, it never returns.
|
|
|
|
*
|
2007-11-14 22:07:53 +00:00
|
|
|
*/
|
|
|
|
function ntlmsso_finish() {
|
2007-11-14 22:17:58 +00:00
|
|
|
global $CFG, $USER, $SESSION;
|
2007-11-14 22:09:15 +00:00
|
|
|
|
2007-11-14 22:12:57 +00:00
|
|
|
$key = sesskey();
|
2007-11-19 02:43:16 +00:00
|
|
|
$cf = get_cache_flags('auth/ldap/ntlmsess');
|
2007-11-19 02:43:34 +00:00
|
|
|
if (!isset($cf[$key]) || $cf[$key] === '') {
|
2007-11-14 22:12:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$username = $cf[$key];
|
|
|
|
// Here we want to trigger the whole authentication machinery
|
|
|
|
// to make sure no step is bypassed...
|
|
|
|
$user = authenticate_user_login($username, $key);
|
|
|
|
if ($user) {
|
|
|
|
add_to_log(SITEID, 'user', 'login', "view.php?id=$USER->id&course=".SITEID,
|
|
|
|
$user->id, 0, $user->id);
|
|
|
|
$USER = complete_user_login($user);
|
|
|
|
|
|
|
|
// Cleanup the key to prevent reuse...
|
|
|
|
// and to allow re-logins with normal credentials
|
|
|
|
unset_cache_flag('auth/ldap/ntlmsess', $key);
|
|
|
|
|
|
|
|
/// Redirection
|
|
|
|
if (user_not_fully_set_up($USER)) {
|
|
|
|
$urltogo = $CFG->wwwroot.'/user/edit.php';
|
|
|
|
// We don't delete $SESSION->wantsurl yet, so we get there later
|
|
|
|
} else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
|
|
|
|
$urltogo = $SESSION->wantsurl; /// Because it's an address in this site
|
|
|
|
unset($SESSION->wantsurl);
|
|
|
|
} else {
|
|
|
|
// no wantsurl stored or external - go to homepage
|
|
|
|
$urltogo = $CFG->wwwroot.'/';
|
|
|
|
unset($SESSION->wantsurl);
|
2007-11-14 22:08:11 +00:00
|
|
|
}
|
2007-11-14 22:12:57 +00:00
|
|
|
redirect($urltogo);
|
2007-11-14 22:08:11 +00:00
|
|
|
}
|
2007-11-14 22:12:57 +00:00
|
|
|
// Should never reach here.
|
2007-11-14 22:08:11 +00:00
|
|
|
return false;
|
2007-11-14 22:07:53 +00:00
|
|
|
}
|
|
|
|
|
2007-03-22 12:27:52 +00:00
|
|
|
/**
|
|
|
|
* Sync roles for this user
|
|
|
|
*
|
|
|
|
* @param $user object user object (without system magic quotes)
|
|
|
|
*/
|
|
|
|
function sync_roles($user) {
|
|
|
|
$iscreator = $this->iscreator($user->username);
|
|
|
|
if ($iscreator === null) {
|
|
|
|
return; //nothing to sync - creators not configured
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($roles = get_roles_with_capability('moodle/legacy:coursecreator', CAP_ALLOW)) {
|
|
|
|
$creatorrole = array_shift($roles); // We can only use one, let's use the first one
|
|
|
|
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
|
|
|
|
|
|
|
|
if ($iscreator) { // Following calls will not create duplicates
|
|
|
|
role_assign($creatorrole->id, $user->id, 0, $systemcontext->id, 0, 0, 0, 'ldap');
|
|
|
|
} else {
|
|
|
|
//unassign only if previously assigned by this plugin!
|
|
|
|
role_unassign($creatorrole->id, $user->id, 0, $systemcontext->id, 'ldap');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
/**
|
|
|
|
* Prints a form for configuring this authentication plugin.
|
|
|
|
*
|
|
|
|
* This function is called from admin/auth.php, and outputs a full page with
|
|
|
|
* a form for configuring this plugin.
|
|
|
|
*
|
|
|
|
* @param array $page An object containing all the data for this page.
|
|
|
|
*/
|
2007-02-20 17:03:36 +00:00
|
|
|
function config_form($config, $err, $user_fields) {
|
|
|
|
include 'config.html';
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes and stores configuration data for this authentication plugin.
|
|
|
|
*/
|
|
|
|
function process_config($config) {
|
|
|
|
// set to defaults if undefined
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->host_url))
|
2007-01-04 04:52:42 +00:00
|
|
|
{ $config->host_url = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (empty($config->ldapencoding))
|
|
|
|
{ $config->ldapencoding = 'utf-8'; }
|
|
|
|
if (!isset($config->contexts))
|
2007-01-04 04:52:42 +00:00
|
|
|
{ $config->contexts = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->user_type))
|
|
|
|
{ $config->user_type = 'default'; }
|
|
|
|
if (!isset($config->user_attribute))
|
2007-01-04 04:52:42 +00:00
|
|
|
{ $config->user_attribute = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->search_sub))
|
2007-01-04 04:52:42 +00:00
|
|
|
{ $config->search_sub = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->opt_deref))
|
2007-01-04 04:52:42 +00:00
|
|
|
{ $config->opt_deref = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->preventpassindb))
|
|
|
|
{ $config->preventpassindb = 0; }
|
|
|
|
if (!isset($config->bind_dn))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->bind_dn = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->bind_pw))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->bind_pw = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->version))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->version = '2'; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->objectclass))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->objectclass = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->memberattribute))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->memberattribute = ''; }
|
2007-03-29 15:53:09 +00:00
|
|
|
if (!isset($config->memberattribute_isdn))
|
|
|
|
{$config->memberattribute_isdn = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->creators))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->creators = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->create_context))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->create_context = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->expiration))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->expiration = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->expiration_warning))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->expiration_warning = '10'; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->expireattr))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->expireattr = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->gracelogins))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->gracelogins = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->graceattr))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->graceattr = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->auth_user_create))
|
2007-01-04 04:52:42 +00:00
|
|
|
{$config->auth_user_create = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->forcechangepassword))
|
|
|
|
{$config->forcechangepassword = 0; }
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!isset($config->stdchangepassword))
|
2008-08-07 17:10:28 +00:00
|
|
|
{$config->stdchangepassword = 0; }
|
2007-03-29 19:50:53 +00:00
|
|
|
if (!isset($config->passtype))
|
|
|
|
{$config->passtype = 'plaintext'; }
|
2007-01-04 04:52:42 +00:00
|
|
|
if (!isset($config->changepasswordurl))
|
|
|
|
{$config->changepasswordurl = ''; }
|
2007-02-20 17:03:36 +00:00
|
|
|
if (!isset($config->removeuser))
|
2008-03-26 01:35:04 +00:00
|
|
|
{$config->removeuser = AUTH_REMOVEUSER_KEEP; }
|
2007-11-14 22:07:16 +00:00
|
|
|
if (!isset($config->ntlmsso_enabled))
|
|
|
|
{$config->ntlmsso_enabled = 0; }
|
|
|
|
if (!isset($config->ntlmsso_subnet))
|
|
|
|
{$config->ntlmsso_subnet = ''; }
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
// save settings
|
|
|
|
set_config('host_url', $config->host_url, 'auth/ldap');
|
2007-02-20 17:03:36 +00:00
|
|
|
set_config('ldapencoding', $config->ldapencoding, 'auth/ldap');
|
|
|
|
set_config('host_url', $config->host_url, 'auth/ldap');
|
2007-01-04 04:52:42 +00:00
|
|
|
set_config('contexts', $config->contexts, 'auth/ldap');
|
|
|
|
set_config('user_type', $config->user_type, 'auth/ldap');
|
|
|
|
set_config('user_attribute', $config->user_attribute, 'auth/ldap');
|
|
|
|
set_config('search_sub', $config->search_sub, 'auth/ldap');
|
|
|
|
set_config('opt_deref', $config->opt_deref, 'auth/ldap');
|
|
|
|
set_config('preventpassindb', $config->preventpassindb, 'auth/ldap');
|
|
|
|
set_config('bind_dn', $config->bind_dn, 'auth/ldap');
|
|
|
|
set_config('bind_pw', $config->bind_pw, 'auth/ldap');
|
|
|
|
set_config('version', $config->version, 'auth/ldap');
|
2008-08-24 20:46:49 +00:00
|
|
|
set_config('objectclass', trim($config->objectclass), 'auth/ldap');
|
2007-01-04 04:52:42 +00:00
|
|
|
set_config('memberattribute', $config->memberattribute, 'auth/ldap');
|
2007-03-29 15:53:09 +00:00
|
|
|
set_config('memberattribute_isdn', $config->memberattribute_isdn, 'auth/ldap');
|
2007-01-04 04:52:42 +00:00
|
|
|
set_config('creators', $config->creators, 'auth/ldap');
|
|
|
|
set_config('create_context', $config->create_context, 'auth/ldap');
|
|
|
|
set_config('expiration', $config->expiration, 'auth/ldap');
|
|
|
|
set_config('expiration_warning', $config->expiration_warning, 'auth/ldap');
|
|
|
|
set_config('expireattr', $config->expireattr, 'auth/ldap');
|
|
|
|
set_config('gracelogins', $config->gracelogins, 'auth/ldap');
|
|
|
|
set_config('graceattr', $config->graceattr, 'auth/ldap');
|
|
|
|
set_config('auth_user_create', $config->auth_user_create, 'auth/ldap');
|
|
|
|
set_config('forcechangepassword', $config->forcechangepassword, 'auth/ldap');
|
|
|
|
set_config('stdchangepassword', $config->stdchangepassword, 'auth/ldap');
|
2007-03-29 19:50:53 +00:00
|
|
|
set_config('passtype', $config->passtype, 'auth/ldap');
|
2007-01-04 04:52:42 +00:00
|
|
|
set_config('changepasswordurl', $config->changepasswordurl, 'auth/ldap');
|
2007-02-20 17:03:36 +00:00
|
|
|
set_config('removeuser', $config->removeuser, 'auth/ldap');
|
2007-11-14 22:07:16 +00:00
|
|
|
set_config('ntlmsso_enabled', (int)$config->ntlmsso_enabled, 'auth/ldap');
|
|
|
|
set_config('ntlmsso_subnet', $config->ntlmsso_subnet, 'auth/ldap');
|
2007-01-04 04:52:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-02-20 17:03:36 +00:00
|
|
|
/**
|
|
|
|
* Quote control characters in texts used in ldap filters - see rfc2254.txt
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
function filter_addslashes($text) {
|
|
|
|
$text = str_replace('\\', '\\5c', $text);
|
|
|
|
$text = str_replace(array('*', '(', ')', "\0"),
|
|
|
|
array('\\2a', '\\28', '\\29', '\\00'), $text);
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quote control characters in quoted "texts" used in ldap
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
function ldap_addslashes($text) {
|
|
|
|
$text = str_replace('\\', '\\\\', $text);
|
|
|
|
$text = str_replace(array('"', "\0"),
|
|
|
|
array('\\"', '\\00'), $text);
|
|
|
|
return $text;
|
|
|
|
}
|
2007-05-30 08:54:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get password expiration time for a given user from Active Directory
|
|
|
|
*
|
|
|
|
* @param string $pwdlastset The time last time we changed the password.
|
|
|
|
* @param resource $lcapconn The open LDAP connection.
|
|
|
|
* @param string $user_dn The distinguished name of the user we are checking.
|
|
|
|
*
|
|
|
|
* @return string $unixtime
|
|
|
|
*/
|
|
|
|
function ldap_get_ad_pwdexpire($pwdlastset, $ldapconn, $user_dn){
|
|
|
|
define ('ROOTDSE', '');
|
|
|
|
// UF_DONT_EXPIRE_PASSWD value taken from MSDN directly
|
|
|
|
define ('UF_DONT_EXPIRE_PASSWD', 0x00010000);
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:54:52 +00:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!function_exists('bcsub')) {
|
|
|
|
error_log ('You need the BCMath extension to use grace logins with Active Directory');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If UF_DONT_EXPIRE_PASSWD flag is set in user's
|
|
|
|
// userAccountControl attribute, the password doesn't expire.
|
2008-08-24 20:46:49 +00:00
|
|
|
$sr = ldap_read($ldapconn, $user_dn, '(objectClass=*)',
|
2007-05-30 08:54:52 +00:00
|
|
|
array('userAccountControl'));
|
|
|
|
if (!$sr) {
|
|
|
|
error_log("ldap: error getting userAccountControl for $user_dn");
|
|
|
|
// don't expire password, as we are not sure it has to be
|
|
|
|
// expired or not.
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:54:52 +00:00
|
|
|
$info = $this->ldap_get_entries($ldapconn, $sr);
|
|
|
|
$useraccountcontrol = $info[0]['userAccountControl'][0];
|
|
|
|
if ($useraccountcontrol & UF_DONT_EXPIRE_PASSWD) {
|
|
|
|
// password doesn't expire.
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:54:52 +00:00
|
|
|
// If pwdLastSet is zero, the user must change his/her password now
|
|
|
|
// (unless UF_DONT_EXPIRE_PASSWD flag is set, but we already
|
|
|
|
// tested this above)
|
|
|
|
if ($pwdlastset === '0') {
|
|
|
|
// password has expired
|
|
|
|
return -1;
|
|
|
|
}
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:54:52 +00:00
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// Password expiration time in Active Directory is the composition of
|
|
|
|
// two values:
|
|
|
|
//
|
|
|
|
// - User's pwdLastSet attribute, that stores the last time
|
|
|
|
// the password was changed.
|
|
|
|
//
|
|
|
|
// - Domain's maxPwdAge attribute, that sets how long
|
|
|
|
// passwords last in this domain.
|
|
|
|
//
|
|
|
|
// We already have the first value (passed in as a parameter). We
|
|
|
|
// need to get the second one. As we don't know the domain DN, we
|
|
|
|
// have to query rootDSE's defaultNamingContext attribute to get
|
|
|
|
// it. Then we have to query that DN's maxPwdAge attribute to get
|
|
|
|
// the real value.
|
|
|
|
//
|
|
|
|
// Once we have both values, we just need to combine them. But MS
|
|
|
|
// chose to use a different base and unit for time measurements.
|
|
|
|
// So we need to convert the values to Unix timestamps (see
|
|
|
|
// details below).
|
|
|
|
// ----------------------------------------------------------------
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2008-08-24 20:46:49 +00:00
|
|
|
$sr = ldap_read($ldapconn, ROOTDSE, '(objectClass=*)',
|
2007-05-30 08:54:52 +00:00
|
|
|
array('defaultNamingContext'));
|
|
|
|
if (!$sr) {
|
|
|
|
error_log("ldap: error querying rootDSE for Active Directory");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:54:52 +00:00
|
|
|
$info = $this->ldap_get_entries($ldapconn, $sr);
|
|
|
|
$domaindn = $info[0]['defaultNamingContext'][0];
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2008-08-24 20:46:49 +00:00
|
|
|
$sr = ldap_read ($ldapconn, $domaindn, '(objectClass=*)',
|
2007-05-30 08:54:52 +00:00
|
|
|
array('maxPwdAge'));
|
|
|
|
$info = $this->ldap_get_entries($ldapconn, $sr);
|
|
|
|
$maxpwdage = $info[0]['maxPwdAge'][0];
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// MSDN says that "pwdLastSet contains the number of 100 nanosecond
|
|
|
|
// intervals since January 1, 1601 (UTC), stored in a 64 bit integer".
|
|
|
|
//
|
|
|
|
// According to Perl's Date::Manip, the number of seconds between
|
|
|
|
// this date and Unix epoch is 11644473600. So we have to
|
|
|
|
// substract this value to calculate a Unix time, once we have
|
|
|
|
// scaled pwdLastSet to seconds. This is the script used to
|
|
|
|
// calculate the value shown above:
|
|
|
|
//
|
|
|
|
// #!/usr/bin/perl -w
|
|
|
|
//
|
|
|
|
// use Date::Manip;
|
|
|
|
//
|
|
|
|
// $date1 = ParseDate ("160101010000 UTC");
|
|
|
|
// $date2 = ParseDate ("197001010000 UTC");
|
|
|
|
// $delta = DateCalc($date1, $date2, \$err);
|
|
|
|
// $secs = Delta_Format($delta, 0, "%st");
|
|
|
|
// print "$secs \n";
|
|
|
|
//
|
|
|
|
// MSDN also says that "maxPwdAge is stored as a large integer that
|
|
|
|
// represents the number of 100 nanosecond intervals from the time
|
|
|
|
// the password was set before the password expires." We also need
|
|
|
|
// to scale this to seconds. Bear in mind that this value is stored
|
|
|
|
// as a _negative_ quantity (at least in my AD domain).
|
|
|
|
//
|
|
|
|
// As a last remark, if the low 32 bits of maxPwdAge are equal to 0,
|
|
|
|
// the maximum password age in the domain is set to 0, which means
|
2007-08-17 11:18:58 +00:00
|
|
|
// passwords do not expire (see
|
2007-05-30 08:54:52 +00:00
|
|
|
// http://msdn2.microsoft.com/en-us/library/ms974598.aspx)
|
|
|
|
//
|
|
|
|
// As the quantities involved are too big for PHP integers, we
|
|
|
|
// need to use BCMath functions to work with arbitrary precision
|
|
|
|
// numbers.
|
|
|
|
// ----------------------------------------------------------------
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:54:52 +00:00
|
|
|
|
|
|
|
// If the low order 32 bits are 0, then passwords do not expire in
|
|
|
|
// the domain. Just do '$maxpwdage mod 2^32' and check the result
|
|
|
|
// (2^32 = 4294967296)
|
|
|
|
if (bcmod ($maxpwdage, 4294967296) === '0') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add up pwdLastSet and maxPwdAge to get password expiration
|
|
|
|
// time, in MS time units. Remember maxPwdAge is stored as a
|
|
|
|
// _negative_ quantity, so we need to substract it in fact.
|
|
|
|
$pwdexpire = bcsub ($pwdlastset, $maxpwdage);
|
2007-08-17 11:18:58 +00:00
|
|
|
|
2007-05-30 08:54:52 +00:00
|
|
|
// Scale the result to convert it to Unix time units and return
|
|
|
|
// that value.
|
|
|
|
return bcsub( bcdiv($pwdexpire, '10000000'), '11644473600');
|
|
|
|
}
|
|
|
|
|
2007-01-04 04:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|