summaryrefslogtreecommitdiff
path: root/pkg/pickle/helpers.go
blob: ec034d35878fb8440b6b10a5bf39886e06fc3150 (plain)
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
//go:generate statik -src=../../helpers -include=*

// Copyright (C) 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 pickle

import (
	"fmt"
	"io/ioutil"
	"log"
	"math/rand"
	"os"
	"os/exec"
	"time"

	_ "0xacab.org/leap/bitmask-vpn/pkg/pickle/statik"
	"github.com/rakyll/statik/fs"
)

const (
	bitmaskRoot = "/usr/sbin/bitmask-root"
	// TODO parametrize this with config.appName
	policyFile = "/usr/share/polkit-1/actions/se.leap.bitmask.riseupvpn.policy"
)

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randSeq(n int) string {
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

func check(e error) {
	if e != nil {
		panic(e)
	}
}

func alreadyThere(path string) bool {
	if _, err := os.Stat(path); err == nil {
		return true
	}
	return false
}

func copyAsRoot(orig, dest string, isExec bool) {
	if alreadyThere(dest) {
		fmt.Println("> File exists: ", dest)
		return
	}
	var confirm string
	fmt.Println("> About to write (as root):", dest)
	fmt.Printf("> Continue? [y/N] ")
	fmt.Scanln(&confirm)
	if confirm != "y" {
		fmt.Println("aborting")
		os.Exit(1)
	}
	cmd := exec.Command("sudo", "cp", orig, dest)
	err := cmd.Run()
	check(err)

	if isExec {
		cmd = exec.Command("sudo", "chmod", "776", dest)
		err = cmd.Run()
		check(err)
	}

	fmt.Println("> done")
}

/* dumpHelper works in linux only at the moment.
   TODO should separate implementations by platform */
func dumpHelper(fname, dest string, isExec bool) {
	stFS, err := fs.New()
	if err != nil {
		log.Fatal(err)
	}

	r, err := stFS.Open("/" + fname)
	if err != nil {
		log.Fatal(err)
	}
	defer r.Close()

	c, err := ioutil.ReadAll(r)
	if err != nil {
		log.Fatal(err)
	}
	tmp := "/dev/shm/" + randSeq(14)

	f, err := os.Create(tmp)
	check(err)
	defer os.Remove(tmp)

	_, err = f.Write(c)
	check(err)
	copyAsRoot(tmp, dest, isExec)
}

func InstallHelpers() {
	rand.Seed(time.Now().UnixNano())
	dumpHelper("bitmask-root", bitmaskRoot, true)
	dumpHelper("se.leap.bitmask.policy", policyFile, false)
}