truststore_nss.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. )
  10. var (
  11. hasNSS bool
  12. hasCertutil bool
  13. certutilPath string
  14. nssDB = filepath.Join(os.Getenv("HOME"), ".pki/nssdb")
  15. )
  16. func init() {
  17. _, err := os.Stat(FirefoxPath)
  18. hasNSS = !os.IsNotExist(err)
  19. switch runtime.GOOS {
  20. case "darwin":
  21. out, err := exec.Command("brew", "--prefix", "nss").Output()
  22. if err != nil {
  23. return
  24. }
  25. certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
  26. _, err = os.Stat(certutilPath)
  27. hasCertutil = !os.IsNotExist(err)
  28. case "linux":
  29. _, err := os.Stat(nssDB)
  30. hasNSS = hasNSS && !os.IsNotExist(err)
  31. certutilPath, err = exec.LookPath("certutil")
  32. hasCertutil = err == nil
  33. }
  34. }
  35. func (m *mkcert) checkNSS() bool {
  36. if !hasCertutil {
  37. return false
  38. }
  39. success := true
  40. if m.forEachNSSProfile(func(profile string) {
  41. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  42. if err != nil {
  43. success = false
  44. }
  45. }) == 0 {
  46. success = false
  47. }
  48. return success
  49. }
  50. func (m *mkcert) installNSS() bool {
  51. if m.forEachNSSProfile(func(profile string) {
  52. cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", m.caUniqueName(), "-i", filepath.Join(m.CAROOT, rootName))
  53. out, err := cmd.CombinedOutput()
  54. fatalIfCmdErr(err, "certutil -A", out)
  55. }) == 0 {
  56. log.Printf("ERROR: no %s security databases found", NSSBrowsers)
  57. return false
  58. }
  59. if !m.checkNSS() {
  60. log.Printf("Installing in %s failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎", NSSBrowsers)
  61. log.Printf("Note that if you never started %s, you need to do that at least once.", NSSBrowsers)
  62. return false
  63. }
  64. return true
  65. }
  66. func (m *mkcert) uninstallNSS() {
  67. m.forEachNSSProfile(func(profile string) {
  68. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  69. if err != nil {
  70. return
  71. }
  72. cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", m.caUniqueName())
  73. out, err := cmd.CombinedOutput()
  74. fatalIfCmdErr(err, "certutil -D", out)
  75. })
  76. }
  77. func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
  78. profiles, _ := filepath.Glob(FirefoxProfile)
  79. if _, err := os.Stat(nssDB); !os.IsNotExist(err) {
  80. profiles = append(profiles, nssDB)
  81. }
  82. if len(profiles) == 0 {
  83. return
  84. }
  85. for _, profile := range profiles {
  86. if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
  87. continue
  88. }
  89. if _, err := os.Stat(filepath.Join(profile, "cert9.db")); !os.IsNotExist(err) {
  90. f("sql:" + profile)
  91. found++
  92. continue
  93. }
  94. if _, err := os.Stat(filepath.Join(profile, "cert8.db")); !os.IsNotExist(err) {
  95. f("dbm:" + profile)
  96. found++
  97. }
  98. }
  99. return
  100. }