2019-06-01 13:58:20 +00:00
|
|
|
// Copyright 2018 The mkcert Authors. All rights reserved.
|
2018-06-26 05:48:41 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-07-04 02:46:39 +00:00
|
|
|
"bytes"
|
2018-08-19 23:02:28 +00:00
|
|
|
"fmt"
|
2018-07-04 02:46:39 +00:00
|
|
|
"io/ioutil"
|
2018-06-26 05:48:41 +00:00
|
|
|
"log"
|
2018-06-28 05:29:20 +00:00
|
|
|
"os"
|
2018-07-04 02:46:39 +00:00
|
|
|
"os/exec"
|
2018-06-26 05:48:41 +00:00
|
|
|
"path/filepath"
|
2018-07-04 02:46:39 +00:00
|
|
|
"strings"
|
2018-06-26 05:48:41 +00:00
|
|
|
)
|
|
|
|
|
2018-06-28 05:29:20 +00:00
|
|
|
var (
|
2019-06-01 13:41:10 +00:00
|
|
|
FirefoxProfile = os.Getenv("HOME") + "/.mozilla/firefox/*"
|
|
|
|
NSSBrowsers = "Firefox and/or Chrome/Chromium"
|
2018-07-04 02:46:39 +00:00
|
|
|
|
|
|
|
SystemTrustFilename string
|
|
|
|
SystemTrustCommand []string
|
2019-06-01 13:41:10 +00:00
|
|
|
CertutilInstallHelp string
|
2018-06-28 05:29:20 +00:00
|
|
|
)
|
|
|
|
|
2018-07-04 02:46:39 +00:00
|
|
|
func init() {
|
2019-06-01 13:41:10 +00:00
|
|
|
switch {
|
|
|
|
case binaryExists("apt"):
|
|
|
|
CertutilInstallHelp = "apt install libnss3-tools"
|
|
|
|
case binaryExists("yum"):
|
|
|
|
CertutilInstallHelp = "yum install nss-tools"
|
|
|
|
case binaryExists("zypper"):
|
|
|
|
CertutilInstallHelp = "zypper install mozilla-nss-tools"
|
|
|
|
}
|
2018-08-19 22:36:14 +00:00
|
|
|
if pathExists("/etc/pki/ca-trust/source/anchors/") {
|
2018-08-19 23:02:28 +00:00
|
|
|
SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/%s.pem"
|
2018-07-04 02:46:39 +00:00
|
|
|
SystemTrustCommand = []string{"update-ca-trust", "extract"}
|
2018-08-19 22:36:14 +00:00
|
|
|
} else if pathExists("/usr/local/share/ca-certificates/") {
|
2018-08-19 23:02:28 +00:00
|
|
|
SystemTrustFilename = "/usr/local/share/ca-certificates/%s.crt"
|
2018-08-19 22:36:14 +00:00
|
|
|
SystemTrustCommand = []string{"update-ca-certificates"}
|
|
|
|
} else if pathExists("/etc/ca-certificates/trust-source/anchors/") {
|
2018-08-19 23:02:28 +00:00
|
|
|
SystemTrustFilename = "/etc/ca-certificates/trust-source/anchors/%s.crt"
|
2018-08-19 22:36:14 +00:00
|
|
|
SystemTrustCommand = []string{"trust", "extract-compat"}
|
2019-06-01 13:05:08 +00:00
|
|
|
} else if pathExists("/usr/share/pki/trust/anchors") {
|
|
|
|
SystemTrustFilename = "/usr/share/pki/trust/anchors/%s.pem"
|
|
|
|
SystemTrustCommand = []string{"update-ca-certificates"}
|
2018-07-04 02:46:39 +00:00
|
|
|
}
|
2019-06-01 13:55:58 +00:00
|
|
|
if SystemTrustCommand != nil && !binaryExists(SystemTrustCommand[0]) {
|
|
|
|
SystemTrustCommand = nil
|
2018-07-04 02:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 23:02:28 +00:00
|
|
|
func (m *mkcert) systemTrustFilename() string {
|
|
|
|
return fmt.Sprintf(SystemTrustFilename, strings.Replace(m.caUniqueName(), " ", "_", -1))
|
|
|
|
}
|
|
|
|
|
2018-07-04 04:06:50 +00:00
|
|
|
func (m *mkcert) installPlatform() bool {
|
2018-07-04 02:46:39 +00:00
|
|
|
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
|
2018-07-04 02:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
|
|
|
|
fatalIfErr(err, "failed to read root certificate")
|
|
|
|
|
2018-08-19 23:02:28 +00:00
|
|
|
cmd := CommandWithSudo("tee", m.systemTrustFilename())
|
2018-07-04 02:46:39 +00:00
|
|
|
cmd.Stdin = bytes.NewReader(cert)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
fatalIfCmdErr(err, "tee", out)
|
|
|
|
|
2018-07-04 04:06:50 +00:00
|
|
|
cmd = CommandWithSudo(SystemTrustCommand...)
|
2018-07-04 02:46:39 +00:00
|
|
|
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 {
|
2018-07-04 02:46:39 +00:00
|
|
|
if SystemTrustCommand == nil {
|
2018-07-04 04:06:50 +00:00
|
|
|
return false
|
2018-07-04 02:46:39 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 23:02:28 +00:00
|
|
|
cmd := CommandWithSudo("rm", "-f", m.systemTrustFilename())
|
2018-07-04 02:46:39 +00:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
fatalIfCmdErr(err, "rm", out)
|
|
|
|
|
2018-08-19 23:02:28 +00:00
|
|
|
// We used to install under non-unique filenames.
|
|
|
|
legacyFilename := fmt.Sprintf(SystemTrustFilename, "mkcert-rootCA")
|
|
|
|
if pathExists(legacyFilename) {
|
|
|
|
cmd := CommandWithSudo("rm", "-f", legacyFilename)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
fatalIfCmdErr(err, "rm (legacy filename)", out)
|
|
|
|
}
|
|
|
|
|
2018-07-04 04:06:50 +00:00
|
|
|
cmd = CommandWithSudo(SystemTrustCommand...)
|
2018-07-04 02:46:39 +00:00
|
|
|
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 {
|
2019-06-01 13:55:58 +00:00
|
|
|
if !binaryExists("sudo") {
|
2018-07-04 04:06:50 +00:00
|
|
|
return exec.Command(cmd[0], cmd[1:]...)
|
|
|
|
}
|
|
|
|
return exec.Command("sudo", append([]string{"--"}, cmd...)...)
|
2018-07-04 02:46:39 +00:00
|
|
|
}
|