| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package internal |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"os/exec" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"strings" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
hasNSS bool |
| 19 |
hasCertutil bool |
| 20 |
certutilPath string |
| 21 |
nssDBs = []string{ |
| 22 |
filepath.Join(os.Getenv("HOME"), ".pki/nssdb"), |
| 23 |
filepath.Join(os.Getenv("HOME"), "snap/chromium/current/.pki/nssdb"), |
| 24 |
"/etc/pki/nssdb", |
| 25 |
} |
| 26 |
firefoxPaths = []string{ |
| 27 |
"/usr/bin/firefox", |
| 28 |
"/usr/bin/firefox-nightly", |
| 29 |
"/usr/bin/firefox-developer-edition", |
| 30 |
"/snap/firefox", |
| 31 |
"/Applications/Firefox.app", |
| 32 |
"/Applications/FirefoxDeveloperEdition.app", |
| 33 |
"/Applications/Firefox Developer Edition.app", |
| 34 |
"/Applications/Firefox Nightly.app", |
| 35 |
"C:\\Program Files\\Mozilla Firefox", |
| 36 |
} |
| 37 |
) |
| 38 |
|
| 39 |
func init() { |
| 40 |
allPaths := append(append([]string{}, nssDBs...), firefoxPaths...) |
| 41 |
for _, path := range allPaths { |
| 42 |
if pathExists(path) { |
| 43 |
hasNSS = true |
| 44 |
break |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
switch runtime.GOOS { |
| 49 |
case "darwin": |
| 50 |
switch { |
| 51 |
case binaryExists("certutil"): |
| 52 |
certutilPath, _ = exec.LookPath("certutil") |
| 53 |
hasCertutil = true |
| 54 |
case binaryExists("/usr/local/opt/nss/bin/certutil"): |
| 55 |
|
| 56 |
certutilPath = "/usr/local/opt/nss/bin/certutil" |
| 57 |
hasCertutil = true |
| 58 |
default: |
| 59 |
out, err := exec.Command("brew", "--prefix", "nss").Output() |
| 60 |
if err == nil { |
| 61 |
certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil") |
| 62 |
hasCertutil = pathExists(certutilPath) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
case "linux": |
| 67 |
if hasCertutil = binaryExists("certutil"); hasCertutil { |
| 68 |
certutilPath, _ = exec.LookPath("certutil") |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func (m *mkcert) checkNSS() bool { |
| 74 |
if !hasCertutil { |
| 75 |
return false |
| 76 |
} |
| 77 |
success := true |
| 78 |
if m.forEachNSSProfile(func(profile string) { |
| 79 |
err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run() |
| 80 |
if err != nil { |
| 81 |
success = false |
| 82 |
} |
| 83 |
}) == 0 { |
| 84 |
success = false |
| 85 |
} |
| 86 |
return success |
| 87 |
} |
| 88 |
|
| 89 |
func (m *mkcert) installNSS() bool { |
| 90 |
if m.forEachNSSProfile(func(profile string) { |
| 91 |
cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", m.caUniqueName(), "-i", filepath.Join(m.CAROOT, rootName)) |
| 92 |
out, err := execCertutil(cmd) |
| 93 |
fatalIfCmdErr(err, "certutil -A -d "+profile, out) |
| 94 |
}) == 0 { |
| 95 |
log.Printf("ERROR: no %s security databases found", NSSBrowsers) |
| 96 |
return false |
| 97 |
} |
| 98 |
if !m.checkNSS() { |
| 99 |
log.Printf("Installing in %s failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎", NSSBrowsers) |
| 100 |
log.Printf("Note that if you never started %s, you need to do that at least once.", NSSBrowsers) |
| 101 |
return false |
| 102 |
} |
| 103 |
return true |
| 104 |
} |
| 105 |
|
| 106 |
func (m *mkcert) uninstallNSS() { |
| 107 |
m.forEachNSSProfile(func(profile string) { |
| 108 |
err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run() |
| 109 |
if err != nil { |
| 110 |
return |
| 111 |
} |
| 112 |
cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", m.caUniqueName()) |
| 113 |
out, err := execCertutil(cmd) |
| 114 |
fatalIfCmdErr(err, "certutil -D -d "+profile, out) |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
func execCertutil(cmd *exec.Cmd) ([]byte, error) { |
| 121 |
out, err := cmd.CombinedOutput() |
| 122 |
if err != nil && bytes.Contains(out, []byte("SEC_ERROR_READ_ONLY")) && runtime.GOOS != "windows" { |
| 123 |
origArgs := cmd.Args[1:] |
| 124 |
cmd = commandWithSudo(cmd.Path) |
| 125 |
cmd.Args = append(cmd.Args, origArgs...) |
| 126 |
out, err = cmd.CombinedOutput() |
| 127 |
} |
| 128 |
return out, err |
| 129 |
} |
| 130 |
|
| 131 |
func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) { |
| 132 |
var profiles []string |
| 133 |
profiles = append(profiles, nssDBs...) |
| 134 |
for _, ff := range FirefoxProfiles { |
| 135 |
pp, _ := filepath.Glob(ff) |
| 136 |
profiles = append(profiles, pp...) |
| 137 |
} |
| 138 |
for _, profile := range profiles { |
| 139 |
if stat, err := os.Stat(profile); err != nil || !stat.IsDir() { |
| 140 |
continue |
| 141 |
} |
| 142 |
if pathExists(filepath.Join(profile, "cert9.db")) { |
| 143 |
f("sql:" + profile) |
| 144 |
found++ |
| 145 |
} else if pathExists(filepath.Join(profile, "cert8.db")) { |
| 146 |
f("dbm:" + profile) |
| 147 |
found++ |
| 148 |
} |
| 149 |
} |
| 150 |
return |
| 151 |
} |