in sshd-core/src/main/java/org/apache/sshd/server/x11/DefaultX11ForwardSupport.java [69:174]
public synchronized String createDisplay(
boolean singleConnection, String authenticationProtocol, String authenticationCookie, int screen)
throws IOException {
boolean debugEnabled = log.isDebugEnabled();
if (isClosed()) {
throw new IllegalStateException("X11ForwardSupport is closed");
}
if (isClosing()) {
throw new IllegalStateException("X11ForwardSupport is closing");
}
// only support non windows systems
if (OsUtils.isWin32()) {
if (debugEnabled) {
log.debug("createDisplay(auth={}, cookie={}, screen={}) Windows O/S N/A",
authenticationProtocol, authenticationCookie, screen);
}
return null;
}
Session session = Objects.requireNonNull(service.getSession(), "No session");
if (acceptor == null) {
FactoryManager manager = Objects.requireNonNull(session.getFactoryManager(), "No factory manager");
IoServiceFactory factory = Objects.requireNonNull(manager.getIoServiceFactory(), "No I/O service factory");
acceptor = factory.createAcceptor(this);
}
int minDisplayNumber = CoreModuleProperties.X11_DISPLAY_OFFSET.getRequired(session);
int maxDisplayNumber = CoreModuleProperties.X11_MAX_DISPLAYS.getRequired(session);
int basePort = CoreModuleProperties.X11_BASE_PORT.getRequired(session);
String bindHost = CoreModuleProperties.X11_BIND_HOST.getRequired(session);
InetSocketAddress addr = null;
// try until bind successful or max is reached
for (int displayNumber = minDisplayNumber; displayNumber < maxDisplayNumber; displayNumber++) {
int port = basePort + displayNumber;
addr = new InetSocketAddress(bindHost, port);
try {
acceptor.bind(addr);
break;
} catch (BindException bindErr) {
if (debugEnabled) {
log.debug("createDisplay(auth={}, cookie={}, screen={}) failed ({}) to bind to address={}: {}",
authenticationProtocol, authenticationCookie, screen,
bindErr.getClass().getSimpleName(), addr, bindErr.getMessage());
}
addr = null;
}
}
if (addr == null) {
log.warn("createDisplay(auth={}, cookie={}, screen={})"
+ " failed to allocate internet-domain X11 display socket in range {}-{}",
authenticationProtocol, authenticationCookie, screen,
minDisplayNumber, maxDisplayNumber);
Collection<SocketAddress> boundAddresses = acceptor.getBoundAddresses();
if (GenericUtils.isEmpty(boundAddresses)) {
if (debugEnabled) {
log.debug("createDisplay(auth={}, cookie={}, screen={}) closing - no more bound addresses",
authenticationProtocol, authenticationCookie, screen);
}
close();
} else {
if (debugEnabled) {
log.debug("createDisplay(auth={}, cookie={}, screen={}) closing - remaining bound addresses: {}",
authenticationProtocol, authenticationCookie, screen, boundAddresses);
}
}
return null;
}
int port = addr.getPort();
int displayNumber = port - basePort;
String authDisplay = "unix:" + displayNumber + "." + screen;
try {
ProcessBuilder processBuilder = new ProcessBuilder(XAUTH_COMMAND, "remove", authDisplay);
Process p = processBuilder.start();
int result = p.waitFor();
if (debugEnabled) {
log.debug("createDisplay({}) {} remove result={}", authDisplay, XAUTH_COMMAND, result);
}
if (result == 0) {
processBuilder = new ProcessBuilder(
XAUTH_COMMAND, "add", authDisplay, authenticationProtocol, authenticationCookie);
p = processBuilder.start();
result = p.waitFor();
if (debugEnabled) {
log.debug("createDisplay({}) {} add result={}", authDisplay, XAUTH_COMMAND, result);
}
}
if (result != 0) {
throw new IllegalStateException("Bad " + XAUTH_COMMAND + " invocation result: " + result);
}
return bindHost + ":" + displayNumber + "." + screen;
} catch (Throwable e) {
warn("createDisplay({}) failed ({}) run xauth: {}",
authDisplay, e.getClass().getSimpleName(), e.getMessage(), e);
return null;
}
}