-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
russh_handler.rs
495 lines (434 loc) · 13.8 KB
/
russh_handler.rs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::fmt::Debug;
use async_trait::async_trait;
use bytes::Bytes;
use russh::keys::key::PublicKey;
use russh::server::{Auth, Handle, Msg, Session};
use russh::{Channel, ChannelId, Pty, Sig};
use tokio::sync::mpsc::UnboundedSender;
use tokio::sync::oneshot;
use tracing::*;
use warpgate_common::Secret;
use crate::common::{PtyRequest, ServerChannelId};
use crate::{DirectTCPIPParams, X11Request};
pub struct HandleWrapper(pub Handle);
impl Debug for HandleWrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "HandleWrapper")
}
}
#[derive(Debug)]
pub enum ServerHandlerEvent {
Authenticated(HandleWrapper),
ChannelOpenSession(ServerChannelId, oneshot::Sender<bool>),
SubsystemRequest(ServerChannelId, String, oneshot::Sender<bool>),
PtyRequest(ServerChannelId, PtyRequest, oneshot::Sender<()>),
ShellRequest(ServerChannelId, oneshot::Sender<bool>),
AuthPublicKey(Secret<String>, PublicKey, oneshot::Sender<Auth>),
AuthPublicKeyOffer(Secret<String>, PublicKey, oneshot::Sender<Auth>),
AuthPassword(Secret<String>, Secret<String>, oneshot::Sender<Auth>),
AuthKeyboardInteractive(
Secret<String>,
Option<Secret<String>>,
oneshot::Sender<Auth>,
),
Data(ServerChannelId, Bytes, oneshot::Sender<()>),
ExtendedData(ServerChannelId, Bytes, u32, oneshot::Sender<()>),
ChannelClose(ServerChannelId, oneshot::Sender<()>),
ChannelEof(ServerChannelId, oneshot::Sender<()>),
WindowChangeRequest(ServerChannelId, PtyRequest, oneshot::Sender<()>),
Signal(ServerChannelId, Sig, oneshot::Sender<()>),
ExecRequest(ServerChannelId, Bytes, oneshot::Sender<bool>),
ChannelOpenDirectTcpIp(ServerChannelId, DirectTCPIPParams, oneshot::Sender<bool>),
EnvRequest(ServerChannelId, String, String, oneshot::Sender<()>),
X11Request(ServerChannelId, X11Request, oneshot::Sender<()>),
TcpIpForward(String, u32, oneshot::Sender<bool>),
CancelTcpIpForward(String, u32, oneshot::Sender<bool>),
Disconnect,
}
pub struct ServerHandler {
pub event_tx: UnboundedSender<ServerHandlerEvent>,
}
#[derive(thiserror::Error, Debug)]
pub enum ServerHandlerError {
#[error("channel disconnected")]
ChannelSend,
}
impl ServerHandler {
fn send_event(&self, event: ServerHandlerEvent) -> Result<(), ServerHandlerError> {
self.event_tx
.send(event)
.map_err(|_| ServerHandlerError::ChannelSend)
}
}
#[async_trait]
impl russh::server::Handler for ServerHandler {
type Error = anyhow::Error;
async fn auth_succeeded(&mut self, session: &mut Session) -> Result<(), Self::Error> {
let handle = session.handle();
self.send_event(ServerHandlerEvent::Authenticated(HandleWrapper(handle)))?;
Ok(())
}
async fn channel_open_session(
&mut self,
channel: Channel<Msg>,
_session: &mut Session,
) -> Result<bool, Self::Error> {
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::ChannelOpenSession(
ServerChannelId(channel.id()),
tx,
))?;
let allowed = rx.await.unwrap_or(false);
Ok(allowed)
}
async fn subsystem_request(
&mut self,
channel: ChannelId,
name: &str,
session: &mut Session,
) -> Result<(), Self::Error> {
let name = name.to_string();
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::SubsystemRequest(
ServerChannelId(channel),
name,
tx,
))?;
if rx.await.unwrap_or(false) {
session.channel_success(channel)
} else {
session.channel_failure(channel)
}
Ok(())
}
async fn pty_request(
&mut self,
channel: ChannelId,
term: &str,
col_width: u32,
row_height: u32,
pix_width: u32,
pix_height: u32,
modes: &[(Pty, u32)],
session: &mut Session,
) -> Result<(), Self::Error> {
let term = term.to_string();
let modes = modes
.iter()
.take_while(|x| (x.0 as u8) > 0 && (x.0 as u8) < 160)
.map(Clone::clone)
.collect();
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::PtyRequest(
ServerChannelId(channel),
PtyRequest {
term,
col_width,
row_height,
pix_width,
pix_height,
modes,
},
tx,
))?;
let _ = rx.await;
session.channel_success(channel);
Ok(())
}
async fn shell_request(
&mut self,
channel: ChannelId,
session: &mut Session,
) -> Result<(), Self::Error> {
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::ShellRequest(
ServerChannelId(channel),
tx,
))?;
if rx.await.unwrap_or(false) {
session.channel_success(channel)
} else {
session.channel_failure(channel)
}
Ok(())
}
async fn auth_publickey_offered(
&mut self,
user: &str,
key: &russh::keys::key::PublicKey,
) -> Result<Auth, Self::Error> {
let user = Secret::new(user.to_string());
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::AuthPublicKeyOffer(
user,
key.clone(),
tx,
))?;
Ok(rx.await.unwrap_or(Auth::Reject {
proceed_with_methods: None,
}))
}
async fn auth_publickey(
&mut self,
user: &str,
key: &russh::keys::key::PublicKey,
) -> Result<Auth, Self::Error> {
let user = Secret::new(user.to_string());
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::AuthPublicKey(user, key.clone(), tx))?;
let result = rx.await.unwrap_or(Auth::UnsupportedMethod);
Ok(result)
}
async fn auth_password(&mut self, user: &str, password: &str) -> Result<Auth, Self::Error> {
let user = Secret::new(user.to_string());
let password = Secret::new(password.to_string());
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::AuthPassword(user, password, tx))?;
let result = rx.await.unwrap_or(Auth::UnsupportedMethod);
Ok(result)
}
async fn auth_keyboard_interactive(
&mut self,
user: &str,
_submethods: &str,
response: Option<russh::server::Response<'async_trait>>,
) -> Result<Auth, Self::Error> {
let user = Secret::new(user.to_string());
let response = response
.and_then(|mut r| r.next())
.and_then(|b| String::from_utf8(b.to_vec()).ok())
.map(Secret::new);
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::AuthKeyboardInteractive(
user, response, tx,
))?;
let result = rx.await.unwrap_or(Auth::UnsupportedMethod);
Ok(result)
}
async fn data(
&mut self,
channel: ChannelId,
data: &[u8],
_session: &mut Session,
) -> Result<(), Self::Error> {
let channel = ServerChannelId(channel);
let data = Bytes::from(data.to_vec());
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::Data(channel, data, tx))?;
let _ = rx.await;
Ok(())
}
async fn extended_data(
&mut self,
channel: ChannelId,
code: u32,
data: &[u8],
_session: &mut Session,
) -> Result<(), Self::Error> {
let channel = ServerChannelId(channel);
let data = Bytes::from(data.to_vec());
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::ExtendedData(channel, data, code, tx))?;
let _ = rx.await;
Ok(())
}
async fn channel_close(
&mut self,
channel: ChannelId,
_session: &mut Session,
) -> Result<(), Self::Error> {
let channel = ServerChannelId(channel);
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::ChannelClose(channel, tx))?;
let _ = rx.await;
Ok(())
}
async fn window_change_request(
&mut self,
channel: ChannelId,
col_width: u32,
row_height: u32,
pix_width: u32,
pix_height: u32,
_session: &mut Session,
) -> Result<(), Self::Error> {
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::WindowChangeRequest(
ServerChannelId(channel),
PtyRequest {
term: "".to_string(),
col_width,
row_height,
pix_width,
pix_height,
modes: vec![],
},
tx,
))?;
let _ = rx.await;
Ok(())
}
async fn channel_eof(
&mut self,
channel: ChannelId,
_session: &mut Session,
) -> Result<(), Self::Error> {
let channel = ServerChannelId(channel);
let (tx, rx) = oneshot::channel();
self.event_tx
.send(ServerHandlerEvent::ChannelEof(channel, tx))
.map_err(|_| ServerHandlerError::ChannelSend)?;
let _ = rx.await;
Ok(())
}
async fn signal(
&mut self,
channel: ChannelId,
signal_name: russh::Sig,
_session: &mut Session,
) -> Result<(), Self::Error> {
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::Signal(
ServerChannelId(channel),
signal_name,
tx,
))?;
let _ = rx.await;
Ok(())
}
async fn exec_request(
&mut self,
channel: ChannelId,
data: &[u8],
session: &mut Session,
) -> Result<(), Self::Error> {
let data = Bytes::from(data.to_vec());
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::ExecRequest(
ServerChannelId(channel),
data,
tx,
))?;
if rx.await.unwrap_or(false) {
session.channel_success(channel)
} else {
session.channel_failure(channel)
}
Ok(())
}
async fn env_request(
&mut self,
channel: ChannelId,
variable_name: &str,
variable_value: &str,
_session: &mut Session,
) -> Result<(), Self::Error> {
let variable_name = variable_name.to_string();
let variable_value = variable_value.to_string();
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::EnvRequest(
ServerChannelId(channel),
variable_name,
variable_value,
tx,
))?;
let _ = rx.await;
Ok(())
}
async fn channel_open_direct_tcpip(
&mut self,
channel: Channel<Msg>,
host_to_connect: &str,
port_to_connect: u32,
originator_address: &str,
originator_port: u32,
_session: &mut Session,
) -> Result<bool, Self::Error> {
let host_to_connect = host_to_connect.to_string();
let originator_address = originator_address.to_string();
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::ChannelOpenDirectTcpIp(
ServerChannelId(channel.id()),
DirectTCPIPParams {
host_to_connect,
port_to_connect,
originator_address,
originator_port,
},
tx,
))?;
let allowed = rx.await.unwrap_or(false);
Ok(allowed)
}
async fn x11_request(
&mut self,
channel: ChannelId,
single_conection: bool,
x11_auth_protocol: &str,
x11_auth_cookie: &str,
x11_screen_number: u32,
_session: &mut Session,
) -> Result<(), Self::Error> {
let x11_auth_protocol = x11_auth_protocol.to_string();
let x11_auth_cookie = x11_auth_cookie.to_string();
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::X11Request(
ServerChannelId(channel),
X11Request {
single_conection,
x11_auth_protocol,
x11_auth_cookie,
x11_screen_number,
},
tx,
))?;
let _ = rx.await;
Ok(())
}
async fn tcpip_forward(
&mut self,
address: &str,
port: &mut u32,
session: &mut Session,
) -> Result<bool, Self::Error> {
let address = address.to_string();
let port = *port;
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::TcpIpForward(address, port, tx))?;
let allowed = rx.await.unwrap_or(false);
if allowed {
session.request_success()
} else {
session.request_failure()
}
Ok(allowed)
}
async fn cancel_tcpip_forward(
&mut self,
address: &str,
port: u32,
session: &mut Session,
) -> Result<bool, Self::Error> {
let address = address.to_string();
let (tx, rx) = oneshot::channel();
self.send_event(ServerHandlerEvent::CancelTcpIpForward(address, port, tx))?;
let allowed = rx.await.unwrap_or(false);
if allowed {
session.request_success()
} else {
session.request_failure()
}
Ok(allowed)
}
}
impl Drop for ServerHandler {
fn drop(&mut self) {
debug!("Dropped");
let _ = self.event_tx.send(ServerHandlerEvent::Disconnect);
}
}
impl Debug for ServerHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ServerHandler")
}
}