Coverage for /pythoncovmergedfiles/medio/medio/src/paramiko/paramiko/ssh_exception.py: 54%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

68 statements  

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# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 

18 

19import socket 

20 

21 

22class SSHException(Exception): 

23 """ 

24 Exception raised by failures in SSH2 protocol negotiation or logic errors. 

25 """ 

26 

27 pass 

28 

29 

30class AuthenticationException(SSHException): 

31 """ 

32 Exception raised when authentication failed for some reason. It may be 

33 possible to retry with different credentials. (Other classes specify more 

34 specific reasons.) 

35 

36 .. versionadded:: 1.6 

37 """ 

38 

39 pass 

40 

41 

42class PasswordRequiredException(AuthenticationException): 

43 """ 

44 Exception raised when a password is needed to unlock a private key file. 

45 """ 

46 

47 pass 

48 

49 

50class BadAuthenticationType(AuthenticationException): 

51 """ 

52 Exception raised when an authentication type (like password) is used, but 

53 the server isn't allowing that type. (It may only allow public-key, for 

54 example.) 

55 

56 .. versionadded:: 1.1 

57 """ 

58 

59 allowed_types = [] 

60 

61 # TODO 4.0: remove explanation kwarg 

62 def __init__(self, explanation, types): 

63 # TODO 4.0: remove this supercall unless it's actually required for 

64 # pickling (after fixing pickling) 

65 AuthenticationException.__init__(self, explanation, types) 

66 self.explanation = explanation 

67 self.allowed_types = types 

68 

69 def __str__(self): 

70 return "{}; allowed types: {!r}".format( 

71 self.explanation, self.allowed_types 

72 ) 

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 = types 

85 

86 def __str__(self): 

87 return "Partial authentication; allowed types: {!r}".format( 

88 self.allowed_types 

89 ) 

90 

91 

92# TODO 4.0: stop inheriting from SSHException, move to auth.py 

93class UnableToAuthenticate(AuthenticationException): 

94 pass 

95 

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 server 

102 

103 .. versionadded:: 1.6 

104 """ 

105 

106 def __init__(self, code, text): 

107 SSHException.__init__(self, code, text) 

108 self.code = code 

109 self.text = text 

110 

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 server 

120 :param PKey got_key: the host key presented by the server 

121 :param PKey expected_key: the host key expected 

122 

123 .. versionadded:: 1.6 

124 """ 

125 

126 def __init__(self, hostname, got_key, expected_key): 

127 SSHException.__init__(self, hostname, got_key, expected_key) 

128 self.hostname = hostname 

129 self.key = got_key 

130 self.expected_key = expected_key 

131 

132 def __str__(self): 

133 msg = "Host key for server '{}' does not match: got '{}', expected '{}'" # noqa 

134 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.9 

146 """ 

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 the 

150 # Transport logic so we record /all/ that /could/ halt kex. 

151 # TODO: update docstrings where this may end up raised so they are more 

152 # specific. 

153 pass 

154 

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 = command 

167 self.error = error 

168 

169 def __str__(self): 

170 return 'ProxyCommand("{}") returned nonzero exit status: {}'.format( 

171 self.command, self.error 

172 ) 

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 are 

181 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 error 

183 message and a ``None`` errno so that instances of this class match most 

184 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 to 

189 that address. 

190 

191 It is implied/assumed that all the errors given to a single instance of 

192 this class are from connecting to the same hostname + port (and thus that 

193 the differences are in the resolution of the hostname - e.g. IPv4 vs v6). 

194 

195 .. versionadded:: 1.16 

196 """ 

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 errno 

212 ) 

213 self.errors = errors 

214 

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.7 

224 """ 

225 

226 pass 

227 

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 not 

235 matching ``key = value`` syntax or misusing certain ``Match`` keywords. 

236 

237 .. versionadded:: 2.7 

238 """ 

239 

240 pass 

241 

242 

243class MessageOrderError(SSHException): 

244 """ 

245 Out-of-order protocol messages were received, violating "strict kex" mode. 

246 

247 .. versionadded:: 3.4 

248 """ 

249 

250 pass