summaryrefslogtreecommitdiff
path: root/internal/cli/migrate_kdbx.go
blob: f4bb58c23a589531fa393b051a8537cc17b9b360 (plain)
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
package cli

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"sort"
	"strings"
	"time"

	"codeberg.org/snonux/foostore/internal/store"
)

type migrateKDBXOptions struct {
	DBPath       string
	PassFile     string
	BinaryOutDir string
	DryRun       bool
}

type migrateKDBXStats struct {
	Total           int
	TextMigrated    int
	BinaryMigrated  int
	OverwrittenText int
	OverwrittenBin  int
	Errors          int
}

func (c *CLI) cmdMigrateKDBX(ctx context.Context, argv []string) int {
	opts, err := c.parseMigrateKDBXOptions(argv)
	if err != nil {
		warn(err.Error())
		return 1
	}

	if err := os.MkdirAll(opts.BinaryOutDir, 0o700); err != nil {
		warn(fmt.Sprintf("creating binary output directory %q: %v", opts.BinaryOutDir, err))
		return 1
	}

	password, err := readPasswordFile(opts.PassFile)
	if err != nil {
		warn(err.Error())
		return 1
	}

	var kdbx KDBXStore
	if !opts.DryRun {
		opener := c.openKDBX
		if opener == nil {
			opener = OpenKDBXStore
		}
		kdbx, err = opener(opts.DBPath, password)
		if err != nil {
			warn(err.Error())
			return 1
		}
	}

	var indexes store.IndexSlice
	if err := c.st.WalkIndexes(ctx, "", func(idx *store.Index) error {
		indexes = append(indexes, idx)
		return nil
	}); err != nil {
		warn(fmt.Sprintf("listing store entries: %v", err))
		return 1
	}
	sort.Sort(indexes)

	stats := migrateKDBXStats{}
	for _, idx := range indexes {
		stats.Total++
		if err := c.migrateOneEntry(ctx, idx, opts, kdbx, &stats); err != nil {
			stats.Errors++
			warn(err.Error())
		}
	}

	if !opts.DryRun {
		if err := kdbx.Save(); err != nil {
			warn(err.Error())
			return 1
		}
	}

	logMsg(fmt.Sprintf(
		"migrate-kdbx done: total=%d text_migrated=%d binary_migrated=%d overwritten_text=%d overwritten_binary=%d errors=%d db=%s binary_out=%s dry_run=%t",
		stats.Total,
		stats.TextMigrated,
		stats.BinaryMigrated,
		stats.OverwrittenText,
		stats.OverwrittenBin,
		stats.Errors,
		opts.DBPath,
		opts.BinaryOutDir,
		opts.DryRun,
	))

	if stats.Errors > 0 {
		return 1
	}
	return 0
}

func (c *CLI) migrateOneEntry(ctx context.Context, idx *store.Index, opts migrateKDBXOptions, kdbx KDBXStore, stats *migrateKDBXStats) error {
	safePath, err := sanitizeRelativePath(idx.Description)
	if err != nil {
		return fmt.Errorf("entry %q: %w", idx.Description, err)
	}

	d, err := c.st.LoadData(ctx, idx)
	if err != nil {
		return fmt.Errorf("loading data for %q: %w", idx.Description, err)
	}

	if idx.IsBinary() {
		groupPath, title, err := splitDescriptionPath(safePath)
		if err != nil {
			return fmt.Errorf("mapping binary entry %q: %w", idx.Description, err)
		}
		if opts.DryRun {
			logMsg(fmt.Sprintf("DRY-RUN binary migrate: %s -> attachment=%s", idx.Description, title))
			stats.BinaryMigrated++
			return nil
		}
		overwrote, err := kdbx.UpsertBinaryEntry(groupPath, title, title, d.Content)
		if err != nil {
			return fmt.Errorf("upserting binary entry %q: %w", idx.Description, err)
		}
		if overwrote {
			stats.OverwrittenBin++
		}
		stats.BinaryMigrated++
		return nil
	}

	groupPath, title, err := splitDescriptionPath(safePath)
	if err != nil {
		return fmt.Errorf("mapping text entry %q: %w", idx.Description, err)
	}
	if opts.DryRun {
		logMsg(fmt.Sprintf("DRY-RUN text migrate: %s -> group=%q title=%q", idx.Description, strings.Join(groupPath, "/"), title))
		stats.TextMigrated++
		return nil
	}

	entryPassword, entryNotes := extractPasswordFromContent(string(d.Content))
	overwrote, err := kdbx.UpsertTextEntry(groupPath, title, entryPassword, entryNotes)
	if err != nil {
		return fmt.Errorf("upserting text entry %q: %w", idx.Description, err)
	}
	if overwrote {
		stats.OverwrittenText++
	}
	stats.TextMigrated++
	return nil
}

func (c *CLI) parseMigrateKDBXOptions(argv []string) (migrateKDBXOptions, error) {
	now := c.now
	if now == nil {
		now = func() time.Time { return time.Now() }
	}
	home := resolveHomeDir()
	exportDir := filepath.Join(home, ".foostore-export")
	if c.cfg != nil && c.cfg.ExportDir != "" {
		exportDir = c.cfg.ExportDir
	}

	opts := migrateKDBXOptions{
		DBPath:       filepath.Join(home, "Documents", "Keepass", "master"),
		PassFile:     filepath.Join(home, ".master.pass"),
		BinaryOutDir: filepath.Join(exportDir, "keepass-binary-dump", now().Format("20060102-150405")),
	}

	for i := 1; i < len(argv); i++ {
		switch argv[i] {
		case "--db":
			i++
			if i >= len(argv) {
				return opts, fmt.Errorf("--db requires a value")
			}
			opts.DBPath = argv[i]
		case "--pass-file":
			i++
			if i >= len(argv) {
				return opts, fmt.Errorf("--pass-file requires a value")
			}
			opts.PassFile = argv[i]
		case "--binary-out":
			i++
			if i >= len(argv) {
				return opts, fmt.Errorf("--binary-out requires a value")
			}
			opts.BinaryOutDir = argv[i]
		case "--dry-run":
			opts.DryRun = true
		default:
			return opts, fmt.Errorf("unknown flag for migrate-kdbx: %s", argv[i])
		}
	}

	opts.DBPath = expandHome(opts.DBPath)
	opts.PassFile = expandHome(opts.PassFile)
	opts.BinaryOutDir = expandHome(opts.BinaryOutDir)

	if _, err := os.Stat(opts.DBPath); err != nil {
		return opts, fmt.Errorf("database file %q is not readable: %w", opts.DBPath, err)
	}
	if _, err := os.Stat(opts.PassFile); err != nil {
		return opts, fmt.Errorf("password file %q is not readable: %w", opts.PassFile, err)
	}
	return opts, nil
}

func readPasswordFile(path string) (string, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return "", fmt.Errorf("reading password file %q: %w", path, err)
	}
	pass := strings.TrimRight(string(data), "\r\n")
	if pass == "" {
		return "", fmt.Errorf("password file %q is empty", path)
	}
	return pass, nil
}

func resolveHomeDir() string {
	home, err := os.UserHomeDir()
	if err != nil || home == "" {
		return "."
	}
	return home
}

func expandHome(path string) string {
	if path == "~" {
		return resolveHomeDir()
	}
	if strings.HasPrefix(path, "~/") {
		return filepath.Join(resolveHomeDir(), path[2:])
	}
	return path
}

var passwordLinePattern = regexp.MustCompile(`(?i)^\s*(pass|password)\s*:\s*(.*)\s*$`)

func extractPasswordFromContent(content string) (password, notes string) {
	lines := strings.Split(content, "\n")
	notesLines := make([]string, 0, len(lines))

	for _, line := range lines {
		m := passwordLinePattern.FindStringSubmatch(line)
		if len(m) == 3 {
			if password == "" {
				password = strings.TrimSpace(m[2])
			}
			continue
		}
		notesLines = append(notesLines, line)
	}

	notes = strings.TrimRight(strings.Join(notesLines, "\n"), "\n")
	return password, notes
}