简体   繁体   中英

How to block SFTP remove operations with Apache MINA SSHD

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!

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.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM