Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Changes
* Added `ssh2.channel.Channel.signal` function for sending signals over SSH to an open channel - #221
* Added `ssh2.session.Session.direct_streamlocal_ex` for creating `Channel` objects tunneling a local UNIX socket
via the remote host to a third party.
* Added new `libssh2` error codes under `ssh2.error_codes`, equivalent Python exceptions under `ssh2.exceptions`
and updated error code handling for all functions.


Packaging
Expand Down
205 changes: 147 additions & 58 deletions ssh2/error_codes.c

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions ssh2/error_codes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,11 @@ cdef extern from "libssh2.h" nogil:
_LIBSSH2_ERROR_KNOWN_HOSTS "LIBSSH2_ERROR_KNOWN_HOSTS"
_LIBSSH2_ERROR_CHANNEL_WINDOW_FULL "LIBSSH2_ERROR_CHANNEL_WINDOW_FULL"
_LIBSSH2_ERROR_KEYFILE_AUTH_FAILED "LIBSSH2_ERROR_KEYFILE_AUTH_FAILED"
_LIBSSH2_ERROR_RANDGEN "LIBSSH2_ERROR_RANDGEN"
_LIBSSH2_ERROR_MISSING_USERAUTH_BANNER "LIBSSH2_ERROR_MISSING_USERAUTH_BANNER"
_LIBSSH2_ERROR_ALGO_UNSUPPORTED "LIBSSH2_ERROR_ALGO_UNSUPPORTED"
_LIBSSH2_ERROR_MAC_FAILURE "LIBSSH2_ERROR_MAC_FAILURE"
_LIBSSH2_ERROR_HASH_INIT "LIBSSH2_ERROR_HASH_INIT"
_LIBSSH2_ERROR_HASH_CALC "LIBSSH2_ERROR_HASH_CALC"

_LIBSSH2CHANNEL_EAGAIN "LIBSSH2_ERROR_EAGAIN"
6 changes: 6 additions & 0 deletions ssh2/error_codes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ LIBSSH2_ERROR_CHANNEL_WINDOW_FULL = \
error_codes._LIBSSH2_ERROR_CHANNEL_WINDOW_FULL
LIBSSH2_ERROR_KEYFILE_AUTH_FAILED = \
error_codes._LIBSSH2_ERROR_KEYFILE_AUTH_FAILED
LIBSSH2_ERROR_RANDGEN = error_codes._LIBSSH2_ERROR_RANDGEN
LIBSSH2_ERROR_MISSING_USERAUTH_BANNER = error_codes._LIBSSH2_ERROR_MISSING_USERAUTH_BANNER
LIBSSH2_ERROR_ALGO_UNSUPPORTED = error_codes._LIBSSH2_ERROR_ALGO_UNSUPPORTED
LIBSSH2_ERROR_MAC_FAILURE = error_codes._LIBSSH2_ERROR_MAC_FAILURE
LIBSSH2_ERROR_HASH_INIT = error_codes._LIBSSH2_ERROR_HASH_INIT
LIBSSH2_ERROR_HASH_CALC = error_codes._LIBSSH2_ERROR_HASH_CALC
534 changes: 375 additions & 159 deletions ssh2/exceptions.c

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions ssh2/exceptions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,29 @@ class KeyfileAuthFailedError(SSH2Error):
"""Raised on key file authentication error"""


class RandGenError(SSH2Error):
"""Raised on randon number generator error"""


class MissingUserAuthBannerError(SSH2Error):
"""Raised on missing user authentication banner error"""


class AlgoUnsupportedError(SSH2Error):
"""Raised on unsupported algorithm error"""


class MacFailureError(SSH2Error):
"""Raised on MAC failure error"""


class HashInitError(SSH2Error):
"""Raised on hash initialisation error"""


class HashCalcError(SSH2Error):
"""Raised on hash calculation error"""


class UnknownError(SSH2Error):
"""Raised on non-specific or unknown errors"""
390 changes: 282 additions & 108 deletions ssh2/utils.c

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions ssh2/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ cpdef int handle_error_codes(int errcode) except -1:
raise exceptions.ChannelWindowFullError
elif errcode == error_codes._LIBSSH2_ERROR_KEYFILE_AUTH_FAILED:
raise exceptions.KeyfileAuthFailedError
elif errcode == error_codes._LIBSSH2_ERROR_RANDGEN:
raise exceptions.RandGenError
elif errcode == error_codes._LIBSSH2_ERROR_MISSING_USERAUTH_BANNER:
raise exceptions.MissingUserAuthBannerError
elif errcode == error_codes._LIBSSH2_ERROR_ALGO_UNSUPPORTED:
raise exceptions.AlgoUnsupportedError
elif errcode == error_codes._LIBSSH2_ERROR_MAC_FAILURE:
raise exceptions.MacFailureError
elif errcode == error_codes._LIBSSH2_ERROR_HASH_INIT:
raise exceptions.HashInitError
elif errcode == error_codes._LIBSSH2_ERROR_HASH_CALC:
raise exceptions.HashCalcError
else:
# Switch default
if errcode < 0:
Expand Down
13 changes: 11 additions & 2 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
LIBSSH2_ERROR_COMPRESS, LIBSSH2_ERROR_OUT_OF_BOUNDARY, \
LIBSSH2_ERROR_AGENT_PROTOCOL, LIBSSH2_ERROR_SOCKET_RECV, \
LIBSSH2_ERROR_SOCKET_SEND, LIBSSH2_ERROR_ENCRYPT, \
LIBSSH2_ERROR_BAD_SOCKET, LIBSSH2_ERROR_KNOWN_HOSTS
LIBSSH2_ERROR_BAD_SOCKET, LIBSSH2_ERROR_KNOWN_HOSTS, \
LIBSSH2_ERROR_RANDGEN, LIBSSH2_ERROR_MISSING_USERAUTH_BANNER, LIBSSH2_ERROR_ALGO_UNSUPPORTED, \
LIBSSH2_ERROR_MAC_FAILURE, LIBSSH2_ERROR_HASH_INIT, LIBSSH2_ERROR_HASH_CALC
from ssh2.exceptions import SSH2Error, AgentError, AuthenticationError, \
AgentConnectionError, AgentAuthenticationError, AgentListIdentitiesError, \
AgentGetIdentityError, AgentProtocolError, SessionError, \
Expand All @@ -38,7 +40,8 @@
InvalidPollTypeError, PublicKeyProtocolError, BufferTooSmallError, \
BadUseError, CompressError, OutOfBoundaryError, SocketRecvError, \
SocketSendError, EncryptError, BadSocketError, SFTPError, SFTPProtocolError, \
KnownHostError, UnknownError
KnownHostError, UnknownError, RandGenError, MissingUserAuthBannerError, \
AlgoUnsupportedError, MacFailureError, HashInitError, HashCalcError
from ssh2.utils import handle_error_codes


Expand All @@ -65,6 +68,12 @@ def test_general_errors(self):
self.assertRaises(MethodNoneError, handle_error_codes, LIBSSH2_ERROR_METHOD_NONE)
self.assertRaises(AuthenticationError, handle_error_codes, LIBSSH2_ERROR_AUTHENTICATION_FAILED)
self.assertRaises(PublickeyUnverifiedError, handle_error_codes, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED)
self.assertRaises(RandGenError, handle_error_codes, LIBSSH2_ERROR_RANDGEN)
self.assertRaises(MissingUserAuthBannerError, handle_error_codes, LIBSSH2_ERROR_MISSING_USERAUTH_BANNER)
self.assertRaises(AlgoUnsupportedError, handle_error_codes, LIBSSH2_ERROR_ALGO_UNSUPPORTED)
self.assertRaises(MacFailureError, handle_error_codes, LIBSSH2_ERROR_MAC_FAILURE)
self.assertRaises(HashInitError, handle_error_codes, LIBSSH2_ERROR_HASH_INIT)
self.assertRaises(HashCalcError, handle_error_codes, LIBSSH2_ERROR_HASH_CALC)

def test_channel_errors(self):
self.assertRaises(ChannelOutOfOrderError, handle_error_codes, LIBSSH2_ERROR_CHANNEL_OUTOFORDER)
Expand Down