Guest User

Oleg

a guest
Feb 14th, 2010
335
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import optparse
  4. import logging
  5. import os
  6. import sys
  7. import time
  8. from xml.etree import ElementTree
  9.  
  10. progname = os.path.basename(sys.argv[0])
  11. version = "0.1"
  12.  
  13. parser = optparse.OptionParser(usage="%prog input")
  14. logging.basicConfig(level=logging.INFO,
  15. format=progname + ": %(levelname)s: %(message)s")
  16. parser.add_option("-i", "--ignore-stripped",
  17. action="store_true",
  18. dest="ignore_stripped",
  19. help="ignore the stripped ids")
  20.  
  21. (options, args) = parser.parse_args()
  22.  
  23. class FatalError(Exception):
  24. pass
  25.  
  26. def error(msg):
  27. logging.error(msg)
  28. raise FatalError
  29.  
  30. stripped_ids = {}
  31.  
  32. from_type_to_icon = {
  33. "email": 19,
  34. "ftp": 27,
  35. "shell": 30,
  36. "creditcard": 9,
  37. "cryptokey": 29,
  38. "door": 52,
  39. "folder": 48,
  40. "generic": 0,
  41. "phone": 68,
  42. "website": 1
  43. }
  44.  
  45. try:
  46. if len(args) != 1:
  47. error("requires one argument")
  48.  
  49. old_filename = args[0]
  50.  
  51. old_doc = ElementTree.ElementTree(file=old_filename)
  52. old_root = old_doc.getroot()
  53. new_root = ElementTree.Element("database")
  54. new_doc = ElementTree.ElementTree(new_root)
  55.  
  56. def parse_folder(old_element, new_parent, group_depth):
  57. description = None
  58. desc_append = ""
  59. for item in old_element.findall("*"):
  60. if item.tag == "entry":
  61. if item.get("type") == "folder":
  62. element = ElementTree.SubElement(new_parent, "group")
  63. ElementTree.SubElement(element, "icon").text = "48"
  64. element.tail = "\n"
  65. parse_folder(item, element, group_depth + 1)
  66. else:
  67. if group_depth == 0:
  68. fake_group = ElementTree.SubElement(new_parent, "group")
  69. ElementTree.SubElement(fake_group, "icon").text = "48"
  70. ElementTree.SubElement(fake_group, "title").text = "Misc"
  71. fake_group.tail = "\n"
  72. new_parent = fake_group
  73. group_depth += 1
  74.  
  75. element = ElementTree.SubElement(new_parent, "entry")
  76. ElementTree.SubElement(element, "icon").text = str(from_type_to_icon[item.get("type")])
  77. element.tail = "\n"
  78. parse_folder(item, element, group_depth)
  79. else:
  80. element = new_parent
  81. if item.tag == "name":
  82. ElementTree.SubElement(element, "title").text = item.text
  83. elif item.tag == "updated":
  84. lastmod_time = time.strftime("%Y-%m-%dT%H:%M:%S",
  85. time.localtime(int(item.text)))
  86. ElementTree.SubElement(element, "lastmod").text = lastmod_time
  87. ElementTree.SubElement(element, "lastaccess").text = lastmod_time
  88. ElementTree.SubElement(element, "creation").text = lastmod_time
  89. elif item.tag == "field":
  90. id = item.get("id")
  91. if id == "generic-username":
  92. ElementTree.SubElement(element, "username").text = item.text
  93. elif id == "generic-password":
  94. ElementTree.SubElement(element, "password").text = item.text
  95. elif id in ("generic-hostname", "generic-url"):
  96. ElementTree.SubElement(element, "url").text = item.text
  97.  
  98. elif id == "generic-email":
  99. if item.text is not None:
  100. desc_append += "e-mail: " + item.text + "\n"
  101. elif id == "generic-port":
  102. if item.text is not None:
  103. desc_append += "Port: " + item.text + "\n"
  104. elif id == "generic-domain":
  105. if item.text is not None:
  106. ElementTree.SubElement(element, "url").text = item.text
  107. elif id == "creditcard-cardtype":
  108. if item.text is not None:
  109. desc_append += "Type: " + item.text + "\n"
  110.  
  111.  
  112. elif id == "creditcard-cardnumber":
  113. if item.text is not None:
  114. desc_append += "Card Number: " + item.text + "\n"
  115. elif id == "creditcard-expirydate":
  116. if item.text is not None:
  117. desc_append += "Card Expiry: " + item.text + "\n"
  118. elif id == "creditcard-ccv":
  119. if item.text is not None:
  120. desc_append += "Card CCV: " + item.text + "\n"
  121. elif id == "generic-pin":
  122. if item.text is not None:
  123. desc_append += "PIN: " + item.text + "\n"
  124. elif id == "phone-phonenumber":
  125. ElementTree.SubElement(element, "password").text = item.text
  126. elif id == "generic-code":
  127. ElementTree.SubElement(element, "password").text = item.text
  128. elif id in ("creditcard-cardtype", "generic-location", "generic-certificate", "generic-keyfile"):
  129. stripped_ids[id] = 1
  130. else:
  131. error("unexpected field id: " + id)
  132. elif item.tag == "description":
  133. description = item.text
  134. else:
  135. error("unexpected tag: " + item.tag)
  136. if description is not None or len(desc_append) > 0:
  137. if len(desc_append) == 0:
  138. ElementTree.SubElement(element, "comment").text = description
  139. elif description is None:
  140. ElementTree.SubElement(element, "comment").text = desc_append
  141. else:
  142. ElementTree.SubElement(element, "comment").text = description + "\n" + desc_append
  143.  
  144.  
  145. parse_folder(old_root, new_root, 0)
  146.  
  147. if len(stripped_ids) and not options.ignore_stripped:
  148. error("stripped following ids: " + ", ".join(stripped_ids.keys()))
  149.  
  150. sys.stdout.write("<!DOCTYPE KEEPASSX_DATABASE>\n")
  151. new_doc.write(sys.stdout)
  152. except FatalError:
  153. pass
RAW Paste Data