truststore_linux.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  44. func (m *mkcert) systemTrustFilename() string {
  45. return fmt.Sprintf(SystemTrustFilename, strings.Replace(m.caUniqueName(), " ", "_", -1))
  46. }
  47. func (m *mkcert) installPlatform() bool {
  48. if SystemTrustCommand == nil {
  49. log.Printf("Installing to the system store is not yet supported on this Linux 😣 but %s will still work.", NSSBrowsers)
  50. log.Printf("You can also manually install the root certificate at %q.", filepath.Join(m.CAROOT, rootName))
  51. return false
  52. }
  53. cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
  54. fatalIfErr(err, "failed to read root certificate")
  55. cmd := commandWithSudo("tee", m.systemTrustFilename())
  56. cmd.Stdin = bytes.NewReader(cert)
  57. out, err := cmd.CombinedOutput()
  58. fatalIfCmdErr(err, "tee", out)
  59. cmd = commandWithSudo(SystemTrustCommand...)
  60. out, err = cmd.CombinedOutput()
  61. fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
  62. return true
  63. }
  64. func (m *mkcert) uninstallPlatform() bool {
  65. if SystemTrustCommand == nil {
  66. return false
  67. }
  68. cmd := commandWithSudo("rm", "-f", m.systemTrustFilename())
  69. out, err := cmd.CombinedOutput()
  70. fatalIfCmdErr(err, "rm", out)
  71. // We used to install under non-unique filenames.
  72. legacyFilename := fmt.Sprintf(SystemTrustFilename, "mkcert-rootCA")
  73. if pathExists(legacyFilename) {
  74. cmd := commandWithSudo("rm", "-f", legacyFilename)
  75. out, err := cmd.CombinedOutput()
  76. fatalIfCmdErr(err, "rm (legacy filename)", out)
  77. }
  78. cmd = commandWithSudo(SystemTrustCommand...)
  79. out, err = cmd.CombinedOutput()
  80. fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
  81. return true
  82. }