1 /*
2 * The contents of this file are subject to the terms of the Common Development and
3 * Distribution License (the License). You may not use this file except in compliance with the
4 * License.
5 *
6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7 * specific language governing permission and limitations under the License.
8 *
9 * When distributing Covered Software, include this CDDL Header Notice in each file and include
10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11 * Header, with the fields enclosed by brackets [] replaced by your own identifying
12 * information: "Portions Copyright [year] [name of copyright owner]".
13 *
14 * Copyright 2012-2015 ForgeRock AS.
15 */
16
17 package org.forgerock.opendj.examples;
18
19 import java.io.Console;
20
21 import org.forgerock.opendj.ldap.Connection;
22 import org.forgerock.opendj.ldap.DN;
23 import org.forgerock.opendj.ldap.Filter;
24 import org.forgerock.opendj.ldap.LdapException;
25 import org.forgerock.opendj.ldap.LDAPConnectionFactory;
26 import org.forgerock.opendj.ldap.SearchScope;
27 import org.forgerock.opendj.ldap.responses.SearchResultEntry;
28
29 /**
30 * An interactive command-line client that performs a search and subsequent
31 * simple bind. The client prompts for email address and for a password, and
32 * then searches based on the email address, to bind as the user with the
33 * password. If successful, the client displays the common name from the user's
34 * entry.
35 * <ul>
36 * <li>host - host name of the directory server</li>
37 * <li>port - port number of the directory server</li>
38 * <li>base-dn - base DN for the search, e.g. dc=example,dc=com</li>
39 * </ul>
40 * All arguments are required.
41 */
42 public final class SearchBind {
43 /**
44 * Prompt for email and password, search and bind, then display message.
45 *
46 * @param args
47 * The command line arguments: host, port, base-dn.
48 */
49 public static void main(final String[] args) {
50 if (args.length != 3) {
51 System.err.println("Usage: host port base-dn");
52 System.err.println("For example: localhost 1389 dc=example,dc=com");
53 System.exit(1);
54 }
55 String host = args[0];
56 int port = Integer.parseInt(args[1]);
57 String baseDN = args[2];
58
59 // --- JCite ---
60 // Prompt for mail and password.
61 Console c = System.console();
62 if (c == null) {
63 System.err.println("No console.");
64 System.exit(1);
65 }
66
67 String mail = c.readLine("Email address: ");
68 char[] password = c.readPassword("Password: ");
69
70 // Search using mail address, and then bind with the DN and password.
71 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
72 Connection connection = null;
73 try {
74 connection = factory.getConnection();
75 SearchResultEntry entry =
76 connection.searchSingleEntry(baseDN,
77 SearchScope.WHOLE_SUBTREE,
78 Filter.equality("mail", mail).toString(),
79 "cn");
80 DN bindDN = entry.getName();
81 connection.bind(bindDN.toString(), password);
82
83 String cn = entry.getAttribute("cn").firstValueAsString();
84 System.out.println("Hello, " + cn + "!");
85 } catch (final LdapException e) {
86 System.err.println("Failed to bind.");
87 System.exit(e.getResult().getResultCode().intValue());
88 return;
89 } finally {
90 if (connection != null) {
91 connection.close();
92 }
93 }
94 // --- JCite ---
95 }
96
97 /**
98 * Constructor not used.
99 */
100 private SearchBind() {
101 // Not used
102 }
103 }