bssh 0.7.0

Parallel SSH command execution tool for cluster management
Documentation
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
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! SSH configuration parsing functionality
//!
//! This module handles parsing of SSH configuration files, converting the text format
//! into structured configuration objects while performing security validation.

use super::security::{secure_validate_path, validate_control_path, validate_executable_string};
use super::types::SshHostConfig;
use anyhow::{Context, Result};

/// Parse SSH configuration content
pub(super) fn parse(content: &str) -> Result<Vec<SshHostConfig>> {
    let mut hosts = Vec::new();
    let mut current_host: Option<SshHostConfig> = None;
    let mut line_number = 0;

    for line in content.lines() {
        line_number += 1;
        let line = line.trim();

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // Split line into keyword and arguments
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.is_empty() {
            continue;
        }

        let keyword = parts[0].to_lowercase();
        let args = &parts[1..];

        match keyword.as_str() {
            "host" => {
                // Save previous host config
                if let Some(host) = current_host.take() {
                    hosts.push(host);
                }

                // Start new host config
                if args.is_empty() {
                    anyhow::bail!(
                        "Host directive requires at least one pattern at line {}",
                        line_number
                    );
                }

                let host_config = SshHostConfig {
                    host_patterns: args.iter().map(|s| s.to_string()).collect(),
                    ..Default::default()
                };
                current_host = Some(host_config);
            }
            _ => {
                // Configuration option
                if let Some(ref mut host) = current_host {
                    parse_option(host, &keyword, args, line_number)
                        .with_context(|| format!("Error at line {line_number}: {line}"))?;
                } else if keyword != "host" {
                    // Global options outside of any Host block are ignored for now
                    // In a full SSH config parser, these would set global defaults
                    tracing::debug!(
                        "Ignoring global option '{}' at line {}",
                        keyword,
                        line_number
                    );
                }
            }
        }
    }

    // Don't forget the last host
    if let Some(host) = current_host {
        hosts.push(host);
    }

    Ok(hosts)
}

/// Parse a configuration option for a host
pub(super) fn parse_option(
    host: &mut SshHostConfig,
    keyword: &str,
    args: &[&str],
    line_number: usize,
) -> Result<()> {
    match keyword {
        "hostname" => {
            if args.is_empty() {
                anyhow::bail!("HostName requires a value at line {}", line_number);
            }
            host.hostname = Some(args[0].to_string());
        }
        "user" => {
            if args.is_empty() {
                anyhow::bail!("User requires a value at line {}", line_number);
            }
            host.user = Some(args[0].to_string());
        }
        "port" => {
            if args.is_empty() {
                anyhow::bail!("Port requires a value at line {}", line_number);
            }
            let port: u16 = args[0].parse().with_context(|| {
                format!("Invalid port number '{}' at line {}", args[0], line_number)
            })?;
            host.port = Some(port);
        }
        "identityfile" => {
            if args.is_empty() {
                anyhow::bail!("IdentityFile requires a value at line {}", line_number);
            }
            let path = secure_validate_path(args[0], "identity", line_number)
                .with_context(|| format!("Invalid IdentityFile path at line {line_number}"))?;
            host.identity_files.push(path);
        }
        "identitiesonly" => {
            if args.is_empty() {
                anyhow::bail!("IdentitiesOnly requires a value at line {}", line_number);
            }
            // Parse yes/no and store in identity_files behavior (implicit)
            let value = parse_yes_no(args[0], line_number)?;
            if value {
                // When IdentitiesOnly is yes, clear default identity files
                // This is handled during resolution
            }
        }
        "proxyjump" => {
            if args.is_empty() {
                anyhow::bail!("ProxyJump requires a value at line {}", line_number);
            }
            host.proxy_jump = Some(args.join(" "));
        }
        "proxycommand" => {
            if args.is_empty() {
                anyhow::bail!("ProxyCommand requires a value at line {}", line_number);
            }
            let command = args.join(" ");
            validate_executable_string(&command, "ProxyCommand", line_number)?;
            host.proxy_command = Some(command);
        }
        "stricthostkeychecking" => {
            if args.is_empty() {
                anyhow::bail!(
                    "StrictHostKeyChecking requires a value at line {}",
                    line_number
                );
            }
            host.strict_host_key_checking = Some(args[0].to_string());
        }
        "userknownhostsfile" => {
            if args.is_empty() {
                anyhow::bail!(
                    "UserKnownHostsFile requires a value at line {}",
                    line_number
                );
            }
            let path =
                secure_validate_path(args[0], "known_hosts", line_number).with_context(|| {
                    format!("Invalid UserKnownHostsFile path at line {line_number}")
                })?;
            host.user_known_hosts_file = Some(path);
        }
        "globalknownhostsfile" => {
            if args.is_empty() {
                anyhow::bail!(
                    "GlobalKnownHostsFile requires a value at line {}",
                    line_number
                );
            }
            let path =
                secure_validate_path(args[0], "known_hosts", line_number).with_context(|| {
                    format!("Invalid GlobalKnownHostsFile path at line {line_number}")
                })?;
            host.global_known_hosts_file = Some(path);
        }
        "forwardagent" => {
            if args.is_empty() {
                anyhow::bail!("ForwardAgent requires a value at line {}", line_number);
            }
            host.forward_agent = Some(parse_yes_no(args[0], line_number)?);
        }
        "forwardx11" => {
            if args.is_empty() {
                anyhow::bail!("ForwardX11 requires a value at line {}", line_number);
            }
            host.forward_x11 = Some(parse_yes_no(args[0], line_number)?);
        }
        "serveraliveinterval" => {
            if args.is_empty() {
                anyhow::bail!(
                    "ServerAliveInterval requires a value at line {}",
                    line_number
                );
            }
            let interval: u32 = args[0].parse().with_context(|| {
                format!(
                    "Invalid ServerAliveInterval value '{}' at line {}",
                    args[0], line_number
                )
            })?;
            host.server_alive_interval = Some(interval);
        }
        "serveralivecountmax" => {
            if args.is_empty() {
                anyhow::bail!(
                    "ServerAliveCountMax requires a value at line {}",
                    line_number
                );
            }
            let count: u32 = args[0].parse().with_context(|| {
                format!(
                    "Invalid ServerAliveCountMax value '{}' at line {}",
                    args[0], line_number
                )
            })?;
            host.server_alive_count_max = Some(count);
        }
        "connecttimeout" => {
            if args.is_empty() {
                anyhow::bail!("ConnectTimeout requires a value at line {}", line_number);
            }
            let timeout: u32 = args[0].parse().with_context(|| {
                format!(
                    "Invalid ConnectTimeout value '{}' at line {}",
                    args[0], line_number
                )
            })?;
            host.connect_timeout = Some(timeout);
        }
        "connectionattempts" => {
            if args.is_empty() {
                anyhow::bail!(
                    "ConnectionAttempts requires a value at line {}",
                    line_number
                );
            }
            let attempts: u32 = args[0].parse().with_context(|| {
                format!(
                    "Invalid ConnectionAttempts value '{}' at line {}",
                    args[0], line_number
                )
            })?;
            host.connection_attempts = Some(attempts);
        }
        "batchmode" => {
            if args.is_empty() {
                anyhow::bail!("BatchMode requires a value at line {}", line_number);
            }
            host.batch_mode = Some(parse_yes_no(args[0], line_number)?);
        }
        "compression" => {
            if args.is_empty() {
                anyhow::bail!("Compression requires a value at line {}", line_number);
            }
            host.compression = Some(parse_yes_no(args[0], line_number)?);
        }
        "tcpkeepalive" => {
            if args.is_empty() {
                anyhow::bail!("TCPKeepAlive requires a value at line {}", line_number);
            }
            host.tcp_keep_alive = Some(parse_yes_no(args[0], line_number)?);
        }
        "preferredauthentications" => {
            if args.is_empty() {
                anyhow::bail!(
                    "PreferredAuthentications requires a value at line {}",
                    line_number
                );
            }
            host.preferred_authentications = args
                .join(",")
                .split(',')
                .map(|s| s.trim().to_string())
                .collect();
        }
        "pubkeyauthentication" => {
            if args.is_empty() {
                anyhow::bail!(
                    "PubkeyAuthentication requires a value at line {}",
                    line_number
                );
            }
            host.pubkey_authentication = Some(parse_yes_no(args[0], line_number)?);
        }
        "passwordauthentication" => {
            if args.is_empty() {
                anyhow::bail!(
                    "PasswordAuthentication requires a value at line {}",
                    line_number
                );
            }
            host.password_authentication = Some(parse_yes_no(args[0], line_number)?);
        }
        "kbdinteractiveauthentication" => {
            if args.is_empty() {
                anyhow::bail!(
                    "KbdInteractiveAuthentication requires a value at line {}",
                    line_number
                );
            }
            host.keyboard_interactive_authentication = Some(parse_yes_no(args[0], line_number)?);
        }
        "gssapiauthentication" => {
            if args.is_empty() {
                anyhow::bail!(
                    "GSSAPIAuthentication requires a value at line {}",
                    line_number
                );
            }
            host.gssapi_authentication = Some(parse_yes_no(args[0], line_number)?);
        }
        "hostkeyalgorithms" => {
            if args.is_empty() {
                anyhow::bail!("HostKeyAlgorithms requires a value at line {}", line_number);
            }
            host.host_key_algorithms = args
                .join(",")
                .split(',')
                .map(|s| s.trim().to_string())
                .collect();
        }
        "kexalgorithms" => {
            if args.is_empty() {
                anyhow::bail!("KexAlgorithms requires a value at line {}", line_number);
            }
            host.kex_algorithms = args
                .join(",")
                .split(',')
                .map(|s| s.trim().to_string())
                .collect();
        }
        "ciphers" => {
            if args.is_empty() {
                anyhow::bail!("Ciphers requires a value at line {}", line_number);
            }
            host.ciphers = args
                .join(",")
                .split(',')
                .map(|s| s.trim().to_string())
                .collect();
        }
        "macs" => {
            if args.is_empty() {
                anyhow::bail!("MACs requires a value at line {}", line_number);
            }
            host.macs = args
                .join(",")
                .split(',')
                .map(|s| s.trim().to_string())
                .collect();
        }
        "sendenv" => {
            if args.is_empty() {
                anyhow::bail!("SendEnv requires a value at line {}", line_number);
            }
            host.send_env.extend(args.iter().map(|s| s.to_string()));
        }
        "setenv" => {
            if args.len() < 2 {
                anyhow::bail!("SetEnv requires name=value at line {}", line_number);
            }
            for arg in args {
                if let Some(eq_pos) = arg.find('=') {
                    let name = arg[..eq_pos].to_string();
                    let value = arg[eq_pos + 1..].to_string();
                    host.set_env.insert(name, value);
                } else {
                    anyhow::bail!(
                        "Invalid SetEnv format '{}' at line {} (expected name=value)",
                        arg,
                        line_number
                    );
                }
            }
        }
        "localforward" => {
            if args.is_empty() {
                anyhow::bail!("LocalForward requires a value at line {}", line_number);
            }
            host.local_forward.push(args.join(" "));
        }
        "remoteforward" => {
            if args.is_empty() {
                anyhow::bail!("RemoteForward requires a value at line {}", line_number);
            }
            host.remote_forward.push(args.join(" "));
        }
        "dynamicforward" => {
            if args.is_empty() {
                anyhow::bail!("DynamicForward requires a value at line {}", line_number);
            }
            host.dynamic_forward.push(args.join(" "));
        }
        "requesttty" => {
            if args.is_empty() {
                anyhow::bail!("RequestTTY requires a value at line {}", line_number);
            }
            host.request_tty = Some(args[0].to_string());
        }
        "escapechar" => {
            if args.is_empty() {
                anyhow::bail!("EscapeChar requires a value at line {}", line_number);
            }
            host.escape_char = Some(args[0].to_string());
        }
        "loglevel" => {
            if args.is_empty() {
                anyhow::bail!("LogLevel requires a value at line {}", line_number);
            }
            host.log_level = Some(args[0].to_string());
        }
        "syslogfacility" => {
            if args.is_empty() {
                anyhow::bail!("SyslogFacility requires a value at line {}", line_number);
            }
            host.syslog_facility = Some(args[0].to_string());
        }
        "protocol" => {
            if args.is_empty() {
                anyhow::bail!("Protocol requires a value at line {}", line_number);
            }
            host.protocol = args
                .join(",")
                .split(',')
                .map(|s| s.trim().to_string())
                .collect();
        }
        "addressfamily" => {
            if args.is_empty() {
                anyhow::bail!("AddressFamily requires a value at line {}", line_number);
            }
            host.address_family = Some(args[0].to_string());
        }
        "bindaddress" => {
            if args.is_empty() {
                anyhow::bail!("BindAddress requires a value at line {}", line_number);
            }
            host.bind_address = Some(args[0].to_string());
        }
        "clearallforwardings" => {
            if args.is_empty() {
                anyhow::bail!(
                    "ClearAllForwardings requires a value at line {}",
                    line_number
                );
            }
            host.clear_all_forwardings = Some(parse_yes_no(args[0], line_number)?);
        }
        "controlmaster" => {
            if args.is_empty() {
                anyhow::bail!("ControlMaster requires a value at line {}", line_number);
            }
            host.control_master = Some(args[0].to_string());
        }
        "controlpath" => {
            if args.is_empty() {
                anyhow::bail!("ControlPath requires a value at line {}", line_number);
            }
            let path = args[0].to_string();
            // ControlPath has different validation - it allows SSH substitution patterns
            validate_control_path(&path, line_number)?;
            host.control_path = Some(path);
        }
        "controlpersist" => {
            if args.is_empty() {
                anyhow::bail!("ControlPersist requires a value at line {}", line_number);
            }
            host.control_persist = Some(args[0].to_string());
        }
        _ => {
            // Unknown option - log a warning but continue
            tracing::warn!(
                "Unknown SSH config option '{}' at line {}",
                keyword,
                line_number
            );
        }
    }

    Ok(())
}

/// Parse yes/no boolean values from SSH configuration
pub(super) fn parse_yes_no(value: &str, line_number: usize) -> Result<bool> {
    match value.to_lowercase().as_str() {
        "yes" | "true" | "1" => Ok(true),
        "no" | "false" | "0" => Ok(false),
        _ => anyhow::bail!(
            "Invalid yes/no value '{}' at line {} (expected yes/no)",
            value,
            line_number
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_yes_no_values() {
        assert!(parse_yes_no("yes", 1).unwrap());
        assert!(parse_yes_no("true", 1).unwrap());
        assert!(parse_yes_no("1", 1).unwrap());
        assert!(!parse_yes_no("no", 1).unwrap());
        assert!(!parse_yes_no("false", 1).unwrap());
        assert!(!parse_yes_no("0", 1).unwrap());
        assert!(parse_yes_no("invalid", 1).is_err());
    }

    #[test]
    fn test_parse_single_host() {
        let content = r#"
Host example.com
    User testuser
    Port 2222
"#;
        let hosts = parse(content).unwrap();
        assert_eq!(hosts.len(), 1);
        assert_eq!(hosts[0].host_patterns, vec!["example.com"]);
        assert_eq!(hosts[0].user, Some("testuser".to_string()));
        assert_eq!(hosts[0].port, Some(2222));
    }

    #[test]
    fn test_parse_multiple_patterns() {
        let content = r#"
Host web*.example.com *.test.com
    User webuser
"#;
        let hosts = parse(content).unwrap();
        assert_eq!(hosts.len(), 1);
        assert_eq!(
            hosts[0].host_patterns,
            vec!["web*.example.com", "*.test.com"]
        );
        assert_eq!(hosts[0].user, Some("webuser".to_string()));
    }

    #[test]
    fn test_parse_comments_and_empty_lines() {
        let content = r#"
# This is a comment
Host example.com
    # Another comment
    User testuser
    
    Port 2222
    
# Final comment
"#;
        let hosts = parse(content).unwrap();
        assert_eq!(hosts.len(), 1);
        assert_eq!(hosts[0].host_patterns, vec!["example.com"]);
        assert_eq!(hosts[0].user, Some("testuser".to_string()));
        assert_eq!(hosts[0].port, Some(2222));
    }
}