croft-software 0.2.132

VSCode-style TUI written in Rust
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
use anyhow::{Context, Result};
use portable_pty::{CommandBuilder, PtySize, native_pty_system};
use std::io::{Read, Write};
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, Sender, TryRecvError, channel};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

use crate::remote::{AdoptedMaster, ssh_control_dir};

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PromptKind {
    Password,
    Passphrase,
    VerificationCode,
    HostKeyConfirmation,
    Generic,
}

#[derive(Clone, Debug)]
pub enum SshAuthEvent {
    Log(String),
    Prompt { kind: PromptKind, line: String },
    Authenticated,
    Failed(String),
}

pub struct SshAuth {
    host: String,
    socket_dir: PathBuf,
    socket_path: PathBuf,
    writer: Option<Box<dyn Write + Send>>,
    child: Box<dyn portable_pty::Child + Send + Sync>,
    events_rx: Receiver<SshAuthEvent>,
    reader_handle: Option<JoinHandle<()>>,
    cancelled: Arc<AtomicBool>,
    finished: bool,
    handed_off: bool,
    authenticated_signaled: bool,
    started: Instant,
}

impl SshAuth {
    pub fn start(host: &str) -> Result<Self> {
        let socket_dir = ssh_control_dir().context("ssh control dir")?;
        let socket_path = socket_dir.join("ctl");

        let pty_system = native_pty_system();
        let pair = pty_system
            .openpty(PtySize {
                cols: 100,
                rows: 24,
                pixel_width: 0,
                pixel_height: 0,
            })
            .context("openpty for ssh auth")?;

        let mut cmd = CommandBuilder::new("ssh");
        cmd.arg("-M");
        cmd.arg("-S");
        cmd.arg(&socket_path);
        cmd.arg("-N");
        cmd.arg("-T");
        cmd.arg("-o");
        cmd.arg("BatchMode=no");
        cmd.arg("-o");
        cmd.arg("ServerAliveInterval=15");
        cmd.arg(host);
        cmd.env("TERM", "dumb");
        cmd.env_remove("SSH_ASKPASS");
        cmd.env_remove("SSH_ASKPASS_REQUIRE");
        cmd.env_remove("DISPLAY");

        let child = pair.slave.spawn_command(cmd).context("spawn ssh master")?;
        drop(pair.slave);

        let writer = pair.master.take_writer().context("take writer")?;
        let reader = pair.master.try_clone_reader().context("clone reader")?;

        let (events_tx, events_rx) = channel();
        let cancelled = Arc::new(AtomicBool::new(false));
        let reader_handle = spawn_reader(reader, events_tx, cancelled.clone());

        Ok(Self {
            host: host.to_string(),
            socket_dir,
            socket_path,
            writer: Some(writer),
            child,
            events_rx,
            reader_handle: Some(reader_handle),
            cancelled,
            finished: false,
            handed_off: false,
            authenticated_signaled: false,
            started: Instant::now(),
        })
    }

    pub fn drain_events(&mut self) -> Vec<SshAuthEvent> {
        let mut out = Vec::new();
        loop {
            match self.events_rx.try_recv() {
                Ok(e) => out.push(e),
                Err(TryRecvError::Empty) => break,
                Err(TryRecvError::Disconnected) => {
                    if !self.finished {
                        self.poll_child_for_completion(&mut out);
                    }
                    break;
                }
            }
        }
        if !self.finished {
            self.poll_child_for_completion(&mut out);
        }
        out
    }

    fn poll_child_for_completion(&mut self, out: &mut Vec<SshAuthEvent>) {
        if !self.authenticated_signaled && self.socket_path.exists() {
            self.authenticated_signaled = true;
            out.push(SshAuthEvent::Authenticated);
            return;
        }
        if let Ok(Some(status)) = self.child.try_wait() {
            self.finished = true;
            if status.success() && self.socket_path.exists() {
                if !self.authenticated_signaled {
                    self.authenticated_signaled = true;
                    out.push(SshAuthEvent::Authenticated);
                }
            } else {
                let detail = if !self.socket_path.exists() {
                    format!(
                        "ssh exited {status} and no control socket appeared at {}",
                        self.socket_path.display()
                    )
                } else {
                    format!("ssh exited {status}")
                };
                out.push(SshAuthEvent::Failed(detail));
            }
        } else if self.started.elapsed() > Duration::from_secs(600) {
            self.cancelled.store(true, Ordering::SeqCst);
            let _ = self.child.kill();
            self.finished = true;
            out.push(SshAuthEvent::Failed(
                "ssh auth timed out after 600s".to_string(),
            ));
        }
    }

    /// Type `answer` into ssh's current prompt and press Enter: a password
    /// or passphrase, a 2FA code, or `yes`/`no` to a host-key question.
    pub fn respond_to_prompt(&mut self, answer: &str) {
        if let Some(w) = self.writer.as_mut() {
            let _ = w.write_all(answer.as_bytes());
            let _ = w.write_all(b"\r");
            let _ = w.flush();
        }
    }

    pub fn cancel(&mut self) {
        if self.handed_off {
            return;
        }
        self.cancelled.store(true, Ordering::SeqCst);
        self.writer.take();
        let _ = self.child.kill();
        let _ = self.child.wait();
        if self.socket_path.exists() {
            let _ = std::process::Command::new("ssh")
                .arg("-S")
                .arg(&self.socket_path)
                .arg("-O")
                .arg("exit")
                .arg(&self.host)
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .status();
        }
        let _ = std::fs::remove_dir_all(&self.socket_dir);
        self.finished = true;
    }

    /// Surrender ownership of the established control socket to a caller
    /// that will run the rest of the croft launch flow against it. After
    /// this call the SshAuth no longer cleans the socket up on drop.
    pub fn hand_off(mut self) -> AdoptedMaster {
        self.handed_off = true;
        let adopted = AdoptedMaster {
            host: self.host.clone(),
            socket_dir: self.socket_dir.clone(),
            socket_path: self.socket_path.clone(),
        };
        std::mem::forget(self);
        adopted
    }
}

impl Drop for SshAuth {
    fn drop(&mut self) {
        if self.handed_off {
            return;
        }
        if !self.finished {
            self.cancelled.store(true, Ordering::SeqCst);
            let _ = self.child.kill();
            let _ = self.child.wait();
        }
        if !self.handed_off && self.socket_path.exists() {
            let _ = std::process::Command::new("ssh")
                .arg("-S")
                .arg(&self.socket_path)
                .arg("-O")
                .arg("exit")
                .arg(&self.host)
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .status();
        }
        if !self.handed_off && !self.socket_dir.as_os_str().is_empty() {
            let _ = std::fs::remove_dir_all(&self.socket_dir);
        }
        if let Some(h) = self.reader_handle.take() {
            let _ = h.join();
        }
    }
}

fn spawn_reader(
    mut reader: Box<dyn Read + Send>,
    events_tx: Sender<SshAuthEvent>,
    cancelled: Arc<AtomicBool>,
) -> JoinHandle<()> {
    thread::spawn(move || {
        let mut buf = [0u8; 4096];
        let mut tail = String::new();
        loop {
            if cancelled.load(Ordering::SeqCst) {
                break;
            }
            match reader.read(&mut buf) {
                Ok(0) => break,
                Ok(n) => {
                    let chunk = strip_ansi(&buf[..n]);
                    if events_tx.send(SshAuthEvent::Log(chunk.clone())).is_err() {
                        break;
                    }
                    tail.push_str(&chunk);
                    while let Some(prompt) = peel_prompt(&mut tail) {
                        if events_tx.send(prompt).is_err() {
                            return;
                        }
                    }
                    if tail.len() > 8192 {
                        let drop_from = tail.len() - 4096;
                        tail.drain(..drop_from);
                    }
                }
                Err(_) => break,
            }
        }
    })
}

/// Strip ANSI CSI/OSC escape sequences and bare control bytes that ssh
/// occasionally interleaves on the TTY path. The remaining text is what
/// the user would see, which is what we want to match prompts against.
pub fn strip_ansi(input: &[u8]) -> String {
    let mut out = String::with_capacity(input.len());
    let mut i = 0;
    while i < input.len() {
        let b = input[i];
        if b == 0x1b && i + 1 < input.len() {
            let next = input[i + 1];
            if next == b'[' {
                i += 2;
                while i < input.len() && !(0x40..=0x7e).contains(&input[i]) {
                    i += 1;
                }
                if i < input.len() {
                    i += 1;
                }
                continue;
            }
            if next == b']' {
                i += 2;
                while i < input.len() && input[i] != 0x07 && input[i] != 0x1b {
                    i += 1;
                }
                if i < input.len()
                    && input[i] == 0x1b
                    && i + 1 < input.len()
                    && input[i + 1] == b'\\'
                {
                    i += 2;
                } else if i < input.len() {
                    i += 1;
                }
                continue;
            }
            i += 2;
            continue;
        }
        if b == 0x07 || b == 0x08 {
            i += 1;
            continue;
        }
        if b == b'\r' {
            i += 1;
            continue;
        }
        out.push(b as char);
        i += 1;
    }
    out
}

/// Peel the most recent un-newlined line off `tail` and classify it as a
/// prompt if it looks like one. Lines ending in newline are kept in tail's
/// past — only the active prompt is interesting, and ssh never emits a
/// newline after a prompt until input is provided.
pub fn peel_prompt(tail: &mut String) -> Option<SshAuthEvent> {
    let nl = tail.rfind('\n').map(|i| i + 1).unwrap_or(0);
    let active = &tail[nl..];
    if active.is_empty() {
        return None;
    }
    let kind = classify_prompt(active)?;
    let line = active.to_string();
    tail.drain(nl..);
    Some(SshAuthEvent::Prompt { kind, line })
}

pub fn classify_prompt(line: &str) -> Option<PromptKind> {
    let lower = line.to_lowercase();
    if !lower.contains("password")
        && !lower.contains("passphrase")
        && !lower.contains("verification code")
        && !lower.contains("(yes/no")
        && !lower.contains("are you sure you want to continue connecting")
        && !lower.ends_with(": ")
        && !lower.ends_with("? ")
    {
        return None;
    }
    if lower.contains("are you sure you want to continue connecting") || lower.contains("(yes/no") {
        return Some(PromptKind::HostKeyConfirmation);
    }
    if lower.contains("verification code") {
        return Some(PromptKind::VerificationCode);
    }
    if lower.contains("passphrase") {
        return Some(PromptKind::Passphrase);
    }
    if lower.contains("password") {
        return Some(PromptKind::Password);
    }
    Some(PromptKind::Generic)
}

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

    #[test]
    fn classifies_lowercase_password_prompt() {
        assert_eq!(
            classify_prompt("user@host's password: "),
            Some(PromptKind::Password)
        );
    }

    #[test]
    fn classifies_passphrase_prompt() {
        assert_eq!(
            classify_prompt("Enter passphrase for key '/Users/v/.ssh/id_ed25519': "),
            Some(PromptKind::Passphrase)
        );
    }

    #[test]
    fn classifies_2fa_prompt() {
        assert_eq!(
            classify_prompt("Verification code: "),
            Some(PromptKind::VerificationCode)
        );
    }

    #[test]
    fn classifies_known_hosts_confirmation() {
        let line = "Are you sure you want to continue connecting (yes/no/[fingerprint])? ";
        assert_eq!(classify_prompt(line), Some(PromptKind::HostKeyConfirmation));
    }

    #[test]
    fn does_not_classify_normal_log_lines() {
        assert_eq!(
            classify_prompt("debug1: Authenticating to host:22 as 'user'"),
            None
        );
        assert_eq!(classify_prompt(""), None);
    }

    #[test]
    fn strip_ansi_keeps_text_drops_csi() {
        let raw = b"\x1b[31mred\x1b[0m text\r\nnext line";
        let out = strip_ansi(raw);
        assert_eq!(out, "red text\nnext line");
    }

    #[test]
    fn strip_ansi_drops_osc_with_bel_terminator() {
        let raw = b"\x1b]0;title\x07after";
        let out = strip_ansi(raw);
        assert_eq!(out, "after");
    }

    #[test]
    fn peel_prompt_extracts_password_from_partial_line() {
        let mut tail = String::from("Last login: foo\nuser@host's password: ");
        let ev = peel_prompt(&mut tail).expect("password tail must produce a prompt event");
        match ev {
            SshAuthEvent::Prompt {
                kind: PromptKind::Password,
                line,
            } => {
                assert!(line.contains("password"));
            }
            other => panic!("expected Password prompt, got {other:?}"),
        }
    }

    #[test]
    fn peel_prompt_returns_none_when_only_log_lines() {
        let mut tail = String::from("debug1: client_loop: send disconnect: Broken pipe\n");
        assert!(peel_prompt(&mut tail).is_none());
    }

    #[test]
    fn control_socket_existence_is_a_valid_authenticated_signal() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let sock = tmp.path().join("ctl");
        assert!(!sock.exists());
        std::fs::write(&sock, b"").expect("touch fake socket");
        assert!(
            sock.exists(),
            "ssh -M -N keeps the child running after auth, so the socket file appearing is the only reliable success signal"
        );
    }

    #[test]
    fn peel_prompt_consumes_the_prompt_so_repeated_calls_do_not_loop() {
        let mut tail = String::from("debug1: connected\nuser@host's password: ");
        let first = peel_prompt(&mut tail);
        assert!(matches!(first, Some(SshAuthEvent::Prompt { .. })));
        let second = peel_prompt(&mut tail);
        assert!(
            second.is_none(),
            "second call must return None or the reader thread spams the channel forever"
        );
    }
}