-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdotfoldtui.sh
More file actions
executable file
·429 lines (380 loc) · 14.1 KB
/
dotfoldtui.sh
File metadata and controls
executable file
·429 lines (380 loc) · 14.1 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/bin/bash
PASS_FILE="/var/lib/dotfold/"$(logname)"/.passwd"
LOCK_FILE="/var/lib/dotfold/"$(logname)"/.lock"
FOLDER_FILE="/var/lib/dotfold/"$(logname)"/.folders"
MAX_ATTEMPTS=3 # max attempt for normal lock
MAX_ATTEMPTS_1=10 # max attempts for permanent lock
INITIAL_LOCKOUT=30 # time in sec
check_deps() {
for cmd in gum openssl sha256sum xdg-open; do
command -v "$cmd" >/dev/null 2>&1 || { echo "Error: $cmd is required."; exit 1; }
done
}
check_deps
hash_passwd() {
local salt=$(openssl rand -hex 16)
local hashed=$(echo -n "$1$salt" | sha256sum | awk '{print $1}')
echo "$hashed:$salt"
}
verify_passwd() {
local input_pass="$1"
local stored_pass=$(sudo cat "$PASS_FILE")
local stored_hash=$(echo "$stored_pass" | cut -d':' -f1)
local salt=$(echo "$stored_pass" | cut -d':' -f2)
local hashed_input=$(echo -n "$input_pass$salt" | sha256sum | awk '{print $1}')
[ "$hashed_input" = "$stored_hash" ]
}
encrypt_folder() {
local folder="$1"
echo -n "$folder" | openssl enc -aes-256-cbc -salt -pass file:<(echo -n "$user_pass") -base64 -A 2>/dev/null
}
decrypt_folder() {
local folder="$1"
echo -n "$folder" | openssl enc -d -aes-256-cbc -salt -pass file:<(echo -n "$user_pass") -base64 -A 2>/dev/null
}
input_passwd() {
gum input --password --placeholder " 🔑 WHaats..the....PAsswd.... "
}
# reencrypts folders while changing password
reencrypt_folders() {
local old_pass="$1"
local new_pass="$2"
local temp_file=$(mktemp)
trap 'rm -f "$temp_file"' EXIT
while IFS= read -r encrypted_line; do
decrypted_path=$(echo -n "$encrypted_line" | openssl enc -d -aes-256-cbc -salt -pass file:<(echo -n "$old_pass") -base64 -A 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$decrypted_path" ]; then
gum style --foreground 9 "❌ Decryption failed for an entry. Aborting password change."
trap 'rm -f "$temp_file"' EXIT
return 1
fi
new_encrypted_path=$(echo -n "$decrypted_path" | openssl enc -aes-256-cbc -salt -pass file:<(echo -n "$new_pass") -base64 -A 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$new_encrypted_path" ]; then
gum style --foreground 9 "❌ Encryption failed for an entry. Aborting password change."
trap 'rm -f "$temp_file"' EXIT
return 1
fi
echo "$new_encrypted_path" >> "$temp_file"
done < <(sudo cat "$FOLDER_FILE")
sudo mv "$temp_file" "$FOLDER_FILE"
}
init_folder_file() {
if ! sudo test -f "$FOLDER_FILE"; then
sudo touch "$FOLDER_FILE"
fi
}
# current time
current_time=$(date +%s)
init_lockfile() {
{
echo "attempt_count=0"
echo "unlock_time=$current_time"
echo "lockout=0"
echo "owner=$(logname)"
} | sudo tee "$LOCK_FILE" >/dev/null
}
# check for lock file
sudo test -f "$LOCK_FILE" || init_lockfile
authenticate() {
# added fix for coruption of files if both cli and tui run at same time.
exec 200>"$LOCK_FILE"
flock -n 200 || { gum style --foreground 9 "Another instance is running."; exit 1; }
# First run
if ! sudo test -f "$PASS_FILE"; then
sudo mkdir -p "$(dirname "$PASS_FILE")"
gum style --foreground 12 "🔐 First-time setup: Create a secure password"
new_pass=$(gum input --password --placeholder "🔑 Set your secret key: ")
# added support for cancellation
if [ $? -ne 0 ] || [ "$new_pass" = "" ]; then
gum style --foreground 11 "↩️ Cancelled...."
exit 1
fi
confirm_pass=$(gum input --password --placeholder " Confirm your secret key: ")
if [ $? -ne 0 ] || [ "$new_pass" = "" ]; then
gum style --foreground 11 "↩️ Cancelled...."
exit 1
fi
if [ -z "$new_pass" ]; then
gum style --foreground 9 "❌ Password cannot be empty"
exit 1
fi
if [ "$new_pass" != "$confirm_pass" ]; then
gum style --foreground 9 "❌ Passwords do not match."
exit 1
fi
hash_passwd "$new_pass" | sudo tee "$PASS_FILE" >/dev/null
init_folder_file
gum style --foreground 10 "✅ Password set successfully!"
fi
while true; do
user_pass=$(input_passwd)
if [ $? -ne 0 ] || [ "$user_pass" = "" ]; then
gum style --foreground 11 "↩️ Cancelled...."
exit 1
fi
if verify_passwd "$user_pass"; then
init_lockfile
break
else
eval "$(sudo cat "$LOCK_FILE")"
attempt_count=$(( "${attempt_count:-0}" + 1 ))
{
echo "attempt_count=$attempt_count"
echo "unlock_time=$current_time"
echo "lockout=0"
echo "owner=$(logname)"
} | sudo tee "$LOCK_FILE" >/dev/null
gum style --foreground 9 "❌ Access Denied (Attempt $attempt_count/$MAX_ATTEMPTS)"
if [ "$attempt_count" -ge "$MAX_ATTEMPTS" ]; then
lockout=$(( ${lockout:-$INITIAL_LOCKOUT} + 30 ))
unlock_time=$(( "$current_time" + "$lockout" ))
remaining_time=$(( unlock_time - current_time ))
mins=$(( remaining_time / 60 ))
secs=$(( remaining_time % 60 ))
{
echo "attempt_count=$attempt_count"
echo "unlock_time=$unlock_time"
echo "lockout=$lockout"
echo "owner=$(logname)"
} | sudo tee "$LOCK_FILE" >/dev/null
gum style --foreground 9 "🔒 Too many attempts! Locking out for ${mins}m ${secs}s..."
exit 1
fi
fi
done
}
# added to fix if someuser mistakenly type wrong password when password file is removed to protect floder data
sudo test -f "$LOCK_FILE" || authenticate
# status of lock file
eval "$(sudo cat "$LOCK_FILE" 2>/dev/null | grep -E '^(attempt_count|unlock_time|lockout)=')"
if [ "${attempt_count:-0}" -ge "$MAX_ATTEMPTS_1" ]; then
gum style --foreground 9 "🔒 Permanent lock! Manual removal required: sudo rm $LOCK_FILE"
exit 1
fi
if [ "${unlock_time:-0}" -gt "$current_time" ]; then
remaining_time=$((unlock_time - current_time))
mins=$(( remaining_time / 60 ))
secs=$(( remaining_time % 60 ))
gum style --foreground 9 "🔒 Locked! Try again in ${mins}m ${secs}s..."
exit 1
fi
authenticate
menu() {
local menu_options
if sudo test -s "$FOLDER_FILE"; then
menu_options="🌌 my-space\n📁 hide-folder\n🔓 un-hide-folder\n🔑 change-passwd\n🚪 Exit"
else
menu_options="🌌 my-space\n📁 hide-folder\n🔑 change-passwd\n🚪 Exit"
fi
echo -e "$menu_options" | fzf --prompt=" FOLDER HIDER " \
--border=rounded \
--reverse \
--color=border:bright-blue \
--height=15% \
--color='fg:white,fg+:bright-white,bg+:bright-black'
}
hide_folder() {
local root_dir="/home/$(logname)" # added path control (no navigation outside usrs's home directory)
if sudo test -f "$LOCK_FILE"; then
owner=$(sudo grep '^owner=' "$LOCK_FILE" | cut -d'=' -f2)
if [ -n "$owner" ]; then
root_dir="/home/$owner"
fi
fi
local current_dir="$root_dir"
while true; do
if [ "$current_dir" = "/" ] || [ "$current_dir" = "" ]; then
current_dir="$root_dir"
fi
selection=$(find "$current_dir" -maxdepth 1 -mindepth 1 -type d -not -name '.*' -printf '%f\n' |
awk 'BEGIN {print ".."} {print}' |
fzf --prompt="┏ FOLDER NAVIGATOR ┓" \
--header="┃ Current: $current_dir
┗━━━━━━━━━━━━━━━━━━━━━━┛" \
--border=rounded \
--reverse \
--border-label="┤ Keys: Enter=Navigate | Ctrl+h=Hide | Esc=Back ├" \
--color=border:bright-blue \
--bind 'ctrl-h:print(hide)+accept' \
--expect=ctrl-h \
--height=50% \
--color='fg:white,fg+:bright-white,bg+:bright-black' \
--prompt=" Search-» ")
keypress=$(echo "$selection" | head -1)
selected_dir=$(echo "$selection" | tail -1)
if [ "$keypress" = "ctrl-h" ]; then
if [ -n "$selected_dir" ] && [ "$selected_dir" != ".." ]; then
folder_to_hide="$current_dir/$selected_dir"
hidden_path="$current_dir/.$selected_dir"
if mv -n "$folder_to_hide" "$hidden_path" 2>/dev/null; then
sudo chown -R root:root "$hidden_path"
sudo chmod 700 "$hidden_path"
encrypted_path=$(encrypt_folder "$hidden_path")
if [ $? -eq 0 ]; then
if ! sudo grep -qF "$encrypted_path" "$FOLDER_FILE"; then
echo "$encrypted_path" | sudo tee -a "$FOLDER_FILE" >/dev/null
else
gum style --foreground 9 "⚠️ Folder already hidden!"
fi
gum style --foreground 9 "✅ Successfully hidden: $(basename "$folder_to_hide")"
return
else
gum style --foreground 9 "❌ Error: Encryption failed!"
sudo mv "$hidden_path" "$folder_to_hide"
if sudo test -f "$LOCK_FILE"; then
owner=$(sudo grep '^owner=' "$LOCK_FILE" | cut -d'=' -f2)
if [ -n "$owner" ]; then
sudo chown -R "$owner:$owner" "$folder_to_hide"
fi
fi
return 1
fi
else
gum style --foreground 9 "❌ Error: Couldn't hide folder!"
return 1
fi
else
gum style --foreground 9 "⚠️ Please select a folder first (can't hide parent directory)"
fi
fi
if [ -z "$selected_dir" ]; then
return
elif [ "$selected_dir" = ".." ]; then
if [ "$current_dir" != "$root_dir" ]; then
current_dir=$(dirname "$current_dir")
fi
else
new_dir="$current_dir/$selected_dir"
case "$new_dir" in
"$root_dir"/*|"$root_dir")
current_dir="$new_dir"
;;
*)
gum style --foreground 9 "⚠️ Access restricted to $root_dir and subdirectories"
;;
esac
fi
done
}
list_hidden_folders() {
if sudo test -s "$FOLDER_FILE"; then
sudo cat "$FOLDER_FILE" | while IFS= read -r encrypted_path; do
decrypted_path=$(decrypt_folder "$encrypted_path")
if [ $? -eq 0 ] && [ -n "$decrypted_path" ] && sudo test -d "$decrypted_path"; then
base=$(basename "$decrypted_path")
display="${base#.}"
echo "$decrypted_path|$display"
fi
done
fi
}
remove_hidden_entry() {
local folder_to_remove="$1"
sudo bash -c '
temp_file=$(mktemp)
found=0
while IFS= read -r encrypted_path; do
decrypted_path=$(echo -n "$encrypted_path" | openssl enc -d -aes-256-cbc -salt -pass file:<(echo -n "'"$user_pass"'") -base64 -A 2>/dev/null)
if [ "$decrypted_path" = "'"$folder_to_remove"'" ]; then
found=1
else
echo "$encrypted_path" >> "$temp_file"
fi
done < "'"$FOLDER_FILE"'"
if [ "$found" -eq 1 ]; then
sudo mv "$temp_file" "'"$FOLDER_FILE"'"
sudo chmod 600 "'"$FOLDER_FILE"'"
else
rm "$temp_file"
fi
'
}
unhide_folders() {
hidden_list=$(list_hidden_folders)
if [ -z "$hidden_list" ]; then
gum style --foreground 9 "❌ No hidden folder found."
else
selected=$(echo -e "$(echo "$hidden_list" | cut -d"|" -f2)\n↩️ Back" | fzf --prompt=" 👇️ Select a folder to un-hide: " --border --height=50% --reverse)
if [ "$selected" = "↩️ Back" ] || [ -z "$selected" ]; then
continue
fi
fullpath=$(echo "$hidden_list" | grep -F "|$selected" | cut -d"|" -f1)
if gum confirm "🔓 Un-hide folder \"$selected\"?"; then
dir=$(dirname "$fullpath")
base=$(basename "$fullpath")
new_name="${base#.}"
visible_folder="${dir}/${new_name}"
sudo mv "$fullpath" "$visible_folder"
remove_hidden_entry "$fullpath"
if sudo test -f "$LOCK_FILE"; then
owner=$(grep '^owner=' "$LOCK_FILE" | cut -d'=' -f2)
if [ -n "$owner" ]; then
sudo chown -R "$owner:$owner" "$visible_folder"
fi
fi
gum style --foreground 10 "✅ Folder un-hidden successfully."
fi
fi
}
while true; do
choice=$(menu)
case "$choice" in
"🌌 my-space")
hidden_list=$(list_hidden_folders)
if [ -z "$hidden_list" ]; then
if gum confirm "❓ You haven't created a private space yet! Would you like to create one now?"; then
hide_folder
fi
else
selected=$(echo -e "$(echo "$hidden_list" | cut -d"|" -f2)\n↩️ Back" | fzf --prompt=" 👇️ Select a private space: " --border --height=50% --reverse)
if [ "$selected" = "↩️ Back" ] || [ -z "$selected" ]; then
continue
fi
fullpath=$(echo "$hidden_list" | grep -F "|$selected" | cut -d"|" -f1)
if [ -d "$fullpath" ]; then
# added fix for thumbail generation on x11
pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY dbus-launch xdg-open "$fullpath" 2>/dev/null || gum style --foreground 9 "❌ Failed to open File Manager."
else
gum style --foreground 9 "❌ Folder not found."
fi
fi
;;
"📁 hide-folder")
hide_folder
;;
"🔓 un-hide-folder")
unhide_folders
;;
"🔑 change-passwd")
if gum confirm "🔑 Do you want to change your password?"; then
while true; do
new_pass=$(gum input --password --placeholder "🔑 New Password: ")
confirm_pass=$(gum input --password --placeholder " Confirm Password: ")
if [ -z "$new_pass" ]; then
gum style --foreground 9 "❌ Password cannot be empty!"
continue
fi
if [ "$new_pass" != "$confirm_pass" ]; then
gum style --foreground 9 "❌ Passwords do not match."
continue
fi
if reencrypt_folders "$user_pass" "$new_pass"; then
hash_passwd "$new_pass" | sudo tee "$PASS_FILE" >/dev/null
user_pass="$new_pass"
gum style --foreground 10 "✅ Password changed successfully!"
break
else
gum style --foreground 9 "❌ Password change failed. Please try again."
fi
done
else
gum style --foreground 11 "↩️ Cancelled...."
fi
;;
"🚪 Exit")
exit 0
;;
*)
;;
esac
done