Guest User

Untitled

a guest
May 11th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. # Parse options
  5. $opts = getopt('h:n:u:p:b:s:', array( 'help' ));
  6.  
  7. if (isset($opts['help']) && $opts['help']) {
  8. echo <<<ENDHELP
  9. Usage:
  10. $argv[0] [-h HOSTNAME] [-n PORTNUM] [-u USERNAME] [-p PASSWORD] [-b BASEDN] [-s SEARCH]
  11. Where:
  12. HOSTNAME is the LDAP hostname to connect to; omit to use default (pool.ldap.csiro.au)
  13. PORTNUM is the port number to connect to; omit to use default (389)
  14. USERNAME is the username passed to ldap_bind(); omit to bind anonymously
  15. PASSWORD is the password passed to ldap_bind(); omit to bind without a password
  16. BASEDN is the base DN passed to ldap_search(); omit to use the default (DC=nexus,DC=csiro,DC=au)
  17. SEARCH is the search string passed to ldap_search(); omit to use the default (sAMAccountName=gib392)
  18. ENDHELP
  19. ;
  20. exit(0);
  21. }
  22.  
  23. # Extract options into variables
  24. $hostname = isset($opts['h']) ? $opts['h'] : 'pool.ldap.csiro.au';
  25. $port_num = isset($opts['n']) ? intval($opts['n']) : 389;
  26. $username = isset($opts['u']) ? $opts['u'] : null;
  27. $password = isset($opts['p']) ? $opts['p'] : null;
  28. $base_dn = isset($opts['b']) ? $opts['b'] : 'DC=nexus,DC=csiro,DC=au';
  29. $search = isset($opts['s']) ? $opts['s'] : 'sAMAccountName=gib392';
  30.  
  31. # Connect to LDAP
  32. echo "Executing: ldap_connect('$hostname', $port_num)" . PHP_EOL;
  33. $ldap = ldap_connect($hostname, $port_num);
  34.  
  35. # Protocol version 3 and no referrals are required for AD
  36. ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
  37. ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
  38.  
  39. # Bind to LDAP
  40. echo "Executing: ldap_bind(<ldap>, '$username', '$password')" . PHP_EOL;
  41. $bind_result = ldap_bind($ldap, $username, $password);
  42. if (!$bind_result) {
  43. echo "Error: Could not bind: " . PHP_EOL . ldap_error($ldap) . PHP_EOL;
  44. exit(100);
  45. }
  46.  
  47. # Perform search
  48. echo "Executing: ldap_search(<ldap>, '$base_dn', '$search')" . PHP_EOL;
  49. $results = ldap_search($ldap, $base_dn, $search);
  50. if (!$results) {
  51. echo "Error: Could not search" . PHP_EOL . ldap_error($ldap) . PHP_EOL;
  52. exit(200);
  53. }
  54.  
  55. # Output results
  56. echo "Got results fro LDAP search..." . PHP_EOL;
  57. print_r(ldap_get_entries($ldap, $results));
Add Comment
Please, Sign In to add comment