Skip to content

Instantly share code, notes, and snippets.

@erayd

erayd/dmenu.sh Secret

Created January 20, 2020 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erayd/0de7837ce6b5ec2c51e18653ebb79899 to your computer and use it in GitHub Desktop.
Save erayd/0de7837ce6b5ec2c51e18653ebb79899 to your computer and use it in GitHub Desktop.
#!/bin/bash
DMENU_FONT='Hack:pixelsize=20'
MODE="$1"; shift
target="$1"; shift
target_key="$1"; shift
shopt -s nullglob globstar
prefix=${PASSWORD_STORE_DIR-~/.password-store}
password_files=( "$prefix"/**/*.gpg )
password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )
password_files+=( "clipboard" )
if [ -f "$HOME/.config/pass/history" ]; then
history_file="$(tail -n 1 "$HOME/.config/pass/history")"
password_files=( "$history_file" "${password_files[@]}" )
fi
function do_totp()
{
SEED="$1"
while [ $((${#SEED} % 8)) -gt 0 ]; do
SEED="$SEED=";
done
KEY=$(echo "$SEED" | base32 -d | hexdump -ve '/1 "%02X"')
INTERVAL=${2:-30}
PERIOD=$(( $(date +%s) / $INTERVAL ))
DIGEST=$(printf "$(printf %016X "$PERIOD" | sed 's/../\\x\0/g')" | openssl dgst -sha1 -mac HMAC -macopt "hexkey:$KEY" | cut -d\ -f2)
OFFSET=$(( $(printf %d "0x${DIGEST:39}") * 2 ))
TOKEN=$(( (0x${DIGEST:OFFSET:8} & 0x7FFFFFFF) % 1000000 ))
printf '%06d\n' "$TOKEN"
}
[[ -n "$target" ]] || target=$(printf '%s\n' "${password_files[@]}" | dmenu -i -fn "$DMENU_FONT" $@)
[ -d "$HOME/.config/pass" ] || mkdir -p "$HOME/.config/pass"
[ -z "$target" ] || [ "$target" = "$history_file" ] || echo "$target" >> "$HOME/.config/pass/history"
[[ -n "$target" ]] || exit 0
if [ "$target" == "clipboard" ]; then
secret="$(xclip -o -selection clipboard)"
else
secret="$(pass show "$target")"
echo "$secret" | head -n 1 | grep -q '^[^\s]\+:.\+'
if [[ $? -eq 0 ]]; then
key=()
else
key=( 'secret' )
fi
key+=( $(echo "$secret" | grep -v '^\s*$' | grep -P '^[^\s]+:\s*[^\s]+' | sed -r 's/^([^:]+):.+$/\1/' | sort | uniq | tr ' ' '\ ') )
printf '%s\n' "${key[@]}" | grep -q '^login$' && printf '%s\n' "${key[@]}" | grep -q '^secret$'
if [[ $? -eq 0 ]]; then
key+=( 'login+secret' )
fi
if [[ $(echo "$secret" | wc -l) -gt 1 ]]; then
key+=( 'everything' )
fi
if [[ $(printf '%s\n' "${key[@]}" | wc -l ) -gt 1 ]]; then
if [[ -n "$target_key" ]]; then
key="$target_key"
else
key=$(printf '%s\n' "${key[@]}" | dmenu -i -fn "$DMENU_FONT" $@)
fi
fi
if [[ "$key" == "secret" ]]; then
secret="$(echo "$secret" | head -n 1 | sed -r 's/^[^:]+:\s*//')"
elif [[ "$key" == "totp" ]]; then
secret="$(do_totp $(printf '%s\n' "${secret[@]}" | grep "^totp:" | head -n 1 | sed -r 's/^[^:]+:\s*//'))"
elif [[ "$key" == "login+secret" ]]; then
login="$(printf '%s\n' "${secret[@]}" | grep "^login:" | head -n 1 | sed -r 's/^[^:]+:\s*//')"
secret="$(printf '%s\t' "$login")$(echo "$secret" | head -n 1 | sed -r 's/^[^:]+:\s*//')"
elif [[ "$key" != 'everything' ]]; then
secret="$(printf '%s\n' "${secret[@]}" | grep "^$key:" | head -n 1 | sed -r 's/^[^:]+:\s*//')"
fi
fi
if [[ "$MODE" == 'clipboard' ]]; then
echo -n "$secret" | xclip -selection clipboard
elif [[ "$MODE" == 'type' ]]; then
echo -n "$secret" | xdotool type -clearmodifiers --file -
elif [[ "$MODE" == 'echo' ]]; then
echo "$secret"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment