import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPExtendedOperation;
import com.novell.ldap.LDAPExtendedResponse;
import com.novell.ldap.extensions.GetBindDNRequest;
import com.novell.ldap.extensions.GetBindDNResponse;
import java.io.UnsupportedEncodingException;
public class GetBindDN {
public static void main( String[] args ) {
if (args.length != 4) {
System.err.println("Usage: java GetBindDN "
+ "<host Name> <port number> <login dn>"
+ "\n <password>");
System.err.println("Example: java GetBindDN Acme.com "
+ "389 \"cn=Admin,o=Acme\" secret");
System.exit(1);
}
int ldapVersion = LDAPConnection.LDAP_V3;
String ldapHost = args[0];
int ldapPort = Integer.parseInt(args[1]);
String loginDN = args[2];
String password = args[3];
LDAPConnection ld = new LDAPConnection();
try {
ld.connect( ldapHost, ldapPort);
ld.bind( ldapVersion, loginDN, password.getBytes("UTF8") );
System.out.println( "\nLogin succeeded");
LDAPExtendedOperation request = new GetBindDNRequest();
LDAPExtendedResponse response = ld.extendedOperation(request);
if ( ( (response.getResultCode()) == LDAPException.SUCCESS ) &&
( response instanceof GetBindDNResponse ) ) {
System.out.println("You were logged in as: "
+ ((GetBindDNResponse)response).
getIdentity());
System.out.println("\nGetBindDN succeeded.\n");
}
else {
System.out.println("GetBindDN failed.\n");
throw new LDAPException( response.getErrorMessage(),
response.getResultCode(),
(String)null);
}
if ( ld.isConnected() )
ld.disconnect();
}
catch( LDAPException e ) {
System.out.println( "\nError: " + e.toString() );
}
catch( UnsupportedEncodingException e ) {
System.out.println( "Error: " + e.toString() );
}
}
}