truststore_linux.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "bytes"
  7. "io/ioutil"
  8. "log"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "strings"
  13. )
  14. var (
  15. FirefoxProfile = os.Getenv("HOME") + "/.mozilla/firefox/*"
  16. CertutilInstallHelp = `apt install libnss3-tools" or "yum install nss-tools`
  17. NSSBrowsers = "Firefox and/or Chrome/Chromium"
  18. SystemTrustFilename string
  19. SystemTrustCommand []string
  20. )
  21. func init() {
  22. _, err := os.Stat("/etc/pki/ca-trust/source/anchors/")
  23. if !os.IsNotExist(err) {
  24. SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/mkcert-rootCA.pem"
  25. SystemTrustCommand = []string{"update-ca-trust", "extract"}
  26. } else {
  27. _, err = os.Stat("/usr/local/share/ca-certificates/")
  28. if !os.IsNotExist(err) {
  29. SystemTrustFilename = "/usr/local/share/ca-certificates/mkcert-rootCA.crt"
  30. SystemTrustCommand = []string{"update-ca-certificates"}
  31. }
  32. }
  33. if SystemTrustCommand != nil {
  34. _, err := exec.LookPath(SystemTrustCommand[0])
  35. if err != nil {
  36. SystemTrustCommand = nil
  37. }
  38. }
  39. }
  40. func (m *mkcert) installPlatform() bool {
  41. if SystemTrustCommand == nil {
  42. log.Printf("Installing to the system store is not yet supported on this Linux 😣 but %s will still work.", NSSBrowsers)
  43. log.Printf("You can also manually install the root certificate at %q.", filepath.Join(m.CAROOT, rootName))
  44. return false
  45. }
  46. cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
  47. fatalIfErr(err, "failed to read root certificate")
  48. cmd := CommandWithSudo("tee", SystemTrustFilename)
  49. cmd.Stdin = bytes.NewReader(cert)
  50. out, err := cmd.CombinedOutput()
  51. fatalIfCmdErr(err, "tee", out)
  52. cmd = CommandWithSudo(SystemTrustCommand...)
  53. out, err = cmd.CombinedOutput()
  54. fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
  55. return true
  56. }
  57. func (m *mkcert) uninstallPlatform() bool {
  58. if SystemTrustCommand == nil {
  59. return false
  60. }
  61. cmd := CommandWithSudo("rm", SystemTrustFilename)
  62. out, err := cmd.CombinedOutput()
  63. fatalIfCmdErr(err, "rm", out)
  64. cmd = CommandWithSudo(SystemTrustCommand...)
  65. out, err = cmd.CombinedOutput()
  66. fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
  67. return true
  68. }
  69. func CommandWithSudo(cmd ...string) *exec.Cmd {
  70. if _, err := exec.LookPath("sudo"); err != nil {
  71. return exec.Command(cmd[0], cmd[1:]...)
  72. }
  73. return exec.Command("sudo", append([]string{"--"}, cmd...)...)
  74. }