From 9d19a83630e51cd9e141c140fb0d8384818849de Mon Sep 17 00:00:00 2001 From: JM Faircloth Date: Wed, 7 Dec 2022 10:02:47 -0600 Subject: [PATCH] move non windows additional notes to new file --- client.go | 54 ---------------------------------------- notes_unix.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ notes_windows.go | 40 ++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 notes_unix.go create mode 100644 notes_windows.go diff --git a/client.go b/client.go index 33bc85a5..d0baf7e8 100644 --- a/client.go +++ b/client.go @@ -6,9 +6,6 @@ import ( "crypto/subtle" "crypto/tls" "crypto/x509" - "debug/elf" - "debug/macho" - "debug/pe" "encoding/base64" "errors" "fmt" @@ -18,14 +15,11 @@ import ( "net" "os" "os/exec" - "os/user" "path/filepath" - "runtime" "strconv" "strings" "sync" "sync/atomic" - "syscall" "time" "github.com/hashicorp/go-hclog" @@ -497,54 +491,6 @@ var peTypes = map[uint16]string{ 0xaa64: "arm64", } -// additionalNotesAboutCommand tries to get additional information about a command that might help diagnose -// why it won't run correctly. It runs as a best effort only. -func additionalNotesAboutCommand(path string) string { - notes := "" - stat, err := os.Stat(path) - if err != nil { - return notes - } - - notes += "\nAdditional notes about plugin:\n" - notes += fmt.Sprintf(" Path: %s\n", path) - notes += fmt.Sprintf(" Mode: %s\n", stat.Mode()) - statT, ok := stat.Sys().(*syscall.Stat_t) - if ok { - currentUsername := "?" - if u, err := user.LookupId(strconv.FormatUint(uint64(os.Getuid()), 10)); err == nil { - currentUsername = u.Username - } - currentGroup := "?" - if g, err := user.LookupGroupId(strconv.FormatUint(uint64(os.Getgid()), 10)); err == nil { - currentGroup = g.Name - } - username := "?" - if u, err := user.LookupId(strconv.FormatUint(uint64(statT.Uid), 10)); err == nil { - username = u.Username - } - group := "?" - if g, err := user.LookupGroupId(strconv.FormatUint(uint64(statT.Gid), 10)); err == nil { - group = g.Name - } - notes += fmt.Sprintf(" Owner: %d [%s] (current: %d [%s])\n", statT.Uid, username, os.Getuid(), currentUsername) - notes += fmt.Sprintf(" Group: %d [%s] (current: %d [%s])\n", statT.Gid, group, os.Getgid(), currentGroup) - } - - if elfFile, err := elf.Open(path); err == nil { - notes += fmt.Sprintf(" ELF architecture: %s (current architecture: %s)\n", elfFile.Machine, runtime.GOARCH) - } else if machoFile, err := macho.Open(path); err == nil { - notes += fmt.Sprintf(" MachO architecture: %s (current architecture: %s)\n", machoFile.Cpu, runtime.GOARCH) - } else if peFile, err := pe.Open(path); err == nil { - machine, ok := peTypes[peFile.Machine] - if !ok { - machine = "unknown" - } - notes += fmt.Sprintf(" PE architecture: %s (current architecture: %s)\n", machine, runtime.GOARCH) - } - return notes -} - // Start the underlying subprocess, communicating with it to negotiate // a port for RPC connections, and returning the address to connect via RPC. // diff --git a/notes_unix.go b/notes_unix.go new file mode 100644 index 00000000..dae1c411 --- /dev/null +++ b/notes_unix.go @@ -0,0 +1,64 @@ +//go:build !windows +// +build !windows + +package plugin + +import ( + "debug/elf" + "debug/macho" + "debug/pe" + "fmt" + "os" + "os/user" + "runtime" + "strconv" + "syscall" +) + +// additionalNotesAboutCommand tries to get additional information about a command that might help diagnose +// why it won't run correctly. It runs as a best effort only. +func additionalNotesAboutCommand(path string) string { + notes := "" + stat, err := os.Stat(path) + if err != nil { + return notes + } + + notes += "\nAdditional notes about plugin:\n" + notes += fmt.Sprintf(" Path: %s\n", path) + notes += fmt.Sprintf(" Mode: %s\n", stat.Mode()) + statT, ok := stat.Sys().(*syscall.Stat_t) + if ok { + currentUsername := "?" + if u, err := user.LookupId(strconv.FormatUint(uint64(os.Getuid()), 10)); err == nil { + currentUsername = u.Username + } + currentGroup := "?" + if g, err := user.LookupGroupId(strconv.FormatUint(uint64(os.Getgid()), 10)); err == nil { + currentGroup = g.Name + } + username := "?" + if u, err := user.LookupId(strconv.FormatUint(uint64(statT.Uid), 10)); err == nil { + username = u.Username + } + group := "?" + if g, err := user.LookupGroupId(strconv.FormatUint(uint64(statT.Gid), 10)); err == nil { + group = g.Name + } + notes += fmt.Sprintf(" Owner: %d [%s] (current: %d [%s])\n", statT.Uid, username, os.Getuid(), currentUsername) + notes += fmt.Sprintf(" Group: %d [%s] (current: %d [%s])\n", statT.Gid, group, os.Getgid(), currentGroup) + } + + if elfFile, err := elf.Open(path); err == nil { + notes += fmt.Sprintf(" ELF architecture: %s (current architecture: %s)\n", elfFile.Machine, runtime.GOARCH) + } else if machoFile, err := macho.Open(path); err == nil { + notes += fmt.Sprintf(" MachO architecture: %s (current architecture: %s)\n", machoFile.Cpu, runtime.GOARCH) + } else if peFile, err := pe.Open(path); err == nil { + machine, ok := peTypes[peFile.Machine] + if !ok { + machine = "unknown" + } + notes += fmt.Sprintf(" PE architecture: %s (current architecture: %s)\n", machine, runtime.GOARCH) + } + return notes +} diff --git a/notes_windows.go b/notes_windows.go new file mode 100644 index 00000000..900b9331 --- /dev/null +++ b/notes_windows.go @@ -0,0 +1,40 @@ +//go:build windows +// +build windows + +package plugin + +import ( + "debug/elf" + "debug/macho" + "debug/pe" + "fmt" + "os" + "runtime" +) + +// additionalNotesAboutCommand tries to get additional information about a command that might help diagnose +// why it won't run correctly. It runs as a best effort only. +func additionalNotesAboutCommand(path string) string { + notes := "" + stat, err := os.Stat(path) + if err != nil { + return notes + } + + notes += "\nAdditional notes about plugin:\n" + notes += fmt.Sprintf(" Path: %s\n", path) + notes += fmt.Sprintf(" Mode: %s\n", stat.Mode()) + + if elfFile, err := elf.Open(path); err == nil { + notes += fmt.Sprintf(" ELF architecture: %s (current architecture: %s)\n", elfFile.Machine, runtime.GOARCH) + } else if machoFile, err := macho.Open(path); err == nil { + notes += fmt.Sprintf(" MachO architecture: %s (current architecture: %s)\n", machoFile.Cpu, runtime.GOARCH) + } else if peFile, err := pe.Open(path); err == nil { + machine, ok := peTypes[peFile.Machine] + if !ok { + machine = "unknown" + } + notes += fmt.Sprintf(" PE architecture: %s (current architecture: %s)\n", machine, runtime.GOARCH) + } + return notes +}