armybox 0.3.0

A memory-safe #[no_std] BusyBox/Toybox clone in Rust - 299 Unix utilities in ~500KB
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
//! telnet - user interface to the TELNET protocol
//!
//! Simple telnet client for remote terminal connections.

use crate::io;
use crate::sys;
use super::get_arg;

// Telnet protocol constants
const IAC: u8 = 255;  // Interpret As Command
const DONT: u8 = 254;
const DO: u8 = 253;
const WONT: u8 = 252;
const WILL: u8 = 251;
const SB: u8 = 250;   // Subnegotiation Begin
const SE: u8 = 240;   // Subnegotiation End

// Telnet options
const TELOPT_ECHO: u8 = 1;
const TELOPT_SGA: u8 = 3;    // Suppress Go Ahead
const TELOPT_TTYPE: u8 = 24; // Terminal Type
const TELOPT_NAWS: u8 = 31;  // Window Size

/// telnet - user interface to the TELNET protocol
///
/// # Synopsis
/// ```text
/// telnet HOST [PORT]
/// ```
///
/// # Description
/// Connect to a host using the TELNET protocol for remote terminal access.
/// Supports basic telnet option negotiation.
///
/// # Exit Status
/// - 0: Success
/// - 1: Error
#[cfg(target_os = "linux")]
pub fn telnet(argc: i32, argv: *const *const u8) -> i32 {
    if argc < 2 {
        io::write_str(2, b"Usage: telnet HOST [PORT]\n");
        return 1;
    }

    let host = match unsafe { get_arg(argv, 1) } {
        Some(h) => h,
        None => {
            io::write_str(2, b"telnet: missing host\n");
            return 1;
        }
    };

    let port: u16 = if argc > 2 {
        match unsafe { get_arg(argv, 2) }.and_then(|p| sys::parse_u64(p)) {
            Some(p) => p as u16,
            None => 23,
        }
    } else {
        23 // Default telnet port
    };

    // Resolve host
    let addr = match resolve_host(host, port) {
        Some(a) => a,
        None => {
            io::write_str(2, b"telnet: cannot resolve ");
            io::write_all(2, host);
            io::write_str(2, b"\n");
            return 1;
        }
    };

    // Create socket
    let sock = unsafe { libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0) };
    if sock < 0 {
        io::write_str(2, b"telnet: cannot create socket\n");
        return 1;
    }

    io::write_str(1, b"Trying ");
    print_addr(&addr);
    io::write_str(1, b"...\n");

    // Connect
    let ret = unsafe {
        libc::connect(
            sock,
            &addr as *const _ as *const libc::sockaddr,
            core::mem::size_of::<libc::sockaddr_in>() as libc::socklen_t,
        )
    };

    if ret < 0 {
        io::write_str(2, b"telnet: Unable to connect to remote host: Connection refused\n");
        unsafe { libc::close(sock) };
        return 1;
    }

    io::write_str(1, b"Connected to ");
    io::write_all(1, host);
    io::write_str(1, b".\nEscape character is '^]'.\n");

    // Set terminal to raw mode
    let mut orig_termios: libc::termios = unsafe { core::mem::zeroed() };
    unsafe {
        libc::tcgetattr(0, &mut orig_termios);
        let mut raw = orig_termios;
        raw.c_lflag &= !(libc::ECHO | libc::ICANON | libc::ISIG | libc::IEXTEN);
        raw.c_iflag &= !(libc::IXON | libc::ICRNL | libc::BRKINT | libc::INPCK | libc::ISTRIP);
        raw.c_oflag &= !libc::OPOST;
        raw.c_cc[libc::VMIN] = 1;
        raw.c_cc[libc::VTIME] = 0;
        libc::tcsetattr(0, libc::TCSAFLUSH, &raw);
    }

    // Set socket to non-blocking for select
    unsafe {
        let flags = libc::fcntl(sock, libc::F_GETFL, 0);
        libc::fcntl(sock, libc::F_SETFL, flags | libc::O_NONBLOCK);

        let flags = libc::fcntl(0, libc::F_GETFL, 0);
        libc::fcntl(0, libc::F_SETFL, flags | libc::O_NONBLOCK);
    }

    let mut telnet_state = TelnetState::new();
    let mut running = true;

    while running {
        // Use select to wait for input from either stdin or socket
        let mut read_fds: libc::fd_set = unsafe { core::mem::zeroed() };
        unsafe {
            libc::FD_ZERO(&mut read_fds);
            libc::FD_SET(0, &mut read_fds);
            libc::FD_SET(sock, &mut read_fds);
        }

        let mut timeout = libc::timeval {
            tv_sec: 1,
            tv_usec: 0,
        };

        let nfds = sock + 1;
        let ret = unsafe {
            libc::select(
                nfds,
                &mut read_fds,
                core::ptr::null_mut(),
                core::ptr::null_mut(),
                &mut timeout,
            )
        };

        if ret < 0 {
            break;
        }

        // Check for input from socket
        if unsafe { libc::FD_ISSET(sock, &read_fds) } {
            let mut buf = [0u8; 1024];
            let n = unsafe {
                libc::recv(sock, buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
            };

            if n <= 0 {
                io::write_str(1, b"\nConnection closed by foreign host.\n");
                running = false;
            } else {
                // Process telnet protocol and output data
                let mut i = 0;
                while i < n as usize {
                    if buf[i] == IAC && i + 1 < n as usize {
                        // Telnet command
                        let cmd = buf[i + 1];
                        match cmd {
                            IAC => {
                                // Escaped 255
                                io::write_all(1, &[255]);
                                i += 2;
                            }
                            DO | DONT | WILL | WONT if i + 2 < n as usize => {
                                let opt = buf[i + 2];
                                handle_telnet_option(sock, cmd, opt, &mut telnet_state);
                                i += 3;
                            }
                            SB => {
                                // Subnegotiation - find SE
                                let mut j = i + 2;
                                while j + 1 < n as usize {
                                    if buf[j] == IAC && buf[j + 1] == SE {
                                        // Handle subnegotiation (skip for now)
                                        j += 2;
                                        break;
                                    }
                                    j += 1;
                                }
                                i = j;
                            }
                            _ => {
                                i += 2;
                            }
                        }
                    } else {
                        // Regular data
                        io::write_all(1, &buf[i..i + 1]);
                        i += 1;
                    }
                }
            }
        }

        // Check for input from stdin
        if unsafe { libc::FD_ISSET(0, &read_fds) } {
            let mut buf = [0u8; 256];
            let n = io::read(0, &mut buf);

            if n > 0 {
                for i in 0..n as usize {
                    if buf[i] == 0x1D {
                        // Ctrl+] - escape character
                        io::write_str(1, b"\ntelnet> ");
                        // Read command
                        let mut cmd_buf = [0u8; 64];

                        // Temporarily restore terminal
                        unsafe { libc::tcsetattr(0, libc::TCSAFLUSH, &orig_termios) };

                        let cn = io::read(0, &mut cmd_buf);

                        // Back to raw mode
                        unsafe {
                            let mut raw = orig_termios;
                            raw.c_lflag &= !(libc::ECHO | libc::ICANON | libc::ISIG);
                            raw.c_iflag &= !(libc::IXON | libc::ICRNL);
                            libc::tcsetattr(0, libc::TCSAFLUSH, &raw);
                        }

                        if cn > 0 {
                            let cmd = &cmd_buf[..cn as usize];
                            if cmd.starts_with(b"quit") || cmd.starts_with(b"q") || cmd == b"\n" {
                                running = false;
                            } else if cmd.starts_with(b"close") || cmd.starts_with(b"c") {
                                running = false;
                            }
                        }
                    } else {
                        // Send to remote
                        let byte = buf[i];
                        if byte == IAC {
                            // Escape IAC
                            let escaped = [IAC, IAC];
                            unsafe {
                                libc::send(sock, escaped.as_ptr() as *const libc::c_void, 2, 0);
                            }
                        } else {
                            unsafe {
                                libc::send(sock, &byte as *const u8 as *const libc::c_void, 1, 0);
                            }
                        }
                    }
                }
            }
        }
    }

    // Restore terminal
    unsafe {
        libc::tcsetattr(0, libc::TCSAFLUSH, &orig_termios);
        libc::close(sock);
    }

    0
}

#[cfg(not(target_os = "linux"))]
pub fn telnet(_argc: i32, _argv: *const *const u8) -> i32 {
    io::write_str(2, b"telnet: only available on Linux\n");
    1
}

#[cfg(target_os = "linux")]
struct TelnetState {
    echo: bool,
    sga: bool,
}

#[cfg(target_os = "linux")]
impl TelnetState {
    fn new() -> Self {
        TelnetState {
            echo: false,
            sga: false,
        }
    }
}

#[cfg(target_os = "linux")]
fn handle_telnet_option(sock: i32, cmd: u8, opt: u8, state: &mut TelnetState) {
    let response = match cmd {
        DO => {
            // Server asks us to do something
            match opt {
                TELOPT_TTYPE | TELOPT_NAWS => {
                    // We can do these
                    [IAC, WILL, opt]
                }
                _ => {
                    // We won't do other things
                    [IAC, WONT, opt]
                }
            }
        }
        DONT => {
            [IAC, WONT, opt]
        }
        WILL => {
            // Server will do something
            match opt {
                TELOPT_ECHO => {
                    state.echo = true;
                    [IAC, DO, opt]
                }
                TELOPT_SGA => {
                    state.sga = true;
                    [IAC, DO, opt]
                }
                _ => {
                    [IAC, DONT, opt]
                }
            }
        }
        WONT => {
            match opt {
                TELOPT_ECHO => state.echo = false,
                TELOPT_SGA => state.sga = false,
                _ => {}
            }
            [IAC, DONT, opt]
        }
        _ => return,
    };

    unsafe {
        libc::send(sock, response.as_ptr() as *const libc::c_void, 3, 0);
    }
}

#[cfg(target_os = "linux")]
fn resolve_host(host: &[u8], port: u16) -> Option<libc::sockaddr_in> {
    // Try to parse as IP address first
    if let Some(ip) = parse_ipv4(host) {
        let mut addr: libc::sockaddr_in = unsafe { core::mem::zeroed() };
        addr.sin_family = libc::AF_INET as u16;
        addr.sin_port = port.to_be();
        addr.sin_addr.s_addr = ip.to_be();
        return Some(addr);
    }

    // Use getaddrinfo for DNS resolution
    let mut host_cstr = [0u8; 256];
    let len = core::cmp::min(host.len(), 255);
    host_cstr[..len].copy_from_slice(&host[..len]);
    host_cstr[len] = 0;

    let mut hints: libc::addrinfo = unsafe { core::mem::zeroed() };
    hints.ai_family = libc::AF_INET;
    hints.ai_socktype = libc::SOCK_STREAM;

    let mut result: *mut libc::addrinfo = core::ptr::null_mut();

    let ret = unsafe {
        libc::getaddrinfo(
            host_cstr.as_ptr() as *const libc::c_char,
            core::ptr::null(),
            &hints,
            &mut result,
        )
    };

    if ret != 0 || result.is_null() {
        return None;
    }

    let addr = unsafe {
        let ai = &*result;
        if ai.ai_family == libc::AF_INET && !ai.ai_addr.is_null() {
            let mut sin = *(ai.ai_addr as *const libc::sockaddr_in);
            sin.sin_port = port.to_be();
            Some(sin)
        } else {
            None
        }
    };

    unsafe { libc::freeaddrinfo(result) };

    addr
}

#[cfg(target_os = "linux")]
fn parse_ipv4(s: &[u8]) -> Option<u32> {
    let mut parts = [0u8; 4];
    let mut part_idx = 0;
    let mut current: u16 = 0;
    let mut has_digit = false;

    for &c in s {
        if c == b'.' {
            if !has_digit || part_idx >= 3 || current > 255 {
                return None;
            }
            parts[part_idx] = current as u8;
            part_idx += 1;
            current = 0;
            has_digit = false;
        } else if c >= b'0' && c <= b'9' {
            current = current * 10 + (c - b'0') as u16;
            has_digit = true;
            if current > 255 {
                return None;
            }
        } else {
            return None;
        }
    }

    if !has_digit || part_idx != 3 || current > 255 {
        return None;
    }
    parts[3] = current as u8;

    Some(((parts[0] as u32) << 24) |
         ((parts[1] as u32) << 16) |
         ((parts[2] as u32) << 8) |
         (parts[3] as u32))
}

#[cfg(target_os = "linux")]
fn print_addr(addr: &libc::sockaddr_in) {
    let ip = u32::from_be(addr.sin_addr.s_addr);
    let port = u16::from_be(addr.sin_port);
    let mut buf = [0u8; 16];

    io::write_all(1, sys::format_u64(((ip >> 24) & 0xFF) as u64, &mut buf));
    io::write_str(1, b".");
    io::write_all(1, sys::format_u64(((ip >> 16) & 0xFF) as u64, &mut buf));
    io::write_str(1, b".");
    io::write_all(1, sys::format_u64(((ip >> 8) & 0xFF) as u64, &mut buf));
    io::write_str(1, b".");
    io::write_all(1, sys::format_u64((ip & 0xFF) as u64, &mut buf));
    io::write_str(1, b":");
    io::write_all(1, sys::format_u64(port as u64, &mut buf));
}

#[cfg(test)]
mod tests {
    extern crate std;
    use std::process::Command;
    use std::path::PathBuf;

    fn get_armybox_path() -> PathBuf {
        if let Ok(path) = std::env::var("ARMYBOX_PATH") {
            return PathBuf::from(path);
        }
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|_| std::env::current_dir().unwrap());
        let release = manifest_dir.join("target/release/armybox");
        if release.exists() { return release; }
        manifest_dir.join("target/debug/armybox")
    }

    #[test]
    fn test_telnet_no_args() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

        let output = Command::new(&armybox)
            .args(["telnet"])
            .output()
            .unwrap();

        assert_eq!(output.status.code(), Some(1));
        let stderr = std::string::String::from_utf8_lossy(&output.stderr);
        assert!(stderr.contains("Usage"));
    }

    #[test]
    fn test_parse_ipv4() {
        assert_eq!(super::parse_ipv4(b"127.0.0.1"), Some(0x7F000001));
        assert_eq!(super::parse_ipv4(b"192.168.1.1"), Some(0xC0A80101));
        assert_eq!(super::parse_ipv4(b"invalid"), None);
    }
}