Integrated saltedpasswords completely instead of using svn:externals
[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 * $Id$
32 */
33
34
35 /**
36 * Class implements salted-password hashes authentication service.
37 *
38 * @author Marcus Krause <marcus#exp2009@t3sec.info>
39 * @author Steffen Ritter <info@rs-websystems.de>
40 *
41 * @since 2009-06-14
42 * @package TYPO3
43 * @subpackage tx_saltedpasswords
44 */
45 class tx_saltedpasswords_sv1 extends tx_sv_authbase {
46 /**
47 * Keeps class name.
48 *
49 * @var string
50 */
51 public $prefixId = 'tx_saltedpasswords_sv1';
52
53 /**
54 * Keeps path to this script relative to the extension directory.
55 *
56 * @var string
57 */
58 public $scriptRelPath = 'sv1/class.tx_saltedpasswords_sv1.php';
59
60 /**
61 * Keeps extension key.
62 *
63 * @var string
64 */
65 public $extKey = 'saltedpasswords';
66
67 /**
68 * Keeps extension configuration.
69 *
70 * @var mixed
71 */
72 protected $extConf;
73
74 /**
75 * An instance of the salted hashing method.
76 * This member is set in the getSaltingInstance() function.
77 *
78 * @var tx_saltedpasswords_abstract_salts
79 */
80 protected $objInstanceSaltedPW = NULL;
81
82
83 /**
84 * Checks if service is available. In case of this service we check that
85 * following prerequesties are fulfilled:
86 * - loginSecurityLevel of according TYPO3_MODE is set to normal
87 *
88 * @return boolean TRUE if service is available
89 */
90 public function init() {
91 $available = FALSE;
92
93 if (tx_saltedpasswords_div::isUsageEnabled()) {
94 $available = TRUE;
95 $this->extConf = tx_saltedpasswords_div::returnExtConf();
96 }
97
98 return $available ? parent::init() : FALSE;
99 }
100
101 /**
102 * Checks the login data with the user record data for builtin login method.
103 *
104 * @param array user data array
105 * @param array login data array
106 * @param string login security level (optional)
107 * @return boolean TRUE if login data matched
108 */
109 function compareUident(array $user, array $loginData, $security_level = 'normal') {
110 $validPasswd = FALSE;
111
112 // could be merged; still here to clarify
113 if (!strcmp(TYPO3_MODE, 'BE')) {
114 $password = $loginData['uident_text'];
115 } else if (!strcmp(TYPO3_MODE, 'FE')) {
116 $password = $loginData['uident_text'];
117 }
118
119 // determine method used for given salted hashed password
120 $this->objInstanceSaltedPW = tx_saltedpasswords_salts_factory::getSaltingInstance($user['password']);
121
122 // existing record is in format of Salted Hash password
123 if (is_object($this->objInstanceSaltedPW)) {
124 $validPasswd = $this->objInstanceSaltedPW->checkPassword($password,$user['password']);
125
126 $defaultHashingClassName = tx_saltedpasswords_div::getDefaultSaltingHashingMethod();
127 $skip = FALSE;
128
129 // test for wrong salted hashing method
130 if ($validPasswd && !(get_class($this->objInstanceSaltedPW) == $defaultHashingClassName) || (is_subclass_of($this->objInstanceSaltedPW, $defaultHashingClassName))) {
131 // instanciate default method class
132 $this->objInstanceSaltedPW = tx_saltedpasswords_salts_factory::getSaltingInstance(NULL);
133 $this->updatePassword(
134 intval($user['uid']),
135 array('password' => $this->objInstanceSaltedPW->getHashedPassword($password))
136 );
137 }
138
139 if ($validPasswd && !$skip && $this->objInstanceSaltedPW->isHashUpdateNeeded($user['password'])) {
140 $this->updatePassword(
141 intval($user['uid']),
142 array('password' => $this->objInstanceSaltedPW->getHashedPassword($password))
143 );
144 }
145 // we process also clear-text, md5 and passwords updated by Portable PHP password hashing framework
146 } else if (!intval($this->extConf['forceSalted'])) {
147
148 // stored password is in deprecated salted hashing method
149 if (t3lib_div::inList('C$,M$', substr($user['password'], 0, 2))) {
150
151 // instanciate default method class
152 $this->objInstanceSaltedPW = tx_saltedpasswords_salts_factory::getSaltingInstance(substr($user['password'], 1));
153
154 // md5
155 if (!strcmp(substr($user['password'], 0, 1), 'M')) {
156 $validPasswd = $this->objInstanceSaltedPW->checkPassword(md5($password), substr($user['password'], 1));
157 } else {
158 $validPasswd = $this->objInstanceSaltedPW->checkPassword(md5($password), substr($user['password'], 1));
159 }
160
161 // password is stored as md5
162 } else if (preg_match('/[0-9abcdef]{32,32}/', $user['password'])) {
163 $validPasswd = (!strcmp(md5($password), $user['password']) ? TRUE : FALSE);
164
165 // password is stored plain or unrecognized format
166 } else {
167 $validPasswd = (!strcmp($password, $user['password']) ? TRUE : FALSE);
168 }
169 // should we store the new format value in DB?
170 if ($validPasswd && intval($this->extConf['updatePasswd'])) {
171 // instanciate default method class
172 $this->objInstanceSaltedPW = tx_saltedpasswords_salts_factory::getSaltingInstance(NULL);
173 $this->updatePassword(
174 intval($user['uid']),
175 array('password' => $this->objInstanceSaltedPW->getHashedPassword($password))
176 );
177 }
178 }
179
180 return $validPasswd;
181 }
182
183 /**
184 * Method adds a further authUser method.
185 *
186 * Will return one of following authentication status codes:
187 * - 0 - authentication failure
188 * - 100 - just go on. User is not authenticated but there is still no reason to stop
189 * - 200 - the service was able to authenticate the user
190 *
191 * @param array Array containing FE user data of the logged user.
192 * @return integer authentication statuscode, one of 0,100 and 200
193 */
194 public function authUser(array $user) {
195 $OK = 100;
196 $validPasswd = FALSE;
197
198 if ($this->pObj->security_level == 'rsa' && t3lib_extMgm::isLoaded('rsaauth')) {
199 require_once(t3lib_extMgm::extPath('rsaauth') . 'sv1/backends/class.tx_rsaauth_backendfactory.php');
200 require_once(t3lib_extMgm::extPath('rsaauth') . 'sv1/storage/class.tx_rsaauth_storagefactory.php');
201
202 $backend = tx_rsaauth_backendfactory::getBackend();
203 $storage = tx_rsaauth_storagefactory::getStorage();
204 // Preprocess the password
205 $password = $this->login['uident'];
206 $key = $storage->get();
207 if ($key != NULL && substr($password, 0, 4) == 'rsa:') {
208 // Decode password and pass to parent
209 $decryptedPassword = $backend->decrypt($key, substr($password, 4));
210 $this->login['uident_text'] = $decryptedPassword;
211 }
212 }
213
214 if ($this->login['uident'] && $this->login['uname']) {
215 if (!empty($this->login['uident_text'])) {
216 $validPasswd = $this->compareUident(
217 $user,
218 $this->login
219 );
220 }
221
222 if (!$validPasswd && intval($this->extConf['onlyAuthService'])) {
223 // Failed login attempt (wrong password) - no delegation to further services
224 $this->writeLog(
225 TYPO3_MODE . ' Authentication failed - wrong password for username \'%s\'',
226 $this->login['uname']
227 );
228 $OK = 0;
229 } else if(!$validPasswd) {
230 // Failed login attempt (wrong password)
231 $this->writeLog(
232 "Login-attempt from %s, username '%s', password not accepted!",
233 $this->authInfo['REMOTE_ADDR'], $this->login['uname']
234 );
235 } else if ($validPasswd && $user['lockToDomain'] && strcasecmp($user['lockToDomain'], $this->authInfo['HTTP_HOST'])) {
236 // Lock domain didn't match, so error:
237 $this->writeLog(
238 "Login-attempt from %s, username '%s', locked domain '%s' did not match '%s'!",
239 $this->authInfo['REMOTE_ADDR'], $this->login['uname'], $user['lockToDomain'], $this->authInfo['HTTP_HOST']
240 );
241 $OK = 0;
242 } else if ($validPasswd) {
243 $this->writeLog(
244 TYPO3_MODE . ' Authentication successful for username \'%s\'',
245 $this->login['uname']
246 );
247 $OK = 200;
248 }
249 }
250
251 return $OK;
252 }
253
254 /**
255 * Method updates a FE/BE user record - in this case a new password string will be set.
256 *
257 * @param integer $uid: uid of user record that will be updated
258 * @param mixed $updateFields: Field values as key=>value pairs to be updated in database
259 * @return void
260 */
261 protected function updatePassword($uid, $updateFields) {
262 if (TYPO3_MODE === 'BE') {
263 $GLOBALS['TYPO3_DB']->exec_UPDATEquery( 'be_users', sprintf('uid = %u', $uid), $updateFields);
264 } else {
265 $GLOBALS['TYPO3_DB']->exec_UPDATEquery( 'fe_users', sprintf('uid = %u', $uid), $updateFields);
266 }
267
268 t3lib_div::devLog(sprintf('Automatic password update for %s user with uid %u', TYPO3_MODE, $uid), $this->extKey, 1);
269 }
270
271 /**
272 * Writes log message. Destination log depends on the current system mode.
273 * For FE the function writes to the admin panel log. For BE messages are
274 * sent to the system log. If developer log is enabled, messages are also
275 * sent there.
276 *
277 * This function accepts variable number of arguments and can format
278 * parameters. The syntax is the same as for sprintf()
279 *
280 * @param string $message: Message to output
281 * @return void
282 * @see sprintf()
283 * @see t3lib::divLog()
284 * @see t3lib_div::sysLog()
285 * @see t3lib_timeTrack::setTSlogMessage()
286 */
287 function writeLog($message) {
288 if (func_num_args() > 1) {
289 $params = func_get_args();
290 array_shift($params);
291 $message = vsprintf($message, $params);
292 }
293
294 if (TYPO3_MODE === 'BE') {
295 t3lib_div::sysLog($message, $this->extKey, 1);
296 } else {
297 $GLOBALS['TT']->setTSlogMessage($message);
298 }
299
300 if (TYPO3_DLOG) {
301 t3lib_div::devLog($message, $this->extKey, 1);
302 }
303 }
304 }
305
306
307 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/saltedpasswords/sv1/class.tx_saltedpasswords_sv1.php']) {
308 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/saltedpasswords/sv1/class.tx_saltedpasswords_sv1.php']);
309 }
310 ?>