forked from TestLinkOpenSourceTRMS/testlink-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
415 lines (353 loc) · 11.8 KB
/
login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* TestLink Open Source Project - http://testlink.sourceforge.net/
* This script is distributed under the GNU General Public License 2 or later.
*
* Login page with configuratin checking and authorization
*
* @filesource login.php
* @package TestLink
* @copyright 2006,2019 TestLink community
* @link http://www.testlink.org
*
**/
require_once('lib/functions/configCheck.php');
checkConfiguration();
require_once('config.inc.php');
require_once('common.php');
require_once('oauth_api.php');
require_once('doAuthorize.php');
$templateCfg = templateConfiguration();
$doRenderLoginScreen = false;
$doAuthPostProcess = false;
doDBConnect($db, database::ONERROREXIT);
$args = init_args();
$gui = init_gui($db,$args);
// if these checks fail => we will redirect to login screen with some message
doBlockingChecks($db,$gui);
switch($args->action) {
case 'doLogin':
case 'ajaxlogin':
doSessionStart(true);
// When doing ajax login we need to skip control regarding session already open
// that we use when doing normal login.
// If we do not proceed this way we will enter an infinite loop
$options = new stdClass();
$options->doSessionExistsCheck = ($args->action =='doLogin');
$op = doAuthorize($db,$args->login,$args->pwd,$options);
$doAuthPostProcess = true;
$gui->draw = true;
break;
case 'ajaxcheck':
processAjaxCheck($db);
break;
case 'oauth':
// If code is empty then break
if (!isset($args->oauth_code)){
renderLoginScreen($gui);
die();
}
// Switch between oauth providers
if (!include_once('lib/functions/oauth_providers/'.$args->oauth_name.'.php')) {
die("Oauth client doesn't exist");
}
$oau = config_get('OAuthServers');
foreach ($oau as $oprov) {
if (strcmp($oprov['oauth_name'],$args->oauth_name) == 0){
$oauth_params = $oprov;
break;
}
}
$user_token = oauth_get_token($oauth_params, $args->oauth_code);
if($user_token->status['status'] == tl::OK) {
doSessionStart(true);
$op = doAuthorize($db,$user_token->options->user,'oauth',$user_token->options);
$doAuthPostProcess = true;
} else {
$gui->note = $user_token->status['msg'];
$gui->draw=true;
renderLoginScreen($gui);
die();
}
break;
case 'loginform':
$doRenderLoginScreen = true;
$gui->draw = true;
$op = null;
// unfortunatelly we use $args->note in order to do some logic.
if( ($args->note=trim($args->note)) == "" ) {
if( $gui->authCfg['SSO_enabled'] ) {
doSessionStart(true);
$doAuthPostProcess = true;
switch ($gui->authCfg['SSO_method']) {
case 'CLIENT_CERTIFICATE':
$op = doSSOClientCertificate($db,$_SERVER,$gui->authCfg);
break;
case 'WEBSERVER_VAR':
//DEBUGsyslogOnCloud('Trying to execute SSO using SAML');
$op = doSSOWebServerVar($db,$gui->authCfg);
break;
}
}
}
break;
}
if( $doAuthPostProcess ) {
list($doRenderLoginScreen,$gui->note) = authorizePostProcessing($args,$op);
}
if( $doRenderLoginScreen ) {
renderLoginScreen($gui);
}
/**
*
*
*/
function init_args() {
$pwdInputLen = config_get('loginPagePasswordMaxLenght');
// 2010904 - eloff - Why is req and reqURI parameters to the login?
$iParams = array("note" => array(tlInputParameter::STRING_N,0,255),
"tl_login" => array(tlInputParameter::STRING_N,0,100),
"tl_password" => array(tlInputParameter::STRING_N,0,$pwdInputLen),
"req" => array(tlInputParameter::STRING_N,0,4000),
"reqURI" => array(tlInputParameter::STRING_N,0,4000),
"action" => array(tlInputParameter::STRING_N,0, 10),
"destination" => array(tlInputParameter::STRING_N, 0, 255),
"loginform_token" => array(tlInputParameter::STRING_N, 0, 255),
"viewer" => array(tlInputParameter::STRING_N, 0, 3),
"oauth" => array(tlInputParameter::STRING_N,0,100),
"code" => array(tlInputParameter::STRING_N,0,4000),
"state" => array(tlInputParameter::STRING_N,0,100),
);
$pParams = R_PARAMS($iParams);
$args = new stdClass();
$args->note = $pParams['note'];
$args->login = $pParams['tl_login'];
$args->pwd = $pParams['tl_password'];
$args->ssodisable = getSSODisable();
$args->reqURI = urlencode($pParams['req']);
$args->preqURI = urlencode($pParams['reqURI']);
$args->destination = urldecode($pParams['destination']);
$args->loginform_token = urldecode($pParams['loginform_token']);
$args->viewer = $pParams['viewer'];
$k2c = array('ajaxcheck' => 'do','ajaxlogin' => 'do');
if (isset($k2c[$pParams['action']])) {
$args->action = $pParams['action'];
} else if (!is_null($args->login)) {
$args->action = 'doLogin';
// This 'if' branch may be removed in later versions. Kept for compatibility
} else if (!is_null($pParams['oauth']) && $pParams['oauth']) {
$args->action = 'oauth';
$args->oauth_name = $pParams['oauth'];
$args->oauth_code = $pParams['code'];
} else if (!is_null($pParams['state']) && !is_null($pParams['code'])) {
$args->action = 'oauth';
$args->oauth_name = $pParams['state'];
$args->oauth_code = $pParams['code'];
} else {
$args->action = 'loginform';
}
// whitelist oauth_name
if (strcasecmp($args->action,'oauth') == 0) {
validateOauth($args->oauth_name);
}
return $args;
}
/**
*
*/
function validateOauth($name) {
$name = trim($name);
$oauthServers = config_get('OAuthServers');
$whitelistOK = false;
foreach ($oauthServers as $serverCfg) {
if (strcasecmp($name, $serverCfg['oauth_name']) == 0) {
$whitelistOK = true;
break;
}
}
if ($whitelistOK == false) {
die("Invalid Oauth Service");
}
}
/**
*
*
*/
function init_gui(&$db,$args) {
$gui = new stdClass();
$gui->viewer = $args->viewer;
$secCfg = config_get('config_check_warning_frequence');
$gui->securityNotes = '';
if( (strcmp($secCfg, 'ALWAYS') == 0) ||
(strcmp($secCfg, 'ONCE_FOR_SESSION') == 0 && !isset($_SESSION['getSecurityNotesDone'])) ) {
$_SESSION['getSecurityNotesDone'] = 1;
$gui->securityNotes = getSecurityNotes($db);
}
$gui->authCfg = config_get('authentication');
$gui->user_self_signup = config_get('user_self_signup');
// Oauth buttons
$oau = config_get('OAuthServers');
$gui->oauth = array();
foreach ($oau as $oauth_prov) {
if ($oauth_prov['oauth_enabled']) {
$name = $oauth_prov['oauth_name'];
$gui->oauth[$name] = new stdClass();
$gui->oauth[$name]->name = ucfirst($name);
$gui->oauth[$name]->link = oauth_link($oauth_prov);
$gui->oauth[$name]->icon = $name . '.png';
}
}
$gui->external_password_mgmt = false;
$domain = $gui->authCfg['domain'];
$mm = $gui->authCfg['method'];
if( isset($domain[$mm]) ) {
$ac = $domain[$mm];
$gui->external_password_mgmt = !$ac['allowPasswordManagement'];
}
$gui->login_disabled = (('LDAP' == $gui->authCfg['method']) && !checkForLDAPExtension()) ? 1 : 0;
switch($args->note) {
case 'expired':
if(!isset($_SESSION)) {
session_start();
}
session_unset();
session_destroy();
$gui->note = lang_get('session_expired');
$gui->reqURI = null;
break;
case 'first':
$gui->note = lang_get('your_first_login');
$gui->reqURI = null;
break;
case 'lost':
$gui->note = lang_get('passwd_lost');
$gui->reqURI = null;
break;
default:
$gui->note = '';
break;
}
$gui->ssodisable = 0;
if(property_exists($args,'ssodisable')) {
$gui->ssodisable = $args->ssodisable;
}
$gui->reqURI = $args->reqURI ? $args->reqURI : $args->preqURI;
$gui->destination = $args->destination;
$gui->pwdInputMaxLenght = config_get('loginPagePasswordMaxLenght');
// Random Background
$imgSet = array();
$imgSet[] = "wp-testing04.jpg";
$imgSet[] = "Fedora-24-Default-Wallpaper-1.png";
$imgSet[] = "fedora-76343.jpg";
$imgSet[] = "fedora-hex_0.002.png";
$imgSet[] = "fedora-x.jpeg";
$imgSet[] = "fedora21.png-1024x640.jpg";
$imgSet[] = "fedora28.png";
$itemQty = count($imgSet)-1;
$ixx = rand(0,$itemQty);
$gui->loginBackgroundImg =
"gui/templates/dashio/img/login/" . $imgSet[$ixx];
return $gui;
}
/**
* doBlockingChecks
*
* wrong Schema version will BLOCK ANY login action
*
* @param &$dbHandler DataBase Handler
* @param &$guiObj some gui elements that will be used to give feedback
*
*/
function doBlockingChecks(&$dbHandler,&$guiObj) {
$op = checkSchemaVersion($dbHandler);
if( $op['status'] < tl::OK ) {
// Houston we have a problem
// This check to kill session was added to avoid following situation
// TestLink 1.9.5 installed
// Install TestLink 1.9.6 in another folder, pointing to same OLD DB
// you logged in TL 1.9.5 => session is created
// you try to login to 1.9.6, you get the Update DB Schema message but
// anyway because a LIVE AND VALID session you are allowed to login => BAD
if(isset($op['kill_session']) && $op['kill_session']) {
session_unset();
session_destroy();
}
$guiObj->draw = false;
$guiObj->note = $op['msg'];
renderLoginScreen($guiObj);
die();
}
}
/**
* renderLoginScreen
* simple piece of code used to clean up code layout
*
* @global $g_tlLogger
* @param stdClassObject $guiObj
*/
function renderLoginScreen($guiObj) {
global $g_tlLogger;
$templateCfg = templateConfiguration();
$logPeriodToDelete = config_get('removeEventsOlderThan');
$g_tlLogger->deleteEventsFor(null, strtotime("-{$logPeriodToDelete} days UTC"));
$smarty = new TLSmarty();
$smarty->assign('gui', $guiObj);
$templ = config_get('tpl');
$tpl = $templ['login'];
$smarty->display($tpl);
}
/**
*
* @param stdClassObject $argsObj
* @param hash $op
*/
function authorizePostProcessing($argsObj,$op) {
$note = null;
$renderLoginScreen = false;
if($op['status'] == tl::OK) {
// Login successful, redirect to destination
logAuditEvent(TLS("audit_login_succeeded",$argsObj->login,
$_SERVER['REMOTE_ADDR']),"LOGIN",$_SESSION['currentUser']->dbID,"users");
if ($argsObj->action == 'ajaxlogin') {
echo json_encode(array('success' => true));
} else {
// If destination param is set redirect to given page ...
if (!empty($argsObj->destination) && preg_match("/linkto.php/", $argsObj->destination)) {
redirect($argsObj->destination);
} else {
// ... or show main page
$_SESSION['viewer'] = $argsObj->viewer;
$ad = $argsObj->ssodisable ? '&ssodisable=1' : '';
$ad .= ($argsObj->preqURI ? "&reqURI=".urlencode($argsObj->preqURI) :"");
$rul = $_SESSION['basehref'] .
"index.php?caller=login&viewer={$argsObj->viewer}" . $ad;
redirect($rul);
}
exit(); // hmm seems is useless
}
} else {
$note = '';
if(!$argsObj->ssodisable) {
$note = is_null($op['msg']) ? lang_get('bad_user_passwd') : $op['msg'];
}
if($argsObj->action == 'ajaxlogin') {
echo json_encode(array('success' => false,'reason' => $note));
} else {
$renderLoginScreen = true;
}
}
return array($renderLoginScreen,$note);
}
/**
*
*
*/
function processAjaxCheck(&$dbHandler) {
// Send a json reply, include localized strings for use in js to display a login form.
doSessionStart(true);
echo json_encode(array('validSession' => checkSessionValid($dbHandler, false),
'username_label' => lang_get('login_name'),
'password_label' => lang_get('password'),
'login_label' => lang_get('btn_login'),
'timeout_info' => lang_get('timeout_info')));
}