test_ldap.phps 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * LDAP connection test script
  4. *
  5. * Script is extended version of LDAP test script from PHP LDAP extension
  6. * manual. It does not suppress LDAP function errors. If some LDAP function
  7. * fails, you should see PHP error messages. If function is missing, you should
  8. * see errors too. If LDAP server returns unexpected output, you should see
  9. * errors.
  10. *
  11. * Change file extension from .phps to .php, if you want to use it. Don't store
  12. * important information (like your luggage password) on this file.
  13. * Copyright (c) 2006 The SquirrelMail Project
  14. * License: script is licensed under GPL.
  15. * See http://www.opensource.org/licenses/gpl-license.php
  16. */
  17. /** Configuration variables */
  18. /**
  19. * URL of LDAP server
  20. *
  21. * You can use IP address, hostname or any other type of URL
  22. * supported by your LDAP libraries. For example: you can add ldaps:// prefix
  23. * for LDAP over SSL connection (636 port) or ldapi:// for LDAP socket
  24. * connection.
  25. */
  26. $ldap_host='localhost';
  27. /**
  28. * LDAP BaseDN
  29. *
  30. * If you don't know it, script will try to show first available basedn when
  31. * it reads LDAP server's base.
  32. */
  33. $ldap_basedn='dc=example,dc=org';
  34. /**
  35. * Controls use of LDAP v3 bind protocol
  36. *
  37. * PHP scripts default to v2 protocol and some LDAP servers (for example: newer
  38. * OpenLDAP versions and ADS) don't support it.
  39. */
  40. $ldap_v3bind=false;
  41. /**
  42. * Controls use of LDAP STARTTLS
  43. *
  44. * Allows to enable TLS encryption on plain text LDAP connection.
  45. * Requires PHP 4.2.0 or newer.
  46. */
  47. $ldap_starttls=false;
  48. /**
  49. * ADS limit scope option
  50. * http://msdn.microsoft.com/library/en-us/ldap/ldap/ldap_server_domain_scope_oid.asp
  51. * Might be required for some Win2k3 ADS setups. Don't enable on other servers.
  52. * Warning: LDAP base search will fail, if option is enabled.
  53. */
  54. $ldap_limit_scope=false;
  55. /**
  56. * BindDN used for authentication
  57. */
  58. $ldap_binddn='';
  59. /**
  60. * Password used for authentication
  61. */
  62. $ldap_bindpw='';
  63. /* end of configuration variables */
  64. // modifications stop here.
  65. /* set error reporting options */
  66. ini_set('html_errors','off');
  67. ini_set('display_errors','on');
  68. error_reporting(E_ALL);
  69. /* set plain text header */
  70. header('Content-Type: text/plain');
  71. /* start testing*/
  72. echo "LDAP query test\n\n";
  73. echo "Connecting ...\n";
  74. $ds=ldap_connect($ldap_host); // must be a valid LDAP server!
  75. echo " connect result - ";
  76. var_dump($ds);
  77. echo "\n";
  78. if ($ds) {
  79. echo "\nSetting LDAP options:\n";
  80. if ($ldap_v3bind) {
  81. if (ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
  82. echo " Using LDAPv3\n";
  83. } else {
  84. echo " Failed to set protocol version to 3\n";
  85. }
  86. } else {
  87. echo " Using LDAPv2 (php default)\n";
  88. }
  89. if ($ldap_starttls) {
  90. if ($ldap_v3bind) {
  91. if (ldap_start_tls($ds)) {
  92. echo " Turned on TLS\n";
  93. } else {
  94. echo " Unable to turn on TLS\n";
  95. }
  96. } else {
  97. echo " You must use LDAPv3 protocol with STARTTLS.\n";
  98. }
  99. } else {
  100. echo " Not using LDAP STARTTLS.\n";
  101. }
  102. if ($ldap_limit_scope) {
  103. if ($ldap_v3bind) {
  104. $ctrl = array ( "oid" => "1.2.840.113556.1.4.1339", "iscritical" => TRUE );
  105. if (ldap_set_option($ds, LDAP_OPT_SERVER_CONTROLS, array($ctrl))) {
  106. echo " Turned on limit_scope\n";
  107. } else {
  108. echo " Unable to turn on limit_scope\n";
  109. }
  110. } else {
  111. echo " You must use LDAPv3 protocol with limit_scope option.\n";
  112. }
  113. } else {
  114. echo " Not using limit_scope option.\n";
  115. }
  116. echo "\nReading LDAP base:\n";
  117. if ($sr = ldap_read($ds,'',"(objectclass=*)")) {
  118. $info = ldap_get_entries($ds, $sr);
  119. echo " namingContexts:\n";
  120. if (isset($info[0]['namingcontexts'])) {
  121. for ($i=0; $i<$info[0]['namingcontexts']['count']; $i++) {
  122. echo ' ' . $i .': ' . $info[0]['namingcontexts'][$i] . "\n";
  123. }
  124. } else {
  125. echo " unavailable\n";
  126. }
  127. } else {
  128. echo " Unable to read LDAP base.\n";
  129. }
  130. echo "\n";
  131. echo "Authentication:\n";
  132. echo " Binding";
  133. if ($ldap_binddn!='') {
  134. echo " with authenticated bind ...\n";
  135. $r = ldap_bind($ds,$ldap_binddn,$ldap_bindpw);
  136. } else {
  137. echo " with anonymous bind ...\n";
  138. $r=ldap_bind($ds);
  139. }
  140. echo " Bind result - ";
  141. var_dump($r);
  142. echo "\n";
  143. echo "\n";
  144. echo "Search:\n";
  145. echo " Searching for (mail=*) ...\n";
  146. // Search for mail entries
  147. if ($sr=ldap_search($ds, $ldap_basedn, "(mail=*)")) {
  148. echo " Search result - ";
  149. var_dump($sr);
  150. echo "\n";
  151. echo " Number of entries: " . ldap_count_entries($ds, $sr) . "\n";
  152. echo " Getting entries ...\n";
  153. $info = ldap_get_entries($ds, $sr);
  154. echo " Data for " . $info["count"] . " items returned:\n";
  155. for ($i=0; $i<$info["count"]; $i++) {
  156. echo " dn is: " . $info[$i]["dn"] . "\n";
  157. if (isset($info[$i]["cn"][0])) {
  158. echo " first cn entry is: " . $info[$i]["cn"][0] . "\n";
  159. } else {
  160. echo " cn attribute is not available.";
  161. }
  162. echo " first email entry is: " . $info[$i]["mail"][0] . "\n------\n";
  163. }
  164. } else {
  165. echo " LDAP search failed.\n";
  166. }
  167. echo "\n";
  168. echo "Closing connection\n";
  169. ldap_close($ds);
  170. } else {
  171. echo "Unable to connect to LDAP server\n";
  172. }
  173. ?>