-
Notifications
You must be signed in to change notification settings - Fork 0
/
term-lock.zsh
164 lines (130 loc) · 4.67 KB
/
term-lock.zsh
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
#!/bin/bash
# To disable the pin simply set USE_PIN to "false"
USE_PIN="true"
function term_lock() {
if [[ -n $USE_PIN && "$USE_PIN" == "true" ]]; then
function fatal() {
CYAN='\033[0;36m'
RED='\033[0;31m'
NC='\033[0m'
echo -e "$0: ${RED}Error:${CYAN} $*" >&2
exit 0
}
function out() {
CYAN='\033[0;36m'
PURPLE='\033[0;35m'
NC='\033[0m'
echo -e "${CYAN}[Term-Lock] ${PURPLE}$*${NC}"
}
function encode() {
if [[ $# -eq 0 ]]; then
cat | base64
else
printf '%s' "$1" | base64
fi
}
function decode() {
if [[ $# -eq 0 ]]; then
cat | base64 --decode
else
printf '%s' "$1" | base64 --decode
fi
}
reset_pin="$1"
CONFIG_DIRECTORY="${HOME}/.config/term-lock/"
PIN_FILE="${CONFIG_DIRECTORY}/.pin.conf"
DONE="false"
new_user="true"
if [[ ! -d "$CONFIG_DIRECTORY" ]]; then
mkdir -p "$CONFIG_DIRECTORY"
fi
if [[ "$reset_pin" == "true" ]]; then
# Instead of writing a whole knew section or function to reset the pin
# I figured I could simply delete the pin file based on an argument.
# That way the check for it fails and you get treated as a new user essentially
# resetting your pin.
sudo rm $PIN_FILE || {
out "Failed to remove old pin. Make sure you have permissions!"
return 1
}
fi
while [[ "$DONE" == "false" ]]; do
good_pin="false"
pin_pattern='^[0-9]{4}$' # 4 digits.
exit_char="q"
# ------------------- CURRENT USERS ------------------
if [[ -f "$PIN_FILE" ]]; then
new_user="false"
correct_pin="$(sed "1q;d" $PIN_FILE | cut -d':' -f2)"
decoded_pin="$(decode $correct_pin)"
while [[ "$good_pin" == "false" ]]; do
clear
out "Enter your pin to continue."
read "pin?pin: "
if [[ -z "$pin" ]]; then
out "Pin is empty. Enter a valid pin to continue."
clear
continue
fi
if [[ "$pin" == "$exit_char" ]]; then
out "Press enter to exit..."
read "a?"
clear
exit 0
fi
if [[ $pin =~ $pin_pattern ]]; then
if [[ "$pin" == "$decoded_pin" ]]; then
good_pin="true"
DONE="true"
clear
break
else
continue
fi
else
clear
out "Invalid pin. Must be no less than 4 digits and no letter or characters."
fi
done
fi
# --------------- NEW USERS ------------------
while [[ "$good_pin" == "false" ]]; do
out "Set a pin for future use
Pin or (q) to quit"
read "pin?pin: "
if [[ -z "$pin" ]]; then
clear
out "Pin is empty. Enter a valid pin to continue."
read "a?Press enter to continue..."
clear
continue
fi
if [[ "$pin" == "$exit_char" ]]; then
out "Press enter to exit..."
read "a?"
clear
exit 0
fi
if [[ $pin =~ $pin_pattern ]]; then
local encode_pin
encoded_pin="$(encode $pin)"
echo "pin:$encoded_pin" > "$PIN_FILE"
good_pin="true"
DONE="true"
clear
else
clear
out "Invalid pin. Must be no less than 4 digits and no letter or characters."
read "a?Press enter to continue..."
clear
fi
done
if [[ $DONE == "true" ]]; then
break
fi
done
elif [[ -n $USE_PIN && "$USE_PIN" == "false" ]]; then
out "Pin system will be disabled."
fi
}
term_lock