[CLEANUP] Remove security_level option from Authentications
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / Classes / SaltedPasswordService.php
1 <?php
2 namespace TYPO3\CMS\Saltedpasswords;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) Marcus Krause (marcus#exp2009@t3sec.info)
8 * (c) Steffen Ritter (info@rs-websystems.de)
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Contains authentication service class for salted hashed passwords.
32 */
33 /**
34 * Class implements salted-password hashes authentication service.
35 *
36 * @author Marcus Krause <marcus#exp2009@t3sec.info>
37 * @author Steffen Ritter <info@rs-websystems.de>
38 * @since 2009-06-14
39 */
40 class SaltedPasswordService extends \TYPO3\CMS\Sv\AbstractAuthenticationService {
41
42 /**
43 * Keeps class name.
44 *
45 * @var string
46 */
47 public $prefixId = 'tx_saltedpasswords_sv1';
48
49 /**
50 * Keeps path to this script relative to the extension directory.
51 *
52 * @var string
53 */
54 public $scriptRelPath = 'sv1/class.tx_saltedpasswords_sv1.php';
55
56 /**
57 * Keeps extension key.
58 *
59 * @var string
60 */
61 public $extKey = 'saltedpasswords';
62
63 /**
64 * Keeps extension configuration.
65 *
66 * @var mixed
67 */
68 protected $extConf;
69
70 /**
71 * An instance of the salted hashing method.
72 * This member is set in the getSaltingInstance() function.
73 *
74 * @var \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt
75 */
76 protected $objInstanceSaltedPW = NULL;
77
78 /**
79 * Indicates whether the salted password authentication has failed.
80 *
81 * Prevents authentication bypass. See vulnerability report:
82 * { @link http://forge.typo3.org/issues/22030 }
83 *
84 * @var boolean
85 */
86 protected $authenticationFailed = FALSE;
87
88 /**
89 * Checks if service is available. In case of this service we check that
90 * following prerequesties are fulfilled:
91 * - loginSecurityLevel of according TYPO3_MODE is set to normal
92 *
93 * @return boolean TRUE if service is available
94 */
95 public function init() {
96 $available = FALSE;
97 $mode = TYPO3_MODE;
98 if ($this->info['requestedServiceSubType'] === 'authUserBE') {
99 $mode = 'BE';
100 } elseif ($this->info['requestedServiceSubType'] === 'authUserFE') {
101 $mode = 'FE';
102 }
103 if (\TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::isUsageEnabled($mode)) {
104 $available = TRUE;
105 $this->extConf = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::returnExtConf();
106 }
107 return $available ? parent::init() : FALSE;
108 }
109
110 /**
111 * Checks the login data with the user record data for builtin login method.
112 *
113 * @param array $user User data array
114 * @param array $loginData Login data array
115 * @return boolean TRUE if login data matched
116 * @todo Define visibility
117 */
118 public function compareUident(array $user, array $loginData) {
119 $validPasswd = FALSE;
120 $password = $loginData['uident_text'];
121 // Determine method used for given salted hashed password
122 $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($user['password']);
123 // Existing record is in format of Salted Hash password
124 if (is_object($this->objInstanceSaltedPW)) {
125 $validPasswd = $this->objInstanceSaltedPW->checkPassword($password, $user['password']);
126 // Record is in format of Salted Hash password but authentication failed
127 // skip further authentication methods
128 if (!$validPasswd) {
129 $this->authenticationFailed = TRUE;
130 }
131 $defaultHashingClassName = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod();
132 $skip = FALSE;
133 // Test for wrong salted hashing method
134 if ($validPasswd && !(get_class($this->objInstanceSaltedPW) == $defaultHashingClassName) || is_subclass_of($this->objInstanceSaltedPW, $defaultHashingClassName)) {
135 // Instanciate default method class
136 $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL);
137 $this->updatePassword((int)$user['uid'], array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
138 }
139 if ($validPasswd && !$skip && $this->objInstanceSaltedPW->isHashUpdateNeeded($user['password'])) {
140 $this->updatePassword((int)$user['uid'], array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
141 }
142 } elseif (!(int)$this->extConf['forceSalted']) {
143 // Stored password is in deprecated salted hashing method
144 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList('C$,M$', substr($user['password'], 0, 2))) {
145 // Instanciate default method class
146 $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(substr($user['password'], 1));
147 // md5
148 if ($user['password'][0] === 'M') {
149 $validPasswd = $this->objInstanceSaltedPW->checkPassword(md5($password), substr($user['password'], 1));
150 } else {
151 $validPasswd = $this->objInstanceSaltedPW->checkPassword($password, substr($user['password'], 1));
152 }
153 // Skip further authentication methods
154 if (!$validPasswd) {
155 $this->authenticationFailed = TRUE;
156 }
157 } elseif (preg_match('/[0-9abcdef]{32,32}/', $user['password'])) {
158 $validPasswd = md5($password) === (string)$user['password'];
159 // Skip further authentication methods
160 if (!$validPasswd) {
161 $this->authenticationFailed = TRUE;
162 }
163 } else {
164 $validPasswd = (string)$password === (string)$user['password'];
165 }
166 // Should we store the new format value in DB?
167 if ($validPasswd && (int)$this->extConf['updatePasswd']) {
168 // Instanciate default method class
169 $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL);
170 $this->updatePassword((int)$user['uid'], array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
171 }
172 }
173 return $validPasswd;
174 }
175
176 /**
177 * Method adds a further authUser method.
178 *
179 * Will return one of following authentication status codes:
180 * - 0 - authentication failure
181 * - 100 - just go on. User is not authenticated but there is still no reason to stop
182 * - 200 - the service was able to authenticate the user
183 *
184 * @param array Array containing FE user data of the logged user.
185 * @return integer Authentication statuscode, one of 0,100 and 200
186 */
187 public function authUser(array $user) {
188 $OK = 100;
189 $validPasswd = FALSE;
190 if ($this->login['uident'] && $this->login['uname']) {
191 if (!empty($this->login['uident_text'])) {
192 $validPasswd = $this->compareUident($user, $this->login);
193 }
194 if (!$validPasswd) {
195 // Failed login attempt (wrong password)
196 $errorMessage = 'Login-attempt from %s (%s), username \'%s\', password not accepted!';
197 // No delegation to further services
198 if ((int)$this->extConf['onlyAuthService'] || $this->authenticationFailed) {
199 $this->writeLogMessage(TYPO3_MODE . ' Authentication failed - wrong password for username \'%s\'', $this->login['uname']);
200 $OK = 0;
201 } else {
202 $this->writeLogMessage($errorMessage, $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']);
203 }
204 $this->writelog(255, 3, 3, 1, $errorMessage, array(
205 $this->authInfo['REMOTE_ADDR'],
206 $this->authInfo['REMOTE_HOST'],
207 $this->login['uname']
208 ));
209 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog(sprintf($errorMessage, $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']), 'Core', \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_INFO);
210 } elseif ($validPasswd && $user['lockToDomain'] && strcasecmp($user['lockToDomain'], $this->authInfo['HTTP_HOST'])) {
211 // Lock domain didn't match, so error:
212 $errorMessage = 'Login-attempt from %s (%s), username \'%s\', locked domain \'%s\' did not match \'%s\'!';
213 $this->writeLogMessage($errorMessage, $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname'], $user['lockToDomain'], $this->authInfo['HTTP_HOST']);
214 $this->writelog(255, 3, 3, 1, $errorMessage, array(
215 $this->authInfo['REMOTE_ADDR'],
216 $this->authInfo['REMOTE_HOST'],
217 $user[$this->db_user['username_column']],
218 $user['lockToDomain'],
219 $this->authInfo['HTTP_HOST']
220 ));
221 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog(sprintf($errorMessage, $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_INFO);
222 $OK = 0;
223 } elseif ($validPasswd) {
224 $this->writeLogMessage(TYPO3_MODE . ' Authentication successful for username \'%s\'', $this->login['uname']);
225 $OK = 200;
226 }
227 }
228 return $OK;
229 }
230
231 /**
232 * Method updates a FE/BE user record - in this case a new password string will be set.
233 *
234 * @param integer $uid uid of user record that will be updated
235 * @param mixed $updateFields Field values as key=>value pairs to be updated in database
236 * @return void
237 */
238 protected function updatePassword($uid, $updateFields) {
239 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($this->pObj->user_table, sprintf('uid = %u', $uid), $updateFields);
240 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog(sprintf('Automatic password update for user record in %s with uid %u', $this->pObj->user_table, $uid), $this->extKey, 1);
241 }
242
243 /**
244 * Writes log message. Destination log depends on the current system mode.
245 * For FE the function writes to the admin panel log. For BE messages are
246 * sent to the system log. If developer log is enabled, messages are also
247 * sent there.
248 *
249 * This function accepts variable number of arguments and can format
250 * parameters. The syntax is the same as for sprintf()
251 *
252 * @param string $message Message to output
253 * @return void
254 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog()
255 * @todo Define visibility
256 */
257 public function writeLogMessage($message) {
258 if (func_num_args() > 1) {
259 $params = func_get_args();
260 array_shift($params);
261 $message = vsprintf($message, $params);
262 }
263 if (TYPO3_MODE === 'BE') {
264 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog($message, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_NOTICE);
265 } else {
266 $GLOBALS['TT']->setTSlogMessage($message);
267 }
268 if (TYPO3_DLOG) {
269 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog($message, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_NOTICE);
270 }
271 }
272
273 }