truststore_linux.go 3.1 KB

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