Debian Codegraph @

internal/truststore_linux.go

raw symbols SCIP
1 // Copyright 2018 The mkcert 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
5 package internal
6
7 import (
8 "bytes"
9 "fmt"
10 "io/ioutil"
11 "log"
12 "os"
13 "path/filepath"
14 "strings"
15 )
16
17 var (
18 FirefoxProfiles = []string{os.Getenv("HOME") + "/.mozilla/firefox/*",
19 os.Getenv("HOME") + "/snap/firefox/common/.mozilla/firefox/*"}
20 NSSBrowsers = "Firefox and/or Chrome/Chromium"
21
22 SystemTrustFilename string
23 SystemTrustCommand []string
24 CertutilInstallHelp string
25 )
26
27 func init() {
28 switch {
29 case binaryExists("apt"):
30 CertutilInstallHelp = "apt install libnss3-tools"
31 case binaryExists("yum"):
32 CertutilInstallHelp = "yum install nss-tools"
33 case binaryExists("zypper"):
34 CertutilInstallHelp = "zypper install mozilla-nss-tools"
35 }
36 if pathExists("/etc/pki/ca-trust/source/anchors/") {
37 SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/%s.pem"
38 SystemTrustCommand = []string{"update-ca-trust", "extract"}
39 } else if pathExists("/usr/local/share/ca-certificates/") {
40 SystemTrustFilename = "/usr/local/share/ca-certificates/%s.crt"
41 SystemTrustCommand = []string{"update-ca-certificates"}
42 } else if pathExists("/etc/ca-certificates/trust-source/anchors/") {
43 SystemTrustFilename = "/etc/ca-certificates/trust-source/anchors/%s.crt"
44 SystemTrustCommand = []string{"trust", "extract-compat"}
45 } else if pathExists("/usr/share/pki/trust/anchors") {
46 SystemTrustFilename = "/usr/share/pki/trust/anchors/%s.pem"
47 SystemTrustCommand = []string{"update-ca-certificates"}
48 }
49 }
50
51 func (m *mkcert) systemTrustFilename() string {
52 return fmt.Sprintf(SystemTrustFilename, strings.Replace(m.caUniqueName(), " ", "_", -1))
53 }
54
55 func (m *mkcert) installPlatform() bool {
56 if SystemTrustCommand == nil {
57 log.Printf("Installing to the system store is not yet supported on this Linux 😣 but %s will still work.", NSSBrowsers)
58 log.Printf("You can also manually install the root certificate at %q.", filepath.Join(m.CAROOT, rootName))
59 return false
60 }
61
62 cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
63 fatalIfErr(err, "failed to read root certificate")
64
65 cmd := commandWithSudo("tee", m.systemTrustFilename())
66 cmd.Stdin = bytes.NewReader(cert)
67 out, err := cmd.CombinedOutput()
68 fatalIfCmdErr(err, "tee", out)
69
70 cmd = commandWithSudo(SystemTrustCommand...)
71 out, err = cmd.CombinedOutput()
72 fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
73
74 return true
75 }
76
77 func (m *mkcert) uninstallPlatform() bool {
78 if SystemTrustCommand == nil {
79 return false
80 }
81
82 cmd := commandWithSudo("rm", "-f", m.systemTrustFilename())
83 out, err := cmd.CombinedOutput()
84 fatalIfCmdErr(err, "rm", out)
85
86 // We used to install under non-unique filenames.
87 legacyFilename := fmt.Sprintf(SystemTrustFilename, "mkcert-rootCA")
88 if pathExists(legacyFilename) {
89 cmd := commandWithSudo("rm", "-f", legacyFilename)
90 out, err := cmd.CombinedOutput()
91 fatalIfCmdErr(err, "rm (legacy filename)", out)
92 }
93
94 cmd = commandWithSudo(SystemTrustCommand...)
95 out, err = cmd.CombinedOutput()
96 fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)
97
98 return true
99 }