Last active
October 6, 2019 09:40
-
-
Save k3karthic/86b1b43fe71e50bceff8220e06bc3935 to your computer and use it in GitHub Desktop.
Convert password-store repo to keepass format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import gnupg | |
import pykeepass | |
from getpass import getpass | |
from pykeepass import PyKeePass | |
from os import walk | |
from os.path import isfile, join | |
from shutil import copyfile | |
gpg = gnupg.GPG() | |
groupscreated = {} | |
templatepath = "/home/karthic/Nextcloud/template.kdbx" | |
mypath = "/home/karthic/.password-store" | |
kdbxpath = "/home/karthic/Nextcloud/pass.kdbx" | |
copyfile(templatepath, kdbxpath) | |
passpass = getpass() | |
db = PyKeePass(kdbxpath, password=passpass) | |
def get_group(kppath): | |
print("Processing %s..." % kppath) | |
groups = kppath.split('/')[0:-1] | |
result = None | |
group_key = [] | |
for g in groups: | |
group_key.append(g) | |
group_key_str = ','.join(group_key) | |
obj = None | |
if group_key_str in groupscreated: | |
obj = groupscreated[group_key_str] | |
if obj is None: | |
if result is None: | |
obj = db.add_group(db.root_group, g) | |
else: | |
obj = db.add_group(result, g) | |
result = obj | |
groupscreated[group_key_str] = obj | |
if result is None: | |
result = db.root_group | |
return result | |
for dirpath, _, fl in walk(mypath): | |
for f in fl: | |
abspath = join(dirpath, f) | |
kppath = abspath.replace(mypath, '')[1:].replace('.gpg', '') | |
if isfile(abspath) and f.endswith('.gpg'): | |
g = get_group(kppath) | |
h = open(abspath, 'rb') | |
data = h.read() | |
h.close() | |
text = gpg.decrypt(data, passphrase=passpass) | |
if not text.ok: | |
print(text.status) | |
print(text.stderr) | |
sys.exit(0) | |
lines = str(text).splitlines() | |
password = '' | |
username = '' | |
url = '' | |
notes = [] | |
for l in lines: | |
if password == '': | |
password = l | |
elif l.startswith("Username:"): | |
username = l.replace("Username: ", '') | |
elif l.startswith("URL:"): | |
url = l.replace("URL: ", '') | |
else: | |
notes.append(l) | |
db.add_entry(g, f.replace('.gpg', ''), username, password, notes="\n".join(notes), url=url) | |
db.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment