/
actions.go
304 lines (291 loc) · 7 KB
/
actions.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Package backend handles kdbx interactions
package backend
import (
"errors"
"os"
"strings"
"time"
"github.com/enckse/lockbox/internal/config"
"github.com/enckse/lockbox/internal/platform"
"github.com/tobischo/gokeepasslib/v3"
)
type (
// ActionMode represents activities performed via transactions
ActionMode string
action func(t Context) error
)
const (
// MoveAction represents changes via moves, like the Move command
MoveAction ActionMode = "mv"
// InsertAction represents changes via inserts, like the Insert command
InsertAction ActionMode = "insert"
// RemoveAction represents changes via deletions, like Remove or globbed remove commands
RemoveAction ActionMode = "rm"
)
func (t *Transaction) act(cb action) error {
if !t.valid {
return errors.New("invalid transaction")
}
key, err := config.NewKey(config.DefaultKeyMode)
if err != nil {
return err
}
k, err := key.Read(platform.ReadInteractivePassword)
if err != nil {
return err
}
file := config.EnvKeyFile.Get()
if !t.exists {
if err := create(t.file, k, file); err != nil {
return err
}
}
f, err := os.Open(t.file)
if err != nil {
return err
}
defer f.Close()
db := gokeepasslib.NewDatabase()
creds, err := getCredentials(k, file)
if err != nil {
return err
}
db.Credentials = creds
if err := gokeepasslib.NewDecoder(f).Decode(db); err != nil {
return err
}
if len(db.Content.Root.Groups) != 1 {
return errors.New("kdbx must have ONE root group")
}
err = cb(Context{db: db})
if err != nil {
return err
}
if t.write {
if err := db.LockProtectedEntries(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
f, err = os.Create(t.file)
if err != nil {
return err
}
defer f.Close()
return encode(f, db)
}
return err
}
func (t *Transaction) change(cb action) error {
if t.readonly {
return errors.New("unable to alter database in readonly mode")
}
return t.act(func(c Context) error {
if err := c.db.UnlockProtectedEntries(); err != nil {
return err
}
t.write = true
return cb(c)
})
}
func (c Context) alterEntities(isAdd bool, offset []string, title string, entity *gokeepasslib.Entry) bool {
g, e, ok := findAndDo(isAdd, title, offset, entity, c.db.Content.Root.Groups[0].Groups, c.db.Content.Root.Groups[0].Entries)
c.db.Content.Root.Groups[0].Groups = g
c.db.Content.Root.Groups[0].Entries = e
return ok
}
func (c Context) removeEntity(offset []string, title string) bool {
return c.alterEntities(false, offset, title, nil)
}
func findAndDo(isAdd bool, entityName string, offset []string, opEntity *gokeepasslib.Entry, g []gokeepasslib.Group, e []gokeepasslib.Entry) ([]gokeepasslib.Group, []gokeepasslib.Entry, bool) {
done := false
if len(offset) == 0 {
if isAdd {
e = append(e, *opEntity)
} else {
var entries []gokeepasslib.Entry
for _, entry := range e {
if getPathName(entry) == entityName {
continue
}
entries = append(entries, entry)
}
e = entries
}
done = true
} else {
name := offset[0]
remaining := []string{}
if len(offset) > 1 {
remaining = offset[1:]
}
if isAdd {
match := false
for _, group := range g {
if group.Name == name {
match = true
}
}
if !match {
newGroup := gokeepasslib.NewGroup()
newGroup.Name = name
g = append(g, newGroup)
}
}
var updateGroups []gokeepasslib.Group
for _, group := range g {
if !done && group.Name == name {
groups, entries, ok := findAndDo(isAdd, entityName, remaining, opEntity, group.Groups, group.Entries)
group.Entries = entries
group.Groups = groups
if ok {
done = true
}
}
updateGroups = append(updateGroups, group)
}
g = updateGroups
if !isAdd {
var groups []gokeepasslib.Group
for _, group := range g {
if group.Name == name && len(group.Entries) == 0 && len(group.Groups) == 0 {
continue
}
groups = append(groups, group)
}
g = groups
}
}
return g, e, done
}
// Move will move a src object to a dst location
func (t *Transaction) Move(src QueryEntity, dst string) error {
if strings.TrimSpace(src.Path) == "" {
return errors.New("empty path not allowed")
}
if strings.TrimSpace(src.Value) == "" {
return errors.New("empty secret not allowed")
}
mod := config.EnvModTime.Get()
modTime := time.Now()
if mod != "" {
p, err := time.Parse(config.ModTimeFormat, mod)
if err != nil {
return err
}
modTime = p
}
dOffset, dTitle, err := splitComponents(dst)
if err != nil {
return err
}
sOffset, sTitle, err := splitComponents(src.Path)
if err != nil {
return err
}
action := MoveAction
if dst == src.Path {
action = InsertAction
}
hook, err := NewHook(src.Path, action)
if err != nil {
return err
}
if err := hook.Run(HookPre); err != nil {
return err
}
multi := len(strings.Split(strings.TrimSpace(src.Value), "\n")) > 1
err = t.change(func(c Context) error {
c.removeEntity(sOffset, sTitle)
if action == MoveAction {
c.removeEntity(dOffset, dTitle)
}
e := gokeepasslib.NewEntry()
e.Values = append(e.Values, value(titleKey, dTitle))
field := passKey
if multi {
field = notesKey
}
v := src.Value
ok, err := isTOTP(dTitle)
if err != nil {
return err
}
if ok {
if multi {
return errors.New("totp tokens can NOT be multi-line")
}
otp := config.EnvFormatTOTP.Get(v)
e.Values = append(e.Values, protectedValue("otp", otp))
}
e.Values = append(e.Values, protectedValue(field, v))
e.Values = append(e.Values, value(modTimeKey, modTime.Format(time.RFC3339)))
c.alterEntities(true, dOffset, dTitle, &e)
return nil
})
if err != nil {
return err
}
return hook.Run(HookPost)
}
// Insert is a move to the same location
func (t *Transaction) Insert(path, val string) error {
return t.Move(QueryEntity{Path: path, Value: val}, path)
}
// Remove will remove a single entity
func (t *Transaction) Remove(entity *QueryEntity) error {
if entity == nil {
return errors.New("entity is empty/invalid")
}
return t.RemoveAll([]QueryEntity{*entity})
}
// RemoveAll handles removing elements
func (t *Transaction) RemoveAll(entities []QueryEntity) error {
if len(entities) == 0 {
return errors.New("no entities given")
}
type removal struct {
parts []string
title string
hook Hook
}
removals := []removal{}
hasHooks := false
for _, entity := range entities {
offset, title, err := splitComponents(entity.Path)
if err != nil {
return err
}
hook, err := NewHook(entity.Path, RemoveAction)
if err != nil {
return err
}
if err := hook.Run(HookPre); err != nil {
return err
}
if hook.enabled {
hasHooks = true
}
removals = append(removals, removal{parts: offset, title: title, hook: hook})
}
err := t.change(func(c Context) error {
for _, entity := range removals {
if ok := c.removeEntity(entity.parts, entity.title); !ok {
return errors.New("failed to remove entity")
}
}
return nil
})
if err != nil {
return err
}
if hasHooks {
for _, entity := range removals {
if err := entity.hook.Run(HookPost); err != nil {
return err
}
}
}
return nil
}