model.authent-ldap.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Authent LDAP
  20. * User authentication Module, no password at all!
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. class UserLDAP extends UserInternal
  26. {
  27. public static function Init()
  28. {
  29. $aParams = array
  30. (
  31. "category" => "addon/authentication",
  32. "key_type" => "autoincrement",
  33. "name_attcode" => "login",
  34. "state_attcode" => "",
  35. "reconc_keys" => array('login'),
  36. "db_table" => "",
  37. "db_key_field" => "id",
  38. "db_finalclass_field" => "",
  39. "display_template" => "",
  40. );
  41. MetaModel::Init_Params($aParams);
  42. MetaModel::Init_InheritAttributes();
  43. // Display lists
  44. MetaModel::Init_SetZListItems('details', array('contactid', 'first_name', 'email', 'login', 'language', 'status', 'profile_list', 'allowed_org_list')); // Attributes to be displayed for the complete details
  45. MetaModel::Init_SetZListItems('list', array('first_name', 'last_name', 'login', 'status')); // Attributes to be displayed for a list
  46. // Search criteria
  47. MetaModel::Init_SetZListItems('standard_search', array('login', 'contactid', 'status')); // Criteria of the std search form
  48. MetaModel::Init_SetZListItems('advanced_search', array('login', 'contactid')); // Criteria of the advanced search form
  49. }
  50. /**
  51. * Check the user's password against the LDAP server
  52. * Algorithm:
  53. * 1) Connect to the LDAP server, using a predefined account (or anonymously)
  54. * 2) Search for the specified user, based on a specific search query/pattern
  55. * 3) If exactly one user is found, continue, otherwise return false (wrong user or wrong query configured)
  56. * 3) Bind again to LDAP using the DN of the found user and the password
  57. * 4) If the bind is successful return true, otherwise return false (wrong password)
  58. * @param string $sPassword The user's password to validate against the LDAP server
  59. * @return boolean True if the password is Ok, false otherwise
  60. */
  61. public function CheckCredentials($sPassword)
  62. {
  63. $sLDAPHost = MetaModel::GetModuleSetting('authent-ldap', 'host', 'localhost');
  64. $iLDAPPort = MetaModel::GetModuleSetting('authent-ldap', 'port', 389);
  65. $sDefaultLDAPUser = MetaModel::GetModuleSetting('authent-ldap', 'default_user', '');
  66. $sDefaultLDAPPwd = MetaModel::GetModuleSetting('authent-ldap', 'default_pwd', '');
  67. $bLDAPStartTLS = MetaModel::GetModuleSetting('authent-ldap', 'start_tls', false);
  68. $aOptions = MetaModel::GetModuleSetting('authent-ldap', 'options', array());
  69. if (array_key_exists(LDAP_OPT_DEBUG_LEVEL, $aOptions))
  70. {
  71. // Set debug level before trying to connect, so that debug info appear in the PHP error log if ldap_connect goes wrong
  72. $bRet = ldap_set_option($hDS, LDAP_OPT_DEBUG_LEVEL, $aOptions[LDAP_OPT_DEBUG_LEVEL]);
  73. $this->LogMessage("ldap_set_option('$name', '$value') returned ".($bRet ? 'true' : 'false'));
  74. }
  75. $hDS = @ldap_connect($sLDAPHost, $iLDAPPort);
  76. if ($hDS === false)
  77. {
  78. $this->LogMessage("ldap_authentication: can not connect to the LDAP server '$sLDAPHost' (port: $iLDAPPort). Check the configuration file config-itop.php.");
  79. return false;
  80. }
  81. foreach($aOptions as $name => $value)
  82. {
  83. $bRet = ldap_set_option($hDS, $name, $value);
  84. $this->LogMessage("ldap_set_option('$name', '$value') returned ".($bRet ? 'true' : 'false'));
  85. }
  86. if ($bLDAPStartTLS)
  87. {
  88. $this->LogMessage("ldap_authentication: start tls required.");
  89. $hStartTLS = ldap_start_tls($hDS);
  90. //$this->LogMessage("ldap_authentication: hStartTLS = '$hStartTLS'");
  91. if (!$hStartTLS)
  92. {
  93. $this->LogMessage("ldap_authentication: start tls failed.");
  94. return false;
  95. }
  96. }
  97. if ($bind = @ldap_bind($hDS, $sDefaultLDAPUser, $sDefaultLDAPPwd))
  98. {
  99. // Search for the person, using the specified query expression
  100. $sLDAPUserQuery = MetaModel::GetModuleSetting('authent-ldap', 'user_query', '');
  101. $sBaseDN = MetaModel::GetModuleSetting('authent-ldap', 'base_dn', '');
  102. $sLogin = $this->Get('login');
  103. $iContactId = $this->Get('contactid');
  104. $sFirstName = '';
  105. $sLastName = '';
  106. $sEMail = '';
  107. if ($iContactId > 0)
  108. {
  109. $oPerson = MetaModel::GetObject('Person', $iContactId);
  110. if (is_object($oPerson))
  111. {
  112. $sFirstName = $oPerson->Get('first_name');
  113. $sLastName = $oPerson->Get('name');
  114. $sEMail = $oPerson->Get('email');
  115. }
  116. }
  117. // %1$s => login
  118. // %2$s => first name
  119. // %3$s => last name
  120. // %4$s => email
  121. $sQuery = sprintf($sLDAPUserQuery, $sLogin, $sFirstName, $sLastName, $sEMail);
  122. $hSearchResult = @ldap_search($hDS, $sBaseDN, $sQuery);
  123. $iCountEntries = ($hSearchResult !== false) ? @ldap_count_entries($hDS, $hSearchResult) : 0;
  124. switch($iCountEntries)
  125. {
  126. case 1:
  127. // Exactly one entry found, let's check the password by trying to bind with this user
  128. $aEntry = ldap_get_entries($hDS, $hSearchResult);
  129. $sUserDN = $aEntry[0]['dn'];
  130. $bUserBind = @ldap_bind($hDS, $sUserDN, $sPassword);
  131. if (($bUserBind !== false) && !empty($sPassword))
  132. {
  133. ldap_unbind($hDS);
  134. return true; // Password Ok
  135. }
  136. $this->LogMessage("ldap_authentication: wrong password for user: '$sUserDN'.");
  137. return false; // Wrong password
  138. break;
  139. case 0:
  140. // User not found...
  141. $this->LogMessage("ldap_authentication: no entry found with the query '$sQuery', base_dn = '$sBaseDN'. User not found in LDAP.");
  142. break;
  143. default:
  144. // More than one entry... maybe the query is not specific enough...
  145. $this->LogMessage("ldap_authentication: several (".ldap_count_entries($hDS, $hSearchResult).") entries match the query '$sQuery', base_dn = '$sBaseDN', check that the query defined in config-itop.php is specific enough.");
  146. }
  147. return false;
  148. }
  149. else
  150. {
  151. // Trace: invalid default user for LDAP initial binding
  152. $this->LogMessage("ldap_authentication: can not bind to the LDAP server '$sLDAPHost' (port: $iLDAPPort), user='$sDefaultLDAPUser', pwd='$sDefaultLDAPPwd'. Error: '".ldap_error($hDS)."'. Check the configuration file config-itop.php.");
  153. return false;
  154. }
  155. }
  156. public function TrustWebServerContext()
  157. {
  158. return false;
  159. }
  160. public function CanChangePassword()
  161. {
  162. return false;
  163. }
  164. public function ChangePassword($sOldPassword, $sNewPassword)
  165. {
  166. return false;
  167. }
  168. protected function LogMessage($sMessage, $aData = array())
  169. {
  170. if (MetaModel::GetModuleSetting('authent-ldap', 'debug', false) && MetaModel::IsLogEnabledIssue())
  171. {
  172. if (MetaModel::IsValidClass('EventIssue'))
  173. {
  174. $oLog = new EventIssue();
  175. $oLog->Set('message', $sMessage);
  176. $oLog->Set('userinfo', '');
  177. $oLog->Set('issue', 'LDAP Authentication');
  178. $oLog->Set('impact', 'User login rejected');
  179. $oLog->Set('data', $aData);
  180. $oLog->DBInsertNoReload();
  181. }
  182. IssueLog::Error($sMessage);
  183. }
  184. }
  185. }
  186. ?>