Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. """
  2. Import a SafeInCloud XML export to pass.
  3.  
  4. Created by Sandro Wahl on 05.08.2019
  5. """
  6. import sys
  7. import re
  8.  
  9. from subprocess import Popen, PIPE
  10. from xml.etree import ElementTree
  11.  
  12. TYPES = ["login", "password", "email", "pin", "text"]
  13.  
  14.  
  15. def insert_data(path, text):
  16. """
  17. Insert data into the password store.
  18. (1) ensures text ends with a new line and encodes in UTF-8
  19. (2) inserts
  20. """
  21. if not text.endswith('\n'):
  22. text = text + '\n'
  23. text = text.encode('utf8')
  24.  
  25. print("Inserting {}".format(path))
  26. proc = Popen(['pass', 'insert', '--multiline', '--force', path], stdin=PIPE, stdout=PIPE)
  27. proc.communicate(text)
  28. proc.wait()
  29.  
  30.  
  31. def clean_string(value):
  32. """
  33. Make the string more commandline friendly.
  34. """
  35. value = value.strip()
  36. value = '_'.join(value.split())
  37. value = re.sub("(\\|\||\(|\)|/|\"|')", "", value)
  38. value = re.sub("\@", "At", value)
  39. value = re.sub("-", "_", value)
  40. value = '_'.join(value.split('_'))
  41. return value
  42.  
  43.  
  44. def parse_database(element):
  45. cards = 0
  46. for card in element.findall("card"):
  47. if is_not(card, "deleted") and is_not(card, "template"):
  48. parse_card(card)
  49. cards += 1
  50. print("Imported {} cards".format(cards))
  51.  
  52.  
  53. def is_not(element, attrib):
  54. return (attrib not in element.attrib) or (attrib in element.attrib and element.attrib[attrib] == "false")
  55.  
  56.  
  57. def parse_card(element):
  58. path = clean_string(element.attrib["title"])
  59. for field in element.findall("field"):
  60. parse_field(field, path)
  61.  
  62.  
  63. def parse_field(element, path):
  64. if element.attrib["type"] in TYPES:
  65. path += "/"
  66. path += clean_string(element.attrib["name"])
  67. insert_data(path, element.text)
  68.  
  69.  
  70. def main(xml_file):
  71. """ Parse XML entries from a SafeInCloud """
  72. element = ElementTree.parse(xml_file).getroot()
  73. assert element.tag == 'database'
  74. parse_database(element)
  75.  
  76.  
  77. if __name__ == '__main__':
  78. main(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement