| 1 |
package openvpn |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"log/slog" |
| 9 |
"net" |
| 10 |
"os" |
| 11 |
"runtime/debug" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/managementauth" |
| 17 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/utils" |
| 18 |
) |
| 19 |
|
| 20 |
const writeTimeout = 1 * time.Second |
| 21 |
|
| 22 |
// handlePassThrough starts a listener for the passthrough interface. This allows the management interface to be |
| 23 |
// accessed from a different network interface or even from a different machine. |
| 24 |
// The passthrough interface is a simple text-based protocol that allows the client to send commands to the management |
| 25 |
// interface and receive the responses. |
| 26 |
// The passthrough interface is disabled by default. To enable it, set the passthrough.enabled option to true in the |
| 27 |
// configuration file. |
| 28 |
// |
| 29 |
//nolint:cyclop,gocognit |
| 30 |
func (c *Client) handlePassThrough(ctx context.Context, errCh chan<- error) { |
| 31 |
var conn net.Conn |
| 32 |
|
| 33 |
listener, closer, err := c.setupPassThroughListener(ctx) |
| 34 |
if err != nil { |
| 35 |
errCh <- fmt.Errorf("error setup openvpn management pass-through listener: %w", err) |
| 36 |
|
| 37 |
return |
| 38 |
} |
| 39 |
|
| 40 |
c.logger.LogAttrs(ctx, slog.LevelInfo, fmt.Sprintf("start pass-through listener on %s://%s", listener.Addr().Network(), listener.Addr().String())) |
| 41 |
|
| 42 |
defer func() { |
| 43 |
defer closer() |
| 44 |
|
| 45 |
if r := recover(); r != nil { |
| 46 |
errCh <- fmt.Errorf("panic: %v; stack %s", r, debug.Stack()) |
| 47 |
} |
| 48 |
}() |
| 49 |
|
| 50 |
connMu := sync.Mutex{} |
| 51 |
|
| 52 |
go func() { |
| 53 |
defer func() { |
| 54 |
if r := recover(); r != nil { |
| 55 |
c.logger.LogAttrs( |
| 56 |
ctx, slog.LevelError, "panic in handlePassThrough", |
| 57 |
slog.Any("err", r), |
| 58 |
slog.String("stack", string(debug.Stack())), |
| 59 |
) |
| 60 |
} |
| 61 |
}() |
| 62 |
|
| 63 |
var ( |
| 64 |
err error |
| 65 |
message string |
| 66 |
ok bool |
| 67 |
) |
| 68 |
|
| 69 |
for { |
| 70 |
select { |
| 71 |
case <-ctx.Done(): |
| 72 |
c.logger.LogAttrs(ctx, slog.LevelInfo, "shutdown OpenVPN pass-through connection") |
| 73 |
closer() |
| 74 |
|
| 75 |
return // Error somewhere, terminate |
| 76 |
case message, ok = <-c.passThroughCh: |
| 77 |
if !ok || message == "" || message == "\r\n" { |
| 78 |
continue |
| 79 |
} |
| 80 |
|
| 81 |
connMu.Lock() |
| 82 |
|
| 83 |
if conn == nil || c.passThroughConnected.Load() == 0 { |
| 84 |
connMu.Unlock() |
| 85 |
|
| 86 |
continue |
| 87 |
} |
| 88 |
|
| 89 |
_ = conn.SetWriteDeadline(time.Now().Add(writeTimeout)) |
| 90 |
|
| 91 |
if _, err = conn.Write([]byte(message + "\r\n")); err != nil { |
| 92 |
remoteAddr := "<unknown>" |
| 93 |
if conn != nil { |
| 94 |
remoteAddr = conn.RemoteAddr().String() |
| 95 |
} |
| 96 |
|
| 97 |
c.logger.LogAttrs(ctx, slog.LevelWarn, fmt.Errorf("unable to write message to client %w", err).Error(), slog.String("client", remoteAddr)) |
| 98 |
} |
| 99 |
|
| 100 |
connMu.Unlock() |
| 101 |
} |
| 102 |
} |
| 103 |
}() |
| 104 |
|
| 105 |
for { |
| 106 |
// Listen for an incoming connection. |
| 107 |
conn, err = listener.Accept() |
| 108 |
if err != nil { |
| 109 |
if errors.Is(err, net.ErrClosed) || ctx.Err() != nil || c.closed.Load() == 1 { |
| 110 |
return |
| 111 |
} |
| 112 |
|
| 113 |
errCh <- fmt.Errorf("error accepting: %w", err) |
| 114 |
|
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
c.handlePassThroughClient(ctx, conn) |
| 119 |
|
| 120 |
c.passThroughConnected.Store(0) |
| 121 |
|
| 122 |
connMu.Lock() |
| 123 |
|
| 124 |
conn = nil |
| 125 |
|
| 126 |
connMu.Unlock() |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func (c *Client) writeToPassThroughClient(message string) { |
| 131 |
if c.conf.OpenVPN.Passthrough.Enabled { |
| 132 |
c.passThroughCh <- message |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func (c *Client) handlePassThroughClient(ctx context.Context, conn net.Conn) { |
| 137 |
defer conn.Close() |
| 138 |
|
| 139 |
var ( |
| 140 |
err error |
| 141 |
logger *slog.Logger |
| 142 |
) |
| 143 |
|
| 144 |
switch c.conf.OpenVPN.Passthrough.Address.Scheme { |
| 145 |
case SchemeTCP: |
| 146 |
logger = c.logger.With(slog.String("client", conn.RemoteAddr().String())) |
| 147 |
case SchemeUnix: |
| 148 |
logger = c.logger.With(slog.String("client", conn.RemoteAddr().Network())) |
| 149 |
default: |
| 150 |
c.logger.LogAttrs(ctx, slog.LevelError, fmt.Errorf("%w %s", ErrUnknownProtocol, c.conf.OpenVPN.Passthrough.Address.Scheme).Error()) |
| 151 |
|
| 152 |
return |
| 153 |
} |
| 154 |
|
| 155 |
logger.LogAttrs(ctx, slog.LevelInfo, "pass-through: accepted connection") |
| 156 |
|
| 157 |
scanner := bufio.NewScanner(conn) |
| 158 |
scanner.Split(bufio.ScanLines) |
| 159 |
scanner.Buffer(make([]byte, 0, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize) |
| 160 |
|
| 161 |
if err = c.handlePassThroughClientAuth(ctx, conn, scanner); err != nil { |
| 162 |
logger.LogAttrs(ctx, slog.LevelWarn, err.Error()) |
| 163 |
|
| 164 |
return |
| 165 |
} |
| 166 |
|
| 167 |
c.passThroughConnected.CompareAndSwap(0, 1) |
| 168 |
c.writeToPassThroughClient(WelcomeBanner) |
| 169 |
|
| 170 |
if err = c.handlePassThroughClientCommands(ctx, conn, logger, scanner); err != nil { |
| 171 |
logger.LogAttrs(ctx, slog.LevelWarn, err.Error()) |
| 172 |
} |
| 173 |
|
| 174 |
logger.LogAttrs(ctx, slog.LevelInfo, "pass-through: closed connection") |
| 175 |
} |
| 176 |
|
| 177 |
func (c *Client) handlePassThroughClientCommands(ctx context.Context, conn net.Conn, logger *slog.Logger, scanner *bufio.Scanner) error { |
| 178 |
var ( |
| 179 |
err error |
| 180 |
line string |
| 181 |
resp string |
| 182 |
) |
| 183 |
|
| 184 |
for scanner.Scan() { |
| 185 |
line = strings.TrimSpace(scanner.Text()) |
| 186 |
if line == "" { |
| 187 |
continue |
| 188 |
} |
| 189 |
|
| 190 |
logger.LogAttrs(ctx, slog.LevelDebug, "received command", slog.String("command", managementCommandName(line))) |
| 191 |
|
| 192 |
switch { |
| 193 |
case passThroughCommandReserved(line): |
| 194 |
c.writeToPassThroughClient("ERROR: command not allowed") |
| 195 |
logger.LogAttrs(ctx, slog.LevelWarn, "pass-through: reserved command rejected", slog.String("command", managementCommandName(line))) |
| 196 |
|
| 197 |
continue |
| 198 |
case line == "hold": |
| 199 |
c.writeToPassThroughClient("SUCCESS: hold release succeeded") |
| 200 |
|
| 201 |
continue |
| 202 |
case line == "exit", line == "quit": |
| 203 |
_ = conn.Close() |
| 204 |
|
| 205 |
return nil |
| 206 |
} |
| 207 |
|
| 208 |
resp, err = c.SendCommand(ctx, line, true) |
| 209 |
if err != nil { |
| 210 |
logger.LogAttrs( |
| 211 |
ctx, |
| 212 |
slog.LevelWarn, |
| 213 |
"pass-through: command failed", |
| 214 |
slog.String("command", managementCommandName(line)), |
| 215 |
slog.String("err", err.Error()), |
| 216 |
) |
| 217 |
} else { |
| 218 |
c.writeToPassThroughClient(strings.TrimSpace(resp)) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if err = scanner.Err(); err != nil { |
| 223 |
return fmt.Errorf("pass-through: unable to read from client: %w", err) |
| 224 |
} |
| 225 |
|
| 226 |
return nil |
| 227 |
} |
| 228 |
|
| 229 |
func passThroughCommandReserved(line string) bool { |
| 230 |
switch strings.ToLower(managementCommandName(line)) { |
| 231 |
case "client-auth", "client-auth-nt", "client-deny", "client-pending-auth": |
| 232 |
return true |
| 233 |
default: |
| 234 |
return false |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func (c *Client) handlePassThroughClientAuth(_ context.Context, conn net.Conn, scanner *bufio.Scanner) error { |
| 239 |
err := managementauth.Authenticate(conn, scanner, c.conf.OpenVPN.Passthrough.Password.String(), writeTimeout) |
| 240 |
if err != nil { |
| 241 |
return fmt.Errorf("pass-through: %w", err) |
| 242 |
} |
| 243 |
|
| 244 |
return nil |
| 245 |
} |
| 246 |
|
| 247 |
func (c *Client) setupPassThroughListener(ctx context.Context) (net.Listener, func(), error) { |
| 248 |
var ( |
| 249 |
err error |
| 250 |
listenConfig net.ListenConfig |
| 251 |
listener net.Listener |
| 252 |
closer func() |
| 253 |
) |
| 254 |
|
| 255 |
switch c.conf.OpenVPN.Passthrough.Address.Scheme { |
| 256 |
case SchemeTCP: |
| 257 |
listener, err = listenConfig.Listen(ctx, c.conf.OpenVPN.Passthrough.Address.Scheme, c.conf.OpenVPN.Passthrough.Address.Host) |
| 258 |
if err != nil { |
| 259 |
return nil, nil, fmt.Errorf("error listen: %w", err) |
| 260 |
} |
| 261 |
|
| 262 |
closer = func() { _ = listener.Close() } |
| 263 |
case SchemeUnix: |
| 264 |
if err = utils.PrepareUnixSocket(ctx, c.conf.OpenVPN.Passthrough.Address.Path); err != nil { |
| 265 |
return nil, nil, fmt.Errorf("error prepare unix socket: %w", err) |
| 266 |
} |
| 267 |
|
| 268 |
listener, err = listenConfig.Listen(ctx, c.conf.OpenVPN.Passthrough.Address.Scheme, c.conf.OpenVPN.Passthrough.Address.Path) |
| 269 |
if err != nil { |
| 270 |
return nil, nil, fmt.Errorf("error listen: %w", err) |
| 271 |
} |
| 272 |
|
| 273 |
if err = c.setupUNIXSocketPermissions(); err != nil { |
| 274 |
_ = listener.Close() |
| 275 |
|
| 276 |
return nil, nil, err |
| 277 |
} |
| 278 |
|
| 279 |
closer = func() { _ = listener.Close() } |
| 280 |
default: |
| 281 |
return nil, nil, fmt.Errorf("%w %s", ErrUnknownProtocol, c.conf.OpenVPN.Addr.Scheme) |
| 282 |
} |
| 283 |
|
| 284 |
return listener, closer, nil |
| 285 |
} |
| 286 |
|
| 287 |
func (c *Client) setupUNIXSocketPermissions() error { |
| 288 |
if c.conf.OpenVPN.Passthrough.SocketGroup != "" { |
| 289 |
gid, err := utils.LookupGroup(c.conf.OpenVPN.Passthrough.SocketGroup) |
| 290 |
if err != nil { |
| 291 |
return fmt.Errorf("error lookup group: %w", err) |
| 292 |
} |
| 293 |
|
| 294 |
if err = os.Chown(c.conf.OpenVPN.Passthrough.Address.Path, -1, gid); err != nil { |
| 295 |
return fmt.Errorf("error chown: %w", err) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if err := os.Chmod(c.conf.OpenVPN.Passthrough.Address.Path, os.FileMode(c.conf.OpenVPN.Passthrough.SocketMode)); err != nil { |
| 300 |
return fmt.Errorf("error chmod: %w", err) |
| 301 |
} |
| 302 |
|
| 303 |
return nil |
| 304 |
} |