/
keepassxctemplatefuncs.go
184 lines (159 loc) · 4.55 KB
/
keepassxctemplatefuncs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package cmd
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"github.com/coreos/go-semver/semver"
"github.com/twpayne/chezmoi/v2/pkg/chezmoi"
"github.com/twpayne/chezmoi/v2/pkg/chezmoilog"
)
type keepassxcAttributeCacheKey struct {
entry string
attribute string
}
type keepassxcConfig struct {
Command string
Database chezmoi.AbsPath
Args []string
version *semver.Version
cache map[string]map[string]string
attachmentCache map[string]map[string]string
attributeCache map[keepassxcAttributeCacheKey]string
password string
}
var (
keepassxcPairRx = regexp.MustCompile(`^([A-Z]\w*):\s*(.*)$`)
keepassxcNeedShowProtectedArgVersion = semver.Version{Major: 2, Minor: 5, Patch: 1}
)
func (c *Config) keepassxcAttachmentTemplateFunc(entry, name string) string {
if data, ok := c.Keepassxc.attachmentCache[entry][name]; ok {
return data
}
args := []string{"attachment-export", "--stdout", "--quiet"}
args = append(args, c.Keepassxc.Args...)
args = append(args, c.Keepassxc.Database.String(), entry, name)
output, err := c.keepassxcOutput(c.Keepassxc.Command, args)
if err != nil {
panic(err)
}
return string(output)
}
func (c *Config) keepassxcTemplateFunc(entry string) map[string]string {
if data, ok := c.Keepassxc.cache[entry]; ok {
return data
}
version, err := c.keepassxcVersion()
if err != nil {
panic(err)
}
args := []string{"show", "--quiet"}
if version.Compare(keepassxcNeedShowProtectedArgVersion) >= 0 {
args = append(args, "--show-protected")
}
args = append(args, c.Keepassxc.Args...)
args = append(args, c.Keepassxc.Database.String(), entry)
output, err := c.keepassxcOutput(c.Keepassxc.Command, args)
if err != nil {
panic(err)
}
data, err := keepassxcParseOutput(output)
if err != nil {
panic(newParseCmdOutputError(c.Keepassxc.Command, args, output, err))
}
if c.Keepassxc.cache == nil {
c.Keepassxc.cache = make(map[string]map[string]string)
}
c.Keepassxc.cache[entry] = data
return data
}
func (c *Config) keepassxcAttributeTemplateFunc(entry, attribute string) string {
key := keepassxcAttributeCacheKey{
entry: entry,
attribute: attribute,
}
if data, ok := c.Keepassxc.attributeCache[key]; ok {
return data
}
args := []string{"show", "--attributes", attribute, "--quiet"}
version, err := c.keepassxcVersion()
if err != nil {
panic(err)
}
if version.Compare(keepassxcNeedShowProtectedArgVersion) >= 0 {
args = append(args, "--show-protected")
}
args = append(args, c.Keepassxc.Args...)
args = append(args, c.Keepassxc.Database.String(), entry)
output, err := c.keepassxcOutput(c.Keepassxc.Command, args)
if err != nil {
panic(err)
}
outputStr := string(bytes.TrimSpace(output))
if c.Keepassxc.attributeCache == nil {
c.Keepassxc.attributeCache = make(map[keepassxcAttributeCacheKey]string)
}
c.Keepassxc.attributeCache[key] = outputStr
return outputStr
}
func (c *Config) keepassxcOutput(name string, args []string) ([]byte, error) {
if c.Keepassxc.Database.Empty() {
panic(errors.New("keepassxc.database not set"))
}
if c.Keepassxc.password == "" {
password, err := c.readPassword(fmt.Sprintf("Insert password to unlock %s: ", c.Keepassxc.Database))
if err != nil {
return nil, err
}
c.Keepassxc.password = password
}
cmd := exec.Command(name, args...)
cmd.Stdin = bytes.NewBufferString(c.Keepassxc.password + "\n")
cmd.Stderr = os.Stderr
output, err := chezmoilog.LogCmdOutput(cmd)
if err != nil {
return nil, newCmdOutputError(cmd, output, err)
}
return output, nil
}
func keepassxcParseOutput(output []byte) (map[string]string, error) {
data := make(map[string]string)
s := bufio.NewScanner(bytes.NewReader(output))
var key string
for i := 0; s.Scan(); i++ {
switch match := keepassxcPairRx.FindStringSubmatch(s.Text()); {
case match != nil:
key = match[1]
data[key] = match[2]
case match == nil && key != "":
data[key] += "\n" + s.Text()
}
}
if err := s.Err(); err != nil {
return nil, err
}
return data, nil
}
func (c *Config) keepassxcVersion() (*semver.Version, error) {
if c.Keepassxc.version != nil {
return c.Keepassxc.version, nil
}
name := c.Keepassxc.Command
args := []string{"--version"}
cmd := exec.Command(name, args...)
output, err := chezmoilog.LogCmdOutput(cmd)
if err != nil {
return nil, newCmdOutputError(cmd, output, err)
}
c.Keepassxc.version, err = semver.NewVersion(string(bytes.TrimSpace(output)))
if err != nil {
return nil, &parseVersionError{
output: output,
err: err,
}
}
return c.Keepassxc.version, nil
}