38 global $db, $conf, $langs;
40 global $dolibarr_main_auth_ldap_host, $dolibarr_main_auth_ldap_port;
41 global $dolibarr_main_auth_ldap_version, $dolibarr_main_auth_ldap_servertype;
42 global $dolibarr_main_auth_ldap_login_attribute, $dolibarr_main_auth_ldap_dn;
43 global $dolibarr_main_auth_ldap_admin_login, $dolibarr_main_auth_ldap_admin_pass;
44 global $dolibarr_main_auth_ldap_filter;
45 global $dolibarr_main_auth_ldap_debug;
48 $entity = $entitytotest;
49 if (isModEnabled(
'multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
54 $resultFetchUser =
'';
56 if (!function_exists(
"ldap_connect")) {
57 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO failed to connect to LDAP. LDAP functions are disabled on this PHP", LOG_ERR);
61 $langs->loadLangs(array(
'main',
'other'));
63 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"ErrorLDAPFunctionsAreDisabledOnThisPHP").
' '.$langs->transnoentitiesnoconv(
"TryAnotherConnectionMode");
68 dol_syslog(
"functions_ldap::check_user_password_ldap usertotest=".$usertotest.
" passwordtotest=".preg_replace(
'/./',
'*', $passwordtotest).
" entitytotest=".$entitytotest);
71 $ldaphost = $dolibarr_main_auth_ldap_host;
72 $ldapport = $dolibarr_main_auth_ldap_port;
73 $ldapversion = $dolibarr_main_auth_ldap_version;
74 $ldapservertype = (empty($dolibarr_main_auth_ldap_servertype) ?
'openldap' : $dolibarr_main_auth_ldap_servertype);
76 $ldapuserattr = $dolibarr_main_auth_ldap_login_attribute;
77 $ldapdn = $dolibarr_main_auth_ldap_dn;
78 $ldapadminlogin = $dolibarr_main_auth_ldap_admin_login;
79 $ldapadminpass = $dolibarr_main_auth_ldap_admin_pass;
80 $ldapdebug = ((empty($dolibarr_main_auth_ldap_debug) || $dolibarr_main_auth_ldap_debug ==
"false") ?
false :
true);
83 print
"DEBUG: Logging LDAP steps<br>\n";
86 require_once DOL_DOCUMENT_ROOT.
'/core/class/ldap.class.php';
88 $ldap->server = explode(
',', $ldaphost);
89 $ldap->serverPort = $ldapport;
90 $ldap->ldapProtocolVersion = $ldapversion;
91 $ldap->serverType = $ldapservertype;
92 $ldap->searchUser = $ldapadminlogin;
93 $ldap->searchPassword = $ldapadminpass;
96 dol_syslog(
"functions_ldap::check_user_password_ldap Server:".join(
',', $ldap->server).
", Port:".$ldap->serverPort.
", Protocol:".$ldap->ldapProtocolVersion.
", Type:".$ldap->serverType);
97 dol_syslog(
"functions_ldap::check_user_password_ldap uid/samaccountname=".$ldapuserattr.
", dn=".$ldapdn.
", Admin:".$ldap->searchUser.
", Pass:".
dol_trunc($ldap->searchPassword, 3));
98 print
"DEBUG: Server:".join(
',', $ldap->server).
", Port:".$ldap->serverPort.
", Protocol:".$ldap->ldapProtocolVersion.
", Type:".$ldap->serverType.
"<br>\n";
99 print
"DEBUG: uid/samaccountname=".$ldapuserattr.
", dn=".$ldapdn.
", Admin:".$ldap->searchUser.
", Pass:".
dol_trunc($ldap->searchPassword, 3).
"<br>\n";
102 $resultFetchLdapUser = 0;
105 $userSearchFilter =
"";
106 if (empty($dolibarr_main_auth_ldap_filter)) {
107 $userSearchFilter =
"(".$ldapuserattr.
"=".$usertotest.
")";
109 $userSearchFilter = str_replace(
'%1%', $usertotest, $dolibarr_main_auth_ldap_filter);
114 if ($ldapadminlogin || $dolibarr_main_auth_ldap_filter) {
115 $result = $ldap->connect_bind();
117 $resultFetchLdapUser = $ldap->fetch($usertotest, $userSearchFilter);
119 if ($resultFetchLdapUser > 0 && $ldap->pwdlastset == 0) {
120 dol_syslog(
'functions_ldap::check_user_password_ldap '.$usertotest.
' must change password next logon');
122 print
"DEBUG: User ".$usertotest.
" must change password<br>\n";
126 $langs->load(
'ldap');
127 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"YouMustChangePassNextLogon", $usertotest, $ldap->domainFQDN);
132 print
"DEBUG: ".$ldap->error.
"<br>\n";
141 $ldap->searchUser = $ldapuserattr.
"=".$usertotest.
",".$ldapdn;
144 if ($resultFetchLdapUser && !empty($ldap->ldapUserDN)) {
145 $ldap->searchUser = $ldap->ldapUserDN;
147 $ldap->searchPassword = $passwordtotest;
151 $result = $ldap->connect_bind();
154 $login = $usertotest;
155 dol_syslog(
"functions_ldap::check_user_password_ldap $login authentication ok");
158 if (!empty($conf->global->LDAP_FIELD_LOGIN) && !empty($ldap->login)) {
159 $login = $ldap->login;
160 dol_syslog(
"functions_ldap::check_user_password_ldap login is now $login (LDAP_FIELD_LOGIN=".
getDolGlobalString(
'LDAP_FIELD_LOGIN').
")");
163 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
188 dol_syslog(
"functions_ldap::check_user_password_ldap Sync ldap2dolibarr");
192 print
"DEBUG: login ldap = ".$login.
"<br>\n";
194 $resultFetchLdapUser = $ldap->fetch($login, $userSearchFilter);
197 print
"DEBUG: UACF = ".join(
',', $ldap->uacf).
"<br>\n";
200 print
"DEBUG: pwdLastSet = ".dol_print_date($ldap->pwdlastset,
'day').
"<br>\n";
203 print
"DEBUG: badPasswordTime = ".dol_print_date($ldap->badpwdtime,
'day').
"<br>\n";
208 if ($conf->global->LDAP_SERVER_TYPE ==
"activedirectory") {
209 $sid = $ldap->getObjectSid($login);
211 print
"DEBUG: sid = ".$sid.
"<br>\n";
215 $usertmp =
new User($db);
216 $resultFetchUser = $usertmp->fetch(
'', $login, $sid, 1, ($entitytotest > 0 ? $entitytotest : -1));
217 if ($resultFetchUser > 0) {
218 dol_syslog(
"functions_ldap::check_user_password_ldap Sync user found user id=".$usertmp->id);
221 if ($usertmp->login != $ldap->login && $ldap->login) {
222 $usertmp->login = $ldap->login;
223 $usertmp->update($usertmp);
233 if (isModEnabled(
'multicompany')) {
236 $usertmp =
new User($db);
237 $usertmp->fetch(
'', $login);
238 if (is_object($mc)) {
239 $ret = $mc->checkRight($usertmp->id, $entitytotest);
241 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO entity '".$entitytotest.
"' not allowed for user id '".$usertmp->id.
"'", LOG_NOTICE);
249 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO bad user/password for '".$usertotest.
"'", LOG_NOTICE);
253 $langs->loadLangs(array(
'main',
'other'));
255 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"ErrorBadLoginPassword");
266 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO failed to connect to LDAP for '".$usertotest.
"'", LOG_NOTICE);
267 if (is_resource($ldap->connection) || is_object($ldap->connection)) {
268 $ldap->ldapErrorCode = ldap_errno($ldap->connection);
269 $ldap->ldapErrorText = ldap_error($ldap->connection);
270 dol_syslog(
"functions_ldap::check_user_password_ldap ".$ldap->ldapErrorCode.
" ".$ldap->ldapErrorText);
275 $langs->loadLangs(array(
'main',
'other',
'errors'));
276 $_SESSION[
"dol_loginmesg"] = ($ldap->error ? $ldap->error : $langs->transnoentitiesnoconv(
"ErrorBadLoginPassword"));