001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *      Copyright 2012-2015 ForgeRock AS.
024 *
025 */
026
027package org.forgerock.opendj.examples;
028
029import java.io.Console;
030
031import org.forgerock.opendj.ldap.Connection;
032import org.forgerock.opendj.ldap.DN;
033import org.forgerock.opendj.ldap.Filter;
034import org.forgerock.opendj.ldap.LdapException;
035import org.forgerock.opendj.ldap.LDAPConnectionFactory;
036import org.forgerock.opendj.ldap.SearchScope;
037import org.forgerock.opendj.ldap.responses.SearchResultEntry;
038
039/**
040 * An interactive command-line client that performs a search and subsequent
041 * simple bind. The client prompts for email address and for a password, and
042 * then searches based on the email address, to bind as the user with the
043 * password. If successful, the client displays the common name from the user's
044 * entry.
045 * <ul>
046 * <li>host - host name of the directory server</li>
047 * <li>port - port number of the directory server</li>
048 * <li>base-dn - base DN for the search, e.g. dc=example,dc=com</li>
049 * </ul>
050 * All arguments are required.
051 */
052public final class SearchBind {
053    /**
054     * Prompt for email and password, search and bind, then display message.
055     *
056     * @param args
057     *            The command line arguments: host, port, base-dn.
058     */
059    public static void main(final String[] args) {
060        if (args.length != 3) {
061            System.err.println("Usage: host port base-dn");
062            System.err.println("For example: localhost 1389 dc=example,dc=com");
063            System.exit(1);
064        }
065        String host = args[0];
066        int port = Integer.parseInt(args[1]);
067        String baseDN = args[2];
068
069        // --- JCite ---
070        // Prompt for mail and password.
071        Console c = System.console();
072        if (c == null) {
073            System.err.println("No console.");
074            System.exit(1);
075        }
076
077        String mail = c.readLine("Email address: ");
078        char[] password = c.readPassword("Password: ");
079
080        // Search using mail address, and then bind with the DN and password.
081        final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
082        Connection connection = null;
083        try {
084            connection = factory.getConnection();
085            SearchResultEntry entry =
086                    connection.searchSingleEntry(baseDN,
087                            SearchScope.WHOLE_SUBTREE,
088                            Filter.equality("mail", mail).toString(),
089                            "cn");
090            DN bindDN = entry.getName();
091            connection.bind(bindDN.toString(), password);
092
093            String cn = entry.getAttribute("cn").firstValueAsString();
094            System.out.println("Hello, " + cn + "!");
095        } catch (final LdapException e) {
096            System.err.println("Failed to bind.");
097            System.exit(e.getResult().getResultCode().intValue());
098            return;
099        } finally {
100            if (connection != null) {
101                connection.close();
102            }
103        }
104        // --- JCite ---
105    }
106
107    /**
108     * Constructor not used.
109     */
110    private SearchBind() {
111        // Not used
112    }
113}