■ ■ ■ ■ ■ ■
internal/client/handlers/shell.go
| skipped 7 lines |
8 | 8 | | "io" |
9 | 9 | | "os" |
10 | 10 | | "os/exec" |
| 11 | + | "path" |
| 12 | + | "path/filepath" |
11 | 13 | | "strings" |
12 | 14 | | "sync" |
13 | 15 | | |
| skipped 3 lines |
17 | 19 | | "golang.org/x/crypto/ssh" |
18 | 20 | | ) |
19 | 21 | | |
20 | | - | var shells []string |
| 22 | + | var ( |
| 23 | + | shells []string |
21 | 24 | | |
22 | | - | func init() { |
| 25 | + | goodShells = []string{ |
| 26 | + | "zsh", |
| 27 | + | "bash", |
| 28 | + | "fish", |
| 29 | + | "dash", |
| 30 | + | "ash", |
| 31 | + | "csh", |
| 32 | + | "ksh", |
| 33 | + | "tcsh", |
| 34 | + | "sh", |
| 35 | + | } |
| 36 | + | ) |
23 | 37 | | |
24 | | - | var potentialShells []string |
| 38 | + | func getSystemShells() ([]string, error) { |
25 | 39 | | file, err := os.Open("/etc/shells") |
26 | | - | if err == nil { |
27 | | - | defer file.Close() |
28 | | - | scanner := bufio.NewScanner(file) |
29 | | - | for scanner.Scan() { |
30 | | - | line := scanner.Text() |
| 40 | + | if err != nil { |
| 41 | + | return nil, err |
| 42 | + | } |
| 43 | + | defer file.Close() |
31 | 44 | | |
32 | | - | if len(line) > 0 && line[0] == '#' || strings.TrimSpace(line) == "" { |
33 | | - | continue |
34 | | - | } |
35 | | - | potentialShells = append(potentialShells, strings.TrimSpace(line)) |
| 45 | + | scanner := bufio.NewScanner(file) |
| 46 | + | |
| 47 | + | allSystemShells := []string{} |
| 48 | + | potentialShells := []string{} |
| 49 | + | |
| 50 | + | search := make(map[string]bool) |
| 51 | + | |
| 52 | + | for _, goodShell := range goodShells { |
| 53 | + | search[goodShell] = true |
| 54 | + | } |
| 55 | + | |
| 56 | + | for scanner.Scan() { |
| 57 | + | line := scanner.Text() |
| 58 | + | |
| 59 | + | if len(line) > 0 && line[0] == '#' || strings.TrimSpace(line) == "" { |
| 60 | + | continue |
| 61 | + | } |
| 62 | + | |
| 63 | + | allSystemShells = append(allSystemShells, line) |
| 64 | + | |
| 65 | + | shellPath := strings.TrimSpace(line) |
| 66 | + | |
| 67 | + | //if shell name is not in our list of known good shells skip it |
| 68 | + | if !search[filepath.Base(shellPath)] { |
| 69 | + | continue |
36 | 70 | | } |
37 | | - | } else { |
38 | | - | //If the host did not have a /etc/shells, guess a few common shells |
39 | | - | potentialShells = []string{ |
40 | | - | "/bin/bash", |
41 | | - | "/bin/zsh", |
42 | | - | "/bin/ash", |
43 | | - | "/bin/sh", |
| 71 | + | |
| 72 | + | potentialShells = append(potentialShells, shellPath) |
| 73 | + | } |
| 74 | + | |
| 75 | + | if len(potentialShells) == 0 { |
| 76 | + | potentialShells = allSystemShells |
| 77 | + | } |
| 78 | + | |
| 79 | + | return potentialShells, nil |
| 80 | + | } |
| 81 | + | |
| 82 | + | func init() { |
| 83 | + | |
| 84 | + | var ( |
| 85 | + | err error |
| 86 | + | potentialShells []string |
| 87 | + | ) |
| 88 | + | for _, goodShell := range goodShells { |
| 89 | + | good, err := exec.LookPath(goodShell) |
| 90 | + | if err != nil { |
| 91 | + | continue |
44 | 92 | | } |
45 | 93 | | |
| 94 | + | potentialShells = append(potentialShells, good) |
46 | 95 | | } |
| 96 | + | |
| 97 | + | if len(potentialShells) == 0 { |
| 98 | + | |
| 99 | + | potentialShells, err = getSystemShells() |
| 100 | + | if err != nil { |
| 101 | + | |
| 102 | + | //Last ditch effort to find a shell |
| 103 | + | for _, goodShell := range goodShells { |
| 104 | + | potentialShells = append(potentialShells, path.Join("/opt/bin/", goodShell)) |
| 105 | + | potentialShells = append(potentialShells, path.Join("/opt/", goodShell)) |
| 106 | + | potentialShells = append(potentialShells, path.Join("/user/local/bin", goodShell)) |
| 107 | + | potentialShells = append(potentialShells, path.Join("/user/local/sbin", goodShell)) |
| 108 | + | potentialShells = append(potentialShells, path.Join("/usr/bin/", goodShell)) |
| 109 | + | potentialShells = append(potentialShells, path.Join("/bin/", goodShell)) |
| 110 | + | potentialShells = append(potentialShells, path.Join("/sbin/", goodShell)) |
| 111 | + | |
| 112 | + | } |
| 113 | + | } |
| 114 | + | } |
| 115 | + | |
47 | 116 | | for _, s := range potentialShells { |
48 | 117 | | |
49 | 118 | | if stats, err := os.Stat(s); err != nil && (os.IsNotExist(err) || !stats.IsDir()) { |
| skipped 100 lines |