mkcert/truststore_linux.go

94 lines
2.6 KiB
Go
Raw Normal View History

2018-06-26 05:48:41 +00:00
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"io/ioutil"
2018-06-26 05:48:41 +00:00
"log"
2018-06-28 05:29:20 +00:00
"os"
"os/exec"
2018-06-26 05:48:41 +00:00
"path/filepath"
"strings"
2018-06-26 05:48:41 +00:00
)
2018-06-28 05:29:20 +00:00
var (
FirefoxProfile = os.Getenv("HOME") + "/.mozilla/firefox/*"
CertutilInstallHelp = `apt install libnss3-tools" or "yum install nss-tools`
NSSBrowsers = "Firefox and/or Chrome/Chromium"
SystemTrustFilename string
SystemTrustCommand []string
2018-06-28 05:29:20 +00:00
)
func init() {
if pathExists("/etc/pki/ca-trust/source/anchors/") {
SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/mkcert-rootCA.pem"
SystemTrustCommand = []string{"update-ca-trust", "extract"}
} else if pathExists("/usr/local/share/ca-certificates/") {
SystemTrustFilename = "/usr/local/share/ca-certificates/mkcert-rootCA.crt"
SystemTrustCommand = []string{"update-ca-certificates"}
} else if pathExists("/etc/ca-certificates/trust-source/anchors/") {
SystemTrustFilename = "/etc/ca-certificates/trust-source/anchors/mkcert-rootCA.crt"
SystemTrustCommand = []string{"trust", "extract-compat"}
}
2018-07-04 04:06:50 +00:00
if SystemTrustCommand != nil {
_, err := exec.LookPath(SystemTrustCommand[0])
if err != nil {
SystemTrustCommand = nil
}
}
}
func pathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
2018-07-04 04:06:50 +00:00
func (m *mkcert) installPlatform() bool {
if SystemTrustCommand == nil {
2018-07-04 04:06:50 +00:00
log.Printf("Installing to the system store is not yet supported on this Linux 😣 but %s will still work.", NSSBrowsers)
log.Printf("You can also manually install the root certificate at %q.", filepath.Join(m.CAROOT, rootName))
return false
}
cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
fatalIfErr(err, "failed to read root certificate")
2018-07-04 04:06:50 +00:00
cmd := CommandWithSudo("tee", SystemTrustFilename)
cmd.Stdin = bytes.NewReader(cert)
out, err := cmd.CombinedOutput()
fatalIfCmdErr(err, "tee", out)
2018-07-04 04:06:50 +00:00
cmd = CommandWithSudo(SystemTrustCommand...)
out, err = cmd.CombinedOutput()
fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
2018-07-04 04:06:50 +00:00
return true
2018-06-26 05:48:41 +00:00
}
2018-07-04 04:06:50 +00:00
func (m *mkcert) uninstallPlatform() bool {
if SystemTrustCommand == nil {
2018-07-04 04:06:50 +00:00
return false
}
2018-07-04 04:06:50 +00:00
cmd := CommandWithSudo("rm", SystemTrustFilename)
out, err := cmd.CombinedOutput()
fatalIfCmdErr(err, "rm", out)
2018-07-04 04:06:50 +00:00
cmd = CommandWithSudo(SystemTrustCommand...)
out, err = cmd.CombinedOutput()
fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
2018-07-04 04:06:50 +00:00
return true
}
func CommandWithSudo(cmd ...string) *exec.Cmd {
if _, err := exec.LookPath("sudo"); err != nil {
return exec.Command(cmd[0], cmd[1:]...)
}
return exec.Command("sudo", append([]string{"--"}, cmd...)...)
}