00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00089 class t3lib_userAuth {
00090 var $global_database = '';
00091 var $session_table = '';
00092 var $name = '';
00093 var $get_name = '';
00094
00095 var $user_table = '';
00096 var $username_column = '';
00097 var $userident_column = '';
00098 var $userid_column = '';
00099 var $lastLogin_column = '';
00100
00101 var $enablecolumns = Array (
00102 'rootLevel' => '',
00103 'disabled' => '',
00104 'starttime' => '',
00105 'endtime' => '',
00106 'deleted' => ''
00107 );
00108
00109 var $formfield_uname = '';
00110 var $formfield_uident = '';
00111 var $formfield_chalvalue = '';
00112 var $formfield_status = '';
00113 var $security_level = '';
00114
00115 var $auth_include = '';
00116
00117 var $auth_timeout_field = 0;
00118 var $lifetime = 0;
00119 var $gc_time = 24;
00120 var $gc_probability = 1;
00121 var $writeStdLog = 0;
00122 var $writeAttemptLog = 0;
00123 var $sendNoCacheHeaders = 1;
00124 var $getFallBack = 0;
00125 var $hash_length = 32;
00126 var $getMethodEnabled = 0;
00127 var $lockIP = 4;
00128 var $lockHashKeyWords = 'useragent';
00129
00130 var $warningEmail = '';
00131 var $warningPeriod = 3600;
00132 var $warningMax = 3;
00133 var $checkPid=1;
00134 var $checkPid_value=0;
00135
00136
00137 var $id;
00138 var $cookieId;
00139 var $loginSessionStarted = 0;
00140
00141 var $user;
00142 var $get_URL_ID = '';
00143
00144 var $forceSetCookie=0;
00145 var $dontSetCookie=0;
00146
00147
00159 function start() {
00160
00161
00162 $mode='';
00163 $new_id = false;
00164 $id = isset($_COOKIE[$this->name]) ? stripslashes($_COOKIE[$this->name]) : '';
00165 $this->hash_length = t3lib_div::intInRange($this->hash_length,6,32);
00166
00167
00168 if (!$id && $this->getFallBack && $this->get_name) {
00169 $id = isset($_GET[$this->get_name]) ? t3lib_div::_GET($this->get_name) : '';
00170 if (strlen($id)!=$this->hash_length) $id='';
00171 $mode='get';
00172 }
00173 $this->cookieId = $id;
00174
00175 if (!$id) {
00176 $id = substr(md5(uniqid('')),0,$this->hash_length);
00177 $new_id = true;
00178 }
00179
00180 $this->id = $id;
00181 if ($mode=='get' && $this->getFallBack && $this->get_name) {
00182 $this->get_URL_ID = '&'.$this->get_name.'='.$id;
00183 }
00184 $this->user = '';
00185
00186
00187 if (($new_id || $this->forceSetCookie) && $this->lifetime==0 ) {
00188 if (!$this->dontSetCookie) SetCookie($this->name, $id, 0, '/');
00189 }
00190 if ($this->lifetime > 0) {
00191 if (!$this->dontSetCookie) SetCookie($this->name, $id, time()+$this->lifetime, '/');
00192 }
00193
00194
00195 if ($this->formfield_status) {
00196 $this->check_authentication();
00197 }
00198 unset($this->user);
00199
00200
00201
00202 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00203 '*',
00204 $this->session_table.','.$this->user_table,
00205 $this->session_table.'.ses_id = "'.$GLOBALS['TYPO3_DB']->quoteStr($this->id, $this->session_table).'"
00206 AND '.$this->session_table.'.ses_name = "'.$GLOBALS['TYPO3_DB']->quoteStr($this->name, $this->session_table).'"
00207 AND '.$this->session_table.'.ses_userid = '.$this->user_table.'.'.$this->userid_column.'
00208 '.$this->ipLockClause().'
00209 '.$this->hashLockClause().'
00210 '.$this->user_where_clause()
00211 );
00212
00213 if ($this->user = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres)) {
00214
00215 if (is_string($this->auth_timeout_field)) {
00216 $timeout = intval($this->user[$this->auth_timeout_field]);
00217 } else {
00218 $timeout = intval($this->auth_timeout_field);
00219 }
00220
00221
00222 if ($timeout>0 && ($GLOBALS['EXEC_TIME'] < ($this->user['ses_tstamp']+$timeout))) {
00223 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
00224 $this->session_table,
00225 'ses_id="'.$GLOBALS['TYPO3_DB']->quoteStr($this->id, $this->session_table).'"
00226 AND ses_name="'.$GLOBALS['TYPO3_DB']->quoteStr($this->name, $this->session_table).'"',
00227 array('ses_tstamp' => $GLOBALS['EXEC_TIME'])
00228 );
00229 $this->user['ses_tstamp'] = $GLOBALS['EXEC_TIME'];
00230 } else {
00231 $this->user = '';
00232 $this->logoff();
00233 }
00234 } else {
00235 $this->logoff();
00236 }
00237
00238 $this->redirect();
00239
00240
00241 if ($this->sendNoCacheHeaders) {
00242 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
00243 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
00244 header('Expires: 0');
00245 header('Cache-Control: no-cache, must-revalidate');
00246 header('Pragma: no-cache');
00247 }
00248
00249
00250 if ((rand()%100) <= $this->gc_probability) {
00251 $this->gc();
00252 }
00253 }
00254
00261 function check_authentication() {
00262
00263
00264 if ($this->getMethodEnabled) {
00265 $F_status = t3lib_div::_GP($this->formfield_status);
00266 $F_uname = t3lib_div::_GP($this->formfield_uname);
00267 $F_uident = t3lib_div::_GP($this->formfield_uident);
00268 $F_chalvalue = t3lib_div::_GP($this->formfield_chalvalue);
00269 } else {
00270 $F_status = t3lib_div::_POST($this->formfield_status);
00271 $F_uname = t3lib_div::_POST($this->formfield_uname);
00272 $F_uident = t3lib_div::_POST($this->formfield_uident);
00273 $F_chalvalue = t3lib_div::_POST($this->formfield_chalvalue);
00274 }
00275
00276 switch ($F_status) {
00277 case 'login':
00278 $refInfo=parse_url(t3lib_div::getIndpEnv('HTTP_REFERER'));
00279 $httpHost = t3lib_div::getIndpEnv('TYPO3_HOST_ONLY');
00280 if (!$this->getMethodEnabled && ($httpHost!=$refInfo['host'] && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer'])) {
00281 die('Error: This host address ("'.$httpHost.'") and the referer host ("'.$refInfo['host'].'") mismatches!<br />
00282 It\'s possible that the environment variable HTTP_REFERER is not passed to the script because of a proxy.<br />
00283 The site administrator can disable this check in the "All Configuration" section of the Install Tool (flag: TYPO3_CONF_VARS[SYS][doNotCheckReferer]).');
00284 }
00285 if ($F_uident && $F_uname) {
00286
00287
00288 $loginFailure=0;
00289
00290
00291 $this->logoff();
00292
00293
00294 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00295 '*',
00296 $this->user_table,
00297 ($this->checkPid ? 'pid IN ('.$GLOBALS['TYPO3_DB']->cleanIntList($this->checkPid_value).') AND ' : '').
00298 $this->username_column.'="'.$GLOBALS['TYPO3_DB']->quoteStr($F_uname, $this->user_table).'" '.
00299 $this->user_where_clause()
00300 );
00301
00302
00303 if ($tempuser = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres)) {
00304
00305 $this->user = $tempuser;
00306
00307
00308 $OK = false;
00309
00310
00311 switch ($this->security_level) {
00312 case 'superchallenged':
00313 case 'challenged':
00314 if (!strcmp($F_uident,md5($tempuser[$this->username_column].':'.$tempuser[$this->userident_column].':'.$F_chalvalue))) {
00315 $OK = true;
00316 };
00317 break;
00318 default:
00319 if (!strcmp($F_uident,$tempuser[$this->userident_column])) {
00320 $OK = true;
00321 };
00322 break;
00323 }
00324
00325
00326 if ($OK) {
00327
00328 if ($this->user['lockToDomain'] && $this->user['lockToDomain']!=t3lib_div::getIndpEnv('HTTP_HOST')) {
00329
00330 if ($this->writeAttemptLog) {
00331 $this->writelog(255,3,3,1,
00332 "Login-attempt from %s (%s), username '%s', locked domain '%s' did not match '%s'!",
00333 Array(t3lib_div::getIndpEnv('REMOTE_ADDR'),t3lib_div::getIndpEnv('REMOTE_HOST'),$F_uname,$this->user['lockToDomain'],t3lib_div::getIndpEnv('HTTP_HOST')));
00334 }
00335 $loginFailure=1;
00336 } else {
00337
00338 $this->loginSessionStarted = 1;
00339
00340
00341 $insertFields = array(
00342 'ses_id' => $this->id,
00343 'ses_name' => $this->name,
00344 'ses_iplock' => $this->user['disableIPlock'] ? '[DISABLED]' : $this->ipLockClause_remoteIPNumber($this->lockIP),
00345 'ses_hashlock' => $this->hashLockClause_getHashInt(),
00346 'ses_userid' => $tempuser[$this->userid_column],
00347 'ses_tstamp' => $GLOBALS['EXEC_TIME']
00348 );
00349 $GLOBALS['TYPO3_DB']->exec_INSERTquery($this->session_table, $insertFields);
00350
00351
00352 if ($this->lastLogin_column) {
00353 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
00354 $this->user_table,
00355 $this->userid_column.'="'.$GLOBALS['TYPO3_DB']->quoteStr($tempuser[$this->userid_column], $this->user_table).'"',
00356 array($this->lastLogin_column => $GLOBALS['EXEC_TIME'])
00357 );
00358 }
00359
00360 if ($this->writeStdLog) {
00361 $this->writelog(255,1,0,1,
00362 'User %s logged in from %s (%s)',
00363 Array($this->user['username'],t3lib_div::getIndpEnv('REMOTE_ADDR'),t3lib_div::getIndpEnv('REMOTE_HOST')));
00364 }
00365 }
00366 } else {
00367
00368 if ($this->writeAttemptLog) {
00369 $this->writelog(255,3,3,1,
00370 "Login-attempt from %s (%s), username '%s', password not accepted!",
00371 Array(t3lib_div::getIndpEnv('REMOTE_ADDR'),t3lib_div::getIndpEnv('REMOTE_HOST'),$F_uname));
00372 }
00373 $loginFailure=1;
00374 }
00375
00376 unset($this->user);
00377 } else {
00378
00379 if ($this->writeAttemptLog) {
00380 $this->writelog(255,3,3,2,
00381 "Login-attempt from %s (%s), username '%s' not found!!",
00382 Array(t3lib_div::getIndpEnv('REMOTE_ADDR'),t3lib_div::getIndpEnv('REMOTE_HOST'),$F_uname));
00383 }
00384 $loginFailure=1;
00385 }
00386
00387
00388 if ($loginFailure) {
00389 $this->checkLogFailures($this->warningEmail, $this->warningPeriod, $this->warningMax);
00390 }
00391 }
00392
00393
00394 return 'login';
00395 break;
00396 case 'logout':
00397
00398 if ($this->writeStdLog) $this->writelog(255,2,0,2,'User %s logged out',Array($this->user['username']));
00399 $this->logoff();
00400
00401
00402 return 'logout';
00403 break;
00404 }
00405 }
00406
00413 function redirect() {
00414 if (!$this->userid && $this->auth_url) {
00415 include ($this->auth_include);
00416 exit;
00417 }
00418 }
00419
00426 function logoff() {
00427 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
00428 $this->session_table,
00429 'ses_id = "'.$GLOBALS['TYPO3_DB']->quoteStr($this->id, $this->session_table).'"
00430 AND ses_name = "'.$GLOBALS['TYPO3_DB']->quoteStr($this->name, $this->session_table).'"'
00431 );
00432 $this->user = "";
00433 }
00434
00441 function gc() {
00442 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
00443 $this->session_table,
00444 'ses_tstamp < '.intval(time()-($this->gc_time*60*60)).'
00445 AND ses_name = "'.$GLOBALS['TYPO3_DB']->quoteStr($this->name, $this->session_table).'"'
00446 );
00447 }
00448
00455 function user_where_clause() {
00456 return (($this->enablecolumns['rootLevel']) ? 'AND '.$this->user_table.'.pid=0 ' : '').
00457 (($this->enablecolumns['disabled']) ? ' AND NOT '.$this->user_table.'.'.$this->enablecolumns['disabled'] : '').
00458 (($this->enablecolumns['deleted']) ? ' AND NOT '.$this->user_table.'.'.$this->enablecolumns['deleted'] : '').
00459 (($this->enablecolumns['starttime']) ? ' AND ('.$this->user_table.'.'.$this->enablecolumns['starttime'].'<='.time().')' : '').
00460 (($this->enablecolumns['endtime']) ? ' AND ('.$this->user_table.'.'.$this->enablecolumns['endtime'].'=0 OR '.$this->user_table.'.'.$this->enablecolumns['endtime'].'>'.time().')' : '');
00461 }
00462
00469 function ipLockClause() {
00470 if ($this->lockIP) {
00471 $wherePart = 'AND (
00472 '.$this->session_table.'.ses_iplock="'.$GLOBALS['TYPO3_DB']->quoteStr($this->ipLockClause_remoteIPNumber($this->lockIP),$this->session_table).'"
00473 OR '.$this->session_table.'.ses_iplock="[DISABLED]"
00474 )';
00475 return $wherePart;
00476 }
00477 }
00478
00487 function ipLockClause_remoteIPNumber($parts) {
00488 $IP = t3lib_div::getIndpEnv('REMOTE_ADDR');
00489
00490 if ($parts>=4) {
00491 return $IP;
00492 } else {
00493 $parts = t3lib_div::intInRange($parts,1,3);
00494 $IPparts = explode('.',$IP);
00495 for($a=4;$a>$parts;$a--) {
00496 unset($IPparts[$a-1]);
00497 }
00498 return implode('.',$IPparts);
00499 }
00500 }
00501
00508 function hashLockClause() {
00509 $wherePart = 'AND '.$this->session_table.'.ses_hashlock='.intval($this->hashLockClause_getHashInt());
00510 return $wherePart;
00511 }
00512
00519 function hashLockClause_getHashInt() {
00520 $hashStr = '';
00521
00522 if (t3lib_div::inList($this->lockHashKeyWords,'useragent')) $hashStr.=':'.t3lib_div::getIndpEnv('HTTP_USER_AGENT');
00523
00524 return t3lib_div::md5int($hashStr);
00525 }
00526
00535 function writeUC($variable='') {
00536 if (is_array($this->user) && $this->user['uid']) {
00537 if (!is_array($variable)) { $variable = $this->uc; }
00538
00539 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($this->user_table, 'uid='.intval($this->user['uid']), array('uc' => serialize($variable)));
00540 }
00541 }
00542
00558 function writelog($type,$action,$error,$details_nr,$details,$data,$tablename,$recuid,$recpid) {
00559 }
00560
00567 function checkLogFailures() {
00568 }
00569
00576 function unpack_uc($theUC='') {
00577 if (!$theUC) $theUC=unserialize($this->user['uc']);
00578 if (is_array($theUC)) {
00579 $this->uc=$theUC;
00580 }
00581 }
00582
00592 function pushModuleData($module,$data,$noSave=0) {
00593 $this->uc['moduleData'][$module] = $data;
00594 $this->uc['moduleSessionID'][$module] = $this->id;
00595 if (!$noSave) $this->writeUC();
00596 }
00597
00605 function getModuleData($module,$type='') {
00606 if ($type!='ses' || $this->uc['moduleSessionID'][$module]==$this->id) {
00607 return $this->uc['moduleData'][$module];
00608 }
00609 }
00610
00618 function getSessionData($key) {
00619 $sesDat = unserialize($this->user['ses_data']);
00620 return $sesDat[$key];
00621 }
00622
00631 function setAndSaveSessionData($key,$data) {
00632 $sesDat = unserialize($this->user['ses_data']);
00633 $sesDat[$key] = $data;
00634 $this->user['ses_data'] = serialize($sesDat);
00635
00636 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($this->session_table, 'ses_id="'.$GLOBALS['TYPO3_DB']->quoteStr($this->user['ses_id'], $this->session_table).'"', array('ses_data' => $this->user['ses_data']));
00637 }
00638
00650 function setBeUserByUid($uid) {
00651 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->user_table, 'uid="'.intval($uid).'" '.$this->user_where_clause());
00652 $this->user = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres);
00653 }
00654
00663 function setBeUserByName($name) {
00664 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->user_table, 'username="'.$GLOBALS['TYPO3_DB']->quoteStr($name, $this->user_table).'" '.$this->user_where_clause());
00665 $this->user = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres);
00666 }
00667 }
00668
00669
00670
00671 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_userauth.php']) {
00672 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_userauth.php']);
00673 }
00674 ?>