//Warning: This code has been marked up for HTML

/**************************************************************************
*  Novell Software Developer Kit
*
*  Copyright (C) 2002-2003 Novell, Inc. All Rights Reserved.
*
*  THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
*  USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
*  ACCOMPANYING THE SOFTWARE DEVELOPER KIT (SDK) THAT CONTAINS THIS WORK.
*  PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
*  ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
*  PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
*  DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
*  PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
*  CUSTOMERS WITH RESPECT TO THIS CODE.
*
* $name:         GetBindDN.java
* $description:  GetBindDN.java retrieves the distinguished name
*                of the user object who made the bind to the LDAP server.
******************************************************************************/
import java.util.Hashtable;
import java.io.IOException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import com.novell.service.ndssdk.jndi.ldap.ext.GetBindDNRequest;
import com.novell.service.ndssdk.jndi.ldap.ext.GetBindDNResponse;

public class GetBindDN {
    public static void main(String[] args) {

        if (args.length != 3) {
            usage();
        }

        String hostURL      = args[0];
        String loginDN      = args[1];
        String passWord     = args[2];
        String identityName;

        try {
           // Specify the initial context.

            Hashtable env = new Hashtable(5, 0.75f);

            env.put(Context.INITIAL_CONTEXT_FACTORY,
                            "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, hostURL);
            env.put(Context.SECURITY_AUTHENTICATION, "simple" );
            env.put(Context.SECURITY_PRINCIPAL, loginDN );
            env.put(Context.SECURITY_CREDENTIALS, passWord );

           // Construct an LdapContext object.

            LdapContext ctx = new InitialLdapContext(env, null);

           // Call extended operation to get context identity name.

            GetBindDNRequest  reqs =
                                new GetBindDNRequest();

            GetBindDNResponse resp =
                (GetBindDNResponse)ctx.extendedOperation(reqs);

            identityName = resp.getBindDN();
            System.out.println("\n\tContext Identity Name: \n\t" + identityName );

            System.out.println("\n\tGetBindDN operation succeeded.");
        }
        catch (NamingException e) {
            System.err.println("\n\tGetBindDN operation failed.");
            e.printStackTrace();
        }
        catch (IOException ioe) {
            System.err.println("GetBindDN operation failed.");
            ioe.printStackTrace();
        }
        finally {
            System.exit(0);
        }
    }

    public static void usage() {
        System.out.println("\n Usage  : java GetBindDN <host URL>"
            + " <login dn> <password>");
        System.out.println("\n Example: java GetBindDN ldap://Acme.com:389"
            + " cn=admin,o=Acme secret");
        System.exit(1);
    }
}