changeset 11:c69665ff37d0

Add merge subcommand (untested).
author David Barts <n5jrn@me.com>
date Sat, 21 Jan 2023 15:39:42 -0800
parents cbe4c797c9a6
children a38a2a1036c3
files PassMan.iml design.txt pom.xml src/main/kotlin/name/blackcap/passman/Database.kt src/main/kotlin/name/blackcap/passman/Entry.kt src/main/kotlin/name/blackcap/passman/HelpSubcommand.kt src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt src/main/kotlin/name/blackcap/passman/RenameSubcommand.kt
diffstat 9 files changed, 257 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/PassMan.iml	Sat Oct 01 20:43:59 2022 -0700
+++ b/PassMan.iml	Sat Jan 21 15:39:42 2023 -0800
@@ -46,5 +46,12 @@
     <orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-reflect:1.7.10" level="project" />
     <orderEntry type="library" name="Maven: org.xerial:sqlite-jdbc:3.36.0.3" level="project" />
     <orderEntry type="library" name="Maven: commons-cli:commons-cli:1.5.0" level="project" />
+    <orderEntry type="library" name="Maven: com.opencsv:opencsv:5.5" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.12.0" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.commons:commons-text:1.9" level="project" />
+    <orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.9.4" level="project" />
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
+    <orderEntry type="library" name="Maven: commons-collections:commons-collections:3.2.2" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.commons:commons-collections4:4.4" level="project" />
   </component>
 </module>
\ No newline at end of file
--- a/design.txt	Sat Oct 01 20:43:59 2022 -0700
+++ b/design.txt	Sat Jan 21 15:39:42 2023 -0800
@@ -58,3 +58,5 @@
     - Default to be silent and not put password in clipboard, just change
       in database.
   + Command-line only, no GUI.
+  + JSON for import subcommand (if we implement):
+    https://javaee.github.io/jsonp/getting-started.html
--- a/pom.xml	Sat Oct 01 20:43:59 2022 -0700
+++ b/pom.xml	Sat Jan 21 15:39:42 2023 -0800
@@ -135,6 +135,11 @@
             <artifactId>commons-cli</artifactId>
             <version>1.5.0</version>
         </dependency>
+        <dependency>
+            <groupId>com.opencsv</groupId>
+            <artifactId>opencsv</artifactId>
+            <version>5.5</version>
+        </dependency>
     </dependencies>
 
 </project>
\ No newline at end of file
--- a/src/main/kotlin/name/blackcap/passman/Database.kt	Sat Oct 01 20:43:59 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Database.kt	Sat Jan 21 15:39:42 2023 -0800
@@ -114,24 +114,38 @@
     fun makeKey(name: String): Long = Hashing.hash(encryption.encryptFromString0(name.lowercase()))
 }
 
-public fun ResultSet.getDecryptedString(columnIndex: Int, encryption: Encryption): String? {
+fun ResultSet.getDecryptedString(columnIndex: Int, encryption: Encryption): String? {
     return encryption.decryptToString(getBytes(columnIndex) ?: return null)
 }
 
-public fun ResultSet.getDecrypted(columnIndex: Int, encryption: Encryption): CharArray? {
+fun ResultSet.getDecrypted(columnIndex: Int, encryption: Encryption): CharArray? {
     return encryption.decrypt(getBytes(columnIndex) ?: return null)
 }
 
-public fun PreparedStatement.setEncryptedString(columnIndex: Int, value: String?, encryption: Encryption) =
+fun PreparedStatement.setEncryptedString(columnIndex: Int, value: String?, encryption: Encryption) =
     if (value == null) {
         setNull(columnIndex, Types.BLOB)
     } else {
         setBytes(columnIndex, encryption.encryptFromString(value))
     }
 
-public fun PreparedStatement.setEncrypted(columnIndex: Int, value: CharArray?, encryption: Encryption) =
+fun PreparedStatement.setEncrypted(columnIndex: Int, value: CharArray?, encryption: Encryption) =
     if (value == null) {
         setNull(columnIndex, Types.BLOB)
     } else {
         setBytes(columnIndex, encryption.encrypt(value))
     }
+
+fun PreparedStatement.setBytesOrNull(columnIndex: Int, value: ByteArray?) =
+    if (value == null) {
+        setNull(columnIndex, Types.BLOB)
+    } else {
+        setBytes(columnIndex, value)
+}
+
+fun PreparedStatement.setLongOrNull(columnIndex: Int, value: Long?) =
+    if (value == null) {
+        setNull(columnIndex, Types.INTEGER)
+    } else {
+        setLong(columnIndex, value)
+    }
--- a/src/main/kotlin/name/blackcap/passman/Entry.kt	Sat Oct 01 20:43:59 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Entry.kt	Sat Jan 21 15:39:42 2023 -0800
@@ -47,6 +47,8 @@
         private fun _getNotes() = readLine("Notes: ")
     }
 
+    val modifiedOrCreated get() = modified ?: created!!
+
     fun print(redactPassword: String? = null) {
         println("Name of site: $name")
         println("Username: $username")
--- a/src/main/kotlin/name/blackcap/passman/HelpSubcommand.kt	Sat Oct 01 20:43:59 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/HelpSubcommand.kt	Sat Jan 21 15:39:42 2023 -0800
@@ -5,11 +5,12 @@
         println("PassMan: a password manager")
         println("Available subcommands:")
         println("create       Create a new username/password pair.")
-        println("read         Retrieve data from existing record.")
-        println("update       Update existing record.")
         println("delete       Delete existing record.")
         println("help         Print this message.")
         println("list         List records.")
         println("merge        Merge passwords in from another PassMan database.")
+        println("read         Retrieve data from existing record.")
+        println("rename       Rename existing record.")
+        println("update       Update existing record.")
     }
 }
--- a/src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt	Sat Oct 01 20:43:59 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt	Sat Jan 21 15:39:42 2023 -0800
@@ -1,22 +1,129 @@
 package name.blackcap.passman
 
+import org.apache.commons.cli.*
+import java.sql.ResultSet
+import java.util.*
+import kotlin.system.exitProcess
+
 class MergeSubcommand(): Subcommand() {
+    private companion object {
+        const val FORCE = "force"
+        const val HELP = "help"
+    }
+    private lateinit var commandLine: CommandLine
+    private lateinit var db: Database
+
     override fun run(args: Array<String>) {
-        /*
-         * To merge, plow through both the old and the new databases in the same
-         * order. By id is sorta idiosyncratic by human standards, but why not?
-         * If the entries do not match, write the lowest-numbered one to the
-         * output database and read the next record from where the lowest-numbered
-         * one came. If they do match, rely on modified time (fall back to created
-         * time if null) to sort out the winner. Continue till we hit the end
-         * of one database, then "drain" the other. Preserve time stamps.
-         *
-         * Maybe do something special (warning? confirmation prompt?) on mismatched
-         * creation times, as this means a new record was created w/o knowledge
-         * of an existing one. Choices should be to clobber w/ newest, pick one
-         * manually, or rename one so the other can persist under original name.
-         *
-         */
-        error("not yet implemented")
+        parseArguments(args)
+        db = Database.open()
+        doMerge()
+    }
+
+    private fun parseArguments(args: Array<String>) {
+        val options = Options().apply {
+            addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.")
+            addOption("h", MergeSubcommand.HELP, false, "Print this help message.")
+        }
+        try {
+            commandLine = DefaultParser().parse(options, args)
+        } catch (e: ParseException) {
+            die(e.message ?: "syntax error", 2)
+        }
+        if (commandLine.hasOption(MergeSubcommand.HELP)) {
+            HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options)
+            exitProcess(0)
+        }
+        if (commandLine.args.isEmpty()) {
+            die("expecting other database name", 2)
+        }
+        if (commandLine.args.size > 1) {
+            die("unexpected trailing arguments", 2)
+        }
+    }
+
+    private fun doMerge() {
+        val otherFile = commandLine.args[0]
+        val otherDb = Database.open(
+            fileName = otherFile,
+            passwordPrompt = "Key for ${see(otherFile)}: ", create = false
+        )
+        otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords").use { stmt ->
+            val results = stmt.executeQuery()
+            while (results.next()) {
+                val otherEntry = makeEntry(results)
+                val thisEntry = getEntry(db, otherEntry.name)
+                if (thisEntry == null) {
+                    doInsert(otherEntry)
+                } else {
+                    doCompare(thisEntry, otherEntry)
+                    thisEntry.password.clear()
+                }
+                otherEntry.password.clear()
+            }
+        }
     }
+
+    private fun makeEntry(results: ResultSet) = Entry(
+        name = results.getDecryptedString(1, db.encryption)!!,
+        username = results.getDecryptedString(2, db.encryption)!!,
+        password = results.getDecrypted(3, db.encryption)!!,
+        notes = results.getDecryptedString(4, db.encryption),
+        created = results.getDate(5),
+        modified = results.getDate(6),
+        accessed = results.getDate(7)
+    )
+
+    private fun getEntry(otherDb: Database, name: String): Entry? {
+        otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { stmt ->
+            stmt.setLong(1, otherDb.makeKey(name))
+            val results = stmt.executeQuery()
+            return if (results.next()) makeEntry(results) else null
+        }
+    }
+
+    private fun doInsert(entry: Entry) {
+        db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)")
+            .use {
+                it.setLong(1, db.makeKey(entry.name))
+                it.setEncryptedString(2, entry.name, db.encryption)
+                it.setEncryptedString(3, entry.username, db.encryption)
+                it.setEncrypted(4, entry.password, db.encryption)
+                it.setEncryptedString(5, entry.notes, db.encryption)
+                it.setLongOrNull(6, entry.created?.time)
+                it.setLongOrNull(7, entry.modified?.time)
+                it.setLongOrNull(8, entry.accessed?.time)
+                it.executeUpdate()
+            }
+    }
+
+    private fun doCompare(thisEntry: Entry, otherEntry: Entry) {
+        if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) {
+            db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use {
+                it.setEncryptedString(1, otherEntry.name, db.encryption)
+                it.setEncryptedString(2, otherEntry.username, db.encryption)
+                it.setEncrypted(3, otherEntry.password, db.encryption)
+                it.setEncryptedString(4, otherEntry.notes, db.encryption)
+                it.setLong(5, otherEntry.modifiedOrCreated.time)
+                it.setLong(6, db.makeKey(thisEntry.name))
+                it.executeUpdate()
+            }
+        }
+    }
+
+    private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean {
+        if (commandLine.hasOption(MergeSubcommand.FORCE)) {
+            return true
+        }
+        val REDACTED = "(redacted)"
+        println("EXISTING ENTRY:")
+        thisEntry.printLong(REDACTED)
+        println()
+        println("NEWER ENTRY:")
+        otherEntry.printLong(REDACTED)
+        println()
+        val answer = name.blackcap.passman.readLine("OK to overwrite existing entry? ")
+        println()
+        return answer.trimStart().firstOrNull()?.uppercaseChar() in setOf('T', 'Y')
+    }
+
 }
--- a/src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt	Sat Oct 01 20:43:59 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt	Sat Jan 21 15:39:42 2023 -0800
@@ -26,7 +26,7 @@
             die(e.message ?: "syntax error", 2)
         }
         if (commandLine.hasOption(HELP)) {
-            HelpFormatter().printHelp("$SHORTNAME read", options)
+            HelpFormatter().printHelp("$SHORTNAME read [options] name", options)
             exitProcess(0)
         }
         if (commandLine.args.isEmpty()) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/kotlin/name/blackcap/passman/RenameSubcommand.kt	Sat Jan 21 15:39:42 2023 -0800
@@ -0,0 +1,96 @@
+package name.blackcap.passman
+
+import org.apache.commons.cli.*
+import kotlin.system.exitProcess
+
+class RenameSubcommand(): Subcommand() {
+    private companion object {
+        const val FORCE = "force"
+        const val HELP = "help"
+    }
+    private lateinit var commandLine: CommandLine
+    private lateinit var source: String
+    private lateinit var destination: String
+    private lateinit var db: Database
+
+    override fun run(args: Array<String>) {
+        parseArguments(args)
+        db = Database.open()
+        renameIt()
+    }
+
+    private fun parseArguments(args: Array<String>) {
+        val options = Options().apply {
+            addOption("f", FORCE, false, "If destination exists exists, force overwrite.")
+            addOption("h", HELP, false, "Print this help message.")
+        }
+        try {
+            commandLine = DefaultParser().parse(options, args)
+        } catch (e: ParseException) {
+            die(e.message ?: "syntax error", 2)
+        }
+        if (commandLine.hasOption(HELP)) {
+            HelpFormatter().printHelp("$SHORTNAME rename [options] source destination", options)
+            exitProcess(0)
+        }
+        if (commandLine.args.size < 2) {
+            die("expecting source and destination", 2)
+        }
+        if (commandLine.args.size > 2) {
+            die("unexpected trailing arguments", 2)
+        }
+        source = commandLine.args[0]
+        destination = commandLine.args[1]
+    }
+
+    private fun renameIt(): Unit {
+        val sid = db.makeKey(source)
+        val did = db.makeKey(destination)
+
+        if(!recordExists(sid)) {
+            die("no record matches ${see(source)}")
+        }
+        if (recordExists(did)) {
+            if (commandLine.hasOption(FORCE)) {
+                deleteRecord(did)
+            } else {
+                die("record matching ${see(destination)} already exists")
+            }
+        }
+
+        db.connection.prepareStatement("select username, password, notes, created, modified, accessed from passwords where id = ?").use { sourceStmt ->
+            sourceStmt.setLong(1, did)
+            val result = sourceStmt.executeQuery()
+            result.next()
+            db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)").run {
+                setLong(1, did)
+                setEncryptedString(2, destination, db.encryption)
+                setBytes(3, result.getBytes(1))
+                setBytes(4, result.getBytes(2))
+                setBytesOrNull(5, result.getBytes(3))
+                setLong(6, result.getLong(4))
+                setLong(7, System.currentTimeMillis())
+                setLongOrNull(8, result.getLong(6))
+                executeUpdate()
+            }
+        }
+
+        deleteRecord(sid)
+    }
+
+    private fun recordExists(id: Long): Boolean {
+        db.connection.prepareStatement("select count(*) from passwords where id = ?").use {
+            it.setLong(1, id)
+            val result = it.executeQuery()
+            result.next()
+            return result.getInt(1) > 0
+        }
+    }
+
+    private fun deleteRecord(id: Long): Unit {
+        db.connection.prepareStatement("delete from passwords where id = ?").use {
+            it.setLong(1, id);
+            it.executeUpdate()
+        }
+    }
+}
\ No newline at end of file