Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cburgmer
Last active November 14, 2023 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cburgmer/061208b4a67b5cab691a1f70122d0b75 to your computer and use it in GitHub Desktop.
Save cburgmer/061208b4a67b5cab691a1f70122d0b75 to your computer and use it in GitHub Desktop.
Sync gopass to Keypass using kpcli
#!/usr/bin/env bash
set -Eeuo pipefail
readonly gopass_prefix=""
generate_empty_store() {
local target_file="$1"
local master_password=""
{
sleep 2
echo "y" # generate it?
sleep 2
echo -n "$master_password"
} | script -q script_output kpcli --command "saveas ${target_file}.kdbx ${target_file}.key"
echo "$master_password" > "${target_file}.pw"
{
sleep 2
echo "y" # save?
} | script -q script_output kpcli --kdb "${target_file}.kdbx" --key "${target_file}.key" --pwfile "${target_file}.pw" --command "mkdir credentials"
rm script_output
}
sync_user() {
local target_file="$1"
local username="$2"
local password
password="$(gopass show "${gopass_prefix}${username}")"
{
echo "$username" # title
echo "$password" # pw
echo "$password" # pw repeat
sleep 10 # work around kpcli flushing stdin?
echo "" # URL
echo "" # tags
sleep 2
echo "F" # finish?
sleep 2
echo "." # notes/comment
sleep 2
echo "y" # save?
} | script -q script_output kpcli --kdb "${target_file}.kdbx" --key "${target_file}.key" --pwfile "${target_file}.pw" --command "new credentials/${username}"
sleep 2
rm script_output
}
clean_up() {
rm -f script_output
}
generate_random_excuse_for_being_so_slow() {
local line=$((1 + RANDOM % 9))
if [[ $((RANDOM % 5)) -ne 0 ]]; then
return;
fi
cat <<EOF | head -$line | tail -1
Mitigating global warming...
Reducing doppler effect...
Temporary routing around anomaly...
Fixing improperly oriented keyboard...
Calibrating cellular telephone interference...
Waiting for bad ether in the cables to clear...
Working around IRQ dropout...
User to computer ration too low, waiting...
EOF
}
all_credentials() {
gopass list -f -s "$gopass_prefix"
}
main() {
local target_file="test-users"
if [[ -f "${target_file}.kdbx.lock" || -f "${target_file}.kdbx" || -f "${target_file}.key" || -f "${target_file}.pw" ]]; then
>&2 echo "Please remove ${target_file}.* first"
exit 1
fi
gopass sync
generate_empty_store "$target_file" > /dev/null
# Handle Ctrl+C
trap clean_up EXIT
while read -r credentials; do
generate_random_excuse_for_being_so_slow
echo "Synching ${credentials}..."
sync_user "$target_file" "$credentials" > /dev/null
done <<< "$(all_credentials)"
echo "Done, see ${target_file}.kdbx, ${target_file}.key, and ${target_file}.pw"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment