I am running this code.
And change this 
            public boolean authenticate(String username, String password,
ServerSession session) {
                   return (GenericUtils.length(username) > 0) &&
username.equals(password);
            }
by this 

            public boolean authenticate(String username, String password,
ServerSession session) {
                return true;
            }


public static void main(String[] args) throws Exception {
        int port = 8000;
        String provider;
        boolean error = false;
        String hostKeyType =
AbstractGeneratorHostKeyProvider.DEFAULT_ALGORITHM;
        Map<String, String> options = new LinkedHashMap<>();

        int numArgs = GenericUtils.length(args);
        for (int i = 0; i < numArgs; i++) {
            String argName = args[i];
            if ("-p".equals(argName)) {
                if (i + 1 >= numArgs) {
                    System.err.println("option requires an argument: " +
argName);
                    break;
                }
                port = Integer.parseInt(args[++i]);
            } else if ("-key-type".equals(argName)) {
                if (i + 1 >= numArgs) {
                    System.err.println("option requires an argument: " +
argName);
                    break;
                }
                hostKeyType = args[++i].toUpperCase();
            } else if ("-io".equals(argName)) {
                if (i + 1 >= numArgs) {
                    System.err.println("option requires an argument: " +
argName);
                    break;
                }
                provider = args[++i];
                if ("mina".equals(provider)) {
                    System.setProperty(IoServiceFactory.class.getName(),
MinaServiceFactory.class.getName());
                } else if ("nio2".endsWith(provider)) {
                    System.setProperty(IoServiceFactory.class.getName(),
Nio2ServiceFactory.class.getName());
                } else {
                    System.err.println("provider should be mina or nio2: " +
argName);
                    break;
                }
            } else if ("-o".equals(argName)) {
                if (i + 1 >= numArgs) {
                    System.err.println("option requires and argument: " +
argName);
                    error = true;
                    break;
                }
                String opt = args[++i];
                int idx = opt.indexOf('=');
                if (idx <= 0) {
                    System.err.println("bad syntax for option: " + opt);
                    error = true;
                    break;
                }
                options.put(opt.substring(0, idx), opt.substring(idx + 1));
            } else if (argName.startsWith("-")) {
                System.err.println("illegal option: " + argName);
                error = true;
                break;
            } else {
                System.err.println("extra argument: " + argName);
                error = true;
                break;
            }
        }
        if (error) {
            System.err.println("usage: sshd [-p port] [-io mina|nio2]
[-key-type RSA|DSA|EC] [-o option=value]");
            System.exit(-1);
        }

        System.err.println("Starting SSHD on port " + port);

        SshServer sshd = SshServer.setUpDefaultServer();
        Map<String, Object> props = sshd.getProperties();
        props.putAll(options);
        setupServerBanner(sshd, options);
        sshd.setPort(port);

        AbstractGeneratorHostKeyProvider hostKeyProvider;
        Path hostKeyFile;
        if (SecurityUtils.isBouncyCastleRegistered()) {
            hostKeyFile = new File("key.pem").toPath();
            hostKeyProvider =
SecurityUtils.createGeneratorHostKeyProvider(hostKeyFile);
        } else {
            hostKeyFile = new File("key.ser").toPath();
            hostKeyProvider = new
SimpleGeneratorHostKeyProvider(hostKeyFile);
        }
        hostKeyProvider.setAlgorithm(hostKeyType);

        List<KeyPair> keys =
ValidateUtils.checkNotNullAndNotEmpty(hostKeyProvider.loadKeys(),
                "Failed to load keys from %s", hostKeyFile);
        KeyPair kp = keys.get(0);
        PublicKey pubKey = kp.getPublic();
        String keyAlgorithm = pubKey.getAlgorithm();
        // force re-generation of host key if not same algorithm
        if (!Objects.equals(keyAlgorithm, hostKeyProvider.getAlgorithm())) {
            Files.deleteIfExists(hostKeyFile);
            hostKeyProvider.clearLoadedKeys();
        }
        sshd.setKeyPairProvider(hostKeyProvider);

        sshd.setShellFactory(InteractiveProcessShellFactory.INSTANCE);
        sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
            @Override
            public boolean authenticate(String username, String password,
ServerSession session) {
                return true;
//                return (GenericUtils.length(username) > 0) &&
username.equals(password);
            }
        });
       
sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE);
        sshd.setTcpipForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
        sshd.setCommandFactory(new
ScpCommandFactory.Builder().withDelegate(new CommandFactory() {
            @Override
            public Command createCommand(String command) {
                return new ProcessShellFactory(GenericUtils.split(command, '
')).create();
            }
        }).build());
       
sshd.setSubsystemFactories(Arrays.<NamedFactory&lt;Command>>asList(new
SftpSubsystemFactory()));
        sshd.start();

        Thread.sleep(Long.MAX_VALUE);
    }

    public static String setupServerBanner(ServerFactoryManager server,
Map<String, ?> options) throws IOException {
        String filePath = GenericUtils.isEmpty(options) ? null :
Objects.toString(options.remove(SshConfigFileReader.BANNER_CONFIG_PROP),
null);
        if (GenericUtils.length(filePath) > 0) {
            if ("none".equals(filePath)) {
                return null;
            }

            Path path = Paths.get(filePath);
            long fileSize = Files.size(path);
            ValidateUtils.checkTrue(fileSize > 0L, "No banner contents in
file=%s", filePath);

            StringBuilder sb = new StringBuilder((int) fileSize +
Long.SIZE);
            try (BufferedReader rdr = new BufferedReader(new
InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8))) {
                for (String line = rdr.readLine(); line != null; line =
rdr.readLine()) {
                    sb.append(line).append('\n');
                }
            }

            PropertyResolverUtils.updateProperty(server,
ServerFactoryManager.WELCOME_BANNER, sb.toString());
        } else {
            PropertyResolverUtils.updateProperty(server,
ServerFactoryManager.WELCOME_BANNER, "Welcome to SSHD\n");
        }

        return PropertyResolverUtils.getString(server,
ServerFactoryManager.WELCOME_BANNER);

    }




--
View this message in context: 
http://apache-mina.10907.n7.nabble.com/Apache-MINA-SSHd-default-ShellFactory-Chocked-the-resources-tp52309p52382.html
Sent from the Apache MINA Developer Forum mailing list archive at Nabble.com.

Reply via email to