-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathagent.go
179 lines (156 loc) · 4.65 KB
/
agent.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
package guardianagent
import (
"fmt"
"io"
"log"
"net"
"os"
"os/user"
"path"
"github.com/hashicorp/yamux"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
"golang.org/x/crypto/ssh/terminal"
)
type InputType uint8
const (
Terminal = iota
Display
)
type Agent struct {
policy Policy
store *Store
}
func NewGuardian(policyConfigPath string, inType InputType) (*Agent, error) {
var ui UI
switch inType {
case Terminal:
if !terminal.IsTerminal(int(os.Stdin.Fd())) {
return nil, fmt.Errorf("standard input is not a terminal")
}
ui = &FancyTerminalUI{}
break
case Display:
ui = NewAskPassUI()
}
// get policy store
store, err := NewStore(policyConfigPath)
if err != nil {
return nil, fmt.Errorf("Failed to load policy store: %s", err)
}
return &Agent{
store: store,
policy: Policy{Store: store, UI: ui}},
nil
}
func (agent *Agent) proxySSH(scope Scope, toClient net.Conn, toServer net.Conn, control net.Conn, fil *ssh.Filter) error {
curuser, err := user.Current()
if err != nil {
return fmt.Errorf("Failed to get current user: %s", err)
}
clientConfig := &ssh.ClientConfig{
User: scope.ServiceUsername,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return HostKeyCallback(hostname, remote, key, agent.policy.UI)
},
Auth: getAuth(scope.ServiceUsername, scope.ServiceHostname, curuser.HomeDir, agent.policy.UI),
HostKeyAlgorithms: knownhosts.OrderHostKeyAlgs(scope.ServiceHostname, toServer.RemoteAddr(), path.Join(curuser.HomeDir, ".ssh", "known_hosts")),
}
meteredConnToServer := CustomConn{Conn: toServer}
proxy, err := ssh.NewProxyConn(scope.ServiceHostname, toClient, &meteredConnToServer, clientConfig, fil)
if err != nil {
return err
}
done := proxy.Run()
err = <-done
var msgNum byte
var msg interface{}
if err != nil {
msg = HandoffFailedMessage{Msg: err.Error()}
msgNum = MsgHandoffFailed
} else {
msg = HandoffCompleteMessage{
NextTransportByte: uint32(meteredConnToServer.BytesRead() - proxy.BufferedFromServer())}
msgNum = MsgHandoffComplete
}
packet := ssh.Marshal(msg)
return WriteControlPacket(control, msgNum, packet)
}
func (agent *Agent) HandleConnection(conn net.Conn) error {
log.Printf("New incoming connection")
var scope Scope
for {
msgNum, payload, err := ReadControlPacket(conn)
if err == io.EOF || err == io.ErrClosedPipe {
return nil
}
if err != nil {
return fmt.Errorf("Failed to read control packet: %s", err)
}
switch msgNum {
case MsgAgentForwardingNotice:
notice := new(AgentForwardingNoticeMsg)
if err := ssh.Unmarshal(payload, notice); err != nil {
return fmt.Errorf("Failed to unmarshal AgentForwardingNoticeMsg: %s", err)
}
scope.Client = notice.Client
case MsgExecutionRequest:
execReq := new(ExecutionRequestMessage)
if err = ssh.Unmarshal(payload, execReq); err != nil {
return fmt.Errorf("Failed to unmarshal ExecutionRequestMessage: %s", err)
}
scope.ServiceHostname = execReq.Server
scope.ServiceUsername = execReq.User
agent.handleExecutionRequest(conn, scope, execReq.Command)
case MsgAgentCExtension:
queryExtension := new(AgentCExtensionMsg)
ssh.Unmarshal(payload, queryExtension)
if queryExtension.ExtensionType == AgentGuardExtensionType {
WriteControlPacket(conn, MsgAgentSuccess, []byte{})
continue
}
fallthrough
default:
WriteControlPacket(conn, MsgAgentFailure, []byte{})
return fmt.Errorf("Unrecognized incoming message: %d", msgNum)
}
}
}
func (ag *Agent) handleExecutionRequest(conn net.Conn, scope Scope, cmd string) error {
err := ag.policy.RequestApproval(scope, cmd)
if err != nil {
WriteControlPacket(conn, MsgExecutionDenied,
ssh.Marshal(ExecutionDeniedMessage{Reason: err.Error()}))
return nil
}
filter := ssh.NewFilter(cmd, func() error { return ag.policy.RequestApprovalForAllCommands(scope) })
WriteControlPacket(conn, MsgExecutionApproved, []byte{})
ymux, err := yamux.Server(conn, nil)
if err != nil {
return fmt.Errorf("Failed to start ymux: %s", err)
}
defer ymux.Close()
control, err := ymux.Accept()
if err != nil {
return fmt.Errorf("Failed to accept control stream: %s", err)
}
defer control.Close()
sshData, err := ymux.Accept()
if err != nil {
return fmt.Errorf("Failed to accept data stream: %s", err)
}
defer sshData.Close()
transport, err := ymux.Accept()
if err != nil {
return fmt.Errorf("Failed to get transport stream: %s", err)
}
defer transport.Close()
err = ag.proxySSH(scope, sshData, transport, control, filter)
transport.Close()
sshData.Close()
control.Close()
if err != nil {
return fmt.Errorf("Proxy session finished with error: %s", err)
}
return nil
}