/** * @param $a_username * @param $password * @param bool $isChallengeResponse * @return bool|void * @throws ilLDAPQueryException */ function fetchData($a_username, $password, $isChallengeResponse = false) { /** * @var $ilDB ilDB * @var $ilSetting ilSetting * @var $rbacadmin ilRbacAdmin */ global $ilDB, $ilSetting, $rbacadmin; $settings = new ilSetting('apache_auth'); if (!$settings->get('apache_enable_auth')) { return false; } if (!$settings->get('apache_auth_indicator_name') || !$settings->get('apache_auth_indicator_value')) { return false; } if (!ilUtil::isLogin($a_username)) { return false; } if ($a_username == 'anonymous' && $password == 'anonymous') { $query = 'SELECT * FROM usr_data WHERE login = %s'; $qres = $ilDB->queryF($query, array('text'), array($a_username)); $userRow = $ilDB->fetchAssoc($qres); if (is_array($userRow) && $userRow['usr_id']) { // user as a local account... // fetch logindata $this->activeUser = $userRow['login']; foreach ($userRow as $key => $value) { if ($key == $this->options['passwordcol'] || $key == $this->options['usernamecol']) { continue; } // Use reference to the auth object if exists // This is because the auth session variable can change so a static call to setAuthData does not make sense $this->_auth_obj->setAuthData($key, $value); } $this->_auth_obj->setAuth($userRow['login']); return true; } return false; } if (!$_SESSION['login_invalid'] && in_array($_SERVER[$settings->get('apache_auth_indicator_name')], array_filter(array_map('trim', str_getcsv($settings->get('apache_auth_indicator_value')))))) { // we have a valid apache auth $list = array($ilSetting->get('auth_mode')); // Respect the auth method sequence include_once './Services/Authentication/classes/class.ilAuthModeDetermination.php'; $det = ilAuthModeDetermination::_getInstance(); if (!$det->isManualSelection() && $det->getCountActiveAuthModes() > 1) { $list = array(); foreach (ilAuthModeDetermination::_getInstance()->getAuthModeSequence() as $auth_mode) { $list[] = $auth_mode; } } foreach ($list as $auth_mode) { if (AUTH_LDAP == $auth_mode) { // if no local user has been found AND ldap lookup is enabled if ($settings->get('apache_enable_ldap')) { include_once 'Services/LDAP/classes/class.ilLDAPServer.php'; $this->server = new ilLDAPServer(ilLDAPServer::_getFirstActiveServer()); $this->server->doConnectionCheck(); $config = $this->server->toPearAuthArray(); $query = new ilLDAPQuery($this->server); $query->bind(); $ldapUser = $query->fetchUser($a_username); if ($ldapUser && $ldapUser[$a_username] && $ldapUser[$a_username][$config['userattr']] == $a_username) { $ldapUser[$a_username]['ilInternalAccount'] = ilObjUser::_checkExternalAuthAccount("ldap", $a_username); $user_data = $ldapUser[$a_username]; //array_change_key_case($a_auth->getAuthData(),CASE_LOWER); if ($this->server->enabledSyncOnLogin()) { if (!$user_data['ilInternalAccount'] && $this->server->isAccountMigrationEnabled() && !self::$force_creation) { $this->_auth_obj->logout(); $_SESSION['tmp_auth_mode'] = 'ldap'; $_SESSION['tmp_external_account'] = $a_username; $_SESSION['tmp_pass'] = $_POST['password']; include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php'; $roles = ilLDAPRoleAssignmentRules::getAssignmentsForCreation($a_username, $user_data); $_SESSION['tmp_roles'] = array(); foreach ($roles as $info) { if ($info['action'] == ilLDAPRoleAssignmentRules::ROLE_ACTION_ASSIGN) { $_SESSION['tmp_roles'][] = $info['id']; } } ilUtil::redirect('ilias.php?baseClass=ilStartUpGUI&cmdClass=ilstartupgui&cmd=showAccountMigration'); } if ($this->updateRequired($a_username)) { $this->initLDAPAttributeToUser(); $this->ldap_attr_to_user->setUserData($ldapUser); $this->ldap_attr_to_user->refresh(); $user_data['ilInternalAccount'] = ilObjUser::_checkExternalAuthAccount("ldap", $a_username); } else { // User exists and no update required $user_data['ilInternalAccount'] = ilObjUser::_checkExternalAuthAccount("ldap", $a_username); } } if ($user_data['ilInternalAccount']) { $this->_auth_obj->setAuth($user_data['ilInternalAccount']); $this->_auth_obj->username = $user_data['ilInternalAccount']; return true; } } } } else { if (AUTH_APACHE != $auth_mode && $settings->get('apache_enable_local')) { $condition = ''; if ($ilSetting->get("auth_mode") && $ilSetting->get("auth_mode") == 'ldap') { $condition = " AND auth_mode != " . $ilDB->quote('default', 'text') . " "; } $query = "SELECT * FROM usr_data WHERE login = %s AND auth_mode != %s {$condition}"; $qres = $ilDB->queryF($query, array('text', 'text'), array($a_username, 'ldap')); $userRow = $ilDB->fetchAssoc($qres); if (is_array($userRow) && $userRow['usr_id']) { // user as a local account... // fetch logindata $this->activeUser = $userRow['login']; foreach ($userRow as $key => $value) { if ($key == $this->options['passwordcol'] || $key == $this->options['usernamecol']) { continue; } // Use reference to the auth object if exists // This is because the auth session variable can change so a static call to setAuthData does not make sense $this->_auth_obj->setAuthData($key, $value); } $this->_auth_obj->setAuth($userRow['login']); return true; } } } } if ($settings->get('apache_enable_local') && $settings->get('apache_local_autocreate')) { if ($_GET['r']) { $_SESSION['profile_complete_redirect'] = $_GET['r']; } $user = new ilObjUser(); $user->setLogin($a_username); $user->setExternalAccount($a_username); $user->setProfileIncomplete(true); $user->create(); $user->setAuthMode('apache'); // set a timestamp for last_password_change // this ts is needed by ilSecuritySettings $user->setLastPasswordChangeTS(time()); $user->setTimeLimitUnlimited(1); $user->setActive(1); //insert user data in table user_data $user->saveAsNew(); $user->writePrefs(); $rbacadmin->assignUser($settings->get('apache_default_role', 4), $user->getId(), true); return true; } } else { if (defined('IL_CERT_SSO') && IL_CERT_SSO) { define('APACHE_ERRORCODE', AUTH_APACHE_FAILED); } } return false; }
/** * Create user account * @param type $a_person_id */ private function createMember($a_person_id) { try { include_once './Services/LDAP/classes/class.ilLDAPServer.php'; $server = ilLDAPServer::getInstanceByServerId(ilLDAPServer::_getFirstActiveServer()); $server->doConnectionCheck(); include_once './Services/LDAP/classes/class.ilLDAPQuery.php'; $query = new ilLDAPQuery($server); $query->bind(IL_LDAP_BIND_DEFAULT); $users = $query->fetchUser($a_person_id); if ($users) { include_once './Services/LDAP/classes/class.ilLDAPAttributeToUser.php'; $xml = new ilLDAPAttributeToUser($server); $xml->setNewUserAuthMode($server->getAuthenticationMappingKey()); $xml->setUserData($users); $xml->refresh(); } } catch (ilLDAPQueryException $exc) { $this->log->write($exc->getMessage()); } }
/** * Check ldap connection and do a fallback to the next server * if no connection is possible. * * @access public * */ public function doConnectionCheck() { global $ilLog; include_once 'Services/LDAP/classes/class.ilLDAPQuery.php'; foreach (array_merge(array(0 => $this->url), $this->fallback_urls) as $url) { try { // Need to do a full bind, since openldap return valid connection links for invalid hosts $query = new ilLDAPQuery($this, $url); $query->bind(IL_LDAP_BIND_TEST); $this->url = $url; $ilLog->write(__METHOD__ . ': Using url: ' . $url . '.'); return TRUE; } catch (ilLDAPQueryException $exc) { $this->rotateFallbacks(); $ilLog->write(__METHOD__ . ': Cannot connect to LDAP server: ' . $url . ' ' . $exc->getCode() . ': ' . $exc->getMessage()); } } $ilLog->write(__METHOD__ . ': No valid LDAP server found.'); return FALSE; }
/** * Get LDAPQueryInstance * * @access private * @param * @throws ilLDAPQueryException */ private function getLDAPQueryInstance($a_server_id, $a_url) { include_once 'Services/LDAP/classes/class.ilLDAPQuery.php'; if (array_key_exists($a_server_id, $this->query) and array_key_exists($a_url, $this->query[$a_server_id]) and is_object($this->query[$a_server_id][$a_url])) { return $this->query[$a_server_id][$a_url]; } try { $tmp_query = new ilLDAPQuery($this->servers[$a_server_id], $a_url); $tmp_query->bind(IL_LDAP_BIND_ADMIN); } catch (ilLDAPQueryException $exc) { throw $exc; } return $this->query[$a_server_id][$a_url] = $tmp_query; }
/** * Check ldap connection and do a fallback to the next server * if no connection is possible. * * @access public * */ public function doConnectionCheck() { global $ilLog; include_once 'Services/LDAP/classes/class.ilLDAPQuery.php'; foreach (array_merge(array(0 => $this->url), $this->fallback_urls) as $url) { try { // Need to do a full bind, since openldap return valid connection links for invalid hosts $query = new ilLDAPQuery($this, $url); $query->bind(); $this->url = $url; $ilLog->write(__METHOD__ . ': Using url: ' . $url . '.'); return true; } catch (ilLDAPQueryException $exc) { $ilLog->write(__METHOD__ . ': Cannot connect to LDAP server: ' . $url . '. Trying fallback...'); } } $ilLog->write(__METHOD__ . ': No valid LDAP server found.'); return false; }
/** * Check if user is member of specific group * * @access private * @param array user data * @param array user_data * */ private function isGroupMember($a_user_data) { global $ilLog; if ($this->isMemberAttributeDN()) { $user_cmp = $a_user_data['dn']; } else { $user_cmp = $a_user_data['ilExternalAccount']; } include_once 'Services/LDAP/classes/class.ilLDAPQuery.php'; include_once 'Services/LDAP/classes/class.ilLDAPServer.php'; $server = ilLDAPServer::getInstanceByServerId(ilLDAPServer::_getFirstActiveServer()); try { $query = new ilLDAPQuery($server); $query->bind(); $res = $query->query($this->getDN(), sprintf('(%s=%s)', $this->getMemberAttribute(), $user_cmp), IL_LDAP_SCOPE_BASE, array('dn')); return $res->numRows() ? true : false; } catch (ilLDAPQueryException $e) { $ilLog->write(__METHOD__ . ': Caught Exception: ' . $e->getMessage()); return false; } }