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 (backwards incompat): remove explanation kwarg 

62 def __init__(self, explanation, types): 

63 # TODO (backwards incompat): remove this supercall unless it's actually 

64 # required for 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 (backwards incompat): 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 = ( 

134 "Host key for server '{}' does not match: got '{}', expected '{}'" 

135 ) 

136 return msg.format( 

137 self.hostname, 

138 self.key.get_base64(), 

139 self.expected_key.get_base64(), 

140 ) 

141 

142 

143class IncompatiblePeer(SSHException): 

144 """ 

145 A disagreement arose regarding an algorithm required for key exchange. 

146 

147 .. versionadded:: 2.9 

148 """ 

149 

150 # TODO (backwards incompat): consider making this annotate w/ 1..N 

151 # 'missing' algorithms, either just the first one that would halt kex, or 

152 # even updating the Transport logic so we record /all/ that /could/ halt 

153 # kex. 

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

155 # specific. 

156 pass 

157 

158 

159class ProxyCommandFailure(SSHException): 

160 """ 

161 The "ProxyCommand" found in the .ssh/config file returned an error. 

162 

163 :param str command: The command line that is generating this exception. 

164 :param str error: The error captured from the proxy command output. 

165 """ 

166 

167 def __init__(self, command, error): 

168 SSHException.__init__(self, command, error) 

169 self.command = command 

170 self.error = error 

171 

172 def __str__(self): 

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

174 self.command, self.error 

175 ) 

176 

177 

178class NoValidConnectionsError(socket.error): 

179 """ 

180 Multiple connection attempts were made and no families succeeded. 

181 

182 This exception class wraps multiple "real" underlying connection errors, 

183 all of which represent failed connection attempts. Because these errors are 

184 not guaranteed to all be of the same error type (i.e. different errno, 

185 `socket.error` subclass, message, etc) we expose a single unified error 

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

187 normal handling of `socket.error` objects. 

188 

189 To see the wrapped exception objects, access the ``errors`` attribute. 

190 ``errors`` is a dict whose keys are address tuples (e.g. ``('127.0.0.1', 

191 22)``) and whose values are the exception encountered trying to connect to 

192 that address. 

193 

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

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

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

197 

198 .. versionadded:: 1.16 

199 """ 

200 

201 def __init__(self, errors): 

202 """ 

203 :param dict errors: 

204 The errors dict to store, as described by class docstring. 

205 """ 

206 addrs = sorted(errors.keys()) 

207 body = ", ".join([x[0] for x in addrs[:-1]]) 

208 tail = addrs[-1][0] 

209 if body: 

210 msg = "Unable to connect to port {0} on {1} or {2}" 

211 else: 

212 msg = "Unable to connect to port {0} on {2}" 

213 super().__init__( 

214 None, 

215 msg.format(addrs[0][1], body, tail), # stand-in for errno 

216 ) 

217 self.errors = errors 

218 

219 def __reduce__(self): 

220 return (self.__class__, (self.errors,)) 

221 

222 

223class CouldNotCanonicalize(SSHException): 

224 """ 

225 Raised when hostname canonicalization fails & fallback is disabled. 

226 

227 .. versionadded:: 2.7 

228 """ 

229 

230 pass 

231 

232 

233class ConfigParseError(SSHException): 

234 """ 

235 A fatal error was encountered trying to parse SSH config data. 

236 

237 Typically this means a config file violated the ``ssh_config`` 

238 specification in a manner that requires exiting immediately, such as not 

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

240 

241 .. versionadded:: 2.7 

242 """ 

243 

244 pass 

245 

246 

247class MessageOrderError(SSHException): 

248 """ 

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

250 

251 .. versionadded:: 3.4 

252 """ 

253 

254 pass