[TASK] Update version number for master to 7
[Packages/TYPO3.CMS.git] / typo3 / sysext / sv / Classes / AuthenticationService.php
1 <?php
2 namespace TYPO3\CMS\Sv;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Authentication services class
19 *
20 * @author René Fritz <r.fritz@colorcube.de>
21 */
22 class AuthenticationService extends \TYPO3\CMS\Sv\AbstractAuthenticationService {
23
24 /**
25 * Process the submitted credentials.
26 * In this case hash the clear text password if it has been submitted.
27 *
28 * @param array $loginData Credentials that are submitted and potentially modified by other services
29 * @param string $passwordTransmissionStrategy Keyword of how the password has been hashed or encrypted before submission
30 * @return boolean
31 */
32 public function processLoginData(array &$loginData, $passwordTransmissionStrategy) {
33 $isProcessed = TRUE;
34 // Processing data according to the state it was submitted in.
35 switch ($passwordTransmissionStrategy) {
36 case 'normal':
37 $loginData['uident_text'] = $loginData['uident'];
38 break;
39 case 'challenged':
40 $loginData['uident_text'] = '';
41 $loginData['uident_challenged'] = $loginData['uident'];
42 $loginData['uident_superchallenged'] = '';
43 break;
44 case 'superchallenged':
45 $loginData['uident_text'] = '';
46 $loginData['uident_challenged'] = '';
47 $loginData['uident_superchallenged'] = $loginData['uident'];
48 break;
49 default:
50 $isProcessed = FALSE;
51 }
52 if (!empty($loginData['uident_text'])) {
53 $loginData['uident_challenged'] = (string) md5(($loginData['uname'] . ':' . $loginData['uident_text'] . ':' . $loginData['chalvalue']));
54 $loginData['uident_superchallenged'] = (string) md5(($loginData['uname'] . ':' . md5($loginData['uident_text']) . ':' . $loginData['chalvalue']));
55 $isProcessed = TRUE;
56 }
57 return $isProcessed;
58 }
59
60 /**
61 * Find a user (eg. look up the user record in database when a login is sent)
62 *
63 * @return mixed User array or FALSE
64 */
65 public function getUser() {
66 $user = FALSE;
67 if ($this->login['status'] == 'login') {
68 if ($this->login['uident']) {
69 $user = $this->fetchUserRecord($this->login['uname']);
70 if (!is_array($user)) {
71 // Failed login attempt (no username found)
72 $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']));
73 // Logout written to log
74 \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);
75 } else {
76 if ($this->writeDevLog) {
77 \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');
78 }
79 }
80 } else {
81 // Failed Login attempt (no password given)
82 $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']));
83 \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);
84 }
85 }
86 return $user;
87 }
88
89 /**
90 * Authenticate a user (Check various conditions for the user that might invalidate its authentication, eg. password match, domain, IP, etc.)
91 *
92 * @param array $user Data of user.
93 *
94 * @return integer >= 200: User authenticated successfully.
95 * No more checking is needed by other auth services.
96 * >= 100: User not authenticated; this service is not responsible.
97 * Other auth services will be asked.
98 * > 0: User authenticated successfully.
99 * Other auth services will still be asked.
100 * <= 0: Authentication failed, no more checking needed
101 * by other auth services.
102 */
103 public function authUser(array $user) {
104 $OK = 100;
105 if ($this->login['uident'] && $this->login['uname']) {
106 // Checking password match for user:
107 $OK = $this->compareUident($user, $this->login);
108 if (!$OK) {
109 // Failed login attempt (wrong password) - write that to the log!
110 if ($this->writeAttemptLog) {
111 $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']));
112 \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);
113 }
114 if ($this->writeDevLog) {
115 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('Password not accepted: ' . $this->login['uident'], 'TYPO3\\CMS\\Sv\\AuthenticationService', 2);
116 }
117 }
118 // Checking the domain (lockToDomain)
119 if ($OK && $user['lockToDomain'] && $user['lockToDomain'] != $this->authInfo['HTTP_HOST']) {
120 // Lock domain didn't match, so error:
121 if ($this->writeAttemptLog) {
122 $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']));
123 \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);
124 }
125 $OK = 0;
126 }
127 }
128 return $OK;
129 }
130
131 /**
132 * Find usergroup records, currently only for frontend
133 *
134 * @param array $user Data of user.
135 * @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.
136 * @return mixed Groups array, keys = uid which must be unique
137 */
138 public function getGroups($user, $knownGroups) {
139 global $TYPO3_CONF_VARS;
140 $groupDataArr = array();
141 if ($this->mode == 'getGroupsFE') {
142 $groups = array();
143 if (is_array($user) && $user[$this->db_user['usergroup_column']]) {
144 $groupList = $user[$this->db_user['usergroup_column']];
145 $groups = array();
146 $this->getSubGroups($groupList, '', $groups);
147 }
148 // ADD group-numbers if the IPmask matches.
149 if (is_array($TYPO3_CONF_VARS['FE']['IPmaskMountGroups'])) {
150 foreach ($TYPO3_CONF_VARS['FE']['IPmaskMountGroups'] as $IPel) {
151 if ($this->authInfo['REMOTE_ADDR'] && $IPel[0] && \TYPO3\CMS\Core\Utility\GeneralUtility::cmpIP($this->authInfo['REMOTE_ADDR'], $IPel[0])) {
152 $groups[] = (int)$IPel[1];
153 }
154 }
155 }
156 $groups = array_unique($groups);
157 if (count($groups)) {
158 $list = implode(',', $groups);
159 if ($this->writeDevLog) {
160 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('Get usergroups with id: ' . $list, 'TYPO3\\CMS\\Sv\\AuthenticationService');
161 }
162 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . $this->authInfo['HTTP_HOST'] . '\')';
163 if (!$this->authInfo['showHiddenRecords']) {
164 $hiddenP = 'AND hidden=0 ';
165 }
166 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->db_groups['table'], 'deleted=0 ' . $hiddenP . ' AND uid IN (' . $list . ')' . $lockToDomain_SQL);
167 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
168 $groupDataArr[$row['uid']] = $row;
169 }
170 if ($res) {
171 $GLOBALS['TYPO3_DB']->sql_free_result($res);
172 }
173 } else {
174 if ($this->writeDevLog) {
175 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('No usergroups found.', 'TYPO3\\CMS\\Sv\\AuthenticationService', 2);
176 }
177 }
178 } elseif ($this->mode == 'getGroupsBE') {
179
180 }
181 return $groupDataArr;
182 }
183
184 /**
185 * Fetches subgroups of groups. Function is called recursively for each subgroup.
186 * Function was previously copied from
187 * \TYPO3\CMS\Core\Authentication\BackendUserAuthentication->fetchGroups and has been slightly modified.
188 *
189 * @param string $grList Commalist of fe_groups uid numbers
190 * @param string $idList List of already processed fe_groups-uids so the function will not fall into a eternal recursion.
191 * @param array $groups
192 * @return array
193 * @access private
194 */
195 public function getSubGroups($grList, $idList = '', &$groups) {
196 // Fetching records of the groups in $grList (which are not blocked by lockedToDomain either):
197 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . $this->authInfo['HTTP_HOST'] . '\')';
198 if (!$this->authInfo['showHiddenRecords']) {
199 $hiddenP = 'AND hidden=0 ';
200 }
201 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,subgroup', 'fe_groups', 'deleted=0 ' . $hiddenP . ' AND uid IN (' . $grList . ')' . $lockToDomain_SQL);
202 // Internal group record storage
203 $groupRows = array();
204 // The groups array is filled
205 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
206 if (!in_array($row['uid'], $groups)) {
207 $groups[] = $row['uid'];
208 }
209 $groupRows[$row['uid']] = $row;
210 }
211 // Traversing records in the correct order
212 $include_staticArr = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $grList);
213 // traversing list
214 foreach ($include_staticArr as $uid) {
215 // Get row:
216 $row = $groupRows[$uid];
217 // Must be an array and $uid should not be in the idList, because then it is somewhere previously in the grouplist
218 if (is_array($row) && !\TYPO3\CMS\Core\Utility\GeneralUtility::inList($idList, $uid)) {
219 // Include sub groups
220 if (trim($row['subgroup'])) {
221 // Make integer list
222 $theList = implode(',', \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $row['subgroup']));
223 // Call recursively, pass along list of already processed groups so they are not recursed again.
224 $this->getSubGroups($theList, $idList . ',' . $uid, $groups);
225 }
226 }
227 }
228 }
229
230 }