convertdb.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. #============================================================================
  17. # Copyright (C) 2013 Oz Nahum <nahumoz@gmail.com>
  18. #============================================================================
  19. from __future__ import print_function
  20. import os
  21. import shutil
  22. import os.path
  23. import time
  24. import getpass
  25. from pwman.util.crypto import CryptoEngine
  26. import pwman.data.factory
  27. from pwman.util.callback import Callback
  28. from pwman.data.nodes import NewNode
  29. from pwman.data.tags import Tag
  30. import sys
  31. _NEWVERSION = 0.4
  32. from pwman.data.database import Database, DatabaseException
  33. import sqlite3 as sqlite
  34. import pwman.util.config as config
  35. import cPickle
  36. class SQLiteDatabaseReader(Database):
  37. """SQLite Database implementation"""
  38. def __init__(self):
  39. """Initialise SQLitePwmanDatabase instance."""
  40. Database.__init__(self)
  41. try:
  42. self._filename = config.get_value('Database', 'filename')
  43. except KeyError, e:
  44. raise DatabaseException(
  45. "SQLite: missing parameter [%s]" % (e))
  46. def _open(self):
  47. try:
  48. self._con = sqlite.connect(self._filename)
  49. self._cur = self._con.cursor()
  50. self._checktables()
  51. except sqlite.DatabaseError, e:
  52. raise DatabaseException("SQLite: %s" % (e))
  53. def close(self):
  54. self._cur.close()
  55. self._con.close()
  56. def getnodes(self, ids):
  57. nodes = []
  58. for i in ids:
  59. sql = "SELECT DATA FROM NODES WHERE ID = ?"
  60. try:
  61. self._cur.execute(sql, [i])
  62. row = self._cur.fetchone()
  63. if row is not None:
  64. node = cPickle.loads(str(row[0]))
  65. node.set_id(i)
  66. nodes.append(node)
  67. except sqlite.DatabaseError, e:
  68. raise DatabaseException("SQLite: %s" % (e))
  69. return nodes
  70. def listnodes(self):
  71. sql = ''
  72. params = []
  73. if len(self._filtertags) == 0:
  74. sql = "SELECT ID FROM NODES ORDER BY ID ASC"
  75. else:
  76. first = True
  77. for t in self._filtertags:
  78. if not first:
  79. sql += " INTERSECT "
  80. else:
  81. first = False
  82. sql += ("SELECT NODE FROM LOOKUP LEFT JOIN TAGS "
  83. + " ON TAG = TAGS.ID"
  84. + " WHERE TAGS.DATA = ? ")
  85. params.append(cPickle.dumps(t))
  86. try:
  87. self._cur.execute(sql, params)
  88. ids = []
  89. row = self._cur.fetchone()
  90. while (row is not None):
  91. ids.append(row[0])
  92. row = self._cur.fetchone()
  93. return ids
  94. except sqlite.DatabaseError, e:
  95. raise DatabaseException("SQLite: %s" % (e))
  96. def _commit(self):
  97. try:
  98. self._con.commit()
  99. except sqlite.DatabaseError, e:
  100. self._con.rollback()
  101. raise DatabaseException(
  102. "SQLite: Error commiting data to db [%s]" % (e))
  103. def _tagids(self, tags):
  104. ids = []
  105. for t in tags:
  106. sql = "SELECT ID FROM TAGS WHERE DATA = ?"
  107. if not isinstance(t, Tag):
  108. raise DatabaseException(
  109. "Tried to insert foreign object into database [%s]", t)
  110. data = cPickle.dumps(t)
  111. try:
  112. self._cur.execute(sql, [data])
  113. row = self._cur.fetchone()
  114. if (row is not None):
  115. ids.append(row[0])
  116. else:
  117. sql = "INSERT INTO TAGS(DATA) VALUES(?)"
  118. self._cur.execute(sql, [data])
  119. ids.append(self._cur.lastrowid)
  120. except sqlite.DatabaseError, e:
  121. raise DatabaseException("SQLite: %s" % (e))
  122. return ids
  123. def _checktables(self):
  124. """ Check if the Pwman tables exist """
  125. self._cur.execute("PRAGMA TABLE_INFO(NODES)")
  126. if (self._cur.fetchone() is None):
  127. # table doesn't exist, create it
  128. # SQLite does have constraints implemented at the moment
  129. # so datatype will just be a string
  130. self._cur.execute("CREATE TABLE NODES "
  131. "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"
  132. "DATA BLOB NOT NULL)")
  133. self._cur.execute("CREATE TABLE TAGS "
  134. "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"
  135. "DATA BLOB NOT NULL UNIQUE)")
  136. self._cur.execute("CREATE TABLE LOOKUP "
  137. "(NODE INTEGER NOT NULL, TAG INTEGER NOT NULL,"
  138. " PRIMARY KEY(NODE, TAG))")
  139. self._cur.execute("CREATE TABLE KEY "
  140. + "(THEKEY TEXT NOT NULL DEFAULT '')")
  141. self._cur.execute("INSERT INTO KEY VALUES('')")
  142. try:
  143. self._con.commit()
  144. except DatabaseException, e:
  145. self._con.rollback()
  146. raise e
  147. def loadkey(self):
  148. """
  149. fetch the key to database. the key is also stored
  150. encrypted.
  151. """
  152. self._cur.execute("SELECT THEKEY FROM KEY")
  153. keyrow = self._cur.fetchone()
  154. if (keyrow[0] == ''):
  155. return None
  156. else:
  157. return keyrow[0]
  158. class CLICallback(Callback):
  159. def getinput(self, question):
  160. return raw_input(question)
  161. def getsecret(self, question):
  162. return getpass.getpass(question + ":")
  163. class PwmanConvertDB(object):
  164. """
  165. Class to migrate from DB in version 0.3 to
  166. DB used in later versions.
  167. """
  168. def __init__(self, args, config):
  169. self.dbname = config.get_value('Database', 'filename')
  170. self.dbtype = config.get_value("Database", "type")
  171. self.newdb_name = args.output
  172. def backup_old_db(self):
  173. print("Will convert the following Database: %s " % self.dbname)
  174. if os.path.exists(config.get_value("Database", "filename")):
  175. dbver = pwman.data.factory.check_db_version(self.dbtype)
  176. self.dbver = float(dbver)
  177. backup = '.backup-%s'.join(os.path.splitext(self.dbname)) % \
  178. time.strftime(
  179. '%Y-%m-%d-%H:%M')
  180. shutil.copy(self.dbname, backup)
  181. print("backup created in ", backup)
  182. def read_old_db(self):
  183. "read the old db and get all nodes"
  184. self.db = SQLiteDatabaseReader()
  185. enc = CryptoEngine.get()
  186. enc.set_callback(CLICallback())
  187. self.db.open()
  188. self.oldnodes = self.db.listnodes()
  189. self.oldnodes = self.db.getnodes(self.oldnodes)
  190. def create_new_db(self):
  191. dest = '-newdb'.join(os.path.splitext(self.dbname))
  192. if os.path.exists('-newdb'.join(os.path.splitext(self.dbname))):
  193. print("%s already exists, please move this file!" % dest)
  194. sys.exit(2)
  195. self.newdb_name = '-newdb'.join(os.path.splitext(self.dbname))
  196. self.newdb = pwman.data.factory.create(self.dbtype, _NEWVERSION,
  197. self.newdb_name)
  198. self.newdb._open()
  199. def convert_nodes(self):
  200. """convert old nodes instances to new format"""
  201. self.NewNodes = []
  202. for node in self.oldnodes:
  203. username = node.get_username()
  204. password = node.get_password()
  205. url = node.get_url()
  206. notes = node.get_notes()
  207. tags = node.get_tags()
  208. tags_strings = [tag.get_name() for tag in tags]
  209. newNode = NewNode()
  210. newNode.username = username
  211. newNode.password = password
  212. newNode.url = url
  213. newNode.notes = notes
  214. tags = tags_strings
  215. newNode.tags = tags
  216. self.NewNodes.append(newNode)
  217. def save_new_nodes_to_db(self):
  218. self.newdb.addnodes(self.NewNodes)
  219. self.newdb._commit()
  220. def save_old_key(self):
  221. enc = CryptoEngine.get()
  222. self.oldkey = enc.get_cryptedkey()
  223. self.newdb.savekey(self.oldkey)
  224. def print_success(self):
  225. print("pwman successfully converted the old database to the new "
  226. "format.\nPlease run `pwman3 -d %s` to make sure your password "
  227. "and data are still correct. If you are convinced that no harm "
  228. "was done, update your config file to indicate the permanent "
  229. "location to your new database. If you found errors, please "
  230. "report a bug in Pwman homepage in github. " % self.newdb_name)
  231. def run(self):
  232. self.backup_old_db()
  233. self.read_old_db()
  234. self.create_new_db()
  235. self.convert_nodes()
  236. self.save_new_nodes_to_db()
  237. self.save_old_key()
  238. self.print_success()