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