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';
165 $tmpuser =
new User($db);
166 $tmpuser->fetch(
'', $login,
'', 1, ($entitytotest > 0 ? $entitytotest : -1));
169 if ($tmpuser->datestartvalidity && $db->jdate($tmpuser->datestartvalidity) >= $now) {
172 $langs->loadLangs(array(
'main',
'errors'));
173 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"ErrorLoginDateValidity");
174 return '--bad-login-validity--';
176 if ($tmpuser->dateendvalidity && $db->jdate($tmpuser->dateendvalidity) <=
dol_get_first_hour($now)) {
179 $langs->loadLangs(array(
'main',
'errors'));
180 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"ErrorLoginDateValidity");
181 return '--bad-login-validity--';
186 dol_syslog(
"functions_ldap::check_user_password_ldap Sync ldap2dolibarr");
190 print
"DEBUG: login ldap = ".$login.
"<br>\n";
192 $resultFetchLdapUser = $ldap->fetch($login, $userSearchFilter);
195 print
"DEBUG: UACF = ".join(
',', $ldap->uacf).
"<br>\n";
198 print
"DEBUG: pwdLastSet = ".dol_print_date($ldap->pwdlastset,
'day').
"<br>\n";
201 print
"DEBUG: badPasswordTime = ".dol_print_date($ldap->badpwdtime,
'day').
"<br>\n";
206 if ($conf->global->LDAP_SERVER_TYPE ==
"activedirectory") {
207 $sid = $ldap->getObjectSid($login);
209 print
"DEBUG: sid = ".$sid.
"<br>\n";
213 $usertmp =
new User($db);
214 $resultFetchUser = $usertmp->fetch(
'', $login, $sid, 1, ($entitytotest > 0 ? $entitytotest : -1));
215 if ($resultFetchUser > 0) {
216 dol_syslog(
"functions_ldap::check_user_password_ldap Sync user found user id=".$usertmp->id);
219 if ($usertmp->login != $ldap->login && $ldap->login) {
220 $usertmp->login = $ldap->login;
221 $usertmp->update($usertmp);
234 $usertmp =
new User($db);
235 $usertmp->fetch(
'', $login);
236 if (is_object($mc)) {
237 $ret = $mc->checkRight($usertmp->id, $entitytotest);
239 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO entity '".$entitytotest.
"' not allowed for user id '".$usertmp->id.
"'", LOG_NOTICE);
247 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO bad user/password for '".$usertotest.
"'", LOG_NOTICE);
251 $langs->loadLangs(array(
'main',
'other'));
253 $_SESSION[
"dol_loginmesg"] = $langs->transnoentitiesnoconv(
"ErrorBadLoginPassword");
264 dol_syslog(
"functions_ldap::check_user_password_ldap Authentication KO failed to connect to LDAP for '".$usertotest.
"'", LOG_NOTICE);
265 if (is_resource($ldap->connection) || is_object($ldap->connection)) {
266 $ldap->ldapErrorCode = ldap_errno($ldap->connection);
267 $ldap->ldapErrorText = ldap_error($ldap->connection);
268 dol_syslog(
"functions_ldap::check_user_password_ldap ".$ldap->ldapErrorCode.
" ".$ldap->ldapErrorText);
273 $langs->loadLangs(array(
'main',
'other',
'errors'));
274 $_SESSION[
"dol_loginmesg"] = ($ldap->error ? $ldap->error : $langs->transnoentitiesnoconv(
"ErrorBadLoginPassword"));
Class to manage LDAP features.
const SYNCHRO_LDAP_TO_DOLIBARR
Ldap to Dolibarr synchronization.
Class to manage Dolibarr users.
dol_get_first_hour($date, $gm='tzserver')
Return GMT time for first hour of a given GMT date (it removes hours, min and second part)
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
check_user_password_ldap($usertotest, $passwordtotest, $entitytotest)
Check validity of user/password/entity If test is ko, reason must be filled into $_SESSION["dol_login...