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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::sync::MutexGuard;
use std::sync::atomic::Ordering::Relaxed;
use crate::channel::Channel;
use crate::tcp::Client;
use crate::{strings, message, size, global, ChannelExec, ChannelShell};
use crate::error::{SshError, SshErrorKind, SshResult};
use crate::kex::Kex;
use crate::packet::{Data, Packet};
use crate::util;
pub struct Session;
impl Session {
pub fn connect(&mut self) -> Result<(), SshError> {
self.receive_version()?;
let config = util::config()?;
config.version.validation()?;
util::unlock(config);
self.send_version()?;
let mut kex = Kex::new()?;
kex.send_algorithm()?;
kex.receive_algorithm()?;
let config = util::config()?;
let (dh, sign) = config.algorithm.matching_algorithm()?;
kex.dh = dh;
kex.signature = sign;
kex.h.set_v_c(config.version.client_version.as_str());
kex.h.set_v_s(config.version.server_version.as_str());
util::unlock(config);
kex.send_qc()?;
kex.verify_signature()?;
kex.new_keys()?;
self.initiate_authentication()?;
self.authentication()
}
pub fn set_nonblocking(&mut self, nonblocking: bool) -> SshResult<()> {
let client = util::client()?;
if let Err(e) = client.stream.set_nonblocking(nonblocking) {
return Err(SshError::from(e))
}
Ok(())
}
pub fn set_user_and_password(&mut self, user: String, password: String) -> SshResult<()> {
let mut config = util::config()?;
config.user.username = user;
config.user.password = password;
Ok(())
}
pub fn close(self) -> SshResult<()> {
let mut client = util::client()?;
client.close()
}
pub fn open_channel(&mut self) -> SshResult<Channel> {
let client_channel = global::CLIENT_CHANNEL.load(Relaxed);
self.ssh_open_channel(client_channel)?;
global::CLIENT_CHANNEL.fetch_add(1, Relaxed);
Ok(Channel {
kex: Kex::new()?,
server_channel: 0,
client_channel,
remote_close: false,
local_close: false
})
}
pub fn open_exec(&mut self) -> SshResult<ChannelExec> {
let channel = self.open_channel()?;
channel.open_exec()
}
pub fn open_shell(&mut self) -> SshResult<ChannelShell> {
let channel = self.open_channel()?;
channel.open_shell()
}
fn ssh_open_channel(&mut self, client_channel: u32) -> SshResult<()> {
let mut data = Data::new();
data.put_u8(message::SSH_MSG_CHANNEL_OPEN)
.put_str(strings::SESSION)
.put_u32(client_channel)
.put_u32(size::LOCAL_WINDOW_SIZE)
.put_u32(size::BUF_SIZE as u32);
let mut packet = Packet::from(data);
packet.build();
let mut client = util::client()?;
client.write(packet.as_slice())
}
fn initiate_authentication(&mut self) -> SshResult<()> {
let mut data = Data::new();
data.put_u8(message::SSH_MSG_SERVICE_REQUEST)
.put_str(strings::SSH_USERAUTH);
let mut packet = Packet::from(data);
packet.build();
let mut client = util::client()?;
client.write(packet.as_slice())
}
fn authentication(&mut self) -> SshResult<()> {
let mut client = util::client()?;
loop {
let results = client.read()?;
for result in results {
if result.is_empty() { continue }
let message_code = result[5];
match message_code {
message::SSH_MSG_SERVICE_ACCEPT => {
password_authentication(&mut client)?;
}
message::SSH_MSG_USERAUTH_FAILURE => {
log::error!("user auth failure");
return Err(SshError::from(SshErrorKind::PasswordError))
},
message::SSH_MSG_USERAUTH_SUCCESS => {
log::info!("user auth success");
return Ok(())
},
message::SSH_MSG_GLOBAL_REQUEST => {
let mut data = Data::new();
data.put_u8(message::SSH_MSG_REQUEST_FAILURE);
let mut packet = Packet::from(data);
packet.build();
client.write(packet.as_slice())?
}
_ => {}
}
}
}
}
fn send_version(&mut self) -> SshResult<()> {
let mut client = util::client()?;
let config = util::config()?;
client.write_version(format!("{}\r\n", config.version.client_version).as_bytes())?;
log::info!("client version => {}", config.version.client_version);
Ok(())
}
fn receive_version(&mut self) -> SshResult<()> {
let mut client = util::client()?;
let vec = client.read_version();
let from_utf8 = util::from_utf8(vec)?;
let sv = from_utf8.trim();
log::info!("server version => {}", sv);
let mut config = util::config()?;
config.version.server_version = sv.to_string();
Ok(())
}
}
fn password_authentication(client: &mut MutexGuard<'static, Client>) -> SshResult<()> {
let config = util::config()?;
if config.user.username.is_empty() {
return Err(SshError::from(SshErrorKind::UserNullError))
}
if config.user.password.is_empty() {
return Err(SshError::from(SshErrorKind::PasswordNullError))
}
let mut data = Data::new();
data.put_u8(message::SSH_MSG_USERAUTH_REQUEST)
.put_str(config.user.username.as_str())
.put_str(strings::SSH_CONNECTION)
.put_str(strings::PASSWORD)
.put_u8(false as u8)
.put_str(config.user.password.as_str());
let mut packet = Packet::from(data);
packet.build();
client.write(packet.as_slice())
}