-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·176 lines (170 loc) · 4.78 KB
/
index.js
File metadata and controls
executable file
·176 lines (170 loc) · 4.78 KB
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
#!/usr/bin/env node
import { pathToFileURL } from "node:url";
import fs from "node:fs/promises";
import {
Credentials,
FileDatasource,
Vault,
VaultFormatB,
setDefaultFormat,
init,
} from "buttercup";
import { stringify } from "csv-stringify/sync";
import { shell } from "shell";
const parse_arguments = () => {
const app = shell({
name: "buttercup-to-keepass",
description:
"Reads a KeePass vault and export its entries as CSV in a KeePass format.",
options: {
columns: {
description: "Print column names in the first line.",
shortcut: "c",
type: "boolean",
},
info: {
description: "Print the vault structure to stdout.",
shortcut: "i",
type: "boolean",
},
otp: {
description: "List of properties interpreted as OTP code.",
default: ["otp"],
shortcut: "o",
type: "array",
},
password: {
description: "Buttercup vault password",
required: true,
shortcut: "p",
},
source: {
description: "Buttercup vault location",
required: true,
shortcut: "s",
},
target: {
description: "CSV exported file location",
required: true,
shortcut: "t",
},
},
});
try {
const args = app.parse(process.argv.slice(2));
if (app.helping(args)) {
process.stdout.write(app.help());
process.exit();
}
return args;
} catch (err) {
process.stdout.write(app.help());
process.exit(1);
}
};
const open_vault = async (args) => {
// See https://buttercup.github.io/core-docs/#/usage/basic
init();
setDefaultFormat(VaultFormatB);
const datasourceCredentials = Credentials.fromDatasource(
{
type: "file",
path: args.source,
},
args.password,
);
const datasource = new FileDatasource(datasourceCredentials);
const loadedState = await datasource.load(
Credentials.fromPassword(args.password),
);
return Vault.createFromHistory(loadedState.history, loadedState.Format);
};
const concert_to_records = (args, vault) => {
const records = [];
const walk = ({ group, depth = 0, parents = [] }) => {
if (depth === 0) {
if (args.info) process.stdout.write("vault\n");
} else {
if (args.info)
process.stdout.write(
" ".repeat(depth * 2) + "- " + group.getTitle() + "\n",
);
for (const entry of group.getEntries()) {
// See https://github.com/adaltas/buttercup-to-keepass/issues/8
// Two format are encountered to store properties values:
// - `entry._source.p[property].value`
// - `entry._source.properties[propery]`
const properties = entry._source.p || entry._source.properties;
for (const k in properties) {
if (!properties[k].value === undefined) continue;
properties[k] = properties[k].value;
}
if (args.info)
process.stdout.write(
" ".repeat(depth * 2) + " " + properties.title + "\n",
);
// https://keepass.info/help/kb/imp_csv.html
const otp = args.otp.find((otp) => properties[otp]);
const record = {
Group: [...parents, group.getTitle()].join("/"),
Title: properties.title,
Username: properties.username,
Password: properties.password,
URL: properties.url,
Notes: properties.notes,
TOTP: properties[otp],
};
for (const property in properties) {
if (
[
"title",
"username",
"password",
"url",
"notes",
...args.otp,
].includes(property)
)
continue;
if (properties[property]) {
if (!record.Notes) {
record.Notes = "";
} else {
record.Notes += "\n\n";
}
record.Notes += `# ${property}` + "\n\n" + properties[property];
}
}
records.push(record);
}
}
for (const child of group.getGroups()) {
walk({
group: child,
depth: depth + 1,
parents: [...parents, depth === 0 ? "" : group.getTitle()],
});
}
};
walk({ group: vault });
return records;
};
const write_csv = async (args, records) => {
const data = stringify(records, {
header: args.columns,
columns: ["Group", "Title", "Username", "Password", "URL", "Notes", "TOTP"],
});
await fs.writeFile(args.target, data);
};
export default {
parse_arguments,
open_vault,
concert_to_records,
write_csv,
};
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
const args = parse_arguments();
const vault = await open_vault(args);
const records = concert_to_records(args, vault);
write_csv(args, records);
}