From 9b66eaac9a4f5464a19d06fd4871f02bd6ab903a Mon Sep 17 00:00:00 2001 From: cnngimenez Date: Sat, 17 Aug 2024 16:51:28 -0300 Subject: [PATCH 1/5] egrep is obsolescent in grep 3.11 (and possibly other versions) --- keepass-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keepass-mode.el b/keepass-mode.el index 62d6815..45b014a 100644 --- a/keepass-mode.el +++ b/keepass-mode.el @@ -143,7 +143,7 @@ "Generate KeePass COMMAND to run, on GROUP." (format "echo %s | \ keepassxc-cli %s %s %s 2>&1 | \ - egrep -v '[Insert|Enter] password to unlock %s'" + grep -E -v '[Insert|Enter] password to unlock %s'" (shell-quote-argument keepass-mode-password) command keepass-mode-db From 31b84e1f79722af3d0c77bd22d6b36227dae302a Mon Sep 17 00:00:00 2001 From: cnngimenez Date: Sat, 17 Aug 2024 17:27:32 -0300 Subject: [PATCH 2/5] Show entry works: requires flatten parameter on ls for this. --- keepass-mode.el | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/keepass-mode.el b/keepass-mode.el index 45b014a..c28e85e 100644 --- a/keepass-mode.el +++ b/keepass-mode.el @@ -29,6 +29,22 @@ ;;; Code: +(defgroup keepass nil + "KeePass/KeePassXC integration with Emacs." + :group 'convenience + :tag "keepass-mode" + :prefix "keepass-mode-") + +(defcustom keepass-mode-ls-recursive t + "Should list entries recursively? +If nil, it will only show the first level of entries and folders. + +Tip: Recursive list may be useful when searching for a key in a buffer with +\\[isearch-forward] (command `isearch-forward'). However, it may be too slow with a +large KeePass database file." + :type 'boolean + :group 'keepass) + (defvar-local keepass-mode-db "") (defvar-local keepass-mode-password "") (defvar-local keepass-mode-group-path "") @@ -121,7 +137,11 @@ (defun keepass-mode-get-entries (group) "Get entry list for GROUP." - (nbutlast (split-string (shell-command-to-string (keepass-mode-command (keepass-mode-quote-unless-empty group) "ls")) "\n") 1)) + (nbutlast (split-string (shell-command-to-string + (keepass-mode-command (keepass-mode-quote-unless-empty group) + (if keepass-mode-ls-recursive + "ls -R -f" + "ls"))) "\n") 1)) (defun keepass-mode-concat-group-path (group) "Concat GROUP and group path." From 660e18a143d7f50d222e6346f4693c5cacea4414 Mon Sep 17 00:00:00 2001 From: cnngimenez Date: Sat, 17 Aug 2024 17:32:17 -0300 Subject: [PATCH 3/5] Making flycheck happy. --- keepass-mode.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/keepass-mode.el b/keepass-mode.el index c28e85e..604e983 100644 --- a/keepass-mode.el +++ b/keepass-mode.el @@ -121,7 +121,8 @@ large KeePass database file." ;;;###autoload (define-derived-mode keepass-mode tabulated-list-mode "KeePass" - "KeePass mode for interacting with the KeePass DB. \\{keepass-mode-map}." + "KeePass mode for interacting with the KeePass DB. +\\{keepass-mode-map}." (setq-local keepass-mode-db buffer-file-truename) (when (zerop (length keepass-mode-password)) (setq-local keepass-mode-password (keepass-mode-ask-password))) From 8994419a3f1e4284df622b2db972c329d81ad30a Mon Sep 17 00:00:00 2001 From: cnngimenez Date: Sat, 17 Aug 2024 18:01:25 -0300 Subject: [PATCH 4/5] Font-lock to highlight directories. --- keepass-mode.el | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/keepass-mode.el b/keepass-mode.el index 604e983..d506848 100644 --- a/keepass-mode.el +++ b/keepass-mode.el @@ -119,10 +119,20 @@ large KeePass database file." (define-key map (kbd "c") 'keepass-mode-copy-password) map)) +(defface keepass-mode-font-lock-directory + '((t (:inherit dired-directory))) + "Face used for directory entries." + :group 'keepass) + +(defconst keepass-mode-font-lock-keywords + '(("^.*/" (0 'keepass-mode-font-lock-directory t))) + "Font-lock keywords for `keepass-mode'.") + ;;;###autoload (define-derived-mode keepass-mode tabulated-list-mode "KeePass" "KeePass mode for interacting with the KeePass DB. \\{keepass-mode-map}." + (setq-local font-lock-defaults '(keepass-mode-font-lock-keywords nil t)) (setq-local keepass-mode-db buffer-file-truename) (when (zerop (length keepass-mode-password)) (setq-local keepass-mode-password (keepass-mode-ask-password))) From cf94171693b3d5365f676ae6dc6884ffe739075f Mon Sep 17 00:00:00 2001 From: cnngimenez Date: Sun, 18 Aug 2024 12:07:39 -0300 Subject: [PATCH 5/5] Do not use password on command line. Pass the password through stdin by using a temporal buffer instead of using the command line (which can be seen on any terminal by using the ps program). Fix issue ifosch/keepass-mode#21 --- keepass-mode.el | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/keepass-mode.el b/keepass-mode.el index d506848..16f63ca 100644 --- a/keepass-mode.el +++ b/keepass-mode.el @@ -142,17 +142,36 @@ large KeePass database file." (add-to-list 'auto-mode-alist '("\\.kdbx\\'" . keepass-mode)) (add-to-list 'auto-mode-alist '("\\.kdb\\'" . keepass-mode)) +(defconst keepass-mode-output-buffer "*keepass-mode-command-output*" + "Buffer name used for the keepassxc-cli command output.") + +(defun keepass-mode-call-command (group command) + "Call the keepassxc-cli command and return its output. +GROUP and COMMAND are passed to `keepass-mode-command'. They are strings with +the group to process (the directory) and the keepass command (for example: +\"ls\", \"show\")." + (let ((password (shell-quote-argument keepass-mode-password)) + (filepath keepass-mode-db)) + (with-current-buffer (get-buffer-create keepass-mode-output-buffer) + (delete-region (point-min) (point-max))) + (with-temp-buffer + (insert password) + (call-shell-region (point-min) (point-max) + (keepass-mode-command group command filepath) + t keepass-mode-output-buffer)) + (with-current-buffer keepass-mode-output-buffer + (buffer-string)))) + (defun keepass-mode-get (field entry) "Retrieve FIELD from ENTRY." - (keepass-mode-get-field field (shell-command-to-string (keepass-mode-command (keepass-mode-quote-unless-empty entry) "show -s")))) + (keepass-mode-get-field field (keepass-mode-call-command (keepass-mode-quote-unless-empty entry) "show -s"))) (defun keepass-mode-get-entries (group) "Get entry list for GROUP." - (nbutlast (split-string (shell-command-to-string - (keepass-mode-command (keepass-mode-quote-unless-empty group) - (if keepass-mode-ls-recursive - "ls -R -f" - "ls"))) "\n") 1)) + (nbutlast (split-string (keepass-mode-call-command (keepass-mode-quote-unless-empty group) + (if keepass-mode-ls-recursive + "ls -R -f" + "ls")) "\n") 1)) (defun keepass-mode-concat-group-path (group) "Concat GROUP and group path." @@ -164,22 +183,20 @@ large KeePass database file." (defun keepass-mode-get-entry (entry) "Get ENTRY details." - (shell-command-to-string (keepass-mode-command (keepass-mode-quote-unless-empty entry) "show"))) + (keepass-mode-call-command (keepass-mode-quote-unless-empty entry) "show")) (defun keepass-mode-get-field (field entry) "Get FIELD from an ENTRY." (keepass-mode-get-value-from-alist field (keepass-mode-read-data-from-string entry))) -(defun keepass-mode-command (group command) +(defun keepass-mode-command (group command &optional db) "Generate KeePass COMMAND to run, on GROUP." - (format "echo %s | \ - keepassxc-cli %s %s %s 2>&1 | \ + (format "keepassxc-cli %s %s %s 2>&1 | \ grep -E -v '[Insert|Enter] password to unlock %s'" - (shell-quote-argument keepass-mode-password) command - keepass-mode-db + (or db keepass-mode-db) group - keepass-mode-db)) + (or db keepass-mode-db))) (defun keepass-mode-quote-unless-empty (text) "Quote TEXT unless it's empty."