Guest User

Untitled

a guest
Nov 30th, 2010
355
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.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. "database":43
  44. }
  45.  
  46. try:
  47. if len(args) != 1:
  48. error("requires one argument")
  49.  
  50. old_filename = args[0]
  51.  
  52. old_doc = ElementTree.ElementTree(file=old_filename)
  53. old_root = old_doc.getroot()
  54. new_root = ElementTree.Element("database")
  55. new_doc = ElementTree.ElementTree(new_root)
  56.  
  57. def parse_folder(old_element, new_parent, group_depth):
  58. description = None
  59. desc_append = ""
  60. for item in old_element.findall("*"):
  61. if item.tag == "entry":
  62. if item.get("type") == "folder":
  63. element = ElementTree.SubElement(new_parent, "group")
  64. ElementTree.SubElement(element, "icon").text = "48"
  65. element.tail = "\n"
  66. parse_folder(item, element, group_depth + 1)
  67. else:
  68. if group_depth == 0:
  69. fake_group = ElementTree.SubElement(new_parent, "group")
  70. ElementTree.SubElement(fake_group, "icon").text = "48"
  71. ElementTree.SubElement(fake_group, "title").text = "Misc"
  72. fake_group.tail = "\n"
  73. new_parent = fake_group
  74. group_depth += 1
  75.  
  76. element = ElementTree.SubElement(new_parent, "entry")
  77. ElementTree.SubElement(element, "icon").text = str(from_type_to_icon[item.get("type")])
  78. element.tail = "\n"
  79. parse_folder(item, element, group_depth)
  80. else:
  81. element = new_parent
  82. if item.tag == "name":
  83. ElementTree.SubElement(element, "title").text = item.text
  84. elif item.tag == "updated":
  85. lastmod_time = time.strftime("%Y-%m-%dT%H:%M:%S",
  86. time.localtime(int(item.text)))
  87. ElementTree.SubElement(element, "lastmod").text = lastmod_time
  88. ElementTree.SubElement(element, "lastaccess").text = lastmod_time
  89. ElementTree.SubElement(element, "creation").text = lastmod_time
  90. elif item.tag == "field":
  91. id = item.get("id")
  92. if id == "generic-username":
  93. ElementTree.SubElement(element, "username").text = item.text
  94. elif id == "generic-password":
  95. ElementTree.SubElement(element, "password").text = item.text
  96. elif id in ("generic-hostname", "generic-url"):
  97. ElementTree.SubElement(element, "url").text = item.text
  98.  
  99. elif id == "generic-email":
  100. if item.text is not None:
  101. desc_append += "e-mail: " + item.text + "\n"
  102. elif id == "generic-port":
  103. if item.text is not None:
  104. desc_append += "Port: " + item.text + "\n"
  105. elif id == "generic-domain":
  106. if item.text is not None:
  107. ElementTree.SubElement(element, "url").text = item.text
  108. elif id == "creditcard-cardtype":
  109. if item.text is not None:
  110. desc_append += "Type: " + item.text + "\n"
  111.  
  112.  
  113. elif id == "creditcard-cardnumber":
  114. if item.text is not None:
  115. desc_append += "Card Number: " + item.text + "\n"
  116. elif id == "creditcard-expirydate":
  117. if item.text is not None:
  118. desc_append += "Card Expiry: " + item.text + "\n"
  119. elif id == "creditcard-ccv":
  120. if item.text is not None:
  121. desc_append += "Card CCV: " + item.text + "\n"
  122. elif id == "generic-pin":
  123. if item.text is not None:
  124. desc_append += "PIN: " + item.text + "\n"
  125. elif id == "phone-phonenumber":
  126. ElementTree.SubElement(element, "password").text = item.text
  127. elif id == "generic-code":
  128. ElementTree.SubElement(element, "password").text = item.text
  129. elif id in ("creditcard-cardtype", "generic-location", "generic-certificate", "generic-keyfile"):
  130. stripped_ids[id] = 1
  131. elif id == "generic-database":
  132. ElementTree.SubElement(element, "title").text = item.text
  133.  
  134. else:
  135. error("unexpected field id: " + id)
  136. elif item.tag == "description":
  137. description = item.text
  138. else:
  139. error("unexpected tag: " + item.tag)
  140. if description is not None or len(desc_append) > 0:
  141. if len(desc_append) == 0:
  142. ElementTree.SubElement(element, "comment").text = description
  143. elif description is None:
  144. ElementTree.SubElement(element, "comment").text = desc_append
  145. else:
  146. ElementTree.SubElement(element, "comment").text = description + "\n" + desc_append
  147.  
  148.  
  149. parse_folder(old_root, new_root, 0)
  150.  
  151. if len(stripped_ids) and not options.ignore_stripped:
  152. error("stripped following ids: " + ", ".join(stripped_ids.keys()))
  153.  
  154. sys.stdout.write("<!DOCTYPE KEEPASSX_DATABASE>\n")
  155. new_doc.write(sys.stdout)
  156. except FatalError:
  157. pass
RAW Paste Data