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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static java.lang.System.Logger.Level.INFO;
/*
* A bare-bones (testing aid) server for LDAP scenarios.
*
* Override the following methods to provide customized behavior
*
* * beforeAcceptingConnections
* * beforeConnectionHandled
* * handleRequest (or handleRequestEx)
*
* Instances of this class are safe for use by multiple threads.
*/
public class BaseLdapServer implements Closeable {
private static final System.Logger logger = System.getLogger("BaseLdapServer");
private final Thread acceptingThread = new Thread(this::acceptConnections);
private final ServerSocket serverSocket;
private final List<Socket> socketList = new ArrayList<>();
private final ExecutorService connectionsPool;
private final Object lock = new Object();
/*
* 3-valued state to detect restarts and other programming errors.
*/
private State state = State.NEW;
private enum State {
NEW,
STARTED,
STOPPED
}
public BaseLdapServer() throws IOException {
this(new ServerSocket(0, 0, InetAddress.getLoopbackAddress()));
}
public BaseLdapServer(ServerSocket serverSocket) {
this.serverSocket = Objects.requireNonNull(serverSocket);
this.connectionsPool = Executors.newCachedThreadPool();
}
private void acceptConnections() {
logger().log(INFO, "Server is accepting connections at port {0}",
getPort());
try {
beforeAcceptingConnections();
while (isRunning()) {
Socket socket = serverSocket.accept();
logger().log(INFO, "Accepted new connection at {0}", socket);
synchronized (lock) {
// Recheck if the server is still running
// as someone has to close the `socket`
if (isRunning()) {
socketList.add(socket);
} else {
closeSilently(socket);
}
}
connectionsPool.submit(() -> handleConnection(socket));
}
} catch (Throwable t) {
if (isRunning()) {
throw new RuntimeException(
"Unexpected exception while accepting connections", t);
}
} finally {
logger().log(INFO, "Server stopped accepting connections at port {0}",
getPort());
}
}
/*
* Called once immediately preceding the server accepting connections.
*
* Override to customize the behavior.
*/
protected void beforeAcceptingConnections() { }
/*
* A "Template Method" describing how a connection (represented by a socket)
* is handled.
*
* The socket is closed immediately before the method returns (normally or
* abruptly).
*/
private void handleConnection(Socket socket) {
// No need to close socket's streams separately, they will be closed
// automatically when `socket.close()` is called
beforeConnectionHandled(socket);
ConnWrapper connWrapper = new ConnWrapper(socket);
try (socket) {
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
byte[] inBuffer = new byte[1024];
int count;
byte[] request;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int msgLen = -1;
// As inBuffer.length > 0, at least 1 byte is read
while ((count = in.read(inBuffer)) > 0) {
buffer.write(inBuffer, 0, count);
if (msgLen <= 0) {
msgLen = LdapMessage.getMessageLength(buffer.toByteArray());
}
if (msgLen > 0 && buffer.size() >= msgLen) {
if (buffer.size() > msgLen) {
byte[] tmpBuffer = buffer.toByteArray();
request = Arrays.copyOf(tmpBuffer, msgLen);
buffer.reset();
buffer.write(tmpBuffer, msgLen, tmpBuffer.length - msgLen);
} else {
request = buffer.toByteArray();
buffer.reset();
}
msgLen = -1;
} else {
logger.log(INFO, "Request message incomplete, " +
"bytes received {0}, expected {1}", buffer.size(), msgLen);
continue;
}
handleRequestEx(socket, new LdapMessage(request), out, connWrapper);
if (connWrapper.updateRequired()) {
var wrapper = connWrapper.getWrapper();
in = wrapper.getInputStream();
out = wrapper.getOutputStream();
connWrapper.clearFlag();
}
}
} catch (Throwable t) {
if (!isRunning()) {
logger.log(INFO, "Connection Handler exit {0}", t.getMessage());
} else {
handleSocketException(socket, t);
}
}
if (connWrapper.getWrapper() != null) {
closeSilently(connWrapper.getWrapper());
}
}
/*
* Called first thing in `handleConnection()`.
*
* Override to customize the behavior.
*/
protected void beforeConnectionHandled(Socket socket) { /* empty */ }
/*
* Called to handle exceptions observed on an established client connection.
*
* By default, an exception stack trace is printed.
*/
protected void handleSocketException(Socket socket, Throwable exception) {
exception.printStackTrace();
}
/*
* Called after an LDAP request has been read in `handleConnection()`.
*
* Override to customize the behavior.
*/
protected void handleRequest(Socket socket,
LdapMessage request,
OutputStream out)
throws IOException
{
logger().log(INFO, "Discarding message {0} from {1}. "
+ "Override {2}.handleRequest to change this behavior.",
request, socket, getClass().getName());
}
/*
* Called after an LDAP request has been read in `handleConnection()`.
*
* Override to customize the behavior if you want to handle starttls
* extended op, otherwise override handleRequest method instead.
*
* This is extended handleRequest method which provide possibility to
* wrap current socket connection, that's necessary to handle starttls
* extended request, here is sample code about how to wrap current socket
*
* switch (request.getOperation()) {
* ......
* case EXTENDED_REQUEST:
* if (new String(request.getMessage()).endsWith(STARTTLS_REQ_OID)) {
* out.write(STARTTLS_RESPONSE);
* SSLSocket sslSocket = (SSLSocket) sslSocketFactory
* .createSocket(socket, null, socket.getLocalPort(),
* false);
* sslSocket.setUseClientMode(false);
* connWrapper.setWrapper(sslSocket);
* }
* break;
* ......
* }
*/
protected void handleRequestEx(Socket socket,
LdapMessage request,
OutputStream out,
ConnWrapper connWrapper)
throws IOException {
// by default, just call handleRequest to keep compatibility
handleRequest(socket, request, out);
}
/*
* To be used by subclasses.
*/
protected final System.Logger logger() {
return logger;
}
/*
* Starts this server. May be called only once.
*/
public BaseLdapServer start() {
synchronized (lock) {
if (state != State.NEW) {
throw new IllegalStateException(state.toString());
}
state = State.STARTED;
logger().log(INFO, "Starting server at port {0}", getPort());
acceptingThread.start();
return this;
}
}
/*
* Stops this server.
*
* May be called at any time, even before a call to `start()`. In the latter
* case the subsequent call to `start()` will throw an exception. Repeated
* calls to this method have no effect.
*
* Stops accepting new connections, interrupts the threads serving already
* accepted connections and closes all the sockets.
*/
@Override
public void close() {
synchronized (lock) {
if (state == State.STOPPED) {
return;
}
state = State.STOPPED;
logger().log(INFO, "Stopping server at port {0}", getPort());
acceptingThread.interrupt();
closeSilently(serverSocket);
// It's important to signal an interruption so that overridden
// methods have a chance to return if they use
// interruption-sensitive blocking operations. However, blocked I/O
// operations on the socket will NOT react on that, hence the socket
// also has to be closed to propagate shutting down.
connectionsPool.shutdownNow();
socketList.forEach(BaseLdapServer.this::closeSilently);
}
}
/**
* Returns the local port this server is listening at.
*
* This method can be called at any time.
*
* @return the port this server is listening at
*/
public int getPort() {
return serverSocket.getLocalPort();
}
/**
* Returns the address this server is listening at.
*
* This method can be called at any time.
*
* @return the address
*/
public InetAddress getInetAddress() {
return serverSocket.getInetAddress();
}
/*
* Returns a flag to indicate whether this server is running or not.
*
* @return {@code true} if this server is running, {@code false} otherwise.
*/
public boolean isRunning() {
synchronized (lock) {
return state == State.STARTED;
}
}
/*
* To be used by subclasses.
*/
protected final void closeSilently(Closeable resource) {
try {
resource.close();
} catch (IOException ignored) { }
}
/*
* To be used for handling starttls extended request
*/
protected class ConnWrapper {
private Socket original;
private Socket wrapper;
private boolean flag = false;
public ConnWrapper(Socket socket) {
original = socket;
}
public Socket getWrapper() {
return wrapper;
}
public void setWrapper(Socket wrapper) {
if (wrapper != null && wrapper != original) {
this.wrapper = wrapper;
flag = true;
}
}
public boolean updateRequired() {
return flag;
}
public void clearFlag() {
flag = false;
}
}
}
|