Skip to content

Commit 73b738c

Browse files
committed
feature/dialog: feature provides a wrapper around dialogs
This feature provides a wrapper around dialogs such as password prompts. Work with dialogs becomes more challenging when plymouth is used. Other utilities can also be used to display the dialog. Signed-off-by: Alexey Gladkov <[email protected]>
1 parent 38233a1 commit 73b738c

File tree

6 files changed

+135
-77
lines changed

6 files changed

+135
-77
lines changed

features/dialog/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Feature: dialog
2+
3+
This feature provides a wrapper around dialogs such as password prompts. Work
4+
with dialogs becomes more challenging when using bootsplash/plymouth. Other
5+
utilities can also be used to display the dialog.

features/dialog/config.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
DIALOG_DATADIR ?= $(FEATURESDIR)/dialog/data
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
if [ -z "${__dialog_sh_functions-}" ]; then
5+
__dialog_sh_functions=1
6+
7+
__dialog_plymouth_is_running=
8+
9+
# shellcheck disable=SC2120
10+
dialog_plymouth_is_running()
11+
{
12+
local recheck="${1-}"
13+
14+
if [ -n "$recheck" ] || [ -z "${__dialog_plymouth_is_running-}" ]; then
15+
type -P plymouth >/dev/null && plymouth --ping 2>/dev/null &&
16+
__dialog_plymouth_is_running=1 ||
17+
__dialog_plymouth_is_running=0
18+
fi
19+
20+
[ "$__dialog_plymouth_is_running" = 1 ]
21+
}
22+
23+
__ask_pass_directly()
24+
{
25+
local prompt tries cmd rc
26+
27+
prompt="$1"; shift
28+
tries="$1"; shift
29+
cmd="$1"; shift
30+
31+
! dialog_plymouth_is_running ||
32+
plymouth hide-splash 2>/dev/null
33+
34+
rc=2
35+
36+
while [ "$rc" != 0 ] && [ "$tries" != 0 ]; do
37+
"$cmd"
38+
rc="$?"
39+
tries=$(($tries - 1))
40+
done
41+
42+
! dialog_plymouth_is_running ||
43+
plymouth show-splash 2>/dev/null
44+
45+
return $rc
46+
}
47+
48+
__ask_pass_with_unl0kr()
49+
{
50+
local prompt tries cmd rc
51+
52+
prompt="$1"; shift
53+
tries="$1"; shift
54+
cmd="$1"; shift
55+
56+
! dialog_plymouth_is_running ||
57+
plymouth hide-splash 2>/dev/null
58+
59+
rc=2
60+
61+
while [ "$rc" != 0 ] && [ "$tries" != 0 ]; do
62+
unl0kr 2>/dev/null | "$cmd"
63+
rc="$?"
64+
tries=$(($tries - 1))
65+
done
66+
67+
! dialog_plymouth_is_running ||
68+
plymouth show-splash 2>/dev/null
69+
70+
return $rc
71+
}
72+
73+
__ask_pass_with_plymouth()
74+
{
75+
local prompt tries cmd
76+
77+
prompt="$1"; shift
78+
tries="$1"; shift
79+
cmd="$1"; shift
80+
81+
plymouth ask-for-password \
82+
--prompt "$prompt" \
83+
--number-of-tries="$tries" \
84+
--command="$cmd"
85+
}
86+
87+
dialog_ask_pass()
88+
{
89+
local rc tries ask tempfile
90+
91+
prompt="$1"; shift
92+
tries="$1"; shift
93+
94+
ask=__ask_pass_directly
95+
96+
! dialog_plymouth_is_running || ask=__ask_pass_with_plymouth
97+
! type -P unl0kr >/dev/null || ask=__ask_pass_with_unl0kr
98+
99+
tempfile="/.initrd/dialog-ask-pass-$$.$BASHPID"
100+
{
101+
printf '#!/bin/bash\n'
102+
printf 'exec '
103+
printf ' %q' "$@"
104+
} > "$tempfile"
105+
chmod +x "$tempfile"
106+
107+
rc=0
108+
"$ask" "$prompt" "$tries" "$tempfile" || rc="$?"
109+
110+
rm -f -- "$tempfile"
111+
112+
return $rc
113+
}
114+
115+
fi # __dialog_sh_functions

features/dialog/rules.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
PUT_FEATURE_DIRS += $(DIALOG_DATADIR)

features/luks/config.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: GPL-3.0-or-later
2-
$(call feature-requires,depmod-image devmapper modules-crypto-user-api system-glibc)
2+
$(call feature-requires,depmod-image devmapper modules-crypto-user-api system-glibc dialog)
33

44
CRYPTSETUP_BIN ?= cryptsetup
55
LUKS_DATADIR ?= $(FEATURESDIR)/luks/data

features/luks/data/lib/uevent/handlers/085-luks

Lines changed: 8 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
. uevent-sh-functions
1010
. initrd-sh-functions
11+
. dialog-sh-functions
1112
. crypttab-sh-functions
1213

1314
PROG="${QUEUE:--}: session=${SESSION:-0}: $PROG"
@@ -48,28 +49,6 @@ match_dev_in_array() {
4849
return 1
4950
}
5051

51-
luks_unl0kr_ask_pass() {
52-
[ "$#" = 2 ] || return 1
53-
54-
local cmd tries rc
55-
56-
rc=2
57-
cmd="$1"; shift
58-
tries="$1"; shift
59-
60-
plymouth hide-splash 2>/dev/null
61-
62-
while [ "$rc" != 0 ] && [ "$tries" != 0 ]; do
63-
unl0kr 2>/dev/null | $cmd
64-
rc=$?
65-
tries=$(($tries - 1))
66-
done
67-
68-
plymouth show-splash 2>/dev/null
69-
70-
return $rc
71-
}
72-
7352
read_pkcs11_key() {
7453
local keyid="$1" tries=${luks_tries:-3}
7554

@@ -104,25 +83,10 @@ read_pkcs11_key() {
10483
type="data"
10584
output_file="$path"
10685
flags="-l -r"
107-
if plymouth --ping 2>/dev/null; then
108-
if type -P unl0kr >/dev/null; then
109-
luks_unl0kr_ask_pass pkcs11-tool-wrapper $tries
110-
rc=$?
111-
else
112-
add_new_line="true"\
113-
plymouth ask-for-password \
114-
--prompt "Please enter passphrase for smart card:" \
115-
--number-of-tries=$tries \
116-
--command="pkcs11-tool-wrapper"
117-
rc=$?
118-
fi
119-
else
120-
rc=1
121-
while [ "$rc" != "0" ]; do
122-
pkcs11-tool-wrapper
123-
rc="$?"
124-
done
125-
fi
86+
87+
dialog_ask_pass "Please enter passphrase for smart card:" "$tries" \
88+
"pkcs11-tool-wrapper"
89+
rc="$?"
12690

12791
return "$rc"
12892
}
@@ -194,40 +158,6 @@ findkey() {
194158
return 2
195159
}
196160

197-
cryptsetup_ask_pass() {
198-
local rc=0 tries=${luks_tries:-3}
199-
200-
{
201-
printf 'exec cryptsetup'
202-
printf ' %q' "$@"
203-
} > /.initrd/cryptsetup-ask-pass
204-
205-
if plymouth --ping 2>/dev/null; then
206-
if type -P unl0kr >/dev/null; then
207-
luks_unl0kr_ask_pass "bash /.initrd/cryptsetup-ask-pass" $tries
208-
rc=$?
209-
else
210-
plymouth ask-for-password \
211-
--prompt "Please enter passphrase for $LUKS_ROOT:" \
212-
--number-of-tries=$tries \
213-
--command="bash /.initrd/cryptsetup-ask-pass"
214-
rc="$?"
215-
fi
216-
else
217-
rc=2
218-
# WARNING: Wait decrypt forever!
219-
while [ "$rc" = 2 ] && [ "$tries" -ne 0 ]; do
220-
bash /.initrd/cryptsetup-ask-pass
221-
rc="$?"
222-
tries=$(( $tries - 1 ))
223-
done
224-
fi
225-
226-
rm -f -- /.initrd/cryptsetup-ask-pass
227-
228-
return $rc
229-
}
230-
231161
in_list() {
232162
local a n="$1"
233163
for a; do
@@ -427,7 +357,9 @@ handler() {
427357
fi
428358
if [ "$rc" -ne 0 ]; then
429359
message "Trying to ask the user for a password."
430-
cryptsetup_ask_pass "$@"
360+
dialog_ask_pass \
361+
"Please enter passphrase for $LUKS_ROOT:" "$luks_tries" \
362+
"cryptsetup" "$@"
431363
rc="$?"
432364
fi
433365
fi

0 commit comments

Comments
 (0)