Guest User

Untitled

a guest
Jan 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. $server="XXX.XXX.XXX.XXX";
  2. $dn = "cn=$username, ";
  3. $basedn="ou=users, ou=accounts, dc=domain, dc=com";
  4.  
  5. if (!($connect = ldap_connect($server))) {
  6. die ("Could not connect to LDAP server");
  7. }
  8.  
  9. if (!($bind = ldap_bind($connect, "$dn" . "$basedn", $password))) {
  10. die ("Could not bind to $dn");
  11. }
  12.  
  13. $sr = ldap_search($connect, $basedn,"$filter");
  14. $info = ldap_get_entries($connect, $sr);
  15. $fullname=$info[0]["displayname"][0];
  16. $fqdn=$info[0]["dn"];
  17.  
  18. <?php
  19.  
  20. //We just need six varaiables here
  21. $baseDN = 'CN=Users,DC=domain,DC=local';
  22. $adminDN = "YourAdminDN";//this is the admin distinguishedName
  23. $adminPswd = "YourAdminPass";
  24. $username = 'Username';//this is the user samaccountname
  25. $userpass = 'UserPass';
  26. $ldap_conn = ldap_connect('ldaps://yourADdomain.local');//I'm using LDAPS here
  27.  
  28. if (! $ldap_conn) {
  29. echo ("<p style='color: red;'>Couldn't connect to LDAP service</p>");
  30. }
  31. else {
  32. echo ("<p style='color: green;'>Connection to LDAP service successful!</p>");
  33. }
  34. //The first step is to bind the administrator so that we can search user info
  35. $ldapBindAdmin = ldap_bind($ldap_conn, $adminDN, $adminPswd);
  36.  
  37. if ($ldapBindAdmin){
  38. echo ("<p style='color: green;'>Admin binding and authentication successful!!!</p>");
  39.  
  40. $filter = '(sAMAccountName='.$username.')';
  41. $attributes = array("name", "telephonenumber", "mail", "samaccountname");
  42. $result = ldap_search($ldap_conn, $baseDN, $filter, $attributes);
  43.  
  44. $entries = ldap_get_entries($ldap_conn, $result);
  45. $userDN = $entries[0]["name"][0];
  46. echo ('<p style="color:green;">I have the user DN: '.$userDN.'</p>');
  47.  
  48. //Okay, we're in! But now we need bind the user now that we have the user's DN
  49. $ldapBindUser = ldap_bind($ldap_conn, $userDN, $userpass);
  50.  
  51. if($ldapBindUser){
  52. echo ("<p style='color: green;'>User binding and authentication successful!!!</p>");
  53.  
  54. ldap_unbind($ldap_conn); // Clean up after ourselves.
  55.  
  56. } else {
  57. echo ("<p style='color: red;'>There was a problem binding the user to LDAP :(</p>");
  58. }
  59.  
  60. } else {
  61. echo ("<p style='color: red;'>There was a problem binding the admin to LDAP :(</p>");
  62. }
  63. ?>
  64.  
  65. ldap_bind($connect, "", "")
  66. $sr = ldap_search($connect, $base_dn, "(sAMAccountName=$username)")
  67.  
  68. ldap_bind($connect, "DN=LDAP_App,OU=Users,DC=Domain,DC=com", "thePassword")
  69. $sr = ldap_search($connect, $base_dn, "(sAMAccountName=$username)")
Add Comment
Please, Sign In to add comment