-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclient.go
216 lines (196 loc) · 5.19 KB
/
client.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package tacplus
import (
"context"
"encoding/binary"
"errors"
"net"
"sync"
)
// ClientSession is a TACACS+ client session.
type ClientSession struct {
*session
p []byte
}
// Close closes the client session.
func (c *ClientSession) Close() {
c.p = nil
c.close()
}
// Abort sends a message back to the server aborting the session with the supplied reason.
func (c *ClientSession) Abort(ctx context.Context, reason string) error {
if len(reason) > maxUint16 {
reason = reason[:maxUint16]
}
err := c.sendRequest(ctx, &AuthenContinue{Abort: true, Message: reason}, nil)
c.Close()
return err
}
// Continue is used to send msg in response to a previous AuthenReply.
// A new AuthenReply or error is returned.
func (c *ClientSession) Continue(ctx context.Context, msg string) (*AuthenReply, error) {
// sequence number too large to continue
if c.seq >= 0xfe {
_ = c.Abort(ctx, "")
return nil, errors.New("session aborted, too many packets")
}
rep := new(AuthenReply)
if err := c.sendRequest(ctx, &AuthenContinue{Message: msg}, rep); err != nil {
c.Close()
return nil, err
}
if rep.last() {
c.Close()
}
return rep, nil
}
func (c *ClientSession) sendRequest(ctx context.Context, req, rep packet) error {
if c.p == nil {
return errSessionClosed
}
p, err := req.marshal(c.p[:hdrLen])
if err != nil {
return err
}
err = c.writePacket(ctx, p)
if err != nil || rep == nil {
return err
}
c.p, err = c.readPacket(ctx)
if err == nil {
err = rep.unmarshal(c.p[hdrLen:])
}
return err
}
// Client is a TACACS+ client that connects to a single TACACS+ server.
//
// If the Client's ConnConfig enables session multiplexing, the client will
// cache a single open connection for this purpose.
type Client struct {
Addr string // TCP address of tacacs server.
ConnConfig ConnConfig // TACACS+ connection configuration.
// Optional DialContext function used to create the network connection.
DialContext func(ctx context.Context, net, addr string) (net.Conn, error)
mu sync.Mutex // protects access to conn
conn *conn // current cached mux connection
}
// Close closes the cached connection.
func (c *Client) Close() {
c.mu.Lock()
conn := c.conn
c.mu.Unlock()
if conn != nil {
conn.close()
}
}
var zeroDialer net.Dialer
func (c *Client) dial(ctx context.Context) (net.Conn, error) {
if c.DialContext != nil {
return c.DialContext(ctx, "tcp", c.Addr)
}
return zeroDialer.DialContext(ctx, "tcp", c.Addr)
}
func (c *Client) newSession(ctx context.Context) (*session, error) {
mux := c.ConnConfig.Mux || c.ConnConfig.LegacyMux
if mux {
// try to use existing cached connection
c.mu.Lock()
conn := c.conn
c.mu.Unlock()
if conn != nil {
if s, _ := conn.newClientSession(ctx); s != nil {
return s, nil
}
}
}
// create new connection
nc, err := c.dial(ctx)
if err != nil {
return nil, err
}
conn := newConn(nc, nil, c.ConnConfig)
go conn.serve()
s, err := conn.newClientSession(ctx)
if err != nil {
conn.close()
return nil, err
}
if mux {
// attempt to cache multiplexed connection
c.mu.Lock()
defer c.mu.Unlock()
if c.conn == nil {
// cache this connection
c.conn = conn
go func() {
// clear cached reference when conn closes
<-conn.done
c.mu.Lock()
c.conn = nil
c.mu.Unlock()
}()
} else {
// already cached one connection, so create goroutine
// that closes connection when session is closed so
// we don't leak idle connections.
go func() {
<-s.done
conn.close()
}()
}
}
return s, nil
}
func (c *Client) startSession(ctx context.Context, ver, t uint8, req, rep packet) (*ClientSession, error) {
s, err := c.newSession(ctx)
if err != nil {
return nil, err
}
p := make([]byte, 1024)
p[hdrVer] = ver
p[hdrType] = t
if s.c.Mux && !s.c.LegacyMux {
p[hdrFlags] = hdrFlagSingleConnect
}
binary.BigEndian.PutUint32(p[hdrID:], s.id)
cs := &ClientSession{s, p}
if err = cs.sendRequest(ctx, req, rep); err != nil {
cs.close()
return nil, err
}
return cs, nil
}
// SendAcctRequest sends an AcctRequest to the server returning an AcctReply or error.
func (c *Client) SendAcctRequest(ctx context.Context, req *AcctRequest) (*AcctReply, error) {
rep := new(AcctReply)
s, err := c.startSession(ctx, verDefault, sessTypeAcct, req, rep)
if err != nil {
return nil, err
}
s.close()
return rep, nil
}
// SendAuthorRequest sends an AuthorRequest to the server returning an AuthorResponse or error.
func (c *Client) SendAuthorRequest(ctx context.Context, req *AuthorRequest) (*AuthorResponse, error) {
resp := new(AuthorResponse)
s, err := c.startSession(ctx, verDefault, sessTypeAuthor, req, resp)
if err != nil {
return nil, err
}
s.close()
return resp, nil
}
// SendAuthenStart sends an AuthenStart to the server returning an AuthenReply and
// optional ClientSession or an error. If ClientSession is set it should be
// used to complete the current interactive authentication session.
func (c *Client) SendAuthenStart(ctx context.Context, as *AuthenStart) (*AuthenReply, *ClientSession, error) {
rep := new(AuthenReply)
s, err := c.startSession(ctx, as.version(), sessTypeAuthen, as, rep)
if err != nil {
return nil, nil, err
}
if rep.last() {
s.close()
return rep, nil, nil
}
return rep, s, nil
}