ssh_exception.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Copyright (C) 2003-2007 Robey Pointer <robeypointer@gmail.com>
  2. #
  3. # This file is part of paramiko.
  4. #
  5. # Paramiko is free software; you can redistribute it and/or modify it under the
  6. # terms of the GNU Lesser General Public License as published by the Free
  7. # Software Foundation; either version 2.1 of the License, or (at your option)
  8. # any later version.
  9. #
  10. # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
  11. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
  17. # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  18. import socket
  19. class SSHException (Exception):
  20. """
  21. Exception raised by failures in SSH2 protocol negotiation or logic errors.
  22. """
  23. pass
  24. class AuthenticationException (SSHException):
  25. """
  26. Exception raised when authentication failed for some reason. It may be
  27. possible to retry with different credentials. (Other classes specify more
  28. specific reasons.)
  29. .. versionadded:: 1.6
  30. """
  31. pass
  32. class PasswordRequiredException (AuthenticationException):
  33. """
  34. Exception raised when a password is needed to unlock a private key file.
  35. """
  36. pass
  37. class BadAuthenticationType (AuthenticationException):
  38. """
  39. Exception raised when an authentication type (like password) is used, but
  40. the server isn't allowing that type. (It may only allow public-key, for
  41. example.)
  42. .. versionadded:: 1.1
  43. """
  44. #: list of allowed authentication types provided by the server (possible
  45. #: values are: ``"none"``, ``"password"``, and ``"publickey"``).
  46. allowed_types = []
  47. def __init__(self, explanation, types):
  48. AuthenticationException.__init__(self, explanation)
  49. self.allowed_types = types
  50. # for unpickling
  51. self.args = (explanation, types, )
  52. def __str__(self):
  53. return '{0} (allowed_types={1!r})'.format(
  54. SSHException.__str__(self), self.allowed_types
  55. )
  56. class PartialAuthentication (AuthenticationException):
  57. """
  58. An internal exception thrown in the case of partial authentication.
  59. """
  60. allowed_types = []
  61. def __init__(self, types):
  62. AuthenticationException.__init__(self, 'partial authentication')
  63. self.allowed_types = types
  64. # for unpickling
  65. self.args = (types, )
  66. class ChannelException (SSHException):
  67. """
  68. Exception raised when an attempt to open a new `.Channel` fails.
  69. :param int code: the error code returned by the server
  70. .. versionadded:: 1.6
  71. """
  72. def __init__(self, code, text):
  73. SSHException.__init__(self, text)
  74. self.code = code
  75. # for unpickling
  76. self.args = (code, text, )
  77. class BadHostKeyException (SSHException):
  78. """
  79. The host key given by the SSH server did not match what we were expecting.
  80. :param str hostname: the hostname of the SSH server
  81. :param PKey got_key: the host key presented by the server
  82. :param PKey expected_key: the host key expected
  83. .. versionadded:: 1.6
  84. """
  85. def __init__(self, hostname, got_key, expected_key):
  86. message = 'Host key for server {0} does not match: got {1}, expected {2}' # noqa
  87. message = message.format(
  88. hostname, got_key.get_base64(),
  89. expected_key.get_base64())
  90. SSHException.__init__(self, message)
  91. self.hostname = hostname
  92. self.key = got_key
  93. self.expected_key = expected_key
  94. # for unpickling
  95. self.args = (hostname, got_key, expected_key, )
  96. class ProxyCommandFailure (SSHException):
  97. """
  98. The "ProxyCommand" found in the .ssh/config file returned an error.
  99. :param str command: The command line that is generating this exception.
  100. :param str error: The error captured from the proxy command output.
  101. """
  102. def __init__(self, command, error):
  103. SSHException.__init__(self,
  104. '"ProxyCommand (%s)" returned non-zero exit status: %s' % (
  105. command, error
  106. )
  107. )
  108. self.error = error
  109. # for unpickling
  110. self.args = (command, error, )
  111. class NoValidConnectionsError(socket.error):
  112. """
  113. Multiple connection attempts were made and no families succeeded.
  114. This exception class wraps multiple "real" underlying connection errors,
  115. all of which represent failed connection attempts. Because these errors are
  116. not guaranteed to all be of the same error type (i.e. different errno,
  117. `socket.error` subclass, message, etc) we expose a single unified error
  118. message and a ``None`` errno so that instances of this class match most
  119. normal handling of `socket.error` objects.
  120. To see the wrapped exception objects, access the ``errors`` attribute.
  121. ``errors`` is a dict whose keys are address tuples (e.g. ``('127.0.0.1',
  122. 22)``) and whose values are the exception encountered trying to connect to
  123. that address.
  124. It is implied/assumed that all the errors given to a single instance of
  125. this class are from connecting to the same hostname + port (and thus that
  126. the differences are in the resolution of the hostname - e.g. IPv4 vs v6).
  127. .. versionadded:: 1.16
  128. """
  129. def __init__(self, errors):
  130. """
  131. :param dict errors:
  132. The errors dict to store, as described by class docstring.
  133. """
  134. addrs = sorted(errors.keys())
  135. body = ', '.join([x[0] for x in addrs[:-1]])
  136. tail = addrs[-1][0]
  137. if body:
  138. msg = "Unable to connect to port {0} on {1} or {2}"
  139. else:
  140. msg = "Unable to connect to port {0} on {2}"
  141. super(NoValidConnectionsError, self).__init__(
  142. None, # stand-in for errno
  143. msg.format(addrs[0][1], body, tail)
  144. )
  145. self.errors = errors
  146. def __reduce__(self):
  147. return (self.__class__, (self.errors, ))