aboutsummaryrefslogblamecommitdiffstats
path: root/pinentry-rofi.sh
blob: b6904be3a0eaad2b71a183fd46fdbaed7ed8ad6f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
           
       
 










                                                                          
 


                                                                                       
                                                                                                     

                                                              












                                                                      
               
 


                                      
 



                               
 

                   
                                        
                             
                                          
                                  




                          


















                                                                      
                                                 

                             
                                  
 
                                
 


                               
 

          

 


                          





                                                                     
                                      



                                      

                                         


                                                                              


                                                  



                                                  
                              

                        
















                                                                 
                                            

                                                  




                                                    




                                                                                                            




                                                                 
                                         
                                                                                     

                                                  
                                                     

                                           
                              


                                                  


                                        
                                                                          

                                                          


                                                    
                                                                          



                                                                  

                                          
                                        
                                            
          
                               
                                     

                                          
                                                                   
                                                                   






                                        
                                                             






                                     

                                  
#!/bin/bash
set -eu

# Using inspiration from
#    https://github.com/plattfot/pinentry-rofis
#    by (c) 2018-2023 Fredrik Salomonsson <plattfot <at> posteo <dot> net>
# which was using inspiration from
#    https://gist.github.com/sardemff7/759cbf956bea20d382a6128c641d2746
#    by (c) 2016 Quentin "Sardem FF7" Glidic
# however no line of code was actually taken from either script
# (they are in different language anyway).

# Copyright (c) 2023 Matěj Cepl <mcepl <at> cepl <dot> eu>
# Licensed under the terms of the include license in the file LICENSE.

# Some documentation
# https://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?(pinentry)Protocol
# https://superuser.com/a/1655428
# TODO https://superuser.com/questions/1457167/i-want-to-make-pinentry-use-gui-locally-and-cli-on-ssh
# Even better
# https://github.com/gpg/libassuan/blob/master/doc/assuan.texi

# Although it is called a PIN-Entry, it does allow to enter reasonably
# long strings (at least 2048 characters are supported by every
# pinentry).

# The client using the PIN-Entry has to check for correctness.

# Note that all strings are expected to be encoded as UTF-8;
# PINENTRY takes care of converting it to the locally used codeset.
# To include linefeeds or other special characters, you may
# percent-escape them (i.e. a line feed is encoded as `%0A', the
# percent sign itself is encoded as `%25').

VERSION="0.0.1"

log_debug() {
    echo "$@" >> /tmp/pinentry-log.txt
}

assuan_send() {
    log_debug "assuan_send: $*"
    echo "$@"
}

split_line() {
    rmfirst=${2:-0}
    log_debug "args: \"$1\", ${rmfirst}"
    read -ra out_arr <<< "$1"
    log_debug "out: $(declare -p out_arr)"
    if [ "$rmfirst" -ne 1 ] ; then
        unset "out_arr[0]"
    fi
    echo "${out_arr[@]}"
}

# Originally from https://www.baeldung.com/linux/decoding-encoded-urls
rfc3986-decode() {
    strg="${*}"
    printf '%s' "${strg%%[%+]*}"
    j="${strg#"${strg%%[%+]*}"}"
    strg="${j#?}"
    case "${j}" in "%"* )
        printf '%b' "\\0$(printf '%o' "0x${strg%"${strg#??}"}")"
        strg="${strg#??}"
        ;; "+"* ) printf ' '
        ;;    * ) return
    esac
    if [ -n "${strg}" ] ; then rfc3986-decode "${strg}"; fi
}

# This is something too clever, doesn’t work in the script
# from https://stackoverflow.com/a/70560850/164233
# rfc3986decode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }

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"
win_prompt="Password"
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[*]}"
        win_mesg="$(rfc3986-decode "${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" =~ ^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 <rofi>"
            log_debug "rofi failed to run: ${passw} / ${passw_err}"
            exit $passw_err
        else
            if [[ -n ${passw} ]] ; then
                assuan_send "D ${passw}"
            fi
        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

fi # End of $INSIDE_BATS exclusion