2 namespace TYPO3\CMS\Sv
;
4 /***************************************************************
7 * (c) 2004-2013 René Fritz <r.fritz@colorcube.de>
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
31 * Authentication services class
33 * @author René Fritz <r.fritz@colorcube.de>
35 class AuthenticationService
extends \TYPO3\CMS\Sv\AbstractAuthenticationService
{
38 * Process the submitted credentials.
39 * In this case hash the clear text password if it has been submitted.
41 * @param array $loginData Credentials that are submitted and potentially modified by other services
42 * @param string $passwordTransmissionStrategy Keyword of how the password has been hashed or encrypted before submission
45 public function processLoginData(array &$loginData, $passwordTransmissionStrategy) {
47 // Processing data according to the state it was submitted in.
48 switch ($passwordTransmissionStrategy) {
50 $loginData['uident_text'] = $loginData['uident'];
53 $loginData['uident_text'] = '';
54 $loginData['uident_challenged'] = $loginData['uident'];
55 $loginData['uident_superchallenged'] = '';
57 case 'superchallenged':
58 $loginData['uident_text'] = '';
59 $loginData['uident_challenged'] = '';
60 $loginData['uident_superchallenged'] = $loginData['uident'];
65 if (!empty($loginData['uident_text'])) {
66 $loginData['uident_challenged'] = (string) md5(($loginData['uname'] . ':' . $loginData['uident_text'] . ':' . $loginData['chalvalue']));
67 $loginData['uident_superchallenged'] = (string) md5(($loginData['uname'] . ':' . md5($loginData['uident_text']) . ':' . $loginData['chalvalue']));
74 * Find a user (eg. look up the user record in database when a login is sent)
76 * @return mixed User array or FALSE
77 * @todo Define visibility
79 public function getUser() {
81 if ($this->login
['status'] == 'login') {
82 if ($this->login
['uident']) {
83 $user = $this->fetchUserRecord($this->login
['uname']);
84 if (!is_array($user)) {
85 // Failed login attempt (no username found)
86 $this->writelog(255, 3, 3, 2, 'Login-attempt from %s (%s), username \'%s\' not found!!', array($this->authInfo
['REMOTE_ADDR'], $this->authInfo
['REMOTE_HOST'], $this->login
['uname']));
87 // Logout written to log
88 \TYPO3\CMS\Core\Utility\GeneralUtility
::sysLog(sprintf('Login-attempt from %s (%s), username \'%s\' not found!', $this->authInfo
['REMOTE_ADDR'], $this->authInfo
['REMOTE_HOST'], $this->login
['uname']), 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility
::SYSLOG_SEVERITY_WARNING
);
90 if ($this->writeDevLog
) {
91 \TYPO3\CMS\Core\Utility\GeneralUtility
::devLog('User found: ' . \TYPO3\CMS\Core\Utility\GeneralUtility
::arrayToLogString($user, array($this->db_user
['userid_column'], $this->db_user
['username_column'])), 'TYPO3\\CMS\\Sv\\AuthenticationService');
95 // Failed Login attempt (no password given)
96 $this->writelog(255, 3, 3, 2, 'Login-attempt from %s (%s) for username \'%s\' with an empty password!', array($this->authInfo
['REMOTE_ADDR'], $this->authInfo
['REMOTE_HOST'], $this->login
['uname']));
97 \TYPO3\CMS\Core\Utility\GeneralUtility
::sysLog(sprintf('Login-attempt from %s (%s), for username \'%s\' with an empty password!', $this->authInfo
['REMOTE_ADDR'], $this->authInfo
['REMOTE_HOST'], $this->login
['uname']), 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility
::SYSLOG_SEVERITY_WARNING
);
104 * Authenticate a user (Check various conditions for the user that might invalidate its authentication, eg. password match, domain, IP, etc.)
106 * @param array $user Data of user.
109 public function authUser(array $user) {
111 if ($this->login
['uident'] && $this->login
['uname']) {
112 // Checking password match for user:
113 $OK = $this->compareUident($user, $this->login
);
115 // Failed login attempt (wrong password) - write that to the log!
116 if ($this->writeAttemptLog
) {
117 $this->writelog(255, 3, 3, 1, 'Login-attempt from %s (%s), username \'%s\', password not accepted!', array($this->authInfo
['REMOTE_ADDR'], $this->authInfo
['REMOTE_HOST'], $this->login
['uname']));
118 \TYPO3\CMS\Core\Utility\GeneralUtility
::sysLog(sprintf('Login-attempt from %s (%s), username \'%s\', password not accepted!', $this->authInfo
['REMOTE_ADDR'], $this->authInfo
['REMOTE_HOST'], $this->login
['uname']), 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility
::SYSLOG_SEVERITY_WARNING
);
120 if ($this->writeDevLog
) {
121 \TYPO3\CMS\Core\Utility\GeneralUtility
::devLog('Password not accepted: ' . $this->login
['uident'], 'TYPO3\\CMS\\Sv\\AuthenticationService', 2);
124 // Checking the domain (lockToDomain)
125 if ($OK && $user['lockToDomain'] && $user['lockToDomain'] != $this->authInfo
['HTTP_HOST']) {
126 // Lock domain didn't match, so error:
127 if ($this->writeAttemptLog
) {
128 $this->writelog(255, 3, 3, 1, 'Login-attempt from %s (%s), username \'%s\', locked domain \'%s\' did not match \'%s\'!', array($this->authInfo
['REMOTE_ADDR'], $this->authInfo
['REMOTE_HOST'], $user[$this->db_user
['username_column']], $user['lockToDomain'], $this->authInfo
['HTTP_HOST']));
129 \TYPO3\CMS\Core\Utility\GeneralUtility
::sysLog(sprintf('Login-attempt from %s (%s), username \'%s\', locked domain \'%s\' did not match \'%s\'!', $this->authInfo
['REMOTE_ADDR'], $this->authInfo
['REMOTE_HOST'], $user[$this->db_user
['username_column']], $user['lockToDomain'], $this->authInfo
['HTTP_HOST']), 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility
::SYSLOG_SEVERITY_WARNING
);
138 * Find usergroup records, currently only for frontend
140 * @param array $user Data of user.
141 * @param array $knownGroups Group data array of already known groups. This is handy if you want select other related groups. Keys in this array are unique IDs of those groups.
142 * @return mixed Groups array, keys = uid which must be unique
143 * @todo Define visibility
145 public function getGroups($user, $knownGroups) {
146 global $TYPO3_CONF_VARS;
147 $groupDataArr = array();
148 if ($this->mode
== 'getGroupsFE') {
150 if (is_array($user) && $user[$this->db_user
['usergroup_column']]) {
151 $groupList = $user[$this->db_user
['usergroup_column']];
153 $this->getSubGroups($groupList, '', $groups);
155 // ADD group-numbers if the IPmask matches.
156 if (is_array($TYPO3_CONF_VARS['FE']['IPmaskMountGroups'])) {
157 foreach ($TYPO3_CONF_VARS['FE']['IPmaskMountGroups'] as $IPel) {
158 if ($this->authInfo
['REMOTE_ADDR'] && $IPel[0] && \TYPO3\CMS\Core\Utility\GeneralUtility
::cmpIP($this->authInfo
['REMOTE_ADDR'], $IPel[0])) {
159 $groups[] = (int)$IPel[1];
163 $groups = array_unique($groups);
164 if (count($groups)) {
165 $list = implode(',', $groups);
166 if ($this->writeDevLog
) {
167 \TYPO3\CMS\Core\Utility\GeneralUtility
::devLog('Get usergroups with id: ' . $list, 'TYPO3\\CMS\\Sv\\AuthenticationService');
169 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . $this->authInfo
['HTTP_HOST'] . '\')';
170 if (!$this->authInfo
['showHiddenRecords']) {
171 $hiddenP = 'AND hidden=0 ';
173 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->db_groups
['table'], 'deleted=0 ' . $hiddenP . ' AND uid IN (' . $list . ')' . $lockToDomain_SQL);
174 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
175 $groupDataArr[$row['uid']] = $row;
178 $GLOBALS['TYPO3_DB']->sql_free_result($res);
181 if ($this->writeDevLog
) {
182 \TYPO3\CMS\Core\Utility\GeneralUtility
::devLog('No usergroups found.', 'TYPO3\\CMS\\Sv\\AuthenticationService', 2);
185 } elseif ($this->mode
== 'getGroupsBE') {
188 return $groupDataArr;
192 * Fetches subgroups of groups. Function is called recursively for each subgroup.
193 * Function was previously copied from
194 * \TYPO3\CMS\Core\Authentication\BackendUserAuthentication->fetchGroups and has been slightly modified.
196 * @param string $grList Commalist of fe_groups uid numbers
197 * @param string $idList List of already processed fe_groups-uids so the function will not fall into a eternal recursion.
198 * @param array $groups
201 * @todo Define visibility
203 public function getSubGroups($grList, $idList = '', &$groups) {
204 // Fetching records of the groups in $grList (which are not blocked by lockedToDomain either):
205 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . $this->authInfo
['HTTP_HOST'] . '\')';
206 if (!$this->authInfo
['showHiddenRecords']) {
207 $hiddenP = 'AND hidden=0 ';
209 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,subgroup', 'fe_groups', 'deleted=0 ' . $hiddenP . ' AND uid IN (' . $grList . ')' . $lockToDomain_SQL);
210 // Internal group record storage
211 $groupRows = array();
212 // The groups array is filled
213 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
214 if (!in_array($row['uid'], $groups)) {
215 $groups[] = $row['uid'];
217 $groupRows[$row['uid']] = $row;
219 // Traversing records in the correct order
220 $include_staticArr = \TYPO3\CMS\Core\Utility\GeneralUtility
::intExplode(',', $grList);
222 foreach ($include_staticArr as $uid) {
224 $row = $groupRows[$uid];
225 // Must be an array and $uid should not be in the idList, because then it is somewhere previously in the grouplist
226 if (is_array($row) && !\TYPO3\CMS\Core\Utility\GeneralUtility
::inList($idList, $uid)) {
227 // Include sub groups
228 if (trim($row['subgroup'])) {
230 $theList = implode(',', \TYPO3\CMS\Core\Utility\GeneralUtility
::intExplode(',', $row['subgroup']));
231 // Call recursively, pass along list of already processed groups so they are not recursed again.
232 $this->getSubGroups($theList, $idList . ',' . $uid, $groups);