aboutsummaryrefslogtreecommitdiffstats
path: root/libpwman/database.py
blob: 5ad1cc03e6af7c8b8609aa7dd655efe28f236006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# -*- coding: utf-8 -*-
"""
#
# Simple password manager
# Encrypted database
#
# Copyright (c) 2011-2023 Michael Büsch <m@bues.ch>
# Licensed under the GNU/GPL version 2 or later.
#
"""

from libpwman.cryptsql import *
from libpwman.exception import *
from libpwman.util import *
import libpwman.otp

import csv
import io
import os
import pathlib
import sys
from copy import deepcopy
from dataclasses import dataclass

__all__ = [
	"CSQLError",
	"PWManDatabase",
	"PWManEntry",
	"PWManEntryAttr",
	"PWManEntryBulk",
	"PWManEntryTOTP",
	"getDefaultDatabase",
]

def getDefaultDatabase():
	"""Get the default database path.
	Returns a pathlib.Path() instance.
	"""
	db = os.getenv("PWMAN_DATABASE")
	if db:
		return pathlib.Path(db)
	home = pathlib.Path.home()
	if home:
		return home / ".pwman.db"
	return pathlib.Path(".pwman.db")

@dataclass
class PWManEntry:
	"""Database entry data structure.
	"""
	category	: str
	title		: str
	user		: str = None
	pw		: str = None
	entryId		: int = None

@dataclass
class PWManEntryAttr:
	"""Entry attribute data structure.
	"""
	name		: str
	data		: str = None
	entry		: PWManEntry = None
	attrId		: int = None

@dataclass
class PWManEntryBulk:
	"""Entry bulk-data data structure.
	"""
	data		: str = None
	entry		: PWManEntry = None
	bulkId		: int = None

@dataclass
class PWManEntryTOTP:
	"""Entry TOTP-data data structure.
	"""
	key		: str
	digits		: int = 6
	hmacHash	: str = "SHA1"
	entry		: PWManEntry = None
	totpId		: int = None

	def generate(self):
		return libpwman.otp.totp(key=self.key,
					 nrDigits=self.digits,
					 hmacHash=self.hmacHash)

class PWManDatabase(CryptSQL):
	"""Encrypted pwman database.
	"""

	DB_TYPE	= "PWMan database"
	DB_VER	= ("0", "1")

	def __init__(self, filename, passphrase, key=None, readOnly=True, silent=False):
		"""filename: Path to the database file.
		             If it does not exist, a new file is created.
		passphrase: The passphrase string for the database file.
		key: An optional key to use instead of the passphrase. Don't use it.
		readOnly: Open the filename read-only. Commits will raise an exception.
		silent: Do not print information messages to the console.
		"""
		try:
			super().__init__(readOnly=readOnly)
			self.__silent = silent
			self.__dirty = False
			self.__openFile(filename, passphrase, key)
		except (CSQLError) as e:
			raise PWManError(str(e))

	def __openFile(self, filename, passphrase, key):
		"""Open the database file and parse the contents.
		"""
		super().setPassphrase(passphrase)
		self.setKey(key)
		self.open(filename)
		self.__setDirty(False)
		initDBVer = False
		if self.sqlIsEmpty():
			initDBVer = True
		else:
			dbType = self.getGlobalAttr("db_type")
			dbVer = self.getGlobalAttr("db_version")
			if dbType is None and dbVer is None: # Compat v0
				dbType = self.DB_TYPE
				dbVer = self.DB_VER[0]
			if (dbType != self.DB_TYPE or
			    dbVer not in self.DB_VER):
				raise PWManError("Unsupported database version '%s / %s'. "
					"Expected '%s / %s'" % (
					str(dbType),
					str(dbVer),
					self.DB_TYPE,
					", ".join(self.DB_VER)))
			if dbVer != self.DB_VER[-1]:
				self.__migrateVersion(dbVer)
				initDBVer = True
		self.__initTables()
		if initDBVer:
			self.setGlobalAttr("db_type", self.DB_TYPE, setDirty=False)
			self.setGlobalAttr("db_version", self.DB_VER[-1], setDirty=False)

	def __migrateVersion(self, dbVer):
		"""Migrate the database format to the latest version.
		"""
		if dbVer == self.DB_VER[0]:
			if not self.__silent:
				print("Migrating database from version %s to version %s..." % (
				      dbVer, self.DB_VER[-1]),
				      file=sys.stderr)

			self.__initTables()

			c = self.sqlExec("SELECT DISTINCT category FROM pw ORDER BY category;")
			categories = c.fetchAll()
			for (category, ) in categories:
				c = self.sqlExec("SELECT title FROM pw WHERE category=? ORDER BY title;",
						 (category,))
				titles = c.fetchAll()
				for (title, ) in titles:
					c = self.sqlExec("SELECT category, title, user, pw, bulk FROM pw "
							 "WHERE category=? AND title=? "
							 "LIMIT 1;",
							 (category, title))
					data = c.fetchOne()
					if not data:
						continue
					c = self.sqlExec("INSERT INTO entries(category, title, user, pw) "
							 "VALUES(?,?,?,?);",
							 (data[0], data[1], data[2], data[3]))
					entryId = c.lastRowID()
					if data[4]:
						c = self.sqlExec("INSERT INTO bulk(entry, data) "
								 "VALUES(?,?);",
								 (entryId, data[4]))
			c = self.sqlExec("SELECT name, data FROM info;")
			infos = c.fetchAll()
			for name, data in infos:
				c = self.sqlExec("INSERT INTO globalattr(name, data) VALUES(?,?);",
						 (name, data))
			c = self.sqlExec("DROP TABLE IF EXISTS pw;")
			c = self.sqlExec("DROP TABLE IF EXISTS info;")
			self.sqlVacuum()
		else:
			assert(0)

	def __initTables(self):
		"""Create the SQL tables, if they don't exist.
		"""
		c = self.sqlExecScript("""
			CREATE TABLE IF NOT EXISTS
			globalattr(id INTEGER PRIMARY KEY AUTOINCREMENT,
				   name TEXT, data TEXT);

			CREATE TABLE IF NOT EXISTS
			entries(id INTEGER PRIMARY KEY AUTOINCREMENT,
				category TEXT, title TEXT, user TEXT, pw TEXT);

			CREATE TABLE IF NOT EXISTS
			bulk(id INTEGER PRIMARY KEY AUTOINCREMENT,
			     entry INTEGER, data TEXT);

			CREATE TABLE IF NOT EXISTS
			entryattr(id INTEGER PRIMARY KEY AUTOINCREMENT,
				  entry INTEGER, name TEXT, data TEXT);

			CREATE TABLE IF NOT EXISTS
			totp(id INTEGER PRIMARY KEY AUTOINCREMENT,
			     entry INTEGER, key TEXT, digits INTEGER, hash TEXT);
		""")

	def __garbageCollect(self):
		"""Remove rows from the SQL database that are not needed anymore.
		"""
		# Do not use sqlExecScript here, as that would commit transactions.
		c = self.sqlExec("DELETE FROM bulk WHERE entry NOT IN (SELECT id FROM entries);")
		c = self.sqlExec("DELETE FROM entryattr WHERE entry NOT IN (SELECT id FROM entries);")
		c = self.sqlExec("DELETE FROM totp WHERE entry NOT IN (SELECT id FROM entries);")

	def setPassphrase(self, passphrase):
		super().setPassphrase(passphrase)
		self.__setDirty()

	def categoryExists(self, category):
		"""Returns True, if a category exists in the database.
		category: The name string of the category.
		"""
		c = self.sqlExec("SELECT EXISTS(SELECT 1 FROM entries "
				 "WHERE category=? "
				 "LIMIT 1);",
				 (category,))
		data = c.fetchOne()
		return bool(data) and bool(data[0])

	def getCategoryNames(self):
		"""Get all category names in the database.
		Returns a sorted list of strings.
		"""
		c = self.sqlExec("SELECT DISTINCT category FROM entries "
				 "ORDER BY category;")
		return [ data[0] for data in c.fetchAll() ]

	def getEntryTitles(self, category):
		"""Get all titles from one category in the database.
		category: The category name string.
		Returns a sorted list of strings.
		"""
		c = self.sqlExec("SELECT title FROM entries "
				 "WHERE category=? "
				 "ORDER BY title;",
				 (category,))
		return [ data[0] for data in c.fetchAll() ]

	def getEntry(self, category, title):
		"""Get an entry from the database.
		category: The name string of the category to get an entry from.
		title: The title string of the entry to get.
		Returns a PWManEntry() instance.
		"""
		c = self.sqlExec("SELECT id, category, title, user, pw FROM entries "
				 "WHERE category=? AND title=? "
				 "LIMIT 1;",
				 (category,
				  title))
		data = c.fetchOne()
		if not data:
			return None
		return PWManEntry(category=data[1],
				  title=data[2],
				  user=data[3],
				  pw=data[4],
				  entryId=data[0])

	def findEntries(self, pattern,
			useRegexp=False,
			search=True,
			inCategory=None,
			matchCategory=False,
			matchTitle=False,
			matchUser=False,
			matchPw=False,
			matchBulk=False,
			matchAttrName=False,
			matchAttrData=False):
		"""Search the database for entries that match a pattern.
		useRegexp: If True, then the pattern is a regular expression string.
		           If False, then the pattern is a SQL LIKE pattern string.
		inCategory: If specified as non-zero length string, then only search
		            the category with this name.
		matchCategory: Match the pattern to the category name string of an entry.
		matchTitle: Match the pattern to the title string of an entry.
		matchUser: Match the pattern to the user string of an entry.
		matchPw: Match the pattern to the password string of an entry.
		matchBulk: Match the pattern to the bulk data string of an entry.
		matchAttrName: Match the pattern to all attribute name strings of an entry.
		matchAttrData: Match the pattern to all attribute data strings of an entry.
		Returns a list of PWManEntry() instances that match the pattern.
		"""
		if useRegexp:
			self.setRegexpFlags(search=search,
					    ignoreCase=True,
					    multiLine=True,
					    dotAll=True)
		else:
			if search:
				pattern = "%" + pattern + "%"

		def dump(sql, params):
			pass
#			print(sql, "\nparams =", params)

		def match(leftHand):
			if useRegexp:
				return "%s REGEXP ?" % leftHand
			return "%s LIKE ?" % leftHand

		IDs = set()

		if matchCategory or matchTitle or matchUser or matchPw:
			conditions = []
			if matchCategory:
				conditions.append( (match("entries.category"), pattern) )
			if matchTitle:
				conditions.append( (match("entries.title"), pattern) )
			if matchUser:
				conditions.append( (match("entries.user"), pattern) )
			if matchPw:
				conditions.append( (match("entries.pw"), pattern) )
			sql = "SELECT id FROM entries WHERE "
			params = []
			if inCategory:
				sql += "category=? AND "
				params.append(inCategory)
			sql += "( " + (" OR ".join(c[0] for c in conditions)) + " );"
			params.extend(c[1] for c in conditions)
			dump(sql, params)
			c = self.sqlExec(sql, params)
			IDs.update(entryId[0] for entryId in (c.fetchAll() or []))

		if matchBulk:
			conditions = [ (match("bulk.data"), pattern) ]
			sql = "SELECT entries.id "\
			      "FROM entries, bulk "\
			      "WHERE bulk.entry = entries.id AND "
			params = []
			if inCategory:
				sql += "entries.category = ? AND "
				params.append(inCategory)
			sql += match("bulk.data") + ";"
			params.append(pattern)
			dump(sql, params)
			c = self.sqlExec(sql, params)
			IDs.update(entryId[0] for entryId in (c.fetchAll() or []))

		if matchAttrName or matchAttrData:
			conditions = []
			if matchAttrName:
				conditions.append( (match("entryattr.name"), pattern) )
			if matchAttrData:
				conditions.append( (match("entryattr.data"), pattern) )
			sql = "SELECT entries.id "\
			      "FROM entries, entryattr "\
			      "WHERE entryattr.entry = entries.id AND "
			params = []
			if inCategory:
				sql += "entries.category = ? AND "
				params.append(inCategory)
			sql += "( " + (" OR ".join(c[0] for c in conditions)) + " );"
			params.extend(c[1] for c in conditions)
			dump(sql, params)
			c = self.sqlExec(sql, params)
			IDs.update(entryId[0] for entryId in (c.fetchAll() or []))

		if not IDs:
			return []
		IDs = sorted(IDs) # stable sorting

		sql = "SELECT entries.id, entries.category, "\
		      "entries.title, entries.user, entries.pw "\
		      "FROM entries "\
		      "WHERE entries.id IN ( "
		sql += ", ".join("?" for ID in IDs)
		sql += " ) "
		sql += "ORDER BY entries.category, entries.title;"
		params = [ str(ID) for ID in IDs ]
		dump(sql, params)
		c = self.sqlExec(sql, params)
		dataSet = c.fetchAll()
		if not dataSet:
			return []
		return [ PWManEntry(category=data[1],
				    title=data[2],
				    user=data[3],
				    pw=data[4],
				    entryId=data[0])
			 for data in dataSet ]

	def entryExists(self, category, title):
		"""Returns True, if an entry exists in the database.
		category: The name string of the category.
		title: The title string of the entry.
		"""
		c = self.sqlExec("SELECT EXISTS(SELECT 1 FROM entries "
				 "WHERE category=? AND title=? "
				 "LIMIT 1);",
				 (category,
				  title))
		data = c.fetchOne()
		return bool(data) and bool(data[0])

	def addEntry(self, entry):
		"""Create a new entry in the database.
		entry: A PWManEntry() instance.
		"""
		if self.entryExists(entry.category, entry.title):
			raise PWManError("Entry does already exist")
		c = self.sqlExec("INSERT INTO entries(category, title, user, pw) "
				 "VALUES(?,?,?,?);",
				 (entry.category,
				  entry.title,
				  entry.user,
				  entry.pw))
		entry.entryId = c.lastRowID()
		self.__setDirty()

	def editEntry(self, entry):
		"""Update the contents of an existing entry.
		entry: A PWManEntry() containing the new data of the entry/
		"""
		oldEntry = self.getEntry(entry.category, entry.title)
		if not oldEntry:
			raise PWManError("Entry does not exist")

		if entry.user is None:
			entry.user = oldEntry.user
		if entry.pw is None:
			entry.pw = oldEntry.pw
		entry.entryId = oldEntry.entryId

		c = self.sqlExec("UPDATE entries SET "
				 "category=?, title=?, user=?, pw=? "
				 "WHERE id=?;",
				 (entry.category,
				  entry.title,
				  entry.user,
				  entry.pw,
				  entry.entryId))
		self.__setDirty()

	def moveEntry(self, entry, newCategory, newTitle, toDb=None, copy=False):
		"""Move or copy an existing entry to a new category and/or set a new entry title.
		entry: The PWManEntry() instance to move/copy.
		newCategory: The target category name string.
		newTitle: The target title string.
		toDb: The target database. Defaults to self.
		copy: If False, then move. If True, then copy.
		"""
		toDb = toDb or self
		if toDb.entryExists(newCategory, newTitle):
			raise PWManError("Entry does already exist.")
		oldEntry = self.getEntry(entry.category, entry.title)
		if not oldEntry:
			raise PWManError("Entry does not exist.")
		if toDb is self and not copy:
			entry.category = newCategory
			entry.title = newTitle
			c = self.sqlExec("UPDATE entries SET "
					 "category=?, title=? "
					 "WHERE id=?;",
					 (entry.category,
					  entry.title,
					  oldEntry.entryId))
			self.__setDirty()
		else:
			newEntry = deepcopy(oldEntry)
			bulk = self.getEntryBulk(newEntry)
			attrs = self.getEntryAttrs(newEntry)
			totp = self.getEntryTotp(newEntry)
			newEntry.entryId = None
			newEntry.category = newCategory
			newEntry.title = newTitle
			toDb.addEntry(newEntry)
			if bulk:
				bulk.bulkId = None
				toDb.setEntryBulk(bulk)
			for attr in attrs:
				attr.attrId = None
				toDb.setEntryAttr(attr)
			if totp:
				totp.totpId = None
				toDb.setEntryTotp(totp)
			if not copy:
				entry.entryId = newEntry.entryId
				entry.category = newEntry.category
				entry.title = newEntry.title
				self.delEntry(oldEntry)

	def moveEntries(self, fromCategory, toCategory, toDb=None, copy=False):
		"""Move or copy all entries from one category to another category.
		fromCategory: The category to move all entries from.
		toCategory: The (new) category to move all entries to.
		toDb: The target database. Defaults to self.
		copy: If False, then move. If True, then copy.
		"""
		toDb = toDb or self
		if not self.categoryExists(fromCategory):
			raise PWManError("Source category does not exist.")
		if toDb is self and fromCategory == toCategory:
			return
		fromTitles = self.getEntryTitles(fromCategory)
		for fromTitle in fromTitles:
			if toDb.entryExists(toCategory, fromTitle):
				raise PWManError("Target entry %s/%s does already exist." % (
						 toCategory, fromTitle))
		if toDb is self and not copy:
			c = self.sqlExec("UPDATE entries SET category=? "
					 "WHERE category=?;",
					 (toCategory,
					  fromCategory))
			self.__setDirty()
		else:
			for fromTitle in fromTitles:
				entry = self.getEntry(fromCategory, fromTitle)
				bulk = self.getEntryBulk(entry)
				attrs = self.getEntryAttrs(entry)
				totp = self.getEntryTotp(entry)
				entry.entryId = None
				entry.category = toCategory
				toDb.addEntry(entry)
				if bulk:
					bulk.bulkId = None
					toDb.setEntryBulk(bulk)
				for attr in attrs:
					attr.attrId = None
					toDb.setEntryAttr(attr)
				if totp:
					totp.totpId = None
					toDb.setEntryTotp(totp)
			if not copy:
				for fromTitle in fromTitles:
					entry = self.getEntry(fromCategory, fromTitle)
					self.delEntry(entry)

	def delEntry(self, entry):
		"""Delete an existing entry from the database.
		entry: The PWManEntry() instance to delete from the database.
		"""
		c = self.sqlExec("SELECT id FROM entries "
				 "WHERE category=? AND title=? "
				 "LIMIT 1;",
				 (entry.category,
				  entry.title))
		entryId = c.fetchOne()
		if entryId is None:
			raise PWManError("Entry does not exist")
		entryId = entryId[0]
		c = self.sqlExec("DELETE FROM entries WHERE id=?;",
				 (entryId,))
		self.__garbageCollect()
		self.__setDirty()

	def getEntryBulk(self, entry):
		"""Get the bulk data associated with an entry.
		entry: The PWManEntry() to get the bulk data for.
		Returns a PWManEntryBulk() instance or None, if there is no bulk data.
		"""
		c = self.sqlExec("SELECT bulk.id, bulk.data "
				 "FROM bulk, entries "
				 "WHERE entries.category=? AND entries.title=? AND "
				 "bulk.entry = entries.id "
				 "LIMIT 1;",
				 (entry.category,
				  entry.title))
		data = c.fetchOne()
		if not data:
			return None
		return PWManEntryBulk(data=data[1],
				      entry=entry,
				      bulkId=data[0])

	def setEntryBulk(self, entryBulk):
		"""Set the bulk data associated with an entry.
		entryBulk: The new PWManEntryBulk() instance to write to the database.
		           If entryBulk.data is None, then the bulk data is deleted.
		"""
		entry = entryBulk.entry
		if not entry or entry.entryId is None:
			raise PWManError("Bulk: Entry does not exist.")
		if entryBulk.data:
			c = self.sqlExec("SELECT id FROM bulk WHERE entry=? LIMIT 1;",
					 (entry.entryId, ))
			bulkId = c.fetchOne()
			if bulkId is None:
				c = self.sqlExec("INSERT INTO bulk(entry, data) "
						 "VALUES(?,?);",
						 (entry.entryId,
						  entryBulk.data))
			else:
				bulkId = bulkId[0]
				c = self.sqlExec("UPDATE bulk "
						 "SET entry=?, data=? "
						 "WHERE id=?;",
						 (entry.entryId,
						  entryBulk.data,
						  bulkId))
		else:
			c = self.sqlExec("DELETE FROM bulk WHERE id=?;",
					 (entryBulk.bulkId,))
		self.__setDirty()

	def getEntryTotp(self, entry):
		"""Get the TOTP parameters associated with an entry.
		entry: The PWManEntry() to get the TOTP parameters for.
		Returns a PWManEntryTOTP() instance, or None if there is no TOTP data.
		"""
		c = self.sqlExec("SELECT totp.id, totp.key, totp.digits, totp.hash "
				 "FROM totp, entries "
				 "WHERE entries.category=? AND entries.title=? AND "
				 "totp.entry = entries.id "
				 "LIMIT 1;",
				 (entry.category,
				  entry.title))
		data = c.fetchOne()
		if not data:
			return None
		return PWManEntryTOTP(key=data[1],
				      digits=data[2],
				      hmacHash=data[3],
				      entry=entry,
				      totpId=data[0])

	def setEntryTotp(self, entryTotp):
		"""Set the TOTP data associated with an entry.
		entryTotp: The new PWManEntryTOTP() instance to write to the database.
		           If entryTotp.key is None, then the TOTP data is deleted.
		"""
		entry = entryTotp.entry
		if not entry or entry.entryId is None:
			raise PWManError("TOTP: Entry does not exist.")
		if entryTotp.key:
			c = self.sqlExec("SELECT id FROM totp WHERE entry=? LIMIT 1;",
					 (entry.entryId, ))
			totpId = c.fetchOne()
			if totpId is None:
				c = self.sqlExec("INSERT INTO totp(entry, key, digits, hash) "
						 "VALUES(?,?,?,?);",
						 (entry.entryId,
						  entryTotp.key,
						  entryTotp.digits,
						  entryTotp.hmacHash))
			else:
				totpId = totpId[0]
				c = self.sqlExec("UPDATE totp "
						 "SET entry=?, key=?, digits=?, hash=? "
						 "WHERE id=?;",
						 (entry.entryId,
						  entryTotp.key,
						  entryTotp.digits,
						  entryTotp.hmacHash,
						  totpId))
		else:
			c = self.sqlExec("DELETE FROM totp WHERE id=?;",
					 (entryTotp.totpId,))
		self.__setDirty()

	def getEntryAttr(self, entry, attrName):
		"""Get an attribute associated with an entry.
		entry: The PWManEntry() to get the attribute for.
		attrName: The name string of the attribute to get.
		Returns a PWManEntryAttr() instance, or None if there is such attribute.
		"""
		c = self.sqlExec("SELECT entryattr.id, entryattr.name, entryattr.data "
				 "FROM entryattr, entries "
				 "WHERE entries.category=? AND entries.title=? AND "
				 "entryattr.entry = entries.id AND entryattr.name=? "
				 "LIMIT 1;",
				 (entry.category,
				  entry.title,
				  attrName))
		data = c.fetchOne()
		if not data:
			return None
		return PWManEntryAttr(name=data[1],
				      data=data[2],
				      entry=entry,
				      attrId=data[0])

	def getEntryAttrs(self, entry):
		"""Get all attributes associated with an entry.
		entry: The PWManEntry() to get the attributes for.
		Returns a list of PWManEntryAttr() instances,
		or an empty list if there are no attributes.
		"""
		c = self.sqlExec("SELECT entryattr.id, entryattr.name, entryattr.data "
				 "FROM entryattr, entries "
				 "WHERE entries.category=? AND entries.title=? AND "
				 "entryattr.entry = entries.id "
				 "ORDER BY entryattr.name;",
				 (entry.category,
				  entry.title))
		dataSet = c.fetchAll()
		if not dataSet:
			return []
		return [ PWManEntryAttr(name=data[1],
				        data=data[2],
				        entry=entry,
				        attrId=data[0])
			 for data in dataSet ]

	def setEntryAttr(self, entryAttr):
		"""Set an attribute associated with an entry.
		entryAttr: The new PWManEntryAttr() instance to write to the database.
		           If entryAttr.data is None, then the attribute is deleted.
		"""
		entry = entryAttr.entry
		if not entry or entry.entryId is None:
			raise PWManError("Attr: Entry does not exist.")
		if entryAttr.data:
			c = self.sqlExec("SELECT id FROM entryattr "
					 "WHERE entry=? AND name=? "
					 "LIMIT 1;",
					 (entry.entryId,
					  entryAttr.name))
			attrId = c.fetchOne()
			if attrId is None:
				c = self.sqlExec("INSERT INTO entryattr(entry, name, data) "
						 "VALUES(?,?,?);",
						 (entry.entryId,
						  entryAttr.name,
						  entryAttr.data))
			else:
				attrId = attrId[0]
				c = self.sqlExec("UPDATE entryattr "
						 "SET entry=?, name=?, data=? "
						 "WHERE id=?;",
						 (entry.entryId,
						  entryAttr.name,
						  entryAttr.data,
						  attrId))
		else:
			c = self.sqlExec("DELETE FROM entryattr WHERE id=?;",
					 (entryAttr.attrId,))
		self.__setDirty()

	def getGlobalAttr(self, name):
		"""Get a global attribute.
		A global attribute is not associated with an entry.
		Returns None, if the attribute does not exist.
		"""
		try:
			c = self.sqlExec("SELECT id, data FROM globalattr "
					 "WHERE name=? "
					 "LIMIT 1;",
					 (name,))
			data = c.fetchOne()
			return data[1] if data else None
		except (CSQLError) as e:
			return None

	def setGlobalAttr(self, name, data, setDirty=True):
		"""Set a global attribute.
		A global attribute is not associated with an entry.
		If data is None or empty, the attribute is deleted from the database.
		"""
		if data:
			c = self.sqlExec("SELECT id FROM globalattr "
					 "WHERE name=? "
					 "LIMIT 1;",
					 (name,))
			attrId = c.fetchOne()
			if attrId is None:
				c = self.sqlExec("INSERT INTO globalattr(name, data) "
						 "VALUES(?,?);",
						 (name, data))
			else:
				attrId = attrId[0]
				c = self.sqlExec("UPDATE globalattr "
						 "SET data=? "
						 "WHERE name=?;",
						 (data, name))
		else:
			c = self.sqlExec("DELETE FROM globalattr WHERE name=?;",
					 (name,))
		if setDirty:
			self.__setDirty()

	def __setDirty(self, d=True):
		"""Set the flag for uncommitted data.
		"""
		self.__dirty = d

	def isDirty(self):
		"""Returns True, if the database contains uncommitted data.
		"""
		return self.__dirty

	def flunkDirty(self):
		"""Print a warning, if the database contains uncommitted data.
		Then set the flag for uncommitted data to False.
		"""
		if self.isDirty():
			print("WARNING: Dropping uncommitted data",
			      file=sys.stderr)
			self.__setDirty(False)

	def dropUncommitted(self):
		super().dropUncommitted()
		self.__setDirty(False)

	def commit(self):
		self.__garbageCollect()
		super().commit()
		self.__setDirty(False)

	def importSqlScript(self, *args, **kwargs):
		self.__setDirty()
		super().importSqlScript(*args, **kwargs)

	def getOnDiskDb(self):
		"""Get a read-only instance of PWManDatabase that contains
		the current on-disk data. The on-disk data is the data
		at the last commit.
		"""
		db = self.__class__(filename=self.getFilename(),
				    passphrase=self.getPassphrase(),
				    key=self.getKey(),
				    readOnly=True,
				    silent=True)
		return db

	def dumpEntry(self, entry, totp="hide"):
		"""Returns a human readable dump string of an entry.
		"""
		res = []
		res.append("===  %s  ===" % entry.category)
		res.append("\t---  %s  ---" % entry.title)
		if entry.user:
			res.append("\tUser:\t\t%s" % entry.user)
		if entry.pw:
			res.append("\tPassword:\t%s" % entry.pw)
		entryBulk = self.getEntryBulk(entry)
		if entryBulk:
			res.append("\tBulk data:\t%s" % entryBulk.data)
		entryTotp = self.getEntryTotp(entry)
		if entryTotp:
			if totp == "show":
				res.append("\tTOTP key:\t%s" % entryTotp.key)
				res.append("\tTOTP digits:\t%d" % entryTotp.digits)
				res.append("\tTOTP hash:\t%s" % entryTotp.hmacHash)
			elif totp == "gen":
				try:
					token = entryTotp.generate()
				except libpwman.otp.OtpError as e:
					raise PWManError("Failed to generate TOTP token: "
							 "%s" % str(e))
				res.append(token)
			elif totp == "hide":
				res.append("\tTOTP:\t\tavailable")
			else:
				assert False
		entryAttrs = self.getEntryAttrs(entry)
		if entryAttrs:
			res.append("\tAttributes:")
			maxLen = max(len(a.name) for a in entryAttrs)
			for entryAttr in entryAttrs:
				align = maxLen - len(entryAttr.name)
				res.append("\t    %s:%s %s" % (
					entryAttr.name,
					align * " ",
					entryAttr.data))
		return "\n".join(res) + "\n"

	def dumpEntries(self, totp="hide"):
		"""Returns a human readable dump string of all entries.
		"""
		ret = []
		for category in self.getCategoryNames():
			for title in self.getEntryTitles(category):
				entry = self.getEntry(category, title)
				dump = self.dumpEntry(entry, totp)
				ret.append(dump)
		return "\n".join(ret)

	def dumpEntriesCsv(self, totp="hide"):
		"""Returns a CSV format dump string of all entries.
		"""
		csvHeads = [
			"Category",
			"Title",
			"User",
			"Password",
			"Bulk data",
			"TOTP key",
			"TOTP digits",
			"TOTP hash",
		]
		rows = []
		attrNames = set()
		for category in self.getCategoryNames():
			for title in self.getEntryTitles(category):
				entry = self.getEntry(category, title)
				row = {
					"Category"	: entry.category,
					"Title"		: entry.title,
					"User"		: entry.user,
					"Password"	: entry.pw,
				}
				entryBulk = self.getEntryBulk(entry)
				if entryBulk:
					row["Bulk data"] = entryBulk.data
				entryTotp = self.getEntryTotp(entry)
				if entryTotp:
					if totp == "show":
						row["TOTP key"] = entryTotp.key
						row["TOTP digits"] = entryTotp.digits
						row["TOTP hash"] = entryTotp.hmacHash
					elif totp == "gen":
						try:
							token = entryTotp.generate()
						except libpwman.otp.OtpError as e:
							raise PWManError("Failed to generate TOTP token: "
									 "%s" % str(e))
						row["TOTP"] = token
					elif totp == "hide":
						row["TOTP key"] = "available"
					else:
						assert False
				entryAttrs = self.getEntryAttrs(entry)
				if entryAttrs:
					for entryAttr in entryAttrs:
						attrNames.add(entryAttr.name)
						row[entryAttr.name] = entryAttr.data
				rows.append(row)
		csvHeads.extend(sorted(attrNames))

		f = io.StringIO()
		w = csv.DictWriter(f, fieldnames=csvHeads, dialect="excel")
		w.writeheader()
		for r in rows:
			w.writerow(r)
		return f.getvalue()
bues.ch cgit interface