1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::fmt;
use std::io;
#[derive(Debug)]
pub enum Error {
Master(io::Error),
Connect(io::Error),
Ssh(io::Error),
Remote(io::Error),
Disconnected,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::Master(_) => write!(f, "the master connection failed"),
Error::Connect(_) => write!(f, "failed to connect to the remote host"),
Error::Ssh(_) => write!(f, "the local ssh command could not be executed"),
Error::Remote(_) => write!(f, "the remote command could not be executed"),
Error::Disconnected => write!(f, "the connection was terminated"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
Error::Master(ref e)
| Error::Connect(ref e)
| Error::Ssh(ref e)
| Error::Remote(ref e) => Some(e),
Error::Disconnected => None,
}
}
}
impl Error {
pub(crate) fn interpret_ssh_error(stderr: &str) -> Self {
let mut stderr = stderr.trim();
if stderr.starts_with("ssh: ") {
stderr = &stderr["ssh: ".len()..];
}
if stderr.starts_with("Warning: Permanently added ") {
stderr = stderr.splitn(2, "\r\n").nth(1).unwrap_or("");
}
let mut kind = io::ErrorKind::ConnectionAborted;
let mut err = stderr.splitn(2, ": ");
if let Some(ssh_error) = err.next() {
if ssh_error.starts_with("Could not resolve") {
kind = io::ErrorKind::Other;
}
if let Some(io_error) = err.next() {
match io_error {
"Network is unreachable" => {
kind = io::ErrorKind::Other;
}
"Connection refused" => {
kind = io::ErrorKind::ConnectionRefused;
}
e if ssh_error.starts_with("connect to host")
&& e == "Connection timed out" =>
{
kind = io::ErrorKind::TimedOut;
}
e if ssh_error.starts_with("connect to host") && e == "Operation timed out" => {
kind = io::ErrorKind::TimedOut;
}
e if ssh_error.starts_with("connect to host") && e == "Permission denied" => {
kind = io::ErrorKind::Other;
}
e if e.contains("Permission denied (") => {
kind = io::ErrorKind::PermissionDenied;
}
_ => {}
}
}
}
Error::Connect(io::Error::new(kind, stderr))
}
}
#[test]
fn parse_error() {
let err = "ssh: Warning: Permanently added \'login.csail.mit.edu,128.52.131.0\' (ECDSA) to the list of known hosts.\r\nopenssh-tester@login.csail.mit.edu: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive).";
let err = Error::interpret_ssh_error(err);
let target = io::Error::new(io::ErrorKind::PermissionDenied, "openssh-tester@login.csail.mit.edu: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password,keyboard-interactive).");
if let Error::Connect(e) = err {
assert_eq!(e.kind(), target.kind());
assert_eq!(format!("{}", e), format!("{}", target));
} else {
unreachable!("{:?}", err);
}
}
#[test]
fn error_sanity() {
use std::error::Error as _;
let ioe = || io::Error::new(io::ErrorKind::Other, "test");
let expect = ioe();
let e = Error::Master(ioe());
assert!(!format!("{}", e).is_empty());
let e = e
.source()
.expect("source failed")
.downcast_ref::<io::Error>()
.expect("source not io");
assert_eq!(e.kind(), expect.kind());
assert_eq!(format!("{}", e), format!("{}", expect));
let e = Error::Connect(ioe());
assert!(!format!("{}", e).is_empty());
let e = e
.source()
.expect("source failed")
.downcast_ref::<io::Error>()
.expect("source not io");
assert_eq!(e.kind(), expect.kind());
assert_eq!(format!("{}", e), format!("{}", expect));
let e = Error::Ssh(ioe());
assert!(!format!("{}", e).is_empty());
let e = e
.source()
.expect("source failed")
.downcast_ref::<io::Error>()
.expect("source not io");
assert_eq!(e.kind(), expect.kind());
assert_eq!(format!("{}", e), format!("{}", expect));
let e = Error::Remote(ioe());
assert!(!format!("{}", e).is_empty());
let e = e
.source()
.expect("source failed")
.downcast_ref::<io::Error>()
.expect("source not io");
assert_eq!(e.kind(), expect.kind());
assert_eq!(format!("{}", e), format!("{}", expect));
let e = Error::Disconnected;
assert!(!format!("{}", e).is_empty());
assert!(e.source().is_none());
}