-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocker.sh
More file actions
executable file
·226 lines (173 loc) · 4.68 KB
/
Copy pathlocker.sh
File metadata and controls
executable file
·226 lines (173 loc) · 4.68 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
#!/usr/bin/sh
# Utility functions
show_help() {
# Show some nice logo
printf "\n"
printf " __ _ \n"
printf " / / ___ ___| | _____ _ __ \n"
printf " / / / _ \ / __| |/ / _ \ '__|\n"
printf "/ /__| (_) | (__| < __/ | \n"
printf "\____/\___/ \___|_|\_\___|_| \n\n"
printf "\n\t--Developed by deepjyoti30, 2020\n"
# Show help
printf "\nUsage: locker OPERATION DIR [--help]\n"
printf "\n"
# Positional arguments
printf "Positional arguments:\n\n"
printf " OPERATION: Either of [lock] or [unlock] can be passed.\n"
printf " DIR: Directory to operate on.\n"
# Optional arguments
printf "\nOptional arguments:\n\n"
printf " --help: Show this message and exit\n"
exit
}
get_lower() {
# Convert the passed string from upper to lower
# Check if arg passed
if [ "$#" -ne 1 ]; then
printf "[-] Error in get_lower! Exiting...!\n"
return 1
fi
echo "$(echo "$1" | tr '[:upper:]' '[:lower:]')"
}
get_upper() {
# Return the uppercase of the passed string
echo "$(echo "$1" | tr '[:lower:]' '[:upper:]')"
}
get_hash() {
# Return the hash of the passed string
echo "$(echo "$1" | md5sum)"
}
get_store_loc() {
# Return the location of the passed dir
dir_hash=$(get_hash $1)
# Replace all the spaces with #
echo "$(echo "$dir_hash" | tr ' ' '#')"
}
save_pw_hash() {
# Generate a hash for the password created by the user
# and save it somewhere safe
hash=$(get_hash "$1")
dir_hash=$(get_store_loc $DIR)
store_loc="$par_dir/.locker/hash-$dir_hash"
# Create the dir in case it doesn't exits
mkdir -p "$par_dir/.locker"; touch "$store_loc"
printf "[*] Storing the passwd hash to %s\n" "$store_loc"
echo "$hash" > $store_loc
}
is_pw_invalid() {
# Check if the entered password is valid
hash=$(get_hash $1)
# Now read the saved hash
dir_hash=$(get_store_loc $DIR)
store_loc="$par_dir/.locker/hash-$dir_hash"
if [ ! -e $store_loc ];then
printf "[-] The directory is probably not locked!\n"
exit
fi
# If the file is present, read from it and
# compare the string
saved_hash=$(cat $store_loc)
if [ "$saved_hash" != "$hash" ];then
return 1
fi
return 0
}
remove_file() {
# Remove the pw file once the dir is unlocked
dir_hash=$(get_store_loc $DIR)
store_loc="$par_dir/.locker/hash-$dir_hash"
rm $store_loc
}
lock_dir() {
# Now lock the dir
chmod u-rwx,go-rwx $1
return $!
}
unlock_dir() {
# Unlock the dir now
chmod u+rwx,go+rx $1
return $!
}
lock() {
# Do all the operations related to locking the DIR
save_pw_hash $1
lock_dir $DIR
status=$?
if [ "$status" = "0" ];then
printf "[*] Directory locked succesfully!\n"
return 0
else
printf "[-] Directory couldn't be locked. SORRY!\n"
return 1
fi
}
unlock() {
# Unlock the DIR
passed_pw=$1
is_pw_invalid $passed_pw
status=$?
if [ "$status" = "1" ];then
printf "[-] The password is wrong!.\n"
return 1
fi
unlock_dir $DIR
status=$?
if [ "$status" = "0" ];then
printf "[*] The directory was unlocked succesfully!\n";
else
printf "[-] Something went wrong. SORRY!\n";
return 1
fi
remove_file
return 0
}
main() {
# Show the passed values
printf "[*] Passed OPERATION is: %s\n" "$(get_upper $OP)"
printf "[*] Passed DIRECTORY is: %s\n" "$DIR"
# Check if the Operation passed is a valid one
# If it's not exit and show reason
case "$OP" in
# Declare the possible operations available
'lock' | 'unlock' )
printf "[*] Passed OPERATION is valid.\n"
;;
* )
printf "[-] Passed OPERATION is not valid! Exitting...\n"
exit 1
;;
esac
# Check if the passed directory is valid or not
if [ -d "$DIR" ]; then
printf "[*] Passed directory is valid....\n"
else
printf "[-] Passed directory is not valid...\n"
fi
# Get a passwd from the user
printf "[*] Enter the password: "
stty -echo
IFS= read -r temp_pw
stty echo
printf "\n"
# Check if to lock or unlock
if [ $OP = "lock" ];then
lock $temp_pw
status=$?
else
# Since we have already checked the operation
# for validity, there are only two possibilities
# THUS, no need to check for unlock
unlock $temp_pw
status=$?
fi
return $status
}
if [ "$#" -ne 2 ] || [ "$1" = "--help" ]; then
show_help
fi
# Extract the passed operation and dir
par_dir=$(echo ~)
OP=$(get_lower $1)
DIR=$2
main