[TASK] Re-work/simplify copyright header in PHP files - Part 9
[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 * @todo Define visibility
65 */
66 public function getUser() {
67 $user = FALSE;
68 if ($this->login['status'] == 'login') {
69 if ($this->login['uident']) {
70 $user = $this->fetchUserRecord($this->login['uname']);
71 if (!is_array($user)) {
72 // Failed login attempt (no username found)
73 $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']));
74 // Logout written to log
75 \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);
76 } else {
77 if ($this->writeDevLog) {
78 \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');
79 }
80 }
81 } else {
82 // Failed Login attempt (no password given)
83 $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']));
84 \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);
85 }
86 }
87 return $user;
88 }
89
90 /**
91 * Authenticate a user (Check various conditions for the user that might invalidate its authentication, eg. password match, domain, IP, etc.)
92 *
93 * @param array $user Data of user.
94 *
95 * @return integer >= 200: User authenticated successfully.
96 * No more checking is needed by other auth services.
97 * >= 100: User not authenticated; this service is not responsible.
98 * Other auth services will be asked.
99 * > 0: User authenticated successfully.
100 * Other auth services will still be asked.
101 * <= 0: Authentication failed, no more checking needed
102 * by other auth services.
103 */
104 public function authUser(array $user) {
105 $OK = 100;
106 if ($this->login['uident'] && $this->login['uname']) {
107 // Checking password match for user:
108 $OK = $this->compareUident($user, $this->login);
109 if (!$OK) {
110 // Failed login attempt (wrong password) - write that to the log!
111 if ($this->writeAttemptLog) {
112 $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']));
113 \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);
114 }
115 if ($this->writeDevLog) {
116 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('Password not accepted: ' . $this->login['uident'], 'TYPO3\\CMS\\Sv\\AuthenticationService', 2);
117 }
118 }
119 // Checking the domain (lockToDomain)
120 if ($OK && $user['lockToDomain'] && $user['lockToDomain'] != $this->authInfo['HTTP_HOST']) {
121 // Lock domain didn't match, so error:
122 if ($this->writeAttemptLog) {
123 $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']));
124 \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);
125 }
126 $OK = 0;
127 }
128 }
129 return $OK;
130 }
131
132 /**
133 * Find usergroup records, currently only for frontend
134 *
135 * @param array $user Data of user.
136 * @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.
137 * @return mixed Groups array, keys = uid which must be unique
138 * @todo Define visibility
139 */
140 public function getGroups($user, $knownGroups) {
141 global $TYPO3_CONF_VARS;
142 $groupDataArr = array();
143 if ($this->mode == 'getGroupsFE') {
144 $groups = array();
145 if (is_array($user) && $user[$this->db_user['usergroup_column']]) {
146 $groupList = $user[$this->db_user['usergroup_column']];
147 $groups = array();
148 $this->getSubGroups($groupList, '', $groups);
149 }
150 // ADD group-numbers if the IPmask matches.
151 if (is_array($TYPO3_CONF_VARS['FE']['IPmaskMountGroups'])) {
152 foreach ($TYPO3_CONF_VARS['FE']['IPmaskMountGroups'] as $IPel) {
153 if ($this->authInfo['REMOTE_ADDR'] && $IPel[0] && \TYPO3\CMS\Core\Utility\GeneralUtility::cmpIP($this->authInfo['REMOTE_ADDR'], $IPel[0])) {
154 $groups[] = (int)$IPel[1];
155 }
156 }
157 }
158 $groups = array_unique($groups);
159 if (count($groups)) {
160 $list = implode(',', $groups);
161 if ($this->writeDevLog) {
162 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('Get usergroups with id: ' . $list, 'TYPO3\\CMS\\Sv\\AuthenticationService');
163 }
164 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . $this->authInfo['HTTP_HOST'] . '\')';
165 if (!$this->authInfo['showHiddenRecords']) {
166 $hiddenP = 'AND hidden=0 ';
167 }
168 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->db_groups['table'], 'deleted=0 ' . $hiddenP . ' AND uid IN (' . $list . ')' . $lockToDomain_SQL);
169 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
170 $groupDataArr[$row['uid']] = $row;
171 }
172 if ($res) {
173 $GLOBALS['TYPO3_DB']->sql_free_result($res);
174 }
175 } else {
176 if ($this->writeDevLog) {
177 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('No usergroups found.', 'TYPO3\\CMS\\Sv\\AuthenticationService', 2);
178 }
179 }
180 } elseif ($this->mode == 'getGroupsBE') {
181
182 }
183 return $groupDataArr;
184 }
185
186 /**
187 * Fetches subgroups of groups. Function is called recursively for each subgroup.
188 * Function was previously copied from
189 * \TYPO3\CMS\Core\Authentication\BackendUserAuthentication->fetchGroups and has been slightly modified.
190 *
191 * @param string $grList Commalist of fe_groups uid numbers
192 * @param string $idList List of already processed fe_groups-uids so the function will not fall into a eternal recursion.
193 * @param array $groups
194 * @return array
195 * @access private
196 * @todo Define visibility
197 */
198 public function getSubGroups($grList, $idList = '', &$groups) {
199 // Fetching records of the groups in $grList (which are not blocked by lockedToDomain either):
200 $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . $this->authInfo['HTTP_HOST'] . '\')';
201 if (!$this->authInfo['showHiddenRecords']) {
202 $hiddenP = 'AND hidden=0 ';
203 }
204 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,subgroup', 'fe_groups', 'deleted=0 ' . $hiddenP . ' AND uid IN (' . $grList . ')' . $lockToDomain_SQL);
205 // Internal group record storage
206 $groupRows = array();
207 // The groups array is filled
208 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
209 if (!in_array($row['uid'], $groups)) {
210 $groups[] = $row['uid'];
211 }
212 $groupRows[$row['uid']] = $row;
213 }
214 // Traversing records in the correct order
215 $include_staticArr = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $grList);
216 // traversing list
217 foreach ($include_staticArr as $uid) {
218 // Get row:
219 $row = $groupRows[$uid];
220 // Must be an array and $uid should not be in the idList, because then it is somewhere previously in the grouplist
221 if (is_array($row) && !\TYPO3\CMS\Core\Utility\GeneralUtility::inList($idList, $uid)) {
222 // Include sub groups
223 if (trim($row['subgroup'])) {
224 // Make integer list
225 $theList = implode(',', \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $row['subgroup']));
226 // Call recursively, pass along list of already processed groups so they are not recursed again.
227 $this->getSubGroups($theList, $idList . ',' . $uid, $groups);
228 }
229 }
230 }
231 }
232
233 }