Debian Codegraph @

kwalletcli_getpin

raw
1 #!/usr/bin/env mksh
2 # $MirOS: contrib/hosted/tg/code/kwalletcli/kwalletcli_getpin,v 1.23 2016/08/30 17:38:44 tg Exp $
3 #-
4 # Copyright © 2009, 2010, 2011, 2016
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 \
23 LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT LC_IDENTIFICATION
24 [[ -z $LC_CTYPE ]] && LC_CTYPE=en_US.UTF-8
25 export LANG=C LC_CTYPE LC_MESSAGES GPG_TERM GPG_TTY
26 set -U
27
28 quiet=0
29 nl='
30 '
31
32 function die {
33 local rv=$1
34
35 shift
36 (( quiet )) || print -u2 -r -- "$@"
37 exit $rv
38 }
39
40 function checkline {
41 local res
42
43 print -pr -- "$@"
44 read -p res || die 3 pinentry died
45 [[ $res = OK@(| *) ]] || die 4 "got '$res' for '$*'"
46 }
47
48 function toutf8 {
49 local intext="$*" outtext=''
50 typeset -i i=0 n=${#intext}
51 typeset -i1 c
52 typeset -Uui16 -Z5 x
53
54 while (( i < n )); do
55 c=1#${intext:(i++):1}
56 if (( (c & 0xFF80) == 0xEF80 )); then
57 (( c &= 0xFF ))
58 fi
59 if (( c < 32 || c == 1#% || c == 1#_ )); then
60 let x=c
61 outtext=${outtext}%${x#16#}
62 elif (( c > 0x7E && c < 0xA0 )); then
63 outtext=${outtext}�
64 else
65 outtext=${outtext}${c#1#}
66 fi
67 done
68
69 print -nr -- "$outtext"
70 }
71
72 function tolat1 {
73 local intext="$*" outtext=''
74 typeset -i i=0 n=${#intext}
75 typeset -i1 c
76 typeset -Uui16 -Z5 x
77
78 while (( i < n )); do
79 c=1#${intext:(i++):1}
80 (( c = c < 0x80 ? c : (c & 0xFF80) == 0xEF80 ? c :
81 c < 0x0100 ? c | 0xEF00 : 0xEFFF ))
82 outtext=${outtext}${c#1#}
83 done
84
85 print -nr -- "$outtext"
86 }
87
88 function usage {
89 print -u2 "Usage:\tkwalletcli_getpin [-q] [-b | -m | -p prompt]"
90 print -u2 "\t[-E | -e errortext] [-N NoBtn] [-t desctext] [-Y YesBtn]"
91 print -u2 "Returns: 0=ok (pw on stdout), 1=cancel, 2+=error"
92 exit 2
93 }
94
95 function get_tty {
96 GPG_TTY=$(tty) || GPG_TTY=
97 [[ -n $GPG_TTY ]] || die 5 Please set GPG_TTY yourself
98 }
99
100
101 asktext='Hast Du das Paßwort?'
102 askprompt='Code:'
103 askerror=''
104 askbool=0
105 btnyes=
106 btnno=
107
108 while getopts 'bEe:hmN:p:qt:Y:' ch; do
109 case $ch {
110 (b) askbool=1 ;;
111 (E) askerror='' ;;
112 (e) askerror=$OPTARG ;;
113 (m) askbool=2 ;;
114 (N) btnno=$OPTARG ;;
115 (p) askprompt=$OPTARG ;;
116 (q) quiet=1 ;;
117 (t) asktext=$OPTARG ;;
118 (Y) btnyes=$OPTARG ;;
119 (*) usage ;;
120 }
121 done
122 shift $((OPTIND - 1))
123 (( $# )) && usage
124
125 set -A pe_args
126 if [[ -n $DISPLAY ]]; then
127 pe_args[${#pe_args[*]}]=--display
128 pe_args[${#pe_args[*]}]=$DISPLAY
129 fi
130
131 [[ -n $PINENTRY ]] || PINENTRY=pinentry
132 if "${PINENTRY%-kwallet}" --version >/dev/null 2>&1; then
133 "${PINENTRY%-kwallet}" "${pe_args[@]}" |&
134 subpid=$?
135 read -p res || die 3 pinentry died
136 [[ $res = OK@(| *) ]] || die 4 "got '$res' for '$*'"
137 got_pe=1
138 trap "kill $subpid; die 6 caught SIGHUP" 1
139 trap "kill $subpid; die 6 caught SIGINT" 2
140 trap "kill $subpid; die 6 caught SIGQUIT" 3
141 trap "kill $subpid; die 6 caught SIGTRAP" 5
142 trap "kill $subpid; die 6 caught SIGPIPE" 13
143 trap "kill $subpid; die 6 caught SIGTERM" 15
144 else
145 got_pe=0
146 fi
147
148 [[ -z $DISPLAY && -z $GPG_TTY ]] && get_tty
149 [[ -z $GPG_TERM ]] && GPG_TERM=${TERM:-vt100}
150
151 if (( !got_pe )); then
152 thetext=
153 sep=
154 (( askbool )) || if [[ -n $askerror ]]; then
155 thetext="*ERROR*: $(toutf8 "$askerror")"
156 sep=$nl$nl
157 fi
158 if [[ -n $asktext ]]; then
159 thetext=$thetext$sep$(toutf8 "$asktext")
160 sep=$nl$nl
161 fi
162 (( askbool )) || if [[ -n $askprompt ]]; then
163 thetext=$thetext$sep$(toutf8 "$askprompt")
164 fi
165
166 whence -p xmessage >/dev/null || unset DISPLAY
167 if (( askbool )) && [[ -n $DISPLAY ]]; then
168 buttons=${btnyes:-OK},${btnno:-Cancel}
169 (( askbool == 2 )) && buttons=${btnyes:-OK}
170 LC_ALL=C xmessage -nearmouse -buttons "$buttons" \
171 -default "${btnyes:-OK}" "$(tolat1 "$thetext")"
172 rc=$?
173 (( rc == 101 )) && exit 0
174 (( rc == 102 )) && exit 1
175 die 3 xmessage errored out
176 fi
177 if (( askbool == 2 )); then
178 thetext="$thetext$nl${nl}[OK${btnyes:+=$btnyes}] "
179 elif (( askbool )); then
180 thetext="$thetext$nl${nl}[Y${btnyes:+=$btnyes}|N${btnno:+=$btnno}] "
181 elif [[ -n $askprompt ]]; then
182 thetext="$thetext "
183 else
184 thetext=$thetext$nl
185 fi
186 trap 'stty echo; exit 1' 1 2 3 13 15
187 [[ -n $GPG_TTY ]] || get_tty
188 while :; do
189 print -nru2 -- "$thetext"
190 stty -echo
191 IFS= read -r resp
192 rc=$?
193 print -u2
194 stty echo
195 if (( rc )); then
196 exit 3
197 elif (( askbool == 2 )); then
198 exit 0
199 elif (( !askbool )); then
200 print -nr -- "$resp"
201 exit 0
202 fi
203 [[ $resp = ?(1|-1|[Tt]?([Rr][Uu][Ee])|[Yy]?([Ee][Ss])) ]] && \
204 exit 0
205 [[ $resp = @(0|[Ff]?([Aa][Ll][Ss][Ee])|[Nn]?([Oo])) ]] && \
206 exit 1
207 print -u2 "Try again: 1/T[rue]/Y[es] vs 0/F[alse]/N[o]"
208 done 0<>$GPG_TTY 2>&0
209 fi
210
211 [[ -z $GPG_TTY ]] || checkline "OPTION ttyname=$GPG_TTY"
212 [[ -z $GPG_TERM ]] || checkline "OPTION ttytype=$GPG_TERM"
213 [[ -z $LC_CTYPE ]] || checkline "OPTION lc-ctype=$LC_CTYPE"
214 [[ -z $LC_MESSAGES ]] || checkline "OPTION lc-messages=$LC_MESSAGES"
215
216 [[ -n $asktext ]] && checkline "SETDESC $(toutf8 "$asktext")"
217 [[ -n $askprompt ]] && checkline "SETPROMPT $(toutf8 "$askprompt")"
218 [[ -n $askerror ]] && checkline "SETERROR $(toutf8 "$askerror")"
219
220 [[ -n $btnyes ]] && checkline "SETOK $(toutf8 "$btnyes")"
221 [[ -n $btnno ]] && checkline "SETCANCEL $(toutf8 "$btnno")"
222
223 if (( askbool == 2 )); then
224 print -p MESSAGE
225 elif (( askbool )); then
226 print -p CONFIRM
227 else
228 print -p GETPIN
229 fi
230 read -p res || die 3 pinentry died
231 if [[ $res = @(D )* ]]; then
232 pass=${res#D }
233 read -p res || die 3 pinentry died
234 else
235 pass=
236 fi
237 if [[ $res = OK@(| *) ]]; then
238 rv=0
239 print -nr -- "$pass"
240 else
241 rv=1
242 fi
243 exec 3>&p; exec 3>&-
244 exit $rv