convertdb.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 time
  23. from pwman.util.crypto_engine import CryptoEngine
  24. import pwman.data.factory
  25. from pwman.util.callback import CLICallback
  26. import sqlite3 as sqlite
  27. import pwman.util.config as config
  28. _NEWVERSION = 0.4
  29. class DBConverter(object):
  30. """
  31. A general class to provide a base template for converting a database
  32. from one version to another
  33. """
  34. def __init__(self, args, config):
  35. self.dbname = config.get_value('Database', 'filename')
  36. self.dbtype = config.get_value("Database", "type")
  37. if not args.output:
  38. self.newdb_name = self.dbname
  39. else:
  40. self.newdb_name = '.new-%s'.join(os.path.splitext(self.dbname))
  41. @staticmethod
  42. def detect_db_version(filename):
  43. """
  44. This method should accept a pwman db file name, and it should try to
  45. detect which database version it is.
  46. """
  47. con = sqlite.connect(filename)
  48. cur = con.cursor()
  49. cur.execute("SELECT DBVERSION FROM DBVERSION")
  50. row = cur.fetchone()
  51. if not row:
  52. return "0.3"
  53. else:
  54. return row[0]
  55. @staticmethod
  56. def invoke_converter(dbversion, future_version):
  57. """
  58. this method should accept the two parameters and according to them
  59. invoke the right converter
  60. """
  61. pass
  62. def backup_old_db(self):
  63. print("Will convert the following Database: %s " % self.dbname)
  64. if os.path.exists(config.get_value("Database", "filename")):
  65. dbver = pwman.data.factory.check_db_version(self.dbtype)
  66. self.dbver = float(dbver)
  67. backup = '.backup-%s'.join(os.path.splitext(self.dbname)) % \
  68. time.strftime(
  69. '%Y-%m-%d-%H:%M')
  70. shutil.copy(self.dbname, backup)
  71. print("backup created in ", backup)
  72. def read_old_db(self):
  73. raise Exception("This methodod should be overriden")
  74. def create_new_db(self, new_version=_NEWVERSION):
  75. if os.path.exists(self.newdb_name):
  76. os.remove(self.newdb_name)
  77. self.newdb = pwman.data.factory.create(self.dbtype, new_version,
  78. self.newdb_name)
  79. self.newdb._open()
  80. def convert_nodes(self):
  81. raise Exception("This methodod should be overriden")
  82. def save_new_nodes_to_db(self):
  83. self.newdb.addnodes(self.NewNodes)
  84. self.newdb._commit()
  85. def save_old_key(self):
  86. raise Exception("This methodod should be overriden")
  87. def print_success(self):
  88. print("pwman successfully converted the old database to the new "
  89. "format.\nPlease run `pwman3 -d %s` to make sure your password "
  90. "and data are still correct. If you found errors, please "
  91. "report a bug in Pwman homepage in github. " % self.newdb_name)
  92. def run(self):
  93. self.backup_old_db()
  94. self.read_old_db()
  95. self.create_new_db()
  96. self.convert_nodes()
  97. self.save_new_nodes_to_db()
  98. self.save_old_key()
  99. self.print_success()
  100. class PwmanConvertKey(DBConverter):
  101. def read_old_db(self):
  102. enc = CryptoEngine.get()
  103. enc.callback = CLICallback()
  104. self.db.open()
  105. self.oldnodes = self.db.listnodes()
  106. self.oldnodes = self.db.getnodes(self.oldnodes)
  107. def save_old_key(self):
  108. CryptoEngine._instance = None
  109. enc = CryptoEngine.get(0.5)
  110. self.oldkey = enc.get_cryptedkey()
  111. self.newdb.savekey(self.oldkey)