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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! The interactive shell.

use crate::busybox;
use clap;

use crate::Error;
use crate::ErrorKind;
use crate::ctrl::{Interface, PromptError};
pub use crate::ffi::ForeignCommand;
use std::io;
use std::io::prelude::*;
use std::sync::Arc;
use std::sync::Mutex;
use std::collections::HashMap;

#[cfg(unix)]
use std::os::unix::io::RawFd;

#[cfg(unix)]
use crate::ffi::daemonize;


#[derive(Clone)]
pub enum Command {
    Native(NativeCommand),
    Foreign(ForeignCommand),
}

pub type NativeCommand = fn(&mut Shell, Vec<String>) -> Result<(), Error>;


impl Command {
    pub fn run(&self, mut sh: &mut Shell, args: Vec<String>) -> Result<(), Error> {
        use self::Command::*;
        match *self {
            Native(ref func)  => func(&mut sh, args),
            Foreign(ref func) => func.run(args),
        }
    }

    #[cfg(unix)]
    pub fn daemonized(&self, sh: &Shell, args: Vec<String>) -> Result<(), Error> {
        daemonize(sh.daemon_clone(), self.clone(), args)
    }

    #[cfg(not(unix))]
    pub fn daemonized(&self, sh: &Shell, args: Vec<String>) -> Result<(), Error> {
        // not implemented, execute in same process
        self.run(&mut sh.daemon_clone(), args)
    }
}


impl From<NativeCommand> for Command {
    fn from(cmd: NativeCommand) -> Command {
        Command::Native(cmd)
    }
}

impl From<ForeignCommand> for Command {
    fn from(cmd: ForeignCommand) -> Command {
        Command::Foreign(cmd)
    }
}


/// The set of registered commands.
#[derive(Default)]
pub struct Toolbox(HashMap<String, Command>);

impl Toolbox {
    /// Create an empty toolbox.
    ///
    /// ```
    /// use boxxy::Toolbox;
    ///
    /// let toolbox = Toolbox::empty();
    /// ```
    #[inline]
    pub fn empty() -> Toolbox {
        Toolbox(HashMap::new())
    }

    /// Create a toolbox that contains the default builtin commands.
    ///
    /// ```
    /// use boxxy::Toolbox;
    ///
    /// let toolbox = Toolbox::new();
    /// ```
    #[inline]
    pub fn new() -> Toolbox {
        let mut toolbox = Toolbox::empty();
        toolbox.insert_many_native(vec![
            ("cat"          , busybox::cat),
            ("cd"           , busybox::cd),
            ("downgrade"    , busybox::downgrade),
            ("echo"         , busybox::echo),
            ("exec"         , busybox::exec),
            ("grep"         , busybox::grep),
            ("help"         , busybox::help),
            ("ls"           , busybox::ls),
            ("mkdir"        , busybox::mkdir),
            ("pwd"          , busybox::pwd),
            ("rm"           , busybox::rm),
        ]);

        #[cfg(unix)]
        toolbox.insert_many_native(vec![
            ("chmod"        , busybox::chmod),
            ("chown"        , busybox::chown),
            ("chroot"       , busybox::chroot),
            ("fchdir"       , busybox::fchdir),
            ("fds"          , busybox::fds),
            ("jit"          , busybox::jit),
            ("id"           , busybox::id),
            ("setgroups"    , busybox::setgroups),
            ("setgid"       , busybox::setgid),
            ("setuid"       , busybox::setuid),
            ("seteuid"      , busybox::seteuid),
        ]);

        #[cfg(target_os="linux")]
        toolbox.insert_many_native(vec![
            ("caps"         , busybox::caps),
            ("mount"        , busybox::mount),
            ("keepcaps"     , busybox::keepcaps),
            ("setresgid"    , busybox::setresgid),
            ("setresuid"    , busybox::setresuid),
            ("setreuid"     , busybox::setreuid),
        ]);

        #[cfg(target_os="openbsd")]
        toolbox.insert_many_native(vec![
            ("pledge"       , busybox::pledge),
        ]);

        #[cfg(feature="archives")]
        toolbox.insert_many_native(vec![
            ("tar"          , busybox::tar),
        ]);

        #[cfg(feature="network")]
        toolbox.insert_many_native(vec![
            ("curl"         , busybox::curl),
            ("revshell"     , busybox::revshell),
            #[cfg(unix)]
            ("ipcshell"     , busybox::ipcshell),
        ]);

        toolbox
    }

    /// Get a command by its name.
    ///
    /// ```
    /// use boxxy::Toolbox;
    ///
    /// let toolbox = Toolbox::new();
    /// println!("command: {:?}", toolbox.get("cat").is_some());
    /// ```
    #[inline]
    pub fn get(&self, key: &str) -> Option<&Command> {
        self.0.get(key)
    }

    /// List available commands.
    ///
    /// ```
    /// use boxxy::Toolbox;
    ///
    /// let toolbox = Toolbox::new();
    /// println!("commands: {:?}", toolbox.keys());
    /// ```
    #[inline]
    pub fn keys(&self) -> Vec<String> {
        self.0
            .keys()
            .map(|x| x.to_owned())
            .collect()
    }

    /// Insert a command into the toolbox.
    ///
    /// ```
    /// #[macro_use] extern crate boxxy;
    /// use boxxy::{Toolbox, Command};
    ///
    /// fn example(sh: &mut boxxy::Shell, args: Vec<String>) -> Result<(), boxxy::Error> {
    ///     shprintln!(sh, "The world is your oyster! {:?}", args);
    ///     Ok(())
    /// }
    ///
    /// fn main() {
    ///     let mut toolbox = Toolbox::new();
    ///     toolbox.insert("example", Command::Native(example));
    ///     println!("commands: {:?}", toolbox.keys());
    /// }
    /// ```
    #[inline]
    pub fn insert<I: Into<String>>(&mut self, key: I, func: Command) {
        self.0.insert(key.into(), func);
    }

    /// Insert many commands into the toolbox.
    ///
    /// ```
    /// #[macro_use] extern crate boxxy;
    /// use boxxy::{Toolbox, Command};
    ///
    /// fn example1(sh: &mut boxxy::Shell, _args: Vec<String>) -> Result<(), boxxy::Error> {
    ///     shprintln!(sh, "example1");
    ///     Ok(())
    /// }
    ///
    /// fn example2(sh: &mut boxxy::Shell, _args: Vec<String>) -> Result<(), boxxy::Error> {
    ///     shprintln!(sh, "example2");
    ///     Ok(())
    /// }
    ///
    /// fn main() {
    ///     let mut toolbox = Toolbox::new();
    ///     toolbox.insert_many(vec![
    ///         ("example1", Command::Native(example1)),
    ///         ("example2", Command::Native(example2)),
    ///     ]);
    ///     println!("commands: {:?}", toolbox.keys());
    /// }
    /// ```
    #[inline]
    pub fn insert_many<I: Into<String>>(&mut self, commands: Vec<(I, Command)>) {
        for (key, func) in commands {
            self.insert(key, func);
        }
    }

    /// Insert many [`NativeCommand`]s into the toolbox.
    ///
    /// [`NativeCommand`]: struct.NativeCommand.html
    ///
    /// ```
    /// #[macro_use] extern crate boxxy;
    /// use boxxy::Toolbox;
    ///
    /// fn example1(sh: &mut boxxy::Shell, _args: Vec<String>) -> Result<(), boxxy::Error> {
    ///     shprintln!(sh, "example1");
    ///     Ok(())
    /// }
    ///
    /// fn example2(sh: &mut boxxy::Shell, _args: Vec<String>) -> Result<(), boxxy::Error> {
    ///     shprintln!(sh, "example2");
    ///     Ok(())
    /// }
    ///
    /// fn main() {
    ///     let mut toolbox = Toolbox::new();
    ///     toolbox.insert_many_native(vec![
    ///         ("example1", example1),
    ///         ("example2", example2),
    ///     ]);
    ///     println!("commands: {:?}", toolbox.keys());
    /// }
    /// ```
    #[inline]
    pub fn insert_many_native<I: Into<String>>(&mut self, commands: Vec<(I, NativeCommand)>) {
        for (key, func) in commands {
            self.insert(key, func.into());
        }
    }

    /// Builder pattern to create a toolbox with custom commands.
    ///
    /// ```
    /// #[macro_use] extern crate boxxy;
    /// use boxxy::Toolbox;
    ///
    /// fn example(sh: &mut boxxy::Shell, args: Vec<String>) -> Result<(), boxxy::Error> {
    ///     shprintln!(sh, "The world is your oyster! {:?}", args);
    ///     Ok(())
    /// }
    ///
    /// fn main() {
    ///    let toolbox = Toolbox::new().with(vec![
    ///            ("example", example),
    ///        ]);
    ///    println!("commands: {:?}", toolbox.keys());
    /// }
    /// ```
    #[inline]
    pub fn with<I: Into<String>>(mut self, commands: Vec<(I, NativeCommand)>) -> Toolbox {
        self.insert_many_native(commands);
        self
    }
}


/// The struct that keeps track of the user interface.
pub struct Shell {
    ui: Interface,
    toolbox: Arc<Mutex<Toolbox>>,
}

impl Read for Shell {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.ui.read(buf)
    }
}

impl Write for Shell {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.ui.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.ui.flush()
    }
}

impl Shell {
    /// Initializes a shell. Takes a [`Toolbox`] that contains the available
    /// commands. The toolbox is also used to configure tab completion.
    ///
    /// [`Toolbox`]: struct.Toolbox.html
    ///
    /// ```
    /// use boxxy::{Shell, Toolbox};
    ///
    /// let toolbox = Toolbox::new();
    /// let shell = Shell::new(toolbox);
    /// ```
    pub fn new(toolbox: Toolbox) -> Shell {
        let toolbox = Arc::new(Mutex::new(toolbox));

        let ui = Interface::default(&toolbox);

        Shell {
            ui,
            toolbox,
        }
    }

    /// Replace the readline interface with a plain stdin/stdout interface.
    ///
    /// ```
    /// use boxxy::{Shell, Toolbox};
    ///
    /// let toolbox = Toolbox::new();
    /// let mut shell = Shell::new(toolbox);
    /// shell.downgrade();
    /// shell.run();
    /// ```
    #[inline]
    pub fn downgrade(&mut self) {
        match self.ui {
            #[cfg(feature="readline")]
            Interface::Fancy(_) => {
                self.ui = Interface::stdio();
            },
            _ => shprintln!(self, "[-] interface is already downgraded"),
        }
    }

    #[inline]
    pub fn list_commands(&self) -> Vec<String> {
        let toolbox = self.toolbox.lock().unwrap();
        toolbox.keys()
    }

    #[inline]
    pub fn hotswap(&mut self, ui: Interface) {
        self.ui = ui;
    }

    #[cfg(unix)]
    #[inline]
    pub fn pipe(&mut self) -> Option<(RawFd, RawFd, RawFd)> {
        self.ui.pipe()
    }

    /// Insert a [`Command`] into the [`Toolbox`].
    ///
    /// [`Toolbox`]: struct.Toolbox.html
    /// [`Command`]: enum.Command.html
    /// ```
    /// use boxxy::{self, Shell, Command, Toolbox};
    ///
    /// let toolbox = Toolbox::empty();
    /// let mut shell = Shell::new(toolbox);
    /// shell.insert("ls", Command::Native(boxxy::busybox::ls));
    /// ```
    #[inline]
    pub fn insert<I: Into<String>>(&mut self, name: I, command: Command) {
        let mut toolbox = self.toolbox.lock().unwrap();
        toolbox.insert(name, command);
    }

    fn process(&mut self, cmd: InputCmd) {
        debug!("cmd: {:?}", cmd);

        let result: Option<Command> = {
            let toolbox = self.toolbox.lock().unwrap();
            match toolbox.get(&cmd.prog) {
                Some(x) => Some(x.clone()),
                None => None,
            }
        };

        let result = match (result, cmd.bg) {
            (Some(func), true) => func.daemonized(&self, cmd.args),
            (Some(func), false) => func.run(self, cmd.args),
            (None, _) => Err(ErrorKind::Args(clap::Error {
                message: String::from("\u{1b}[1;31merror:\u{1b}[0m unknown command"),
                kind: clap::ErrorKind::MissingRequiredArgument,
                info: None,
            }).into()),
        };

        if let Err(err) = result {
            match *err.kind() {
                ErrorKind::Args(ref err)    => shprintln!(self, "{}", err.message),
                _                           => shprintln!(self, "error: {:?}", err),
            }
        }
    }

    fn daemon_clone(&self) -> Shell {
        let toolbox = self.toolbox.clone();
        let ui = Interface::default(&toolbox);
        Shell {
            ui,
            toolbox,
        }
    }

    #[inline]
    fn prompt(&mut self) -> Result<String, PromptError> {
        self.ui.readline(" [%]> ")
    }

    #[inline]
    fn get_line(&mut self) -> Result<Option<InputCmd>, ()> {
        let readline = self.prompt();

        match readline {
            Ok(line) => {
                #[cfg(feature="readline")]
                self.ui.add_history_entry(line.as_ref());
                Ok(parse_line(&line))
            },
            Err(_) => Err(()),
        }
    }

    #[inline]
    pub fn exec_once(&mut self, line: &str) {
        if let Some(cmd) = parse_line(line) {
            self.process(cmd);
        }
    }

    /// Run the input loop. This doesn't return until the shell is exited.
    ///
    /// ```
    /// use boxxy::{Shell, Toolbox};
    ///
    /// let toolbox = Toolbox::new();
    /// let mut shell = Shell::new(toolbox);
    ///
    /// // run the loop
    /// shell.run();
    /// ```
    pub fn run(&mut self) {
        loop {
            match self.get_line() {
                Ok(Some(cmd)) => self.process(cmd),
                Ok(None) => (),
                Err(_) => break,
            }
        }
    }
}


#[inline]
fn tokenize(line: &str) -> Vec<String> {
    let mut cmd = Vec::new();
    let mut token = String::new();

    let mut escape = false;
    for x in line.chars() {
        if escape {
            token.push(x);
            escape = false;
            continue;
        }

        match x {
            ' ' | '\n' => {
                if !token.is_empty() {
                    cmd.push(token);
                    token = String::new();
                }
            },
            '\\' => {
                escape = true;
            },
            x => {
                token.push(x);
            },
        }
    }

    if !token.is_empty() {
        cmd.push(token);
    }

    cmd
}


#[derive(Debug, PartialEq)]
pub struct InputCmd {
    prog: String,
    args: Vec<String>,
    bg: bool,
}


#[inline]
fn parse_line(line: &str) -> Option<InputCmd> {
    trace!("line: {:?}", line);
    if is_comment(&line) {
        return None;
    }

    let (bg, line) = if line.ends_with(" &") {
        (true, &line[..line.len()-2])
    } else {
        (false, line)
    };

    let cmd = tokenize(&line);
    debug!("got {:?}", cmd);

    if cmd.is_empty() {
        None
    } else {
        let prog = cmd[0].clone();
        Some(InputCmd {
            prog,
            args: cmd,
            bg,
        })
    }
}


#[inline]
fn is_comment(line: &str) -> bool {
    for x in line.chars() {
        match x {
            '#' => return true,
            ' ' => (),
            _   => return false,
        }
    }

    false
}


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

    #[test]
    fn test_parse() {
        let cmd = tokenize("foo\\  \\\\bar");
        assert_eq!(cmd, vec!["foo ", "\\bar"]);
    }

    #[test]
    fn test_empty() {
        let cmd = tokenize("");

        let expected: Vec<String> = Vec::new();
        assert_eq!(expected, cmd);
    }

    #[test]
    fn test_newline() {
        let cmd = tokenize("\n");

        let expected: Vec<String> = Vec::new();
        assert_eq!(expected, cmd);
    }

    #[test]
    fn test_is_comment() {
        assert_eq!(false, is_comment("hello world"));
        assert_eq!(true, is_comment("#hello world"));
        assert_eq!(false, is_comment("hello #world"));
        assert_eq!(false, is_comment(""));
        assert_eq!(false, is_comment("  "));
        assert_eq!(true, is_comment("  # x"));
    }

    #[test]
    fn test_bg() {
        let cmd = parse_line("foo bar &");
        assert_eq!(Some(InputCmd {
            prog: "foo".to_string(),
            args: vec!["foo".to_string(), "bar".to_string()],
            bg: true,
        }), cmd);
    }
}