-
Notifications
You must be signed in to change notification settings - Fork 4
/
samlvpn.go
334 lines (284 loc) · 8.52 KB
/
samlvpn.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"net"
"net/url"
"os"
"os/exec"
"strings"
"time"
"github.com/pkg/errors"
)
var (
ErrAuthFailed = errors.New("auth failed")
ErrConnectionLost = errors.New("connection established then lost")
)
// SAMLVPN makes it easy to retry stuff that fails, etc.
type SAMLVPN struct {
Config *Config
OpenVPNConfig *OpenVPNConfig
}
// Configure parses all configs and sets them in s.
func (s *SAMLVPN) Configure(flagConfigFile *string) error {
var configFilePath string
defaultConfigFiles := []string{
"$XDG_CONFIG_HOME/samlvpn/config.yaml",
"$XDG_CONFIG_HOME/samlvpn.yaml",
"$HOME/.config/samlvpn.yaml",
"$HOME/.samlvpn.yaml",
}
if *flagConfigFile != "" {
configFilePath = *flagConfigFile
} else {
for _, path := range defaultConfigFiles {
fullPath := os.ExpandEnv(path)
if _, err := os.Stat(fullPath); err == nil {
configFilePath = fullPath
break
}
}
}
if configFilePath == "" {
return errors.Errorf(
"please specify a config file, could not find any default in %q",
strings.Join(defaultConfigFiles, ", "))
}
configFile, err := os.Open(configFilePath)
if err != nil {
return errors.Wrap(err, "could not open config file")
}
defer configFile.Close()
s.Config = &Config{}
if err := s.Config.ParseWithDefaults(configFile); err != nil {
return errors.Wrapf(err, "could not parse %q", configFilePath)
}
log.Printf("parsed config file %q", configFilePath)
if errs := s.Config.Validate(); errs != nil {
log.Println("the configuration file contains the following error(s):")
for i := range errs {
fmt.Fprintln(os.Stderr, "-", errs[i].Error())
}
os.Exit(1)
}
openVPNConfigFile, err := os.Open(s.Config.OpenVPNConfigFile)
if err != nil {
return errors.Wrap(err, "could not open OpenVPN config")
}
defer openVPNConfigFile.Close()
s.OpenVPNConfig, err = ParseOpenVPNConfig(openVPNConfigFile)
if err != nil {
errors.Wrap(err, "could not parse OpenVPN config")
}
return nil
}
// resolveHostname resolves the OpenVPN hostname.
func (s *SAMLVPN) resolveHostname() (string, error) {
host := randomString() + "." + s.OpenVPNConfig.Host
log.Println("looking up", host)
addrs, err := net.LookupHost(host)
if err != nil {
return "", err
}
if len(addrs) < 1 {
return "", errors.Errorf("no addresses found")
}
return addrs[0], nil
}
func (s *SAMLVPN) getLoginURLAndSID() (*url.URL, string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
authFile, err := tmpfile(s.Config.TempCredentialsFilePath,
"N/A\nACS::35001", s.Config.TempCredentialsPermissions)
if err != nil {
return nil, "", errors.Wrap(err, "could not create temp file")
}
defer authFile.Close()
cmd := exec.CommandContext(
ctx,
s.Config.OpenVPNBinary,
"--config", s.Config.OpenVPNConfigFile,
"--verb", "3",
"--auth-retry", "none",
"--auth-user-pass", authFile.Name(),
)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, "", errors.Errorf(
"could not run command to get AUTH_FAILED response: %s\nOpenVPN output:\n%s",
err, string(output))
}
for _, line := range strings.Split(string(output), "\n") {
if strings.Contains(line, "AUTH_FAILED") {
split := strings.Split(line, ":")
if len(split) < 10 {
return nil, "", errors.Errorf("could not find SID in output: %q", line)
}
url, err := url.Parse(split[8] + ":" + split[9])
if err != nil {
return nil, "", errors.Wrap(err, "could not parse URL")
}
return url, split[6], nil
}
}
return nil, "", errors.Errorf("could not find AUTH_FAILED line")
}
func (s *SAMLVPN) getSAMLCallback(challengeURL string) (string, error) {
addr := "0.0.0.0:35001"
// Long timeout to allow user to follow SAML prompts etc.
timeout := time.Second * 120
log.Printf("starting HTTP server on %s, timeout %v", addr, timeout)
server := NewServer(addr, s.Config.RedirectURL, timeout)
server.Start()
s.openOrShowLink(challengeURL)
log.Println("waiting for server to receive SAML callback")
response, err := server.WaitForResponse()
if err != nil {
return "", errors.Wrap(err, "could not get response")
}
return response, nil
}
// openOrShowLink opens or shows a link to the user, depending on config.
func (s *SAMLVPN) openOrShowLink(url string) {
if len(s.Config.BrowserCommand) < 1 {
log.Println("open this:", url)
return
}
for i := range s.Config.BrowserCommand {
if strings.Contains(s.Config.BrowserCommand[i], "%s") {
s.Config.BrowserCommand[i] = fmt.Sprintf(s.Config.BrowserCommand[i], url)
break
}
}
cmd := exec.Command(s.Config.BrowserCommand[0], s.Config.BrowserCommand[1:]...)
log.Println("launching", cmd)
output := &bytes.Buffer{}
cmd.Stderr = output
cmd.Stdout = output
if err := cmd.Run(); err != nil {
log.Println(errors.Wrap(err, "could not open URL in browser"))
log.Println("open this manually:", url)
return
}
log.Println("your browser said:", strings.TrimSpace(output.String()))
}
func (s *SAMLVPN) getCredentials() (string, error) {
challengeURL, SID, err := s.getLoginURLAndSID()
if err != nil {
return "", errors.Wrap(err, "could not get challenge URL")
}
samlCallback, err := s.getSAMLCallback(challengeURL.String())
if err != nil {
return "", errors.Wrap(err, "could not get SAML callback")
}
return fmt.Sprintf("N/A\nCRV1::%s::%s", SID, samlCallback), nil
}
func (s *SAMLVPN) Connect() error {
credentials, err := s.getCredentials()
if err != nil {
return errors.Wrap(err, "could not get credentials")
}
credsFile, err := tmpfile(s.Config.TempCredentialsFilePath,
credentials, s.Config.TempCredentialsPermissions)
if err != nil {
log.Fatal(errors.Wrap(err, "could not create credentials file"))
}
defer credsFile.Close()
log.Println("saved credentials to", credsFile.Name())
if s.Config.RunCommand {
for i := 0; i <= s.Config.AuthFailedRetries; i++ {
err := s.runCommand(credsFile)
if errors.Is(err, ErrAuthFailed) && s.Config.AuthFailedRetries != 0 {
log.Println("Auth failed, try #", i+1, ".")
continue
}
if errors.Is(err, ErrConnectionLost) {
log.Println("Connection lost. Restart SamlVPN to reconnect.")
s.runConnLost()
}
return err
}
}
s.printCommand(credsFile)
return nil
}
// runConnLost runs the command that the user wants to run when the connection
// is lost.
func (s *SAMLVPN) runConnLost() {
if len(s.Config.ConnectionLostCommand) == 0 {
return
}
cmd := exec.Command(
s.Config.ConnectionLostCommand[0],
s.Config.ConnectionLostCommand[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
log.Printf("conn lost command did not execute correctly: %s", err)
}
}
// rebuildCommand rebuilds the OpenVPN command with a new hostname.
func (s *SAMLVPN) rebuildCommand(ctx context.Context, credsFile *os.File) (*exec.Cmd, error) {
cmd := exec.CommandContext(
ctx,
"sudo",
s.Config.OpenVPNBinary,
"--config", s.Config.OpenVPNConfigFile,
"--verb", "3",
"--auth-nocache",
"--proto", s.OpenVPNConfig.Protocol,
"--auth-retry", "none",
"--auth-user-pass", credsFile.Name())
hostname, err := s.resolveHostname()
if err != nil {
return nil, errors.Wrap(err, "could not resolve hostname")
}
cmd.Args = append(cmd.Args, "--remote", hostname, fmt.Sprint(s.OpenVPNConfig.Port))
return cmd, nil
}
func (s *SAMLVPN) runCommand(credsFile *os.File) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd, err := s.rebuildCommand(ctx, credsFile)
if err != nil {
return errors.Wrap(err, "could not rebuild command")
}
var output bytes.Buffer
cmd.Stdout = io.MultiWriter(&output, os.Stdout)
cmd.Stderr = io.MultiWriter(&output, os.Stderr)
log.Println("starting openvpn")
if err := cmd.Run(); err != nil {
return errors.Wrap(err, "could not run command")
}
// Check if it failed to run. The exit code does not indicate this so need
// to check the logs.
sca := bufio.NewScanner(&output)
for sca.Scan() {
line := sca.Text()
if strings.Contains(line, "AUTH_FAILED") {
return ErrAuthFailed
}
if strings.Contains(line, "Initialization Sequence Completed") {
// After the init sequence is complete, the failure can only
// logically be that the connection has been lost.
return ErrConnectionLost
}
}
if err := sca.Err(); err != nil {
return errors.Wrap(err, "could not close scanner")
}
return nil
}
func (s *SAMLVPN) printCommand(credsFile *os.File) error {
cmd, err := s.rebuildCommand(context.TODO(), credsFile)
if err != nil {
return errors.Wrap(err, "could not rebuild command")
}
fmt.Print(cmd)
return nil
}