-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremember.sh
executable file
·57 lines (43 loc) · 975 Bytes
/
remember.sh
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
#!/bin/bash
# Ask password and remember it for ELAPSED seconds.
#
# Use keyctl. Ask password to the user and store it in linux
# keyring.
# authors pbt, ante
# avril 2024
# licence WTFL
set -eo pipefail
TIMEOUT="7200" #"1200" # 1200 seconds aka 20 minutes
set -u
usage() {
local scriptname=$(basename ${0})
echo "
Remember value like password for a while (default 20m).
Usage: ${scriptname} <key>
"
exit 1
}
# do stuff now
read_passwd() {
echo -n "value for ${keyname} >>> "
# save tty conf and disable echo
stty_orig=$(stty -g)
stty -echo
# read and store in keyring
key=$(head -n 1 | keyctl padd user "user:$keyname" @s)
keyctl timeout "$key" "$TIMEOUT"
# restore tty config
stty "$stty_orig"
}
get_passwd() {
if ! key=$(keyctl request user "user:$keyname" 2>/dev/null);
then
read_passwd $keyname
fi
keyctl pipe "$key"
}
if [ "$#" -ne 1 ]; then
usage
fi
keyname=$1
get_passwd $keyname