How to block SFTP remove operations with Apache MINA SSHD
Asked Answered
H

1

3

I am trying to create a custom sftp server using Apache Mina SSHD. My code so far:

 SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPort(PORT_NUMBER);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("keys/private_key.ppk")));

        SftpSubsystemFactory factory = new SftpSubsystemFactory.Builder()
                .build();


        factory.addSftpEventListener(new BasicSftpEventListener());

        sshd.setSubsystemFactories(Collections.singletonList(factory));
        sshd.setShellFactory(new ProcessShellFactory("/bin/sh", "-i", "-l"));
        sshd.start();

As you can see, I implemented my own SftpEventListener:

public class BasicSftpEventListener implements SftpEventListener {

    @Override
    public void removing(ServerSession session, Path path) throws IOException {
        System.out.println("Removin");
    }

    @Override
    public void removed(ServerSession session, Path path, Throwable thrown) throws IOException {
        System.out.println("removed");
    }

When I want to remove file, it executes my removing and removed listeners, BUT the remove operation proceeds and the file is deleted.

Is there a way how to stop this from happening?

Thanks for help!

Heres answered 18/1, 2020 at 17:23 Comment(0)
A
0

If you want to block delete actions, you will need to interrupt the flow of the removing method with an exception. This will tell Mina to stop and not remove the file. I would recommend using java.lang.UnsupportedOperationException for this:

@Override
public void removing(ServerSession session, Path path) throws UnsupportedOperationException{
    throw new UnsupportedActionException("Removing files is not permitted.");
}
Acidulate answered 21/1, 2020 at 18:30 Comment(1)
well this is one way, but i dont think throwing exception to interrupt flow is good idea. Seems like only "nice" way to do it is extend some classes and override behavior.Heres

© 2022 - 2024 — McMap. All rights reserved.