sshbind 0.1.0

SSHBind is a Rust library that securely binds remote services behind multiple SSH jump hosts to a local socket, enabling seamless access with encrypted credential management, TOTP-based two-factor authentication, and automatic reconnection.
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
use clap::{Parser, Subcommand};
use env_logger::{Builder, Target};
use interprocess::local_socket::prelude::*;
use interprocess::local_socket::{
    GenericFilePath, GenericNamespaced, ListenerOptions, Name, Stream, ToFsName, ToNsName,
};
use log::{error, LevelFilter};
use named_sem::{Error as SemError, NamedSemaphore};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error::Error;
use std::fs::OpenOptions;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::result::Result;
use std::sync::LazyLock;
use std::sync::Mutex;
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};

static LOG_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
    dirs::data_local_dir()
        .expect("Failed to get local data dir")
        .join("sshbind/sshbind.log")
});

static BINDINGS: LazyLock<Mutex<HashMap<String, BindingDetails>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

static TIME_TO_DIE: LazyLock<Mutex<bool>> = LazyLock::new(|| Mutex::new(false));

#[derive(Serialize, Deserialize, Debug)]
enum DaemonCommand {
    Ping,
    Bind {
        addr: String,
        jump_hosts: Vec<String>,
        remote: Option<String>,
        sopsfile: String,
        cmd: Option<String>,
    },
    Unbind {
        addr: String,
    },
    List,
    Shutdown,
}

#[derive(Serialize, Deserialize, Debug)]
enum DaemonResponse {
    Success(String),
    List(Vec<BindingDetails>),
    Error(String),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct BindingDetails {
    addr: String,
    jump_hosts: Vec<String>,
    cmd: Option<String>,
    remote: Option<String>,
    timestamp: u64,
}

#[derive(Parser)]
#[command(author, version, about)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,
    #[arg(short = 'v', long)]
    verbose: bool,
    #[arg(short = 'l', long)]
    log_level: Option<u64>,
}

#[derive(Subcommand)]
enum Commands {
    Bind {
        #[arg(short, long)]
        addr: String,
        #[arg(short = 'j', long)]
        jump_host: Vec<String>,
        #[arg(short, long)]
        remote: Option<String>,
        #[arg(short, long)]
        sopsfile: String,
        #[arg(short, long)]
        cmd: Option<String>,
    },
    Unbind {
        #[arg(short, long)]
        addr: Option<String>,
        #[arg(long)]
        all: bool,
    },
    Shutdown,
    List,
    #[command(hide = true)]
    Daemon,
}

// Path where we will both listen and also advertise our socket name to clients.
const SERVER_NAME_PATH: &str = "/tmp/sshbind_daemon.ipc";
const SEM_SERVER_READY: &str = "/sshbind_server_ready";
// Serializes client creation and server readiness checking
const SEM_SENDING_STICK: &str = "/sshbind_sending_stick";
// I believe we need this because forking isn't allowed in rust
// Thus multiple daemon instances could be started at the same
// time by invoking the daemon command
const SEM_CHECK_LOCK: &str = "/sshbind_check_lock";
const SEM_FLOOD_GATE: &str = "/sshbind_flood_gate";

fn server_name() -> Result<Name<'static>, Box<dyn Error>> {
    // Prefer non-persistent, namespaced name
    if let Ok(n) = "sshbind.daemon".to_ns_name::<GenericNamespaced>() {
        Ok(n.into_owned())
    } else {
        // Fallback for macOS/BSD to a filesystem path
        match SERVER_NAME_PATH.to_fs_name::<GenericFilePath>() {
            Ok(n) => Ok(n.into_owned()),
            Err(e) => Err(Box::new(e)),
        }
    }
}

fn cleanup_server_resources() {
    let mut sending_stick = NamedSemaphore::create(SEM_SENDING_STICK, 1)
        .expect("Failed to create sending_stick semaphore");
    let mut server_ready = NamedSemaphore::create(SEM_SERVER_READY, 0)
        .expect("Failed to create server_ready semaphore");
    let mut check_lock =
        NamedSemaphore::create(SEM_CHECK_LOCK, 1).expect("Failed to create check_lock semaphore");
    let mut flood_gate =
        NamedSemaphore::create(SEM_FLOOD_GATE, 0).expect("Failed to create flood_gate semaphore");
    sending_stick
        .wait()
        .expect("Failed to wait for sending_stick");
    check_lock.wait().expect("Failed to wait for check_lock");
    server_ready
        .wait()
        .expect("Failed to wait for server_ready");

    check_lock.post().expect("Failed to post check_lock");
    flood_gate.wait().expect("Failed to wait for flood_gate");
    sending_stick.post().expect("Failed to post sending_stick");
}

#[cfg(any(target_os = "linux", target_os = "windows"))]
fn start_ipc_daemon() -> Result<(), Box<dyn Error>> {
    // Bootstrap first server
    let mut server_ready = NamedSemaphore::create(SEM_SERVER_READY, 0)
        .expect("Failed to create server_ready semaphore");
    let mut check_lock =
        NamedSemaphore::create(SEM_CHECK_LOCK, 1).expect("Failed to create check_lock semaphore");
    let mut flood_gate =
        NamedSemaphore::create(SEM_FLOOD_GATE, 0).expect("Failed to create server_ready semaphore");

    // Serialize client creation and server readiness checking
    check_lock
        .wait()
        .expect("Failed to wait for startup semaphore");

    // Catch dispatch race condition. If the semaphore was already posted, another
    // daemon instance has already bound the socket and is running, so we exit.
    if server_ready.try_wait().is_ok() {
        check_lock.post().expect("Failed to post check_lock");
        return Ok(());
    }

    // We are the first server. Bind a local socket on the predetermined path.
    let socket_name = match server_name() {
        Ok(name) => name,
        Err(e) => {
            // Failed to convert the path into a Name, post the lock and propagate error.
            check_lock.post().expect("Failed to post check_lock");
            return Err(e);
        }
    };
    let listener = ListenerOptions::new()
        .name(socket_name)
        .create_sync()
        .map_err(|e| {
            // Failed to bind the socket; post the lock and propagate error.
            check_lock.post().expect("Failed to post check_lock");
            Box::<dyn Error>::from(e)
        })?;

    // Signal readiness so clients can continue.
    server_ready.post().expect("Failed to post server_ready");
    check_lock.post().expect("Failed to post check_lock");
    flood_gate.post().expect("Failed to post flood_gate");

    // Initialise logging to our log file
    std::fs::create_dir_all(LOG_PATH.parent().expect("Parent must exist"))
        .expect("Failed to create log directory");
    let log_file = OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .append(false)
        .open(LOG_PATH.as_path())
        .expect("Cannot open log file");
    Builder::new()
        .filter(None, LevelFilter::Debug)
        .target(Target::Pipe(Box::new(log_file)))
        .init();

    // Accept incoming connections. Each stream is handled in its own thread.
    for stream in listener.incoming() {
        match stream {
            Ok(mut stream) => {
                thread::spawn(move || {
                    // Read a single command from the stream, handle it, and write
                    // back the response. Newlines delimit messages.
                    if let Err(e) = handle_client_stream(&mut stream) {
                        // Log or ignore errors on individual client connections.
                        error!("Error handling client: {}", e);
                    }
                    if *TIME_TO_DIE.lock().unwrap() {
                        cleanup_server_resources();
                        std::process::exit(0);
                    }
                });
            }
            Err(e) => {
                // If accepting a connection fails, log and continue listening.
                error!("Failed to accept client: {}", e);
            }
        }
    }
    Ok(())
}

/// Handle a single client connection on the server side.
///
/// The protocol is newline-delimited JSON.  We read bytes until a newline
/// character, deserialize the command, invoke the handler, then write the
/// serialized response followed by a newline.  Using JSON via serde allows
/// forward compatibility and human readability.
fn handle_client_stream(stream: &mut Stream) -> Result<(), Box<dyn Error>> {
    // Read the incoming command up to the newline delimiter
    let mut buf = Vec::new();
    loop {
        let mut byte = [0u8; 1];
        let n = stream.read(&mut byte)?;
        if n == 0 {
            // Connection closed prematurely
            return Err(Box::from("client closed connection"));
        }
        if byte[0] == b'\n' {
            break;
        }
        buf.push(byte[0]);
    }

    // Deserialize command using serde_json
    let cmd: DaemonCommand = serde_json::from_slice(&buf)?;
    let response = handle_command(cmd);
    // Serialize the response and write it back with a newline terminator
    let resp_bytes = serde_json::to_vec(&response)?;
    stream.write_all(&resp_bytes)?;
    stream.write_all(b"\n")?;
    stream.flush()?;
    Ok(())
}

/// Client-side: send a command and wait for a response.
fn send_command(cmd: DaemonCommand) -> Result<DaemonResponse, String> {
    let socket_name = match server_name() {
        Ok(name) => name,
        Err(e) => {
            return Err(format!("Failed to get server name: {}", e));
        }
    };
    let mut stream =
        Stream::connect(socket_name).map_err(|e| format!("ipc connect failed: {}", e))?;
    // Serialize and send the command with a newline terminator
    let cmd_bytes = serde_json::to_vec(&cmd).map_err(|e| format!("serialize error: {}", e))?;
    stream
        .write_all(&cmd_bytes)
        .map_err(|e| format!("IPC send error: {}", e))?;
    stream
        .write_all(b"\n")
        .map_err(|e| format!("IPC send error: {}", e))?;
    stream
        .flush()
        .map_err(|e| format!("IPC flush error: {}", e))?;
    // Read the response up to the newline delimiter
    let mut buf = Vec::new();
    loop {
        let mut byte = [0u8; 1];
        let n = stream
            .read(&mut byte)
            .map_err(|e| format!("IPC recv error: {}", e))?;
        if n == 0 {
            return Err("IPC recv error: server closed connection".into());
        }
        if byte[0] == b'\n' {
            break;
        }
        buf.push(byte[0]);
    }
    let resp: DaemonResponse =
        serde_json::from_slice(&buf).map_err(|e| format!("deserialize error: {}", e))?;
    Ok(resp)
}

/// Process a DaemonCommand and mutate state, returning a DaemonResponse.
fn handle_command(cmd: DaemonCommand) -> DaemonResponse {
    let response = match cmd {
        DaemonCommand::Ping => DaemonResponse::Success("PONG".into()),
        DaemonCommand::Bind {
            addr,
            jump_hosts,
            remote,
            sopsfile,
            cmd,
        } => {
            let mut binds = BINDINGS.lock().unwrap();
            if binds.contains_key(&addr) {
                DaemonResponse::Error(format!("Address {} is already bound.", addr))
            } else {
                binds.insert(
                    addr.clone(),
                    BindingDetails {
                        addr: addr.clone(),
                        jump_hosts: jump_hosts.clone(),
                        cmd: cmd.clone(),
                        remote: remote.clone(),
                        timestamp: SystemTime::now()
                            .duration_since(UNIX_EPOCH)
                            .unwrap()
                            .as_secs(),
                    },
                );
                sshbind::bind(&addr, jump_hosts, remote, &sopsfile, cmd);
                DaemonResponse::Success(format!("Bound at {}", addr))
            }
        }
        DaemonCommand::Unbind { addr } => {
            sshbind::unbind(&addr);
            BINDINGS.lock().unwrap().remove(&addr);
            DaemonResponse::Success(format!("Unbound {}", addr))
        }
        DaemonCommand::Shutdown => {
            let mut binds = BINDINGS.lock().unwrap();
            for a in binds.keys().cloned().collect::<Vec<_>>() {
                sshbind::unbind(&a);
            }
            binds.clear();
            let mut ttd = TIME_TO_DIE.lock().unwrap();
            *ttd = true;
            DaemonResponse::Success("All bindings cleared. Shutting down.".into())
        }
        DaemonCommand::List => {
            let list = BINDINGS.lock().unwrap().values().cloned().collect();
            DaemonResponse::List(list)
        }
    };
    response
}

fn spawn_daemon_if_needed() {
    let mut server_ready = NamedSemaphore::create(SEM_SERVER_READY, 0)
        .expect("Failed to create server_ready semaphore");
    let mut check_lock =
        NamedSemaphore::create(SEM_CHECK_LOCK, 1).expect("Failed to create server_ready semaphore");
    let mut flood_gate =
        NamedSemaphore::create(SEM_FLOOD_GATE, 0).expect("Failed to create flood_gate semaphore");
    let mut sending_stick = NamedSemaphore::create(SEM_SENDING_STICK, 1)
        .expect("Failed to create sending_stick semaphore");
    sending_stick
        .wait()
        .expect("Failed to wait for sending_stick");
    check_lock
        .wait()
        .expect("Failed to wait for startup semaphore");
    match server_ready.try_wait() {
        Ok(_) => {
            server_ready.post().expect("Failed to post server_ready");
            check_lock.post().expect("Failed to post check_lock");
            sending_stick.post().expect("Failed to post sending_stick");
            return;
        }
        Err(SemError::WouldBlock) => {
            std::fs::create_dir_all(LOG_PATH.parent().expect("Parent must exist"))
                .expect("Failed to create log directory");

            let stderr = OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(
                    LOG_PATH
                        .parent()
                        .expect("Parent must exist")
                        .join("sshbind_stderr.log"),
                )
                .expect("Cannot open stderr log file");
            let stdout = OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(
                    LOG_PATH
                        .parent()
                        .expect("Parent must exist")
                        .join("sshbind_stdout.log"),
                )
                .expect("Cannot open stdout log file");
            let exe = std::env::current_exe().expect("Failed to get current exe");

            let _ = Command::new(exe)
                .arg("daemon")
                .stdout(Stdio::from(stdout))
                .stderr(Stdio::from(stderr))
                .spawn();
        }
        Err(_) => println!("Try again. Maybe reboot"),
    }
    check_lock.post().expect("Failed to post check_lock");
    flood_gate.wait().expect("Failed to wait for flood_gate");
    flood_gate.post().expect("Failed to post flood_gate");
    sending_stick.post().expect("Failed to post sending_stick");
}

pub fn main() {
    let cli = Cli::parse();
    let start_offset = if cli.verbose {
        let start = match cli.log_level {
            Some(0) => SeekFrom::End(0),
            Some(1) => SeekFrom::Start(0),
            None => SeekFrom::End(0),
            Some(_) => {
                println!("Unsupported Log-Level");
                return;
            }
        };
        std::fs::create_dir_all(LOG_PATH.parent().expect("Parent must exist"))
            .expect("Failed to create log directory");

        let mut log_file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(LOG_PATH.as_path())
            .expect("Cannot open log file");
        log_file.seek(start).expect("Cannot seek in log file")
    } else {
        0
    };

    match cli.command {
        Some(Commands::Daemon) => {
            start_ipc_daemon().unwrap();
        }
        Some(Commands::Bind {
            addr,
            jump_host,
            remote,
            sopsfile,
            cmd,
        }) => {
            spawn_daemon_if_needed();
            let cmd = DaemonCommand::Bind {
                addr,
                jump_hosts: jump_host,
                remote,
                sopsfile,
                cmd,
            };
            match send_command(cmd) {
                Ok(DaemonResponse::Success(msg)) => println!("{}", msg),
                Ok(resp) => println!("Unexpected: {:?}", resp),
                Err(e) => eprintln!("{}", e),
            }
        }
        Some(Commands::Unbind { addr, all }) => {
            spawn_daemon_if_needed();
            let cmd = if all {
                DaemonCommand::Shutdown
            } else if let Some(a) = addr {
                DaemonCommand::Unbind { addr: a }
            } else {
                eprintln!("--addr or --all required");
                return;
            };
            match send_command(cmd) {
                Ok(DaemonResponse::Success(msg)) => println!("{}", msg),
                Ok(resp) => println!("Unexpected: {:?}", resp),
                Err(e) => eprintln!("{}", e),
            }
        }
        Some(Commands::Shutdown) => {
            spawn_daemon_if_needed();
            if let Ok(DaemonResponse::Success(msg)) = send_command(DaemonCommand::Shutdown) {
                println!("{}", msg);
            }
        }
        Some(Commands::List) => {
            spawn_daemon_if_needed();
            match send_command(DaemonCommand::List) {
                Ok(DaemonResponse::List(list)) if list.is_empty() => {
                    println!("No active bindings.")
                }
                Ok(DaemonResponse::List(list)) => {
                    println!("{:<20} {:<30} {:<10} Hosts", "Address", "Remote", "Time");
                    for b in list {
                        println!(
                            "{:<20} {:<30} {:<10} {:?}",
                            b.addr,
                            b.remote.unwrap_or_default(),
                            b.timestamp,
                            b.jump_hosts
                        );
                        match b.cmd {
                            None => println!("  Command: None"),
                            Some(ref cmd) => println!("  Command: {:?}", cmd),
                        }
                    }
                }
                Ok(resp) => println!("Unexpected: {:?}", resp),
                Err(e) => eprintln!("{}", e),
            }
        }
        None => println!("Use --help"),
    }

    if cli.verbose {
        std::fs::create_dir_all(LOG_PATH.parent().expect("Parent must exist"))
            .expect("Failed to create log directory");
        let mut log_file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(LOG_PATH.as_path())
            .expect("Cannot open log file");
        log_file
            .seek(SeekFrom::Start(start_offset))
            .expect("Cannot seek in log file");

        let mut reader = BufReader::new(log_file);
        let mut line = String::new();

        // Read until EOF
        while reader.read_line(&mut line).expect("") > 0 {
            print!("{}", line);
            line.clear();
        }
    }
}