Repository: mina-sshd Updated Branches: refs/heads/master 0f547d822 -> 421faf51a
[SSHD-434] Use a standard helper API for accessing FactoryManager properties Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/421faf51 Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/421faf51 Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/421faf51 Branch: refs/heads/master Commit: 421faf51ae8c69029c482f3bb38b2042ac4db9cb Parents: 0f547d8 Author: Guillaume Nodet <[email protected]> Authored: Mon Mar 23 09:46:57 2015 +0100 Committer: Guillaume Nodet <[email protected]> Committed: Mon Mar 23 09:46:57 2015 +0100 ---------------------------------------------------------------------- .../sshd/agent/local/ProxyAgentFactory.java | 12 +++-- .../sshd/agent/unix/ChannelAgentForwarding.java | 4 +- .../sshd/agent/unix/UnixAgentFactory.java | 11 +++-- .../client/session/ClientConnectionService.java | 41 +++++++--------- .../session/ClientUserAuthServiceNew.java | 30 +++++------- .../sshd/common/AbstractFactoryManager.java | 21 +++----- .../org/apache/sshd/common/SshException.java | 2 + .../sshd/common/channel/AbstractChannel.java | 11 +---- .../common/io/AbstractIoServiceFactory.java | 16 +++--- .../sshd/common/io/mina/MinaAcceptor.java | 17 +++---- .../apache/sshd/common/io/mina/MinaService.java | 7 ++- .../sshd/common/io/nio2/Nio2Acceptor.java | 10 ++-- .../apache/sshd/common/io/nio2/Nio2Service.java | 7 +-- .../sshd/common/session/AbstractSession.java | 1 + .../sshd/server/channel/ChannelSession.java | 23 ++++----- .../java/org/apache/sshd/server/kex/DHGEX.java | 51 ++++++++++++++------ .../sshd/server/session/ServerSession.java | 13 +++-- .../server/session/ServerUserAuthService.java | 48 +++++++++--------- 18 files changed, 170 insertions(+), 155 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/agent/local/ProxyAgentFactory.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/local/ProxyAgentFactory.java b/sshd-core/src/main/java/org/apache/sshd/agent/local/ProxyAgentFactory.java index fd918c0..197d04d 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/local/ProxyAgentFactory.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/local/ProxyAgentFactory.java @@ -27,9 +27,11 @@ import org.apache.sshd.agent.SshAgentFactory; import org.apache.sshd.agent.SshAgentServer; import org.apache.sshd.common.Channel; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.Session; import org.apache.sshd.common.session.ConnectionService; +import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.server.session.ServerSession; public class ProxyAgentFactory implements SshAgentFactory { @@ -41,14 +43,16 @@ public class ProxyAgentFactory implements SshAgentFactory { } public SshAgent createClient(FactoryManager manager) throws IOException { - String proxyId = manager.getProperties().get(SshAgent.SSH_AUTHSOCKET_ENV_NAME); - if (proxyId == null) { + String proxyId = FactoryManagerUtils.getString(manager, SshAgent.SSH_AUTHSOCKET_ENV_NAME); + if (GenericUtils.isEmpty(proxyId)) { throw new IllegalStateException("No " + SshAgent.SSH_AUTHSOCKET_ENV_NAME + " environment variable set"); } + AgentServerProxy proxy = proxies.get(proxyId); if (proxy == null) { - throw new IllegalStateException("No ssh agent found"); + throw new IllegalStateException("No ssh agent found for ID=" + proxyId); } + return proxy.createClient(); } @@ -57,12 +61,14 @@ public class ProxyAgentFactory implements SshAgentFactory { if (!(session instanceof ServerSession)) { throw new IllegalStateException("The session used to create an agent server proxy must be a server session"); } + final AgentServerProxy proxy = new AgentServerProxy(service); proxies.put(proxy.getId(), proxy); return new SshAgentServer() { public String getId() { return proxy.getId(); } + public void close() { proxies.remove(proxy.getId()); proxy.close(); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java index 572c4d0..4f6516e 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java @@ -25,11 +25,11 @@ import org.apache.sshd.agent.SshAgent; import org.apache.sshd.client.future.DefaultOpenFuture; import org.apache.sshd.client.future.OpenFuture; import org.apache.sshd.common.Channel; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.channel.ChannelOutputStream; import org.apache.sshd.common.future.CloseFuture; -import org.apache.sshd.common.future.SshFuture; import org.apache.sshd.common.future.SshFutureListener; import org.apache.sshd.common.util.Buffer; import org.apache.sshd.server.channel.AbstractServerChannel; @@ -67,7 +67,7 @@ public class ChannelAgentForwarding extends AbstractServerChannel { final OpenFuture f = new DefaultOpenFuture(this); try { out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA); - authSocket = session.getFactoryManager().getProperties().get(SshAgent.SSH_AUTHSOCKET_ENV_NAME); + authSocket = FactoryManagerUtils.getString(session, SshAgent.SSH_AUTHSOCKET_ENV_NAME); pool = Pool.create(AprLibrary.getInstance().getRootPool()); handle = Local.create(authSocket, pool); int result = Local.connect(handle, 0); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/agent/unix/UnixAgentFactory.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/UnixAgentFactory.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/UnixAgentFactory.java index 87797d3..be5b8a7 100644 --- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/UnixAgentFactory.java +++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/UnixAgentFactory.java @@ -25,9 +25,12 @@ import org.apache.sshd.agent.SshAgentFactory; import org.apache.sshd.agent.SshAgentServer; import org.apache.sshd.common.Channel; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.Session; +import org.apache.sshd.common.SshException; import org.apache.sshd.common.session.ConnectionService; +import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.server.session.ServerSession; public class UnixAgentFactory implements SshAgentFactory { @@ -37,9 +40,11 @@ public class UnixAgentFactory implements SshAgentFactory { } public SshAgent createClient(FactoryManager manager) throws IOException { - String authSocket = manager.getProperties().get(SshAgent.SSH_AUTHSOCKET_ENV_NAME); - SshAgent agent = new AgentClient(authSocket); - return agent; + String authSocket = FactoryManagerUtils.getString(manager, SshAgent.SSH_AUTHSOCKET_ENV_NAME); + if (GenericUtils.isEmpty(authSocket)) { + throw new SshException("No " + SshAgent.SSH_AUTHSOCKET_ENV_NAME + " value"); + } + return new AgentClient(authSocket); } public SshAgentServer createServer(ConnectionService service) throws IOException { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java index 44bfdeb..7c03fb8 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java @@ -19,21 +19,19 @@ package org.apache.sshd.client.session; import java.io.IOException; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.apache.sshd.client.ClientFactoryManager; -import org.apache.sshd.client.future.OpenFuture; -import org.apache.sshd.common.Channel; -import org.apache.sshd.common.NamedFactory; +import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.Service; import org.apache.sshd.common.ServiceFactory; import org.apache.sshd.common.Session; import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.SshException; -import org.apache.sshd.common.future.SshFutureListener; import org.apache.sshd.common.session.AbstractConnectionService; import org.apache.sshd.common.util.Buffer; -import org.apache.sshd.server.channel.OpenChannelException; /** * Client side <code>ssh-connection</code> service. @@ -42,6 +40,9 @@ import org.apache.sshd.server.channel.OpenChannelException; */ public class ClientConnectionService extends AbstractConnectionService { + public static final String DEFAULT_KEEP_ALIVE_HEARTBEAT_STRING = "[email protected]"; + public static final long DEFAULT_HEARTBEAT_INTERVAL = 0L; + public static class Factory implements ServiceFactory { public String getName() { @@ -69,33 +70,28 @@ public class ClientConnectionService extends AbstractConnectionService { } protected void startHeartBeat() { - String intervalStr = session.getFactoryManager().getProperties().get(ClientFactoryManager.HEARTBEAT_INTERVAL); - try { - int interval = intervalStr != null ? Integer.parseInt(intervalStr) : 0; - if (interval > 0) { - session.getFactoryManager().getScheduledExecutorService().scheduleAtFixedRate(new Runnable() { - public void run() { - sendHeartBeat(); - } - }, interval, interval, TimeUnit.MILLISECONDS); - } - } catch (NumberFormatException e) { - log.warn("Ignoring bad heartbeat interval: {}", intervalStr); + long interval = FactoryManagerUtils.getLongProperty(session, ClientFactoryManager.HEARTBEAT_INTERVAL, DEFAULT_HEARTBEAT_INTERVAL); + if (interval > 0L) { + FactoryManager manager = session.getFactoryManager(); + ScheduledExecutorService service = manager.getScheduledExecutorService(); + service.scheduleAtFixedRate(new Runnable() { + public void run() { + sendHeartBeat(); + } + }, interval, interval, TimeUnit.MILLISECONDS); + log.debug("startHeartbeat - started at interval={}", interval); } } protected void sendHeartBeat() { + String request = FactoryManagerUtils.getStringProperty(session, ClientFactoryManager.HEARTBEAT_REQUEST, DEFAULT_KEEP_ALIVE_HEARTBEAT_STRING); try { Buffer buf = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST); - String request = session.getFactoryManager().getProperties().get(ClientFactoryManager.HEARTBEAT_REQUEST); - if (request == null) { - request = "[email protected]"; - } buf.putString(request); buf.putBoolean(false); session.writePacket(buf); } catch (IOException e) { - log.info("Error sending keepalive message", e); + log.info("Error sending keepalive message=" + request, e); } } @@ -108,5 +104,4 @@ public class ClientConnectionService extends AbstractConnectionService { public String createX11Display(boolean singleConnection, String authenticationProtocol, String authenticationCookie, int screen) throws IOException { throw new IllegalStateException("Server side operation"); } - } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java index 5616c0d..7e1f310 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java @@ -26,24 +26,17 @@ import java.util.List; import org.apache.sshd.client.ClientFactoryManager; import org.apache.sshd.client.UserAuth; import org.apache.sshd.client.UserInteraction; -import org.apache.sshd.client.auth.UserAuthKeyboardInteractive; -import org.apache.sshd.client.auth.UserAuthPassword; -import org.apache.sshd.client.auth.UserAuthPublicKey; import org.apache.sshd.client.future.AuthFuture; import org.apache.sshd.client.future.DefaultAuthFuture; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.Service; import org.apache.sshd.common.Session; import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.SshException; -import org.apache.sshd.common.future.CloseFuture; -import org.apache.sshd.common.future.DefaultCloseFuture; import org.apache.sshd.common.util.Buffer; import org.apache.sshd.common.util.CloseableUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static org.apache.sshd.common.util.KeyUtils.getKeyType; +import org.apache.sshd.common.util.GenericUtils; /** * Client side <code>ssh-auth</code> service. @@ -63,10 +56,10 @@ public class ClientUserAuthServiceNew extends CloseableUtils.AbstractCloseable i private List<Object> identities; private String service; - List<NamedFactory<UserAuth>> authFactories; - List<String> clientMethods; - List<String> serverMethods; - UserAuth userAuth; + private List<NamedFactory<UserAuth>> authFactories; + private List<String> clientMethods; + private List<String> serverMethods; + private UserAuth userAuth; public ClientUserAuthServiceNew(Session s) { if (!(s instanceof ClientSessionImpl)) { @@ -76,11 +69,14 @@ public class ClientUserAuthServiceNew extends CloseableUtils.AbstractCloseable i authFuture = new DefaultAuthFuture(session.getLock()); authFactories = session.getFactoryManager().getUserAuthFactories(); clientMethods = new ArrayList<String>(); - String prefs = session.getFactoryManager().getProperties().get(ClientFactoryManager.PREFERRED_AUTHS); - if (prefs != null) { + + String prefs = FactoryManagerUtils.getString(session, ClientFactoryManager.PREFERRED_AUTHS); + if (!GenericUtils.isEmpty(prefs)) { for (String pref : prefs.split(",")) { if (NamedFactory.Utils.get(authFactories, pref) != null) { clientMethods.add(pref); + } else { + log.debug("Skip unknown prefered authentication method: {}", pref); } } } else { @@ -121,7 +117,7 @@ public class ClientUserAuthServiceNew extends CloseableUtils.AbstractCloseable i } else if (cmd == SshConstants.SSH_MSG_USERAUTH_BANNER) { String welcome = buffer.getString(); String lang = buffer.getString(); - log.debug("Welcome banner: {}", welcome); + log.debug("Welcome banner(lang={}): {}", lang, welcome); UserInteraction ui = session.getFactoryManager().getUserInteraction(); if (ui != null) { ui.welcome(welcome); @@ -132,7 +128,7 @@ public class ClientUserAuthServiceNew extends CloseableUtils.AbstractCloseable i } } - int currentMethod; + private int currentMethod; /** * execute one step in user authentication. http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java b/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java index f8404a6..5ae8d91 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java @@ -158,13 +158,10 @@ public abstract class AbstractFactoryManager extends CloseableUtils.AbstractInne protected void loadVersion() { this.version = "SSHD-UNKNOWN"; try { - InputStream input = getClass().getClassLoader().getResourceAsStream("org/apache/sshd/sshd-version.properties"); - try { + try (InputStream input = getClass().getClassLoader().getResourceAsStream("org/apache/sshd/sshd-version.properties")) { Properties props = new Properties(); props.load(input); this.version = props.getProperty("version").toUpperCase(); - } finally { - input.close(); } } catch (Exception e) { log.warn("Unable to load version from resources. Missing org/apache/sshd/sshd-version.properties ?", e); @@ -180,21 +177,19 @@ public abstract class AbstractFactoryManager extends CloseableUtils.AbstractInne } public int getNioWorkers() { - String nioWorkers = getProperties().get(NIO_WORKERS); - if (nioWorkers != null && nioWorkers.length() > 0) { - int nb = Integer.parseInt(nioWorkers); - if (nb > 0) { - return nb; - } + int nb=FactoryManagerUtils.getIntProperty(this, NIO_WORKERS, DEFAULT_NIO_WORKERS); + if (nb > 0) { + return nb; + } else { // it may have been configured to a negative value + return DEFAULT_NIO_WORKERS; } - return DEFAULT_NIO_WORKERS; } public void setNioWorkers(int nioWorkers) { if (nioWorkers > 0) { - getProperties().put(NIO_WORKERS, Integer.toString(nioWorkers)); + FactoryManagerUtils.updateProperty(this, NIO_WORKERS, nioWorkers); } else { - getProperties().remove(NIO_WORKERS); + FactoryManagerUtils.updateProperty(this, NIO_WORKERS, null); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/SshException.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/SshException.java b/sshd-core/src/main/java/org/apache/sshd/common/SshException.java index 170d75b..5883560 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/SshException.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/SshException.java @@ -27,6 +27,8 @@ import java.io.IOException; */ public class SshException extends IOException { + private static final long serialVersionUID = -7349477687125144606L; + private final int disconnectCode; public SshException() { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java index 1d04b17..fd2aa0f 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java @@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicReference; import org.apache.sshd.common.Channel; import org.apache.sshd.common.Closeable; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.RequestHandler; import org.apache.sshd.common.Session; import org.apache.sshd.common.SshConstants; @@ -174,15 +175,7 @@ public abstract class AbstractChannel extends CloseableUtils.AbstractInnerClosea Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_CLOSE); buffer.putInt(recipient); try { - long timeout = DEFAULT_CHANNEL_CLOSE_TIMEOUT; - String val = getSession().getFactoryManager().getProperties().get(FactoryManager.CHANNEL_CLOSE_TIMEOUT); - if (val != null) { - try { - timeout = Long.parseLong(val); - } catch (NumberFormatException e) { - // Ignore - } - } + long timeout = FactoryManagerUtils.getLongProperty(getSession(), FactoryManager.CHANNEL_CLOSE_TIMEOUT, DEFAULT_CHANNEL_CLOSE_TIMEOUT); session.writePacket(buffer, timeout, TimeUnit.MILLISECONDS).addListener(new SshFutureListener<IoWriteFuture>() { public void operationComplete(IoWriteFuture future) { if (future.isWritten()) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoServiceFactory.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoServiceFactory.java b/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoServiceFactory.java index f244cb4..6682884 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoServiceFactory.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoServiceFactory.java @@ -19,11 +19,11 @@ package org.apache.sshd.common.io; -import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.util.CloseableUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,15 +78,11 @@ public abstract class AbstractIoServiceFactory extends CloseableUtils.AbstractCl } public static int getNioWorkers(FactoryManager manager) { - Map<String, String> properties = manager.getProperties(); - String nioWorkers = properties.get(FactoryManager.NIO_WORKERS); - if ((nioWorkers != null) && (nioWorkers.length() > 0)) { - int nb = Integer.parseInt(nioWorkers); - if (nb > 0) { - return nb; - } + int nb = FactoryManagerUtils.getIntProperty(manager, FactoryManager.NIO_WORKERS, FactoryManager.DEFAULT_NIO_WORKERS); + if (nb > 0) { + return nb; + } else { + return FactoryManager.DEFAULT_NIO_WORKERS; } - - return FactoryManager.DEFAULT_NIO_WORKERS; } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java index 7a20d78..2e92590 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java @@ -30,27 +30,24 @@ import org.apache.mina.core.service.IoService; import org.apache.mina.transport.socket.nio.NioSession; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; /** */ public class MinaAcceptor extends MinaService implements org.apache.sshd.common.io.IoAcceptor, IoHandler { + public static final int DEFAULT_BACKLOG=0; + public static final boolean DEFAULT_REUSE_ADDRESS=true; protected volatile IoAcceptor acceptor; // Acceptor - protected int backlog = 0; - protected boolean reuseAddress = true; + protected int backlog = DEFAULT_BACKLOG; + protected boolean reuseAddress = DEFAULT_REUSE_ADDRESS; public MinaAcceptor(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) { super(manager, handler, ioProcessor); - String valStr = manager.getProperties().get(FactoryManager.SOCKET_BACKLOG); - if (valStr != null) { - backlog = Integer.parseInt(valStr); - } - valStr = manager.getProperties().get(FactoryManager.SOCKET_REUSEADDR); - if (valStr != null) { - reuseAddress = Boolean.parseBoolean(valStr); - } + backlog = FactoryManagerUtils.getIntProperty(manager, FactoryManager.SOCKET_BACKLOG, DEFAULT_BACKLOG); + reuseAddress = FactoryManagerUtils.getBooleanProperty(manager, FactoryManager.SOCKET_REUSEADDR, DEFAULT_REUSE_ADDRESS); } protected IoAcceptor createAcceptor() { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java index ee795b3..5eefd24 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java @@ -32,6 +32,7 @@ import org.apache.mina.transport.socket.SocketSessionConfig; import org.apache.mina.transport.socket.nio.NioSession; import org.apache.sshd.common.Closeable; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.util.CloseableUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -141,13 +142,11 @@ public abstract class MinaService extends CloseableUtils.AbstractCloseable imple } protected Integer getInteger(String property) { - String strVal = manager.getProperties().get(property); - return (strVal != null) ? Integer.parseInt(strVal) : null; + return FactoryManagerUtils.getInteger(manager, property); } protected Boolean getBoolean(String property) { - String strVal = manager.getProperties().get(property); - return (strVal != null) ? Boolean.parseBoolean(strVal) : null; + return FactoryManagerUtils.getBoolean(manager, property); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java index 9bba704..5d1a0ae 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java @@ -32,6 +32,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.future.CloseFuture; import org.apache.sshd.common.io.IoAcceptor; import org.apache.sshd.common.io.IoHandler; @@ -39,18 +40,15 @@ import org.apache.sshd.common.io.IoHandler; /** */ public class Nio2Acceptor extends Nio2Service implements IoAcceptor { + public static final int DEFAULT_BACKLOG=0; private final Map<SocketAddress, AsynchronousServerSocketChannel> channels; - private int backlog = 0; + private int backlog = DEFAULT_BACKLOG; public Nio2Acceptor(FactoryManager manager, IoHandler handler, AsynchronousChannelGroup group) { super(manager, handler, group); channels = new ConcurrentHashMap<SocketAddress, AsynchronousServerSocketChannel>(); - - String valStr = manager.getProperties().get(FactoryManager.SOCKET_BACKLOG); - if (valStr != null) { - backlog = Integer.parseInt(valStr); - } + backlog = FactoryManagerUtils.getIntProperty(manager, FactoryManager.SOCKET_BACKLOG, DEFAULT_BACKLOG); } public void bind(Collection<? extends SocketAddress> addresses) throws IOException { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java index 4bdb8f7..419e234 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java @@ -29,10 +29,12 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.sshd.common.Closeable; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.io.IoHandler; import org.apache.sshd.common.io.IoService; import org.apache.sshd.common.io.IoSession; import org.apache.sshd.common.util.CloseableUtils; +import org.apache.sshd.common.util.GenericUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,9 +79,9 @@ public abstract class Nio2Service extends CloseableUtils.AbstractInnerCloseable } protected <T> void setOption(NetworkChannel socket, String property, SocketOption<T> option, T defaultValue) throws IOException { - String valStr = manager.getProperties().get(property); + String valStr = FactoryManagerUtils.getString(manager, property); T val = defaultValue; - if (valStr != null) { + if (!GenericUtils.isEmpty(valStr)) { Class<T> type = option.type(); if (type == Integer.class) { val = type.cast(Integer.parseInt(valStr)); @@ -97,5 +99,4 @@ public abstract class Nio2Service extends CloseableUtils.AbstractInnerCloseable } } } - } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java index 6c342a9..7d44beb 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java @@ -800,6 +800,7 @@ public abstract class AbstractSession extends CloseableUtils.AbstractInnerClosea * @param ident our identification to send */ protected void sendIdentification(String ident) { + log.debug("Send identification: {}", ident); byte[] data = (ident + "\r\n").getBytes(); ioSession.write(new Buffer(data)); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java b/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java index 658c050..a9d98a0 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java @@ -29,16 +29,20 @@ import java.util.Set; import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.apache.sshd.agent.SshAgent; import org.apache.sshd.agent.SshAgentFactory; import org.apache.sshd.common.Channel; import org.apache.sshd.common.Closeable; +import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.ForwardingFilter; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.PtyMode; import org.apache.sshd.common.RequestHandler; +import org.apache.sshd.common.Session; import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.channel.ChannelAsyncOutputStream; import org.apache.sshd.common.channel.ChannelOutputStream; @@ -172,7 +176,7 @@ public class ChannelSession extends AbstractServerChannel { synchronized (listeners) { ls = listeners.get(signal); if (ls == null) { - ls = new CopyOnWriteArraySet<SignalListener>(); + ls = new CopyOnWriteArraySet<>(); listeners.put(signal, ls); } } @@ -232,17 +236,14 @@ public class ChannelSession extends AbstractServerChannel { commandExitFuture.setClosed(); } }; - long timeout = DEFAULT_COMMAND_EXIT_TIMEOUT; - String val = getSession().getFactoryManager().getProperties().get(ServerFactoryManager.COMMAND_EXIT_TIMEOUT); - if (val != null) { - try { - timeout = Long.parseLong(val); - } catch (NumberFormatException e) { - // Ignore - } + + FactoryManager manager = getSession().getFactoryManager(); + long timeout = FactoryManagerUtils.getLongProperty(manager, ServerFactoryManager.COMMAND_EXIT_TIMEOUT, DEFAULT_COMMAND_EXIT_TIMEOUT); + if (log.isDebugEnabled()) { + log.debug("Wait {} ms for shell to exit cleanly", timeout); } - log.debug("Wait {} ms for shell to exit cleanly", timeout); - getSession().getFactoryManager().getScheduledExecutorService().schedule(task, timeout, TimeUnit.MILLISECONDS); + + manager.getScheduledExecutorService().schedule(task, timeout, TimeUnit.MILLISECONDS); commandExitFuture.addListener(new SshFutureListener<CloseFuture>() { public void operationComplete(CloseFuture future) { task.cancel(); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java b/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java index 007b3a8..ef777d6 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java @@ -18,6 +18,7 @@ */ package org.apache.sshd.server.kex; +import java.io.FileNotFoundException; import java.io.IOException; import java.math.BigInteger; import java.net.URL; @@ -27,6 +28,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.sshd.common.Digest; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.KeyExchange; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.Random; @@ -39,6 +41,7 @@ import org.apache.sshd.common.kex.DHGroupData; import org.apache.sshd.common.session.AbstractSession; import org.apache.sshd.common.util.Buffer; import org.apache.sshd.common.util.BufferUtils; +import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.SecurityUtils; import org.apache.sshd.server.ServerFactoryManager; import org.apache.sshd.server.session.ServerSession; @@ -221,21 +224,7 @@ public class DHGEX implements KeyExchange { } private DH chooseDH(int min, int prf, int max) throws Exception { - List<Moduli.DhGroup> groups = null; - URL moduli; - String moduliStr = session.getFactoryManager().getProperties().get(ServerFactoryManager.MODULI_URL); - if (moduliStr != null) { - try { - moduli = new URL(moduliStr); - groups = Moduli.parseModuli(moduli); - } catch (IOException e) { - log.warn("Error loading external moduli", e); - } - } - if (groups == null) { - moduli = getClass().getResource("/org/apache/sshd/moduli"); - groups = Moduli.parseModuli(moduli); - } + List<Moduli.DhGroup> groups = loadModuliGroups(); min = Math.max(min, 1024); prf = Math.max(prf, 1024); @@ -267,6 +256,38 @@ public class DHGEX implements KeyExchange { return getDH(group.p, group.g); } + protected List<Moduli.DhGroup> loadModuliGroups() throws IOException { + List<Moduli.DhGroup> groups = null; + URL moduli; + String moduliStr = FactoryManagerUtils.getString(session, ServerFactoryManager.MODULI_URL); + if (!GenericUtils.isEmpty(moduliStr)) { + try { + moduli = new URL(moduliStr); + groups = Moduli.parseModuli(moduli); + } catch (IOException e) { // OK - use internal moduli + log.warn("Error (" + e.getClass().getSimpleName() + ") loading external moduli from " + moduliStr + ": " + e.getMessage()); + } + } + + if (groups == null) { + moduliStr = "/org/apache/sshd/moduli"; + try { + if ((moduli = getClass().getResource(moduliStr)) == null) { + throw new FileNotFoundException("Missing internal moduli file"); + } + + moduliStr = moduli.toExternalForm(); + groups = Moduli.parseModuli(moduli); + } catch (IOException e) { + log.warn("Error (" + e.getClass().getSimpleName() + ") loading internal moduli from " + moduliStr + ": " + e.getMessage()); + throw e; // this time we MUST throw the exception + } + } + + log.debug("Loaded moduli groups from {}", moduliStr); + return groups; + } + protected DH getDH(BigInteger p, BigInteger g) throws Exception { DH dh = new DH(new SHA1.Factory()); dh.setP(p); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java index 4799c69..fa87cb4 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java @@ -23,6 +23,7 @@ import java.security.KeyPair; import java.util.List; import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.KeyPairProvider; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.ServiceFactory; @@ -33,6 +34,7 @@ import org.apache.sshd.common.io.IoSession; import org.apache.sshd.common.io.IoWriteFuture; import org.apache.sshd.common.session.AbstractSession; import org.apache.sshd.common.util.Buffer; +import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.server.ServerFactoryManager; /** @@ -42,6 +44,7 @@ import org.apache.sshd.server.ServerFactoryManager; * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ public class ServerSession extends AbstractSession { + public static final String DEFAULT_SSH_VERSION_PREFIX="SSH-2.0-"; protected static final long MAX_PACKETS = (1l << 31); @@ -89,10 +92,12 @@ public class ServerSession extends AbstractSession { } private void sendServerIdentification() { - if (getFactoryManager().getProperties() != null && getFactoryManager().getProperties().get(ServerFactoryManager.SERVER_IDENTIFICATION) != null) { - serverVersion = "SSH-2.0-" + getFactoryManager().getProperties().get(ServerFactoryManager.SERVER_IDENTIFICATION); + FactoryManager manager = getFactoryManager(); + String ident = FactoryManagerUtils.getString(manager, ServerFactoryManager.SERVER_IDENTIFICATION); + if (GenericUtils.isEmpty(ident)) { + serverVersion = DEFAULT_SSH_VERSION_PREFIX + manager.getVersion(); } else { - serverVersion = "SSH-2.0-" + getFactoryManager().getVersion(); + serverVersion = DEFAULT_SSH_VERSION_PREFIX + ident; } sendIdentification(serverVersion); } @@ -155,7 +160,7 @@ public class ServerSession extends AbstractSession { return false; } log.debug("Client version string: {}", clientVersion); - if (!clientVersion.startsWith("SSH-2.0-")) { + if (!clientVersion.startsWith(DEFAULT_SSH_VERSION_PREFIX)) { String msg = "Unsupported protocol version: " + clientVersion; ioSession.write(new Buffer((msg + "\n").getBytes())).addListener(new SshFutureListener<IoWriteFuture>() { @Override http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/421faf51/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java index 1d7ea3d..baa0e83 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java @@ -25,20 +25,19 @@ import java.util.Collections; import java.util.List; import org.apache.sshd.SshServer; +import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.FactoryManagerUtils; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.Service; import org.apache.sshd.common.ServiceFactory; import org.apache.sshd.common.Session; import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.SshException; -import org.apache.sshd.common.future.CloseFuture; -import org.apache.sshd.common.future.DefaultCloseFuture; import org.apache.sshd.common.util.Buffer; import org.apache.sshd.common.util.CloseableUtils; +import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.server.ServerFactoryManager; import org.apache.sshd.server.UserAuth; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> @@ -77,17 +76,19 @@ public class ServerUserAuthService extends CloseableUtils.AbstractCloseable impl } maxAuthRequests = session.getIntProperty(ServerFactoryManager.MAX_AUTH_REQUESTS, maxAuthRequests); - userAuthFactories = new ArrayList<NamedFactory<UserAuth>>(getFactoryManager().getUserAuthFactories()); + userAuthFactories = new ArrayList<>(getFactoryManager().getUserAuthFactories()); // Get authentication methods - authMethods = new ArrayList<List<String>>(); - String mths = getFactoryManager().getProperties().get(SshServer.AUTH_METHODS); - if (mths == null) { - for (NamedFactory<UserAuth> uaf : getFactoryManager().getUserAuthFactories()) { - authMethods.add(new ArrayList<String>(Collections.singletonList(uaf.getName()))); + authMethods = new ArrayList<>(); + + ServerFactoryManager manager=getFactoryManager(); + String mths = FactoryManagerUtils.getString(manager, SshServer.AUTH_METHODS); + if (GenericUtils.isEmpty(mths)) { + for (NamedFactory<UserAuth> uaf : manager.getUserAuthFactories()) { + authMethods.add(new ArrayList<>(Collections.singletonList(uaf.getName()))); } } else { for (String mthl : mths.split("\\s")) { - authMethods.add(new ArrayList<String>(Arrays.asList(mthl.split(",")))); + authMethods.add(new ArrayList<>(Arrays.asList(mthl.split(",")))); } } // Verify all required methods are supported @@ -98,7 +99,10 @@ public class ServerUserAuthService extends CloseableUtils.AbstractCloseable impl } } } - log.debug("Authorized authentication methods: {}", NamedFactory.Utils.getNames(userAuthFactories)); + + if (log.isDebugEnabled()) { + log.debug("Authorized authentication methods: {}", NamedFactory.Utils.getNames(userAuthFactories)); + } } public void start() { @@ -177,20 +181,20 @@ public class ServerUserAuthService extends CloseableUtils.AbstractCloseable impl success |= l.isEmpty(); } } + if (success) { - if (getFactoryManager().getProperties() != null) { - String maxSessionCountAsString = getFactoryManager().getProperties().get(ServerFactoryManager.MAX_CONCURRENT_SESSIONS); - if (maxSessionCountAsString != null) { - int maxSessionCount = Integer.parseInt(maxSessionCountAsString); - int currentSessionCount = session.getActiveSessionCountForUser(username); - if (currentSessionCount >= maxSessionCount) { - session.disconnect(SshConstants.SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE, "Too many concurrent connections"); - return; - } + FactoryManager manager = getFactoryManager(); + Integer maxSessionCount = FactoryManagerUtils.getInteger(manager, ServerFactoryManager.MAX_CONCURRENT_SESSIONS); + if (maxSessionCount != null) { + int currentSessionCount = session.getActiveSessionCountForUser(username); + if (currentSessionCount >= maxSessionCount.intValue()) { + session.disconnect(SshConstants.SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE, + "Too many concurrent connections (" + currentSessionCount + ") - max. allowed: " + maxSessionCount); + return; } } - String welcomeBanner = getFactoryManager().getProperties().get(ServerFactoryManager.WELCOME_BANNER); + String welcomeBanner = FactoryManagerUtils.getString(manager, ServerFactoryManager.WELCOME_BANNER); if (welcomeBanner != null) { buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_BANNER); buffer.putString(welcomeBanner);
