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
|
extern crate ssh2;
extern crate libc;
use std::os;
use std::mem;
mod agent;
mod session;
mod channel;
mod knownhosts;
mod sftp;
pub struct TcpStream(libc::c_int);
pub fn socket() -> TcpStream {
unsafe {
let socket = libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0);
assert!(socket != -1, "{} {}", os::errno(), os::last_os_error());
let addr = libc::sockaddr_in {
sin_family: libc::AF_INET as libc::sa_family_t,
sin_port: 22.to_be(),
sin_addr: libc::in_addr {
s_addr: 0x7f000001.to_be(),
},
..mem::zeroed()
};
let r = libc::connect(socket, &addr as *const _ as *const _,
mem::size_of_val(&addr) as libc::c_uint);
assert!(r != -1, "{} {}", os::errno(), os::last_os_error());
TcpStream(socket)
}
}
pub fn authed_session() -> (TcpStream, ssh2::Session) {
let user = os::getenv("USER").unwrap();
let mut sess = ssh2::Session::new().unwrap();
let socket = socket();
sess.handshake(socket.fd()).unwrap();
assert!(!sess.authenticated());
{
let mut agent = sess.agent().unwrap();
agent.connect().unwrap();
agent.list_identities().unwrap();
let identity = agent.identities().next().unwrap().unwrap();
agent.userauth(user.as_slice(), &identity).unwrap();
}
assert!(sess.authenticated());
(socket, sess)
}
impl TcpStream {
fn fd(&self) -> libc::c_int { let TcpStream(fd) = *self; fd }
}
impl Drop for TcpStream {
fn drop(&mut self) {
unsafe { libc::close(self.fd()); }
}
}
|