[CLEANUP] Remove code duplication in saltedpasswords
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / sv1 / class.tx_saltedpasswords_sv1.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) Marcus Krause (marcus#exp2009@t3sec.info)
6 * (c) Steffen Ritter (info@rs-websystems.de)
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * Contains authentication service class for salted hashed passwords.
30 */
31
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 *
39 * @since 2009-06-14
40 * @package TYPO3
41 * @subpackage tx_saltedpasswords
42 */
43 class tx_saltedpasswords_sv1 extends tx_sv_authbase {
44 /**
45 * Keeps class name.
46 *
47 * @var string
48 */
49 public $prefixId = 'tx_saltedpasswords_sv1';
50
51 /**
52 * Keeps path to this script relative to the extension directory.
53 *
54 * @var string
55 */
56 public $scriptRelPath = 'sv1/class.tx_saltedpasswords_sv1.php';
57
58 /**
59 * Keeps extension key.
60 *
61 * @var string
62 */
63 public $extKey = 'saltedpasswords';
64
65 /**
66 * Keeps extension configuration.
67 *
68 * @var mixed
69 */
70 protected $extConf;
71
72 /**
73 * An instance of the salted hashing method.
74 * This member is set in the getSaltingInstance() function.
75 *
76 * @var tx_saltedpasswords_abstract_salts
77 */
78 protected $objInstanceSaltedPW = NULL;
79
80 /**
81 * Indicates whether the salted password authentication has failed.
82 *
83 * Prevents authentication bypass. See vulnerability report:
84 * { @link http://bugs.typo3.org/view.php?id=13372 }
85 *
86 * @var boolean
87 */
88 protected $authenticationFailed = FALSE;
89
90 /**
91 * Checks if service is available. In case of this service we check that
92 * following prerequesties are fulfilled:
93 * - loginSecurityLevel of according TYPO3_MODE is set to normal
94 *
95 * @return boolean TRUE if service is available
96 */
97 public function init() {
98 $available = FALSE;
99
100 if (tx_saltedpasswords_div::isUsageEnabled()) {
101 $available = TRUE;
102 $this->extConf = tx_saltedpasswords_div::returnExtConf();
103 }
104
105 return $available ? parent::init() : FALSE;
106 }
107
108 /**
109 * Checks the login data with the user record data for builtin login method.
110 *
111 * @param array user data array
112 * @param array login data array
113 * @param string login security level (optional)
114 * @return boolean TRUE if login data matched
115 */
116 function compareUident(array $user, array $loginData, $security_level = 'normal') {
117 $validPasswd = FALSE;
118
119 // could be merged; still here to clarify
120 if (!strcmp(TYPO3_MODE, 'BE')) {
121 $password = $loginData['uident_text'];
122 } elseif (!strcmp(TYPO3_MODE, 'FE')) {
123 $password = $loginData['uident_text'];
124 }
125
126 // determine method used for given salted hashed password
127 $this->objInstanceSaltedPW = tx_saltedpasswords_salts_factory::getSaltingInstance($user['password']);
128
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
133 // record is in format of Salted Hash password but authentication failed
134 // skip further authentication methods
135 if (!$validPasswd) {
136 $this->authenticationFailed = TRUE;
137 }
138
139 $defaultHashingClassName = tx_saltedpasswords_div::getDefaultSaltingHashingMethod();
140 $skip = FALSE;
141
142 // test for wrong salted hashing method
143 if ($validPasswd && !(get_class($this->objInstanceSaltedPW) == $defaultHashingClassName) || (is_subclass_of($this->objInstanceSaltedPW, $defaultHashingClassName))) {
144 // instanciate default method class
145 $this->objInstanceSaltedPW = tx_saltedpasswords_salts_factory::getSaltingInstance(NULL);
146 $this->updatePassword(
147 intval($user['uid']),
148 array('password' => $this->objInstanceSaltedPW->getHashedPassword($password))
149 );
150 }
151
152 if ($validPasswd && !$skip && $this->objInstanceSaltedPW->isHashUpdateNeeded($user['password'])) {
153 $this->updatePassword(
154 intval($user['uid']),
155 array('password' => $this->objInstanceSaltedPW->getHashedPassword($password))
156 );
157 }
158 // we process also clear-text, md5 and passwords updated by Portable PHP password hashing framework
159 } elseif (!intval($this->extConf['forceSalted'])) {
160
161 // stored password is in deprecated salted hashing method
162 if (t3lib_div::inList('C$,M$', substr($user['password'], 0, 2))) {
163
164 // instanciate default method class
165 $this->objInstanceSaltedPW = tx_saltedpasswords_salts_factory::getSaltingInstance(substr($user['password'], 1));
166
167 // md5
168 if (!strcmp(substr($user['password'], 0, 1), 'M')) {
169 $validPasswd = $this->objInstanceSaltedPW->checkPassword(md5($password), substr($user['password'], 1));
170 } else {
171 $validPasswd = $this->objInstanceSaltedPW->checkPassword($password, substr($user['password'], 1));
172 }
173
174 // skip further authentication methods
175 if (!$validPasswd) {
176 $this->authenticationFailed = TRUE;
177 }
178
179 // password is stored as md5
180 } elseif (preg_match('/[0-9abcdef]{32,32}/', $user['password'])) {
181 $validPasswd = (!strcmp(md5($password), $user['password']) ? TRUE : FALSE);
182
183 // skip further authentication methods
184 if (!$validPasswd) {
185 $this->authenticationFailed = TRUE;
186 }
187
188 // password is stored plain or unrecognized format
189 } else {
190 $validPasswd = (!strcmp($password, $user['password']) ? TRUE : FALSE);
191 }
192 // should we store the new format value in DB?
193 if ($validPasswd && intval($this->extConf['updatePasswd'])) {
194 // instanciate default method class
195 $this->objInstanceSaltedPW = tx_saltedpasswords_salts_factory::getSaltingInstance(NULL);
196 $this->updatePassword(
197 intval($user['uid']),
198 array('password' => $this->objInstanceSaltedPW->getHashedPassword($password))
199 );
200 }
201 }
202
203 return $validPasswd;
204 }
205
206 /**
207 * Method adds a further authUser method.
208 *
209 * Will return one of following authentication status codes:
210 * - 0 - authentication failure
211 * - 100 - just go on. User is not authenticated but there is still no reason to stop
212 * - 200 - the service was able to authenticate the user
213 *
214 * @param array Array containing FE user data of the logged user.
215 * @return integer authentication statuscode, one of 0,100 and 200
216 */
217 public function authUser(array $user) {
218 $OK = 100;
219 $validPasswd = FALSE;
220
221 if ($this->pObj->security_level == 'rsa' && t3lib_extMgm::isLoaded('rsaauth')) {
222 $backend = tx_rsaauth_backendfactory::getBackend();
223 $storage = tx_rsaauth_storagefactory::getStorage();
224 // Preprocess the password
225 $password = $this->login['uident'];
226 $key = $storage->get();
227 if ($key != NULL && substr($password, 0, 4) == 'rsa:') {
228 // Decode password and pass to parent
229 $decryptedPassword = $backend->decrypt($key, substr($password, 4));
230 $this->login['uident_text'] = $decryptedPassword;
231 }
232 }
233
234 if ($this->login['uident'] && $this->login['uname']) {
235 if (!empty($this->login['uident_text'])) {
236 $validPasswd = $this->compareUident(
237 $user,
238 $this->login
239 );
240 }
241
242 if (!$validPasswd) {
243 // Failed login attempt (wrong password)
244 $errorMessage = 'Login-attempt from %s (%s), username \'%s\', password not accepted!';
245 // no delegation to further services
246 if (intval($this->extConf['onlyAuthService']) || $this->authenticationFailed) {
247 $this->writeLogMessage(
248 TYPO3_MODE . ' Authentication failed - wrong password for username \'%s\'',
249 $this->login['uname']
250 );
251 } else {
252 $this->writeLogMessage(
253 $errorMessage,
254 $this->authInfo['REMOTE_ADDR'],
255 $this->authInfo['REMOTE_HOST'],
256 $this->login['uname']
257 );
258 }
259 $this->writelog(255, 3, 3, 1,
260 $errorMessage,
261 array(
262 $this->authInfo['REMOTE_ADDR'],
263 $this->authInfo['REMOTE_HOST'],
264 $this->login['uname']
265 )
266 );
267 t3lib_div::sysLog(
268 sprintf(
269 $errorMessage,
270 $this->authInfo['REMOTE_ADDR'],
271 $this->authInfo['REMOTE_HOST'],
272 $this->login['uname']
273 ),
274 'Core',
275 0
276 );
277 if (intval($this->extConf['onlyAuthService']) || $this->authenticationFailed) {
278 $OK = 0;
279 }
280 } elseif ($validPasswd && $user['lockToDomain'] && strcasecmp($user['lockToDomain'], $this->authInfo['HTTP_HOST'])) {
281 // Lock domain didn't match, so error:
282 $errorMessage = 'Login-attempt from %s (%s), username \'%s\', locked domain \'%s\' did not match \'%s\'!';
283 $this->writeLogMessage(
284 $errorMessage,
285 $this->authInfo['REMOTE_ADDR'],
286 $this->authInfo['REMOTE_HOST'],
287 $this->login['uname'],
288 $user['lockToDomain'],
289 $this->authInfo['HTTP_HOST']
290 );
291 $this->writelog(255, 3, 3, 1,
292 $errorMessage,
293 array(
294 $this->authInfo['REMOTE_ADDR'],
295 $this->authInfo['REMOTE_HOST'],
296 $user[$this->db_user['username_column']],
297 $user['lockToDomain'],
298 $this->authInfo['HTTP_HOST']
299 )
300 );
301 t3lib_div::sysLog(
302 sprintf(
303 $errorMessage,
304 $this->authInfo['REMOTE_ADDR'],
305 $this->authInfo['REMOTE_HOST'],
306 $user[$this->db_user['username_column']],
307 $user['lockToDomain'],
308 $this->authInfo['HTTP_HOST']
309 ),
310 'Core',
311 0
312 );
313 $OK = 0;
314 } elseif ($validPasswd) {
315 $this->writeLogMessage(
316 TYPO3_MODE . ' Authentication successful for username \'%s\'',
317 $this->login['uname']
318 );
319 $OK = 200;
320 }
321 }
322
323 return $OK;
324 }
325
326 /**
327 * Method updates a FE/BE user record - in this case a new password string will be set.
328 *
329 * @param integer $uid: uid of user record that will be updated
330 * @param mixed $updateFields: Field values as key=>value pairs to be updated in database
331 * @return void
332 */
333 protected function updatePassword($uid, $updateFields) {
334 if (TYPO3_MODE === 'BE') {
335 $GLOBALS['TYPO3_DB']->exec_UPDATEquery( 'be_users', sprintf('uid = %u', $uid), $updateFields);
336 } else {
337 $GLOBALS['TYPO3_DB']->exec_UPDATEquery( 'fe_users', sprintf('uid = %u', $uid), $updateFields);
338 }
339
340 t3lib_div::devLog(sprintf('Automatic password update for %s user with uid %u', TYPO3_MODE, $uid), $this->extKey, 1);
341 }
342
343 /**
344 * Writes log message. Destination log depends on the current system mode.
345 * For FE the function writes to the admin panel log. For BE messages are
346 * sent to the system log. If developer log is enabled, messages are also
347 * sent there.
348 *
349 * This function accepts variable number of arguments and can format
350 * parameters. The syntax is the same as for sprintf()
351 *
352 * @param string $message: Message to output
353 * @return void
354 * @see sprintf()
355 * @see t3lib::divLog()
356 * @see t3lib_div::sysLog()
357 * @see t3lib_timeTrack::setTSlogMessage()
358 */
359 function writeLogMessage($message) {
360 if (func_num_args() > 1) {
361 $params = func_get_args();
362 array_shift($params);
363 $message = vsprintf($message, $params);
364 }
365
366 if (TYPO3_MODE === 'BE') {
367 t3lib_div::sysLog($message, $this->extKey, 1);
368 } else {
369 $GLOBALS['TT']->setTSlogMessage($message);
370 }
371
372 if (TYPO3_DLOG) {
373 t3lib_div::devLog($message, $this->extKey, 1);
374 }
375 }
376 }
377
378
379 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/saltedpasswords/sv1/class.tx_saltedpasswords_sv1.php'])) {
380 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/saltedpasswords/sv1/class.tx_saltedpasswords_sv1.php']);
381 }
382 ?>