-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathradiucal.go
242 lines (227 loc) · 5.78 KB
/
radiucal.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
// Implementation of a UDP proxy
package main
import (
"flag"
"fmt"
"github.com/epiphyte/goutils"
"github.com/epiphyte/radiucal/plugins"
"layeh.com/radius"
"net"
"os"
"os/signal"
"path/filepath"
"sync"
)
var vers = "master"
var (
proxy *net.UDPConn
serverAddress *net.UDPAddr
clients map[string]*connection = make(map[string]*connection)
clientLock *sync.Mutex = new(sync.Mutex)
)
type connection struct {
client *net.UDPAddr
server *net.UDPConn
}
func logError(message string, err error) bool {
if err == nil {
return false
}
goutils.WriteError(message, err)
return true
}
func newConnection(srv, cli *net.UDPAddr) *connection {
conn := new(connection)
conn.client = cli
srvudp, err := net.DialUDP("udp", nil, srv)
if logError("dial udp", err) {
return nil
}
conn.server = srvudp
return conn
}
func setup(hostport string, port int) error {
saddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", port))
if err != nil {
return err
}
pudp, err := net.ListenUDP("udp", saddr)
if err != nil {
return err
}
proxy = pudp
srvaddr, err := net.ResolveUDPAddr("udp", hostport)
if err != nil {
return err
}
serverAddress = srvaddr
return nil
}
func runConnection(conn *connection) {
var buffer [radius.MaxPacketLength]byte
for {
n, err := conn.server.Read(buffer[0:])
if logError("unable to read", err) {
continue
}
_, err = proxy.WriteToUDP(buffer[0:n], conn.client)
logError("relaying", err)
}
}
func runProxy(ctx *context) {
if ctx.debug {
goutils.WriteInfo("=============WARNING==================")
goutils.WriteInfo("debugging is enabled!")
goutils.WriteInfo("dumps from debugging may contain secrets")
goutils.WriteInfo("do NOT share debugging dumps")
goutils.WriteInfo("=============WARNING==================")
goutils.WriteDebug("secret", string(ctx.secret))
}
var buffer [radius.MaxPacketLength]byte
for {
n, cliaddr, err := proxy.ReadFromUDP(buffer[0:])
if logError("read from udp", err) {
continue
}
saddr := cliaddr.String()
clientLock.Lock()
conn, found := clients[saddr]
if !found {
conn = newConnection(serverAddress, cliaddr)
if conn == nil {
clientLock.Unlock()
continue
}
clients[saddr] = conn
clientLock.Unlock()
go runConnection(conn)
} else {
clientLock.Unlock()
}
buffered := []byte(buffer[0:n])
if !ctx.authorize(buffered) {
if !ctx.noreject {
p, err := radius.Parse(buffered, []byte(ctx.secret))
if err == nil {
p = p.Response(radius.CodeAccessReject)
rej, err := p.Encode()
if err == nil {
proxy.WriteToUDP(rej, conn.client)
} else {
if ctx.debug {
goutils.WriteError("unable to encode rejection", err)
}
}
} else {
if ctx.debug {
goutils.WriteError("unable to parse packets", err)
}
}
}
continue
}
_, err = conn.server.Write(buffer[0:n])
logError("server write", err)
}
}
func account(ctx *context) {
var buffer [radius.MaxPacketLength]byte
for {
n, _, err := proxy.ReadFromUDP(buffer[0:])
if logError("accounting udp error", err) {
continue
}
ctx.account(buffer[0:n])
}
}
func main() {
goutils.WriteInfo(fmt.Sprintf("radiucal (%s)", vers))
var config = flag.String("config", "/etc/radiucal/radiucal.conf", "Configuration file")
var instance = flag.String("instance", "", "Instance name")
var debugging = flag.Bool("debug", false, "debugging")
flag.Parse()
conf, err := goutils.LoadConfig(*config, goutils.NewConfigSettings())
if err != nil {
goutils.WriteError("unable to load config", err)
panic("invalid/unable to load config")
}
debug := conf.GetTrue("debug") || *debugging
logOpts := goutils.NewLogOptions()
logOpts.Debug = debug
logOpts.Info = true
logOpts.Instance = *instance
goutils.ConfigureLogging(logOpts)
host := conf.GetStringOrDefault("host", "localhost")
var to int = 1814
accounting := conf.GetTrue("accounting")
defaultBind := 1812
if accounting {
defaultBind = 1813
} else {
to, err = conf.GetIntOrDefault("to", 1814)
if err != nil {
goutils.WriteError("unable to get bind-to", err)
panic("cannot bind to another socket")
}
}
bind, err := conf.GetIntOrDefault("bind", defaultBind)
if err != nil {
goutils.WriteError("unable to bind address", err)
panic("unable to bind")
}
addr := fmt.Sprintf("%s:%d", host, to)
err = setup(addr, bind)
if logError("proxy setup", err) {
panic("unable to proceed")
}
lib := conf.GetStringOrDefault("dir", "/var/lib/radiucal/")
secrets := filepath.Join(lib, "secrets")
secret := parseSecrets(secrets)
ctx := &context{debug: debug, secret: []byte(secret), noreject: conf.GetTrue("noreject")}
mods := conf.GetArrayOrEmpty("plugins")
pCtx := &plugins.PluginContext{}
pCtx.Logs = filepath.Join(lib, "log")
pCtx.Lib = lib
pCtx.Config = conf
pCtx.Instance = *instance
pPath := filepath.Join(lib, "plugins")
for _, p := range mods {
oPath := filepath.Join(pPath, fmt.Sprintf("%s.rd", p))
goutils.WriteInfo("loading plugin", p, oPath)
obj, err := plugins.LoadPlugin(oPath, pCtx)
if err != nil {
goutils.WriteError(fmt.Sprintf("unable to load plugin: %s", p), err)
panic("unable to load plugin")
}
if i, ok := obj.(plugins.Accounting); ok {
ctx.acct = true
ctx.accts = append(ctx.accts, i)
}
if i, ok := obj.(plugins.Authing); ok {
ctx.auth = true
ctx.auths = append(ctx.auths, i)
}
if i, ok := obj.(plugins.PreAuth); ok {
ctx.preauth = true
ctx.preauths = append(ctx.preauths, i)
}
ctx.modules = append(ctx.modules, obj)
ctx.module = true
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
clientLock.Lock()
clients = make(map[string]*connection)
clientLock.Unlock()
ctx.reload()
}
}()
if accounting {
goutils.WriteInfo("accounting mode")
account(ctx)
} else {
runProxy(ctx)
}
}