Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. #!/usr/local/bin/php
  2. <?php
  3.  
  4.  
  5. // $dir is profiles path
  6. // $username and password are a valid AD username & password
  7. $dir = "c:\path\to\profiles";
  8. $username = "username";
  9.  
  10. //must always check that password length > 0
  11. $password = "password";
  12.  
  13. // Setup Active Directory Params
  14. $ldap_url = 'dumbledore.rus.mson.org';
  15. $ldap_domain = 'rus.mson.org';
  16. $ldap_dn = "dc=rus,dc=mson,dc=org";
  17.  
  18. // Connect to LDAP
  19. $ds = ldap_connect( $ldap_url );
  20. ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
  21. ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
  22.  
  23. // now try a real login
  24. $login = ldap_bind( $ds, "$username@$ldap_domain", $password );
  25.  
  26. // OK. Is the path given a directory?
  27. if (is_dir($dir)) {
  28.     if ($dh = opendir($dir)) {
  29.         while (($file = readdir($dh)) !== false) {
  30.  
  31.         // Lets get directories within the directory
  32.         if(is_dir(''.$dir.'/'.$file.'') && $file != "." && $file != "..") {
  33.  
  34.                         // Lets search LDAP for our account
  35.                         $attributes = array("displayname");
  36.                         $filter = "(&(objectCategory=person)(sAMAccountName=$file))";
  37.  
  38.                         $result = ldap_search($ds, $ldap_dn, $filter, $attributes);
  39.  
  40.                         $entries = ldap_get_entries($ds, $result);
  41.  
  42.                         if($entries["count"] > 0){
  43.                                 // OK. Account exists, so lets go! :D
  44.                         } else {
  45.                         } else {
  46.                                 // Can't find account, goodbye profile.
  47.                                 exec("rmdir $dir/$file");
  48.  
  49.                         }
  50.                 }
  51.         }
  52.         closedir($dh);
  53.     }
  54. }
  55.  
  56.  
  57. ldap_unbind($ds);
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement