import java.io.UnsupportedEncodingException;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPJSSESecureSocketFactory;
public class GetAuthenticated
{
public static void main( String[] args ) {
if (args.length != 3) {
usage();
System.exit(1);
}
int ldapVersion = LDAPConnection.LDAP_V3;
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapSSLPort = LDAPConnection.DEFAULT_SSL_PORT;
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
LDAPConnection conn = new LDAPConnection();
anonymousBind( conn, ldapHost, ldapPort );
simpleBind1( conn, ldapHost, ldapPort, loginDN, password );
simpleBind2( ldapVersion, conn, ldapHost, ldapPort, loginDN, password );
java.security.Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());
SSLBind( ldapVersion, ldapHost, ldapSSLPort, loginDN, password );
System.exit(0);
}
private static void usage() {
System.err.println(
"Usage: java GetAuthenticated <host Name> <login dn> <password>");
System.err.println(
"Example: java GetAuthenticated Acme.com \"cn=admin,o=Acme\" secret");
System.err.println(
"To set the keystore for JSSE: " +
"java -Djavax.net.ssl.trustStore=/path/keystoreName.keystore ...");
}
private static void anonymousBind( LDAPConnection conn, String host,
int port ) {
try {
System.out.println("\nanonymous bind...");
conn.connect( host, port );
System.out.println((conn.isBound()) ?
"\n\tAuthenticated to the server\n":
"\n\tAnonymous bind to the server\n");
conn.disconnect();
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
}
return;
}
private static void simpleBind1(LDAPConnection conn, String host,
int port, String dn, String passwd ) {
try {
System.out.println("Simple bind...");
conn.connect( host, port );
try {
conn.bind( LDAPConnection.LDAP_V3, dn, passwd.getBytes("UTF8") );
} catch (UnsupportedEncodingException u){
throw new LDAPException( "UTF8 Invalid Encoding",
LDAPException.LOCAL_ERROR,
(String)null, u);
}
System.out.println((conn.isBound()) ?
"\n\tAuthenticated to the server ( simple )\n":
"\n\tNot authenticated to the server\n");
conn.disconnect();
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
}
return;
}
private static void simpleBind2( int version, LDAPConnection conn,
String host, int port,String dn, String passwd ) {
try {
System.out.println("Simple bind with connection method...");
conn.connect( host, port );
try {
conn.bind( version, dn, passwd.getBytes("UTF8") );
} catch (UnsupportedEncodingException u){
throw new LDAPException( "UTF8 Invalid Encoding",
LDAPException.LOCAL_ERROR,
(String)null, u);
}
System.out.println((conn.isBound()) ?
"\n\tAuthenticated to the server ( simple )\n":
"\n\tNot authenticated to the server\n");
conn.disconnect();
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
}
return;
}
private static void SSLBind( int version, String host, int SSLPort,
String dn, String passwd ) {
LDAPJSSESecureSocketFactory ssf = new LDAPJSSESecureSocketFactory();
LDAPConnection conn = new LDAPConnection(ssf);
try {
System.out.println("SSL bind...");
conn.connect( host, SSLPort);
try {
conn.bind( version, dn, passwd.getBytes("UTF8") );
} catch (UnsupportedEncodingException u){
throw new LDAPException( "UTF8 Invalid Encoding",
LDAPException.LOCAL_ERROR,
(String)null, u);
}
System.out.println((conn.isBound()) ?
"\n\tAuthenticated to the server ( ssl )\n":
"\n\tNot authenticated to the server\n");
conn.disconnect();
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
}
return;
}
}