TYPO3  7.6
SaltedPasswordService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Saltedpasswords;
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 
23 {
29  public $prefixId = 'tx_saltedpasswords_sv1';
30 
36  public $scriptRelPath = 'sv1/class.tx_saltedpasswords_sv1.php';
37 
43  public $extKey = 'saltedpasswords';
44 
50  protected $extConf;
51 
58  protected $objInstanceSaltedPW = null;
59 
68  protected $authenticationFailed = false;
69 
77  public function init()
78  {
79  $available = false;
80  $mode = TYPO3_MODE;
81  if ($this->info['requestedServiceSubType'] === 'authUserBE') {
82  $mode = 'BE';
83  } elseif ($this->info['requestedServiceSubType'] === 'authUserFE') {
84  $mode = 'FE';
85  }
86  if (\TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::isUsageEnabled($mode)) {
87  $available = true;
88  $this->extConf = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::returnExtConf();
89  }
90  return $available ? parent::init() : false;
91  }
92 
101  public function compareUident(array $user, array $loginData, $passwordCompareStrategy = '')
102  {
103  $validPasswd = false;
104  $password = $loginData['uident_text'];
105  // Determine method used for given salted hashed password
106  $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($user['password']);
107  // Existing record is in format of Salted Hash password
108  if (is_object($this->objInstanceSaltedPW)) {
109  $validPasswd = $this->objInstanceSaltedPW->checkPassword($password, $user['password']);
110  // Record is in format of Salted Hash password but authentication failed
111  // skip further authentication methods
112  if (!$validPasswd) {
113  $this->authenticationFailed = true;
114  }
115  $defaultHashingClassName = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod();
116  $skip = false;
117  // Test for wrong salted hashing method
118  if ($validPasswd && !(get_class($this->objInstanceSaltedPW) == $defaultHashingClassName) || is_subclass_of($this->objInstanceSaltedPW, $defaultHashingClassName)) {
119  // Instanciate default method class
120  $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(null);
121  $this->updatePassword((int)$user['uid'], array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
122  }
123  if ($validPasswd && !$skip && $this->objInstanceSaltedPW->isHashUpdateNeeded($user['password'])) {
124  $this->updatePassword((int)$user['uid'], array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
125  }
126  } elseif (!(int)$this->extConf['forceSalted']) {
127  // Stored password is in deprecated salted hashing method
128  if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList('C$,M$', substr($user['password'], 0, 2))) {
129  // Instanciate default method class
130  $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(substr($user['password'], 1));
131  // md5
132  if ($user['password'][0] === 'M') {
133  $validPasswd = $this->objInstanceSaltedPW->checkPassword(md5($password), substr($user['password'], 1));
134  } else {
135  $validPasswd = $this->objInstanceSaltedPW->checkPassword($password, substr($user['password'], 1));
136  }
137  // Skip further authentication methods
138  if (!$validPasswd) {
139  $this->authenticationFailed = true;
140  }
141  } elseif (preg_match('/[0-9abcdef]{32,32}/', $user['password'])) {
142  $validPasswd = md5($password) === (string)$user['password'];
143  // Skip further authentication methods
144  if (!$validPasswd) {
145  $this->authenticationFailed = true;
146  }
147  } else {
148  $validPasswd = (string)$password === (string)$user['password'];
149  }
150  // Should we store the new format value in DB?
151  if ($validPasswd && (int)$this->extConf['updatePasswd']) {
152  // Instanciate default method class
153  $this->objInstanceSaltedPW = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(null);
154  $this->updatePassword((int)$user['uid'], array('password' => $this->objInstanceSaltedPW->getHashedPassword($password)));
155  }
156  }
157  return $validPasswd;
158  }
159 
171  public function authUser(array $user)
172  {
173  $OK = 100;
174  $validPasswd = false;
175  if ($this->login['uident'] && $this->login['uname']) {
176  if (!empty($this->login['uident_text'])) {
177  $validPasswd = $this->compareUident($user, $this->login);
178  }
179  if (!$validPasswd) {
180  // Failed login attempt (wrong password)
181  $errorMessage = 'Login-attempt from %s (%s), username \'%s\', password not accepted!';
182  // No delegation to further services
183  if ((int)$this->extConf['onlyAuthService'] || $this->authenticationFailed) {
184  $this->writeLogMessage(TYPO3_MODE . ' Authentication failed - wrong password for username \'%s\'', $this->login['uname']);
185  $OK = 0;
186  } else {
187  $this->writeLogMessage($errorMessage, $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']);
188  }
189  $this->writelog(255, 3, 3, 1, $errorMessage, array(
190  $this->authInfo['REMOTE_ADDR'],
191  $this->authInfo['REMOTE_HOST'],
192  $this->login['uname']
193  ));
194  \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);
195  } elseif ($validPasswd && $user['lockToDomain'] && strcasecmp($user['lockToDomain'], $this->authInfo['HTTP_HOST'])) {
196  // Lock domain didn't match, so error:
197  $errorMessage = 'Login-attempt from %s (%s), username \'%s\', locked domain \'%s\' did not match \'%s\'!';
198  $this->writeLogMessage($errorMessage, $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname'], $user['lockToDomain'], $this->authInfo['HTTP_HOST']);
199  $this->writelog(255, 3, 3, 1, $errorMessage, array(
200  $this->authInfo['REMOTE_ADDR'],
201  $this->authInfo['REMOTE_HOST'],
202  $user[$this->db_user['username_column']],
203  $user['lockToDomain'],
204  $this->authInfo['HTTP_HOST']
205  ));
206  \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);
207  $OK = 0;
208  } elseif ($validPasswd) {
209  $this->writeLogMessage(TYPO3_MODE . ' Authentication successful for username \'%s\'', $this->login['uname']);
210  $OK = 200;
211  }
212  }
213  return $OK;
214  }
215 
223  protected function updatePassword($uid, $updateFields)
224  {
225  $GLOBALS['TYPO3_DB']->exec_UPDATEquery($this->pObj->user_table, sprintf('uid = %u', $uid), $updateFields);
226  \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);
227  }
228 
242  public function writeLogMessage($message)
243  {
244  if (func_num_args() > 1) {
245  $params = func_get_args();
246  array_shift($params);
247  $message = vsprintf($message, $params);
248  }
249  if (TYPO3_MODE === 'BE') {
250  \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog($message, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_NOTICE);
251  } else {
252  $GLOBALS['TT']->setTSlogMessage($message);
253  }
254  if (TYPO3_DLOG) {
255  \TYPO3\CMS\Core\Utility\GeneralUtility::devLog($message, $this->extKey, \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_NOTICE);
256  }
257  }
258 }