2018-08-15 00:59:11 -04:00
|
|
|
#!/usr/bin/env python3
|
2021-05-19 15:30:10 -04:00
|
|
|
import argparse
|
2016-05-29 19:00:02 -04:00
|
|
|
import csv
|
2018-12-27 12:12:06 -05:00
|
|
|
import logging
|
2020-10-24 17:26:01 -04:00
|
|
|
import pathlib
|
2021-05-19 15:30:10 -04:00
|
|
|
import re
|
|
|
|
import sys
|
2016-05-29 19:00:02 -04:00
|
|
|
|
2018-12-27 12:12:06 -05:00
|
|
|
import gnupg
|
|
|
|
|
2022-11-15 15:13:32 -05:00
|
|
|
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
2018-12-27 12:12:06 -05:00
|
|
|
|
|
|
|
|
2021-05-19 15:30:10 -04:00
|
|
|
def set_meta(entry, path, grouping_base):
|
|
|
|
pure_path = pathlib.PurePath(path)
|
|
|
|
group = pure_path.relative_to(grouping_base).parent
|
|
|
|
if group.name == '':
|
|
|
|
group = ''
|
|
|
|
entry['group'] = group
|
|
|
|
entry['title'] = pure_path.stem
|
2018-12-27 12:12:06 -05:00
|
|
|
|
2020-10-24 17:26:01 -04:00
|
|
|
|
2021-05-19 15:30:10 -04:00
|
|
|
def set_data(entry, data, exclude, get_fields, get_lines):
|
|
|
|
lines = data.splitlines()
|
|
|
|
tail = lines[1:]
|
|
|
|
entry['password'] = lines[0]
|
2018-12-27 12:12:06 -05:00
|
|
|
|
2021-05-19 15:30:10 -04:00
|
|
|
filtered_tail = []
|
|
|
|
for line in tail:
|
|
|
|
for exclude_pattern in exclude:
|
|
|
|
if exclude_pattern.search(line):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
filtered_tail.append(line)
|
2020-06-07 16:39:28 -04:00
|
|
|
|
2021-05-19 15:30:10 -04:00
|
|
|
matching_indices = set()
|
|
|
|
fields = entry.setdefault('fields', {})
|
2020-06-07 16:39:28 -04:00
|
|
|
|
2021-05-19 15:30:10 -04:00
|
|
|
for i, line in enumerate(filtered_tail):
|
2021-05-21 16:30:02 -04:00
|
|
|
for name, pattern in get_fields:
|
2021-05-19 15:30:10 -04:00
|
|
|
if name in fields:
|
|
|
|
# multiple patterns with same name, we've already found a match
|
|
|
|
continue
|
|
|
|
match = pattern.search(line)
|
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
inverse_match = line[0:match.start()] + line[match.end():]
|
|
|
|
value = inverse_match.strip()
|
|
|
|
fields[name] = value
|
|
|
|
matching_indices.add(i)
|
|
|
|
break
|
|
|
|
|
|
|
|
matching_lines = {}
|
|
|
|
for i, line in enumerate(filtered_tail):
|
2021-05-21 16:30:02 -04:00
|
|
|
for name, pattern in get_lines:
|
2021-05-19 15:30:10 -04:00
|
|
|
match = pattern.search(line)
|
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
matches = matching_lines.setdefault(name, [])
|
|
|
|
matches.append(line)
|
|
|
|
matching_indices.add(i)
|
|
|
|
break
|
|
|
|
for name, matches in matching_lines.items():
|
2022-11-15 08:18:57 -05:00
|
|
|
fields[name] = '\n'.join(matches)
|
2021-05-19 15:30:10 -04:00
|
|
|
|
|
|
|
final_tail = []
|
|
|
|
for i, line in enumerate(filtered_tail):
|
|
|
|
if i not in matching_indices:
|
|
|
|
final_tail.append(line)
|
|
|
|
|
|
|
|
entry['notes'] = '\n'.join(final_tail).strip()
|
|
|
|
|
|
|
|
|
|
|
|
def write(file, entries, get_fields, get_lines):
|
|
|
|
get_field_names = set(x[0] for x in get_fields)
|
|
|
|
get_line_names = set(x[0] for x in get_lines)
|
|
|
|
field_names = get_field_names | get_line_names
|
|
|
|
header = ["Group(/)", "Title", "Password", *field_names, "Notes"]
|
|
|
|
csvw = csv.writer(file)
|
2022-11-15 15:13:32 -05:00
|
|
|
logging.info("\nWriting data to %s\n", file.name)
|
2021-05-19 15:30:10 -04:00
|
|
|
csvw.writerow(header)
|
|
|
|
for entry in entries:
|
|
|
|
fields = [entry['fields'].get(name) for name in field_names]
|
|
|
|
columns = [
|
|
|
|
entry['group'], entry['title'], entry['password'],
|
|
|
|
*fields,
|
|
|
|
entry['notes']
|
|
|
|
]
|
|
|
|
csvw.writerow(columns)
|
|
|
|
|
|
|
|
|
2022-11-15 15:14:13 -05:00
|
|
|
def main(store_path, outfile, grouping_base, gpgbinary, use_agent, encodings,
|
2021-05-19 15:30:10 -04:00
|
|
|
exclude, get_fields, get_lines):
|
|
|
|
entries = []
|
|
|
|
failures = []
|
|
|
|
path = pathlib.Path(store_path)
|
|
|
|
grouping_path = pathlib.Path(grouping_base)
|
|
|
|
gpg = gnupg.GPG(gpgbinary=gpgbinary, use_agent=use_agent)
|
2022-03-29 10:26:06 -04:00
|
|
|
files = path.glob('**/*.gpg')
|
2022-03-24 08:31:10 -04:00
|
|
|
if not path.is_dir():
|
2022-03-29 10:48:14 -04:00
|
|
|
if path.is_file():
|
|
|
|
files = [path]
|
|
|
|
else:
|
2022-04-10 06:20:30 -04:00
|
|
|
err = "No such file or directory: {}".format(path)
|
2022-03-29 10:48:14 -04:00
|
|
|
logging.error(err)
|
2022-04-10 06:20:38 -04:00
|
|
|
sys.exit(1)
|
2022-03-24 08:31:10 -04:00
|
|
|
for file in files:
|
2021-05-19 15:30:10 -04:00
|
|
|
logging.info("Processing %s", file)
|
|
|
|
with open(file, 'rb') as fp:
|
|
|
|
decrypted = gpg.decrypt_file(fp)
|
|
|
|
if not decrypted.ok:
|
2021-07-28 16:29:35 -04:00
|
|
|
err = "Could not decrypt {}: {}".format(file, decrypted.status)
|
|
|
|
logging.error(err)
|
|
|
|
failures.append(err)
|
|
|
|
continue
|
|
|
|
for i, encoding in enumerate(encodings):
|
|
|
|
try:
|
|
|
|
# decrypted.data is bytes
|
|
|
|
decrypted_data = decrypted.data.decode(encoding)
|
|
|
|
except Exception as e:
|
|
|
|
logging.warning(
|
|
|
|
"Could not decode {} with encoding {}: {}"
|
|
|
|
.format(file, encoding, e)
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
if i > 0:
|
|
|
|
# don't log if the first encoding worked
|
|
|
|
logging.warning("Decoded {} with encoding {}".format(file, encoding))
|
|
|
|
break
|
|
|
|
else:
|
2022-11-15 15:13:32 -05:00
|
|
|
err = "Could not decode {}, see messages above for more info.".format(file)
|
2021-07-28 16:29:35 -04:00
|
|
|
failures.append(err)
|
2021-05-19 15:30:10 -04:00
|
|
|
continue
|
|
|
|
entry = {}
|
|
|
|
set_meta(entry, file, grouping_path)
|
2021-07-28 16:29:35 -04:00
|
|
|
set_data(entry, decrypted_data, exclude, get_fields, get_lines)
|
2021-05-19 15:30:10 -04:00
|
|
|
entries.append(entry)
|
|
|
|
if failures:
|
2022-11-15 15:13:32 -05:00
|
|
|
logging.warning("\nGot errors while processing files:")
|
2021-07-28 16:29:35 -04:00
|
|
|
for err in failures:
|
|
|
|
logging.warning(err)
|
2021-05-19 15:30:10 -04:00
|
|
|
write(outfile, entries, get_fields, get_lines)
|
|
|
|
|
|
|
|
|
2022-11-15 15:14:13 -05:00
|
|
|
def parse_args(args=None):
|
2021-05-19 15:30:10 -04:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
'store_path',
|
2022-11-15 15:14:13 -05:00
|
|
|
metavar='STOREPATH',
|
2021-05-19 15:30:10 -04:00
|
|
|
type=str,
|
|
|
|
help="path to the password-store to export",
|
|
|
|
)
|
|
|
|
|
2022-11-15 15:14:13 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'outfile',
|
|
|
|
metavar='OUTFILE',
|
|
|
|
type=argparse.FileType('w'),
|
|
|
|
help="file to write exported data to, use - for stdout",
|
|
|
|
)
|
|
|
|
|
2021-05-19 15:30:10 -04:00
|
|
|
parser.add_argument(
|
|
|
|
'-b', '--base',
|
|
|
|
metavar='path',
|
|
|
|
type=str,
|
|
|
|
help="path to use as base for grouping passwords",
|
|
|
|
dest='base_path'
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-g', '--gpg',
|
|
|
|
metavar='executable',
|
|
|
|
type=str,
|
|
|
|
default="gpg",
|
|
|
|
help="path to the gpg binary you wish to use (default 'gpg')",
|
|
|
|
dest='gpgbinary'
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-a', '--use-agent',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help="ask gpg to use its auth agent",
|
|
|
|
dest='use_agent'
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2021-07-28 16:29:35 -04:00
|
|
|
'--encodings',
|
|
|
|
metavar='encodings',
|
2021-05-19 15:30:10 -04:00
|
|
|
type=str,
|
|
|
|
default="utf-8",
|
2021-07-28 16:29:35 -04:00
|
|
|
help=(
|
|
|
|
"comma-separated text encodings to try, in order, when decoding"
|
|
|
|
" gpg output (default 'utf-8')"
|
|
|
|
),
|
|
|
|
dest='encodings'
|
2021-05-19 15:30:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-e', '--exclude',
|
|
|
|
metavar='pattern',
|
2021-07-28 16:29:59 -04:00
|
|
|
action='append',
|
2021-05-19 15:30:10 -04:00
|
|
|
type=str,
|
|
|
|
default=[],
|
2021-07-28 16:29:59 -04:00
|
|
|
help=(
|
|
|
|
"regexp for lines which should not be exported, can be specified"
|
|
|
|
" multiple times"
|
|
|
|
),
|
2021-05-19 15:30:10 -04:00
|
|
|
dest='exclude'
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-f', '--get-field',
|
|
|
|
metavar=('name', 'pattern'),
|
|
|
|
action='append',
|
|
|
|
nargs=2,
|
|
|
|
type=str,
|
|
|
|
default=[],
|
|
|
|
help=(
|
|
|
|
"a name and a regexp, the part of the line matching the regexp"
|
|
|
|
" will be removed and the remaining line will be added to a field"
|
|
|
|
" with the chosen name. only one match per password, matching"
|
|
|
|
" stops after the first match"
|
|
|
|
),
|
|
|
|
dest='get_fields'
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-l', '--get-line',
|
|
|
|
metavar=('name', 'pattern'),
|
|
|
|
action='append',
|
|
|
|
nargs=2,
|
|
|
|
type=str,
|
|
|
|
default=[],
|
|
|
|
help=(
|
|
|
|
"a name and a regexp for which all lines that match are included"
|
|
|
|
" in a field with the chosen name"
|
|
|
|
),
|
|
|
|
dest='get_lines'
|
|
|
|
)
|
|
|
|
|
|
|
|
return parser.parse_args(args)
|
|
|
|
|
|
|
|
|
|
|
|
def compile_regexp(pattern):
|
|
|
|
try:
|
|
|
|
regexp = re.compile(pattern, re.I)
|
|
|
|
except re.error as e:
|
|
|
|
logging.error(
|
|
|
|
"Could not compile pattern '%s', %s at position %s",
|
|
|
|
pattern.replace("'", "\\'"), e.msg, e.pos
|
2020-06-07 16:39:28 -04:00
|
|
|
)
|
2021-05-19 15:30:10 -04:00
|
|
|
return None
|
|
|
|
return regexp
|
2020-06-07 16:39:28 -04:00
|
|
|
|
2018-12-27 12:12:06 -05:00
|
|
|
|
2016-05-29 19:00:02 -04:00
|
|
|
if __name__ == '__main__':
|
2022-11-15 15:14:13 -05:00
|
|
|
parsed = parse_args()
|
2021-05-19 15:30:10 -04:00
|
|
|
|
|
|
|
failed = False
|
|
|
|
exclude_patterns = []
|
|
|
|
for pattern in parsed.exclude:
|
|
|
|
regexp = compile_regexp(pattern)
|
|
|
|
if not regexp:
|
|
|
|
failed = True
|
|
|
|
exclude_patterns.append(regexp)
|
|
|
|
|
|
|
|
get_fields = []
|
2021-05-21 16:30:02 -04:00
|
|
|
for name, pattern in parsed.get_fields:
|
2021-05-19 15:30:10 -04:00
|
|
|
regexp = compile_regexp(pattern)
|
|
|
|
if not regexp:
|
|
|
|
failed = True
|
|
|
|
get_fields.append((name, regexp))
|
|
|
|
|
|
|
|
get_lines = []
|
2021-05-21 16:30:02 -04:00
|
|
|
for name, pattern in parsed.get_lines:
|
2021-05-19 15:30:10 -04:00
|
|
|
regexp = compile_regexp(pattern)
|
|
|
|
if not regexp:
|
|
|
|
failed = True
|
|
|
|
get_lines.append((name, regexp))
|
|
|
|
|
|
|
|
if failed:
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if parsed.base_path:
|
|
|
|
grouping_base = parsed.base_path
|
|
|
|
else:
|
|
|
|
grouping_base = parsed.store_path
|
|
|
|
|
2021-07-28 16:29:35 -04:00
|
|
|
encodings = [e for e in parsed.encodings.split(',') if e]
|
|
|
|
if not encodings:
|
|
|
|
logging.error(
|
2021-07-28 16:47:07 -04:00
|
|
|
"Did not understand '--encodings {}'".format(parsed.encoding)
|
2021-07-28 16:29:35 -04:00
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-05-19 15:30:10 -04:00
|
|
|
kwargs = {
|
|
|
|
'store_path': parsed.store_path,
|
2022-11-15 15:14:13 -05:00
|
|
|
'outfile': parsed.outfile,
|
2021-05-19 15:30:10 -04:00
|
|
|
'grouping_base': grouping_base,
|
|
|
|
'gpgbinary': parsed.gpgbinary,
|
|
|
|
'use_agent': parsed.use_agent,
|
2021-07-28 16:29:35 -04:00
|
|
|
'encodings': encodings,
|
2021-05-19 15:30:10 -04:00
|
|
|
'exclude': exclude_patterns,
|
|
|
|
'get_fields': get_fields,
|
|
|
|
'get_lines': get_lines
|
|
|
|
}
|
|
|
|
|
|
|
|
main(**kwargs)
|