2023-01-23 16:34:56 +01:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
#
|
|
|
|
|
# Purpose {{{
|
|
|
|
|
# This script will :
|
2024-03-14 17:22:41 +01:00
|
|
|
|
# 1. If no gpg-key is in cache.
|
2023-01-23 16:34:56 +01:00
|
|
|
|
# a. Run a terminal with a tmux splits (at least a split to enter gpg passphrase).
|
|
|
|
|
# b. Wait until timeout.
|
|
|
|
|
# c. Check every second if a key is now cached.
|
|
|
|
|
# d. Kill dedicated terminal emulator window.
|
|
|
|
|
# 2. (When) a gpg-key is finally cached, simply call the "real" rofi-pass.
|
|
|
|
|
#
|
|
|
|
|
# 2023-01-12
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
# GPG command to check cache {{{
|
|
|
|
|
# 0 : No key in cache
|
|
|
|
|
# 1 : At leaste one key in cache
|
|
|
|
|
# }}}
|
|
|
|
|
gpg_agent_info=$(gpg-connect-agent 'keyinfo --list' /bye 2>/dev/null | awk 'BEGIN{CACHED=0} /^S/ {if($7==1){CACHED=1}} END{if($0!=""){print CACHED} else {print "none"}}')
|
|
|
|
|
# Terminal emulator and window title
|
|
|
|
|
TERM_TITLE="Authentication with tmux"
|
|
|
|
|
|
|
|
|
|
TIMEOUT=60
|
|
|
|
|
TIME=1
|
|
|
|
|
|
2024-03-14 17:22:41 +01:00
|
|
|
|
# Get "real" rofi-pass binary path {{{
|
|
|
|
|
if [ -f /bin/rofi-pass ]; then
|
|
|
|
|
ROFI_PASS_BIN="/bin/rofi-pass"
|
|
|
|
|
elif [ -f /usr/bin/rofi-pass ]; then
|
|
|
|
|
ROFI_PASS_BIN="/usr/bin/rofi-pass"
|
|
|
|
|
elif [ -f "${HOME}"/repos/rofi-pass/rofi-pass ]; then
|
|
|
|
|
ROFI_PASS_BIN="${HOME}/repos/rofi-pass/rofi-pass"
|
|
|
|
|
else
|
|
|
|
|
printf '%s' "No rofi-pass binary available"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
# }}}
|
|
|
|
|
|
2023-01-23 16:34:56 +01:00
|
|
|
|
# If gpg-agent doesn't have any key in cache
|
|
|
|
|
if [ "${gpg_agent_info}" -eq 0 ]; then
|
|
|
|
|
## Start a terminal emulator
|
|
|
|
|
## Create new tmux splits to ask for gpg passphrase
|
2023-05-17 10:54:37 +02:00
|
|
|
|
"${TERM_EMULATOR}" --title "${TERM_TITLE}" -e bash -c 'tmux source-file "${HOME}"/.tmux/splitAUTHENTICATION' &
|
2023-01-23 16:34:56 +01:00
|
|
|
|
|
|
|
|
|
# Minimum time to enter my passphrases
|
|
|
|
|
sleep 5
|
|
|
|
|
|
|
|
|
|
# Wait until TIMEOUT
|
|
|
|
|
while [ "${TIME}" -lt "${TIMEOUT}" ]; do
|
|
|
|
|
gpg_agent_info=$(gpg-connect-agent 'keyinfo --list' /bye 2>/dev/null | awk 'BEGIN{CACHED=0} /^S/ {if($7==1){CACHED=1}} END{if($0!=""){print CACHED} else {print "none"}}')
|
|
|
|
|
|
|
|
|
|
## If a gpg key is in cache {{{
|
|
|
|
|
if [ "${gpg_agent_info}" -eq 1 ]; then
|
|
|
|
|
echo "While loop − GPG in cache"
|
|
|
|
|
### Leave the loop
|
|
|
|
|
break
|
|
|
|
|
## }}}
|
|
|
|
|
## Still no key {{{
|
|
|
|
|
else
|
|
|
|
|
## Wait a second
|
|
|
|
|
TIME=$((TIME + 1))
|
|
|
|
|
sleep 1
|
|
|
|
|
fi
|
|
|
|
|
## }}}
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
### Kill any remaining window
|
|
|
|
|
kill $(pgrep --newest --full "${TERM_TITLE}") 2>/dev/null
|
|
|
|
|
|
|
|
|
|
## If a gpg key is finally in cache
|
|
|
|
|
if [ "${gpg_agent_info}" -eq 1 ]; then
|
|
|
|
|
# Then, call real rofi-pass
|
2024-03-14 17:22:41 +01:00
|
|
|
|
"${ROFI_PASS_BIN}"
|
2023-01-23 16:34:56 +01:00
|
|
|
|
else
|
2024-03-14 17:22:41 +01:00
|
|
|
|
exit 2
|
2023-01-23 16:34:56 +01:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# If gpg-agent already have a key in cache {{{
|
2023-05-17 10:54:37 +02:00
|
|
|
|
elif [ "${gpg_agent_info}" -gt 0 ]; then
|
2023-01-23 16:34:56 +01:00
|
|
|
|
# Then, call real rofi-pass
|
2024-03-14 17:22:41 +01:00
|
|
|
|
"${ROFI_PASS_BIN}"
|
2023-05-17 10:54:37 +02:00
|
|
|
|
else # Not supposed to happen
|
2024-03-14 17:22:41 +01:00
|
|
|
|
exit 3
|
2023-01-23 16:34:56 +01:00
|
|
|
|
fi
|
|
|
|
|
# }}}
|