[TASK] Merge submodule dbal into core
[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 textfile 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://bugs.typo3.org/view.php?id=13372 }
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 * @param string $security_level Login security level (optional)
116 * @return boolean TRUE if login data matched
117 * @todo Define visibility
118 */
119 public function compareUident(array $user, array $loginData, $security_level = 'normal') {
120 $validPasswd = FALSE;
121 // Could be merged; still here to clarify
122 if (!strcmp(TYPO3_MODE, 'BE')) {
123 $password = $loginData['uident_text'];
124 } elseif (!strcmp(TYPO3_MODE, 'FE')) {
125 $password = $loginData['uident_text'];
126 }
127 // Determine method used for given salted hashed password
128 $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($user['password']);
129 // Existing record is in format of Salted Hash password
130 if (is_object($this->objInstanceSaltedPW)) {
131 $validPasswd = $this->objInstanceSaltedPW->checkPassword($password, $user['password']);
132 // Record is in format of Salted Hash password but authentication failed
133 // skip further authentication methods
134 if (!$validPasswd) {
135 $this->authenticationFailed = TRUE;
136 }
137 $defaultHashingClassName = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod();
138 $skip = FALSE;
139 // Test for wrong salted hashing method
140 if ($validPasswd && !(get_class($this->objInstanceSaltedPW) == $defaultHashingClassName) || is_subclass_of($this->objInstanceSaltedPW, $defaultHashingClassName)) {
141 // Instanciate default method class
142 $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL);
143 $this->updatePassword(intval($user['uid']), array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
144 }
145 if ($validPasswd && !$skip && $this->objInstanceSaltedPW->isHashUpdateNeeded($user['password'])) {
146 $this->updatePassword(intval($user['uid']), array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
147 }
148 } elseif (!intval($this->extConf['forceSalted'])) {
149 // Stored password is in deprecated salted hashing method
150 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList('C$,M$', substr($user['password'], 0, 2))) {
151 // Instanciate default method class
152 $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(substr($user['password'], 1));
153 // md5
154 if (!strcmp(substr($user['password'], 0, 1), 'M')) {
155 $validPasswd = $this->objInstanceSaltedPW->checkPassword(md5($password), substr($user['password'], 1));
156 } else {
157 $validPasswd = $this->objInstanceSaltedPW->checkPassword($password, substr($user['password'], 1));
158 }
159 // Skip further authentication methods
160 if (!$validPasswd) {
161 $this->authenticationFailed = TRUE;
162 }
163 } elseif (preg_match('/[0-9abcdef]{32,32}/', $user['password'])) {
164 $validPasswd = !strcmp(md5($password), $user['password']) ? TRUE : FALSE;
165 // Skip further authentication methods
166 if (!$validPasswd) {
167 $this->authenticationFailed = TRUE;
168 }
169 } else {
170 $validPasswd = !strcmp($password, $user['password']) ? TRUE : FALSE;
171 }
172 // Should we store the new format value in DB?
173 if ($validPasswd && intval($this->extConf['updatePasswd'])) {
174 // Instanciate default method class
175 $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL);
176 $this->updatePassword(intval($user['uid']), array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
177 }
178 }
179 return $validPasswd;
180 }
181
182 /**
183 * Method adds a further authUser method.
184 *
185 * Will return one of following authentication status codes:
186 * - 0 - authentication failure
187 * - 100 - just go on. User is not authenticated but there is still no reason to stop
188 * - 200 - the service was able to authenticate the user
189 *
190 * @param array Array containing FE user data of the logged user.
191 * @return integer Authentication statuscode, one of 0,100 and 200
192 */
193 public function authUser(array $user) {
194 $OK = 100;
195 $validPasswd = FALSE;
196 if ($this->login['uident'] && $this->login['uname']) {
197 if (!empty($this->login['uident_text'])) {
198 $validPasswd = $this->compareUident($user, $this->login);
199 }
200 if (!$validPasswd) {
201 // Failed login attempt (wrong password)
202 $errorMessage = 'Login-attempt from %s (%s), username \'%s\', password not accepted!';
203 // No delegation to further services
204 if (intval($this->extConf['onlyAuthService']) || $this->authenticationFailed) {
205 $this->writeLogMessage(TYPO3_MODE . ' Authentication failed - wrong password for username \'%s\'', $this->login['uname']);
206 } else {
207 $this->writeLogMessage($errorMessage, $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']);
208 }
209 $this->writelog(255, 3, 3, 1, $errorMessage, array(
210 $this->authInfo['REMOTE_ADDR'],
211 $this->authInfo['REMOTE_HOST'],
212 $this->login['uname']
213 ));
214 \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);
215 if (intval($this->extConf['onlyAuthService']) || $this->authenticationFailed) {
216 $OK = 0;
217 }
218 } elseif ($validPasswd && $user['lockToDomain'] && strcasecmp($user['lockToDomain'], $this->authInfo['HTTP_HOST'])) {
219 // Lock domain didn't match, so error:
220 $errorMessage = 'Login-attempt from %s (%s), username \'%s\', locked domain \'%s\' did not match \'%s\'!';
221 $this->writeLogMessage($errorMessage, $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname'], $user['lockToDomain'], $this->authInfo['HTTP_HOST']);
222 $this->writelog(255, 3, 3, 1, $errorMessage, array(
223 $this->authInfo['REMOTE_ADDR'],
224 $this->authInfo['REMOTE_HOST'],
225 $user[$this->db_user['username_column']],
226 $user['lockToDomain'],
227 $this->authInfo['HTTP_HOST']
228 ));
229 \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);
230 $OK = 0;
231 } elseif ($validPasswd) {
232 $this->writeLogMessage(TYPO3_MODE . ' Authentication successful for username \'%s\'', $this->login['uname']);
233 $OK = 200;
234 }
235 }
236 return $OK;
237 }
238
239 /**
240 * Method updates a FE/BE user record - in this case a new password string will be set.
241 *
242 * @param integer $uid uid of user record that will be updated
243 * @param mixed $updateFields Field values as key=>value pairs to be updated in database
244 * @return void
245 */
246 protected function updatePassword($uid, $updateFields) {
247 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($this->pObj->user_table, sprintf('uid = %u', $uid), $updateFields);
248 \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);
249 }
250
251 /**
252 * Writes log message. Destination log depends on the current system mode.
253 * For FE the function writes to the admin panel log. For BE messages are
254 * sent to the system log. If developer log is enabled, messages are also
255 * sent there.
256 *
257 * This function accepts variable number of arguments and can format
258 * parameters. The syntax is the same as for sprintf()
259 *
260 * @param string $message Message to output
261 * @return void
262 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog()
263 * @todo Define visibility
264 */
265 public function writeLogMessage($message) {
266 if (func_num_args() > 1) {
267 $params = func_get_args();
268 array_shift($params);
269 $message = vsprintf($message, $params);
270 }
271 if (TYPO3_MODE === 'BE') {
272 \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog($message, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_NOTICE);
273 } else {
274 $GLOBALS['TT']->setTSlogMessage($message);
275 }
276 if (TYPO3_DLOG) {
277 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog($message, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_NOTICE);
278 }
279 }
280
281 }
282
283
284 ?>