PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/Users/authentication/SugarAuthenticate/SugarAuthenticate.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 366 lines | 200 code | 53 blank | 113 comment | 63 complexity | 6bfbcf6f12037e86cf8d1272a7fc36a6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM Community Edition is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Affero General Public License version 3 as published by the
  9. * Free Software Foundation with the addition of the following permission added
  10. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  11. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  12. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License along with
  20. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  21. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301 USA.
  23. *
  24. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  25. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by SugarCRM".
  36. ********************************************************************************/
  37. /**
  38. * This file is used to control the authentication process.
  39. * It will call on the user authenticate and controll redirection
  40. * based on the users validation
  41. *
  42. */
  43. class SugarAuthenticate{
  44. var $userAuthenticateClass = 'SugarAuthenticateUser';
  45. var $authenticationDir = 'SugarAuthenticate';
  46. /**
  47. * Constructs SugarAuthenticate
  48. * This will load the user authentication class
  49. *
  50. * @return SugarAuthenticate
  51. */
  52. function SugarAuthenticate()
  53. {
  54. // check in custom dir first, in case someone want's to override an auth controller
  55. if (file_exists('custom/modules/Users/authentication/'.$this->authenticationDir.'/' . $this->userAuthenticateClass . '.php')) {
  56. require_once('custom/modules/Users/authentication/'.$this->authenticationDir.'/' . $this->userAuthenticateClass . '.php');
  57. }
  58. elseif (file_exists('modules/Users/authentication/'.$this->authenticationDir.'/' . $this->userAuthenticateClass . '.php')) {
  59. require_once('modules/Users/authentication/'.$this->authenticationDir.'/' . $this->userAuthenticateClass . '.php');
  60. }
  61. $this->userAuthenticate = new $this->userAuthenticateClass();
  62. }
  63. /**
  64. * Authenticates a user based on the username and password
  65. * returns true if the user was authenticated false otherwise
  66. * it also will load the user into current user if he was authenticated
  67. *
  68. * @param string $username
  69. * @param string $password
  70. * @return boolean
  71. */
  72. function loginAuthenticate($username, $password, $fallback=false, $PARAMS = array ()){
  73. global $mod_strings;
  74. unset($_SESSION['login_error']);
  75. $usr= new user();
  76. $usr_id=$usr->retrieve_user_id($username);
  77. $usr->retrieve($usr_id);
  78. $_SESSION['login_error']='';
  79. $_SESSION['waiting_error']='';
  80. $_SESSION['hasExpiredPassword']='0';
  81. if ($this->userAuthenticate->loadUserOnLogin($username, $password, $fallback, $PARAMS)) {
  82. require_once('modules/Users/password_utils.php');
  83. if(hasPasswordExpired($username)) {
  84. $_SESSION['hasExpiredPassword'] = '1';
  85. }
  86. // now that user is authenticated, reset loginfailed
  87. if ($usr->getPreference('loginfailed') != '' && $usr->getPreference('loginfailed') != 0) {
  88. $usr->setPreference('loginfailed','0');
  89. $usr->savePreferencesToDB();
  90. }
  91. return $this->postLoginAuthenticate();
  92. }
  93. else
  94. {
  95. if(!empty($usr_id) && $res['lockoutexpiration'] > 0){
  96. if (($logout=$usr->getPreference('loginfailed'))=='')
  97. $usr->setPreference('loginfailed','1');
  98. else
  99. $usr->setPreference('loginfailed',$logout+1);
  100. $usr->savePreferencesToDB();
  101. }
  102. }
  103. if(strtolower(get_class($this)) != 'sugarauthenticate'){
  104. $sa = new SugarAuthenticate();
  105. $error = (!empty($_SESSION['login_error']))?$_SESSION['login_error']:'';
  106. if($sa->loginAuthenticate($username, $password, true, $PARAMS)){
  107. return true;
  108. }
  109. $_SESSION['login_error'] = $error;
  110. }
  111. $_SESSION['login_user_name'] = $username;
  112. $_SESSION['login_password'] = $password;
  113. if(empty($_SESSION['login_error'])){
  114. $_SESSION['login_error'] = translate('ERR_INVALID_PASSWORD', 'Users');
  115. }
  116. return false;
  117. }
  118. /**
  119. * 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
  120. *
  121. */
  122. function postLoginAuthenticate(){
  123. global $reset_theme_on_default_user, $reset_language_on_default_user, $sugar_config;
  124. //THIS SECTION IS TO ENSURE VERSIONS ARE UPTODATE
  125. require_once ('modules/Versions/CheckVersions.php');
  126. $invalid_versions = get_invalid_versions();
  127. if (!empty ($invalid_versions)) {
  128. if (isset ($invalid_versions['Rebuild Relationships'])) {
  129. unset ($invalid_versions['Rebuild Relationships']);
  130. // flag for pickup in DisplayWarnings.php
  131. $_SESSION['rebuild_relationships'] = true;
  132. }
  133. if (isset ($invalid_versions['Rebuild Extensions'])) {
  134. unset ($invalid_versions['Rebuild Extensions']);
  135. // flag for pickup in DisplayWarnings.php
  136. $_SESSION['rebuild_extensions'] = true;
  137. }
  138. $_SESSION['invalid_versions'] = $invalid_versions;
  139. }
  140. //just do a little house cleaning here
  141. unset($_SESSION['login_password']);
  142. unset($_SESSION['login_error']);
  143. unset($_SESSION['login_user_name']);
  144. unset($_SESSION['ACL']);
  145. //set the server unique key
  146. if (isset ($sugar_config['unique_key']))$_SESSION['unique_key'] = $sugar_config['unique_key'];
  147. //set user language
  148. if (isset ($reset_language_on_default_user) && $reset_language_on_default_user && $GLOBALS['current_user']->user_name == $sugar_config['default_user_name']) {
  149. $authenticated_user_language = $sugar_config['default_language'];
  150. } else {
  151. $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']);
  152. }
  153. $_SESSION['authenticated_user_language'] = $authenticated_user_language;
  154. $GLOBALS['log']->debug("authenticated_user_language is $authenticated_user_language");
  155. // Clear all uploaded import files for this user if it exists
  156. require_once('modules/Import/ImportCacheFiles.php');
  157. $tmp_file_name = ImportCacheFiles::getImportDir()."/IMPORT_" . $GLOBALS['current_user']->id;
  158. if (file_exists($tmp_file_name)) {
  159. unlink($tmp_file_name);
  160. }
  161. return true;
  162. }
  163. /**
  164. * On every page hit this will be called to ensure a user is authenticated
  165. * @return boolean
  166. */
  167. function sessionAuthenticate(){
  168. global $module, $action, $allowed_actions;
  169. $authenticated = false;
  170. $allowed_actions = array ("Authenticate", "Login"); // these are actions where the user/server keys aren't compared
  171. if (isset ($_SESSION['authenticated_user_id'])) {
  172. $GLOBALS['log']->debug("We have an authenticated user id: ".$_SESSION["authenticated_user_id"]);
  173. $authenticated = $this->postSessionAuthenticate();
  174. } else
  175. if (isset ($action) && isset ($module) && $action == "Authenticate" && $module == "Users") {
  176. $GLOBALS['log']->debug("We are authenticating user now");
  177. } else {
  178. $GLOBALS['log']->debug("The current user does not have a session. Going to the login page");
  179. $action = "Login";
  180. $module = "Users";
  181. $_REQUEST['action'] = $action;
  182. $_REQUEST['module'] = $module;
  183. }
  184. if (empty ($GLOBALS['current_user']->id) && !in_array($action, $allowed_actions)) {
  185. $GLOBALS['log']->debug("The current user is not logged in going to login page");
  186. $action = "Login";
  187. $module = "Users";
  188. $_REQUEST['action'] = $action;
  189. $_REQUEST['module'] = $module;
  190. }
  191. if($authenticated && ((empty($_REQUEST['module']) || empty($_REQUEST['action'])) || ($_REQUEST['module'] != 'Users' || $_REQUEST['action'] != 'Logout'))){
  192. $this->validateIP();
  193. }
  194. return $authenticated;
  195. }
  196. /**
  197. * Called after a session is authenticated - if this returns false the sessionAuthenticate will return false and destroy the session
  198. * and it will load the current user
  199. * @return boolean
  200. */
  201. function postSessionAuthenticate(){
  202. global $action, $allowed_actions, $sugar_config;
  203. $_SESSION['userTime']['last'] = time();
  204. $user_unique_key = (isset ($_SESSION['unique_key'])) ? $_SESSION['unique_key'] : '';
  205. $server_unique_key = (isset ($sugar_config['unique_key'])) ? $sugar_config['unique_key'] : '';
  206. //CHECK IF USER IS CROSSING SITES
  207. if (($user_unique_key != $server_unique_key) && (!in_array($action, $allowed_actions)) && (!isset ($_SESSION['login_error']))) {
  208. $GLOBALS['log']->debug('Destroying Session User has crossed Sites');
  209. session_destroy();
  210. header("Location: index.php?action=Login&module=Users".$GLOBALS['app']->getLoginRedirect());
  211. sugar_cleanup(true);
  212. }
  213. if (!$this->userAuthenticate->loadUserOnSession($_SESSION['authenticated_user_id'])) {
  214. session_destroy();
  215. header("Location: index.php?action=Login&module=Users&loginErrorMessage=LBL_SESSION_EXPIRED");
  216. $GLOBALS['log']->debug('Current user session does not exist redirecting to login');
  217. sugar_cleanup(true);
  218. }
  219. $GLOBALS['log']->debug('Current user is: '.$GLOBALS['current_user']->user_name);
  220. return true;
  221. }
  222. /**
  223. * Make sure a user isn't stealing sessions so check the ip to ensure that the ip address hasn't dramatically changed
  224. *
  225. */
  226. function validateIP() {
  227. global $sugar_config;
  228. // grab client ip address
  229. $clientIP = query_client_ip();
  230. $classCheck = 0;
  231. // check to see if config entry is present, if not, verify client ip
  232. if (!isset ($sugar_config['verify_client_ip']) || $sugar_config['verify_client_ip'] == true) {
  233. // check to see if we've got a current ip address in $_SESSION
  234. // and check to see if the session has been hijacked by a foreign ip
  235. if (isset ($_SESSION["ipaddress"])) {
  236. $session_parts = explode(".", $_SESSION["ipaddress"]);
  237. $client_parts = explode(".", $clientIP);
  238. if(count($session_parts) < 4) {
  239. $classCheck = 0;
  240. }
  241. else {
  242. // match class C IP addresses
  243. for ($i = 0; $i < 3; $i ++) {
  244. if ($session_parts[$i] == $client_parts[$i]) {
  245. $classCheck = 1;
  246. continue;
  247. } else {
  248. $classCheck = 0;
  249. break;
  250. }
  251. }
  252. }
  253. // we have a different IP address
  254. if ($_SESSION["ipaddress"] != $clientIP && empty ($classCheck)) {
  255. $GLOBALS['log']->fatal("IP Address mismatch: SESSION IP: {$_SESSION['ipaddress']} CLIENT IP: {$clientIP}");
  256. session_destroy();
  257. die("Your session was terminated due to a significant change in your IP address. <a href=\"{$sugar_config['site_url']}\">Return to Home</a>");
  258. }
  259. } else {
  260. $_SESSION["ipaddress"] = $clientIP;
  261. }
  262. }
  263. }
  264. /**
  265. * Called when a user requests to logout
  266. *
  267. */
  268. function logout(){
  269. session_destroy();
  270. ob_clean();
  271. header('Location: index.php?module=Users&action=Login');
  272. sugar_cleanup(true);
  273. }
  274. /**
  275. * Encodes a users password. This is a static function and can be called at any time.
  276. *
  277. * @param STRING $password
  278. * @return STRING $encoded_password
  279. */
  280. function encodePassword($password){
  281. return strtolower(md5($password));
  282. }
  283. /**
  284. * If a user may change there password through the Sugar UI
  285. *
  286. */
  287. function canChangePassword(){
  288. return true;
  289. }
  290. /**
  291. * If a user may change there user name through the Sugar UI
  292. *
  293. */
  294. function canChangeUserName(){
  295. return true;
  296. }
  297. /**
  298. * pre_login
  299. *
  300. * This function allows the SugarAuthenticate subclasses to perform some pre login initialization as needed
  301. */
  302. function pre_login()
  303. {
  304. if (isset($_SESSION['authenticated_user_id']))
  305. {
  306. ob_clean();
  307. // fixing bug #46837: Previosly links/URLs to records in Sugar from MSO Excel/Word were referred to the home screen and not the record
  308. // It used to appear when default browser was not MS IE
  309. header("Location: ".$GLOBALS['app']->getLoginRedirect());
  310. sugar_cleanup(true);
  311. }
  312. }
  313. }