-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcryptdir
executable file
·268 lines (233 loc) · 6.3 KB
/
cryptdir
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
#!/bin/bash
# SPDX-FileCopyrightText: 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -eu
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command grep sed && exit 3
#
# Functions
#
function set_icon {
if check_command gio && [ -n "$(whereis -b gvfs | cut -d ":" -f 2)" ]; then
gio set "${DECRYPTPATH}" -t string metadata::custom-icon-name folder-unlocked
fi
if check_command kwriteconfig5; then
kwriteconfig5 --file "${DECRYPTPATH}/.directory" --group 'Desktop Entry' --key 'Icon' 'folder-unlocked'
fi
}
function unset_icon {
if check_command gio && [ -n "$(whereis -b gvfs | cut -d ":" -f 2)" ]; then
gio set --delete "${DECRYPTPATH}" metadata::custom-icon-name
fi
if check_command kwriteconfig5; then
kwriteconfig5 --file "${DECRYPTPATH}/.directory" --group 'Desktop Entry' --key 'Icon' --delete
fi
}
function mount_crypt_dir {
if grep -q "${DECRYPTPATH}" /etc/mtab; then
show_warning "${DECRYPTPATH} is already mounted."
exit
fi
case "${CRYPTTOOL,,}" in
cryfs)
cryfs "${ENCRYPTPATH}" "${DECRYPTPATH}"
;;
ecryptfs)
local encryptname
local pass
local hash
if ! lsmod | grep -q ecryptfs; then
show_info "Loading ecryptfs kernel module."
sudo modprobe ecryptfs
fi
if [ -d "${HOME}/.ecryptfs" ]; then
mkdir -p "${HOME}/.ecryptfs"
fi
encryptname="${ENCRYPTPATH##*/}"
encryptname="${encryptname#\.}"
if ! [ -f "${HOME}/.ecryptfs/${encryptname}.conf" ]; then
echo "${ENCRYPTPATH} ${DECRYPTPATH} ecryptfs" > \
"${HOME}/.ecryptfs/${encryptname}.conf"
fi
if ! [ -f "${HOME}/.ecryptfs/${encryptname}.mnt" ]; then
echo "${DECRYPTPATH}" > \
"${HOME}/.ecryptfs/${encryptname}.mnt"
fi
pass="$(ask_secret "Password:")"
if ! [ -f "${HOME}/.ecryptfs/${encryptname}.sig" ]; then
hash="$(printf "%s" "${pass}" |
ecryptfs-add-passphrase - |
sed -n "s/.*\[\([0-9a-z]\+\)\].*/\1/p")"
echo "${hash}" > "${HOME}/.ecryptfs/${encryptname}.sig"
echo "${hash}" >> "${HOME}/.ecryptfs/${encryptname}.sig"
else
printf "%s" "${pass}" | ecryptfs-add-passphrase -
fi
mount.ecryptfs_private "${encryptname}"
;;
encfs)
encfs "${ENCRYPTPATH}" "${DECRYPTPATH}"
;;
gocryptfs)
if ! [ -f "${ENCRYPTPATH}"/gocryptfs.conf ]; then
gocryptfs -init "${ENCRYPTPATH}"
fi
gocryptfs "${ENCRYPTPATH}" "${DECRYPTPATH}"
;;
esac
if grep -q "${DECRYPTPATH}" /etc/mtab; then
set_icon
show_success "Decrypted successfully."
else
show_error "Decryption failed."
exit 3
fi
}
function unmount_crypt_dir {
if ! grep -q "${DECRYPTPATH}" /etc/mtab; then
show_warning "${DECRYPTPATH} is not decrypted."
exit
fi
if ! grep -q "^${CRYPTTOOL} ${DECRYPTPATH}" /etc/mtab; then
show_warning "Selected ${CRYPTTOOL@Q} does not match /etc/mtab entry."
exit
fi
case "${CRYPTTOOL,,}" in
cryfs)
cryfs-unmount "${DECRYPTPATH}"
;;
ecryptfs)
local encryptname
encryptname="${ENCRYPTPATH##*/}"
encryptname="${encryptname#\.}"
umount.ecryptfs_private "${encryptname}"
;;
encfs)
fusermount -u "${DECRYPTPATH}"
;;
gocryptfs)
fusermount -u "${DECRYPTPATH}"
;;
esac
if ! grep -q "${DECRYPTPATH}" /etc/mtab; then
unset_icon
show_success "Encrypted successfully."
else
show_error "Encryption failed."
exit 3
fi
}
function check_crypttool {
case "${CRYPTTOOL,,}" in
cryfs)
if ! check_command cryfs; then
exit 3
fi
;;
ecryptfs)
if ! check_command ecryptfs-stat; then
exit 3
fi
;;
encfs)
show_warning "EncFS has security issues. (https://defuse.ca/audits/encfs.htm)"
if ! check_command encfs fusermount; then
exit 3
fi
;;
gocryptfs)
if ! check_command fusermount gocryptfs; then
exit 3
fi
;;
*)
show_error "ERROR: ${CRYPTTOOL@Q} not supported. Exiting."
exit 3
;;
esac
}
function print_usage {
show_header "Usage: cryptdir on|off"
cat << EOF
-e|--encrypt_dir path to directory with encrypted data (${ENCRYPTPATH})
-d|--decrypt_dir directory path to mount decrypted data (${DECRYPTPATH})
-s|--crypt_std encryption standard (${CRYPTTOOL})
crypt_std options: 'cryfs', 'ecryptfs', 'encfs', and 'gocryptfs'
Use 'on' to decrypt an encrypted store and 'off' to encrypt an decrypted
mounted directory.
EOF
}
#
# Globals
#
ENCRYPTPATH="${encryptpath:-${HOME}/.encrypted}"
DECRYPTPATH="${decryptpath:-${HOME}/.decrypted}"
CRYPTTOOL=cryfs
#
# Main
#
OPTIONS=d:e:s:h
LONGOPTIONS=decrypt_dir:,encrypt_dir:,crypt_std:,help
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ "${#}" -ge 1 ]; do
case "${1}" in
-d | --decrypt_dir)
DECRYPTPATH="${2}"
shift 2
;;
-e | --encrypt_dir)
ENCRYPTPATH="${2}"
shift 2
;;
-s | --crypt_std)
CRYPTTOOL="${2}"
shift 2
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR: Unknown argument ${1@Q}. Exiting."
exit 3
;;
esac
done
check_crypttool
if ! [ "${#}" -eq 1 ]; then
show_error "ERROR: Incorrect # of parameters. Exiting."
exit 3
fi
mkdir -p "${ENCRYPTPATH}" "${DECRYPTPATH}"
case "${1}" in
on | mount | decrypt)
mount_crypt_dir
;;
off | unmount | encrypt)
unmount_crypt_dir
;;
*)
show_error "ERROR: Unrecognized option ${1@Q}. Exiting."
exit 3
;;
esac