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"
|
|
11 }
|
|
12 private lateinit var commandLine: CommandLine
|
|
13 private lateinit var db: Database
|
|
14
|
8
|
15 override fun run(args: Array<String>) {
|
11
|
16 parseArguments(args)
|
|
17 db = Database.open()
|
|
18 doMerge()
|
|
19 }
|
|
20
|
|
21 private fun parseArguments(args: Array<String>) {
|
|
22 val options = Options().apply {
|
|
23 addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.")
|
|
24 addOption("h", MergeSubcommand.HELP, false, "Print this help message.")
|
|
25 }
|
|
26 try {
|
|
27 commandLine = DefaultParser().parse(options, args)
|
|
28 } catch (e: ParseException) {
|
|
29 die(e.message ?: "syntax error", 2)
|
|
30 }
|
|
31 if (commandLine.hasOption(MergeSubcommand.HELP)) {
|
|
32 HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options)
|
|
33 exitProcess(0)
|
|
34 }
|
|
35 if (commandLine.args.isEmpty()) {
|
|
36 die("expecting other database name", 2)
|
|
37 }
|
|
38 if (commandLine.args.size > 1) {
|
|
39 die("unexpected trailing arguments", 2)
|
|
40 }
|
|
41 }
|
|
42
|
|
43 private fun doMerge() {
|
|
44 val otherFile = commandLine.args[0]
|
|
45 val otherDb = Database.open(
|
|
46 fileName = otherFile,
|
|
47 passwordPrompt = "Key for ${see(otherFile)}: ", create = false
|
|
48 )
|
|
49 otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords").use { stmt ->
|
|
50 val results = stmt.executeQuery()
|
|
51 while (results.next()) {
|
|
52 val otherEntry = makeEntry(results)
|
|
53 val thisEntry = getEntry(db, otherEntry.name)
|
|
54 if (thisEntry == null) {
|
12
|
55 otherEntry.insert(db)
|
11
|
56 } else {
|
|
57 doCompare(thisEntry, otherEntry)
|
|
58 thisEntry.password.clear()
|
|
59 }
|
|
60 otherEntry.password.clear()
|
|
61 }
|
|
62 }
|
8
|
63 }
|
11
|
64
|
|
65 private fun makeEntry(results: ResultSet) = Entry(
|
|
66 name = results.getDecryptedString(1, db.encryption)!!,
|
|
67 username = results.getDecryptedString(2, db.encryption)!!,
|
|
68 password = results.getDecrypted(3, db.encryption)!!,
|
|
69 notes = results.getDecryptedString(4, db.encryption),
|
|
70 created = results.getDate(5),
|
|
71 modified = results.getDate(6),
|
|
72 accessed = results.getDate(7)
|
|
73 )
|
|
74
|
|
75 private fun getEntry(otherDb: Database, name: String): Entry? {
|
|
76 otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { stmt ->
|
|
77 stmt.setLong(1, otherDb.makeKey(name))
|
|
78 val results = stmt.executeQuery()
|
|
79 return if (results.next()) makeEntry(results) else null
|
|
80 }
|
|
81 }
|
|
82
|
|
83 private fun doCompare(thisEntry: Entry, otherEntry: Entry) {
|
|
84 if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) {
|
|
85 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use {
|
|
86 it.setEncryptedString(1, otherEntry.name, db.encryption)
|
|
87 it.setEncryptedString(2, otherEntry.username, db.encryption)
|
|
88 it.setEncrypted(3, otherEntry.password, db.encryption)
|
|
89 it.setEncryptedString(4, otherEntry.notes, db.encryption)
|
|
90 it.setLong(5, otherEntry.modifiedOrCreated.time)
|
|
91 it.setLong(6, db.makeKey(thisEntry.name))
|
|
92 it.executeUpdate()
|
|
93 }
|
|
94 }
|
|
95 }
|
|
96
|
12
|
97 private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean =
|
|
98 commandLine.hasOption(FORCE) || askUserIfOkToOverwrite(thisEntry, otherEntry)
|
8
|
99 }
|