Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2012 Juhamatti Niemelä <iiska@iki.fi>. All Rights Reserved.
  5. # This file is licensed under the GPLv2+. Please see COPYING for more information.
  6.  
  7. import sys
  8. import re
  9.  
  10. from subprocess import Popen, PIPE
  11. from xml.etree import ElementTree
  12.  
  13. def space_to_camelcase(value):
  14. output = ""
  15. first_word_passed = False
  16. for word in value.split(" "):
  17. if not word:
  18. output += "_"
  19. continue
  20. if first_word_passed:
  21. output += word.capitalize()
  22. else:
  23. output += word.lower()
  24. first_word_passed = True
  25. return output
  26.  
  27. def cleanTitle(title):
  28. # make the title more command line friendly
  29. title = re.sub("(\\|\||\(|\)|/)", "-", title)
  30. title = re.sub("-$", "", title)
  31. title = re.sub("\@", "At", title)
  32. title = re.sub("'", "", title)
  33. return title
  34.  
  35. def path_for(element, path=''):
  36. """ Generate path name from elements title and current path """
  37. title_text = element.find('title').text
  38. if title_text is None:
  39. title_text = ''
  40. title = cleanTitle(space_to_camelcase(title_text))
  41. return '/'.join([path, title])
  42.  
  43. def path_username(element, path=''):
  44. """Generate path name from elements title and username"""
  45. title_text = element.find('title').text
  46. if title_text is None:
  47. title_text = ''
  48. title = cleanTitle(space_to_camelcase(title_text))
  49. username = element.find('username').text
  50. if username is None:
  51. username = 'no_username'
  52. return title + '/' + username
  53.  
  54. def password_data(element):
  55. """ Return password data and additional info if available from
  56. password entry element. """
  57. passwd = element.find('password').text
  58. ret = passwd + "\n" if passwd else "\n"
  59. for field in ['username', 'url', 'comment']:
  60. fel = element.find(field)
  61. children = [unicode(e.text or '') + unicode(e.tail or '') for e in list(fel)]
  62. if len(children) > 0:
  63. children.insert(0, '')
  64. text = (fel.text or '') + "\n".join(children)
  65. if len(text) > 0:
  66. ret = "%s%s: %s\n" % (ret, fel.tag, text)
  67. return ret
  68.  
  69. def import_entry(element, path=''):
  70. """ Import new password entry to password-store using pass insert
  71. command """
  72. print "Importing " + path_for(element, path)
  73. proc = Popen(['pass', 'insert', '--multiline', '--force',
  74. path_username(element, path)],
  75. stdin=PIPE, stdout=PIPE)
  76. proc.communicate(password_data(element).encode('utf8'))
  77. proc.wait()
  78.  
  79. def import_group(element, path=''):
  80. """ Import all entries and sub-groups from given group """
  81. npath = path_for(element, path)
  82. for group in element.findall('group'):
  83. import_group(group, npath)
  84. for entry in element.findall('entry'):
  85. import_entry(entry, npath)
  86.  
  87.  
  88. def main(xml_file):
  89. """ Parse given KeepassX XML file and import password groups from it """
  90. for group in ElementTree.parse(xml_file).findall('group'):
  91. import_group(group)
  92.  
  93. if __name__ == '__main__':
  94. main(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement