qssh 0.5.0

Post-quantum secure shell with NIST PQC algorithms (Falcon, SPHINCS+, ML-KEM), configurable security tiers, and quantum-resistant protocol design
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
//! QSSH Client - Quantum-secure SSH replacement

use clap::Parser;
use qssh::{QsshConfig, PqAlgorithm, QsshClient, ReconnectConfig, security_tiers::SecurityTier};
use qssh::config::ConfigParser;
use qssh::port_forward::PortForwardManager;
use qssh::multiplex::{ControlMaster, ControlClient};
use qssh::proxy::{ProxyConnection, ProxyConfig, ProxyType};
use log::info;
use std::process;
use std::path::PathBuf;
use std::sync::Arc;

#[derive(Parser, Debug)]
#[clap(name = "qssh")]
#[clap(about = "Quantum-Secure Shell - Connect to remote systems with post-quantum security")]
#[clap(version = env!("CARGO_PKG_VERSION"))]
struct Args {
    /// User@host to connect to
    destination: String,
    
    /// Port to connect to
    #[clap(short = 'p', long, default_value = "22222")]
    port: u16,
    
    /// Enable QKD (Quantum Key Distribution)
    #[clap(long)]
    qkd: bool,
    
    /// QKD endpoint URL
    #[clap(long)]
    qkd_endpoint: Option<String>,
    
    /// QKD client certificate path
    #[clap(long)]
    qkd_cert: Option<String>,
    
    /// QKD client key path
    #[clap(long)]
    qkd_key: Option<String>,
    
    /// QKD CA certificate path
    #[clap(long)]
    qkd_ca: Option<String>,
    
    /// Local port forwarding (e.g., 8080:localhost:80)
    #[clap(short = 'L', long)]
    local: Vec<String>,
    
    /// Remote port forwarding (e.g., 9000:localhost:9000)
    #[clap(short = 'R', long)]
    remote: Vec<String>,

    /// Dynamic port forwarding / SOCKS5 proxy (e.g., 1080)
    #[clap(short = 'D', long)]
    dynamic: Vec<String>,
    
    /// Post-quantum algorithm to use (sphincs, falcon512, falcon1024)
    #[clap(long, default_value = "sphincs")]
    pq_algo: String,
    
    /// Execute command instead of shell
    #[clap(short = 'c', long)]
    command: Option<String>,

    /// Use password authentication
    #[clap(short = 'P', long)]
    use_password: bool,

    /// Enable X11 forwarding
    #[clap(short = 'X', long)]
    x11: bool,

    /// Enable trusted X11 forwarding
    #[clap(short = 'Y', long)]
    trusted_x11: bool,

    /// Enable agent forwarding (forwards QSSH_AUTH_SOCK to remote)
    #[clap(short = 'A', long = "agent-forward")]
    agent_forward: bool,

    /// Verbose output
    #[clap(short, long)]
    verbose: bool,

    /// Use quantum-native transport (768-byte indistinguishable frames)
    #[clap(long)]
    quantum_native: bool,

    /// Use classical transport (for compatibility)
    #[clap(long)]
    classical: bool,

    /// Auto-reconnect on disconnect (exponential backoff)
    #[clap(long)]
    persistent: bool,

    /// Maximum reconnection attempts (0 = infinite, default 10)
    #[clap(long, default_value = "10")]
    max_retries: u32,

    /// Control socket path for connection multiplexing (like ssh -S)
    /// If a master is already running, new sessions reuse its connection.
    /// Otherwise, this process becomes the master.
    #[clap(short = 'S', long = "ctl-path")]
    ctl_path: Option<String>,

    /// ProxyJump through intermediate host (e.g., user@jumphost or user@jump1,jump2)
    #[clap(short = 'J', long = "proxy-jump")]
    jump: Option<String>,
}

#[tokio::main]
async fn main() {
    let args = Args::parse();
    
    // Initialize logging
    if args.verbose {
        env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug"))
            .target(env_logger::Target::Stderr)
            .init();
    } else {
        env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn"))
            .target(env_logger::Target::Stderr)
            .init();
    }
    
    // Load config file
    let config_parser = ConfigParser::load_default().unwrap_or_else(|e| {
        if args.verbose {
            eprintln!("Warning: Failed to load config file: {}", e);
        }
        ConfigParser::parse("").unwrap()
    });

    // Parse destination
    let (username, hostname) = if args.destination.contains('@') {
        let parts: Vec<&str> = args.destination.split('@').collect();
        if parts.len() != 2 {
            eprintln!("Error: Invalid destination format. Use user@host or host");
            process::exit(1);
        }
        (Some(parts[0].to_string()), parts[1].to_string())
    } else {
        (None, args.destination.clone())
    };

    // Get config for host
    let host_config = config_parser.get_host_config(&hostname);

    // Override with command line args
    let username = username.or(host_config.user).unwrap_or_else(|| {
        eprintln!("Error: Username not specified. Use user@host or configure in ~/.qssh/config");
        process::exit(1);
    });

    let port = if args.port != 22222 {
        args.port
    } else {
        host_config.port.unwrap_or(22222)
    };

    let host = if let Some(h) = host_config.hostname {
        format!("{}:{}", h, port)
    } else {
        format!("{}:{}", hostname, port)
    };
    
    // Parse local forward specs (-L)
    let mut local_forwards = Vec::new();
    for spec in &args.local {
        match PortForwardManager::parse_forward_spec(spec, "local") {
            Ok(fwd) => local_forwards.push(fwd),
            Err(e) => {
                eprintln!("Error: Invalid local forward spec '{}': {}", spec, e);
                process::exit(1);
            }
        }
    }
    
    // Determine PQ algorithm (Kyber removed due to vulnerabilities)
    let pq_algorithm = match args.pq_algo.as_str() {
        "sphincs" => PqAlgorithm::SphincsPlus,
        "falcon" | "falcon512" => PqAlgorithm::Falcon512,
        "falcon1024" => PqAlgorithm::Falcon1024,
        // Warn about deprecated/vulnerable algorithms
        "kyber" | "kyber512" | "kyber768" | "kyber1024" => {
            eprintln!("Warning: Kyber algorithms are vulnerable to KyberSlash attacks.");
            eprintln!("Using Falcon-512 instead for quantum security.");
            PqAlgorithm::Falcon512
        },
        _ => {
            eprintln!("Error: Unknown PQ algorithm: {}", args.pq_algo);
            eprintln!("Available algorithms: sphincs, falcon512, falcon1024");
            process::exit(1);
        }
    };
    
    // Handle password authentication
    let password = if args.use_password {
        // Prompt for password
        eprint!("Password: ");
        rpassword::read_password().ok()
    } else {
        None
    };

    // Determine transport type
    let quantum_native = !args.classical;

    // Create configuration
    let config = QsshConfig {
        server: host.clone(),
        username: username.clone(),
        password,
        port_forwards: Vec::new(), // -L forwards handled via PortForwardManager below
        use_qkd: args.qkd,
        qkd_endpoint: args.qkd_endpoint.clone(),
        qkd_cert_path: args.qkd_cert.clone(),
        qkd_key_path: args.qkd_key.clone(),
        qkd_ca_path: args.qkd_ca.clone(),
        pq_algorithm,
        kex_algorithm: host_config.kex_algorithm.unwrap_or_default(),
        key_rotation_interval: 3600, // 1 hour
        security_tier: SecurityTier::default(),
        quantum_native,
    };
    
    info!("Connecting to {} as {}...", host, username);
    info!("Using post-quantum algorithm: {:?}", pq_algorithm);
    if quantum_native {
        info!("Transport: Quantum-native (768-byte indistinguishable frames)");
    } else {
        info!("Transport: Classical (variable-size frames)");
    }
    
    if args.qkd {
        info!("QKD enabled");
        if let Some(endpoint) = args.qkd_endpoint {
            info!("QKD endpoint: {}", endpoint);
        }
    }
    
    // Parse remote forward specs
    let mut remote_forwards = Vec::new();
    for spec in &args.remote {
        match PortForwardManager::parse_forward_spec(spec, "remote") {
            Ok(fwd) => remote_forwards.push(fwd),
            Err(e) => {
                eprintln!("Error: Invalid remote forward spec '{}': {}", spec, e);
                process::exit(1);
            }
        }
    }

    // Parse dynamic forward specs (-D SOCKS5)
    let mut dynamic_forwards = Vec::new();
    for spec in &args.dynamic {
        match PortForwardManager::parse_forward_spec(spec, "dynamic") {
            Ok(fwd) => dynamic_forwards.push(fwd),
            Err(e) => {
                eprintln!("Error: Invalid dynamic forward spec '{}': {}", spec, e);
                process::exit(1);
            }
        }
    }

    let has_remote_forwards = !remote_forwards.is_empty();
    let has_forwards = !local_forwards.is_empty() || !remote_forwards.is_empty() || !dynamic_forwards.is_empty();

    // ──── Multiplexing: -S <socket_path> ────
    // If a control master is already running, connect as a client (reuse existing PQ tunnel).
    // Otherwise fall through to normal connect and optionally become the master.
    if let Some(ref ctl_path_str) = args.ctl_path {
        let ctl_path = expand_tilde(ctl_path_str);
        if ControlMaster::check_master(&ctl_path).await {
            info!("Reusing existing connection via control master at {:?}", ctl_path);

            let mut mux_client = ControlClient::new(ctl_path);
            match mux_client.connect().await {
                Ok(()) => {
                    match mux_client.new_session(args.command.clone()).await {
                        Ok(session_id) => {
                            info!("Multiplexed session {} created", session_id);
                            // For exec mode: read output from session
                            // For shell mode: the session runs in the master, we just wait
                            eprintln!("Session {} active on existing connection. Press Ctrl+C to close.", session_id);
                            tokio::signal::ctrl_c().await.ok();
                            let _ = mux_client.close_session(session_id).await;
                        }
                        Err(e) => {
                            eprintln!("Failed to create multiplexed session: {}", e);
                            process::exit(1);
                        }
                    }
                }
                Err(e) => {
                    eprintln!("Failed to connect to control master: {}", e);
                    process::exit(1);
                }
            }
            return;
        }
    }

    // Build reconnect config
    let reconnect_config = if args.persistent {
        Some(ReconnectConfig {
            max_attempts: args.max_retries,
            ..ReconnectConfig::default()
        })
    } else {
        None
    };

    // Outer reconnect loop — re-entered when --persistent and the session drops
    let mut session_count = 0u32;
    loop {
        session_count += 1;

        // Create client and connect
        let mut client = QsshClient::new(config.clone());

        let connect_result = if let Some(ref jump_spec) = args.jump {
            // ProxyJump: connect through intermediate host
            info!("Using ProxyJump through: {}", jump_spec);
            let proxy_config = ProxyConfig {
                proxy_type: ProxyType::Jump,
                target: jump_spec.clone(),
                options: Vec::new(),
            };
            let proxy = ProxyConnection::new(proxy_config, config.clone());

            // Extract host and port from the server address
            let parts: Vec<&str> = config.server.split(':').collect();
            let proxy_host = parts[0];
            let proxy_port: u16 = parts.get(1).and_then(|p| p.parse().ok()).unwrap_or(22222);

            match proxy.connect(proxy_host, proxy_port).await {
                Ok(stream) => client.connect_via_stream(stream).await,
                Err(e) => Err(e),
            }
        } else if let Some(ref rc) = reconnect_config {
            client.connect_with_retry(rc).await
        } else {
            client.connect().await
        };

        match connect_result {
            Ok(()) => {
                if session_count > 1 {
                    eprintln!("Reconnected (session #{})", session_count);
                }
                info!("Connected successfully!");

                // Start control master if -S was requested and no master existed
                if let Some(ref ctl_path_str) = args.ctl_path {
                    if let Some(transport) = client.transport() {
                        let ctl_path = expand_tilde(ctl_path_str);
                        let master = ControlMaster::new(ctl_path.clone(), Arc::new(transport.clone()));
                        info!("Starting control master at {:?}", ctl_path);
                        tokio::spawn(async move {
                            if let Err(e) = master.start().await {
                                log::error!("Control master error: {}", e);
                            }
                        });
                    }
                }

                // Set up port forwards (-L and -R) via PortForwardManager
                if has_forwards {
                    if let Some(transport) = client.transport() {
                        let transport = Arc::new(transport.clone());
                        let mut pfm = PortForwardManager::new(transport);
                        for fwd in &local_forwards {
                            pfm.add_forward(fwd.clone());
                        }
                        for fwd in &remote_forwards {
                            pfm.add_forward(fwd.clone());
                        }
                        for fwd in &dynamic_forwards {
                            pfm.add_forward(fwd.clone());
                        }
                        match pfm.start_all().await {
                            Ok(()) => {
                                info!("Port forwards established");
                                client.set_remote_forward_state(
                                    pfm.remote_registry(),
                                    pfm.channel_router(),
                                );
                            }
                            Err(e) => {
                                eprintln!("Error setting up port forwards: {}", e);
                                if reconnect_config.is_none() { process::exit(1); }
                            }
                        }
                    }
                }

                // Enable X11 forwarding if requested
                if args.x11 || args.trusted_x11 {
                    match client.enable_x11(args.trusted_x11).await {
                        Ok(()) => info!("X11 forwarding enabled"),
                        Err(e) => eprintln!("Warning: Failed to enable X11 forwarding: {}", e),
                    }
                }

                // Enable agent forwarding if requested
                if args.agent_forward {
                    match client.enable_agent_forwarding().await {
                        Ok(()) => info!("Agent forwarding enabled"),
                        Err(e) => eprintln!("Warning: Failed to enable agent forwarding: {}", e),
                    }
                }

                // Execute command or start shell
                if let Some(ref command) = args.command {
                    match client.exec_with_status(command).await {
                        Ok((output, exit_code)) => {
                            print!("{}", output);
                            if exit_code != 0 && reconnect_config.is_none() && !has_remote_forwards && !has_forwards {
                                process::exit(exit_code as i32);
                            }
                        }
                        Err(e) => {
                            eprintln!("Command execution failed: {}", e);
                            if reconnect_config.is_none() { process::exit(1); }
                        }
                    }

                    // If forwards are active, keep connection alive
                    if has_remote_forwards || has_forwards {
                        info!("Exec completed, keeping connection alive for port forwards");
                        info!("Press Ctrl+C to disconnect");
                        if let Err(e) = client.run_forward_loop().await {
                            log::debug!("Forward loop ended: {}", e);
                        }
                    }
                } else {
                    // Interactive shell
                    match client.shell().await {
                        Ok(_) => info!("Shell session terminated normally"),
                        Err(e) => {
                            eprintln!("Shell error: {}", e);
                            eprintln!("Error details: {:?}", e);
                        }
                    }
                }

                // Disconnect
                let _ = client.disconnect().await;

                // Clean up control master socket on disconnect
                if let Some(ref ctl_path_str) = args.ctl_path {
                    let ctl_path = expand_tilde(ctl_path_str);
                    let _ = std::fs::remove_file(&ctl_path);
                }
            }
            Err(e) => {
                eprintln!("Connection failed: {}", e);
                if reconnect_config.is_none() { process::exit(1); }
                // --persistent: the connect_with_retry already exhausted retries
                process::exit(1);
            }
        }

        // If not persistent, exit after one session
        if reconnect_config.is_none() {
            break;
        }

        // Persistent mode: brief pause then reconnect
        eprintln!("Session ended. Reconnecting in 1s...");
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    }
}

fn expand_tilde(path: &str) -> PathBuf {
    if path.starts_with("~/") {
        if let Ok(home) = std::env::var("HOME") {
            return PathBuf::from(format!("{}{}", home, &path[1..]));
        }
    }
    PathBuf::from(path)
}