include the breakglass mountpoint in $PATH

That way, users don’t need to use the ./ prefix all the time.
This commit is contained in:
Michael Stapelberg 2018-06-23 15:45:50 +02:00
parent a8f85f5027
commit b99b39b334
2 changed files with 32 additions and 1 deletions

View File

@ -100,6 +100,10 @@ func main() {
log.Fatal(err)
}
if err := os.Setenv("PATH", unpackDir+":"+os.Getenv("PATH")); err != nil {
log.Fatal(err)
}
accept := func(listener net.Listener) {
for {
conn, err := listener.Accept()

29
ssh.go
View File

@ -7,6 +7,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"unsafe"
@ -47,6 +48,32 @@ func handleChannel(newChannel ssh.NewChannel) {
}(channel, requests)
}
func expandPath(env []string) []string {
pwd, err := os.Getwd()
if err != nil {
return env
}
found := false
for idx, val := range env {
parts := strings.Split(val, "=")
if len(parts) < 2 {
continue // malformed entry
}
key := parts[0]
if key != "PATH" {
continue
}
val := strings.Join(parts[1:], "=")
env[idx] = fmt.Sprintf("%s=%s:%s", key, pwd, val)
found = true
}
if !found {
const busyboxDefaultPATH = "/sbin:/usr/sbin:/bin:/usr/bin"
env = append(env, fmt.Sprintf("PATH=%s:%s", pwd, busyboxDefaultPATH))
}
return env
}
type session struct {
env []string
ptyf *os.File
@ -124,7 +151,7 @@ func (s *session) request(req *ssh.Request) error {
}
cmd := exec.Command(cmdline[0], cmdline[1:]...)
cmd.Env = s.env
cmd.Env = expandPath(s.env)
cmd.SysProcAttr = &syscall.SysProcAttr{}
if s.ttyf == nil {