truststore_linux.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "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. CertutilInstallHelp = `apt install libnss3-tools" or "yum install nss-tools`
  18. NSSBrowsers = "Firefox and/or Chrome/Chromium"
  19. SystemTrustFilename string
  20. SystemTrustCommand []string
  21. )
  22. func init() {
  23. if pathExists("/etc/pki/ca-trust/source/anchors/") {
  24. SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/%s.pem"
  25. SystemTrustCommand = []string{"update-ca-trust", "extract"}
  26. } else if pathExists("/usr/local/share/ca-certificates/") {
  27. SystemTrustFilename = "/usr/local/share/ca-certificates/%s.crt"
  28. SystemTrustCommand = []string{"update-ca-certificates"}
  29. } else if pathExists("/etc/ca-certificates/trust-source/anchors/") {
  30. SystemTrustFilename = "/etc/ca-certificates/trust-source/anchors/%s.crt"
  31. SystemTrustCommand = []string{"trust", "extract-compat"}
  32. }
  33. if SystemTrustCommand != nil {
  34. _, err := exec.LookPath(SystemTrustCommand[0])
  35. if err != nil {
  36. SystemTrustCommand = nil
  37. }
  38. }
  39. }
  40. func pathExists(path string) bool {
  41. _, err := os.Stat(path)
  42. return err == nil
  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. }
  83. func CommandWithSudo(cmd ...string) *exec.Cmd {
  84. if _, err := exec.LookPath("sudo"); err != nil {
  85. return exec.Command(cmd[0], cmd[1:]...)
  86. }
  87. return exec.Command("sudo", append([]string{"--"}, cmd...)...)
  88. }