functions_dolibarr.php 6.5 KB

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