This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git

commit 79b845277fdaa9715fa0d9de4bc436c8fca3cdac
Author: Lyor Goldstein <lgoldst...@apache.org>
AuthorDate: Fri Jun 5 11:44:18 2020 +0300

    [SSHD-1009] Added CLI capability to specify SCP as the ShellFactory
---
 .../sshd/cli/server/SshServerCliSupport.java       | 23 ++++++++++++++++++++++
 .../org/apache/sshd/cli/server/SshServerMain.java  | 18 ++++++++++++++---
 .../org/apache/sshd/cli/server/SshFsMounter.java   | 13 +++++++++++-
 3 files changed, 50 insertions(+), 4 deletions(-)

diff --git 
a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java 
b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
index b1a613c..5057ac9 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
@@ -59,6 +59,7 @@ import 
org.apache.sshd.server.config.SshServerConfigFileReader;
 import org.apache.sshd.server.forward.ForwardingFilter;
 import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
 import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
+import org.apache.sshd.server.scp.ScpCommandFactory;
 import org.apache.sshd.server.shell.InteractiveProcessShellFactory;
 import org.apache.sshd.server.shell.ShellFactory;
 import org.apache.sshd.server.subsystem.SubsystemFactory;
@@ -232,6 +233,24 @@ public abstract class SshServerCliSupport extends 
CliSupport {
         return factory;
     }
 
+    /**
+     * Attempts to examine the {@link #SHELL_FACTORY_OPTION} configuration.
+     * <UL>
+     * <LI>If missing/empty then returns the {@link 
#DEFAULT_SHELL_FACTORY}.</LI>
+     *
+     * <LI>If {@link PropertyResolverUtils#isNoneValue(String) NONE} then 
returns {@code null}</LI>
+     *
+     * <LI>If {@link ScpCommandFactory#SCP_FACTORY_NAME SCP} then returns a 
{@link ScpCommandFactory}</LI>
+     *
+     * <LI>Otherwise, assumes this is a fully qualified class path of a {@link 
ShellFactory} implementation and attempts
+     * to load and instantiate it using a public no-args constructor</LI>
+     * </UL>
+     * 
+     * @param  stderr    The STDERR stream for errors
+     * @param  options   The available options - assuming defaults if {@code 
null}
+     * @return           The resolved {@link ShellFactory}
+     * @throws Exception If failed to resolve
+     */
     public static ShellFactory resolveShellFactory(PrintStream stderr, 
PropertyResolver options) throws Exception {
         String factory = (options == null) ? null : 
options.getString(SHELL_FACTORY_OPTION);
         if (GenericUtils.isEmpty(factory)) {
@@ -242,6 +261,10 @@ public abstract class SshServerCliSupport extends 
CliSupport {
             return null;
         }
 
+        if (ScpCommandFactory.SCP_FACTORY_NAME.equalsIgnoreCase(factory)) {
+            return new ScpCommandFactory();
+        }
+
         ClassLoader cl = 
ThreadUtils.resolveDefaultClassLoader(ShellFactory.class);
         try {
             Class<?> clazz = cl.loadClass(factory);
diff --git 
a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java 
b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
index 9361ee6..a6e6e51 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
@@ -40,6 +40,7 @@ import org.apache.sshd.common.keyprovider.KeyPairProvider;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.server.SshServer;
 import org.apache.sshd.server.auth.pubkey.AcceptAllPublickeyAuthenticator;
+import org.apache.sshd.server.command.CommandFactory;
 import org.apache.sshd.server.config.SshServerConfigFileReader;
 import org.apache.sshd.server.config.keys.ServerIdentity;
 import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
@@ -205,9 +206,7 @@ public class SshServerMain extends SshServerCliSupport {
         
sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE);
         setupUserAuthFactories(sshd, resolver);
         setupServerForwarding(sshd, level, System.out, System.err, resolver);
-        sshd.setCommandFactory(new ScpCommandFactory.Builder()
-                .withDelegate(ProcessShellCommandFactory.INSTANCE)
-                .build());
+        setupCommandFactory(sshd, shellFactory);
 
         List<SubsystemFactory> subsystems = resolveServerSubsystems(sshd, 
level, System.out, System.err, resolver);
         if (GenericUtils.isNotEmpty(subsystems)) {
@@ -220,4 +219,17 @@ public class SshServerMain extends SshServerCliSupport {
         Thread.sleep(Long.MAX_VALUE);
         System.err.println("Exiting after a very (very very) long time");
     }
+
+    private static CommandFactory setupCommandFactory(SshServer sshd, 
ShellFactory shellFactory) {
+        ScpCommandFactory scpFactory;
+        if (shellFactory instanceof ScpCommandFactory) {
+            scpFactory = (ScpCommandFactory) shellFactory;
+        } else {
+            scpFactory = new ScpCommandFactory.Builder()
+                    .withDelegate(ProcessShellCommandFactory.INSTANCE)
+                    .build();
+        }
+        sshd.setCommandFactory(scpFactory);
+        return scpFactory;
+    }
 }
diff --git 
a/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java 
b/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java
index dd33d61..f6b3c99 100644
--- a/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java
+++ b/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java
@@ -332,7 +332,18 @@ public final class SshFsMounter extends 
SshServerCliSupport {
         }
         sshd.setPasswordAuthenticator(AcceptAllPasswordAuthenticator.INSTANCE);
         sshd.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
-        sshd.setCommandFactory(new 
ScpCommandFactory.Builder().withDelegate(MounterCommandFactory.INSTANCE).build());
+
+        ScpCommandFactory scpFactory;
+        if (shellFactory instanceof ScpCommandFactory) {
+            scpFactory = (ScpCommandFactory) shellFactory;
+            
scpFactory.setDelegateCommandFactory(MounterCommandFactory.INSTANCE);
+        } else {
+            scpFactory = new ScpCommandFactory.Builder()
+                    .withDelegate(MounterCommandFactory.INSTANCE)
+                    .build();
+        }
+
+        sshd.setCommandFactory(scpFactory);
         sshd.setSubsystemFactories(Collections.singletonList(new 
SftpSubsystemFactory()));
         sshd.setPort(port);
 

Reply via email to