forked from blind-oracle/go-radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
311 lines (258 loc) · 7.32 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package radius
import (
"errors"
"fmt"
"net"
"strconv"
"time"
)
// Some commonly used attribute IDs
const (
AttrFramedIPAddress = 8
AttrVendorSpecific = 26
AttrCallingStationID = 31
AttrAcctSessionID = 44
)
// Default CoA ports
const (
coaPortCisco = 1700
coaPortMikrotik = 3799
coaPortJuniper = 3799
)
// Some service stuff
var (
ciscoReauthenticateCommand = "subscriber:command=account-reauthenticate"
ciscoReauthenticateAVPair []byte
juniperServiceActive = []byte("104 Service active")
)
// RequestParams are parameters specific to an outgoing RADIUS request
type RequestParams struct {
Secret []byte
SrcAddress *net.UDPAddr
DstAddressPort *net.UDPAddr
}
// Client that can exchange packets with a RADIUS-enabled host
type Client struct {
// Local address to use for outgoing connections (can be nil)
LocalAddr *net.UDPAddr
// Timeout and retry count
Timeout time.Duration
Retries int
}
// RequestResult is a RADIUS request result
type RequestResult struct {
Success bool
Duration time.Duration
ErrorCause ErrorCause
Error error
ErrorString string
ResultString string
Timestamp time.Time
AVPairs []*AVP
}
func init() {
// Encode the request here for later use
ciscoReauthenticateAVPair = EncodeAVPairCisco(ciscoReauthenticateCommand)
juniperServiceActive = []byte(juniperServiceActive)
}
// Exchange sends the packet to the given server address and waits for a
// response.
func (c *Client) Exchange(packet *Packet, dst *net.UDPAddr, src *net.UDPAddr) (reply *Packet, err error) {
var (
wire []byte
conn *net.UDPConn
n int
buf [maxPacketSize]byte
)
if wire, err = packet.Encode(); err != nil {
return
}
// If we weren't provided a src address - try default from context (which may be nil too)
if src == nil {
src = c.LocalAddr
}
if conn, err = net.DialUDP("udp4", src, dst); err != nil {
return
}
for i := 0; i < c.Retries; i++ {
conn.SetWriteDeadline(time.Now().Add(c.Timeout))
if _, err = conn.Write(wire); err != nil {
break
}
conn.SetReadDeadline(time.Now().Add(c.Timeout))
if n, err = conn.Read(buf[:]); err == nil {
if reply, err = Parse(buf[:n], packet.Secret, packet.Dictionary); err == nil {
if !reply.IsAuthentic(packet) {
err = errors.New("Non-authentic packet")
}
break
}
}
}
conn.Close()
return
}
// Request send a RADIUS request
func (c *Client) Request(params *RequestParams, requestType Code, attrs ...*Attribute) (result *RequestResult) {
var (
reply *Packet
)
result = &RequestResult{Timestamp: time.Now()}
p := New(requestType, params.Secret)
p.AddAttrs(attrs)
if reply, result.Error = c.Exchange(p, params.DstAddressPort, params.SrcAddress); result.Error == nil {
switch reply.Code {
case CodeDisconnectACK, CodeCoAACK:
result.Success = true
case CodeDisconnectNAK, CodeCoANAK:
if reply.Value("Error-Cause") != nil {
result.ErrorCause = ErrorCause(reply.Value("Error-Cause").(uint32))
}
if result.AVPairs, result.Error = DecodeAVPairs(reply); result.Error != nil {
result.Error = fmt.Errorf("Got NAK, but unable to parse reply AVPairs: %s", result.Error.Error())
}
default:
result.Error = fmt.Errorf("Unknown reply code: %d", int(reply.Code))
}
}
if result.Success {
result.ResultString = "ACK"
return
}
if result.Error == nil {
result.ResultString = "NAK"
if result.ErrorCause > 0 {
result.ResultString += " (ErrorCause " + strconv.Itoa(int(result.ErrorCause)) + ")"
}
} else {
if err, ok := result.Error.(net.Error); ok && err.Timeout() {
result.ResultString = "Timeout"
} else {
result.ResultString = "Error"
result.ErrorString = result.Error.Error()
}
}
return
}
// CiscoRequest is a Cisco request wrapper
func (c *Client) CiscoRequest(Params *RequestParams, RequestType Code, Attrs ...*Attribute) (Result *RequestResult) {
if Params.DstAddressPort.Port == 0 {
Params.DstAddressPort.Port = coaPortCisco
}
return c.Request(Params, RequestType, Attrs...)
}
// CiscoDisconnect is a Cisco disconnect wrapper
func (c *Client) CiscoDisconnect(Params *RequestParams, CallingStationID string) *RequestResult {
return c.CiscoRequest(
Params,
CodeDisconnectRequest,
&Attribute{
Type: AttrCallingStationID,
Value: CallingStationID,
},
)
}
// CiscoReauthenticate is a Cisco reauthenticate wrapper
func (c *Client) CiscoReauthenticate(Params *RequestParams, CallingStationID string) *RequestResult {
return c.CiscoRequest(
Params,
CodeCoARequest,
&Attribute{
Type: AttrCallingStationID,
Value: CallingStationID,
},
&Attribute{
Type: AttrVendorSpecific,
Value: ciscoReauthenticateAVPair,
},
)
}
// JuniperRequest is a Juniper request wrapper
func (c *Client) JuniperRequest(Params *RequestParams, RequestType Code, Attrs ...*Attribute) (Result *RequestResult) {
if Params.DstAddressPort.Port == 0 {
Params.DstAddressPort.Port = coaPortJuniper
}
return c.Request(Params, RequestType, Attrs...)
}
// JuniperActivateService activates a Juniper service
func (c *Client) JuniperActivateService(Params *RequestParams, AcctSessionID, Service string, Timeout uint32) *RequestResult {
return c.JuniperRequest(
Params,
CodeCoARequest,
&Attribute{
Type: AttrAcctSessionID,
Value: AcctSessionID,
},
&Attribute{
Type: AttrVendorSpecific,
Value: EncodeAVPairJuniperByteTag(AVPJunSvcAct, 1, []byte(Service)),
},
&Attribute{
Type: AttrVendorSpecific,
Value: EncodeAVPairJuniperByte(AVPJunSvcTimeout, EncodeJuniperTimeoutTag(Timeout, 1)),
},
)
}
// JuniperDeactivateService deactivates a Juniper service
func (c *Client) JuniperDeactivateService(Params *RequestParams, AcctSessionID, Service string) *RequestResult {
return c.JuniperRequest(
Params,
CodeCoARequest,
&Attribute{
Type: AttrAcctSessionID,
Value: AcctSessionID,
},
&Attribute{
Type: AttrVendorSpecific,
Value: EncodeAVPairJuniperByte(AVPJunSvcDeact, []byte(Service)),
},
)
}
// JuniperUpdateService Updates a Juniper service
func (c *Client) JuniperUpdateService(Params *RequestParams, AcctSessionID, Service string, Timeout uint32) *RequestResult {
return c.JuniperRequest(
Params,
CodeCoARequest,
&Attribute{
Type: AttrAcctSessionID,
Value: AcctSessionID,
},
&Attribute{
Type: AttrVendorSpecific,
Value: EncodeAVPairJuniperByteTag(AVPJunSvcUpdate, 1, []byte(Service)),
},
&Attribute{
Type: AttrVendorSpecific,
Value: EncodeAVPairJuniperByte(AVPJunSvcTimeout, EncodeJuniperTimeoutTag(Timeout, 1)),
},
)
}
// JuniperDisconnect disconnects a Juniper session
func (c *Client) JuniperDisconnect(Params *RequestParams, AcctSessionID string) *RequestResult {
return c.JuniperRequest(
Params,
CodeDisconnectRequest,
&Attribute{
Type: AttrAcctSessionID,
Value: AcctSessionID,
},
)
}
// MikrotikRequest is a Mikrotik request wrapper
func (c *Client) MikrotikRequest(Params *RequestParams, RequestType Code, Attrs ...*Attribute) (Result *RequestResult) {
if Params.DstAddressPort.Port == 0 {
Params.DstAddressPort.Port = coaPortMikrotik
}
return c.Request(Params, RequestType, Attrs...)
}
// MikrotikDisconnect is a Mikrotik disconnect wrapper
func (c *Client) MikrotikDisconnect(Params *RequestParams, ClientIP net.IP) *RequestResult {
return c.MikrotikRequest(
Params,
CodeDisconnectRequest,
&Attribute{
Type: AttrFramedIPAddress,
Value: ClientIP,
},
)
}