Skip to content

Commit

Permalink
Added disable/nohup and refactored noop commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlewis42 committed Dec 24, 2023
1 parent 41b0936 commit f3f82c7
Show file tree
Hide file tree
Showing 51 changed files with 291 additions and 426 deletions.
178 changes: 178 additions & 0 deletions commands/aaa_noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package commands

import (
"fmt"

"github.com/josephlewis42/honeyssh/core/vos"
)

// No-op commands.
type NoOpCommand struct {
Name string
Use string
Short string
Stdout string
ExitCode int
}

// Convert the no-op command description to a functioning command.
func (c *NoOpCommand) ToCommand() vos.ProcessFunc {
return func(virtOS vos.VOS) int {
cmd := &SimpleCommand{
Use: c.Use,
Short: c.Short,
// Never bail, even if args are bad.
NeverBail: true,
}

return cmd.Run(virtOS, func() int {
if c.Stdout != "" {
w := virtOS.Stdout()
fmt.Fprintln(w, c.Stdout)
}

return c.ExitCode
})
}
}

var noOpBinCommands = []NoOpCommand{
{
Name: "kill",
Use: "kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]",
Short: "Send a signal to a process.",
},
{
Name: "killall",
Use: "killall [OPTION]... [--] NAME...",
Short: "Kill a process by name.",
},
{
Name: "lscpu",
Use: "lscpu [OPTION...]",
Short: "Display information about the CPU architecture.",
Stdout: mustDedent(`
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 40 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Vendor ID: GenuineIntel
BIOS Vendor ID: unknown
Model name: unknown
BIOS CPU family: 1
CPU family: 6
Model: 63
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
Stepping: 2
BogoMIPS: 1234.59
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx
fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm pni pclmulqdq dtes64 monitor ds_cpl
vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer
aes xsave avx xsaveopt
Virtualization features:
Virtualization: VT-x
Hypervisor vendor: KVM
Virtualization type: full
Caches (sum of all):
L1d: 32 KiB (1 instance)
L1i: 32 KiB (1 instance)
L2: 4 MiB (1 instance)
NUMA:
NUMA node(s): 1
NUMA node0 CPU(s): 0
`),
},
{
Name: "lspci",
Use: "lspci [OPTION...]",
Short: "List PCI devices.",
Stdout: mustDedent(`
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371SB PIIX3 IDE [Natoma/Triton II]
00:01.2 USB controller: Intel Corporation 82371SB PIIX3 USB [Natoma/Triton II] (rev 01)
00:01.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 03)
00:02.0 VGA compatible controller: Red Hat, Inc. Virtio 1.0 GPU (rev 01)
00:03.0 Ethernet controller: Red Hat, Inc. Virtio network device
00:04.0 Ethernet controller: Red Hat, Inc. Virtio network device
00:05.0 SCSI storage controller: Red Hat, Inc. Virtio SCSI
00:06.0 SCSI storage controller: Red Hat, Inc. Virtio block device
00:07.0 SCSI storage controller: Red Hat, Inc. Virtio block device
00:08.0 Unclassified device [00ff]: Red Hat, Inc. Virtio memory balloon`),
},
{
Name: "lsusb",
Use: "lsusb [OPTION...]",
Short: "List USB devices.",
Stdout: "Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub",
},
{
Name: "make",
Use: "make [options] [target] ...",
Short: "Run a dependency graph of commands.",
Stdout: "make: *** No rule to make target. Stop.",
ExitCode: 1,
},
{
Name: "nohup",
Use: "nohup COMMAND [ARG]...",
Short: "Run COMMAND, ignoring hangup signals.",
},
{
Name: "perl",
Use: "perl [switches] [--] [programfile] [arguments]",
Short: "The Perl 5 language interpreter.",
Stdout: "Can't locate perl5db.pl: No such file or directory",
ExitCode: 1,
},
{
Name: "php",
Use: "php [options] [-f] <file> [--] [args...]",
Short: "PHP Command Line Interface.",
Stdout: "PHP: Error parsing php.ini on line 424",
ExitCode: 1,
},
{
Name: "pkill",
Use: "pkill [OPTION]... PATTERN",
Short: "Signal a process by pattern",
},
{
Name: "python",
Use: "python [option] ... [-c cmd | -m mod | file | -] [arg] ...",
Short: "Embedded version of the Python language.",
Stdout: "python: No module named os",
ExitCode: 1,
},
{
Name: "python3",
Use: "python3 [option] ... [-c cmd | -m mod | file | -] [arg] ...",
Short: "Embedded version of the Python language.",
Stdout: "python: No module named os",
ExitCode: 1,
},
{
Name: "screen",
Use: "screen [-opts] [cmd [args]]",
Short: "screen manager with VT100/ANSI terminal emulation",
},
}

var noOpSbinCommands = []NoOpCommand{}

func init() {
for i := range noOpBinCommands {
cmd := noOpBinCommands[i]
mustAddBinCmd(cmd.Name, cmd.ToCommand())
}

for i := range noOpSbinCommands {
cmd := noOpSbinCommands[i]

mustAddSbinCmd(cmd.Name, cmd.ToCommand())
}
}
4 changes: 2 additions & 2 deletions commands/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ func Apt(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Apt

func init() {
addBinCmd("apt", Apt)
addBinCmd("apt-get", Apt)
mustAddBinCmd("apt", Apt)
mustAddBinCmd("apt-get", Apt)
}
62 changes: 57 additions & 5 deletions commands/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path"
"strconv"
"strings"
"unicode"

"github.com/fatih/color"
fcolor "github.com/fatih/color"
Expand All @@ -28,7 +29,7 @@ type commandTable struct {
lookup map[string]vos.ProcessFunc
}

func (ct *commandTable) AddCommand(proc vos.ProcessFunc, names ...string) {
func (ct *commandTable) AddCommand(proc vos.ProcessFunc, names ...string) error {
ct.commands = append(ct.commands, CommandEntry{
Names: names,
Proc: proc,
Expand All @@ -37,8 +38,12 @@ func (ct *commandTable) AddCommand(proc vos.ProcessFunc, names ...string) {
ct.lookup = make(map[string]vos.ProcessFunc)
}
for _, name := range names {
if _, ok := ct.lookup[name]; ok {
return fmt.Errorf("name already registered: %q", name)
}
ct.lookup[name] = proc
}
return nil
}

var allCommands = commandTable{}
Expand All @@ -56,13 +61,27 @@ func ListBuiltinCommands() []CommandEntry {
var _ vos.ProcessResolver = BuiltinProcessResolver

// addBinCmd adds a command under /bin and /usr/bin.
func addBinCmd(name string, cmd vos.ProcessFunc) {
allCommands.AddCommand(cmd, path.Join("/bin", name), path.Join("/usr/bin", name))
func addBinCmd(name string, cmd vos.ProcessFunc) error {
return allCommands.AddCommand(cmd, path.Join("/bin", name), path.Join("/usr/bin", name))
}

// addSbinCmd adds a command under /sbin and /usr/sbin.
func addSbinCmd(name string, cmd vos.ProcessFunc) {
allCommands.AddCommand(cmd, path.Join("/sbin", name), path.Join("/usr/sbin", name))
func addSbinCmd(name string, cmd vos.ProcessFunc) error {
return allCommands.AddCommand(cmd, path.Join("/sbin", name), path.Join("/usr/sbin", name))
}

// mustAddBinCmd adds a command under /bin and /usr/bin and panics if it already exists.
func mustAddBinCmd(name string, cmd vos.ProcessFunc) {
if err := addBinCmd(name, cmd); err != nil {
panic(err)
}
}

// mustAddSbinCmd adds a command under /sbin and /usr/sbin and panics if it already exists.
func mustAddSbinCmd(name string, cmd vos.ProcessFunc) {
if err := addSbinCmd(name, cmd); err != nil {
panic(err)
}
}

func BytesToHuman(bytes int64) string {
Expand Down Expand Up @@ -318,3 +337,36 @@ func (c *ColorPrinter) Sprintf(color *color.Color, format string, a ...interface
}
return fmt.Sprintf(format, a...)
}

// mustDedent takes a multline string and dedents it based on the
// indentation of the second line. The first line MUST be blank.
func mustDedent(s string) string {
s = strings.TrimRightFunc(s, unicode.IsSpace)
lines := strings.Split(s, "\n")
if lines[0] != "" {
panic("First line must be blank.")
}

if len(lines) == 1 {
return ""
}

firstLine := lines[1]
remainderLen := len(strings.TrimLeftFunc(firstLine, unicode.IsSpace))
prefix := firstLine[:len(firstLine)-remainderLen]

out := ""
for i, line := range lines[1:] {
if i != 0 {
out += "\n"
}
trimmed := strings.TrimPrefix(line, prefix)
if trimmed == line {
panic(fmt.Sprintf("Line %q doesn't have prefix %q", line, prefix))
}

out += trimmed
}

return out
}
14 changes: 14 additions & 0 deletions commands/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,17 @@ func AssertScript(t *testing.T, commands ...string) {

g.AssertJson(t, t.Name(), steps)
}

func TestMustDedent(t *testing.T) {
got := mustDedent(`
abc
def
ghi
jkl
`)
want := "abc\n\tdef\n\t\tghi\njkl"

if got != want {
t.Fatalf("Wanted %q, got %q", want, got)
}
}
2 changes: 1 addition & 1 deletion commands/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ func Cat(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Cat

func init() {
addBinCmd("cat", Cat)
mustAddBinCmd("cat", Cat)
}
2 changes: 1 addition & 1 deletion commands/chmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,5 @@ func Chmod(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Chmod

func init() {
addBinCmd("chmod", Chmod)
mustAddBinCmd("chmod", Chmod)
}
2 changes: 1 addition & 1 deletion commands/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ func Clear(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Clear

func init() {
addBinCmd("clear", Clear)
mustAddBinCmd("clear", Clear)
}
2 changes: 1 addition & 1 deletion commands/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,5 @@ func Curl(virtOS vos.VOS) int {
}

func init() {
addBinCmd("curl", Curl)
mustAddBinCmd("curl", Curl)
}
2 changes: 1 addition & 1 deletion commands/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ func Echo(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Echo

func init() {
addBinCmd("echo", Echo)
mustAddBinCmd("echo", Echo)
}
2 changes: 1 addition & 1 deletion commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ func Env(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Env

func init() {
addBinCmd("env", Env)
mustAddBinCmd("env", Env)
}
2 changes: 1 addition & 1 deletion commands/free.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ Swap: 24587768 4301240 20286528`)
var _ vos.ProcessFunc = Free

func init() {
addBinCmd("free", Free)
mustAddBinCmd("free", Free)
}
4 changes: 2 additions & 2 deletions commands/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ func Grep(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Grep

func init() {
addBinCmd("grep", Grep)
addBinCmd("egrep", Grep)
mustAddBinCmd("grep", Grep)
mustAddBinCmd("egrep", Grep)
}
2 changes: 1 addition & 1 deletion commands/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func Hostname(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Hostname

func init() {
addBinCmd("hostname", Hostname)
mustAddBinCmd("hostname", Hostname)
}
2 changes: 1 addition & 1 deletion commands/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ func Id(virtOS vos.VOS) int {
var _ vos.ProcessFunc = Id

func init() {
addBinCmd("id", Id)
mustAddBinCmd("id", Id)
}
Loading

0 comments on commit f3f82c7

Please sign in to comment.