codekingpro/portable-devtools
114k
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 the6# terms of the GNU Lesser General Public License as published by the Free7# 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 ANY11# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR12# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more13# details.14#15# You should have received a copy of the GNU Lesser General Public License16# along with Paramiko; if not, write to the Free Software Foundation, Inc.,17# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.18 19import socket20 21 22class SSHException(Exception):23 """24 Exception raised by failures in SSH2 protocol negotiation or logic errors.25 """26 27 pass28 29 30class AuthenticationException(SSHException):31 """32 Exception raised when authentication failed for some reason. It may be33 possible to retry with different credentials. (Other classes specify more34 specific reasons.)35 36 .. versionadded:: 1.637 """38 39 pass40 41 42class PasswordRequiredException(AuthenticationException):43 """44 Exception raised when a password is needed to unlock a private key file.45 """46 47 pass48 49 50class BadAuthenticationType(AuthenticationException):51 """52 Exception raised when an authentication type (like password) is used, but53 the server isn't allowing that type. (It may only allow public-key, for54 example.)55 56 .. versionadded:: 1.157 """58 59 allowed_types = []60 61 # TODO 4.0: remove explanation kwarg62 def __init__(self, explanation, types):63 # TODO 4.0: remove this supercall unless it's actually required for64 # pickling (after fixing pickling)65 AuthenticationException.__init__(self, explanation, types)66 self.explanation = explanation67 self.allowed_types = types68 69 def __str__(self):70 return "{}; allowed types: {!r}".format(71 self.explanation, self.allowed_types72 )73 74 75class PartialAuthentication(AuthenticationException):76 """77 An internal exception thrown in the case of partial authentication.78 """79 80 allowed_types = []81 82 def __init__(self, types):83 AuthenticationException.__init__(self, types)84 self.allowed_types = types85 86 def __str__(self):87 return "Partial authentication; allowed types: {!r}".format(88 self.allowed_types89 )90 91 92# TODO 4.0: stop inheriting from SSHException, move to auth.py93class UnableToAuthenticate(AuthenticationException):94 pass95 96 97class ChannelException(SSHException):98 """99 Exception raised when an attempt to open a new `.Channel` fails.100 101 :param int code: the error code returned by the server102 103 .. versionadded:: 1.6104 """105 106 def __init__(self, code, text):107 SSHException.__init__(self, code, text)108 self.code = code109 self.text = text110 111 def __str__(self):112 return "ChannelException({!r}, {!r})".format(self.code, self.text)113 114 115class BadHostKeyException(SSHException):116 """117 The host key given by the SSH server did not match what we were expecting.118 119 :param str hostname: the hostname of the SSH server120 :param PKey got_key: the host key presented by the server121 :param PKey expected_key: the host key expected122 123 .. versionadded:: 1.6124 """125 126 def __init__(self, hostname, got_key, expected_key):127 SSHException.__init__(self, hostname, got_key, expected_key)128 self.hostname = hostname129 self.key = got_key130 self.expected_key = expected_key131 132 def __str__(self):133 msg = "Host key for server '{}' does not match: got '{}', expected '{}'" # noqa134 return msg.format(135 self.hostname,136 self.key.get_base64(),137 self.expected_key.get_base64(),138 )139 140 141class IncompatiblePeer(SSHException):142 """143 A disagreement arose regarding an algorithm required for key exchange.144 145 .. versionadded:: 2.9146 """147 148 # TODO 4.0: consider making this annotate w/ 1..N 'missing' algorithms,149 # either just the first one that would halt kex, or even updating the150 # Transport logic so we record /all/ that /could/ halt kex.151 # TODO: update docstrings where this may end up raised so they are more152 # specific.153 pass154 155 156class ProxyCommandFailure(SSHException):157 """158 The "ProxyCommand" found in the .ssh/config file returned an error.159 160 :param str command: The command line that is generating this exception.161 :param str error: The error captured from the proxy command output.162 """163 164 def __init__(self, command, error):165 SSHException.__init__(self, command, error)166 self.command = command167 self.error = error168 169 def __str__(self):170 return 'ProxyCommand("{}") returned nonzero exit status: {}'.format(171 self.command, self.error172 )173 174 175class NoValidConnectionsError(socket.error):176 """177 Multiple connection attempts were made and no families succeeded.178 179 This exception class wraps multiple "real" underlying connection errors,180 all of which represent failed connection attempts. Because these errors are181 not guaranteed to all be of the same error type (i.e. different errno,182 `socket.error` subclass, message, etc) we expose a single unified error183 message and a ``None`` errno so that instances of this class match most184 normal handling of `socket.error` objects.185 186 To see the wrapped exception objects, access the ``errors`` attribute.187 ``errors`` is a dict whose keys are address tuples (e.g. ``('127.0.0.1',188 22)``) and whose values are the exception encountered trying to connect to189 that address.190 191 It is implied/assumed that all the errors given to a single instance of192 this class are from connecting to the same hostname + port (and thus that193 the differences are in the resolution of the hostname - e.g. IPv4 vs v6).194 195 .. versionadded:: 1.16196 """197 198 def __init__(self, errors):199 """200 :param dict errors:201 The errors dict to store, as described by class docstring.202 """203 addrs = sorted(errors.keys())204 body = ", ".join([x[0] for x in addrs[:-1]])205 tail = addrs[-1][0]206 if body:207 msg = "Unable to connect to port {0} on {1} or {2}"208 else:209 msg = "Unable to connect to port {0} on {2}"210 super().__init__(211 None, msg.format(addrs[0][1], body, tail) # stand-in for errno212 )213 self.errors = errors214 215 def __reduce__(self):216 return (self.__class__, (self.errors,))217 218 219class CouldNotCanonicalize(SSHException):220 """221 Raised when hostname canonicalization fails & fallback is disabled.222 223 .. versionadded:: 2.7224 """225 226 pass227 228 229class ConfigParseError(SSHException):230 """231 A fatal error was encountered trying to parse SSH config data.232 233 Typically this means a config file violated the ``ssh_config``234 specification in a manner that requires exiting immediately, such as not235 matching ``key = value`` syntax or misusing certain ``Match`` keywords.236 237 .. versionadded:: 2.7238 """239 240 pass241 242 243class MessageOrderError(SSHException):244 """245 Out-of-order protocol messages were received, violating "strict kex" mode.246 247 .. versionadded:: 3.4248 """249 250 pass251 