bssh 2.1.2

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
// 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.

//! Exec command execution for Match directive

use anyhow::{Context, Result};
use std::collections::HashMap;
use std::process::Command;
use std::time::Duration;

use super::MatchContext;

/// Maximum timeout for exec commands
const EXEC_TIMEOUT_SECS: u64 = 5;

/// Execute a command for Match exec condition
pub fn execute_match_command(command: &str, context: &MatchContext) -> Result<bool> {
    // Security validation
    validate_exec_command(command)?;

    // Expand variables in command
    let expanded_command = expand_variables(command, &context.variables);

    tracing::debug!("Executing Match exec command: {}", expanded_command);

    // Parse command into program and args using shell parsing for proper handling
    let parts = shell_words::split(&expanded_command)
        .with_context(|| format!("Failed to parse command: {expanded_command}"))?;

    if parts.is_empty() {
        anyhow::bail!("Empty command for Match exec");
    }

    let program = &parts[0];
    let args = &parts[1..];

    // Execute with proper timeout enforcement
    #[cfg(unix)]
    {
        use std::process::Stdio;
        use std::time::Instant;

        let start = Instant::now();
        let timeout = Duration::from_secs(EXEC_TIMEOUT_SECS);

        let mut cmd = Command::new(program);
        cmd.args(args)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        // Set environment variables
        for (key, value) in &context.variables {
            cmd.env(format!("SSH_MATCH_{}", key.to_uppercase()), value);
        }

        // Spawn the process
        let mut child = match cmd.spawn() {
            Ok(child) => child,
            Err(e) => {
                tracing::debug!("Failed to spawn Match exec command '{}': {}", program, e);
                return Ok(false); // Command execution failure means condition doesn't match
            }
        };

        // Wait with timeout using a loop
        loop {
            // Try to get the exit status without blocking
            match child.try_wait() {
                Ok(Some(status)) => {
                    // Process exited
                    let success = status.success();
                    let elapsed = start.elapsed();

                    tracing::debug!(
                        "Match exec command '{}' completed in {:.1}s with status: {} (exit code: {:?})",
                        program,
                        elapsed.as_secs_f64(),
                        success,
                        status.code()
                    );

                    return Ok(success);
                }
                Ok(None) => {
                    // Process still running, check timeout
                    if start.elapsed() > timeout {
                        // Timeout exceeded, kill the process
                        tracing::warn!(
                            "Match exec command '{}' exceeded timeout of {}s, killing process",
                            program,
                            EXEC_TIMEOUT_SECS
                        );

                        // Try to kill the process
                        let _ = child.kill();
                        // Wait a bit for it to die
                        std::thread::sleep(Duration::from_millis(100));
                        // Force wait to clean up zombie
                        let _ = child.wait();

                        return Ok(false);
                    }

                    // Sleep a bit before checking again
                    std::thread::sleep(Duration::from_millis(50));
                }
                Err(e) => {
                    tracing::error!("Error waiting for Match exec command '{}': {}", program, e);
                    // Try to kill the process just in case
                    let _ = child.kill();
                    return Ok(false);
                }
            }
        }
    }

    #[cfg(not(unix))]
    {
        use std::process::Stdio;

        // On non-Unix systems, use a simpler approach
        let mut cmd = Command::new(program);
        cmd.args(args)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        // Set environment variables
        for (key, value) in &context.variables {
            cmd.env(format!("SSH_MATCH_{}", key.to_uppercase()), value);
        }

        // Note: Windows doesn't have good timeout support without additional dependencies
        match cmd.status() {
            Ok(status) => {
                let success = status.success();
                tracing::debug!(
                    "Match exec command '{}' returned: {} (exit code: {:?})",
                    program,
                    success,
                    status.code()
                );
                Ok(success)
            }
            Err(e) => {
                tracing::debug!("Match exec command '{}' failed: {}", program, e);
                Ok(false)
            }
        }
    }
}

/// Validate an exec command for security
pub fn validate_exec_command(command: &str) -> Result<()> {
    // Check command length first
    const MAX_COMMAND_LENGTH: usize = 1024;
    if command.len() > MAX_COMMAND_LENGTH {
        anyhow::bail!(
            "Match exec command is too long ({} bytes). Maximum allowed is {} bytes.",
            command.len(),
            MAX_COMMAND_LENGTH
        );
    }

    // Check for newlines and control characters
    if command
        .chars()
        .any(|c| c.is_control() && c != ' ' && c != '\t')
    {
        anyhow::bail!(
            "Match exec command contains control characters. This is blocked for security."
        );
    }

    // Check for dangerous patterns with more comprehensive list
    const DANGEROUS_PATTERNS: &[&str] = &[
        "rm ", "rm\t", "rm-", "rmdir", "dd ", "dd\t", "mkfs", "format", "fdisk", ">", ">>", "<",
        "<<", // File redirection
        "|",  // Pipes
        ";",  // Command chaining
        "&&", "||", // Conditional execution
        "&",  // Background execution
        "`",  // Command substitution
        "$(", // Command substitution
        "${", // Variable expansion that could be dangerous
        "\\n", "\\r", // Escaped newlines
        "../", "..\\", // Directory traversal
        "~/.", "~root", // Hidden file or root access attempts
    ];

    for pattern in DANGEROUS_PATTERNS {
        if command.contains(pattern) {
            anyhow::bail!(
                "Match exec command contains potentially dangerous pattern '{pattern}'. \
                 This is blocked for security reasons."
            );
        }
    }

    // Check for quotes that might hide dangerous patterns
    let mut in_single_quote = false;
    let mut in_double_quote = false;
    let mut prev_char = '\0';

    for ch in command.chars() {
        match ch {
            '\'' if prev_char != '\\' => in_single_quote = !in_single_quote,
            '"' if prev_char != '\\' => in_double_quote = !in_double_quote,
            '`' if !in_single_quote => {
                anyhow::bail!(
                    "Match exec command contains backtick outside single quotes. \
                     This could allow command substitution."
                );
            }
            '$' if !in_single_quote => {
                // $ is dangerous in double quotes or unquoted
                if let Some(next) = command.chars().nth(command.find('$').unwrap() + 1)
                    && (next == '(' || next == '{')
                {
                    anyhow::bail!(
                        "Match exec command contains potential command or variable substitution. \
                             This is blocked for security."
                    );
                }
            }
            _ => {}
        }
        prev_char = ch;
    }

    // Ensure quotes are balanced
    if in_single_quote || in_double_quote {
        anyhow::bail!("Match exec command has unbalanced quotes.");
    }

    // Block potentially dangerous executables
    const BLOCKED_COMMANDS: &[&str] = &[
        "sh", "bash", "zsh", "ksh", "csh", "fish", // Shells
        "python", "python2", "python3", "perl", "ruby", "php", "node", // Interpreters
        "nc", "netcat", "ncat", "socat", // Network tools
        "wget", "curl", "fetch", // Download tools
        "chmod", "chown", "chgrp", // Permission changes
    ];

    // Extract the first word (command name)
    let first_word = command
        .split_whitespace()
        .next()
        .unwrap_or("")
        .trim_start_matches('/');

    // Check against blocked commands
    for blocked in BLOCKED_COMMANDS {
        if first_word == *blocked || first_word.ends_with(&format!("/{blocked}")) {
            anyhow::bail!(
                "Match exec command uses blocked executable '{blocked}'. \
                 Executing shells or interpreters is not allowed for security."
            );
        }
    }

    // Warn about potentially sensitive commands
    const SENSITIVE_COMMANDS: &[&str] = &["sudo", "su", "doas", "passwd", "ssh", "scp", "sftp"];
    for cmd in SENSITIVE_COMMANDS {
        if first_word == *cmd || first_word.ends_with(&format!("/{cmd}")) {
            tracing::warn!(
                "Match exec command uses potentially sensitive command '{}'. \
                 Please ensure this is intentional and secure.",
                cmd
            );
        }
    }

    // Restrict to allowlisted commands for maximum security (optional, logged as info)
    const SAFE_COMMANDS: &[&str] = &[
        "test", "[", "ls", "cat", "grep", "head", "tail", "echo", "true", "false", "date",
        "hostname",
    ];
    if !SAFE_COMMANDS
        .iter()
        .any(|&safe| first_word == safe || first_word.ends_with(&format!("/{safe}")))
    {
        tracing::info!(
            "Match exec command '{}' is not in the safe command allowlist. \
             Consider using one of: {:?}",
            first_word,
            SAFE_COMMANDS
        );
    }

    Ok(())
}

/// Expand variables in a command string
pub fn expand_variables(command: &str, variables: &HashMap<String, String>) -> String {
    // Early return if no % characters to expand
    if !command.contains('%') {
        return command.to_string();
    }

    let mut result = String::with_capacity(command.len() + 32);
    let mut chars = command.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '%' {
            if let Some(&next_ch) = chars.peek() {
                // Look for single character variable
                let key = next_ch.to_string();
                if let Some(value) = variables.get(&key) {
                    result.push_str(value);
                    chars.next(); // Consume the variable character
                } else {
                    result.push(ch); // Keep the % if no matching variable
                }
            } else {
                result.push(ch); // Keep trailing %
            }
        } else {
            result.push(ch);
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ssh::ssh_config::match_directive::MatchContext;

    #[test]
    fn test_validate_exec_command() {
        // Valid commands
        assert!(validate_exec_command("test -f /tmp/file").is_ok());
        assert!(validate_exec_command("ls -la").is_ok());
        assert!(validate_exec_command("echo hello").is_ok());

        // Dangerous commands
        assert!(validate_exec_command("rm -rf /").is_err());
        assert!(validate_exec_command("ls; rm file").is_err());
        assert!(validate_exec_command("echo `whoami`").is_err());
        assert!(validate_exec_command("cat file | grep pattern").is_err());
        assert!(validate_exec_command("dd if=/dev/zero of=/dev/sda").is_err());
    }

    #[test]
    fn test_expand_variables() {
        let mut variables = HashMap::new();
        variables.insert("h".to_string(), "example.com".to_string());
        variables.insert("u".to_string(), "testuser".to_string());
        variables.insert("l".to_string(), "localuser".to_string());

        let command = "test -f /tmp/%h.lock";
        let expanded = expand_variables(command, &variables);
        assert_eq!(expanded, "test -f /tmp/example.com.lock");

        let command = "echo %u@%h";
        let expanded = expand_variables(command, &variables);
        assert_eq!(expanded, "echo testuser@example.com");
    }

    #[test]
    fn test_validate_exec_security_edge_cases() {
        // Test boundary condition: exactly 1024 characters
        let long_cmd = "a".repeat(1024);
        assert!(validate_exec_command(&long_cmd).is_ok());

        // Test over limit: 1025 characters
        let too_long_cmd = "a".repeat(1025);
        assert!(validate_exec_command(&too_long_cmd).is_err());

        // Test unbalanced quotes
        assert!(validate_exec_command("echo \"hello").is_err());
        assert!(validate_exec_command("echo 'hello").is_err());
        assert!(validate_exec_command("echo \"hello'").is_err());

        // Test dangerous patterns with spaces (validation checks for "rm ")
        assert!(validate_exec_command("rm -rf /").is_err());
        assert!(validate_exec_command("dd if=/dev/zero").is_err());

        // Test semicolon (shell command separator)
        assert!(validate_exec_command("ls;rm file").is_err());
        assert!(validate_exec_command("echo hello ; rm file").is_err());
    }

    #[test]
    #[cfg(unix)]
    fn test_exec_timeout() {
        use std::time::Instant;

        let context = MatchContext::new("example.com".to_string(), None).unwrap();

        // Test command that sleeps longer than timeout
        let start = Instant::now();
        let result = execute_match_command("sleep 10", &context).unwrap();
        let duration = start.elapsed();

        // Should timeout and return false
        assert!(
            !result,
            "Long-running command should timeout and return false"
        );
        assert!(
            duration.as_secs() <= EXEC_TIMEOUT_SECS + 1,
            "Should timeout within {EXEC_TIMEOUT_SECS} seconds, took {duration:?}"
        );
    }

    #[test]
    #[cfg(unix)]
    fn test_exec_nonexistent_command() {
        let context = MatchContext::new("example.com".to_string(), None).unwrap();

        // Test command that doesn't exist
        let result = execute_match_command("nonexistent_command_12345", &context).unwrap();

        // Should return false for nonexistent command
        assert!(!result, "Nonexistent command should return false");
    }

    #[test]
    #[cfg(unix)]
    fn test_exec_exit_code_handling() {
        let context = MatchContext::new("example.com".to_string(), None).unwrap();

        // Test command that exits with success (0)
        let result = execute_match_command("test -d /tmp", &context).unwrap();
        assert!(result, "Successful command should return true");

        // Test command that exits with failure (non-zero)
        let result = execute_match_command("test -f /nonexistent_file_12345", &context).unwrap();
        assert!(!result, "Failed command should return false");
    }

    #[test]
    #[cfg(windows)]
    fn test_exec_disabled_on_windows() {
        let context = MatchContext::new("example.com".to_string(), None).unwrap();

        // exec should be disabled on Windows
        let result = execute_match_command("echo test", &context);

        assert!(
            result.is_err(),
            "exec should be disabled on Windows for security"
        );
    }

    #[test]
    fn test_expand_variables_edge_cases() {
        let mut variables = HashMap::new();
        variables.insert("h".to_string(), "example.com".to_string());

        // Test unknown variable (should be left unchanged)
        let command = "test -f /tmp/%unknown";
        let expanded = expand_variables(command, &variables);
        assert_eq!(expanded, "test -f /tmp/%unknown");

        // Test escaped percent
        let command = "echo 100%%";
        let expanded = expand_variables(command, &variables);
        assert!(expanded.contains("%"));

        // Test variable at start
        let command = "%h.example.com";
        let expanded = expand_variables(command, &variables);
        assert_eq!(expanded, "example.com.example.com");

        // Test variable at end
        let command = "prefix-%h";
        let expanded = expand_variables(command, &variables);
        assert_eq!(expanded, "prefix-example.com");
    }
}