simplify sbean

This commit is contained in:
2023-10-17 13:41:33 +03:00
parent 74a35674e1
commit 6eec25d376

View File

@@ -1,28 +1,27 @@
#!/bin/bash #!/bin/bash
_help() { _help() {
echo " echo "
sbean: Spilling the beans, a keepassxc cli wrapper sbean: Spilling the beans, a keepassxc cli wrapper
commands: syntax:
- apt install keepassxc: [db-file] [key-file] [command] [[entry-name]] [[options]]
install
- create database
[db-file] [key-file] create
- list entries
[db-file] [key-file] ls
- add new entry, password through stdin, or interactive
[db-file] [key-file] add [entry-name] --user user-name
- get user name
[db-file] [key-file] get-user [entry-name]
- get password
[db-file] [key-file] get-password [entry-name]
- open database using cli
[db-file] [key-file] open
- open database using keepassxc:
[db-file] [key-file] openx
Commands:
install apt install keepassxc
create database create
ls list entries
add [entry-name] add new entry, password asked interactively, or use stdin
options:
--user optional user-name
--file use a file as password (may be binary)
get-user [entry-name] get user name
get-password [entry-name] get password
options:
--decode use if --file was used to add
open open database using cli
openx open database using keepassxc
" "
exit exit
} }
_exit() { _exit() {
@@ -51,29 +50,37 @@ _create() {
} }
_ls() { _ls() {
keepassxc-cli ls -k "$DBKEY" --no-password -R -f "$DBPATH" keepassxc-cli ls -k "$DBKEY" --no-password -R -f "$DBPATH"
} }
_add() { _add() {
if [[ -z "$ENTRY" ]]; then _error "entry required"; fi if [[ -z "$ENTRY" ]]; then _error "entry required"; fi
if [[ -e "$ENTRY_FILE" ]]; then
cat "$ENTRY_FILE" | base64 -w 0 | keepassxc-cli add -k "$DBKEY" --no-password "$DBPATH" -p -u "$ENTRY_USER" "$ENTRY"
else
keepassxc-cli add -k "$DBKEY" --no-password "$DBPATH" -p -u "$ENTRY_USER" "$ENTRY" keepassxc-cli add -k "$DBKEY" --no-password "$DBPATH" -p -u "$ENTRY_USER" "$ENTRY"
fi
} }
_get_user() { _get_user() {
if [[ -z "$ENTRY" ]]; then _error "entry required"; fi if [[ -z "$ENTRY" ]]; then _error "entry required"; fi
keepassxc-cli show -k "$DBKEY" --no-password -a username "$DBPATH" "$ENTRY" keepassxc-cli show -k "$DBKEY" --no-password -a username "$DBPATH" "$ENTRY"
} }
_get_pass() { _get_pass() {
if [[ -z "$ENTRY" ]]; then _error "entry required"; fi if [[ -z "$ENTRY" ]]; then _error "entry required"; fi
if [[ -z "$ENTRY_DECODE" ]]; then
keepassxc-cli show -k "$DBKEY" --no-password -a password "$DBPATH" "$ENTRY" keepassxc-cli show -k "$DBKEY" --no-password -a password "$DBPATH" "$ENTRY"
else
keepassxc-cli show -k "$DBKEY" --no-password -a password "$DBPATH" "$ENTRY" | base64 -d
fi
} }
_open() { _open() {
keepassxc-cli open -k "$DBKEY" --no-password "$DBPATH" keepassxc-cli open -k "$DBKEY" --no-password "$DBPATH"
} }
_openx() { _openx() {
keepassxc --keyfile "$DBKEY" "$DBPATH" keepassxc --keyfile "$DBKEY" "$DBPATH"
} }
for (( i=1; i<=$#; i++ )); do for (( i=1; i<=$#; i++ )); do
value=${!i} value=${!i}
@@ -82,6 +89,8 @@ for (( i=1; i<=$#; i++ )); do
[[ "$value" =~ -h ]] && { _help; } [[ "$value" =~ -h ]] && { _help; }
[[ "$value" =~ --help ]] && { _help; } [[ "$value" =~ --help ]] && { _help; }
[[ "$value" =~ --user ]] && { ENTRY_USER="${!j}"; } [[ "$value" =~ --user ]] && { ENTRY_USER="${!j}"; }
[[ "$value" =~ --file ]] && { ENTRY_FILE="${!j}"; }
[[ "$value" =~ --decode ]] && { ENTRY_DECODE="true"; continue; }
i=$j i=$j
continue continue
} }
@@ -92,22 +101,22 @@ for (( i=1; i<=$#; i++ )); do
done done
if [[ ! "$CMD" = install ]]; then if [[ ! "$CMD" = install ]]; then
which keepassxc-cli &> /dev/null || { which keepassxc-cli &> /dev/null || {
echo Missing keepassxc-cli >/dev/stderr echo Missing keepassxc-cli >/dev/stderr
exit 1 exit 1
} }
fi fi
case $CMD in case $CMD in
install) _install;; install) _install ;;
create) _create;; create) _create ;;
ls) _ls;; ls) _ls ;;
add) _add;; add) _add ;;
get-user) _get_user;; get-user) _get_user ;;
get-pass*) _get_pass;; get-pass*) _get_pass ;;
open) _open;; open) _open ;;
openx) _openx;; openx) _openx ;;
*) _help;; *) _help ;;
esac esac