convertdb.py 4.2 KB

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