123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- // Copyright 2018 The mkcert Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package main
- import (
- "log"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strings"
- )
- var (
- hasNSS bool
- hasCertutil bool
- certutilPath string
- nssDB = filepath.Join(os.Getenv("HOME"), ".pki/nssdb")
- )
- func init() {
- for _, path := range []string{
- "/usr/bin/firefox", nssDB, "/Applications/Firefox.app",
- "/Applications/Firefox Developer Edition.app",
- "/Applications/Firefox Nightly.app",
- "C:\\Program Files\\Mozilla Firefox",
- } {
- hasNSS = hasNSS || pathExists(path)
- }
- switch runtime.GOOS {
- case "darwin":
- if hasCertutil = binaryExists("certutil"); hasCertutil {
- certutilPath, _ = exec.LookPath("certutil")
- } else {
- out, err := exec.Command("brew", "--prefix", "nss").Output()
- if err == nil {
- certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
- hasCertutil = pathExists(certutilPath)
- }
- }
- case "linux":
- if hasCertutil = binaryExists("certutil"); hasCertutil {
- certutilPath, _ = exec.LookPath("certutil")
- }
- }
- }
- func (m *mkcert) checkNSS() bool {
- if !hasCertutil {
- return false
- }
- success := true
- if m.forEachNSSProfile(func(profile string) {
- err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
- if err != nil {
- success = false
- }
- }) == 0 {
- success = false
- }
- return success
- }
- func (m *mkcert) installNSS() bool {
- if m.forEachNSSProfile(func(profile string) {
- cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", m.caUniqueName(), "-i", filepath.Join(m.CAROOT, rootName))
- out, err := cmd.CombinedOutput()
- fatalIfCmdErr(err, "certutil -A", out)
- }) == 0 {
- log.Printf("ERROR: no %s security databases found", NSSBrowsers)
- return false
- }
- if !m.checkNSS() {
- log.Printf("Installing in %s failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎", NSSBrowsers)
- log.Printf("Note that if you never started %s, you need to do that at least once.", NSSBrowsers)
- return false
- }
- return true
- }
- func (m *mkcert) uninstallNSS() {
- m.forEachNSSProfile(func(profile string) {
- err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
- if err != nil {
- return
- }
- cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", m.caUniqueName())
- out, err := cmd.CombinedOutput()
- fatalIfCmdErr(err, "certutil -D", out)
- })
- }
- func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
- profiles, _ := filepath.Glob(FirefoxProfile)
- if pathExists(nssDB) {
- profiles = append(profiles, nssDB)
- }
- if len(profiles) == 0 {
- return
- }
- for _, profile := range profiles {
- if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
- continue
- }
- if pathExists(filepath.Join(profile, "cert9.db")) {
- f("sql:" + profile)
- found++
- } else if pathExists(filepath.Join(profile, "cert8.db")) {
- f("dbm:" + profile)
- found++
- }
- }
- return
- }
|