sqlite.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute iut 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) 2012, 2013, 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. """SQLite Database implementation."""
  22. from pwman.data.database import Database, DatabaseException
  23. from pwman.data.database import __DB_FORMAT__
  24. import sqlite3 as sqlite
  25. class SQLite(Database):
  26. @classmethod
  27. def check_db_version(cls, fname):
  28. """
  29. check the data base version.
  30. """
  31. con = sqlite.connect(fname)
  32. cur = con.cursor()
  33. cur.execute("PRAGMA TABLE_INFO(DBVERSION)")
  34. row = cur.fetchone()
  35. if not row:
  36. return "0.3" # pragma: no cover
  37. try:
  38. return row[-2]
  39. except IndexError: # pragma: no cover
  40. raise DatabaseException("Something seems fishy with the DB")
  41. def __init__(self, filename, dbformat=__DB_FORMAT__):
  42. """Initialise SQLitePwmanDatabase instance."""
  43. self._filename = filename
  44. self.dbformat = dbformat
  45. self._filtertags = None # TODO: get rid of this property
  46. def _open(self):
  47. self._con = sqlite.connect(self._filename)
  48. self._cur = self._con.cursor()
  49. self._create_tables()
  50. def listnodes(self, filter=None):
  51. """return a list of node ids"""
  52. if not filter:
  53. sql_all = "SELECT ID FROM NODE"
  54. self._cur.execute(sql_all)
  55. ids = self._cur.fetchall()
  56. return [id[0] for id in ids]
  57. else:
  58. tagid = self._get_tag(filter)
  59. if not tagid:
  60. return []
  61. sql_filter = "SELECT NODEID FROM LOOKUP WHERE TAGID = ? "
  62. self._cur.execute(sql_filter, (tagid))
  63. ids = self._cur.fetchall()
  64. return [id[0] for id in ids]
  65. def listtags(self):
  66. self._clean_orphands()
  67. get_tags = "select data from tag"
  68. self._cur.execute(get_tags)
  69. tags = self._cur.fetchall()
  70. if tags:
  71. return [t[0] for t in tags]
  72. return []
  73. def _create_tables(self):
  74. self._cur.execute("PRAGMA TABLE_INFO(NODE)")
  75. if self._cur.fetchone() is not None:
  76. return
  77. self._cur.execute("CREATE TABLE NODE (ID INTEGER PRIMARY KEY "
  78. "AUTOINCREMENT, "
  79. "USER TEXT NOT NULL, "
  80. "PASSWORD TEXT NOT NULL, "
  81. "URL TEXT NOT NULL,"
  82. "NOTES TEXT NOT NULL)")
  83. self._cur.execute("CREATE TABLE TAG"
  84. "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"
  85. "DATA BLOB NOT NULL UNIQUE)")
  86. self._cur.execute("CREATE TABLE LOOKUP ("
  87. "nodeid INTEGER NOT NULL, "
  88. "tagid INTEGER NOT NULL, "
  89. "FOREIGN KEY(nodeid) REFERENCES NODE(ID),"
  90. "FOREIGN KEY(tagid) REFERENCES TAG(ID))")
  91. self._cur.execute("CREATE TABLE CRYPTO"
  92. "(SEED TEXT,"
  93. " DIGEST TEXT)")
  94. # create a table to hold DB version info
  95. self._cur.execute("CREATE TABLE DBVERSION"
  96. "(DB VERSION TEXT NOT NULL DEFAULT '%s')" %
  97. self.dbformat)
  98. self._cur.execute("INSERT INTO DBVERSION VALUES('%s')" %
  99. self.dbformat)
  100. try:
  101. self._con.commit()
  102. except DatabaseException as e: # pragma: no cover
  103. self._con.rollback()
  104. raise e
  105. def fetch_crypto_info(self):
  106. self._cur.execute("SELECT * FROM CRYPTO")
  107. keyrow = self._cur.fetchone()
  108. return keyrow
  109. def save_crypto_info(self, seed, digest):
  110. """save the random seed and the digested key"""
  111. self._cur.execute("DELETE FROM CRYPTO")
  112. self._cur.execute("INSERT INTO CRYPTO VALUES(?, ?)", [seed, digest])
  113. self._con.commit()
  114. def add_node(self, node):
  115. sql = ("INSERT INTO NODE(USER, PASSWORD, URL, NOTES)"
  116. "VALUES(?, ?, ?, ?)")
  117. node_tags = list(node)
  118. node, tags = node_tags[:4], node_tags[-1]
  119. self._cur.execute(sql, (node))
  120. self._setnodetags(self._cur.lastrowid, tags)
  121. self._con.commit()
  122. def _get_tag(self, tagcipher):
  123. sql_search = "SELECT ID FROM TAG WHERE DATA = ?"
  124. self._cur.execute(sql_search, ([tagcipher]))
  125. rv = self._cur.fetchone()
  126. return rv
  127. def _get_or_create_tag(self, tagcipher):
  128. rv = self._get_tag(tagcipher)
  129. if rv:
  130. return rv[0]
  131. else:
  132. sql_insert = "INSERT INTO TAG(DATA) VALUES(?)"
  133. self._cur.execute(sql_insert, ([tagcipher]))
  134. return self._cur.lastrowid
  135. def _update_tag_lookup(self, nodeid, tid):
  136. sql_lookup = "INSERT INTO LOOKUP(nodeid, tagid) VALUES(?,?)"
  137. self._cur.execute(sql_lookup, (nodeid, tid))
  138. self._con.commit()
  139. def _setnodetags(self, nodeid, tags):
  140. for tag in tags:
  141. tid = self._get_or_create_tag(tag)
  142. self._update_tag_lookup(nodeid, tid)
  143. def _get_node_tags(self, node):
  144. sql = "SELECT tagid FROM LOOKUP WHERE NODEID = ?"
  145. tagids = self._cur.execute(sql, (str(node[0]),)).fetchall()
  146. sql = ("SELECT DATA FROM TAG WHERE ID IN (%s)"
  147. "" % ','.join('?'*len(tagids)))
  148. tagids = [str(id[0]) for id in tagids]
  149. self._cur.execute(sql, (tagids))
  150. tags = self._cur.fetchall()
  151. for t in tags:
  152. yield t[0]
  153. def getnodes(self, ids):
  154. """
  155. get nodes as raw ciphertext
  156. """
  157. sql = "SELECT * FROM NODE WHERE ID IN (%s)" % ','.join('?'*len(ids))
  158. self._cur.execute(sql, (ids))
  159. nodes = self._cur.fetchall()
  160. nodes_w_tags = []
  161. for node in nodes:
  162. tags = list(self._get_node_tags(node))
  163. nodes_w_tags.append(list(node) + tags)
  164. return nodes_w_tags
  165. def editnode(self, nid, **kwargs):
  166. tags = kwargs.pop('tags', None)
  167. sql = ("UPDATE NODE SET %s WHERE ID = ? "
  168. "" % ','.join('%s=?' % k for k in list(kwargs)))
  169. self._cur.execute(sql, (list(kwargs.values()) + [nid]))
  170. if tags:
  171. # update all old node entries in lookup
  172. # create new entries
  173. # clean all old tags
  174. sql_clean = "DELETE FROM LOOKUP WHERE NODEID=?"
  175. self._cur.execute(sql_clean, (str(nid),))
  176. self._setnodetags(nid, tags)
  177. self._con.commit()
  178. def removenodes(self, nids):
  179. sql_rm = "delete from node where id in (%s)" % ','.join('?'*len(nids))
  180. self._cur.execute(sql_rm, (nids))
  181. def _clean_orphands(self):
  182. clean = ("delete from tag where not exists "
  183. "(select 'x' from lookup l where l.tagid = tag.id)")
  184. self._cur.execute(clean)
  185. self._con.commit()
  186. def savekey(self, key):
  187. salt, digest = key.split('$6$')
  188. sql = "INSERT INTO CRYPTO(SEED, DIGEST) VALUES(?,?)"
  189. self._cur.execute("DELETE FROM CRYPTO")
  190. self._cur.execute(sql, (salt, digest))
  191. self._digest = digest.encode('utf-8')
  192. self._salt = salt.encode('utf-8')
  193. self._con.commit()
  194. def loadkey(self):
  195. # TODO: rename this method!
  196. """
  197. return _keycrypted
  198. """
  199. sql = "SELECT * FROM CRYPTO"
  200. try:
  201. seed, digest = self._cur.execute(sql).fetchone()
  202. return seed + u'$6$' + digest
  203. except TypeError:
  204. return None
  205. def close(self):
  206. self._clean_orphands()
  207. self._cur.close()
  208. self._con.close()