postgresql.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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) 2015 Oz Nahum <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. """Postgresql Database implementation."""
  22. import sys
  23. if sys.version_info.major > 2: # pragma: no cover
  24. from urllib.parse import urlparse
  25. else:
  26. from urlparse import urlparse
  27. import psycopg2 as pg
  28. from pwman.data.database import Database, DatabaseException, __DB_FORMAT__
  29. class PostgresqlDatabase(Database):
  30. """
  31. Postgresql Database implementation
  32. This assumes that your database admin has created a pwman database
  33. for you and shared the user name and password with you.
  34. This driver send no clear text on wire. ONLY excrypted stuff is sent
  35. between the client and the server.
  36. Encryption and decryption are happening on your localhost, not on
  37. the Postgresql server.
  38. """
  39. @classmethod
  40. def check_db_version(cls, dburi):
  41. """
  42. Check the database version
  43. """
  44. con = pg.connect(dburi.geturl())
  45. cur = con.cursor()
  46. try:
  47. cur.execute("SELECT VERSION from DBVERSION")
  48. version = cur.fetchone()
  49. con.close()
  50. cur.close()
  51. return version
  52. except pg.ProgrammingError:
  53. con.rollback()
  54. raise DatabaseException("Something seems fishy with the DB")
  55. def __init__(self, pgsqluri, dbformat=__DB_FORMAT__):
  56. """
  57. Initialise PostgresqlDatabase instance.
  58. """
  59. self._pgsqluri = pgsqluri
  60. self.dbversion = dbformat
  61. def _open(self):
  62. u = urlparse(self._pgsqluri)
  63. self._con = pg.connect(database=u.path[1:], user=u.username,
  64. password=u.password, host=u.hostname)
  65. self._cur = self._con.cursor()
  66. self._create_tables()
  67. def listnodes(self, filter=None):
  68. if not filter:
  69. sql_all = "SELECT ID FROM NODE"
  70. self._cur.execute(sql_all)
  71. ids = self._cur.fetchall()
  72. return [id[0] for id in ids]
  73. else:
  74. tagid = self._get_tag(filter)
  75. if not tagid:
  76. return [] # pragma: no cover
  77. sql_filter = "SELECT NODEID FROM LOOKUP WHERE TAGID = %s "
  78. self._cur.execute(sql_filter, (tagid))
  79. self._con.commit()
  80. ids = self._cur.fetchall()
  81. return [id[0] for id in ids]
  82. def listtags(self):
  83. self._clean_orphans()
  84. get_tags = "select data from tag"
  85. self._cur.execute(get_tags)
  86. tags = self._cur.fetchall()
  87. if tags:
  88. return [t[0] for t in tags]
  89. return [] # pragma: no cover
  90. def _create_tables(self):
  91. try:
  92. self._cur.execute("SELECT 1 from DBVERSION")
  93. version = self._cur.fetchone()
  94. if version:
  95. return
  96. except pg.ProgrammingError:
  97. self._con.rollback()
  98. try:
  99. self._cur.execute("CREATE TABLE NODE(ID SERIAL PRIMARY KEY, "
  100. "USERNAME TEXT NOT NULL, "
  101. "PASSWORD TEXT NOT NULL, "
  102. "URL TEXT NOT NULL, "
  103. "NOTES TEXT NOT NULL"
  104. ")")
  105. self._cur.execute("CREATE TABLE TAG"
  106. "(ID SERIAL PRIMARY KEY,"
  107. "DATA TEXT NOT NULL UNIQUE)")
  108. self._cur.execute("CREATE TABLE LOOKUP ("
  109. "nodeid SERIAL REFERENCES NODE(ID),"
  110. "tagid SERIAL REFERENCES TAG(ID)"
  111. ")")
  112. self._cur.execute("CREATE TABLE CRYPTO "
  113. "(SEED TEXT, DIGEST TEXT)")
  114. self._cur.execute("CREATE TABLE DBVERSION("
  115. "VERSION TEXT NOT NULL DEFAULT {}"
  116. ")".format(__DB_FORMAT__))
  117. self._cur.execute("INSERT INTO DBVERSION VALUES(%s)",
  118. (self.dbversion,))
  119. self._con.commit()
  120. except pg.ProgrammingError: # pragma: no cover
  121. self._con.rollback()
  122. def fetch_crypto_info(self):
  123. self._cur.execute("SELECT * FROM CRYPTO")
  124. row = self._cur.fetchone()
  125. return row
  126. def save_crypto_info(self, seed, digest):
  127. """save the random seed and the digested key"""
  128. self._cur.execute("DELETE FROM CRYPTO")
  129. self._cur.execute("INSERT INTO CRYPTO VALUES(%s, %s)", (seed, digest))
  130. self._con.commit()
  131. def add_node(self, node):
  132. sql = ("INSERT INTO NODE(USERNAME, PASSWORD, URL, NOTES)"
  133. "VALUES(%s, %s, %s, %s) RETURNING ID")
  134. node_tags = list(node)
  135. node, tags = node_tags[:4], node_tags[-1]
  136. self._cur.execute(sql, (node))
  137. nid = self._cur.fetchone()[0]
  138. self._setnodetags(nid, tags)
  139. self._con.commit()
  140. def _get_tag(self, tagcipher):
  141. sql_search = "SELECT ID FROM TAG WHERE DATA = %s"
  142. self._cur.execute(sql_search, ([tagcipher]))
  143. rv = self._cur.fetchone()
  144. return rv
  145. def _get_or_create_tag(self, tagcipher):
  146. rv = self._get_tag(tagcipher)
  147. if rv:
  148. return rv[0]
  149. else:
  150. sql_insert = "INSERT INTO TAG(DATA) VALUES(%s) RETURNING ID"
  151. self._cur.execute(sql_insert, ([tagcipher]))
  152. rid = self._cur.fetchone()[0]
  153. return rid
  154. def _update_tag_lookup(self, nodeid, tid):
  155. sql_lookup = "INSERT INTO LOOKUP(nodeid, tagid) VALUES(%s, %s)"
  156. self._cur.execute(sql_lookup, (nodeid, tid))
  157. self._con.commit()
  158. def _setnodetags(self, nodeid, tags):
  159. for tag in tags:
  160. tid = self._get_or_create_tag(tag)
  161. self._update_tag_lookup(nodeid, tid)
  162. def _get_node_tags(self, node): # pragma: no cover
  163. sql = "SELECT tagid FROM LOOKUP WHERE NODEID = %s"
  164. self._cur.execute(sql, (str(node[0]),))
  165. tagids = self._cur.fetchall()
  166. sql = ("SELECT DATA FROM TAG WHERE ID IN (%s)"
  167. "" % ','.join(['%s']*len(tagids)))
  168. tagids = [str(id[0]) for id in tagids]
  169. self._cur.execute(sql, (tagids))
  170. tags = self._cur.fetchall()
  171. for t in tags:
  172. yield t[0]
  173. def getnodes(self, ids):
  174. sql = "SELECT * FROM NODE WHERE ID IN ({})".format(','.join('%s' for
  175. i in ids))
  176. self._cur.execute(sql, (ids))
  177. nodes = self._cur.fetchall()
  178. nodes_w_tags = []
  179. for node in nodes:
  180. tags = list(self._get_node_tags(node))
  181. nodes_w_tags.append(list(node) + tags)
  182. return nodes_w_tags
  183. def editnode(self, nid, **kwargs): # pragma: no cover
  184. tags = kwargs.pop('tags', None)
  185. sql = ("UPDATE NODE SET %s WHERE ID = %%s "
  186. "" % ','.join('%s=%%s' % k for k in list(kwargs)))
  187. self._cur.execute(sql, (list(kwargs.values()) + [nid]))
  188. if tags:
  189. # update all old node entries in lookup
  190. # create new entries
  191. # clean all old tags
  192. sql_clean = "DELETE FROM LOOKUP WHERE NODEID=?"
  193. self._cur.execute(sql_clean, (str(nid),))
  194. self._setnodetags(nid, tags)
  195. self._con.commit()
  196. def removenodes(self, nid):
  197. # shall we do this also in the sqlite driver?
  198. sql_clean = "DELETE FROM LOOKUP WHERE NODEID=%s"
  199. self._cur.execute(sql_clean, nid)
  200. sql_rm = "delete from node where id = %s"
  201. self._cur.execute(sql_rm, nid)
  202. self._con.commit()
  203. def _clean_orphans(self):
  204. clean = ("delete from tag where not exists "
  205. "(select 'x' from lookup l where l.tagid = tag.id)")
  206. self._cur.execute(clean)
  207. self._con.commit()
  208. def savekey(self, key):
  209. salt, digest = key.split('$6$')
  210. sql = "INSERT INTO CRYPTO(SEED, DIGEST) VALUES(%s,%s)"
  211. self._cur.execute("DELETE FROM CRYPTO")
  212. self._cur.execute(sql, (salt, digest))
  213. self._digest = digest.encode('utf-8')
  214. self._salt = salt.encode('utf-8')
  215. self._con.commit()
  216. def loadkey(self):
  217. sql = "SELECT * FROM CRYPTO"
  218. try:
  219. self._cur.execute(sql)
  220. seed, digest = self._cur.fetchone()
  221. return seed + u'$6$' + digest
  222. except TypeError: # pragma: no cover
  223. return None
  224. def close(self): # pragma: no cover
  225. self._clean_orphans()
  226. self._cur.close()
  227. self._con.close()