-
Notifications
You must be signed in to change notification settings - Fork 3
/
platform_linux.go
190 lines (159 loc) · 4.79 KB
/
platform_linux.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
package truststore
import (
"bytes"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"sync"
"syscall"
)
var nssBrowsers = "Firefox and/or Chrome/Chromium"
type Platform struct {
HomeDir string
DataFS DataFS
SysFS CmdFS
inito sync.Once
nssBrowsers string
certutilInstallHelp string
trustFilenamePattern string
trustCommand []string
caBundleFileName string
}
func firefoxProfiles(homeDir string) []string {
return []string{
filepath.Join(homeDir, "/.mozilla/firefox/*"),
filepath.Join(homeDir, "/.mozilla/firefox-esr/*"),
filepath.Join(homeDir, "/.mozilla/firefox-trunk/*"),
filepath.Join(homeDir, "/snap/firefox/common/.mozilla/firefox/*"),
}
}
func certutilInstallHelp(sysFS CmdFS) string {
switch {
case binaryExists(sysFS, "apt"):
return "apt install libnss3-tools"
case binaryExists(sysFS, "yum"):
return "yum install nss-tools"
case binaryExists(sysFS, "zypper"):
return "zypper install mozilla-nss-tools"
}
return ""
}
func (s *Platform) Description() string { return "System (Linux)" }
func (s *Platform) init() {
s.inito.Do(func() {
s.certutilInstallHelp = certutilInstallHelp(s.SysFS)
switch {
case pathExists(s.DataFS, "/etc/pki/ca-trust/source/anchors/"):
s.trustFilenamePattern = "/etc/pki/ca-trust/source/anchors/%s.pem"
s.caBundleFileName = "/etc/pki/tls/certs/ca-bundle.crt"
s.trustCommand = []string{"update-ca-trust", "extract"}
case pathExists(s.DataFS, "/usr/local/share/ca-certificates/"):
s.trustFilenamePattern = "/usr/local/share/ca-certificates/%s.crt"
s.caBundleFileName = "/etc/ssl/certs/ca-certificates.crt"
s.trustCommand = []string{"update-ca-certificates"}
case pathExists(s.DataFS, "/etc/ca-certificates/trust-source/anchors/"):
s.trustFilenamePattern = "/etc/ca-certificates/trust-source/anchors/%s.crt"
s.caBundleFileName = "/etc/ssl/certs/ca-certificates.crt"
s.trustCommand = []string{"trust", "extract-compat"}
case pathExists(s.DataFS, "/usr/share/pki/trust/anchors"):
s.trustFilenamePattern = "/usr/share/pki/trust/anchors/%s.pem"
s.caBundleFileName = "/etc/ssl/ca-bundle.pem"
s.trustCommand = []string{"update-ca-certificates"}
}
})
}
func (s *Platform) check() (bool, error) {
s.init()
if s.trustCommand == nil {
return false, ErrUnsupportedDistro
}
return true, nil
}
func (s *Platform) checkCA(ca *CA) (bool, error) {
if ok, err := s.check(); !ok {
return ok, err
}
buf, err := s.DataFS.ReadFile(s.trustFilenamePath(ca))
if err != nil {
if errors.Is(err, syscall.ENOENT) {
return false, nil
}
return false, err
}
var blk *pem.Block
for len(buf) > 0 {
if blk, buf = pem.Decode(buf); blk == nil {
return false, nil
}
if bytes.Equal(ca.Raw, blk.Bytes) {
return true, nil
}
}
return false, nil
}
func (s *Platform) installCA(ca *CA) (bool, error) {
cert, err := ioutil.ReadFile(ca.FilePath)
if err != nil {
return false, fatalErr(err, "failed to read root certificate")
}
cmd := s.SysFS.Command("tee", s.trustFilenamePath(ca))
cmd.Stdin = bytes.NewReader(cert)
if out, err := s.SysFS.SudoExec(cmd); err != nil {
return false, fatalCmdErr(err, "tee", out)
}
if out, err := s.SysFS.SudoExec(s.SysFS.Command(s.trustCommand[0], s.trustCommand[1:]...)); err != nil {
return false, fatalCmdErr(err, strings.Join(s.trustCommand, " "), out)
}
return true, nil
}
func (s *Platform) listCAs() ([]*CA, error) {
if ok, err := s.check(); !ok {
return nil, err
}
data, err := s.DataFS.ReadFile(s.caBundleFileName)
if err != nil {
if errors.Is(err, syscall.ENOENT) {
return nil, nil
}
return nil, err
}
var cas []*CA
for p, buf := pem.Decode(data); p != nil; p, buf = pem.Decode(buf) {
cert, err := x509.ParseCertificate(p.Bytes)
if err != nil {
return nil, err
}
ca := &CA{
Certificate: cert,
UniqueName: cert.SerialNumber.Text(16),
}
cas = append(cas, ca)
}
return cas, nil
}
func (s *Platform) uninstallCA(ca *CA) (bool, error) {
cmd := s.SysFS.Command("rm", "-f", s.trustFilenamePath(ca))
if out, err := s.SysFS.SudoExec(cmd); err != nil {
return false, fatalCmdErr(err, "rm", out)
}
// We used to install under non-unique filenames.
legacyFilename := fmt.Sprintf(s.trustFilenamePattern, "mkcert-rootCA")
if pathExists(s.DataFS, legacyFilename) {
cmd := s.SysFS.Command("rm", "-f", legacyFilename)
if out, err := s.SysFS.SudoExec(cmd); err != nil {
return false, fatalCmdErr(err, "rm (legacy filename)", out)
}
}
cmd = s.SysFS.Command(s.trustCommand[0], s.trustCommand[1:]...)
if out, err := s.SysFS.SudoExec(cmd); err != nil {
return false, fatalCmdErr(err, strings.Join(s.trustCommand, " "), out)
}
return true, nil
}
func (s *Platform) trustFilenamePath(ca *CA) string {
return fmt.Sprintf(s.trustFilenamePattern, strings.Replace(ca.UniqueName, " ", "_", -1))
}