Mercurial > cgi-bin > hgweb.cgi > PassMan
annotate src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 20:4391afcf6bd0
Fix more bugs, correct more bad tests.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 30 Jun 2024 22:28:52 -0700 |
parents | 8f3ddebb4295 |
children | ea65ab890f66 |
rev | line source |
---|---|
8 | 1 package name.blackcap.passman |
2 | |
11 | 3 import org.apache.commons.cli.* |
4 import java.sql.ResultSet | |
5 import kotlin.system.exitProcess | |
6 | |
8 | 7 class MergeSubcommand(): Subcommand() { |
11 | 8 private companion object { |
9 const val FORCE = "force" | |
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 | 12 } |
13 private lateinit var commandLine: CommandLine | |
14 private lateinit var db: Database | |
15 | |
8 | 16 override fun run(args: Array<String>) { |
11 | 17 parseArguments(args) |
18 db = Database.open() | |
19 doMerge() | |
20 } | |
21 | |
22 private fun parseArguments(args: Array<String>) { | |
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 | 25 addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.") |
26 addOption("h", MergeSubcommand.HELP, false, "Print this help message.") | |
27 } | |
28 try { | |
29 commandLine = DefaultParser().parse(options, args) | |
30 } catch (e: ParseException) { | |
31 die(e.message ?: "syntax error", 2) | |
32 } | |
33 if (commandLine.hasOption(MergeSubcommand.HELP)) { | |
34 HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options) | |
35 exitProcess(0) | |
36 } | |
37 if (commandLine.args.isEmpty()) { | |
38 die("expecting other database name", 2) | |
39 } | |
40 if (commandLine.args.size > 1) { | |
41 die("unexpected trailing arguments", 2) | |
42 } | |
43 } | |
44 | |
45 private fun doMerge() { | |
46 val otherFile = commandLine.args[0] | |
47 val otherDb = Database.open( | |
48 fileName = otherFile, | |
49 passwordPrompt = "Key for ${see(otherFile)}: ", create = false | |
50 ) | |
51 otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords").use { stmt -> | |
52 val results = stmt.executeQuery() | |
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 | 56 val thisEntry = getEntry(db, otherEntry.name) |
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 | 59 otherEntry.insert(db) |
11 | 60 } else { |
61 doCompare(thisEntry, otherEntry) | |
62 thisEntry.password.clear() | |
63 } | |
64 otherEntry.password.clear() | |
65 } | |
66 } | |
8 | 67 } |
11 | 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 | 74 created = results.getDate(5), |
75 modified = results.getDate(6), | |
76 accessed = results.getDate(7) | |
77 ) | |
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 | 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 | 84 } |
85 } | |
86 | |
87 private fun doCompare(thisEntry: Entry, otherEntry: Entry) { | |
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 | 90 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use { |
91 it.setEncryptedString(1, otherEntry.name, db.encryption) | |
92 it.setEncryptedString(2, otherEntry.username, db.encryption) | |
93 it.setEncrypted(3, otherEntry.password, db.encryption) | |
94 it.setEncryptedString(4, otherEntry.notes, db.encryption) | |
95 it.setLong(5, otherEntry.modifiedOrCreated.time) | |
96 it.setLong(6, db.makeKey(thisEntry.name)) | |
97 it.executeUpdate() | |
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 | 101 } |
102 } | |
103 | |
12 | 104 private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean = |
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 | 118 } |