forked from samm-git/aws-vpn-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
253 lines (237 loc) · 6.9 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
)
/*
* Wrapper for doing the open-vpn dance for aws vpns that use SAML auth
* Step 1: Attempt an openvpn connection using static names; that will give us the saml url
* Step 2: Visit the SAML url in a web browser and catch the response
* Step 3: Re-run openvpn with the new auth
*/
func main() {
sourceConfigFile := flag.String("config", "~/.awsvpn.conf", "Source aws vpn config file")
flag.Parse()
filePath := *sourceConfigFile
usr, _ := user.Current()
dir := usr.HomeDir
if filePath == "~" {
// In case of "~", which won't be caught by the "else if"
filePath = dir
} else if strings.HasPrefix(filePath, "~/") {
// Use strings.HasPrefix so we don't match paths like
// "/something/~/something/"
filePath = filepath.Join(dir, filePath[2:])
}
fmt.Println("Loading config from", filePath)
configFilename, serverURL, serverPort, err := createTempConfigFile(filePath)
if err != nil {
log.Fatal(err)
}
defer os.Remove(configFilename)
ips, err := net.LookupIP("dns." + serverURL) // have to use "random" subdomain
if err != nil || len(ips) == 0 {
fmt.Fprintf(os.Stderr, "Could not get IPs for VPN server : %v\n", err)
os.Exit(1)
}
serverURL = ips[0].String()
fmt.Printf("Starting vpn to %s:%s\n", serverURL, serverPort)
//Connect once to find the saml auth url to use
a := newawsSAMLAuthWrapper(serverURL, serverPort, configFilename)
a.runHTTPServer()
}
type awsSAMLAuthWrapper struct {
reauthrequest chan bool
samlResponseChan chan string
sidID string
server string
port string
confpath string
}
func newawsSAMLAuthWrapper(server, port, confpath string) *awsSAMLAuthWrapper {
s := &awsSAMLAuthWrapper{
samlResponseChan: make(chan string, 2),
sidID: "",
server: server,
port: port,
confpath: confpath,
reauthrequest: make(chan bool, 2),
}
return s
}
func (s *awsSAMLAuthWrapper) runHTTPServer() {
go s.worker()
s.reauthrequest <- true // Kick it all off
http.HandleFunc("/", s.handleSAMLServer)
log.Printf("Starting HTTP server at 127.0.0.1:35001")
err := http.ListenAndServe("127.0.0.1:35001", nil)
log.Fatal(err)
}
func (s *awsSAMLAuthWrapper) worker() {
//Listens for events from saml http server and spawns openvpn as appropriate
for {
select {
case auth, ok := <-s.samlResponseChan:
if !ok {
return
}
//we have authentication, lets spawn the correct openvpn
fmt.Println("Starting the actual openvpn ")
runOpenVPNAuthenticated(auth, s.sidID, s.server, s.port, s.confpath)
case <-s.reauthrequest:
//Startup the first stage to get our authentication going
s.stageOne()
}
}
}
func (s *awsSAMLAuthWrapper) stageOne() {
samlAuthpage, sid, err := initalcontactFindSAMLURL(s.confpath, s.server, s.port)
if err != nil {
log.Fatal(err)
}
s.sidID = sid
fmt.Println("Opening webpage to auth now", samlAuthpage)
openbrowser(samlAuthpage)
}
func (s *awsSAMLAuthWrapper) handleSAMLServer(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
SAMLResponse := r.FormValue("SAMLResponse")
if len(SAMLResponse) == 0 {
log.Printf("SAMLResponse field is empty or not exists")
return
}
s.samlResponseChan <- url.QueryEscape(SAMLResponse)
log.Printf("Got SAMLResponse field")
return
default:
fmt.Fprintf(w, "Error: POST method expected, %s recieved", r.Method)
}
}
func runOpenVPNAuthenticated(samlAuth, sid, server, serverPort, confpath string) {
fmt.Printf("Running openvpn with SID:%s server %s:%s\n", sid, server, serverPort)
destFile, err := ioutil.TempFile("", "aws_vpn_wrapper_config_*.password")
if err != nil {
return
}
commandInput := fmt.Sprintf("%s\r\nCRV1::%s::%s\r\n", "N/A", sid, samlAuth)
destFile.WriteString(commandInput)
destFile.Close()
cmd := exec.Command("sudo", "./openvpn-patched", "--config", confpath, "--remote", server, serverPort, "--auth-user-pass", destFile.Name())
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err = cmd.Start()
cmd.Wait()
cmd.Process.Kill()
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
func initalcontactFindSAMLURL(confpath, server, serverPort string) (SAMLString, sid string, err error) {
destFile, err := ioutil.TempFile("", "aws_vpn_wrapper_config_*.password")
if err != nil {
return
}
commandInput := fmt.Sprintf("%s\r\n%s\r\n", "N/A", "ACS::35001")
destFile.WriteString(commandInput)
destFile.Close()
cmd := exec.Command("./openvpn-patched", "--config", confpath, "--remote", server, serverPort, "--auth-user-pass", destFile.Name())
var outb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = os.Stderr
err = cmd.Start()
cmd.Wait()
// We wait until we get response
scanner := bufio.NewScanner(&outb)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "AUTH_FAILED,CRV1") {
//This line is our saml url
parts := strings.Split(line, "https://")
SAMLString = "https://" + parts[1]
parts = strings.Split(line, ":")
sid = parts[6]
}
}
cmd.Process.Kill()
return
}
//createTempConfigFile Creates a temporary config to use for authentication that has the server name parsed out
// and this is returned seperately
func createTempConfigFile(sourceFilePath string) (outputFilename string, server string, port string, err error) {
//Read the source file in and strip the server path and port out, and copy to a temp file
destFile, err := ioutil.TempFile("", "aws_vpn_wrapper_config_*.conf")
if err != nil {
return
}
outputWriter := bufio.NewWriter(destFile)
defer outputWriter.Flush()
defer destFile.Close()
outputFilename = destFile.Name()
//Read the source file in and copy all lines 1:1 except stripping server
sourceFile, err := os.Open(sourceFilePath)
if err != nil {
return
}
defer sourceFile.Close()
scanner := bufio.NewScanner(sourceFile)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "auth-retry") {
//Strip
} else if strings.HasPrefix(line, "resolv-retry") {
//Strip
} else if strings.HasPrefix(line, "auth-federate") {
//Strip
} else if strings.HasPrefix(line, "remote ") {
// Split this apart to find the hostname and the port
fields := strings.Fields(line)
server = fields[1]
port = fields[2]
} else {
_, err = outputWriter.WriteString(line + "\n")
if err != nil {
return
}
}
}
outputWriter.Flush()
if err = scanner.Err(); err != nil {
return
}
return
}