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
|
package cli
import (
"fmt"
"os"
"path/filepath"
"strings"
gokeepasslib "github.com/tobischo/gokeepasslib/v3"
)
// KDBXStore is the minimal interface needed by migrate-kdbx.
type KDBXStore interface {
UpsertTextEntry(groupPath []string, title, password, notes string) (overwrote bool, err error)
UpsertBinaryEntry(groupPath []string, title, filename string, content []byte) (overwrote bool, err error)
Save() error
}
type kdbxStore struct {
path string
db *gokeepasslib.Database
}
// OpenKDBXStore opens an existing KDBX database using password credentials.
func OpenKDBXStore(dbPath, password string) (KDBXStore, error) {
f, err := os.Open(dbPath)
if err != nil {
return nil, fmt.Errorf("opening kdbx %q: %w", dbPath, err)
}
defer f.Close()
db := gokeepasslib.NewDatabase()
db.Credentials = gokeepasslib.NewPasswordCredentials(password)
if err := gokeepasslib.NewDecoder(f).Decode(db); err != nil {
return nil, fmt.Errorf("decoding kdbx %q: %w", dbPath, err)
}
if err := db.UnlockProtectedEntries(); err != nil {
return nil, fmt.Errorf("unlocking kdbx %q: %w", dbPath, err)
}
if db.Content == nil {
db.Content = gokeepasslib.NewContent()
}
if db.Content.Root == nil {
db.Content.Root = gokeepasslib.NewRootData()
}
if len(db.Content.Root.Groups) == 0 {
root := gokeepasslib.NewGroup()
root.Name = "Root"
db.Content.Root.Groups = append(db.Content.Root.Groups, root)
}
return &kdbxStore{
path: dbPath,
db: db,
}, nil
}
func (s *kdbxStore) UpsertTextEntry(groupPath []string, title, password, notes string) (bool, error) {
g := s.ensureGroup(groupPath)
entry, overwrote := upsertEntryByTitle(g, title)
setEntryField(entry, "Title", title)
setEntryField(entry, "Password", password)
setEntryField(entry, "Notes", notes)
return overwrote, nil
}
func (s *kdbxStore) UpsertBinaryEntry(groupPath []string, title, filename string, content []byte) (bool, error) {
g := s.ensureGroup(groupPath)
entry, overwrote := upsertEntryByTitle(g, title)
setEntryField(entry, "Title", title)
setEntryField(entry, "Password", "")
b := s.db.AddBinary(content)
entry.Binaries = []gokeepasslib.BinaryReference{b.CreateReference(filename)}
// Keep notes concise for binary-only entries.
setEntryField(entry, "Notes", fmt.Sprintf("Migrated binary attachment: %s", filename))
return overwrote, nil
}
func (s *kdbxStore) ensureGroup(groupPath []string) *gokeepasslib.Group {
g := &s.db.Content.Root.Groups[0]
for _, segment := range groupPath {
if segment == "" {
continue
}
found := -1
for i := range g.Groups {
if g.Groups[i].Name == segment {
found = i
break
}
}
if found == -1 {
ng := gokeepasslib.NewGroup()
ng.Name = segment
g.Groups = append(g.Groups, ng)
found = len(g.Groups) - 1
}
g = &g.Groups[found]
}
return g
}
func upsertEntryByTitle(g *gokeepasslib.Group, title string) (*gokeepasslib.Entry, bool) {
for i := range g.Entries {
if g.Entries[i].GetTitle() == title {
return &g.Entries[i], true
}
}
e := gokeepasslib.NewEntry()
g.Entries = append(g.Entries, e)
return &g.Entries[len(g.Entries)-1], false
}
func (s *kdbxStore) Save() error {
if err := s.db.LockProtectedEntries(); err != nil {
return fmt.Errorf("locking kdbx entries: %w", err)
}
tmpPath := s.path + ".tmp"
out, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o600)
if err != nil {
return fmt.Errorf("creating temporary kdbx %q: %w", tmpPath, err)
}
defer out.Close()
if err := gokeepasslib.NewEncoder(out).Encode(s.db); err != nil {
return fmt.Errorf("encoding kdbx to %q: %w", tmpPath, err)
}
if err := out.Close(); err != nil {
return fmt.Errorf("closing temporary kdbx %q: %w", tmpPath, err)
}
if err := os.Rename(tmpPath, s.path); err != nil {
return fmt.Errorf("replacing kdbx %q: %w", s.path, err)
}
return nil
}
func setEntryField(entry *gokeepasslib.Entry, key, value string) {
for i := range entry.Values {
if entry.Values[i].Key == key {
entry.Values[i].Value.Content = value
return
}
}
entry.Values = append(entry.Values, gokeepasslib.ValueData{
Key: key,
Value: gokeepasslib.V{
Content: value,
},
})
}
func splitDescriptionPath(description string) ([]string, string, error) {
safePath, err := sanitizeRelativePath(description)
if err != nil {
return nil, "", err
}
parts := strings.Split(safePath, "/")
if len(parts) == 1 {
return nil, parts[0], nil
}
return parts[:len(parts)-1], parts[len(parts)-1], nil
}
func sanitizeRelativePath(path string) (string, error) {
normalised := strings.ReplaceAll(path, "\\", "/")
normalised = strings.TrimSpace(normalised)
if normalised == "" {
return "", fmt.Errorf("empty entry description")
}
clean := filepath.Clean(normalised)
clean = strings.TrimPrefix(clean, "/")
if clean == "." || clean == "" || clean == ".." || strings.HasPrefix(clean, "../") {
return "", fmt.Errorf("unsafe entry description path %q", path)
}
return clean, nil
}
|