1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.forgerock.opendj.examples;
28
29 import java.security.GeneralSecurityException;
30
31 import javax.net.ssl.SSLContext;
32
33 import org.forgerock.opendj.ldap.Connection;
34 import org.forgerock.opendj.ldap.ErrorResultException;
35 import org.forgerock.opendj.ldap.LDAPConnectionFactory;
36 import org.forgerock.opendj.ldap.LDAPOptions;
37 import org.forgerock.opendj.ldap.ResultCode;
38 import org.forgerock.opendj.ldap.SSLContextBuilder;
39 import org.forgerock.opendj.ldap.TrustManagers;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public final class SimpleAuth {
56
57
58
59
60
61
62
63
64 public static void main(final String[] args) {
65 parseArgs(args);
66
67 if (useStartTLS) {
68 connectStartTLS();
69 } else if (useSSL) {
70 connectSSL();
71 } else {
72 connect();
73 }
74 }
75
76
77
78
79 private static void connect() {
80 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
81 Connection connection = null;
82
83 try {
84 connection = factory.getConnection();
85 connection.bind(bindDN, bindPassword.toCharArray());
86 System.out.println("Authenticated as " + bindDN + ".");
87 } catch (final ErrorResultException e) {
88 System.err.println(e.getMessage());
89 System.exit(e.getResult().getResultCode().intValue());
90 return;
91 } finally {
92 if (connection != null) {
93 connection.close();
94 }
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107
108 private static LDAPOptions getTrustAllOptions() throws GeneralSecurityException {
109 LDAPOptions lo = new LDAPOptions();
110 SSLContext sslContext =
111 new SSLContextBuilder().setTrustManager(TrustManagers.trustAll()).getSSLContext();
112 lo.setSSLContext(sslContext);
113 lo.setUseStartTLS(useStartTLS);
114 return lo;
115 }
116
117
118
119
120
121 private static void trustAllConnect() {
122 Connection connection = null;
123
124 try {
125 final LDAPConnectionFactory factory =
126 new LDAPConnectionFactory(host, port, getTrustAllOptions());
127 connection = factory.getConnection();
128 connection.bind(bindDN, bindPassword.toCharArray());
129 System.out.println("Authenticated as " + bindDN + ".");
130 } catch (final ErrorResultException e) {
131 System.err.println(e.getMessage());
132 System.exit(e.getResult().getResultCode().intValue());
133 return;
134 } catch (final GeneralSecurityException e) {
135 System.err.println(e.getMessage());
136 System.exit(ResultCode.CLIENT_SIDE_CONNECT_ERROR.intValue());
137 } finally {
138 if (connection != null) {
139 connection.close();
140 }
141 }
142 }
143
144
145
146
147 private static void connectStartTLS() {
148 trustAllConnect();
149 }
150
151
152
153
154 private static void connectSSL() {
155 trustAllConnect();
156 }
157
158 private static String host;
159 private static int port;
160 private static String bindDN;
161 private static String bindPassword;
162 private static boolean useStartTLS = false;
163 private static boolean useSSL = false;
164
165
166
167
168
169
170
171 private static void parseArgs(String[] args) {
172 if (args.length < 4 || args.length > 5) {
173 giveUp();
174 }
175
176 host = args[0];
177 port = Integer.parseInt(args[1]);
178 bindDN = args[2];
179 bindPassword = args[3];
180
181 if (args.length == 5) {
182 if (args[4].toLowerCase().equals("use-starttls")) {
183 useStartTLS = true;
184 useSSL = false;
185 } else if (args[4].toLowerCase().equals("use-ssl")) {
186 useStartTLS = false;
187 useSSL = true;
188 } else {
189 giveUp();
190 }
191 }
192 }
193
194 private static void giveUp() {
195 printUsage();
196 System.exit(1);
197 }
198
199 private static void printUsage() {
200 System.err.println("Usage: host port bind-dn bind-password [ use-starttls | use-ssl ]");
201 System.err.println("\thost, port, bind-dn, and bind-password arguments are required.");
202 System.err.println("\tuse-starttls and use-ssl are optional and mutually exclusive.");
203 }
204
205 private SimpleAuth() {
206
207 }
208 }