/
SugarAuthenticate.php
executable file
·476 lines (416 loc) · 19 KB
/
SugarAuthenticate.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/**
*
* SugarCRM Community Edition is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
*
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
* Copyright (C) 2011 - 2018 SalesAgility Ltd.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
*/
if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
}
/**
* This file is used to control the authentication process.
* It will call on the user authenticate and controll redirection
* based on the users validation
*
*/
#[\AllowDynamicProperties]
class SugarAuthenticate
{
public $userAuthenticateClass = 'SugarAuthenticateUser';
public $authenticationDir = 'SugarAuthenticate';
/**
* @var SugarAuthenticateUser
*/
public $userAuthenticate;
/**
* Constructs SugarAuthenticate
* This will load the user authentication class
*
* @return SugarAuthenticate
*/
public function __construct()
{
// check in custom dir first, in case someone want's to override an auth controller
if (file_exists('custom/modules/Users/authentication/'.$this->authenticationDir.'/' . $this->userAuthenticateClass . '.php')) {
require_once('custom/modules/Users/authentication/'.$this->authenticationDir.'/' . $this->userAuthenticateClass . '.php');
} elseif (file_exists('modules/Users/authentication/'.$this->authenticationDir.'/' . $this->userAuthenticateClass . '.php')) {
require_once('modules/Users/authentication/'.$this->authenticationDir.'/' . $this->userAuthenticateClass . '.php');
}
$this->userAuthenticate = new $this->userAuthenticateClass();
}
/**
* Authenticates a user based on the username and password
* returns true if the user was authenticated false otherwise
* it also will load the user into current user if they were authenticated
*
* @param string $username
* @param string $password
* @return boolean
*/
public function loginAuthenticate($username, $password, $fallback=false, $PARAMS = array())
{
global $mod_strings;
unset($_SESSION['login_error']);
$usr= new user();
$usr_id=$usr->retrieve_user_id($username);
$usr->retrieve($usr_id);
$_SESSION['login_error']='';
$_SESSION['waiting_error']='';
$_SESSION['hasExpiredPassword']='0';
if ($this->userAuthenticate->loadUserOnLogin($username, $password, $fallback, $PARAMS)) {
require_once('modules/Users/password_utils.php');
if (hasPasswordExpired($username)) {
$_SESSION['hasExpiredPassword'] = '1';
}
// now that user is authenticated, reset loginfailed
if ($usr->getPreference('loginfailed') != '' && $usr->getPreference('loginfailed') != 0) {
$usr->setPreference('loginfailed', '0');
$usr->savePreferencesToDB();
}
return $this->postLoginAuthenticate();
} else {
//if(!empty($usr_id) && $res['lockoutexpiration'] > 0){
if (!empty($usr_id)) {
if (($logout=$usr->getPreference('loginfailed'))=='') {
$usr->setPreference('loginfailed', '1');
} else {
$usr->setPreference('loginfailed', $logout+1);
}
$usr->savePreferencesToDB();
}
}
if (strtolower(get_class($this)) != 'sugarauthenticate') {
$sa = new SugarAuthenticate();
$error = (!empty($_SESSION['login_error']))?$_SESSION['login_error']:'';
if ($sa->loginAuthenticate($username, $password, true, $PARAMS)) {
return true;
}
$_SESSION['login_error'] = $error;
}
$_SESSION['login_user_name'] = $username;
$_SESSION['login_password'] = $password;
if (empty($_SESSION['login_error'])) {
$_SESSION['login_error'] = translate('ERR_INVALID_PASSWORD', 'Users');
}
return false;
}
/**
* Once a user is authenticated on login this function will be called. Populate the session with what is needed and log anything that needs to be logged
*
*/
public function postLoginAuthenticate()
{
global $reset_language_on_default_user, $sugar_config;
//just do a little house cleaning here
unset($_SESSION['login_password']);
unset($_SESSION['login_error']);
unset($_SESSION['login_user_name']);
unset($_SESSION['ACL']);
//set the server unique key
if (isset($sugar_config['unique_key'])) {
$_SESSION['unique_key'] = $sugar_config['unique_key'];
}
//set user language
if (isset($reset_language_on_default_user) && $reset_language_on_default_user && $GLOBALS['current_user']->user_name == $sugar_config['default_user_name']) {
$authenticated_user_language = $sugar_config['default_language'];
} else {
$authenticated_user_language = isset($_REQUEST['login_language']) ? $_REQUEST['login_language'] : (isset($_REQUEST['ck_login_language_20']) ? $_REQUEST['ck_login_language_20'] : $sugar_config['default_language']);
}
$_SESSION['authenticated_user_language'] = $authenticated_user_language;
$GLOBALS['log']->debug("authenticated_user_language is $authenticated_user_language");
// Clear all uploaded import files for this user if it exists
require_once('modules/Import/ImportCacheFiles.php');
$tmp_file_name = ImportCacheFiles::getImportDir()."/IMPORT_" . $GLOBALS['current_user']->id;
if (file_exists($tmp_file_name)) {
unlink($tmp_file_name);
}
return true;
}
/**
* On every page hit this will be called to ensure a user is authenticated
* @return boolean
*/
public function sessionAuthenticate()
{
global $module, $action, $allowed_actions;
$authenticated = false;
$allowed_actions = array("Authenticate", "Login"); // these are actions where the user/server keys aren't compared
if (isset($_SESSION['authenticated_user_id'])) {
$GLOBALS['log']->debug("We have an authenticated user id: ".$_SESSION["authenticated_user_id"]);
$authenticated = $this->postSessionAuthenticate();
} else {
if (isset($action) && isset($module) && $action == "Authenticate" && $module == "Users") {
$GLOBALS['log']->debug("We are authenticating user now");
} else {
$GLOBALS['log']->debug("The current user does not have a session. Going to the login page");
$action = "Login";
$module = "Users";
$_REQUEST['action'] = $action;
$_REQUEST['module'] = $module;
}
}
if (empty($GLOBALS['current_user']->id) && !in_array($action, $allowed_actions)) {
$GLOBALS['log']->debug("The current user is not logged in going to login page");
$action = "Login";
$module = "Users";
$_REQUEST['action'] = $action;
$_REQUEST['module'] = $module;
}
if ($authenticated && ((empty($_REQUEST['module']) || empty($_REQUEST['action'])) || ($_REQUEST['module'] != 'Users' || $_REQUEST['action'] != 'Logout'))) {
$this->validateIP();
}
return $authenticated;
}
/**
* Called after a session is authenticated - if this returns false the sessionAuthenticate will return false and destroy the session
* and it will load the current user
* @return boolean
*/
public function postSessionAuthenticate()
{
global $action, $allowed_actions, $sugar_config, $app_strings;
$_SESSION['userTime']['last'] = time();
$user_unique_key = (isset($_SESSION['unique_key'])) ? $_SESSION['unique_key'] : '';
$server_unique_key = (isset($sugar_config['unique_key'])) ? $sugar_config['unique_key'] : '';
//CHECK IF USER IS CROSSING SITES
if (($user_unique_key != $server_unique_key) && (!in_array($action, $allowed_actions)) && (!isset($_SESSION['login_error']))) {
$GLOBALS['log']->debug('Destroying Session User has crossed Sites');
session_destroy();
header("Location: index.php?action=Login&module=Users" . $GLOBALS['app']->getLoginRedirect());
sugar_cleanup(true);
}
if (!$this->userAuthenticate->loadUserOnSession($_SESSION['authenticated_user_id'])) {
session_destroy();
header("Location: index.php?action=Login&module=Users&loginErrorMessage=LBL_SESSION_EXPIRED");
$GLOBALS['log']->debug('Current user session does not exist redirecting to login');
sugar_cleanup(true);
}
$GLOBALS['log']->debug('FACTOR AUTH: -------------------------------------------------------------');
$GLOBALS['log']->debug('FACTOR AUTH: --------------------- CHECK FACTOR AUtH ---------------------');
$GLOBALS['log']->debug('FACTOR AUTH: -------------------------------------------------------------');
//session_destroy(); die();
if (!$this->userAuthenticate->isUserLogoutRequest()) {
$GLOBALS['log']->debug('FACTOR AUTH: User needs factor auth, request is not Logout');
if ($this->userAuthenticate->isUserNeedFactorAuthentication()) {
$GLOBALS['log']->debug('FACTOR AUTH: User needs factor auth, set on User Profile page');
if (!$this->userAuthenticate->isUserFactorAuthenticated()) {
$GLOBALS['log']->debug('FACTOR AUTH: User is not factor authenticated yet');
if ($this->userAuthenticate->isUserFactorTokenReceived()) {
$GLOBALS['log']->debug('FACTOR AUTH: User sent back a token in request');
if (!$this->userAuthenticate->factorAuthenticateCheck()) {
$GLOBALS['log']->debug('FACTOR AUTH: User factor auth failed so we show token input form');
$msg = $app_strings['ERR_TWO_FACTOR_FAILED'];
self::addFactorMessage($msg);
$this->userAuthenticate->showFactorTokenInput();
} else {
$GLOBALS['log']->debug('FACTOR AUTH: User factor auth success!');
}
} else {
$GLOBALS['log']->debug('FACTOR AUTH: User did not sent back the token so we send a new one and redirect to token input form');
if (
$this->userAuthenticate->isFactorTokenSent()
&& $this->userAuthenticate->isUserRequestedResendToken() === false
) {
$GLOBALS['log']->fatal('DEBUG: token is not sent yet, do we send a token to user');
$this->userAuthenticate->showFactorTokenInput();
} else {
$GLOBALS['log']->fatal('DEBUG: token already sent');
}
if ($this->userAuthenticate->sendFactorTokenToUser()) {
$GLOBALS['log']->debug('FACTOR AUTH: Factor Token sent to User');
$msg = $app_strings['ERR_TWO_FACTOR_CODE_SENT'];
self::addFactorMessage($msg);
$this->userAuthenticate->showFactorTokenInput();
} else {
$GLOBALS['log']->debug('FACTOR AUTH: failed to send factor token to user so just redirect to the logout url and kick off ');
$msg = $app_strings['ERR_TWO_FACTOR_CODE_FAILED'];
self::addFactorMessage($msg);
$this->userAuthenticate->redirectToLogout();
}
}
} else {
$GLOBALS['log']->debug('FACTOR AUTH: User factor authenticated already');
}
} else {
$GLOBALS['log']->debug('FACTOR AUTH: User does`nt need factor auth');
}
} else {
$GLOBALS['log']->debug('FACTOR AUTH: User Logout requested');
}
$GLOBALS['log']->debug('Current user is: ' . $GLOBALS['current_user']->user_name);
return true;
}
/**
* Store message in a session array
* @param $msg
*/
public static function addFactorMessage($msg)
{
if (!isset($_SESSION['factor_message'])) {
$_SESSION['factor_message'] = array();
}
if (!in_array($msg, $_SESSION['factor_message'])) {
$_SESSION['factor_message'][] = $msg;
}
}
/**
* Read back the session messages and clear it;
* @return bool|string
* @throws \RuntimeException
*/
public static function getFactorMessages($sep = '<br>')
{
$factorMessage = false;
if (isset($_SESSION['factor_message']) && $_SESSION['factor_message']) {
if (is_array($_SESSION['factor_message']) || is_object($_SESSION['factor_message'])) {
$factorMessage = implode($sep, $_SESSION['factor_message']);
} elseif (is_string($_SESSION['factor_message'])) {
$factorMessage = $_SESSION['factor_message'];
} else {
$msg = 'Incorrect login factor message type.';
$GLOBALS['log']->warn($msg);
throw new RuntimeException($msg);
}
unset($_SESSION['factor_message']);
}
return $factorMessage;
}
/**
* Make sure a user isn't stealing sessions so check the ip to ensure that the ip address hasn't dramatically changed
*
*/
public function validateIP()
{
global $sugar_config, $mod_strings;
// grab client ip address
$clientIP = query_client_ip();
$classCheck = 0;
// check to see if config entry is present, if not, verify client ip
if (!isset($sugar_config['verify_client_ip']) || $sugar_config['verify_client_ip'] == true) {
// check to see if we've got a current ip address in $_SESSION
// and check to see if the session has been hijacked by a foreign ip
if (isset($_SESSION["ipaddress"])) {
$session_parts = explode(".", $_SESSION["ipaddress"]);
$client_parts = explode(".", $clientIP);
if (count($session_parts) < 4) {
$classCheck = 0;
} else {
// match class C IP addresses
for ($i = 0; $i < 3; $i ++) {
if ($session_parts[$i] === $client_parts[$i]) {
$classCheck = 1;
continue;
} else {
$classCheck = 0;
break;
}
}
}
// we have a different IP address
if ($_SESSION["ipaddress"] != $clientIP && empty($classCheck)) {
$GLOBALS['log']->fatal("IP Address mismatch: SESSION IP: {$_SESSION['ipaddress']} CLIENT IP: {$clientIP}");
session_destroy();
die($mod_strings['ERR_IP_CHANGE'] . "<a href=\"{$sugar_config['site_url']}\">" . $mod_strings['ERR_RETURN'] . "</a>");
}
} else {
$_SESSION["ipaddress"] = $clientIP;
}
}
}
/**
* Called when a user requests to logout
*
*/
public function logout()
{
session_start();
session_destroy();
ob_clean();
header('Location: index.php?module=Users&action=Login');
sugar_cleanup(true);
}
/**
* Encodes a users password. This is a static function and can be called at any time.
*
* @param STRING $password
* @return STRING $encoded_password
*/
public static function encodePassword($password)
{
return strtolower(md5($password));
}
/**
* If a user may change there password through the Sugar UI
*
*/
public function canChangePassword()
{
return true;
}
/**
* If a user may change there user name through the Sugar UI
*
*/
public function canChangeUserName()
{
return true;
}
/**
* pre_login
*
* This function allows the SugarAuthenticate subclasses to perform some pre login initialization as needed
*/
public function pre_login()
{
if (isset($_SESSION['authenticated_user_id'])) {
ob_clean();
// fixing bug #46837: Previosly links/URLs to records in Sugar from MSO Excel/Word were referred to the home screen and not the record
// It used to appear when default browser was not MS IE
header("Location: ".$GLOBALS['app']->getLoginRedirect());
sugar_cleanup(true);
}
}
/**
* Redirect to login page
*
* @param SugarApplication $app
*/
public function redirectToLogin(SugarApplication $app)
{
$loginVars = $app->createLoginVars();
SugarApplication::redirect('index.php?action=Login&module=Users' . $loginVars);
}
}