|
@@ -23,159 +23,12 @@ import time
|
|
from pwman.util.crypto_engine import CryptoEngine
|
|
from pwman.util.crypto_engine import CryptoEngine
|
|
import pwman.data.factory
|
|
import pwman.data.factory
|
|
from pwman.util.callback import CLICallback
|
|
from pwman.util.callback import CLICallback
|
|
-from pwman.data.nodes import NewNode
|
|
|
|
-from pwman.data.tags import Tag
|
|
|
|
-from pwman.data.database import Database, DatabaseException
|
|
|
|
import sqlite3 as sqlite
|
|
import sqlite3 as sqlite
|
|
import pwman.util.config as config
|
|
import pwman.util.config as config
|
|
-import sys
|
|
|
|
-if sys.version_info.major > 2:
|
|
|
|
- import pickle as cPickle
|
|
|
|
-else:
|
|
|
|
- import cPickle
|
|
|
|
|
|
|
|
_NEWVERSION = 0.4
|
|
_NEWVERSION = 0.4
|
|
|
|
|
|
|
|
|
|
-class SQLiteDatabaseReader(Database):
|
|
|
|
- """SQLite Database implementation"""
|
|
|
|
-
|
|
|
|
- def __init__(self, filename=None):
|
|
|
|
- """Initialise SQLitePwmanDatabase instance."""
|
|
|
|
- Database.__init__(self)
|
|
|
|
-
|
|
|
|
- try:
|
|
|
|
- self._filename = config.get_value('Database', 'filename')
|
|
|
|
- except KeyError as e:
|
|
|
|
- raise DatabaseException(
|
|
|
|
- "SQLite: missing config parameter [%s]" % (e))
|
|
|
|
-
|
|
|
|
- def _open(self):
|
|
|
|
- try:
|
|
|
|
- self._con = sqlite.connect(self._filename)
|
|
|
|
- self._cur = self._con.cursor()
|
|
|
|
- self._checktables()
|
|
|
|
- except sqlite.DatabaseError as e:
|
|
|
|
- raise DatabaseException("SQLite: %s" % (e))
|
|
|
|
-
|
|
|
|
- def close(self):
|
|
|
|
- self._cur.close()
|
|
|
|
- self._con.close()
|
|
|
|
-
|
|
|
|
- def getnodes(self, ids):
|
|
|
|
- nodes = []
|
|
|
|
- for i in ids:
|
|
|
|
- sql = "SELECT DATA FROM NODES WHERE ID = ?"
|
|
|
|
- try:
|
|
|
|
- self._cur.execute(sql, [i])
|
|
|
|
-
|
|
|
|
- row = self._cur.fetchone()
|
|
|
|
- if row is not None:
|
|
|
|
- node = cPickle.loads(str(row[0]))
|
|
|
|
- node.set_id(i)
|
|
|
|
- nodes.append(node)
|
|
|
|
- except sqlite.DatabaseError as e:
|
|
|
|
- raise DatabaseException("SQLite: %s" % (e))
|
|
|
|
- return nodes
|
|
|
|
-
|
|
|
|
- def listnodes(self):
|
|
|
|
- sql = ''
|
|
|
|
- params = []
|
|
|
|
- if len(self._filtertags) == 0:
|
|
|
|
- sql = "SELECT ID FROM NODES ORDER BY ID ASC"
|
|
|
|
- else:
|
|
|
|
- first = True
|
|
|
|
- for t in self._filtertags:
|
|
|
|
- if not first:
|
|
|
|
- sql += " INTERSECT "
|
|
|
|
- else:
|
|
|
|
- first = False
|
|
|
|
- sql += ("SELECT NODE FROM LOOKUP LEFT JOIN TAGS "
|
|
|
|
- " ON TAG = TAGS.ID"
|
|
|
|
- " WHERE TAGS.DATA = ? ")
|
|
|
|
-
|
|
|
|
- params.append(cPickle.dumps(t))
|
|
|
|
- try:
|
|
|
|
- self._cur.execute(sql, params)
|
|
|
|
-
|
|
|
|
- ids = []
|
|
|
|
- row = self._cur.fetchone()
|
|
|
|
- while (row is not None):
|
|
|
|
- ids.append(row[0])
|
|
|
|
- row = self._cur.fetchone()
|
|
|
|
- return ids
|
|
|
|
- except sqlite.DatabaseError as e:
|
|
|
|
- raise DatabaseException("SQLite: %s" % (e))
|
|
|
|
-
|
|
|
|
- def _commit(self):
|
|
|
|
- try:
|
|
|
|
- self._con.commit()
|
|
|
|
- except sqlite.DatabaseError as e:
|
|
|
|
- self._con.rollback()
|
|
|
|
- raise DatabaseException(
|
|
|
|
- "SQLite: Error commiting data to db [%s]" % (e))
|
|
|
|
-
|
|
|
|
- def _tagids(self, tags):
|
|
|
|
- ids = []
|
|
|
|
- for t in tags:
|
|
|
|
- sql = "SELECT ID FROM TAGS WHERE DATA = ?"
|
|
|
|
- if not isinstance(t, Tag):
|
|
|
|
- raise DatabaseException(
|
|
|
|
- "Tried to insert foreign object into database [%s]", t)
|
|
|
|
- data = cPickle.dumps(t)
|
|
|
|
-
|
|
|
|
- try:
|
|
|
|
- self._cur.execute(sql, [data])
|
|
|
|
- row = self._cur.fetchone()
|
|
|
|
- if (row is not None):
|
|
|
|
- ids.append(row[0])
|
|
|
|
- else:
|
|
|
|
- sql = "INSERT INTO TAGS(DATA) VALUES(?)"
|
|
|
|
- self._cur.execute(sql, [data])
|
|
|
|
- ids.append(self._cur.lastrowid)
|
|
|
|
- except sqlite.DatabaseError as e:
|
|
|
|
- raise DatabaseException("SQLite: %s" % (e))
|
|
|
|
- return ids
|
|
|
|
-
|
|
|
|
- def _checktables(self):
|
|
|
|
- """ Check if the Pwman tables exist """
|
|
|
|
- self._cur.execute("PRAGMA TABLE_INFO(NODES)")
|
|
|
|
- if (self._cur.fetchone() is None):
|
|
|
|
- # table doesn't exist, create it
|
|
|
|
- # SQLite does have constraints implemented at the moment
|
|
|
|
- # so datatype will just be a string
|
|
|
|
- self._cur.execute("CREATE TABLE NODES "
|
|
|
|
- "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"
|
|
|
|
- "DATA BLOB NOT NULL)")
|
|
|
|
- self._cur.execute("CREATE TABLE TAGS "
|
|
|
|
- "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"
|
|
|
|
- "DATA BLOB NOT NULL UNIQUE)")
|
|
|
|
- self._cur.execute("CREATE TABLE LOOKUP "
|
|
|
|
- "(NODE INTEGER NOT NULL, TAG INTEGER NOT NULL,"
|
|
|
|
- " PRIMARY KEY(NODE, TAG))")
|
|
|
|
-
|
|
|
|
- self._cur.execute("CREATE TABLE KEY "
|
|
|
|
- + "(THEKEY TEXT NOT NULL DEFAULT '')")
|
|
|
|
- self._cur.execute("INSERT INTO KEY VALUES('')")
|
|
|
|
- try:
|
|
|
|
- self._con.commit()
|
|
|
|
- except DatabaseException as e:
|
|
|
|
- self._con.rollback()
|
|
|
|
- raise e
|
|
|
|
-
|
|
|
|
- def loadkey(self):
|
|
|
|
- """
|
|
|
|
- fetch the key to database. the key is also stored
|
|
|
|
- encrypted.
|
|
|
|
- """
|
|
|
|
- self._cur.execute("SELECT THEKEY FROM KEY")
|
|
|
|
- keyrow = self._cur.fetchone()
|
|
|
|
- if (keyrow[0] == ''):
|
|
|
|
- return None
|
|
|
|
- else:
|
|
|
|
- return keyrow[0]
|
|
|
|
-
|
|
|
|
-
|
|
|
|
class DBConverter(object):
|
|
class DBConverter(object):
|
|
"""
|
|
"""
|
|
A general class to provide a base template for converting a database
|
|
A general class to provide a base template for converting a database
|
|
@@ -260,45 +113,6 @@ class DBConverter(object):
|
|
self.print_success()
|
|
self.print_success()
|
|
|
|
|
|
|
|
|
|
-class PwmanConvertDB(DBConverter):
|
|
|
|
- """
|
|
|
|
- Class to migrate from DB in version 0.3 to
|
|
|
|
- DB used in version 0.4 to 0.5.
|
|
|
|
- """
|
|
|
|
- def read_old_db(self):
|
|
|
|
- "read the old db and get all nodes"
|
|
|
|
-
|
|
|
|
- self.db = SQLiteDatabaseReader()
|
|
|
|
- enc = CryptoEngine.get()
|
|
|
|
- enc.callback = CLICallback()
|
|
|
|
- self.db.open()
|
|
|
|
- self.oldnodes = self.db.listnodes()
|
|
|
|
- self.oldnodes = self.db.getnodes(self.oldnodes)
|
|
|
|
-
|
|
|
|
- def save_old_key(self):
|
|
|
|
- enc = CryptoEngine.get()
|
|
|
|
- self.oldkey = enc.get_cryptedkey()
|
|
|
|
- self.newdb.savekey(self.oldkey)
|
|
|
|
-
|
|
|
|
- def convert_nodes(self):
|
|
|
|
- """convert old nodes instances to new format"""
|
|
|
|
- self.NewNodes = []
|
|
|
|
- for node in self.oldnodes:
|
|
|
|
- username = node.get_username()
|
|
|
|
- password = node.get_password()
|
|
|
|
- url = node.get_url()
|
|
|
|
- notes = node.get_notes()
|
|
|
|
- tags = node.get_tags()
|
|
|
|
- tags_strings = [tag._name for tag in tags]
|
|
|
|
- newNode = NewNode()
|
|
|
|
- newNode.username = username
|
|
|
|
- newNode.password = password
|
|
|
|
- newNode.url = url
|
|
|
|
- newNode.notes = notes
|
|
|
|
- newNode.tags = tags_strings
|
|
|
|
- self.NewNodes.append(newNode)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
class PwmanConvertKey(DBConverter):
|
|
class PwmanConvertKey(DBConverter):
|
|
|
|
|
|
def read_old_db(self):
|
|
def read_old_db(self):
|