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