/
main.go
282 lines (213 loc) · 7.01 KB
/
main.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
package openvpn
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"log/slog"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/jkroepke/openvpn-auth-oauth2/internal/config"
"github.com/jkroepke/openvpn-auth-oauth2/internal/oauth2"
"github.com/jkroepke/openvpn-auth-oauth2/internal/openvpn/connection"
"github.com/jkroepke/openvpn-auth-oauth2/internal/state"
"github.com/jkroepke/openvpn-auth-oauth2/internal/utils"
)
const minManagementInterfaceVersion = 5
func New(ctx context.Context, logger *slog.Logger, conf config.Config, oauth2Client *oauth2.Provider) *Client {
client := &Client{
conf: conf,
logger: logger,
oauth2: oauth2Client,
connMu: sync.Mutex{},
commandsBuffer: bytes.Buffer{},
clientsCh: make(chan connection.Client, 10),
commandResponseCh: make(chan string, 10),
commandsCh: make(chan string, 10),
passthroughCh: make(chan string, 10),
}
client.commandsBuffer.Grow(512)
client.ctx, client.ctxCancel = context.WithCancelCause(ctx)
return client
}
func (c *Client) Connect() error {
var err error
c.logger.Info("connect to openvpn management interface " + c.conf.OpenVpn.Addr.String())
if err = c.setupConnection(); err != nil {
return fmt.Errorf("unable to connect to openvpn management interface %s: %w", c.conf.OpenVpn.Addr.String(), err)
}
defer c.conn.Close()
c.scanner = bufio.NewScanner(c.conn)
c.scanner.Split(bufio.ScanLines)
c.scanner.Buffer(make([]byte, 0, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
if err = c.handlePassword(); err != nil {
return err
}
go c.handleMessages()
go c.handleClients()
go c.handleCommands()
c.logger.Info("connection to OpenVPN management interface established.")
if c.conf.OpenVpn.Passthrough.Enabled {
go c.handlePassthrough()
}
err = c.checkManagementInterfaceVersion()
if err != nil && !errors.Is(err, ErrConnectionTerminated) {
c.ctxCancel(err)
}
<-c.ctx.Done()
c.Shutdown()
err = context.Cause(c.ctx)
if err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("OpenVPN management error: %w", err)
}
return nil
}
func (c *Client) setupConnection() error {
c.connMu.Lock()
defer c.connMu.Unlock()
var err error
switch c.conf.OpenVpn.Addr.Scheme {
case SchemeTCP:
c.conn, err = net.DialTimeout(c.conf.OpenVpn.Addr.Scheme, c.conf.OpenVpn.Addr.Host, 1*time.Second)
case SchemeUnix:
c.conn, err = net.DialTimeout(c.conf.OpenVpn.Addr.Scheme, c.conf.OpenVpn.Addr.Path, 1*time.Second)
default:
err = fmt.Errorf("unable to connect to openvpn management interface: %w %s", ErrUnknownProtocol, c.conf.OpenVpn.Addr.Scheme)
}
return err
}
func (c *Client) checkManagementInterfaceVersion() error {
resp, err := c.SendCommand("version", false)
if resp == "" {
return nil
}
if err != nil {
return fmt.Errorf("error from version command: %w", err)
}
if !strings.HasPrefix(resp, "OpenVPN Version: ") {
return fmt.Errorf("error from version command: %w: %s", ErrErrorResponse, resp)
}
versionParts := strings.Split(resp, "\r\n")
if len(versionParts) != 4 {
return fmt.Errorf("unexpected response from version command: %s", resp)
}
c.logger.Info(utils.StringConcat(versionParts[0], " - ", versionParts[1]))
managementInterfaceVersion, err := strconv.Atoi(versionParts[1][len(versionParts[1])-1:])
if err != nil {
return fmt.Errorf("unable to parse openvpn management interface version: %w", err)
}
// Management Interface Version 5 is required at minimum
// ref: https://github.com/OpenVPN/openvpn/commit/a261e173341f8e68505a6ab5a413d09b0797a459
if managementInterfaceVersion < minManagementInterfaceVersion {
return errors.New("openvpn-auth-oauth2 requires OpenVPN management interface version 5 or higher")
}
return nil
}
func (c *Client) checkClientSsoCapabilities(logger *slog.Logger, client connection.Client) bool {
if strings.Contains(client.IvSSO, "webauth") {
return true
}
errorSsoNotSupported := "OpenVPN Client does not support SSO authentication via webauth"
logger.Warn(errorSsoNotSupported)
c.DenyClient(logger, state.ClientIdentifier{CID: client.CID, KID: client.KID}, errorSsoNotSupported)
return false
}
// Shutdown shutdowns the client connection.
func (c *Client) Shutdown() {
if !c.closed.CompareAndSwap(0, 1) {
return
}
c.logger.Info("shutdown OpenVPN management connection")
c.ctxCancel(nil)
c.connMu.Lock()
defer c.connMu.Unlock()
if c.conn != nil {
_ = c.conn.Close()
}
close(c.commandsCh)
}
// SendCommand passes command to a given connection (adds logging and EOL character) and returns the response.
func (c *Client) SendCommand(cmd string, passTrough bool) (string, error) {
if c.closed.Load() == 1 {
return "", nil
}
c.commandsCh <- cmd
select {
case <-c.ctx.Done():
return "", ErrConnectionTerminated // Error somewhere, terminate
case resp := <-c.commandResponseCh:
if passTrough {
return resp, nil
}
if resp == "" {
cmdFirstLine := strings.SplitN(cmd, "\r\n", 2)[0]
return "", fmt.Errorf("command error '%s': %w", cmdFirstLine, ErrEmptyResponse)
}
if strings.HasPrefix(resp, "ERROR:") {
cmdFirstLine := strings.SplitN(cmd, "\r\n", 2)[0]
c.logger.Warn(fmt.Sprintf("command error '%s': %s", cmdFirstLine, resp))
}
return resp, nil
case <-time.After(10 * time.Second):
cmdFirstLine := strings.SplitN(cmd, "\r\n", 2)[0]
return "", fmt.Errorf("command timeout '%s': %w", cmdFirstLine, ErrTimeout)
}
}
// SendCommandf passes command to a given connection (adds logging and EOL character) and returns the response.
func (c *Client) SendCommandf(format string, a ...any) (string, error) {
return c.SendCommand(fmt.Sprintf(format, a...), false)
}
// rawCommand passes command to a given connection (adds logging and EOL character).
func (c *Client) rawCommand(cmd string) error {
if c.logger.Enabled(context.Background(), slog.LevelDebug) {
c.logger.Debug(cmd)
}
c.commandsBuffer.Reset()
c.commandsBuffer.WriteString(cmd)
c.commandsBuffer.WriteString("\r\n")
if err := c.conn.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
return fmt.Errorf("unable to set read deadline: %w", err)
}
if _, err := c.commandsBuffer.WriteTo(c.conn); err != nil {
return fmt.Errorf("unable to write into OpenVPN management connection: %w", err)
}
return nil
}
// readMessage .
func (c *Client) readMessage(buf *bytes.Buffer) error {
buf.Reset()
var line []byte
for c.scanner.Scan() {
line = c.scanner.Bytes()
if len(line) == 0 {
continue
}
if _, err := buf.Write(line); err != nil {
return fmt.Errorf("unable to write string to buffer: %w", err)
}
if _, err := buf.WriteString("\r\n"); err != nil {
return fmt.Errorf("unable to write newline to buffer: %w", err)
}
if c.isMessageLineEOF(line) {
return nil
}
}
if c.scanner.Err() != nil {
return fmt.Errorf("scanner error: %w", c.scanner.Err())
}
return io.EOF
}
func (c *Client) isMessageLineEOF(line []byte) bool {
switch string(line[0:2]) {
// SUCCESS, ERROR, END, >HOLD, >INFO, >NOTIFY
case "SU", "ER", "EN", ">H", ">I", ">N":
return true
default:
return bytes.Equal(line, []byte(">CLIENT:ENV,END"))
}
}