From 7e9dd614c58b6a4b00e3326c57ec75f0da68ec13 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Thu, 10 Aug 2023 14:14:55 +0200 Subject: test: remove $INSIDE_BATS home-made protection and use the proper way. If in the end of the script differentiates between the script being sourced and run. --- pinentry-rofi.sh | 253 ++++++++++++++++++++++++------------------------ tests/foundational.bats | 1 - 2 files changed, 126 insertions(+), 128 deletions(-) diff --git a/pinentry-rofi.sh b/pinentry-rofi.sh index 2695a88..13e89cb 100755 --- a/pinentry-rofi.sh +++ b/pinentry-rofi.sh @@ -89,8 +89,6 @@ basturldecode () { rofi_cmd="rofi -dmenu -input /dev/null -password" INSIDE_BATS=${INSIDE_BATS:-0} -if [ "$INSIDE_BATS" -ne 1 ] ; then - assuan_send "OK Please go ahead" win_title="Prompt for password" @@ -99,132 +97,133 @@ win_mesg="" keyinfo="" - - -while : ; do - read -r line - log_debug "line=$line" - # Set options for the connection. The syntax of such a line is - # OPTION name [ [=] value ] - # Leading and trailing spaces around name and value are - # allowed but should be ignored. For compatibility reasons, name - # may be prefixed with two dashes. The use of the equal sign - # is optional but suggested if value is given. - if [[ "$line" =~ ^OPTION ]] ; then - # OPTION grab - # OPTION ttyname=/dev/pts/1 - # OPTION ttytype=tmux-256color - # OPTION lc-messages=C - assuan_send "OK" - elif [[ "$line" =~ ^GETINFO ]] ; then - # https://www.gnupg.org/documentation/manuals/gnupg/Agent-GETINFO.html - # version or pid of this script? - # gpg-agent --version works but it must be filtered - IFS=" " line_arr=($(split_line "$line")) - log_debug "line_arr: ${line_arr[*]}" - subcommand=${line_arr[0]} - log_debug "subcommand=${subcommand}" - if [[ "$subcommand" == "version" ]] ; then - assuan_send "D ${VERSION}" - elif [[ "$subcommand" == "pid" ]] ; then - assuan_send "D $$" - fi - assuan_send "OK" - # This command is reserved for future extensions. - # True NOOP - elif [[ "$line" =~ ^CANCEL ]] ; then - assuan_send "OK" - # This command is reserved for future extensions. Not yet - # specified as we don't implement it in the first phase. See - # Werner's mail to gpa-dev on 2001-10-25 about the rationale - # for measurements against local attacks. - # True NOOP - elif [[ "$line" =~ ^AUTH ]] ; then - assuan_send "OK" - # And this actually is NOOP - elif [[ "$line" =~ ^NOP ]] ; then - assuan_send "OK" - elif [[ "$line" =~ ^KEYINFO ]] ; then - assuan_send "${keyinfo}" - assuan_send "OK" - elif [[ "$line" =~ ^SETKEYINFO ]] ; then - IFS=" " line_arr=($(split_line "$line")) - log_debug "line_arr: ${line_arr[*]}" - if [[ "${line_arr[0]}" =~ ^--clear ]] ; then - keyinfo="" - else - keyinfo="${line_arr[*]}" - fi - assuan_send "OK" - elif [[ "$line" =~ ^SETOK|^SETNOTOK|^SETERROR|^SETCANCEL|^SETTIMEOUT|^SETQUALITYBAR|^SETGENPIN ]] ; then - assuan_send "OK" - elif [[ "$line" =~ ^CONFIRM|^MESSAGE ]] ; then - assuan_send "OK" - # Reset the connection but not any existing authentication. - # The server should release all resources associated with the - # connection. - elif [[ "$line" =~ ^RESET ]] ; then - assuan_send "OK" - elif [[ "$line" =~ ^SETDESC ]] ; then - #SETDESC Please enter the passphrase for the ssh key%0A ke:yf:in:ge:rp:ri:nt - IFS=" " line_arr=($(split_line "$line")) - log_debug "line_arr: ${line_arr[*]}" - temp_str="$(basturldecode "${line_arr[*]}")" - log_debug "temp_str: ${temp_str}" - win_mesg="$(rawurlencode "${temp_str}")" - assuan_send "OK" - elif [[ "$line" =~ ^SETPROMPT ]] ; then - #SETPROMPT Passphrase: - IFS=" " line_arr=($(split_line "$line")) - log_debug "line_arr: ${line_arr[*]}" - win_prompt="${line_arr[0]}" - assuan_send "OK" - elif [[ "$line" =~ ^SETTITLE ]] ; then - IFS=" " line_arr=($(split_line "$line")) - log_debug "line_arr: ${line_arr[*]}" - log_debug "line_arr: ${line_arr[*]}" - temp_str="$(basturldecode "${line_arr[*]}")" - log_debug "temp_str: ${temp_str}" - win_title="$(rawurlencode "${temp_str}")" - assuan_send "OK" - elif [[ "$line" =~ ^GETPIN ]] ; then - passw=None - sys_env="$(systemctl --user show-environment | tr -s " \t\n" " ")" - IFS=" " sys_env_arr=($(split_line "$sys_env" 1)) - log_debug "sys_env_arr: ${sys_env_arr[*]}" - for env_line in "${sys_env_arr[@]}" ; do - log_debug "env_line=${env_line}" - # GPIN_VALID=re.compile(r) - if [[ "$env_line" =~ ^([A-Za-z][A-Za-z_]*)=(.+)$ ]] ; then - log_debug "env_match=${BASH_REMATCH[*]}" - export "${BASH_REMATCH[1]}=${BASH_REMATCH[2]}" +main () { + + while : ; do + read -r line + log_debug "line=$line" + # Set options for the connection. The syntax of such a line is + # OPTION name [ [=] value ] + # Leading and trailing spaces around name and value are + # allowed but should be ignored. For compatibility reasons, name + # may be prefixed with two dashes. The use of the equal sign + # is optional but suggested if value is given. + if [[ "$line" =~ ^OPTION ]] ; then + # OPTION grab + # OPTION ttyname=/dev/pts/1 + # OPTION ttytype=tmux-256color + # OPTION lc-messages=C + assuan_send "OK" + elif [[ "$line" =~ ^GETINFO ]] ; then + # https://www.gnupg.org/documentation/manuals/gnupg/Agent-GETINFO.html + # version or pid of this script? + # gpg-agent --version works but it must be filtered + IFS=" " line_arr=($(split_line "$line")) + log_debug "line_arr: ${line_arr[*]}" + subcommand=${line_arr[0]} + log_debug "subcommand=${subcommand}" + if [[ "$subcommand" == "version" ]] ; then + assuan_send "D ${VERSION}" + elif [[ "$subcommand" == "pid" ]] ; then + assuan_send "D $$" + fi + assuan_send "OK" + # This command is reserved for future extensions. + # True NOOP + elif [[ "$line" =~ ^CANCEL ]] ; then + assuan_send "OK" + # This command is reserved for future extensions. Not yet + # specified as we don't implement it in the first phase. See + # Werner's mail to gpa-dev on 2001-10-25 about the rationale + # for measurements against local attacks. + # True NOOP + elif [[ "$line" =~ ^AUTH ]] ; then + assuan_send "OK" + # And this actually is NOOP + elif [[ "$line" =~ ^NOP ]] ; then + assuan_send "OK" + elif [[ "$line" =~ ^KEYINFO ]] ; then + assuan_send "${keyinfo}" + assuan_send "OK" + elif [[ "$line" =~ ^SETKEYINFO ]] ; then + IFS=" " line_arr=($(split_line "$line")) + log_debug "line_arr: ${line_arr[*]}" + if [[ "${line_arr[0]}" =~ ^--clear ]] ; then + keyinfo="" + else + keyinfo="${line_arr[*]}" + fi + assuan_send "OK" + elif [[ "$line" =~ ^SETOK|^SETNOTOK|^SETERROR|^SETCANCEL|^SETTIMEOUT|^SETQUALITYBAR|^SETGENPIN ]] ; then + assuan_send "OK" + elif [[ "$line" =~ ^CONFIRM|^MESSAGE ]] ; then + assuan_send "OK" + # Reset the connection but not any existing authentication. + # The server should release all resources associated with the + # connection. + elif [[ "$line" =~ ^RESET ]] ; then + assuan_send "OK" + elif [[ "$line" =~ ^SETDESC ]] ; then + #SETDESC Please enter the passphrase for the ssh key%0A ke:yf:in:ge:rp:ri:nt + IFS=" " line_arr=($(split_line "$line")) + log_debug "line_arr: ${line_arr[*]}" + win_mesg="$(basturldecode "${line_arr[*]}")" + assuan_send "OK" + elif [[ "$line" =~ ^SETPROMPT ]] ; then + #SETPROMPT Passphrase: + IFS=" " line_arr=($(split_line "$line")) + log_debug "line_arr: ${line_arr[*]}" + win_prompt="${line_arr[0]}" + assuan_send "OK" + elif [[ "$line" =~ ^SETTITLE ]] ; then + IFS=" " line_arr=($(split_line "$line")) + log_debug "line_arr: ${line_arr[*]}" + log_debug "line_arr: ${line_arr[*]}" + win_title="$(basturldecode "${line_arr[*]}")" + assuan_send "OK" + elif [[ "$line" =~ ^GETPIN ]] ; then + passw=None + sys_env="$(systemctl --user show-environment | tr -s " \t\n" " ")" + IFS=" " sys_env_arr=($(split_line "$sys_env" 1)) + log_debug "sys_env_arr: ${sys_env_arr[*]}" + for env_line in "${sys_env_arr[@]}" ; do + log_debug "env_line=${env_line}" + # GPIN_VALID=re.compile(r) + if [[ "$env_line" =~ ^([A-Za-z][A-Za-z_]*)=(.+)$ ]] ; then + log_debug "env_match=${BASH_REMATCH[*]}" + export "${BASH_REMATCH[1]}=${BASH_REMATCH[2]}" + fi + done + rofi_cmd+=" -p '${win_prompt}'" + rofi_cmd+=" -title '${win_title}'" + if [[ -n "${win_mesg}" ]] ; then + rofi_cmd+=" -mesg '${win_mesg}'" + fi + log_debug "${rofi_cmd}" + passw="$(eval "${rofi_cmd}")" + passw_err=$? + if [[ ${passw_err} -ne 0 ]] ; then + # assuan_send "ERR 83886179 Operation cancelled " + log_debug "rofi failed to run: ${passw} / ${passw_err}" + exit $passw_err + else + if [[ -n ${passw} ]] ; then + assuan_send "D ${passw}" fi - done - rofi_cmd+=" -p '${win_prompt}'" - rofi_cmd+=" -title '${win_title}'" - if [[ -n "${win_mesg}" ]] ; then - rofi_cmd+=" -mesg '${win_mesg}'" - fi - log_debug "${rofi_cmd}" - passw="$(eval "${rofi_cmd}")" - passw_err=$? - if [[ ${passw_err} -ne 0 ]] ; then - # assuan_send "ERR 83886179 Operation cancelled " - log_debug "rofi failed to run: ${passw} / ${passw_err}" - exit $passw_err - else - if [[ -n ${passw} ]] ; then - assuan_send "D ${passw}" fi + assuan_send "OK" + # Close the connection. The server will respond with OK. + elif [[ ${line} =~ ^BYE ]] ; then + exit 0 + else + assuan_send "BYE" + exit 1 fi - assuan_send "OK" - # Close the connection. The server will respond with OK. - elif [[ ${line} =~ ^BYE ]] ; then - exit 0 - else - assuan_send "BYE" - exit 1 - fi -done + done +} + + +if [ "$0" = "$BASH_SOURCE" ]; then + main +fi -fi # End of $INSIDE_BATS exclusion diff --git a/tests/foundational.bats b/tests/foundational.bats index 458b43f..7c69969 100644 --- a/tests/foundational.bats +++ b/tests/foundational.bats @@ -1,7 +1,6 @@ #!/usr/bin/env bats setup() { - export INSIDE_BATS=1 DIR="$( cd "$(dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" # shellcheck source=pinentry-rofi.sh source "$(readlink -f "${DIR}/../pinentry-rofi.sh")" -- cgit