39 global $db, $conf, $langs;
41 global $dolibarr_main_auth_ldap_host, $dolibarr_main_auth_ldap_port;
42 global $dolibarr_main_auth_ldap_version, $dolibarr_main_auth_ldap_servertype;
43 global $dolibarr_main_auth_ldap_login_attribute, $dolibarr_main_auth_ldap_dn;
44 global $dolibarr_main_auth_ldap_admin_login, $dolibarr_main_auth_ldap_admin_pass;
45 global $dolibarr_main_auth_ldap_filter;
46 global $dolibarr_main_auth_ldap_debug;
49 $entity = $entitytotest;
50 if (isModEnabled(
'multicompany') &&
getDolGlobalString(
'MULTICOMPANY_TRANSVERSE_MODE')) {
55 $resultFetchUser =
'';
57 if (!function_exists(
"ldap_connect")) {
58 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO failed to connect to LDAP. LDAP functions are disabled on this PHP", LOG_ERR);
62 $langs->loadLangs(array(
'main',
'other'));
64 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"ErrorLDAPFunctionsAreDisabledOnThisPHP").
' '.$langs->transnoentitiesnoconv(
"TryAnotherConnectionMode");
69 dol_syslog(
"functions_ldap::check_user_password_ldap usertotest=".$usertotest.
" passwordtotest=".preg_replace(
'/./',
'*', $passwordtotest).
" entitytotest=".$entitytotest);
72 $ldaphost = $dolibarr_main_auth_ldap_host;
73 $ldapport = $dolibarr_main_auth_ldap_port;
74 $ldapversion = $dolibarr_main_auth_ldap_version;
75 $ldapservertype = (empty($dolibarr_main_auth_ldap_servertype) ?
'openldap' : $dolibarr_main_auth_ldap_servertype);
77 $ldapuserattr = $dolibarr_main_auth_ldap_login_attribute;
78 $ldapdn = $dolibarr_main_auth_ldap_dn;
79 $ldapadminlogin = $dolibarr_main_auth_ldap_admin_login;
80 $ldapadminpass = $dolibarr_main_auth_ldap_admin_pass;
81 $ldapdebug = ((empty($dolibarr_main_auth_ldap_debug) || $dolibarr_main_auth_ldap_debug ==
"false") ?
false :
true);
84 print
"DEBUG: Logging LDAP steps<br>\n";
87 require_once DOL_DOCUMENT_ROOT.
'/core/class/ldap.class.php';
89 $ldap->server = explode(
',', $ldaphost);
90 $ldap->serverPort = $ldapport;
91 $ldap->ldapProtocolVersion = $ldapversion;
92 $ldap->serverType = $ldapservertype;
93 $ldap->searchUser = $ldapadminlogin;
94 $ldap->searchPassword = $ldapadminpass;
97 dol_syslog(
"functions_ldap::check_user_password_ldap Server:".join(
',', $ldap->server).
", Port:".$ldap->serverPort.
", Protocol:".$ldap->ldapProtocolVersion.
", Type:".$ldap->serverType);
98 dol_syslog(
"functions_ldap::check_user_password_ldap uid/samaccountname=".$ldapuserattr.
", dn=".$ldapdn.
", Admin:".$ldap->searchUser.
", Pass:".
dol_trunc($ldap->searchPassword, 3));
99 print
"DEBUG: Server:".join(
',', $ldap->server).
", Port:".$ldap->serverPort.
", Protocol:".$ldap->ldapProtocolVersion.
", Type:".$ldap->serverType.
"<br>\n";
100 print
"DEBUG: uid/samaccountname=".$ldapuserattr.
", dn=".$ldapdn.
", Admin:".$ldap->searchUser.
", Pass:".
dol_trunc($ldap->searchPassword, 3).
"<br>\n";
103 $resultFetchLdapUser = 0;
106 $userSearchFilter =
"";
107 if (empty($dolibarr_main_auth_ldap_filter)) {
108 $userSearchFilter =
"(".$ldapuserattr.
"=".$usertotest.
")";
110 $userSearchFilter = str_replace(
'%1%', $usertotest, $dolibarr_main_auth_ldap_filter);
115 if ($ldapadminlogin || $dolibarr_main_auth_ldap_filter) {
116 $result = $ldap->connect_bind();
118 $resultFetchLdapUser = $ldap->fetch($usertotest, $userSearchFilter);
120 if ($resultFetchLdapUser > 0 && $ldap->pwdlastset == 0) {
121 dol_syslog(
'functions_ldap::check_user_password_ldap '.$usertotest.
' must change password next logon');
123 print
"DEBUG: User ".$usertotest.
" must change password<br>\n";
127 $langs->load(
'ldap');
128 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"YouMustChangePassNextLogon", $usertotest, $ldap->domainFQDN);
133 print
"DEBUG: ".$ldap->error.
"<br>\n";
142 $ldap->searchUser = $ldapuserattr.
"=".$usertotest.
",".$ldapdn;
145 if ($resultFetchLdapUser && !empty($ldap->ldapUserDN)) {
146 $ldap->searchUser = $ldap->ldapUserDN;
148 $ldap->searchPassword = $passwordtotest;
152 $result = $ldap->connect_bind();
155 $login = $usertotest;
156 dol_syslog(
"functions_ldap::check_user_password_ldap $login authentication ok");
160 $login = $ldap->login;
161 dol_syslog(
"functions_ldap::check_user_password_ldap login is now $login (LDAP_FIELD_LOGIN=".
getDolGlobalString(
'LDAP_FIELD_LOGIN').
")");
164 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
170 dol_syslog(
"functions_ldap::check_user_password_ldap Sync ldap2dolibarr");
174 print
"DEBUG: login ldap = ".$login.
"<br>\n";
176 $resultFetchLdapUser = $ldap->fetch($login, $userSearchFilter);
179 print
"DEBUG: UACF = ".join(
',', $ldap->uacf).
"<br>\n";
182 print
"DEBUG: pwdLastSet = ".dol_print_date($ldap->pwdlastset,
'day').
"<br>\n";
185 print
"DEBUG: badPasswordTime = ".dol_print_date($ldap->badpwdtime,
'day').
"<br>\n";
191 $sid = $ldap->getObjectSid($login);
193 print
"DEBUG: sid = ".$sid.
"<br>\n";
197 $usertmp =
new User($db);
198 $resultFetchUser = $usertmp->fetch(
'', $login, $sid, 1, ($entitytotest > 0 ? $entitytotest : -1));
199 if ($resultFetchUser > 0) {
200 dol_syslog(
"functions_ldap::check_user_password_ldap Sync user found user id=".$usertmp->id);
203 if ($usertmp->login != $ldap->login && $ldap->login) {
204 $usertmp->login = $ldap->login;
205 $usertmp->update($usertmp);
215 if (isModEnabled(
'multicompany')) {
218 $usertmp =
new User($db);
219 $usertmp->fetch(
'', $login);
220 if (is_object($mc)) {
221 $ret = $mc->checkRight($usertmp->id, $entitytotest);
223 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO entity '".$entitytotest.
"' not allowed for user id '".$usertmp->id.
"'", LOG_NOTICE);
231 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO bad user/password for '".$usertotest.
"'", LOG_NOTICE);
235 $langs->loadLangs(array(
'main',
'other'));
237 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"ErrorBadLoginPassword");
248 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO failed to connect to LDAP for '".$usertotest.
"'", LOG_NOTICE);
249 if (is_resource($ldap->connection) || is_object($ldap->connection)) {
251 $ldap->ldapErrorCode = ldap_errno($ldap->connection);
252 $ldap->ldapErrorText = ldap_error($ldap->connection);
253 dol_syslog(
"functions_ldap::check_user_password_ldap ".$ldap->ldapErrorCode.
" ".$ldap->ldapErrorText);
254 }
catch (Throwable $exception) {
255 $ldap->ldapErrorCode =
'';
256 $ldap->ldapErrorText =
'';
257 dol_syslog(
'functions_ldap::check_user_password_ldap '.$exception, LOG_WARNING);
262 $langs->loadLangs(array(
'main',
'other',
'errors'));
263 $_SESSION[
"dol_loginmesg"] = ($ldap->error ? $ldap->error : $langs->transnoentitiesnoconv(
"ErrorBadLoginPassword"));