-
Notifications
You must be signed in to change notification settings - Fork 4
/
easy-ecryptfs
executable file
·404 lines (327 loc) · 9.98 KB
/
easy-ecryptfs
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
#!/bin/bash
# Version 1.1
set -euo pipefail
BASENAME="$(basename "$0")"
ECRYPTFS_CONFIG_DIR="$HOME/.ecryptfs"
KEYBYTES=16 # must match what mount.ecryptfs_private allows
DEBUG=1
log_debug() {
if [ -n "$DEBUG" ]; then
echo >&2 "$BASENAME: DEBUG: $*"
fi
}
usage() {
cat >&2 <<EOM
usage: $BASENAME COMMAND ARGS...
Supported commands:
$BASENAME mount <DIRECTORY|LABEL>
$BASENAME umount <DIRECTORY|LABEL>
$BASENAME create LABEL LOWER MOUNTPOINT
$BASENAME import LOWER
$BASENAME status
Directory names containing spaces are unsupported.
EOM
}
# usage: getpass [PROMPT]
#
# Prompt the user for a password
#
getpass() {
local prompt ans
case "$#" in
0)
prompt="Password: "
;;
1)
prompt="$1"
;;
*)
echo >&2 "error: getpass: unexpected arguments: $*"
return 1
;;
esac
read -r -s -p "$prompt" ans
echo -n "$ans"
echo >&2
}
random_hex_key() {
local bytes
bytes="$1"
log_debug "Generating ${bytes}-byte key"
head -c "$bytes" /dev/random | xxd -p | tr -d '\n'
}
# usage: read_abpw_path_from_config FILENAME
#
# Given an easy-ecryptfs config file, parse out the abpw_path
# The file should be simple "key=value" lines, with no whitespace. Any special
# characters in value are treated as is.
read_abpw_path_from_config() {
if [ ! -f "$1" ]; then
echo >&2 "error: Expected to find easy-ecryptfs.conf at '$1'"
echo >&2 "PWD: $PWD"
return 1
fi
log_debug "Reading config from '$(readlink -m "$1")'"
grep '^abpw_path=' "$1" | cut -d '=' -f '2-'
}
# usage: read_label_from_config FILENAME
#
# Given an easy-ecryptfs config file, parse out the label
# The file should be simple "key=value" lines, with no whitespace. Any special
# characters in value are treated as is.
read_label_from_config() {
if [ ! -f "$1" ]; then
echo >&2 "error: Expected to find easy-ecryptfs.conf at '$1'"
echo >&2 "PWD: $PWD"
return 1
fi
log_debug "Reading config from '$(readlink -m "$1")'"
grep '^label=' "$1" | cut -d '=' -f '2-'
}
# Assert that the given label contains alphanumerics plus - and _
check_valid_label() {
if [ -z "$1" ]; then
echo >&2 "error: label must not be empty"
fi
if [ "$(tr -d 'a-zA-Z0-9_-' <<< "$1")" != '' ]; then
echo >&2 "error: label $1 contains invalid characters"
return 1
fi
}
assert_not_exists() {
if [ -e "$1" ]; then
echo >&2 "error: file or directory already exists: $1"
return 1
fi
}
run() {
echo >&2 "+ $*"
"$@"
}
# usage: load_passphrase DIRECTORY PASSWORD
#
# Using the wrapped ecryptfs passphrase found at DIRECTORY/wrapped-passphrase,
# prompt for the wrapping password if not given as PASSWORD, unwrap the wrapped
# passphrase and insert it into the kernel keyring with
# ecryptfs-insert-wrapped-passphrase-into-keyring.
#
load_passphrase() {
local dir filename passwd
dir="$1"
if [ ! -d "$dir" ]; then
echo >&2 "error: no such directory: $dir"
return 1
fi
passwd="$2"
filename="$(readlink -f "$dir/wrapped-passphrase")"
if ! [ -e "$filename" ]; then
echo >&2 "error: no such file: $filename"
return 1
fi
if ! keyctl show | grep -q "_uid.$UID"; then
# link user keyring to session keyring as needed for gnome
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=870126
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=870335
run keyctl link @u @s
fi
if [ -z "$passwd" ]; then
run ecryptfs-insert-wrapped-passphrase-into-keyring "$filename"
else
run ecryptfs-insert-wrapped-passphrase-into-keyring "$filename" \
<<< "$passwd"
fi
}
# usage: resolve_label LABEL
# usage: resolve_label DIRECTORY
#
# Given a user-provided argument that may be a label or a directory name,
# determine what the label should be.
resolve_label() {
local name config_file label lower_dir mount_dir
name="$1"
# Check if name is a valid label
config_file="$ECRYPTFS_CONFIG_DIR/$name.conf"
if [ -e "$config_file" ]; then
log_debug "Found config file at $config_file"
echo "$name"
return
fi
# Check if name is a directory
if ! [ -d "$name" ]; then
echo >&2 "error: $name is neither label nor directory"
echo >&2 "No label config found at $config_file"
echo >&2 "No directory found at $name"
return 1
fi
if [ -e "$name/easy-ecryptfs.conf" ]; then
# if easy-ecryptfs.conf exists, treat as lower
log_debug "Found lower directory config at $name/easy-ecryptfs.conf"
label="$(read_label_from_config "$name/easy-ecryptfs.conf")"
elif [ -e "$name/easy-ecryptfs.source" ]; then
# if easy-ecryptfs.source exists, treat as mountpoint
log_debug "Found mount point config at $name/easy-ecryptfs.source"
mount_dir="$name"
lower_dir="$(cat "$name/easy-ecryptfs.source")"
# interpret lower_dir path as relative to mount_dir
label="$(CDPATH='' cd "$mount_dir" && \
read_label_from_config "$lower_dir/easy-ecryptfs.conf")"
else
echo >&2 "error: $name is a directory, but no config found"
echo >&2 "Could not find easy-ecryptfs.conf or easy-ecryptfs.source"
return 2
fi
# make sure expected config file exists
config_file="$ECRYPTFS_CONFIG_DIR/$label.conf"
if [ -e "$config_file" ]; then
echo "$label"
return
fi
cat >&2 <<EOM
The easy-ecryptfs config files say the label should be '$label',
but no config file was found at '$config_file'.
If you would like to create this config file based on the easy-ecryptfs config,
then try running:
$BASENAME import $name
EOM
return 3
}
cmd_mount() {
local name label lower_dir mount_dir config_file easy_config_file
local abpw_path password
name="$1"
label="$(resolve_label "$name")"
config_file="$ECRYPTFS_CONFIG_DIR/$label.conf"
lower_dir="$(head -1 "$config_file" | cut -f1 -d' ')/.."
easy_config_file="$lower_dir/easy-ecryptfs.conf"
abpw_path=
if [ -e "$easy_config_file" ]; then
abpw_path="$(read_abpw_path_from_config "$easy_config_file" || true)"
fi
password=
if [ -n "$abpw_path" ]; then
password="$(run abpw get "$abpw_path" pass)"
fi
load_passphrase "$lower_dir" "$password"
run mount.ecryptfs_private "$label"
}
cmd_umount() {
local name label
name="$1"
label="$(resolve_label "$name")"
run umount.ecryptfs_private "$label"
echo OK
}
# usage: cmd_create LABEL LOWER_DIR MOUNT_DIR
cmd_create() {
local label lower_dir mount_dir
# TODO update locals
label="$1"
lower_dir="$2"
mount_dir="$3"
check_valid_label "$label"
assert_not_exists "$lower_dir"
assert_not_exists "$mount_dir"
mkdir -v -m 700 "$lower_dir"
mkdir -v -m 700 "$lower_dir/root"
mkdir -v -m 700 "$mount_dir"
# Use absolute paths for lower and mount directories
lower_dir="$(readlink -e "$lower_dir")"
mount_dir="$(readlink -e "$mount_dir")"
cat > "$lower_dir/easy-ecryptfs.conf" <<EOM
label=$label
created=$(date --rfc-3339=seconds)
EOM
chmod -w "$lower_dir/easy-ecryptfs.conf"
cat > "$mount_dir/README.txt" <<EOM
This directory is the unmounted mount point for an ecryptfs filesystem.
When this filesystem was set up by easy-ecryptfs on $(date +%F), it had
these settings:
lower: $lower_dir
mount: $mount_dir
label: $label
Mount the filesystem with this command:
easy-ecryptfs mount $label
EOM
echo "$lower_dir" > "$mount_dir/easy-ecryptfs.source"
chmod -R u-w,go-rwx "$mount_dir/"
# create fstab style config file used by mount.ecryptfs_private
config_file="$lower_dir/$label.conf"
echo "$lower_dir/root $mount_dir ecryptfs" > "$config_file"
log_debug "Created config files and README"
mount_key="$(random_hex_key "$KEYBYTES")"
wrapping_pass="$(getpass 'Passphrase: ')"
run ecryptfs-wrap-passphrase "$lower_dir/wrapped-passphrase" - <<EOM
$mount_key
$wrapping_pass
EOM
chmod 400 "$lower_dir/wrapped-passphrase"
log_debug "Saved wrapped passphrase to $lower_dir/wrapped-passphrase"
log_debug "Inserting key into kernel keyring"
response="$(run ecryptfs-add-passphrase --fnek - <<< "$mount_key")"
sigs="$(echo "$response" | grep "Inserted auth tok" | sed "s/^.*\[//" | sed "s/\].*$//")"
# There should be two signatures, one for content and one for fnek
if [ "$(wc -l <<< "$sigs")" -ne 2 ]; then
echo >&2 "error: failed to parse ecryptfs-add-passphrase output"
echo >&2 "output: '''$response'''"
return 3
fi
# Validate each signature is hex of the expected length
echo "$sigs" | while read -r line; do
if grep -vEq "^[0-9a-fA-F]{$KEYBYTES,$KEYBYTES}$" <<< "$line"; then
echo >&2 "Invalid hex key signature: '$line'"
return 3
fi
done
# Write to sig file
sig_file="$lower_dir/$label.sig"
echo "$sigs" > "$sig_file"
chmod 444 "$sig_file"
log_debug "Saved key signatures to $sig_file"
# create symlinks in ~/.ecryptfs pointing to our lower configs
log_debug "Symlinking configuration into $ECRYPTFS_CONFIG_DIR"
ln -svt "$ECRYPTFS_CONFIG_DIR/" "$config_file" "$sig_file"
cat <<EOM
Your new ecryptfs partition is ready to go!"
Mount command:
$BASENAME mount $label
Unmount command:
$BASENAME umount $label
EOM
}
cmd_import() {
# TODO
echo 'Not yet implemented'
return 3
}
cmd_status() {
run keyctl show
run grep ecryptfs /proc/mounts
}
if [ $# -lt 1 ]; then
usage
exit 1
fi
cmd="$1"
shift
case "$cmd" in
mount)
cmd_mount "$@"
;;
umount|unmount)
cmd_umount "$@"
;;
create)
cmd_create "$@"
;;
status)
cmd_status "$@"
;;
import)
cmd_import "$@"
;;
*)
usage
echo >&2 "error: unknown command '$cmd'"
exit 1
;;
esac