/
sudo.go
141 lines (105 loc) · 2.91 KB
/
sudo.go
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
package gomez
import (
"bufio"
"fmt"
"io"
"log"
"github.com/gcmurphy/getpass"
)
type sudoRunner struct {
}
type sudoMatcher struct {
currentIndexMatch int
currentPrompt string
stringToFind string
totalMatchLength int
}
func WrapSudoCommand(cmd string) string {
return fmt.Sprintf("/usr/bin/sudo bash <<CMD\nexport PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin\n%s\nCMD", cmd)
}
func (sudo *sudoRunner) handlePrompt(runner *runner, done <-chan bool) {
writer, err := runner.session.StdinPipe()
if err != nil {
panic("Failed to run: " + err.Error())
}
reader, err := runner.session.StdoutPipe()
if err != nil {
panic("Failed to run: " + err.Error())
}
go sudo.sendPassword(runner, writer, reader, done)
}
func (sudo *sudoRunner) promptForPassword(runner *runner, writer io.Writer) (string, error) {
if runner.host.Password == "" {
password, err := getpass.GetPassWithOptions(fmt.Sprintf("enter sudo password for %s: ", runner.host.Host), 0, 100)
if err != nil {
return password, err
}
runner.host.Password = password
}
return runner.host.Password, nil
}
func (sudo *sudoRunner) sendPassword(runner *runner, writer io.Writer, reader io.Reader, done <-chan bool) {
matcher := newSudoMatcher(runner.host.User)
for {
select {
case <-done:
default:
bytesRead := make([]byte, matcher.totalMatchLength)
_, err := reader.Read(bytesRead)
if err == io.EOF {
continue
}
if err != nil {
log.Fatalln(err)
return
}
matchFound := matcher.Match(bytesRead)
if matchFound {
password, err := sudo.promptForPassword(runner, writer)
if err != nil {
log.Fatalln(err)
}
writer.Write([]byte(password + "\n"))
break
}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
if scanner.Text() == "Sorry, try again." {
runner.host.Password = ""
password, err := sudo.promptForPassword(runner, writer)
if err != nil {
log.Fatalln(err)
}
writer.Write([]byte(password + "\n"))
continue
}
if matcher.Match(scanner.Bytes()) {
continue
}
OutputRemote(runner.host, scanner.Text())
}
break
}
}
}
func newSudoMatcher(user string) sudoMatcher {
stringToFind := fmt.Sprintf("[sudo] password for %s:", user)
totalMatchLength := len([]byte(stringToFind))
return sudoMatcher{0, "", stringToFind, totalMatchLength}
}
func (matcher *sudoMatcher) Match(additionalBytes []byte) bool {
readString := string(additionalBytes)
for _, runeVal := range readString {
if len(matcher.stringToFind) > matcher.currentIndexMatch && runeVal == rune(matcher.stringToFind[matcher.currentIndexMatch]) {
matcher.currentPrompt = matcher.currentPrompt + string(runeVal)
matcher.currentIndexMatch++
} else {
matcher.currentPrompt = ""
matcher.currentIndexMatch = 0
}
if len(matcher.currentPrompt) == matcher.totalMatchLength {
return true
}
}
return false
}