truststore_linux.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. FirefoxPath = "/usr/bin/firefox"
  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. _, err := os.Stat("/etc/pki/ca-trust/source/anchors/")
  24. if !os.IsNotExist(err) {
  25. SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/mkcert-rootCA.pem"
  26. SystemTrustCommand = []string{"update-ca-trust", "extract"}
  27. } else {
  28. _, err = os.Stat("/usr/local/share/ca-certificates/")
  29. if !os.IsNotExist(err) {
  30. SystemTrustFilename = "/usr/local/share/ca-certificates/mkcert-rootCA.crt"
  31. SystemTrustCommand = []string{"update-ca-certificates"}
  32. }
  33. }
  34. if SystemTrustCommand != nil {
  35. _, err := exec.LookPath(SystemTrustCommand[0])
  36. if err != nil {
  37. SystemTrustCommand = nil
  38. }
  39. }
  40. }
  41. func (m *mkcert) installPlatform() bool {
  42. if SystemTrustCommand == nil {
  43. log.Printf("Installing to the system store is not yet supported on this Linux 😣 but %s will still work.", NSSBrowsers)
  44. log.Printf("You can also manually install the root certificate at %q.", filepath.Join(m.CAROOT, rootName))
  45. return false
  46. }
  47. cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
  48. fatalIfErr(err, "failed to read root certificate")
  49. cmd := CommandWithSudo("tee", SystemTrustFilename)
  50. cmd.Stdin = bytes.NewReader(cert)
  51. out, err := cmd.CombinedOutput()
  52. fatalIfCmdErr(err, "tee", out)
  53. cmd = CommandWithSudo(SystemTrustCommand...)
  54. out, err = cmd.CombinedOutput()
  55. fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
  56. return true
  57. }
  58. func (m *mkcert) uninstallPlatform() bool {
  59. if SystemTrustCommand == nil {
  60. return false
  61. }
  62. cmd := CommandWithSudo("rm", SystemTrustFilename)
  63. out, err := cmd.CombinedOutput()
  64. fatalIfCmdErr(err, "rm", out)
  65. cmd = CommandWithSudo(SystemTrustCommand...)
  66. out, err = cmd.CombinedOutput()
  67. fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
  68. return true
  69. }
  70. func CommandWithSudo(cmd ...string) *exec.Cmd {
  71. if _, err := exec.LookPath("sudo"); err != nil {
  72. return exec.Command(cmd[0], cmd[1:]...)
  73. }
  74. return exec.Command("sudo", append([]string{"--"}, cmd...)...)
  75. }