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
|
// +build linux
// Copyright (C) 2018-2020 LEAP
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package vpn
import (
"errors"
"log"
"os"
"os/exec"
"strings"
"0xacab.org/leap/bitmask-vpn/pkg/config"
"0xacab.org/leap/bitmask-vpn/pkg/vpn/bonafide"
"github.com/keybase/go-ps"
)
const (
systemOpenvpnPath = "/usr/sbin/openvpn"
)
var bitmaskRootPaths = []string{
"/usr/sbin/bitmask-root",
"/usr/local/sbin/bitmask-root",
}
type launcher struct {
openvpnCh chan []string
}
func newLauncher() (*launcher, error) {
l := launcher{make(chan []string, 1)}
go l.openvpnRunner()
return &l, nil
}
func (l *launcher) close() error {
return nil
}
func (l *launcher) check() (helpers bool, privilege bool, err error) {
hasHelpers, err := hasHelpers()
if err != nil {
log.Println("Error checking helpers")
return
}
if !hasHelpers {
log.Println("Could not find helpers")
return false, true, nil
}
isRunning, err := isPolkitRunning()
if err != nil {
log.Println("Error checking if polkit is running")
return
}
if !isRunning {
polkitPath := getPolkitPath()
if polkitPath == "" {
log.Println("Cannot find any usable polkit")
return true, false, nil
}
cmd := exec.Command("setsid", polkitPath)
err = cmd.Start()
if err != nil {
log.Println("Cannot launch polkit")
return
}
log.Println("Checking if polkit is running after attempted launch")
isRunning, err = isPolkitRunning()
return true, isRunning, err
}
return true, true, nil
}
func hasHelpers() (bool, error) {
/* TODO add polkit file too */
if _, err := bitmaskRootPath(); err == nil {
return true, nil
}
return false, nil
}
func isPolkitRunning() (bool, error) {
// TODO shouldn't we also check for polkitd running?
var polkitProcNames = [...]string{
"polkit-gnome-authentication-agent-1",
"polkit-kde-auth",
"polkit-mate-authentication-agent-1",
"lxpolkit",
"lxqt-policykit-agent",
"lxsession",
"gnome-shell",
"gnome-flashback",
"fingerprint-polkit-agent",
"xfce-polkit",
}
processes, err := ps.Processes()
if err != nil {
return false, err
}
for _, proc := range processes {
executable := proc.Executable()
for _, name := range polkitProcNames {
if strings.Contains(executable, name) {
return true, nil
}
}
}
return false, nil
}
func getPolkitPath() string {
var polkitPaths = [...]string{
"/usr/bin/lxpolkit",
"/usr/bin/lxqt-policykit-agent",
"/usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1",
"/usr/lib/x86_64-linux-gnu/polkit-mate/polkit-mate-authentication-agent-1",
"/usr/lib/mate-polkit/polkit-mate-authentication-agent-1",
"/usr/lib/x86_64-linux-gnu/libexec/polkit-kde-authentication-agent-1",
"/usr/lib/kde4/libexec/polkit-kde-authentication-agent-1",
// now we get weird
"/usr/libexec/policykit-1-pantheon/pantheon-agent-polkit",
"/usr/lib/polkit-1-dde/dde-polkit-agent",
// do you know some we"re still missing? :)
}
for _, polkit := range polkitPaths {
_, err := os.Stat(polkit)
if err == nil {
return polkit
}
}
return ""
}
func (l *launcher) openvpnStart(flags ...string) error {
log.Println("openvpn start: ", flags)
arg := []string{"openvpn", "start", getOpenvpnPath()}
arg = append(arg, flags...)
l.openvpnCh <- arg
return nil
}
func (l *launcher) openvpnStop() error {
l.openvpnCh <- nil
log.Println("openvpn stop")
return runBitmaskRoot("openvpn", "stop")
}
func (l *launcher) firewallStart(gateways []bonafide.Gateway) error {
log.Println("firewall start")
arg := []string{"firewall", "start"}
for _, gw := range gateways {
arg = append(arg, gw.IPAddress)
}
return runBitmaskRoot(arg...)
}
func (l *launcher) firewallStop() error {
log.Println("firewall stop")
return runBitmaskRoot("firewall", "stop")
}
func (l *launcher) firewallIsUp() bool {
err := runBitmaskRoot("firewall", "isup")
return err == nil
}
func (l *launcher) openvpnRunner(arg ...string) {
running := false
runOpenvpn := func(arg []string) {
for running {
err := runBitmaskRoot(arg...)
if err != nil {
log.Printf("An error ocurred running openvpn: %v", err)
}
}
}
for arg := range l.openvpnCh {
if arg == nil {
running = false
} else {
running = true
go runOpenvpn(arg)
}
}
}
func runBitmaskRoot(arg ...string) error {
bitmaskRoot, err := bitmaskRootPath()
if err != nil {
return err
}
arg = append([]string{bitmaskRoot}, arg...)
out, err := exec.Command("pkexec", arg...).Output()
if err != nil && arg[2] != "isup" {
log.Println("Error while running bitmask-root:")
log.Println("args: ", arg)
log.Println("output: ", string(out))
}
return err
}
func bitmaskRootPath() (string, error) {
if os.Getenv("SNAP") != "" {
path := "/snap/bin/" + config.BinaryName + ".bitmask-root"
if _, err := os.Stat(path); !os.IsNotExist(err) {
return path, nil
}
}
for _, path := range bitmaskRootPaths {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return path, nil
}
}
log.Println("Can't find bitmask-root")
return "", errors.New("nohelpers")
}
func getOpenvpnPath() string {
if os.Getenv("SNAP") != "" {
return "/snap/bin/" + config.BinaryName + ".openvpn"
}
return systemOpenvpnPath
}
|