-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (125 loc) · 3.81 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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"slices"
"strings"
"syscall"
"github.com/tobischo/gokeepasslib/v3"
"golang.org/x/term"
)
type (
ConfigEntry struct {
Uuid string `json:"uuid"`
Exports []ConfigEntryExports `json:"exports"`
}
ConfigEntryExports struct {
Field string `json:"field"`
Variable string `json:"variable"`
}
)
func main() {
if len(os.Args) < 3 || slices.Contains(os.Args[1:3], "") {
fmt.Fprint(os.Stderr, "Missing arguments.\nUsage: keexp <database_path> <config_path>\n")
os.Exit(1)
}
databasePath := os.Args[1]
configPath := os.Args[2]
config, err := readConfig(configPath)
checkError(err)
fmt.Fprint(os.Stderr, "Enter password: ")
passwordBytes, err := term.ReadPassword(syscall.Stdin)
fmt.Fprintln(os.Stderr)
checkError(err)
databaseFile, err := os.Open(databasePath)
checkError(err)
database := gokeepasslib.NewDatabase()
database.Credentials = gokeepasslib.NewPasswordCredentials(string(passwordBytes))
err = gokeepasslib.NewDecoder(databaseFile).Decode(database)
checkError(err)
err = database.UnlockProtectedEntries()
checkError(err)
entriesByUuid := getEntriesByUuid(database)
for _, configEntry := range config {
uuidBytes, err := hex.DecodeString(configEntry.Uuid)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: UUID \"%s\" could not be decoded: %s\n", configEntry.Uuid, err)
continue
}
uuidLength := len(uuidBytes)
if uuidLength != 16 {
fmt.Fprintf(os.Stderr, "Warning: UUID \"%s\" has invalid length: %d\n", configEntry.Uuid, uuidLength)
continue
}
uuid := gokeepasslib.UUID(uuidBytes)
entry, entryExists := entriesByUuid[uuid]
if !entryExists {
fmt.Fprintf(os.Stderr, "Warning: Entry with UUID \"%s\" could not be found\n", configEntry.Uuid)
continue
}
valuesByKey := getValuesByKey(entry)
for _, export := range configEntry.Exports {
value, valueExists := valuesByKey[export.Field]
if !valueExists {
fmt.Fprintf(os.Stderr, "Warning: Field \"%s\" for entry with UUID \"%s\" could not be found\n", export.Field, configEntry.Uuid)
continue
}
if value == "" {
fmt.Fprintf(os.Stderr, "Warning: Field \"%s\" for entry with UUID \"%s\" is empty\n", export.Field, configEntry.Uuid)
continue
}
escapedValue := strings.ReplaceAll(value, "'", "'\\''")
fmt.Fprintln(os.Stdout, "export "+export.Variable+"='"+escapedValue+"';")
}
}
}
func readConfig(configPath string) ([]ConfigEntry, error) {
configFile, err := os.Open(configPath)
if err != nil {
return nil, err
}
configJson, err := io.ReadAll(configFile)
if err != nil {
return nil, err
}
var config []ConfigEntry
err = json.Unmarshal(configJson, &config)
return config, err
}
func getEntriesByUuid(database *gokeepasslib.Database) map[gokeepasslib.UUID]*gokeepasslib.Entry {
entriesByUuid := make(map[gokeepasslib.UUID]*gokeepasslib.Entry)
groups := database.Content.Root.Groups
for len(groups) > 0 {
for _, entry := range groups[0].Entries {
if entriesByUuid[entry.UUID] != nil {
fmt.Fprintf(os.Stderr, "Warning: Found multiple entries with UUID \"%s\"\n", entry.UUID)
continue
}
entriesByUuid[entry.UUID] = &entry
}
groups = append(groups, groups[0].Groups...)
groups[0] = groups[len(groups)-1]
groups = groups[:len(groups)-1]
}
return entriesByUuid
}
func getValuesByKey(entry *gokeepasslib.Entry) map[string]string {
valuesByKey := make(map[string]string, len(entry.Values))
for _, value := range entry.Values {
if valuesByKey[value.Key] != "" {
fmt.Fprintf(os.Stderr, "Warning: Found values with duplicate key \"%s\" for entry with UUID \"%s\"\n", value.Key, entry.UUID)
continue
}
valuesByKey[value.Key] = value.Value.Content
}
return valuesByKey
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}