From 24eac6c2b377cb0ef36ab061240725dadd32ee66 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sun, 15 Jan 2023 21:43:00 +0100 Subject: [PATCH] Added error handling for libssh --- .../gitnuro/credentials/GRemoteSession.kt | 24 ++++++++++++++++++- .../jetpackduba/gitnuro/ssh/libssh/Enums.kt | 10 ++++++++ .../gitnuro/ssh/libssh/LibSshSession.kt | 20 ++++++++++++---- .../gitnuro/ssh/libssh/LibSshWrapper.kt | 6 ++++- .../streams/LibSshChannelOutputStream.kt | 7 ++++++ 5 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/Enums.kt diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/credentials/GRemoteSession.kt b/src/main/kotlin/com/jetpackduba/gitnuro/credentials/GRemoteSession.kt index 7a9c6fd..d86abb9 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/credentials/GRemoteSession.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/credentials/GRemoteSession.kt @@ -40,8 +40,30 @@ class GRemoteSession @Inject constructor( fun setup(uri: URIish) { val session = processSession.get() session.setOptions(LibSshOptions.SSH_OPTIONS_HOST, uri.host) + session.setOptions(LibSshOptions.SSH_OPTIONS_USER, uri.user) + session.loadOptionsFromConfig() + session.connect() - session.userAuthPublicKeyAuto(uri.user, null) + var result = session.userAuthPublicKeyAuto(null, null) + + if(result == 1) { + credentialsStateManager.updateState(CredentialsState.SshCredentialsRequested) + + var credentials = credentialsStateManager.currentCredentialsState + while (credentials is CredentialsState.CredentialsRequested) { + credentials = credentialsStateManager.currentCredentialsState + } + + val password = if (credentials !is CredentialsState.SshCredentialsAccepted) + null + else + credentials.password + + result = session.userAuthPublicKeyAuto(null, password) + } + + if(result != 0) + throw Exception("Something went wrong with authentication") this.session = session } diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/Enums.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/Enums.kt new file mode 100644 index 0000000..78f8d31 --- /dev/null +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/Enums.kt @@ -0,0 +1,10 @@ +package com.jetpackduba.gitnuro.ssh.libssh + +enum class sshAuthE(val value: Int) { + SSH_AUTH_SUCCESS(0), + SSH_AUTH_DENIED(1), + SSH_AUTH_PARTIAL(2), + SSH_AUTH_INFO(3), + SSH_AUTH_AGAIN(4), + SSH_AUTH_ERROR(-1) +} \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/LibSshSession.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/LibSshSession.kt index 282aa63..c830452 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/LibSshSession.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/LibSshSession.kt @@ -1,5 +1,6 @@ package com.jetpackduba.gitnuro.ssh.libssh +import com.jetpackduba.gitnuro.ssh.libssh.streams.checkValidResult import javax.inject.Inject class LibSshSession @Inject constructor() { @@ -8,19 +9,25 @@ class LibSshSession @Inject constructor() { private var session: ssh_session = sshLib.ssh_new() private var channel: LibSshChannel? = null - fun setOptions(option: LibSshOptions, value: String) { sshLib.ssh_options_set(session, option.ordinal, value) } + fun loadOptionsFromConfig() { + checkValidResult(sshLib.ssh_options_parse_config(session, null)) + } + fun connect() { sshLib.ssh_connect(session) } + fun userAuthPublicKeyAuto(username: String?, password: String?): Int { + val result = sshLib.ssh_userauth_publickey_auto(session, username, password) + if(result != 0) + println("RESULT is $result. ERROR IS: ${getError()}") - fun userAuthPublicKeyAuto(username: String?, password: String?) { - sshLib.ssh_userauth_publickey_auto(session, username, password) + return result } fun createChannel(): LibSshChannel { @@ -31,8 +38,13 @@ class LibSshSession @Inject constructor() { return newChannel } + private fun getError(): String { + return sshLib.ssh_get_error(session) + } + fun disconnect() { sshLib.ssh_disconnect(session) } +} + -} \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/LibSshWrapper.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/LibSshWrapper.kt index fdd5ea6..9e68b68 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/LibSshWrapper.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/LibSshWrapper.kt @@ -12,10 +12,14 @@ interface SSHLibrary : Library { fun ssh_new(): ssh_session fun ssh_disconnect(session: ssh_session): ssh_session fun ssh_options_set(session: ssh_session, enumValue: Int, value: String) + + fun ssh_options_parse_config(session: ssh_session, fileName: String?): Int + fun ssh_connect(session: ssh_session) : Int - fun ssh_userauth_password(session: ssh_session, username: String, password: String): Int + fun ssh_userauth_agent(session: ssh_session, username: String?): Int fun ssh_userauth_publickey_auto(session: ssh_session, username: String?, password: String?): Int + fun ssh_get_error(session: ssh_session): String fun ssh_channel_new(sshSession: ssh_session): ssh_channel diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/streams/LibSshChannelOutputStream.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/streams/LibSshChannelOutputStream.kt index 31b4f8e..d4ea57b 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/streams/LibSshChannelOutputStream.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/streams/LibSshChannelOutputStream.kt @@ -31,4 +31,11 @@ class LibSshChannelOutputStream(private val sshChannel: ssh_channel) : OutputStr fun checkValidResult(result: Int) { if (result != 0) throw Exception("Result is $result") +} + +fun checkValidResult(result: Int, callback: (Int) -> Unit) { + if (result != 0) { + callback(result) + throw Exception("Result is $result") + } } \ No newline at end of file