| 1 |
#!/usr/bin/env mksh |
| 2 |
rcsid='$MirOS: contrib/hosted/tg/code/kwalletcli/pinentry-kwallet,v 1.16 2016/08/30 17:37:59 tg Exp $' |
| 3 |
#- |
| 4 |
# Copyright © 2009, 2010, 2011 |
| 5 |
# mirabilos <[email protected]> |
| 6 |
# |
| 7 |
# Provided that these terms and disclaimer and all copyright notices |
| 8 |
# are retained or reproduced in an accompanying document, permission |
| 9 |
# is granted to deal in this work without restriction, including un‐ |
| 10 |
# limited rights to use, publicly perform, distribute, sell, modify, |
| 11 |
# merge, give away, or sublicence. |
| 12 |
# |
| 13 |
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to |
| 14 |
# the utmost extent permitted by applicable law, neither express nor |
| 15 |
# implied; without malicious intent or gross negligence. In no event |
| 16 |
# may a licensor, author or contributor be held liable for indirect, |
| 17 |
# direct, other damage, loss, or other issues arising in any way out |
| 18 |
# of dealing in the work, even if advised of the possibility of such |
| 19 |
# damage or existence of a defect, except proven that it results out |
| 20 |
# of said person’s immediate fault when using the work as intended. |
| 21 |
|
| 22 |
unset LC_ALL LANGUAGE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES \ |
| 23 |
LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT LC_IDENTIFICATION |
| 24 |
export LANG=C LC_CTYPE=en_US.UTF-8 |
| 25 |
set -U |
| 26 |
|
| 27 |
iodebug=0 |
| 28 |
iodp=~/pinentry-kwallet.debug |
| 29 |
if (( iodebug )); then |
| 30 |
print "\n$$ === new $(date)" >>"$iodp" |
| 31 |
chmod 0600 "$iodp" |
| 32 |
fi |
| 33 |
function io_p_in { |
| 34 |
local io_line |
| 35 |
IFS= read -r io_line || return $? |
| 36 |
(( iodebug )) && print -r -- "$$ <p $io_line" >>"$iodp" |
| 37 |
eval $1='${io_line%
}' |
| 38 |
return 0 |
| 39 |
} |
| 40 |
function io_s_in { |
| 41 |
local io_line |
| 42 |
IFS= read -pr io_line || return $? |
| 43 |
(( iodebug )) && print -r -- "$$ <s $io_line" >>"$iodp" |
| 44 |
eval $1=\$io_line |
| 45 |
return 0 |
| 46 |
} |
| 47 |
function io_p_out { |
| 48 |
(( iodebug )) && print -r -- "$$ >p $(print "$@")" >>"$iodp" |
| 49 |
print "$@" |
| 50 |
} |
| 51 |
function io_s_out { |
| 52 |
(( iodebug )) && print -r -- "$$ >s $(print "$@")" >>"$iodp" |
| 53 |
print -p "$@" |
| 54 |
} |
| 55 |
function log { |
| 56 |
(( iodebug )) && print -r -- "$$ LOG $*" >>"$iodp" |
| 57 |
} |
| 58 |
|
| 59 |
if [[ -n $PINENTRY_KWALLET ]]; then |
| 60 |
io_p_out ERR 7 trying to call me recursively |
| 61 |
while io_p_in line; do |
| 62 |
io_p_out ERR 7 trying to call me recursively |
| 63 |
done |
| 64 |
exit 1 |
| 65 |
fi |
| 66 |
|
| 67 |
quiet=0 |
| 68 |
set -A args |
| 69 |
last= |
| 70 |
i=0 |
| 71 |
for arg in "$@"; do |
| 72 |
log "argv[$((++i))]='${arg//\'/\'\\\'\'}'" |
| 73 |
if [[ $last = --display ]]; then |
| 74 |
v=DISPLAY |
| 75 |
last=specvar |
| 76 |
elif [[ $last = --ttyname ]]; then |
| 77 |
v=GPG_TTY |
| 78 |
last=specvar |
| 79 |
elif [[ $last = --ttytype ]]; then |
| 80 |
v=GPG_TERM |
| 81 |
last=specvar |
| 82 |
elif [[ $last = --lc-type ]]; then |
| 83 |
v=LC_CTYPE |
| 84 |
last=specvar |
| 85 |
elif [[ $last = --lc-messages ]]; then |
| 86 |
v=LC_MESSAGES |
| 87 |
last=specvar |
| 88 |
fi |
| 89 |
if [[ $last = specvar ]]; then |
| 90 |
eval $v=\$arg |
| 91 |
eval export $v |
| 92 |
last=addone |
| 93 |
fi |
| 94 |
if [[ $last = addone ]]; then |
| 95 |
args[${#args[*]}]=$arg |
| 96 |
last= |
| 97 |
continue |
| 98 |
fi |
| 99 |
last= |
| 100 |
if [[ $arg = -V ]]; then |
| 101 |
(( quiet )) || print -ru2 -- "$rcsid" |
| 102 |
exit 0 |
| 103 |
elif [[ $arg = --version ]]; then |
| 104 |
print -r -- "$rcsid" |
| 105 |
exit 0 |
| 106 |
elif [[ $arg = @(-h|--help) ]]; then |
| 107 |
print "Usage: pinentry-kwallet [options]" |
| 108 |
exit 0 |
| 109 |
elif [[ $arg = -q ]]; then |
| 110 |
quiet=1 |
| 111 |
elif [[ $arg = \ |
| 112 |
@(-@(d|e|g)|--@(debug|enhanced|no-global-grab)) ]]; then |
| 113 |
args[${#args[*]}]=$arg |
| 114 |
elif [[ $arg = \ |
| 115 |
--@(display|ttyname|ttytype|lc-type|lc-messages) ]]; then |
| 116 |
args[${#args[*]}]=$arg |
| 117 |
last=$arg |
| 118 |
elif [[ $arg = --parent-wid ]]; then |
| 119 |
args[${#args[*]}]=$arg |
| 120 |
last=addone |
| 121 |
else |
| 122 |
(( quiet )) || print -u2 "warning: unknown argument '$arg'" |
| 123 |
args[${#args[*]}]=$arg |
| 124 |
fi |
| 125 |
done |
| 126 |
|
| 127 |
if [[ -z $DISPLAY ]]; then |
| 128 |
log "since DISPLAY is not set, replacing with: ${PINENTRY:-pinentry}" |
| 129 |
PINENTRY_KWALLET=set exec "${PINENTRY:-pinentry}" "${args[@]}" |
| 130 |
fi |
| 131 |
|
| 132 |
PINENTRY_KWALLET=set "${PINENTRY:-pinentry}" "${args[@]}" |& |
| 133 |
copid=$? |
| 134 |
log "starting coproc $copid: PINENTRY_KWALLET=set '${PINENTRY:-pinentry}' args" |
| 135 |
io_s_in resp || resp='ERR 14 no coprocess' |
| 136 |
if [[ $resp = OK@(| *) ]]; then |
| 137 |
have_sub=1 |
| 138 |
trap "kill $copid; exit 255" 1 2 3 5 13 15 |
| 139 |
else |
| 140 |
have_sub=0 |
| 141 |
exec 3>&p; exec 3>&- |
| 142 |
fi |
| 143 |
log have_sub=$have_sub |
| 144 |
io_p_out OK ready to listen to your demands |
| 145 |
|
| 146 |
x_dsctxt= |
| 147 |
x_prompt= |
| 148 |
x_errtxt= |
| 149 |
|
| 150 |
function getit { |
| 151 |
local type=$1 key=〈${x_prompt}〉$x_dsctxt pw rv tw=0 d errcnt blst=0 |
| 152 |
|
| 153 |
copyline=0 |
| 154 |
# the errcnt handling is a little tricky, because GnuPG v2 does |
| 155 |
# not reuse the pinentry session (suckers, unable to... *rant*) |
| 156 |
if pw=$(kwalletcli -q -f pinentry-kwallet -e "$type-B-$key") && \ |
| 157 |
[[ $pw = yes* ]]; then |
| 158 |
log "blacklisted" |
| 159 |
blst=1 |
| 160 |
elif pw=$(kwalletcli -q -f pinentry-kwallet -e "$type-e-$key"); then |
| 161 |
log "read errcnt: '$pw'" |
| 162 |
set -A errcnt -- $pw |
| 163 |
d=$(date -u +'%s') |
| 164 |
(( errcnt[0] < (d - 15) )) && errcnt[1]=0 |
| 165 |
else |
| 166 |
log "read errcnt failed" |
| 167 |
fi |
| 168 |
(( blst )) || [[ -z $x_errtxt ]] || (( errcnt[1]++ )) |
| 169 |
(( blst )) || if (( errcnt[1] )); then |
| 170 |
errcnt[0]=${d:-$(date -u +'%s')} |
| 171 |
kwalletcli -q -f pinentry-kwallet -e "$type-e-$key" \ |
| 172 |
-p "${errcnt[*]}" |
| 173 |
log "write errcnt: '${errcnt[*]}' -> $?" |
| 174 |
fi |
| 175 |
(( blst )) || if (( errcnt[1] < 2 )); then |
| 176 |
pw=$(kwalletcli -q -f pinentry-kwallet -e "$type-v-$key") |
| 177 |
rv=$? |
| 178 |
log "read pass $rv: '$pw'" |
| 179 |
case $type:$rv { |
| 180 |
(bool:0) |
| 181 |
if [[ $pw = \ |
| 182 |
@(1|-1|[Tt][Rr][Uu][Ee]|[Yy][Ee][Ss]) ]]; then |
| 183 |
io_p_out OK |
| 184 |
return |
| 185 |
elif [[ $pw = \ |
| 186 |
@(0|[Ff][Aa][Ll][Ss][Ee]|[Nn][Oo]) ]]; then |
| 187 |
io_p_out ERR 128 not confirmed |
| 188 |
return |
| 189 |
fi |
| 190 |
;; |
| 191 |
(pass:0) |
| 192 |
[[ -n $pw ]] && io_p_out -r "D $pw" |
| 193 |
io_p_out OK |
| 194 |
return |
| 195 |
;; |
| 196 |
} |
| 197 |
fi |
| 198 |
if (( !have_sub )); then |
| 199 |
io_p_out ERR 14 no coprocess |
| 200 |
return |
| 201 |
fi |
| 202 |
if [[ $type = bool ]]; then |
| 203 |
io_s_out CONFIRM |
| 204 |
io_s_in resp |
| 205 |
case $resp { |
| 206 |
(OK@(| *)) |
| 207 |
pw=1 |
| 208 |
tw=1 |
| 209 |
;; |
| 210 |
(ERR\ @(128|83886194|83886179)@(| *)) |
| 211 |
# 128 = not confirmed (hardy) |
| 212 |
# 83886194 = not confirmed (wheezy) |
| 213 |
# 83886179 = canceled (wheezy) |
| 214 |
pw=0 |
| 215 |
tw=1 |
| 216 |
;; |
| 217 |
} |
| 218 |
else |
| 219 |
io_s_out GETPIN |
| 220 |
io_s_in resp |
| 221 |
pw= |
| 222 |
#XXX normally, read until OK|ERR |
| 223 |
if [[ $resp = @(D )* ]]; then |
| 224 |
pw=${resp#D } |
| 225 |
io_s_in resp |
| 226 |
fi |
| 227 |
[[ $resp = OK@(| *) ]] && tw=1 |
| 228 |
fi |
| 229 |
(( tw && !blst )) && if kwalletcli_getpin -q -b \ |
| 230 |
-t "Do you want to store your response for description |
| 231 |
'$x_dsctxt', |
| 232 |
prompt '$x_prompt' in the KDE Wallet?"; then |
| 233 |
kwalletcli -q -f pinentry-kwallet -e "$type-v-$key" -p "$pw" |
| 234 |
log "want store: yes, pw '$pw' -> $?" |
| 235 |
else |
| 236 |
# create blacklist entry for this answer |
| 237 |
kwalletcli -q -f pinentry-kwallet -e "$type-B-$key" -p yes |
| 238 |
log "want store: no" |
| 239 |
fi |
| 240 |
[[ $type = pass ]] && [[ -n $pw ]] && io_p_out -r "D $pw" |
| 241 |
io_p_out -r -- "$resp" |
| 242 |
} |
| 243 |
|
| 244 |
while io_p_in line; do |
| 245 |
[[ -z $line || $line = '#'* ]] && continue |
| 246 |
copyline=1 |
| 247 |
case $line { |
| 248 |
(SETDESC) |
| 249 |
x_dsctxt= |
| 250 |
;; |
| 251 |
(SETDESC\ *) |
| 252 |
x_dsctxt=${line#SETDESC } |
| 253 |
;; |
| 254 |
(SETPROMPT) |
| 255 |
x_prompt= |
| 256 |
;; |
| 257 |
(SETPROMPT\ *) |
| 258 |
x_prompt=${line#SETPROMPT } |
| 259 |
;; |
| 260 |
(SETTITLE@(| *)) |
| 261 |
;; |
| 262 |
(SET@(OK|CANCEL|NOTOK)@(| *)) |
| 263 |
;; |
| 264 |
(SETERROR) |
| 265 |
x_errtxt= |
| 266 |
;; |
| 267 |
(SETERROR\ *) |
| 268 |
x_errtxt=${line#SETERROR } |
| 269 |
;; |
| 270 |
(SETQUALITYBAR*) |
| 271 |
;; |
| 272 |
(GETPIN) |
| 273 |
getit pass |
| 274 |
;; |
| 275 |
(CONFIRM) |
| 276 |
getit bool |
| 277 |
;; |
| 278 |
(MESSAGE|CONFIRM\ --one-button) |
| 279 |
;; |
| 280 |
(OPTION\ ttyname=*) |
| 281 |
GPG_TTY=${line#*=} |
| 282 |
export GPG_TTY |
| 283 |
;; |
| 284 |
(OPTION\ ttytype=*) |
| 285 |
GPG_TERM=${line#*=} |
| 286 |
export GPG_TERM |
| 287 |
;; |
| 288 |
(OPTION\ lc-ctype=*) |
| 289 |
LC_CTYPE=${line#*=} |
| 290 |
export LC_CTYPE |
| 291 |
;; |
| 292 |
(OPTION\ lc-messages=*) |
| 293 |
LC_MESSAGES=${line#*=} |
| 294 |
export LC_MESSAGES |
| 295 |
;; |
| 296 |
(OPTION\ *) |
| 297 |
;; |
| 298 |
(CONFIRM\ *) |
| 299 |
(( quiet )) || print -ru2 "warning: unknown CONFIRM" \ |
| 300 |
"option ${line#CONFIRM }" |
| 301 |
;; |
| 302 |
(BYE@(| *)) |
| 303 |
io_p_out OK |
| 304 |
break |
| 305 |
;; |
| 306 |
(NOP@(| *)) |
| 307 |
# copy quietly, in case of keepalive |
| 308 |
;; |
| 309 |
(GETINFO\ pid) |
| 310 |
# undocumented, but used by GnuPG v2 |
| 311 |
io_p_out D $$ |
| 312 |
io_p_out OK |
| 313 |
copyline=0 |
| 314 |
;; |
| 315 |
(GETINFO\ version) |
| 316 |
# undocumented, but used by GnuPG v2 |
| 317 |
# just copy it quietly |
| 318 |
#XXX maybe return "ERR 103 unknown command"? |
| 319 |
;; |
| 320 |
(GETINFO\ *) |
| 321 |
# undocumented, but used by GnuPG v2 |
| 322 |
(( quiet )) || print -ru2 "warning: unknown GETINFO" \ |
| 323 |
"capability ${line#GETINFO }" |
| 324 |
;; |
| 325 |
(*) |
| 326 |
(( quiet )) || print -ru2 "warning: unknown line '$line'" |
| 327 |
;; |
| 328 |
} |
| 329 |
(( copyline )) && if (( have_sub )); then |
| 330 |
io_s_out -r -- "$line" |
| 331 |
resp= |
| 332 |
while [[ $resp != @(OK|ERR)@(| *) ]]; do |
| 333 |
io_s_in resp |
| 334 |
io_p_out -r -- "$resp" |
| 335 |
done |
| 336 |
else |
| 337 |
io_p_out OK |
| 338 |
fi |
| 339 |
done |
| 340 |
if (( have_sub )); then |
| 341 |
io_s_out BYE |
| 342 |
io_s_in resp |
| 343 |
fi |
| 344 |
exec 2>/dev/null # avoid "no coprocess" warnings |
| 345 |
exec 3>&p; exec 3>&- |
| 346 |
exit 0 |