AD & LDAP
Could somebody please provide some code sample how to list all users within the domain, including the guuid of each user? Also where is the list of the attributes the AD provides located? URL will be really appreciated.
this returns 0 as search results. It alows user b0fh to login (he is in the administrators group), but not search, it seems :(
/**
* searchforguid.java
* 5 July 2001
* Sample JNDI application to perform a search against the Active Directory
* and also return the objectGUID in both binary and string formats
*
*/
import java.util.Hashtable;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import javax.naming.*;
public class searchforguid {
public static void main (String[] args) {
Hashtable env = new Hashtable();
//Can use either DN, NTLM or UPN style credentials
String adminName = "b0fh@burunduki";
String adminPassword = "123456";
String ldapURL = "ldap://10.0.1.2:389";
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
//set security credentials, note using simple cleartext authentication
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,adminName);
env.put(Context.SECURITY_CREDENTIALS,adminPassword);
//specify attributes to be returned in binary format
env.put("java.naming.ldap.attributes.binary","objectGUID");
//connect to my domain controller
env.put(Context.PROVIDER_URL,ldapURL);
try {
//Create the initial directory context
LdapContext ctx = new InitialLdapContext(env,null);
//Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the attributes to return
String returnedAtts[]={"sn","givenName","mail","objectGUID"};
searchCtls.setReturningAttributes(returnedAtts);
//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
//String searchFilter = "(&(objectClass=user)(cn=Albert Einstein))";
String searchFilter = "(&(objectclass=user)(objectcategory=person));";
//Specify the Base for the search
String searchBase = "DC=burunduki";
//initialize counter to total the results
int totalResults = 0;
//Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
//Loop through the search results
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();
totalResults++;
System.out.println(">>>" + sr.getName());
// Print out some of the attributes, catch the exception if the attributes have no values
Attributes attrs = sr.getAttributes();
if (attrs != null) {
try {
System.out.println(" name: " + attrs.get("givenName").get() + " " + attrs.get("sn").get());
System.out.println(" mail: " + attrs.get("mail").get());
byte[] GUID = (byte[])attrs.get("objectGUID").get();
String strGUID = "";
String byteGUID = "";
//Convert the GUID into string using the byte format
for (int c=0;c<GUID.length;c++) {
byteGUID = byteGUID + "\\" + AddLeadingZero((int)GUID[c] & 0xFF);
}
//convert the GUID into string format
strGUID = "{";
strGUID = strGUID + AddLeadingZero((int)GUID[3] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[2] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[1] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[0] & 0xFF);
strGUID = strGUID + "-";
strGUID = strGUID + AddLeadingZero((int)GUID[5] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[4] & 0xFF);
strGUID = strGUID + "-";
strGUID = strGUID + AddLeadingZero((int)GUID[7] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[6] & 0xFF);
strGUID = strGUID + "-";
strGUID = strGUID + AddLeadingZero((int)GUID[8] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[9] & 0xFF);
strGUID = strGUID + "-";
strGUID = strGUID + AddLeadingZero((int)GUID[10] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[11] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[12] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[13] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[14] & 0xFF);
strGUID = strGUID + AddLeadingZero((int)GUID[15] & 0xFF);
strGUID = strGUID + "}";
System.out.println("GUID (String format): " + strGUID);
System.out.println("GUID (Byte format): " + byteGUID);
}
catch (NullPointerException e) {
System.err.println("Problem listing attributes: " + e);
}
}
}
System.out.println("Total results: " + totalResults);
ctx.close();
}
catch (NamingException e) {
System.err.println("Problem searching directory: " + e);
e.printStackTrace(System.out);
}
}
static String AddLeadingZero(int k) {
return ((k & 0xF) > 0)?"0" + Integer.toHexString(k):Integer.toHexString(k);
}
}this returns 0 as search results. It alows user b0fh to login (he is in the administrators group), but not search, it seems :(
