blob: 2695a8899413731ef25e683f306510473229c5cc (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#!/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://github.com/sfinktah/bash/blob/master/rawurlencode.inc.sh
# There is also more explanation and documentation there
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
printf "%s" "${encoded}" # You can either set a return variable (FASTER)
}
rawurldecode() {
# This is perhaps a risky gambit, but since all escape characters must be
# encoded, we can replace %NN with \xNN and pass the lot to printf -b, which
# will decode hex for us
printf '%b' "${1//%/\\x}"
}
# Except Assuan apparently doesn't want really encoded strings, but some kind of bastard
basturldecode () {
echo "$1" | sed -e 's/</\</g' -e 's/>/\>/g'
}
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[*]}"
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]}"
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
|