PageRenderTime 44ms CodeModel.GetById 5ms RepoModel.GetById 7ms app.codeStats 0ms

/libraries/joomla/client/ldap.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 487 lines | 278 code | 33 blank | 176 comment | 34 complexity | 7b0e12958e4dce1ce89c52277de6e9f7 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: ldap.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Client
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software and parts of it may contain or be derived from the
  9. * GNU General Public License or other free or open source software licenses.
  10. * See COPYRIGHT.php for copyright notices and details.
  11. */
  12. /**
  13. * LDAP client class
  14. *
  15. * @package Joomla.Framework
  16. * @subpackage Client
  17. * @since 1.5
  18. */
  19. // no direct access
  20. defined('_JEXEC') or die('Restricted access');
  21. class JLDAP extends JObject
  22. {
  23. /** @var string Hostname of LDAP server
  24. @access public */
  25. var $host = null;
  26. /** @var bool Authorization Method to use
  27. @access public */
  28. var $auth_method = null;
  29. /** @var int Port of LDAP server
  30. @access public */
  31. var $port = null;
  32. /** @var string Base DN (e.g. o=MyDir)
  33. @access public */
  34. var $base_dn = null;
  35. /** @var string User DN (e.g. cn=Users,o=MyDir)
  36. @access public */
  37. var $users_dn = null;
  38. /** @var string Search String
  39. @access public */
  40. var $search_string = null;
  41. /** @var boolean Use LDAP Version 3
  42. @access public */
  43. var $use_ldapV3 = null;
  44. /** @var boolean No referrals (server transfers)
  45. @access public */
  46. var $no_referrals = null;
  47. /** @var boolean Negotiate TLS (encrypted communications)
  48. @access public */
  49. var $negotiate_tls = null;
  50. /** @var string Username to connect to server
  51. @access public */
  52. var $username = null;
  53. /** @var string Password to connect to server
  54. @access public */
  55. var $password = null;
  56. /** @var mixed LDAP Resource Identifier
  57. @access private */
  58. var $_resource = null;
  59. /** @var string Current DN
  60. @access private */
  61. var $_dn = null;
  62. /**
  63. * Constructor
  64. *
  65. * @param object An object of configuration variables
  66. * @access public
  67. */
  68. function __construct($configObj = null)
  69. {
  70. if (is_object($configObj))
  71. {
  72. $vars = get_class_vars(get_class($this));
  73. foreach (array_keys($vars) as $var)
  74. {
  75. if (substr($var, 0, 1) != '_') {
  76. if ($param = $configObj->get($var)) {
  77. $this-> $var = $param;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * Connect to server
  85. * @return boolean True if successful
  86. * @access public
  87. */
  88. function connect()
  89. {
  90. if ($this->host == '') {
  91. return false;
  92. }
  93. $this->_resource = @ ldap_connect($this->host, $this->port);
  94. if ($this->_resource)
  95. {
  96. if ($this->use_ldapV3) {
  97. if (!@ldap_set_option($this->_resource, LDAP_OPT_PROTOCOL_VERSION, 3)) {
  98. return false;
  99. }
  100. }
  101. if (!@ldap_set_option($this->_resource, LDAP_OPT_REFERRALS, intval($this->no_referrals))) {
  102. return false;
  103. }
  104. if ($this->negotiate_tls) {
  105. if (!@ldap_start_tls($this->_resource)) {
  106. return false;
  107. }
  108. }
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. }
  114. /**
  115. * Close the connection
  116. * @access public
  117. */
  118. function close() {
  119. @ ldap_close($this->_resource);
  120. }
  121. /**
  122. * Sets the DN with some template replacements
  123. *
  124. * @param string The username
  125. * @access public
  126. */
  127. function setDN($username,$nosub = 0)
  128. {
  129. if ($this->users_dn == '' || $nosub) {
  130. $this->_dn = $username;
  131. } else if(strlen($username)) {
  132. $this->_dn = str_replace('[username]', $username, $this->users_dn);
  133. } else {
  134. $this->_dn = '';
  135. }
  136. }
  137. /**
  138. * @return string The current dn
  139. * @access public
  140. */
  141. function getDN() {
  142. return $this->_dn;
  143. }
  144. /**
  145. * Anonymously Binds to LDAP Directory
  146. */
  147. function anonymous_bind()
  148. {
  149. $bindResult = @ldap_bind($this->_resource);
  150. return $bindResult;
  151. }
  152. /**
  153. * Binds to the LDAP directory
  154. *
  155. * @param string The username
  156. * @param string The password
  157. * @return boolean Result
  158. * @access public
  159. */
  160. function bind($username = null, $password = null, $nosub = 0)
  161. {
  162. if (is_null($username)) {
  163. $username = $this->username;
  164. }
  165. if (is_null($password)) {
  166. $password = $this->password;
  167. }
  168. $this->setDN($username,$nosub);
  169. //if(strlen($this->getDN()))
  170. $bindResult = @ldap_bind($this->_resource, $this->getDN(), $password);
  171. return $bindResult;
  172. }
  173. /**
  174. * Perform an LDAP search using comma seperated search strings
  175. *
  176. * @param string search string of search values
  177. */
  178. function simple_search($search)
  179. {
  180. $results = explode(';', $search);
  181. foreach($results as $key=>$result) {
  182. $results[$key] = '('.$result.')';
  183. }
  184. return $this->search($results);
  185. }
  186. /**
  187. * Perform an LDAP search
  188. *
  189. * @param array Search Filters (array of strings)
  190. * @param string DN Override
  191. * @return array Multidimensional array of results
  192. * @access public
  193. */
  194. function search($filters, $dnoverride = null)
  195. {
  196. $attributes = array ();
  197. if ($dnoverride) {
  198. $dn = $dnoverride;
  199. } else {
  200. $dn = $this->base_dn;
  201. }
  202. $resource = $this->_resource;
  203. foreach ($filters as $search_filter)
  204. {
  205. $search_result = @ldap_search($resource, $dn, $search_filter);
  206. if ($search_result && ($count = @ldap_count_entries($resource, $search_result)) > 0)
  207. {
  208. for ($i = 0; $i < $count; $i++)
  209. {
  210. $attributes[$i] = Array ();
  211. if (!$i) {
  212. $firstentry = @ldap_first_entry($resource, $search_result);
  213. } else {
  214. $firstentry = @ldap_next_entry($resource, $firstentry);
  215. }
  216. $attributes_array = @ldap_get_attributes($resource, $firstentry); // load user-specified attributes
  217. // ldap returns an array of arrays, fit this into attributes result array
  218. foreach ($attributes_array as $ki => $ai)
  219. {
  220. if (is_array($ai))
  221. {
  222. $subcount = $ai['count'];
  223. $attributes[$i][$ki] = Array ();
  224. for ($k = 0; $k < $subcount; $k++) {
  225. $attributes[$i][$ki][$k] = $ai[$k];
  226. }
  227. }
  228. }
  229. $attributes[$i]['dn'] = @ldap_get_dn($resource, $firstentry);
  230. }
  231. }
  232. }
  233. return $attributes;
  234. }
  235. /**
  236. * Replace an entry and return a true or false result
  237. *
  238. * @param string dn The DN which contains the attribute you want to replace
  239. * @param string attribute The attribute values you want to replace
  240. * @return mixed result of comparison (true, false, -1 on error)
  241. */
  242. function replace($dn, $attribute) {
  243. return @ldap_mod_replace($this->_resource, $dn, $attribute);
  244. }
  245. /**
  246. * Modifies an entry and return a true or false result
  247. *
  248. * @param string dn The DN which contains the attribute you want to modify
  249. * @param string attribute The attribute values you want to modify
  250. * @return mixed result of comparison (true, false, -1 on error)
  251. */
  252. function modify($dn, $attribute) {
  253. return @ldap_modify($this->_resource, $dn, $attribute);
  254. }
  255. /**
  256. * Removes attribute value from given dn and return a true or false result
  257. *
  258. * @param string dn The DN which contains the attribute you want to remove
  259. * @param string attribute The attribute values you want to remove
  260. * @return mixed result of comparison (true, false, -1 on error)
  261. */
  262. function remove($dn, $attribute)
  263. {
  264. $resource = $this->_resource;
  265. return @ldap_mod_del($resource, $dn, $attribute);
  266. }
  267. /**
  268. * Compare an entry and return a true or false result
  269. *
  270. * @param string dn The DN which contains the attribute you want to compare
  271. * @param string attribute The attribute whose value you want to compare
  272. * @param string value The value you want to check against the LDAP attribute
  273. * @return mixed result of comparison (true, false, -1 on error)
  274. * @access public
  275. */
  276. function compare($dn, $attribute, $value) {
  277. return @ldap_compare($this->_resource, $dn, $attribute, $value);
  278. }
  279. /**
  280. * Read all or specified attributes of given dn
  281. *
  282. * @param string dn The DN of the object you want to read
  283. * @param string attribute The attribute values you want to read (Optional)
  284. * @return array of attributes or -1 on error
  285. * @access public
  286. */
  287. function read($dn, $attribute = array())
  288. {
  289. $base = substr($dn,strpos($dn,',')+1);
  290. $cn = substr($dn,0,strpos($dn,','));
  291. $result = @ldap_read($this->_resource, $base, $cn);
  292. if ($result) {
  293. return @ldap_get_entries($this->_resource, $result);
  294. } else {
  295. return $result;
  296. }
  297. }
  298. /**
  299. * Deletes a given DN from the tree
  300. *
  301. * @param string dn The DN of the object you want to delete
  302. * @return bool result of operation
  303. * @access public
  304. */
  305. function delete($dn) {
  306. return @ldap_delete($this->_resource, $dn);
  307. }
  308. /**
  309. * Create a new DN
  310. *
  311. * @param string dn The DN where you want to put the object
  312. * @param array entries An array of arrays describing the object to add
  313. * @return bool result of operation
  314. */
  315. function create($dn, $entries) {
  316. return @ldap_add($this->_resource, $dn, $entries);
  317. }
  318. /**
  319. * Add an attribute to the given DN
  320. * Note: DN has to exist already
  321. *
  322. * @param string dn The DN of the entry to add the attribute
  323. * @param array entry An array of arrays with attributes to add
  324. * @return bool Result of operation
  325. */
  326. function add($dn, $entry) {
  327. return @ldap_mod_add($this->_resource, $dn, $entry);
  328. }
  329. /**
  330. * Rename the entry
  331. *
  332. * @param string dn The DN of the entry at the moment
  333. * @param string newdn The DN of the entry should be (only cn=newvalue)
  334. * @param string newparent The full DN of the parent (null by default)
  335. * @param bool deleteolddn Delete the old values (default)
  336. * @return bool Result of operation
  337. */
  338. function rename($dn, $newdn, $newparent, $deleteolddn) {
  339. return @ldap_rename($this->_resource, $dn, $newdn, $newparent, $deleteolddn);
  340. }
  341. /**
  342. * Returns the error message
  343. *
  344. * @return string error message
  345. */
  346. function getErrorMsg() {
  347. return @ldap_error($this->_resource);
  348. }
  349. /**
  350. * Converts a dot notation IP address to net address (e.g. for Netware, etc)
  351. *
  352. * @param string IP Address (e.g. xxx.xxx.xxx.xxx)
  353. * @return string Net address
  354. * @access public
  355. */
  356. function ipToNetAddress($ip)
  357. {
  358. $parts = explode('.', $ip);
  359. $address = '1#';
  360. foreach ($parts as $int) {
  361. $tmp = dechex($int);
  362. if (strlen($tmp) != 2) {
  363. $tmp = '0' . $tmp;
  364. }
  365. $address .= '\\' . $tmp;
  366. }
  367. return $address;
  368. }
  369. /**
  370. * extract readable network address from the LDAP encoded networkAddress attribute.
  371. * @author Jay Burrell, Systems & Networks, Mississippi State University
  372. * Please keep this document block and author attribution in place.
  373. *
  374. * Novell Docs, see: http://developer.novell.com/ndk/doc/ndslib/schm_enu/data/sdk5624.html#sdk5624
  375. * for Address types: http://developer.novell.com/ndk/doc/ndslib/index.html?page=/ndk/doc/ndslib/schm_enu/data/sdk4170.html
  376. * LDAP Format, String:
  377. * taggedData = uint32String "#" octetstring
  378. * byte 0 = uint32String = Address Type: 0= IPX Address; 1 = IP Address
  379. * byte 1 = char = "#" - separator
  380. * byte 2+ = octetstring - the ordinal value of the address
  381. * Note: with eDirectory 8.6.2, the IP address (type 1) returns
  382. * correctly, however, an IPX address does not seem to. eDir 8.7 may correct this.
  383. * Enhancement made by Merijn van de Schoot:
  384. * If addresstype is 8 (UDP) or 9 (TCP) do some additional parsing like still returning the IP address
  385. */
  386. function LDAPNetAddr($networkaddress)
  387. {
  388. $addr = "";
  389. $addrtype = intval(substr($networkaddress, 0, 1));
  390. $networkaddress = substr($networkaddress, 2); // throw away bytes 0 and 1 which should be the addrtype and the "#" separator
  391. if (($addrtype == 8) || ($addrtype = 9)) {
  392. // TODO 1.6: If UDP or TCP, (TODO fill addrport and) strip portnumber information from address
  393. $networkaddress = substr($networkaddress, (strlen($networkaddress)-4));
  394. }
  395. $addrtypes = array (
  396. 'IPX',
  397. 'IP',
  398. 'SDLC',
  399. 'Token Ring',
  400. 'OSI',
  401. 'AppleTalk',
  402. 'NetBEUI',
  403. 'Socket',
  404. 'UDP',
  405. 'TCP',
  406. 'UDP6',
  407. 'TCP6',
  408. 'Reserved (12)',
  409. 'URL',
  410. 'Count'
  411. );
  412. $len = strlen($networkaddress);
  413. if ($len > 0)
  414. {
  415. for ($i = 0; $i < $len; $i += 1)
  416. {
  417. $byte = substr($networkaddress, $i, 1);
  418. $addr .= ord($byte);
  419. if ( ($addrtype == 1) || ($addrtype == 8) || ($addrtype = 9) ) { // dot separate IP addresses...
  420. $addr .= ".";
  421. }
  422. }
  423. if ( ($addrtype == 1) || ($addrtype == 8) || ($addrtype = 9) ) { // strip last period from end of $addr
  424. $addr = substr($addr, 0, strlen($addr) - 1);
  425. }
  426. } else {
  427. $addr .= "address not available.";
  428. }
  429. return Array('protocol'=>$addrtypes[$addrtype], 'address'=>$addr);
  430. }
  431. /**
  432. * Generates a LDAP compatible password
  433. *
  434. * @param string password Clear text password to encrypt
  435. * @param string type Type of password hash, either md5 or SHA
  436. * @return string encrypted password
  437. */
  438. function generatePassword($password, $type='md5') {
  439. $userpassword = '';
  440. switch(strtolower($type)) {
  441. case 'sha':
  442. $userpassword = '{SHA}' . base64_encode( pack( 'H*', sha1( $password ) ) );
  443. case 'md5':
  444. default:
  445. $userpassword = '{MD5}' . base64_encode( pack( 'H*', md5( $password ) ) );
  446. break;
  447. }
  448. return $userpassword;
  449. }
  450. }