From 15a11cce02689bf961e3471e24963fe0114d391c Mon Sep 17 00:00:00 2001 From: Samoilenko Yuri Date: Thu, 16 Jan 2014 09:51:32 +0400 Subject: [PATCH] Publick key auth for sftp plugin --- src/main/java/org/kde/kdeconnect/Device.java | 2 +- .../Plugins/SftpPlugin/SftpImpl.java | 30 ++++++++++++++++++- .../Plugins/SftpPlugin/SftpPlugin.java | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/kde/kdeconnect/Device.java b/src/main/java/org/kde/kdeconnect/Device.java index 1e4cc57c..918e948b 100644 --- a/src/main/java/org/kde/kdeconnect/Device.java +++ b/src/main/java/org/kde/kdeconnect/Device.java @@ -39,7 +39,7 @@ public class Device implements BaseLink.PackageReceiver { private String deviceId; private String name; - private PublicKey publicKey; + public PublicKey publicKey; private int notificationId; private int protocolVersion; diff --git a/src/main/java/org/kde/kdeconnect/Plugins/SftpPlugin/SftpImpl.java b/src/main/java/org/kde/kdeconnect/Plugins/SftpPlugin/SftpImpl.java index 2644050f..29b3c082 100644 --- a/src/main/java/org/kde/kdeconnect/Plugins/SftpPlugin/SftpImpl.java +++ b/src/main/java/org/kde/kdeconnect/Plugins/SftpPlugin/SftpImpl.java @@ -11,6 +11,7 @@ import org.apache.sshd.server.Command; import org.apache.sshd.server.FileSystemFactory; import org.apache.sshd.server.FileSystemView; import org.apache.sshd.server.PasswordAuthenticator; +import org.apache.sshd.server.PublickeyAuthenticator; import org.apache.sshd.server.SshFile; import org.apache.sshd.server.command.ScpCommandFactory; import org.apache.sshd.server.filesystem.NativeFileSystemView; @@ -18,13 +19,17 @@ import org.apache.sshd.server.filesystem.NativeSshFile; import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; import org.apache.sshd.server.session.ServerSession; import org.apache.sshd.server.sftp.SftpSubsystem; +import org.kde.kdeconnect.Device; import java.io.File; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; +import java.security.PublicKey; +import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; +import java.util.List; class SimplePasswordAuthenticator implements PasswordAuthenticator { @@ -43,6 +48,26 @@ class SimplePasswordAuthenticator implements PasswordAuthenticator { private String password; } +class SimplePublicKeyAuthenticator implements PublickeyAuthenticator { + + private List keys = new ArrayList(); + + public void addKey(PublicKey key) { + keys.add(key); + } + + @Override + public boolean authenticate(String user, PublicKey key, ServerSession session) { + for (PublicKey k : keys) { + if (key.equals(k)) { + return true; + } + } + return false; + } + +} + class SimpleSftpServer { private static final int STARTPORT = 1739; private static final int ENDPORT = 1764; @@ -53,11 +78,13 @@ class SimpleSftpServer { private static boolean started = false; public final SimplePasswordAuthenticator passwordAuth = new SimplePasswordAuthenticator(); + public final SimplePublicKeyAuthenticator keyAuth = new SimplePublicKeyAuthenticator(); private final SshServer sshd = SshServer.setUpDefaultServer(); - public void init(Context ctx) { + public void init(Context ctx, Device device) { passwordAuth.setUser(USER); + keyAuth.addKey(device.publicKey); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(ctx.getFilesDir() + "/sftpd.ser")); //sshd.setFileSystemFactory(new NativeFileSystemFactory()); @@ -67,6 +94,7 @@ class SimpleSftpServer { sshd.setSubsystemFactories(Arrays.>asList(new SftpSubsystem.Factory())); sshd.setPasswordAuthenticator(passwordAuth); + sshd.setPublickeyAuthenticator(keyAuth); } public boolean start() { diff --git a/src/main/java/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java b/src/main/java/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java index 39487c77..50f6609a 100644 --- a/src/main/java/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java +++ b/src/main/java/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java @@ -42,7 +42,7 @@ public class SftpPlugin extends Plugin { @Override public boolean onCreate() { - server.init(context); + server.init(context, device); return true; }