summaryrefslogtreecommitdiff
path: root/contrib/keepassx2pass.py
blob: cc7593833ba9eccbdcdb3579de59b67b240ac05b (plain)
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# (C) Copyright 2012 Juhamatti Niemelä <iiska@iki.fi>. All Rights
# Reserved. This file is licensed under the GPLv2+. Please see COPYING
# for more information.

import sys

from subprocess import Popen, PIPE
from xml.etree import ElementTree

def path_for(element, path=''):
    """ Generate path name from elements title and current path """
    title = element.find('title').text
    return '/'.join([path, title])

def password_data(element):
    """ Return password data and additional info if available from
    password entry element. """
    ret = element.find('password').text + "\n"
    for field in ['username', 'url', 'comment']:
        fel = element.find(field)
        if fel.text is not None:
            ret = "%s%s: %s\n" % (ret, fel.tag, fel.text)
    return ret

def import_entry(element, path=''):
    """ Import new password entry to password-store using pass insert
    command """
    proc = Popen(['pass', 'insert', '--multiline', '--force',
                  path_for(element, path)],
              stdin=PIPE, stdout=PIPE)
    proc.communicate(password_data(element).encode('utf8'))
    proc.wait()

def import_group(element, path=''):
    """ Import all entries and sub-groups from given group """
    npath = path_for(element, path)
    for group in element.findall('group'):
        import_group(group, npath)
    for entry in element.findall('entry'):
        import_entry(entry, npath)


def main(xml_file):
    """ Parse given KeepassX XML file and import password groups from it """
    with open(xml_file) as xml:
        for group in ElementTree.XML(xml.read()).findall('group'):
            import_group(group)

if __name__ == '__main__':
    main(sys.argv[1])
0c2f47eaf467db8ab1443e61dfa1c280f30a&follow=1'>Add display of tree content w/ui-tree.cLars Hjemli9-8/+113 2006-12-12cache_lock: do xstrdup/free on lockfileLars Hjemli1-1/+2 2006-12-11Don't truncate valid cachefilesLars Hjemli3-4/+16 2006-12-11Move global variables + callback functions into shared.cLars Hjemli4-82/+86 2006-12-11Move functions for generic object output into ui-view.cLars Hjemli4-34/+43 2006-12-11Move log-functions into ui-log.cLars Hjemli5-111/+121 2006-12-11Move repo summary functions into ui-summary.cLars Hjemli4-47/+59 2006-12-11Move functions for repolist output into ui-repolist.cLars Hjemli5-70/+90 2006-12-11Move common output-functions into ui-shared.cLars Hjemli4-82/+99 2006-12-11Rename config.c to parsing.c + move cgit_parse_query from cgit.c to parsing.cLars Hjemli4-28/+29 2006-12-11Avoid infinite loops in caching layerLars Hjemli3-14/+31 2006-12-11Let 'make install' clear all cachefilesLars Hjemli1-0/+2 2006-12-11Fix cache algorithm loopholeLars Hjemli3-11/+16 2006-12-10Add version identifier in generated filesLars Hjemli2-9/+14 2006-12-10Add license file and copyright noticesLars Hjemli5-0/+372 2006-12-10Add caching infrastructureLars Hjemli9-28/+353