-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (134 loc) · 3.07 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
spath "path"
"strings"
)
var (
path string
root string
found = make(map[string]struct{})
)
func init() {
flag.StringVar(&path, "path", "", "XML file")
flag.StringVar(&root, "root", "/", "Root in the password store")
}
func main() {
flag.Parse()
if path == "" {
fmt.Printf("Specify a path\n")
os.Exit(1)
}
if kpFile, err := Parse(path); err != nil {
fmt.Printf("Failed to parse '%s': %#v\n", path, err)
os.Exit(1)
} else {
if err := export(kpFile); err != nil {
fmt.Printf("Failed to export to passwordstore: %#v\n", err)
os.Exit(1)
}
}
}
func export(kpFile *KeePassFile) error {
for _, group := range kpFile.Groups {
if err := exportGroup(kpFile, group, root); err != nil {
return err
}
}
return nil
}
func exportGroup(kpFile *KeePassFile, group Group, rootPath string) error {
groupPath := spath.Join(rootPath, group.Name)
for _, entry := range group.Entries {
if err := exportEntry(kpFile, entry, groupPath); err != nil {
return err
}
}
for _, childGroup := range group.Groups {
if err := exportGroup(kpFile, childGroup, groupPath); err != nil {
return err
}
}
return nil
}
func exportEntry(kpFile *KeePassFile, entry Entry, groupPath string) error {
title := cleanPath(entry.GetValue("Title"))
path := makeUniquePath(spath.Join(groupPath, title))
found[path] = struct{}{}
fmt.Printf("Found '%s'\n", path)
content, err := formatEntry(kpFile, entry)
if err != nil {
return err
}
if err := insertIntoPath(path, []byte(content)); err != nil {
return err
}
for _, bin := range entry.Binaries {
key := bin.Key
if key == "" {
continue
}
data, err := findBinary(kpFile, bin.Value.Ref)
if err != nil {
return err
}
attachmentPath := makeUniquePath(spath.Join(path, cleanPath(key)))
found[attachmentPath] = struct{}{}
if err := insertIntoPath(attachmentPath, data); err != nil {
return err
}
}
return nil
}
func cleanPath(path string) string {
if strings.HasPrefix(path, ".") {
return "_" + path[1:]
}
return path
}
func insertIntoPath(path string, content []byte) error {
cmd := exec.Command("pass", "insert", "-m", path)
cmd.Stdin = bytes.NewBuffer(content)
if stdout, err := cmd.Output(); err != nil {
fmt.Printf("Failed to insert '%s': %v\n%s\n", path, err, string(stdout))
return err
}
return nil
}
func makeUniquePath(path string) string {
if _, ok := found[path]; !ok {
return path
}
i := 1
for {
p := fmt.Sprintf("%s@%d", path, i)
if _, ok := found[p]; !ok {
return p
}
i++
}
}
func formatEntry(kpFile *KeePassFile, entry Entry) (string, error) {
lines := []string{
entry.GetValue("Password"),
}
for _, key := range []string{"UserName", "URL", "Notes"} {
x := entry.GetValue(key)
if x != "" {
lines = append(lines, fmt.Sprintf("%s: %s", key, x))
}
}
return strings.Join(lines, "\n"), nil
}
func findBinary(kpFile *KeePassFile, id string) ([]byte, error) {
for _, bin := range kpFile.Meta.Binaries {
if bin.ID == id {
return bin.Decode()
}
}
return nil, fmt.Errorf("Binary '%s' not found", id)
}