forked from StanfordSNR/guardian-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshfwd.go
180 lines (155 loc) · 4.59 KB
/
sshfwd.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package guardianagent
import (
"bufio"
"io"
"log"
"math/rand"
"os"
"os/exec"
"path"
"syscall"
"golang.org/x/crypto/ssh"
"net"
"fmt"
"io/ioutil"
"strconv"
)
const debugSSHFwd = true
type SSHFwd struct {
SSHProgram string
SSHArgs []string
Host string
RemoteReadableName string
RemoteStubName string
localSocket string
remoteSocket string
listener net.Listener
}
func (fwd *SSHFwd) SetupForwarding() error {
fwd.SSHArgs = append(fwd.SSHArgs, "-S", path.Join(UserTempDir(), strconv.Itoa(int(rand.Int31()))), fwd.Host)
remoteStub := exec.Command(fwd.SSHProgram, append(fwd.SSHArgs, "-M", fwd.RemoteStubName)...)
remoteStdErr, err := remoteStub.StderrPipe()
if err != nil {
return fmt.Errorf("Failed to get ssh stderr: %s", err)
}
remoteStdOut, err := remoteStub.StdoutPipe()
if err != nil {
return fmt.Errorf("Failed to get ssh stdout: %s", err)
}
remoteStdIn, err := remoteStub.StdinPipe()
if err != nil {
return fmt.Errorf("Failed to get ssh stdin: %s", err)
}
err = remoteStub.Start()
if err != nil {
var stdErr []byte
if ee, ok := err.(*exec.ExitError); ok {
stdErr = ee.Stderr
}
os.Stderr.Write(stdErr)
fullStdErr, _ := ioutil.ReadAll(remoteStdErr)
return fmt.Errorf("Failed to run %s %s: %s\n%s", remoteStub.Path, remoteStub.Args, err, fullStdErr)
}
stubReader := bufio.NewReader(remoteStdOut)
remoteSocket, _, err := stubReader.ReadLine()
if err != nil {
// Some error messsage hacking to get the most informative message to the user.
allErr, _ := ioutil.ReadAll(remoteStdErr)
if err == io.EOF {
err = fmt.Errorf("Failed to run remote stub: %s", allErr)
} else {
err = fmt.Errorf("failed to run remote stub: %s\n%s\nMake sure that guardian agent is properly installed on the remote host", err, allErr)
}
stubErr := remoteStub.Wait()
if exiterr, ok := stubErr.(*exec.ExitError); ok {
// If ssh failed with 255, then there is a problem with the connections.
// Otherwise there is usually a problem with the installation of the tool on the remote side.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok && status.ExitStatus() == 255 {
return err
}
}
return fmt.Errorf("%s\nMake sure that guardian agent is properly installed on the remote host", err)
}
listener, bindAddr, err := CreateSocket("")
if err != nil {
return fmt.Errorf("Failed to listen on socket %s: %s", bindAddr, err)
}
log.Printf("Listening on: %s", bindAddr)
fwd.localSocket = bindAddr
fwd.remoteSocket = string(remoteSocket)
fwd.listener = listener
go func() {
err = remoteStub.Wait()
fwd.listener.Close()
}()
child := exec.Command(fwd.SSHProgram,
append(fwd.SSHArgs, "-o ExitOnForwardFailure yes", "-T", "-O", "forward",
fmt.Sprintf("-R %s:%s", string(remoteSocket), bindAddr))...)
_, err = child.Output()
if err != nil {
var stdErr []byte
if ee, ok := err.(*exec.ExitError); ok {
stdErr = ee.Stderr
}
return fmt.Errorf("Failed to run SSH forwarding: %s\n%s", err, stdErr)
}
_, err = fmt.Fprintln(remoteStdIn, "start")
if err != nil {
return fmt.Errorf("Failed to ack forwarding: %s", err)
}
_, _, err = stubReader.ReadLine()
if err != nil {
allErr, _ := ioutil.ReadAll(remoteStdErr)
return fmt.Errorf("Failed to establish ssh forwarding with stub: %s\n%s", err, allErr)
}
return nil
}
func (fwd *SSHFwd) RunRemote(cmd string) error {
if cmd == "" {
fwd.SSHArgs = append(fwd.SSHArgs, "-t")
} else {
fwd.SSHArgs = append(fwd.SSHArgs, cmd)
}
child := exec.Command(fwd.SSHProgram, fwd.SSHArgs...)
child.Stderr = os.Stderr
child.Stdout = os.Stdout
child.Stdin = os.Stdin
return child.Run()
}
func (fwd *SSHFwd) RunLocal(cmd string) error {
child := exec.Command(os.Getenv("SHELL"), "-c", cmd)
child.Stderr = os.Stderr
child.Stdout = os.Stdout
child.Stdin = os.Stdin
return child.Run()
}
func (fwd *SSHFwd) Accept() (net.Conn, error) {
client, err := fwd.listener.Accept()
if err != nil {
return nil, err
}
clientPipe, agentPipe := net.Pipe()
go func() {
io.Copy(client, clientPipe)
client.Close()
}()
go func() {
msg := AgentForwardingNoticeMsg{Client: fwd.RemoteReadableName}
if err = WriteControlPacket(clientPipe, MsgAgentForwardingNotice, ssh.Marshal(msg)); err != nil {
log.Printf("Failed to send message to agent: %s", err)
return
}
io.Copy(clientPipe, client)
if debugSSHFwd {
log.Printf("Finished copying from client to real agent.")
}
clientPipe.Close()
}()
return agentPipe, nil
}
func (fwd *SSHFwd) Close() {
child := exec.Command(fwd.SSHProgram, append(fwd.SSHArgs, "-O exit")...)
child.Run()
os.Remove(fwd.localSocket)
fwd.listener.Close()
}