functions_ldapmc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014-2020 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /**
  20. * \file /multicompany/core/login/functions_ldapmc.php
  21. * \ingroup core
  22. * \brief Authentication functions for LDAP with Multicompany
  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_ldapmc($usertotest, $passwordtotest, $entitytotest)
  34. {
  35. global $db, $conf, $langs;
  36. global $_POST;
  37. global $dolibarr_main_auth_ldap_host, $dolibarr_main_auth_ldap_port;
  38. global $dolibarr_main_auth_ldap_version, $dolibarr_main_auth_ldap_servertype;
  39. global $dolibarr_main_auth_ldap_login_attribute, $dolibarr_main_auth_ldap_dn;
  40. global $dolibarr_main_auth_ldap_admin_login, $dolibarr_main_auth_ldap_admin_pass;
  41. global $dolibarr_main_auth_ldap_filter;
  42. global $dolibarr_main_auth_ldap_debug;
  43. // Force master entity in transversal mode
  44. $entity = $entitytotest;
  45. if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) $entity = 1;
  46. $login = '';
  47. $resultFetchUser = '';
  48. if (!function_exists("ldap_connect"))
  49. {
  50. dol_syslog("functions_ldap::check_user_password_ldap Authentication KO failed to connect to LDAP. LDAP functions are disabled on this PHP", LOG_ERR);
  51. sleep(1);
  52. // Load translation files required by the page
  53. $langs->loadLangs(array('main', 'other'));
  54. $_SESSION["dol_loginmesg"] = $langs->trans("ErrorLDAPFunctionsAreDisabledOnThisPHP").' '.$langs->trans("TryAnotherConnectionMode");
  55. return;
  56. }
  57. if ($usertotest)
  58. {
  59. dol_syslog("functions_ldap::check_user_password_ldap usertotest=".$usertotest." passwordtotest=".preg_replace('/./', '*', $passwordtotest)." entitytotest=".$entitytotest);
  60. // If test username/password asked, we define $test=false and $login var if ok, set $_SESSION["dol_loginmesg"] if ko
  61. $ldaphost = $dolibarr_main_auth_ldap_host;
  62. $ldapport = $dolibarr_main_auth_ldap_port;
  63. $ldapversion = $dolibarr_main_auth_ldap_version;
  64. $ldapservertype = (empty($dolibarr_main_auth_ldap_servertype) ? 'openldap' : $dolibarr_main_auth_ldap_servertype);
  65. $ldapuserattr = $dolibarr_main_auth_ldap_login_attribute;
  66. $ldapdn = $dolibarr_main_auth_ldap_dn;
  67. $ldapadminlogin = $dolibarr_main_auth_ldap_admin_login;
  68. $ldapadminpass = $dolibarr_main_auth_ldap_admin_pass;
  69. $ldapdebug = (empty($dolibarr_main_auth_ldap_debug) || $dolibarr_main_auth_ldap_debug == "false" ? false : true);
  70. if ($ldapdebug) print "DEBUG: Logging LDAP steps<br>\n";
  71. require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  72. $ldap = new Ldap();
  73. $ldap->server = explode(',', $ldaphost);
  74. $ldap->serverPort = $ldapport;
  75. $ldap->ldapProtocolVersion = $ldapversion;
  76. $ldap->serverType = $ldapservertype;
  77. $ldap->searchUser = $ldapadminlogin;
  78. $ldap->searchPassword = $ldapadminpass;
  79. if ($ldapdebug)
  80. {
  81. dol_syslog("functions_ldap::check_user_password_ldap Server:".join(',', $ldap->server).", Port:".$ldap->serverPort.", Protocol:".$ldap->ldapProtocolVersion.", Type:".$ldap->serverType);
  82. dol_syslog("functions_ldap::check_user_password_ldap uid/samacountname=".$ldapuserattr.", dn=".$ldapdn.", Admin:".$ldap->searchUser.", Pass:".$ldap->searchPassword);
  83. print "DEBUG: Server:".join(',', $ldap->server).", Port:".$ldap->serverPort.", Protocol:".$ldap->ldapProtocolVersion.", Type:".$ldap->serverType."<br>\n";
  84. print "DEBUG: uid/samacountname=".$ldapuserattr.", dn=".$ldapdn.", Admin:".$ldap->searchUser.", Pass:".$ldap->searchPassword."<br>\n";
  85. }
  86. $resultFetchLdapUser = 0;
  87. // Define $userSearchFilter
  88. $userSearchFilter = "";
  89. if (empty($dolibarr_main_auth_ldap_filter)) {
  90. $userSearchFilter = "(".$ldapuserattr."=".$usertotest.")";
  91. } else {
  92. $userSearchFilter = str_replace('%1%', $usertotest, $dolibarr_main_auth_ldap_filter);
  93. }
  94. // If admin login provided
  95. // Code to get user in LDAP from an admin connection (may differ from user connection, done later)
  96. if ($ldapadminlogin)
  97. {
  98. $result = $ldap->connect_bind();
  99. if ($result > 0)
  100. {
  101. $resultFetchLdapUser = $ldap->fetch($usertotest, $userSearchFilter);
  102. //dol_syslog('functions_ldap::check_user_password_ldap resultFetchLdapUser='.$resultFetchLdapUser);
  103. if ($resultFetchLdapUser > 0 && $ldap->pwdlastset == 0) // If ok but password need to be reset
  104. {
  105. dol_syslog('functions_ldap::check_user_password_ldap '.$usertotest.' must change password next logon');
  106. if ($ldapdebug) print "DEBUG: User ".$usertotest." must change password<br>\n";
  107. $ldap->close();
  108. sleep(1);
  109. $langs->load('ldap');
  110. $_SESSION["dol_loginmesg"] = $langs->trans("YouMustChangePassNextLogon", $usertotest, $ldap->domainFQDN);
  111. return '';
  112. }
  113. } else {
  114. if ($ldapdebug) print "DEBUG: ".$ldap->error."<br>\n";
  115. }
  116. $ldap->close();
  117. }
  118. // Forge LDAP user and password to test with them
  119. // If LDAP need a dn with login like "uid=jbloggs,ou=People,dc=foo,dc=com", default dn may work even if previous code with
  120. // admin login no exectued.
  121. $ldap->searchUser = $ldapuserattr."=".$usertotest.",".$ldapdn; // Default dn (will work if LDAP accept a dn with login value inside)
  122. // But if LDAP need a dn with name like "cn=Jhon Bloggs,ou=People,dc=foo,dc=com", previous part must have been executed to have
  123. // dn detected into ldapUserDN.
  124. if ($resultFetchLdapUser && !empty($ldap->ldapUserDN)) $ldap->searchUser = $ldap->ldapUserDN;
  125. $ldap->searchPassword = $passwordtotest;
  126. // Test with this->seachUser and this->searchPassword
  127. //print $resultFetchLdapUser."-".$ldap->ldapUserDN."-".$ldap->searchUser.'-'.$ldap->searchPassword;exit;
  128. $result = $ldap->connect_bind();
  129. if ($result > 0)
  130. {
  131. if ($result == 2) // Connection is ok for user/pass into LDAP
  132. {
  133. dol_syslog("functions_ldap::check_user_password_ldap Authentification ok");
  134. $login = $usertotest;
  135. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  136. $tmpuser = new User($db);
  137. $tmpuser->fetch('', $login, '', 1, ($entitytotest > 0 ? $entitytotest : -1));
  138. $now = dol_now();
  139. if ($tmpuser->datestartvalidity && $db->jdate($tmpuser->datestartvalidity) >= $now) {
  140. $ldap->close();
  141. // Load translation files required by the page
  142. $langs->loadLangs(array('main', 'errors'));
  143. $_SESSION["dol_loginmesg"] = $langs->trans("ErrorLoginDateValidity");
  144. return '--bad-login-validity--';
  145. }
  146. if ($tmpuser->dateendvalidity && $db->jdate($tmpuser->dateendvalidity) <= dol_get_first_hour($now)) {
  147. $ldap->close();
  148. // Load translation files required by the page
  149. $langs->loadLangs(array('main', 'errors'));
  150. $_SESSION["dol_loginmesg"] = $langs->trans("ErrorLoginDateValidity");
  151. return '--bad-login-validity--';
  152. }
  153. // ldap2dolibarr synchronisation
  154. if ($login && !empty($conf->ldap->enabled) && $conf->global->LDAP_SYNCHRO_ACTIVE == 'ldap2dolibarr') // ldap2dolibarr synchronisation
  155. {
  156. dol_syslog("functions_ldap::check_user_password_ldap Sync ldap2dolibarr");
  157. // On charge les attributs du user ldap
  158. if ($ldapdebug) print "DEBUG: login ldap = ".$login."<br>\n";
  159. $resultFetchLdapUser = $ldap->fetch($login, $userSearchFilter);
  160. if ($ldapdebug) print "DEBUG: UACF = ".join(',', $ldap->uacf)."<br>\n";
  161. if ($ldapdebug) print "DEBUG: pwdLastSet = ".dol_print_date($ldap->pwdlastset, 'day')."<br>\n";
  162. if ($ldapdebug) print "DEBUG: badPasswordTime = ".dol_print_date($ldap->badpwdtime, 'day')."<br>\n";
  163. // On recherche le user dolibarr en fonction de son SID ldap (only for Active Directory)
  164. $sid = null;
  165. if ($conf->global->LDAP_SERVER_TYPE == "activedirectory")
  166. {
  167. $sid = $ldap->getObjectSid($login);
  168. if ($ldapdebug) print "DEBUG: sid = ".$sid."<br>\n";
  169. }
  170. $usertmp = new User($db);
  171. $resultFetchUser = $usertmp->fetch('', $login, $sid);
  172. if ($resultFetchUser > 0)
  173. {
  174. dol_syslog("functions_ldap::check_user_password_ldap Sync user found user id=".$usertmp->id);
  175. // On verifie si le login a change et on met a jour les attributs dolibarr
  176. if ($usertmp->login != $ldap->login && $ldap->login)
  177. {
  178. $usertmp->login = $ldap->login;
  179. $usertmp->update($usertmp);
  180. // TODO Que faire si update echoue car on update avec un login deja existant.
  181. }
  182. //$resultUpdate = $usertmp->update_ldap2dolibarr($ldap);
  183. }
  184. unset($usertmp);
  185. }
  186. if (!empty($conf->multicompany->enabled)) // We must check entity (even if sync is not active)
  187. {
  188. global $mc;
  189. $usertmp = new User($db);
  190. $usertmp->fetch('', $login);
  191. $ret = $mc->checkRight($usertmp->id, $entitytotest);
  192. if ($ret < 0)
  193. {
  194. dol_syslog("functions_ldap::check_user_password_ldap Authentication KO entity '".$entitytotest."' not allowed for user '".$usertmp->id."'", LOG_NOTICE);
  195. $login = ''; // force authentication failure
  196. }
  197. unset($usertmp);
  198. }
  199. }
  200. if ($result == 1)
  201. {
  202. dol_syslog("functions_ldap::check_user_password_ldap Authentication KO bad user/password for '".$usertotest."'", LOG_NOTICE);
  203. sleep(1);
  204. // Load translation files required by the page
  205. $langs->loadLangs(array('main', 'other'));
  206. $_SESSION["dol_loginmesg"] = $langs->trans("ErrorBadLoginPassword");
  207. }
  208. } else {
  209. /* Login failed. Return false, together with the error code and text from
  210. ** the LDAP server. The common error codes and reasons are listed below :
  211. ** (for iPlanet, other servers may differ)
  212. ** 19 - Account locked out (too many invalid login attempts)
  213. ** 32 - User does not exist
  214. ** 49 - Wrong password
  215. ** 53 - Account inactive (manually locked out by administrator)
  216. */
  217. dol_syslog("functions_ldap::check_user_password_ldap Authentication KO failed to connect to LDAP for '".$usertotest."'", LOG_NOTICE);
  218. if (is_resource($ldap->connection)) // If connection ok but bind ko
  219. {
  220. $ldap->ldapErrorCode = ldap_errno($ldap->connection);
  221. $ldap->ldapErrorText = ldap_error($ldap->connection);
  222. dol_syslog("functions_ldap::check_user_password_ldap ".$ldap->ldapErrorCode." ".$ldap->ldapErrorText);
  223. }
  224. sleep(2); // Anti brut force protection
  225. // Load translation files required by the page
  226. $langs->loadLangs(array('main', 'other', 'errors'));
  227. $_SESSION["dol_loginmesg"] = ($ldap->error ? $ldap->error : $langs->trans("ErrorBadLoginPassword"));
  228. }
  229. $ldap->close();
  230. }
  231. return $login;
  232. }