import javax.naming.directory.*;
import javax.naming.*;
import java.util.*;
public class ActDir {
public static void main(String args[]) throws Exception {
// java ActDir exdc03 389
Hashtable env = new Hashtable();
DirContext ctx;
String port = args[1];
String host = args[0];
String user = "corp\\username";
String passwd = "password";
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, passwd);
//env.put("java.naming.ldap.version", "3");
//env.put(Context.REFERRAL, "follow");
String url = new String("ldap://"+host+":"+port);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,url);
System.err.println("Connecting to " +url);
ctx = new InitialDirContext(env);
//ctx.bind(user, ctx, null);
System.err.println("Connected to " +url);
// Read supportedSASLMechanisms from root DSE
Attributes attrs = ctx.getAttributes(url,new String[]{"supportedSASLMechanisms"});
System.err.println(attrs);
Attributes answer = ctx.getAttributes("OU=People,OU=Exeter,DC=corp,DC=acme,DC=net");
// Specify the attributes to match
// Ask for objects that has a surname ("sn") attribute with
// the value "Geisel" and the "mail" attribute
Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case
matchAttrs.put(new BasicAttribute("sn", "Keeping"));
matchAttrs.put(new BasicAttribute("mail"));
// Search for objects that have those matching attributes
NamingEnumeration ne = ctx.search("OU=People,OU=Exeter,DC=corp,DC=acme,DC=net", matchAttrs);
while (ne.hasMoreElements()) {
System.out.println(ne.next());
}
ctx.close();
}
}