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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
//! SSH session management.
//!
//! This module provides SSH session handling with actual russh integration
//! when the `ssh` feature is enabled.
use std::sync::Arc;
use std::time::Duration;
use super::auth::{AuthMethod, HostKeyVerification, SshCredentials};
#[cfg(feature = "ssh")]
use crate::error::SshError;
/// SSH session configuration.
#[derive(Debug, Clone)]
pub struct SshConfig {
/// Host to connect to.
pub host: String,
/// Port (default 22).
pub port: u16,
/// Credentials.
pub credentials: SshCredentials,
/// Connection timeout.
pub connect_timeout: Duration,
/// Host key verification policy.
pub host_key_verification: HostKeyVerification,
/// Enable compression.
pub compression: bool,
/// TCP keepalive interval.
pub tcp_keepalive: Option<Duration>,
}
impl Default for SshConfig {
fn default() -> Self {
Self {
host: String::new(),
port: 22,
credentials: SshCredentials::default(),
connect_timeout: Duration::from_secs(30),
host_key_verification: HostKeyVerification::default(),
compression: false,
tcp_keepalive: Some(Duration::from_secs(60)),
}
}
}
impl SshConfig {
/// Create new config for a host.
#[must_use]
pub fn new(host: impl Into<String>) -> Self {
Self {
host: host.into(),
..Default::default()
}
}
/// Set port.
#[must_use]
pub const fn port(mut self, port: u16) -> Self {
self.port = port;
self
}
/// Set credentials.
#[must_use]
pub fn credentials(mut self, credentials: SshCredentials) -> Self {
self.credentials = credentials;
self
}
/// Set username.
#[must_use]
pub fn username(mut self, username: impl Into<String>) -> Self {
self.credentials.username = username.into();
self
}
/// Set connect timeout.
#[must_use]
pub const fn connect_timeout(mut self, timeout: Duration) -> Self {
self.connect_timeout = timeout;
self
}
/// Set host key verification.
#[must_use]
pub const fn host_key_verification(mut self, policy: HostKeyVerification) -> Self {
self.host_key_verification = policy;
self
}
/// Enable compression.
#[must_use]
pub const fn with_compression(mut self) -> Self {
self.compression = true;
self
}
/// Get the address string.
#[must_use]
pub fn address(&self) -> String {
format!("{}:{}", self.host, self.port)
}
}
/// SSH session state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SshSessionState {
/// Not connected.
Disconnected,
/// Connecting.
Connecting,
/// Authenticating.
Authenticating,
/// Connected and ready.
Connected,
/// Error state.
Error,
}
// ============================================================================
// russh integration (when ssh feature is enabled)
// ============================================================================
#[cfg(feature = "ssh")]
mod russh_impl {
use std::path::Path;
use russh::client;
use russh::keys::{PrivateKey, PrivateKeyWithHashAlg, PublicKey};
use super::{Arc, AuthMethod, HostKeyVerification, SshCredentials, SshError};
/// Client handler for russh that manages host key verification.
pub struct SshClientHandler {
/// Host key verification policy.
pub host_key_verification: HostKeyVerification,
/// The host we're connecting to.
pub host: String,
/// The port we're connecting to.
pub port: u16,
}
impl client::Handler for SshClientHandler {
type Error = russh::Error;
/// Verify the server's host key.
async fn check_server_key(
&mut self,
server_public_key: &PublicKey,
) -> Result<bool, Self::Error> {
match self.host_key_verification {
#[cfg(feature = "insecure-skip-verify")]
HostKeyVerification::AcceptAll => {
tracing::warn!(
host = %self.host,
"Accepting server key without verification (INSECURE)"
);
Ok(true)
}
HostKeyVerification::RejectUnknown => {
tracing::debug!(
host = %self.host,
key = ?server_public_key,
"Rejecting unknown host key"
);
Ok(false)
}
HostKeyVerification::KnownHosts => {
// Check against known_hosts file using russh-keys
check_known_hosts(&self.host, self.port, server_public_key)
}
HostKeyVerification::Tofu => {
// Trust on first use - accept and save to known_hosts
handle_tofu(&self.host, self.port, server_public_key)
}
}
}
}
/// Check a server key against the `known_hosts` file.
#[allow(clippy::unnecessary_wraps)]
fn check_known_hosts(
host: &str,
port: u16,
server_public_key: &PublicKey,
) -> Result<bool, russh::Error> {
let known_hosts_path = get_known_hosts_path();
if !known_hosts_path.exists() {
tracing::warn!(
host = %host,
path = %known_hosts_path.display(),
"known_hosts file not found, rejecting key"
);
return Ok(false);
}
// Read and parse the known_hosts file
let contents = match std::fs::read_to_string(&known_hosts_path) {
Ok(c) => c,
Err(e) => {
tracing::warn!(
host = %host,
error = %e,
"Failed to read known_hosts file"
);
return Ok(false);
}
};
// Build the host pattern to search for
// Standard SSH uses "host" for port 22, "[host]:port" for non-standard ports
let host_pattern = if port == 22 {
host.to_string()
} else {
format!("[{host}]:{port}")
};
// Parse each line looking for matching host entries
for line in contents.lines() {
let line = line.trim();
// Skip empty lines and comments
if line.is_empty() || line.starts_with('#') {
continue;
}
// Skip markers like @cert-authority and @revoked for now
if line.starts_with('@') {
continue;
}
// Parse: hostnames keytype base64key [comment]
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() < 3 {
continue;
}
let hostnames = parts[0];
let key_type = parts[1];
let key_data = parts[2];
// Check if this line matches our host
let host_matches = hostnames.split(',').any(|h| {
let h = h.trim();
h == host || h == host_pattern || h == format!("{host},*") || h == "*"
});
if !host_matches {
continue;
}
// Try to parse and compare the key
if let Some(stored_key) = parse_known_hosts_key(key_type, key_data) {
if keys_match(&stored_key, server_public_key) {
tracing::debug!(
host = %host,
"Host key verified against known_hosts"
);
return Ok(true);
}
// Key mismatch - potential MITM attack!
tracing::error!(
host = %host,
"HOST KEY MISMATCH! Possible man-in-the-middle attack!"
);
return Ok(false);
}
}
// Host not found in known_hosts
tracing::warn!(
host = %host,
"Host not found in known_hosts file"
);
Ok(false)
}
/// Parse a public key from `known_hosts` format.
fn parse_known_hosts_key(key_type: &str, key_data: &str) -> Option<PublicKey> {
// The key_type should match what's in the decoded data
// Common types: ssh-rsa, ssh-ed25519, ecdsa-sha2-nistp256, etc.
match key_type {
"ssh-ed25519"
| "ssh-rsa"
| "ecdsa-sha2-nistp256"
| "ecdsa-sha2-nistp384"
| "ecdsa-sha2-nistp521" => {
// Try to parse using russh-keys (takes base64 directly)
russh::keys::parse_public_key_base64(key_data).ok()
}
_ => {
tracing::debug!(key_type = %key_type, "Unknown key type in known_hosts");
None
}
}
}
/// Compare two public keys for equality.
fn keys_match(stored: &PublicKey, server: &PublicKey) -> bool {
// Compare the key fingerprints using SHA-256 (standard for OpenSSH)
use russh::keys::HashAlg;
stored.fingerprint(HashAlg::Sha256) == server.fingerprint(HashAlg::Sha256)
}
/// Handle Trust On First Use - accept and save the key.
#[allow(clippy::unnecessary_wraps)]
fn handle_tofu(
host: &str,
port: u16,
server_public_key: &PublicKey,
) -> Result<bool, russh::Error> {
let known_hosts_path = get_known_hosts_path();
// Create .ssh directory if it doesn't exist
if let Some(parent) = known_hosts_path.parent()
&& !parent.exists()
{
if let Err(e) = std::fs::create_dir_all(parent) {
tracing::warn!(
error = %e,
"Failed to create .ssh directory, accepting key without saving"
);
return Ok(true);
}
// Set proper permissions on Unix
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(parent, std::fs::Permissions::from_mode(0o700));
}
}
// Format the host entry
let host_entry = if port == 22 {
host.to_string()
} else {
format!("[{host}]:{port}")
};
// Get the key in OpenSSH format
let key_str = format_public_key_openssh(server_public_key);
// Append to known_hosts
let line = format!("{host_entry} {key_str}\n");
match std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&known_hosts_path)
{
Ok(mut file) => {
use std::io::Write;
if let Err(e) = file.write_all(line.as_bytes()) {
tracing::warn!(
error = %e,
"Failed to write to known_hosts, accepting key without saving"
);
} else {
tracing::info!(
host = %host,
path = %known_hosts_path.display(),
"Added host key to known_hosts (TOFU)"
);
// Set proper permissions on Unix
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(
&known_hosts_path,
std::fs::Permissions::from_mode(0o644),
);
}
}
}
Err(e) => {
tracing::warn!(
error = %e,
"Failed to open known_hosts for writing, accepting key without saving"
);
}
}
Ok(true)
}
/// Format a public key in OpenSSH format for `known_hosts`.
fn format_public_key_openssh(key: &PublicKey) -> String {
// Use the built-in to_openssh method which returns "key_type base64_data [comment]"
// For known_hosts we strip the comment portion
key.to_openssh()
.unwrap_or_else(|_| format!("{} <encoding-error>", key.algorithm().as_str()))
.split_whitespace()
.take(2)
.collect::<Vec<_>>()
.join(" ")
}
/// Get the default `known_hosts` path.
fn get_known_hosts_path() -> std::path::PathBuf {
// Check for custom path in environment
if let Ok(path) = std::env::var("SSH_KNOWN_HOSTS") {
return std::path::PathBuf::from(path);
}
// Use standard location
let home = std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.unwrap_or_else(|_| ".".to_string());
std::path::PathBuf::from(home)
.join(".ssh")
.join("known_hosts")
}
/// Load a private key from a file.
///
/// Supports both unencrypted and encrypted private keys. For encrypted keys,
/// provide the passphrase to decrypt the key.
///
/// # Supported Formats
///
/// - OpenSSH format (both encrypted and unencrypted)
/// - PKCS#8 format (both encrypted and unencrypted)
/// - PEM format
///
/// # Example
///
/// ```ignore
/// // Load an unencrypted key
/// let key = load_private_key(Path::new("~/.ssh/id_ed25519"), None).await?;
///
/// // Load an encrypted key with passphrase
/// let key = load_private_key(
/// Path::new("~/.ssh/id_rsa"),
/// Some("my_passphrase"),
/// ).await?;
/// ```
pub async fn load_private_key(
path: &Path,
passphrase: Option<&str>,
) -> crate::error::Result<Arc<PrivateKey>> {
let key_data = tokio::fs::read(path).await.map_err(|e| {
crate::error::ExpectError::Ssh(SshError::Authentication {
user: String::new(),
reason: format!("Failed to read key file {}: {}", path.display(), e),
})
})?;
// Convert bytes to string for decode_secret_key
let key_str = String::from_utf8(key_data).map_err(|e| {
crate::error::ExpectError::Ssh(SshError::Authentication {
user: String::new(),
reason: format!("Key file {} is not valid UTF-8: {}", path.display(), e),
})
})?;
// Use russh::keys::decode_secret_key which handles both encrypted and unencrypted keys
let key = russh::keys::decode_secret_key(&key_str, passphrase).map_err(|e| {
let reason = if passphrase.is_none() && e.to_string().contains("encrypted") {
format!(
"Key {} appears to be encrypted but no passphrase was provided. \
Use AuthMethod::public_key_with_passphrase() to specify a passphrase.",
path.display()
)
} else {
format!("Failed to decode key {}: {}", path.display(), e)
};
crate::error::ExpectError::Ssh(SshError::Authentication {
user: String::new(),
reason,
})
})?;
Ok(Arc::new(key))
}
/// Authenticate using the configured methods.
#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
pub async fn authenticate(
handle: &mut client::Handle<SshClientHandler>,
credentials: &SshCredentials,
) -> crate::error::Result<bool> {
let username = &credentials.username;
for method in &credentials.auth_methods {
match method {
AuthMethod::Password(password) => {
tracing::debug!(user = %username, "Attempting password authentication");
match handle.authenticate_password(username, password).await {
Ok(auth_result) if auth_result.success() => {
tracing::info!(user = %username, "Password authentication successful");
return Ok(true);
}
Ok(_) => {
tracing::debug!(user = %username, "Password authentication failed");
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Password authentication error"
);
}
}
}
AuthMethod::PublicKey {
private_key,
passphrase,
} => {
tracing::debug!(
user = %username,
key = %private_key.display(),
"Attempting public key authentication"
);
match load_private_key(private_key, passphrase.as_deref()).await {
Ok(key) => {
// Get the best supported RSA hash algorithm if applicable
// best_supported_rsa_hash returns Result<Option<Option<HashAlg>>, _>
let rsa_hash = handle
.best_supported_rsa_hash()
.await
.ok()
.flatten()
.flatten();
let key_with_hash = PrivateKeyWithHashAlg::new(key, rsa_hash);
match handle.authenticate_publickey(username, key_with_hash).await {
Ok(auth_result) if auth_result.success() => {
tracing::info!(
user = %username,
"Public key authentication successful"
);
return Ok(true);
}
Ok(_) => {
tracing::debug!(
user = %username,
"Public key authentication failed"
);
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Public key authentication error"
);
}
}
}
Err(e) => {
tracing::debug!(
user = %username,
key = %private_key.display(),
error = %e,
"Failed to load private key"
);
}
}
}
AuthMethod::Agent => {
tracing::debug!(user = %username, "Attempting SSH agent authentication");
// Connect to the SSH agent
#[cfg(unix)]
match russh::keys::agent::client::AgentClient::connect_env().await {
Ok(mut agent) => {
// Get list of keys from agent
match agent.request_identities().await {
Ok(keys) => {
tracing::debug!(
user = %username,
key_count = keys.len(),
"Found keys in SSH agent"
);
// Try each key from the agent
for key in keys {
// Get the best supported RSA hash algorithm if applicable
let rsa_hash = handle
.best_supported_rsa_hash()
.await
.ok()
.flatten()
.flatten();
match handle
.authenticate_publickey_with(
username,
key.clone(),
rsa_hash,
&mut agent,
)
.await
{
Ok(auth_result) if auth_result.success() => {
tracing::info!(
user = %username,
key_type = %key.algorithm().as_str(),
"SSH agent authentication successful"
);
return Ok(true);
}
Ok(_) => {
tracing::debug!(
user = %username,
key_type = %key.algorithm().as_str(),
"SSH agent key rejected, trying next"
);
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"SSH agent authentication error"
);
}
}
}
tracing::debug!(
user = %username,
"All SSH agent keys exhausted"
);
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Failed to get identities from SSH agent"
);
}
}
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Failed to connect to SSH agent"
);
}
}
// Windows: Try Pageant first, then OpenSSH agent via named pipe
#[cfg(windows)]
{
// Try Pageant first (PuTTY SSH agent)
tracing::debug!(user = %username, "Trying Pageant SSH agent");
match russh::keys::agent::client::AgentClient::connect_pageant().await {
Ok(mut agent) => match agent.request_identities().await {
Ok(keys) => {
tracing::debug!(
user = %username,
key_count = keys.len(),
"Found keys in Pageant"
);
for key in keys {
let rsa_hash = handle
.best_supported_rsa_hash()
.await
.ok()
.flatten()
.flatten();
match handle
.authenticate_publickey_with(
username,
key.clone(),
rsa_hash,
&mut agent,
)
.await
{
Ok(auth_result) if auth_result.success() => {
tracing::info!(
user = %username,
key_type = %key.algorithm().as_str(),
"Pageant authentication successful"
);
return Ok(true);
}
Ok(_) => {
tracing::debug!(
user = %username,
key_type = %key.algorithm().as_str(),
"Pageant key rejected, trying next"
);
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Pageant authentication error"
);
}
}
}
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Failed to get identities from Pageant, trying OpenSSH agent"
);
}
},
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Pageant not available, trying OpenSSH agent"
);
}
}
// Try Windows OpenSSH agent via named pipe
const OPENSSH_AGENT_PIPE: &str = r"\\.\pipe\openssh-ssh-agent";
tracing::debug!(
user = %username,
pipe = OPENSSH_AGENT_PIPE,
"Trying OpenSSH agent via named pipe"
);
match russh::keys::agent::client::AgentClient::connect_named_pipe(
OPENSSH_AGENT_PIPE,
)
.await
{
Ok(mut agent) => match agent.request_identities().await {
Ok(keys) => {
tracing::debug!(
user = %username,
key_count = keys.len(),
"Found keys in OpenSSH agent"
);
for key in keys {
let rsa_hash = handle
.best_supported_rsa_hash()
.await
.ok()
.flatten()
.flatten();
match handle
.authenticate_publickey_with(
username,
key.clone(),
rsa_hash,
&mut agent,
)
.await
{
Ok(auth_result) if auth_result.success() => {
tracing::info!(
user = %username,
key_type = %key.algorithm().as_str(),
"OpenSSH agent authentication successful"
);
return Ok(true);
}
Ok(_) => {
tracing::debug!(
user = %username,
key_type = %key.algorithm().as_str(),
"OpenSSH agent key rejected, trying next"
);
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"OpenSSH agent authentication error"
);
}
}
}
tracing::debug!(
user = %username,
"All OpenSSH agent keys exhausted"
);
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Failed to get identities from OpenSSH agent"
);
}
},
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Failed to connect to OpenSSH agent"
);
}
}
}
#[cfg(not(any(unix, windows)))]
{
tracing::debug!(
user = %username,
"SSH agent authentication not supported on this platform"
);
}
}
AuthMethod::KeyboardInteractive { responses } => {
tracing::debug!(
user = %username,
response_count = responses.len(),
"Attempting keyboard-interactive authentication"
);
// Start the keyboard-interactive authentication
match handle
.authenticate_keyboard_interactive_start(username.clone(), None)
.await
{
Ok(auth_response) => {
use russh::client::KeyboardInteractiveAuthResponse;
let mut current_response = auth_response;
let mut response_index = 0;
// Loop to handle multiple rounds of prompts
loop {
match current_response {
KeyboardInteractiveAuthResponse::Success => {
tracing::info!(
user = %username,
"Keyboard-interactive authentication successful"
);
return Ok(true);
}
KeyboardInteractiveAuthResponse::Failure {
remaining_methods,
partial_success,
} => {
tracing::debug!(
user = %username,
partial_success = partial_success,
remaining = ?remaining_methods,
"Keyboard-interactive authentication failed"
);
break; // Try next auth method
}
KeyboardInteractiveAuthResponse::InfoRequest {
name,
instructions,
prompts,
} => {
tracing::debug!(
user = %username,
name = %name,
instructions = %instructions,
prompt_count = prompts.len(),
"Received keyboard-interactive prompts"
);
// Build responses for the prompts
let mut prompt_responses =
Vec::with_capacity(prompts.len());
for prompt in &prompts {
let response = if response_index < responses.len() {
responses[response_index].clone()
} else {
tracing::warn!(
user = %username,
prompt = %prompt.prompt,
"No response available for prompt, using empty string"
);
String::new()
};
prompt_responses.push(response);
response_index += 1;
}
// Send responses
match handle
.authenticate_keyboard_interactive_respond(
prompt_responses,
)
.await
{
Ok(next_response) => {
current_response = next_response;
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Keyboard-interactive response error"
);
break; // Try next auth method
}
}
}
}
}
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"Keyboard-interactive start error"
);
}
}
}
AuthMethod::None => {
tracing::debug!(user = %username, "Attempting none authentication");
match handle.authenticate_none(username).await {
Ok(auth_result) if auth_result.success() => {
tracing::info!(user = %username, "None authentication successful");
return Ok(true);
}
Ok(_) => {
tracing::debug!(user = %username, "None authentication failed");
}
Err(e) => {
tracing::debug!(
user = %username,
error = %e,
"None authentication error"
);
}
}
}
}
}
Err(crate::error::ExpectError::Ssh(SshError::Authentication {
user: username.clone(),
reason: "All authentication methods exhausted".to_string(),
}))
}
}
/// SSH session with russh integration.
///
/// When the `ssh` feature is enabled, this provides a full SSH client
/// implementation using the russh library.
#[cfg(feature = "ssh")]
pub struct SshSession {
/// Configuration.
config: SshConfig,
/// Current state.
state: SshSessionState,
/// The russh client handle (when connected).
handle: Option<russh::client::Handle<russh_impl::SshClientHandler>>,
}
#[cfg(feature = "ssh")]
impl std::fmt::Debug for SshSession {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SshSession")
.field("config", &self.config)
.field("state", &self.state)
.field("connected", &self.handle.is_some())
.finish()
}
}
#[cfg(feature = "ssh")]
impl SshSession {
/// Create a new session.
#[must_use]
pub const fn new(config: SshConfig) -> Self {
Self {
config,
state: SshSessionState::Disconnected,
handle: None,
}
}
/// Get configuration.
#[must_use]
pub const fn config(&self) -> &SshConfig {
&self.config
}
/// Get current state.
#[must_use]
pub const fn state(&self) -> SshSessionState {
self.state
}
/// Check if connected.
#[must_use]
pub fn is_connected(&self) -> bool {
self.state == SshSessionState::Connected && self.handle.is_some()
}
/// Connect to the SSH server asynchronously.
///
/// This establishes a TCP connection, performs the SSH handshake,
/// and authenticates using the configured credentials.
///
/// # Errors
///
/// Returns an error if:
/// - The TCP connection fails
/// - Host key verification fails
/// - All authentication methods are exhausted
pub async fn connect_async(&mut self) -> crate::error::Result<()> {
self.state = SshSessionState::Connecting;
// Create the russh client config
let ssh_config = Arc::new(russh::client::Config::default());
// Create the handler
let handler = russh_impl::SshClientHandler {
host_key_verification: self.config.host_key_verification,
host: self.config.host.clone(),
port: self.config.port,
};
// Connect to the server
let addr = (self.config.host.as_str(), self.config.port);
tracing::info!(
host = %self.config.host,
port = %self.config.port,
"Connecting to SSH server"
);
let mut handle = tokio::time::timeout(
self.config.connect_timeout,
russh::client::connect(ssh_config, addr, handler),
)
.await
.map_err(|_| {
self.state = SshSessionState::Error;
crate::error::ExpectError::Ssh(SshError::Timeout {
duration: self.config.connect_timeout,
})
})?
.map_err(|e| {
self.state = SshSessionState::Error;
crate::error::ExpectError::Ssh(SshError::Connection {
host: self.config.host.clone(),
port: self.config.port,
reason: e.to_string(),
})
})?;
// Authenticate
self.state = SshSessionState::Authenticating;
tracing::debug!(
user = %self.config.credentials.username,
"Authenticating with SSH server"
);
russh_impl::authenticate(&mut handle, &self.config.credentials).await?;
// Success!
self.state = SshSessionState::Connected;
self.handle = Some(handle);
tracing::info!(
host = %self.config.host,
user = %self.config.credentials.username,
"SSH connection established"
);
Ok(())
}
/// Connect synchronously by blocking on the async connection.
///
/// This is a convenience method that uses the current tokio runtime
/// to block on the async connection. Prefer `connect_async` when possible.
///
/// # Errors
///
/// Returns an error if the connection or authentication fails.
///
/// # Panics
///
/// Panics if called outside of a tokio runtime context.
pub fn connect(&mut self) -> crate::error::Result<()> {
// Try to get the current runtime handle
if let Ok(handle) = tokio::runtime::Handle::try_current() {
// We're in an async context, use block_in_place
tokio::task::block_in_place(|| handle.block_on(self.connect_async()))
} else {
// No runtime, create a temporary one
let rt = tokio::runtime::Runtime::new().map_err(|e| {
crate::error::ExpectError::Ssh(SshError::Session {
reason: format!("Failed to create runtime: {e}"),
})
})?;
rt.block_on(self.connect_async())
}
}
/// Disconnect from the SSH server.
pub fn disconnect(&mut self) {
if let Some(handle) = self.handle.take() {
// Attempt graceful disconnect
let _ = tokio::runtime::Handle::try_current().map(|rt| {
tokio::task::block_in_place(|| {
rt.block_on(async {
let _ = handle
.disconnect(russh::Disconnect::ByApplication, "", "en")
.await;
});
});
});
}
self.state = SshSessionState::Disconnected;
}
/// Get a reference to the russh handle.
///
/// This is useful for advanced operations like opening channels.
#[must_use]
pub const fn handle(&self) -> Option<&russh::client::Handle<russh_impl::SshClientHandler>> {
self.handle.as_ref()
}
/// Get a mutable reference to the russh handle.
pub const fn handle_mut(
&mut self,
) -> Option<&mut russh::client::Handle<russh_impl::SshClientHandler>> {
self.handle.as_mut()
}
/// Open a session channel.
///
/// This opens a new SSH channel that can be used for executing commands
/// or starting an interactive shell.
///
/// # Errors
///
/// Returns an error if the session is not connected or channel opening fails.
pub async fn open_channel(
&mut self,
) -> crate::error::Result<russh::Channel<russh::client::Msg>> {
let handle = self.handle.as_mut().ok_or_else(|| {
crate::error::ExpectError::Ssh(SshError::Session {
reason: "Not connected".to_string(),
})
})?;
let channel = handle.channel_open_session().await.map_err(|e| {
crate::error::ExpectError::Ssh(SshError::Channel {
reason: e.to_string(),
})
})?;
Ok(channel)
}
/// Open an interactive shell session with a PTY.
///
/// This is a convenience method that opens a channel, requests a PTY,
/// and starts a shell, returning a stream that implements `AsyncRead` and `AsyncWrite`.
///
/// # Example
///
/// ```ignore
/// use rust_expect::backend::ssh::{SshSession, SshConfig, SshCredentials};
/// use tokio::io::{AsyncReadExt, AsyncWriteExt};
///
/// let config = SshConfig::new("example.com")
/// .username("user")
/// .credentials(SshCredentials::new("user").with_password("pass"));
///
/// let mut session = SshSession::new(config);
/// session.connect_async().await?;
///
/// let mut shell = session.shell().await?;
/// shell.write_all(b"ls -la\n").await?;
/// ```
///
/// # Errors
///
/// Returns an error if the session is not connected, channel opening fails,
/// PTY request fails, or shell request fails.
pub async fn shell(&mut self) -> crate::error::Result<super::channel::SshChannelStream> {
self.shell_with_config(super::channel::ChannelConfig::default())
.await
}
/// Open an interactive shell session with custom configuration.
///
/// # Errors
///
/// Returns an error if the session is not connected, channel opening fails,
/// PTY request fails (if enabled), or shell request fails.
pub async fn shell_with_config(
&mut self,
config: super::channel::ChannelConfig,
) -> crate::error::Result<super::channel::SshChannelStream> {
let channel = self.open_channel().await?;
let mut stream = super::channel::SshChannelStream::new(channel, config);
// Request PTY if configured
if stream.config().pty {
stream.request_pty().await?;
}
// Request shell
stream.request_shell().await?;
Ok(stream)
}
/// Execute a command and return a stream for reading output.
///
/// This opens a channel, optionally requests a PTY, and executes the specified command.
/// The returned stream can be used to read command output and write stdin.
///
/// # Example
///
/// ```ignore
/// let mut exec = session.exec("uname -a").await?;
/// let mut output = String::new();
/// exec.read_to_string(&mut output).await?;
/// println!("Output: {}", output);
/// ```
///
/// # Errors
///
/// Returns an error if the session is not connected or command execution fails.
pub async fn exec(
&mut self,
command: &str,
) -> crate::error::Result<super::channel::SshChannelStream> {
self.exec_with_config(command, super::channel::ChannelConfig::default().no_pty())
.await
}
/// Execute a command with custom configuration.
///
/// # Errors
///
/// Returns an error if the session is not connected or command execution fails.
pub async fn exec_with_config(
&mut self,
command: &str,
config: super::channel::ChannelConfig,
) -> crate::error::Result<super::channel::SshChannelStream> {
let channel = self.open_channel().await?;
let mut stream = super::channel::SshChannelStream::new(channel, config);
// Request PTY if configured
if stream.config().pty {
stream.request_pty().await?;
}
// Execute command
stream.exec(command).await?;
Ok(stream)
}
}
#[cfg(feature = "ssh")]
impl Drop for SshSession {
fn drop(&mut self) {
self.disconnect();
}
}
// ============================================================================
// Stub implementation (when ssh feature is disabled)
// ============================================================================
/// SSH session stub for when the `ssh` feature is disabled.
///
/// This provides API compatibility but operations will fail at runtime.
#[cfg(not(feature = "ssh"))]
#[derive(Debug)]
pub struct SshSession {
/// Configuration.
config: SshConfig,
/// Current state.
state: SshSessionState,
}
#[cfg(not(feature = "ssh"))]
impl SshSession {
/// Create a new session.
#[must_use]
pub const fn new(config: SshConfig) -> Self {
Self {
config,
state: SshSessionState::Disconnected,
}
}
/// Get configuration.
#[must_use]
pub const fn config(&self) -> &SshConfig {
&self.config
}
/// Get current state.
#[must_use]
pub const fn state(&self) -> SshSessionState {
self.state
}
/// Check if connected.
#[must_use]
pub fn is_connected(&self) -> bool {
self.state == SshSessionState::Connected
}
/// Connect (stub - always succeeds for API compatibility in tests).
pub fn connect(&mut self) -> crate::error::Result<()> {
self.state = SshSessionState::Connected;
Ok(())
}
/// Disconnect.
pub fn disconnect(&mut self) {
self.state = SshSessionState::Disconnected;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ssh_config_builder() {
let config = SshConfig::new("example.com")
.port(2222)
.username("admin")
.with_compression();
assert_eq!(config.host, "example.com");
assert_eq!(config.port, 2222);
assert!(config.compression);
assert_eq!(config.address(), "example.com:2222");
}
#[test]
fn ssh_session_state() {
let mut session = SshSession::new(SshConfig::new("host"));
assert_eq!(session.state(), SshSessionState::Disconnected);
// Note: In the stub implementation, connect() succeeds
// In the real implementation, it would fail without a server
#[cfg(not(feature = "ssh"))]
{
session.connect().unwrap();
assert!(session.is_connected());
}
session.disconnect();
assert!(!session.is_connected());
}
#[test]
fn ssh_config_defaults() {
let config = SshConfig::default();
assert_eq!(config.port, 22);
assert_eq!(config.connect_timeout, Duration::from_secs(30));
assert!(!config.compression);
assert_eq!(
config.host_key_verification,
HostKeyVerification::KnownHosts
);
}
}