truststore_linux.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2018 The mkcert 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. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "os/exec"
  12. "path/filepath"
  13. "strings"
  14. )
  15. var (
  16. FirefoxProfile = os.Getenv("HOME") + "/.mozilla/firefox/*"
  17. NSSBrowsers = "Firefox and/or Chrome/Chromium"
  18. SystemTrustFilename string
  19. SystemTrustCommand []string
  20. CertutilInstallHelp string
  21. )
  22. func init() {
  23. switch {
  24. case binaryExists("apt"):
  25. CertutilInstallHelp = "apt install libnss3-tools"
  26. case binaryExists("yum"):
  27. CertutilInstallHelp = "yum install nss-tools"
  28. case binaryExists("zypper"):
  29. CertutilInstallHelp = "zypper install mozilla-nss-tools"
  30. }
  31. if pathExists("/etc/pki/ca-trust/source/anchors/") {
  32. SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/%s.pem"
  33. SystemTrustCommand = []string{"update-ca-trust", "extract"}
  34. } else if pathExists("/usr/local/share/ca-certificates/") {
  35. SystemTrustFilename = "/usr/local/share/ca-certificates/%s.crt"
  36. SystemTrustCommand = []string{"update-ca-certificates"}
  37. } else if pathExists("/etc/ca-certificates/trust-source/anchors/") {
  38. SystemTrustFilename = "/etc/ca-certificates/trust-source/anchors/%s.crt"
  39. SystemTrustCommand = []string{"trust", "extract-compat"}
  40. } else if pathExists("/usr/share/pki/trust/anchors") {
  41. SystemTrustFilename = "/usr/share/pki/trust/anchors/%s.pem"
  42. SystemTrustCommand = []string{"update-ca-certificates"}
  43. }
  44. if SystemTrustCommand != nil && !binaryExists(SystemTrustCommand[0]) {
  45. SystemTrustCommand = nil
  46. }
  47. }
  48. func (m *mkcert) systemTrustFilename() string {
  49. return fmt.Sprintf(SystemTrustFilename, strings.Replace(m.caUniqueName(), " ", "_", -1))
  50. }
  51. func (m *mkcert) installPlatform() bool {
  52. if SystemTrustCommand == nil {
  53. log.Printf("Installing to the system store is not yet supported on this Linux 😣 but %s will still work.", NSSBrowsers)
  54. log.Printf("You can also manually install the root certificate at %q.", filepath.Join(m.CAROOT, rootName))
  55. return false
  56. }
  57. cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
  58. fatalIfErr(err, "failed to read root certificate")
  59. cmd := CommandWithSudo("tee", m.systemTrustFilename())
  60. cmd.Stdin = bytes.NewReader(cert)
  61. out, err := cmd.CombinedOutput()
  62. fatalIfCmdErr(err, "tee", out)
  63. cmd = CommandWithSudo(SystemTrustCommand...)
  64. out, err = cmd.CombinedOutput()
  65. fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
  66. return true
  67. }
  68. func (m *mkcert) uninstallPlatform() bool {
  69. if SystemTrustCommand == nil {
  70. return false
  71. }
  72. cmd := CommandWithSudo("rm", "-f", m.systemTrustFilename())
  73. out, err := cmd.CombinedOutput()
  74. fatalIfCmdErr(err, "rm", out)
  75. // We used to install under non-unique filenames.
  76. legacyFilename := fmt.Sprintf(SystemTrustFilename, "mkcert-rootCA")
  77. if pathExists(legacyFilename) {
  78. cmd := CommandWithSudo("rm", "-f", legacyFilename)
  79. out, err := cmd.CombinedOutput()
  80. fatalIfCmdErr(err, "rm (legacy filename)", out)
  81. }
  82. cmd = CommandWithSudo(SystemTrustCommand...)
  83. out, err = cmd.CombinedOutput()
  84. fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
  85. return true
  86. }
  87. func CommandWithSudo(cmd ...string) *exec.Cmd {
  88. if !binaryExists("sudo") {
  89. return exec.Command(cmd[0], cmd[1:]...)
  90. }
  91. return exec.Command("sudo", append([]string{"--"}, cmd...)...)
  92. }