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
_help() {
echo "
echo "
sbean: Spilling the beans, a keepassxc cli wrapper
commands:
- apt install keepassxc:
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
syntax:
[db-file] [key-file] [command] [[entry-name]] [[options]]
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() {
@@ -51,29 +50,37 @@ _create() {
}
_ls() {
keepassxc-cli ls -k "$DBKEY" --no-password -R -f "$DBPATH"
keepassxc-cli ls -k "$DBKEY" --no-password -R -f "$DBPATH"
}
_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"
fi
}
_get_user() {
if [[ -z "$ENTRY" ]]; then _error "entry required"; fi
keepassxc-cli show -k "$DBKEY" --no-password -a username "$DBPATH" "$ENTRY"
if [[ -z "$ENTRY" ]]; then _error "entry required"; fi
keepassxc-cli show -k "$DBKEY" --no-password -a username "$DBPATH" "$ENTRY"
}
_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"
else
keepassxc-cli show -k "$DBKEY" --no-password -a password "$DBPATH" "$ENTRY" | base64 -d
fi
}
_open() {
keepassxc-cli open -k "$DBKEY" --no-password "$DBPATH"
keepassxc-cli open -k "$DBKEY" --no-password "$DBPATH"
}
_openx() {
keepassxc --keyfile "$DBKEY" "$DBPATH"
keepassxc --keyfile "$DBKEY" "$DBPATH"
}
for (( i=1; i<=$#; i++ )); do
value=${!i}
@@ -82,6 +89,8 @@ for (( i=1; i<=$#; i++ )); do
[[ "$value" =~ -h ]] && { _help; }
[[ "$value" =~ --help ]] && { _help; }
[[ "$value" =~ --user ]] && { ENTRY_USER="${!j}"; }
[[ "$value" =~ --file ]] && { ENTRY_FILE="${!j}"; }
[[ "$value" =~ --decode ]] && { ENTRY_DECODE="true"; continue; }
i=$j
continue
}
@@ -92,22 +101,22 @@ for (( i=1; i<=$#; i++ )); do
done
if [[ ! "$CMD" = install ]]; then
which keepassxc-cli &> /dev/null || {
echo Missing keepassxc-cli >/dev/stderr
exit 1
}
which keepassxc-cli &> /dev/null || {
echo Missing keepassxc-cli >/dev/stderr
exit 1
}
fi
case $CMD in
install) _install;;
create) _create;;
ls) _ls;;
add) _add;;
get-user) _get_user;;
get-pass*) _get_pass;;
open) _open;;
openx) _openx;;
*) _help;;
install) _install ;;
create) _create ;;
ls) _ls ;;
add) _add ;;
get-user) _get_user ;;
get-pass*) _get_pass ;;
open) _open ;;
openx) _openx ;;
*) _help ;;
esac