truststore_firefox.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. )
  9. var (
  10. hasFirefox bool
  11. hasCertutil bool
  12. certutilPath string
  13. )
  14. func init() {
  15. _, err := os.Stat(FirefoxPath)
  16. hasFirefox = !os.IsNotExist(err)
  17. out, err := exec.Command("brew", "--prefix", "nss").Output()
  18. if err != nil {
  19. return
  20. }
  21. certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
  22. _, err = os.Stat(certutilPath)
  23. hasCertutil = !os.IsNotExist(err)
  24. }
  25. func (m *mkcert) checkFirefox() bool {
  26. if !hasCertutil {
  27. return false
  28. }
  29. success := true
  30. if m.forEachFirefoxProfile(func(profile string) {
  31. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  32. if err != nil {
  33. success = false
  34. }
  35. }) == 0 {
  36. success = false
  37. }
  38. return success
  39. }
  40. func (m *mkcert) installFirefox() {
  41. if m.forEachFirefoxProfile(func(profile string) {
  42. cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", m.caUniqueName(), "-i", filepath.Join(m.CAROOT, rootName))
  43. out, err := cmd.CombinedOutput()
  44. fatalIfCmdErr(err, "certutil -A", out)
  45. }) == 0 {
  46. log.Println("ERROR: no Firefox security databases found")
  47. }
  48. if !m.checkFirefox() {
  49. log.Println("Installing in Firefox failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎")
  50. log.Println("Note that if you never started Firefox, you need to do that at least once.")
  51. }
  52. }
  53. func (m *mkcert) uninstallFirefox() {
  54. m.forEachFirefoxProfile(func(profile string) {
  55. err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
  56. if err != nil {
  57. return
  58. }
  59. cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", m.caUniqueName())
  60. out, err := cmd.CombinedOutput()
  61. fatalIfCmdErr(err, "certutil -D", out)
  62. })
  63. }
  64. func (m *mkcert) forEachFirefoxProfile(f func(profile string)) (found int) {
  65. profiles, _ := filepath.Glob(FirefoxProfile)
  66. if len(profiles) == 0 {
  67. return
  68. }
  69. for _, profile := range profiles {
  70. if _, err := os.Stat(filepath.Join(profile, "cert8.db")); !os.IsNotExist(err) {
  71. f(profile)
  72. found++
  73. }
  74. if _, err := os.Stat(filepath.Join(profile, "cert9.db")); !os.IsNotExist(err) {
  75. f("sql:" + profile)
  76. found++
  77. }
  78. }
  79. return
  80. }