annotate src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 18:8f3ddebb4295

Was using wrong db object to decrypt, fixed.
author David Barts <n5jrn@me.com>
date Tue, 04 Apr 2023 20:38:52 -0700
parents a38a2a1036c3
children ea65ab890f66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 package name.blackcap.passman
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
2
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
3 import org.apache.commons.cli.*
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
4 import java.sql.ResultSet
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
5 import kotlin.system.exitProcess
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
6
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 class MergeSubcommand(): Subcommand() {
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
8 private companion object {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
9 const val FORCE = "force"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
10 const val HELP = "help"
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
11 const val VERBOSE = "verbose"
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
12 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
13 private lateinit var commandLine: CommandLine
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
14 private lateinit var db: Database
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
15
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 override fun run(args: Array<String>) {
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
17 parseArguments(args)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
18 db = Database.open()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
19 doMerge()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
20 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
21
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
22 private fun parseArguments(args: Array<String>) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
23 val options = Options().apply {
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
24 addOption("v", MergeSubcommand.VERBOSE, false, "Verbose mode, print what we are doing.")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
25 addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
26 addOption("h", MergeSubcommand.HELP, false, "Print this help message.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
27 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
28 try {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
29 commandLine = DefaultParser().parse(options, args)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
30 } catch (e: ParseException) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
31 die(e.message ?: "syntax error", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
32 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
33 if (commandLine.hasOption(MergeSubcommand.HELP)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
34 HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
35 exitProcess(0)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
36 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
37 if (commandLine.args.isEmpty()) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
38 die("expecting other database name", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
39 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
40 if (commandLine.args.size > 1) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
41 die("unexpected trailing arguments", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
42 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
43 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
44
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
45 private fun doMerge() {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
46 val otherFile = commandLine.args[0]
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
47 val otherDb = Database.open(
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
48 fileName = otherFile,
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
49 passwordPrompt = "Key for ${see(otherFile)}: ", create = false
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
50 )
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
51 otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords").use { stmt ->
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
52 val results = stmt.executeQuery()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
53 while (results.next()) {
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
54 val otherEntry = makeEntry(otherDb, results)
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
55 vprint("read ${see(otherEntry.name)}…")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
56 val thisEntry = getEntry(db, otherEntry.name)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
57 if (thisEntry == null) {
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
58 vprintln(" missing, inserting it")
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
59 otherEntry.insert(db)
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
60 } else {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
61 doCompare(thisEntry, otherEntry)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
62 thisEntry.password.clear()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
63 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
64 otherEntry.password.clear()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
65 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
66 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 }
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
68
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
69 private fun makeEntry(dbParam: Database, results: ResultSet) = Entry(
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
70 name = results.getDecryptedString(1, dbParam.encryption)!!,
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
71 username = results.getDecryptedString(2, dbParam.encryption)!!,
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
72 password = results.getDecrypted(3, dbParam.encryption)!!,
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
73 notes = results.getDecryptedString(4, dbParam.encryption),
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
74 created = results.getDate(5),
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
75 modified = results.getDate(6),
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
76 accessed = results.getDate(7)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
77 )
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
78
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
79 private fun getEntry(dbParam: Database, name: String): Entry? {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
80 dbParam.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { stmt ->
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
81 stmt.setLong(1, dbParam.makeKey(name))
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
82 val results = stmt.executeQuery()
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
83 return if (results.next()) makeEntry(dbParam, results) else null
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
84 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
85 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
86
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
87 private fun doCompare(thisEntry: Entry, otherEntry: Entry) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
88 if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) {
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
89 vprintln(" newer, updating it")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
90 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
91 it.setEncryptedString(1, otherEntry.name, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
92 it.setEncryptedString(2, otherEntry.username, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
93 it.setEncrypted(3, otherEntry.password, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
94 it.setEncryptedString(4, otherEntry.notes, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
95 it.setLong(5, otherEntry.modifiedOrCreated.time)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
96 it.setLong(6, db.makeKey(thisEntry.name))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
97 it.executeUpdate()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
98 }
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
99 } else {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
100 vprintln(" older or update denied, ignoring it")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
101 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
102 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
103
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
104 private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean =
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
105 commandLine.hasOption(FORCE) || askUserIfOkToOverwrite(thisEntry, otherEntry)
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
106
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
107 private fun vprint(message: String) {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
108 if (commandLine.hasOption(VERBOSE)) {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
109 print(message)
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
110 }
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
111 }
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
112
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
113 private fun vprintln(message: String) {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
114 if (commandLine.hasOption(VERBOSE)) {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
115 println(message)
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
116 }
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
117 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
118 }