-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
197 lines (171 loc) · 3.98 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
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
yaml "gopkg.in/yaml.v2"
)
const DEFAULT_VPNC_SCRIPT = "/usr/lib/openconnect-helper/script"
type creds struct {
Username string `yaml:"username,omitempty"`
Password string
TOTP string `yaml:"totp,omitempty"`
}
func init() {
log.SetFlags(log.Lshortfile)
log.SetOutput(os.Stderr)
}
func checkEnv() error {
for _, env := range []string{
"PASS_NAME",
"PROTOCOL",
} {
if os.Getenv(env) == "" {
return fmt.Errorf("environment variable %s not set", env)
}
}
return nil
}
func newCreds(passName, stripDomain string) (*creds, error) {
cmd := exec.Command("gopass", "show", "-f", passName)
out, err := cmd.Output()
if err != nil {
var b strings.Builder
fmt.Fprintf(&b, "error running cmd: %v", err)
switch err := err.(type) {
case *exec.ExitError:
fmt.Fprintf(&b, "out: %s; err: %s", out, err.Stderr)
}
fmt.Fprint(&b, "\n")
return nil, fmt.Errorf(b.String())
}
split := bytes.Split(out, []byte("---\n"))
var cred creds
cred.Password = string(bytes.Split(split[0], []byte("\n"))[0])
if err := yaml.Unmarshal(split[1], &cred); err != nil {
log.Printf("error unmarshalling YAML: %v\n", err)
}
if stripDomain != "" {
cred.Username = strings.Split(cred.Username, "@")[0]
}
return &cred, nil
}
func getTOTP(passName string) (string, error) {
cmd := exec.Command("gopass", "otp", passName)
out, err := cmd.Output()
if err != nil {
var b strings.Builder
fmt.Fprintf(&b, "error running cmd: %v", err)
switch err := err.(type) {
case *exec.ExitError:
fmt.Fprintf(&b, "out: %s; err: %s", out, err.Stderr)
}
fmt.Fprint(&b, "\n")
return "", fmt.Errorf(b.String())
}
return string(bytes.Fields(out)[0]), nil
}
func startOpenconnect(cred *creds, authgroup, totp, protocol, iface, script, extraArgs, server string) (*exec.Cmd, error) {
var b strings.Builder
b.WriteString(cred.Password)
b.WriteString("\n")
if authgroup != "" {
b.WriteString(authgroup)
b.WriteString("\n")
}
b.WriteString(totp)
b.WriteString("\n")
args := []string{
"--protocol", protocol,
"--user", cred.Username,
"--passwd-on-stdin",
"--interface", iface,
"--script", script,
}
args = append(args, strings.Fields(extraArgs)...)
args = append(args, server)
cmd := exec.Command(
"openconnect",
args...,
)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("error creating stdin pipe: %v", err)
}
go func() {
defer stdin.Close()
io.WriteString(stdin, b.String())
}()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Printf("running cmd: %s\n", cmd.Args)
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("error starting cmd: %v", err)
}
return cmd, nil
}
func main() {
if err := checkEnv(); err != nil {
log.Fatal(err)
}
if len(os.Args) != 2 && os.Args[1] != "" {
log.Fatal("no server specified")
}
passName := os.Getenv("PASS_NAME")
cred, err := newCreds(passName, os.Getenv("STRIP_DOMAIN"))
if err != nil {
log.Fatal(err)
}
var totp string
otpName := os.Getenv("OTP_NAME")
switch {
case otpName != "":
totp, err = getTOTP(otpName)
case cred.TOTP != "":
totp, err = getTOTP(passName)
}
if err != nil {
log.Fatal(err)
}
extra_args := os.Getenv("EXTRA_ARGS")
authgroup := os.Getenv("AUTHGROUP")
if authgroup != "" {
extra_args += "--usergroup portal"
}
script := os.Getenv("SCRIPT")
if script == "" {
script = DEFAULT_VPNC_SCRIPT
}
process, err := startOpenconnect(
cred,
authgroup,
totp,
os.Getenv("PROTOCOL"),
strings.Replace(fmt.Sprintf("tun-%.9s", os.Args[1]), ".", "-", -1),
script,
extra_args,
os.Args[1],
)
if err != nil {
log.Fatal(err)
}
sigs := make(chan os.Signal, 1)
done := make(chan struct{}, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Printf("received signal %v", sig)
process.Process.Signal(sig)
close(done)
}()
if err := process.Wait(); err != nil {
log.Printf("error running cmd: %v\n", err)
os.Exit(1)
}
}