functions_dolibarr.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /* Copyright (C) 2007-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007-2015 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/login/functions_dolibarr.php
  21. * \ingroup core
  22. * \brief Authentication functions for Dolibarr mode (check user on login or email and check pass)
  23. */
  24. /**
  25. * Check validity of user/password/entity
  26. * If test is ko, reason must be filled into $_SESSION["dol_loginmesg"]
  27. *
  28. * @param string $usertotest Login
  29. * @param string $passwordtotest Password
  30. * @param int $entitytotest Number of instance (always 1 if module multicompany not enabled)
  31. * @return string Login if OK, '' if KO
  32. */
  33. function check_user_password_dolibarr($usertotest, $passwordtotest, $entitytotest = 1)
  34. {
  35. global $db, $conf, $langs;
  36. // Force master entity in transversal mode
  37. $entity = $entitytotest;
  38. if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  39. $entity = 1;
  40. }
  41. $login = '';
  42. if (!empty($usertotest)) {
  43. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  44. dol_syslog("functions_dolibarr::check_user_password_dolibarr usertotest=".$usertotest." passwordtotest=".preg_replace('/./', '*', $passwordtotest)." entitytotest=".$entitytotest);
  45. // If test username/password asked, we define $test=false if ko and $login var to login if ok, set also $_SESSION["dol_loginmesg"] if ko
  46. $table = MAIN_DB_PREFIX."user";
  47. $usernamecol1 = 'login';
  48. $usernamecol2 = 'email';
  49. $entitycol = 'entity';
  50. $sql = "SELECT rowid, login, entity, pass, pass_crypted, datestartvalidity, dateendvalidity";
  51. $sql .= " FROM ".$table;
  52. $sql .= " WHERE (".$usernamecol1." = '".$db->escape($usertotest)."'";
  53. if (preg_match('/@/', $usertotest)) {
  54. $sql .= " OR ".$usernamecol2." = '".$db->escape($usertotest)."'";
  55. }
  56. $sql .= ") AND ".$entitycol." IN (0,".($entity ? ((int) $entity) : 1).")";
  57. $sql .= " AND statut = 1";
  58. // Note: Test on validity is done later
  59. // Order is required to firstly found the user into entity, then the superadmin.
  60. // For the case (TODO: we must avoid that) a user has renamed its login with same value than a user in entity 0.
  61. $sql .= " ORDER BY entity DESC";
  62. $resql = $db->query($sql);
  63. if ($resql) {
  64. $obj = $db->fetch_object($resql);
  65. if ($obj) {
  66. $now = dol_now();
  67. if ($obj->datestartvalidity && $db->jdate($obj->datestartvalidity) > $now) {
  68. // Load translation files required by the page
  69. $langs->loadLangs(array('main', 'errors'));
  70. $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorLoginDateValidity");
  71. return '--bad-login-validity--';
  72. }
  73. if ($obj->dateendvalidity && $db->jdate($obj->dateendvalidity) < dol_get_first_hour($now)) {
  74. // Load translation files required by the page
  75. $langs->loadLangs(array('main', 'errors'));
  76. $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorLoginDateValidity");
  77. return '--bad-login-validity--';
  78. }
  79. $passclear = $obj->pass;
  80. $passcrypted = $obj->pass_crypted;
  81. $passtyped = $passwordtotest;
  82. $passok = false;
  83. // Check crypted password
  84. $cryptType = '';
  85. if (!empty($conf->global->DATABASE_PWD_ENCRYPTED)) {
  86. $cryptType = $conf->global->DATABASE_PWD_ENCRYPTED;
  87. }
  88. // By default, we use default setup for encryption rule
  89. if (!in_array($cryptType, array('auto'))) {
  90. $cryptType = 'auto';
  91. }
  92. // Check crypted password according to crypt algorithm
  93. if ($cryptType == 'auto') {
  94. if (dol_verifyHash($passtyped, $passcrypted, '0')) {
  95. $passok = true;
  96. dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentification ok - hash ".$cryptType." of pass is ok");
  97. }
  98. }
  99. // For compatibility with very old versions
  100. if (!$passok) {
  101. if ((!$passcrypted || $passtyped)
  102. && ($passclear && ($passtyped == $passclear))) {
  103. $passok = true;
  104. dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentification ok - found pass in database");
  105. }
  106. }
  107. // Password ok ?
  108. if ($passok) {
  109. $login = $obj->login;
  110. } else {
  111. sleep(2); // Anti brut force protection
  112. dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO bad password for '".$usertotest."', cryptType=".$cryptType, LOG_NOTICE);
  113. // Load translation files required by the page
  114. $langs->loadLangs(array('main', 'errors'));
  115. $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorBadLoginPassword");
  116. }
  117. // We must check entity
  118. if ($passok && !empty($conf->multicompany->enabled)) { // We must check entity
  119. global $mc;
  120. if (!isset($mc)) {
  121. $conf->multicompany->enabled = false; // Global not available, disable $conf->multicompany->enabled for safety
  122. } else {
  123. $ret = $mc->checkRight($obj->rowid, $entitytotest);
  124. if ($ret < 0) {
  125. dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO entity '".$entitytotest."' not allowed for user '".$obj->rowid."'", LOG_NOTICE);
  126. $login = ''; // force authentication failure
  127. }
  128. }
  129. }
  130. } else {
  131. dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO user not found for '".$usertotest."'", LOG_NOTICE);
  132. sleep(1);
  133. // Load translation files required by the page
  134. $langs->loadLangs(array('main', 'errors'));
  135. $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorBadLoginPassword");
  136. }
  137. } else {
  138. dol_syslog("functions_dolibarr::check_user_password_dolibarr Authentication KO db error for '".$usertotest."' error=".$db->lasterror(), LOG_ERR);
  139. sleep(1);
  140. $_SESSION["dol_loginmesg"] = $db->lasterror();
  141. }
  142. }
  143. return $login;
  144. }