forked from mmonaco/cryptmount
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cryptmount.sh
executable file
·688 lines (557 loc) · 14.8 KB
/
cryptmount.sh
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#!/bin/sh
SHORTOPTS="LMUc:fw:nqvho:O:"
DEPS="cryptsetup blkid mkswap mktemp"
UDEVRUNNING=0
LOGLEVEL=1
DRYRUN=0
WAITTIME=10
CRYPTTAB=/etc/crypttab
OPTIONS=
FILTER="!noauto"
FORCE=0
ct_print_usage() {
cat <<__EOF__
usage: $0 [OPTIONS] [-L]
$0 [OPTIONS] -M [NAME|DEVICE]
$0 [OPTIONS] -M NAME DEVICE [KEY]
$0 [OPTIONS] -U [NAME[,...]]
List, map, and unmap encrypted volumes. The utility is a wrapper for
cryptsetup which makes use of a crypttab file.
actions:
-L list the names of volumes defined in crypttab, this is
the default
-M map a volume defined in crypttab or defined on the command
line. with no arguments, map all volumes without the noauto
option
-U unmap volumes defined in crypttab. with no arguments, unmap
all volumes without the noauto option
options:
-c FILE set the crypttab location (default: /etc/crypttab)
-f force destructive operations even when a block device appears to
contain data
-w NUM wait time (seconds) for a device if it is not already available
-n dry run
-q decrease verbosity
-v increase verbosity
-h print this message
-o OPT[,...]
options which are appened to the options defined in crypttab
(they take precedence). specifying this multiple times is
cumulative
-O OPT[,...]
filter used *only* when no volumes are given on the command
line. an option may start with a ! to require that it must not
be present. specifying this multiple times is cumulative
__EOF__
exit "$1"
}
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
# #
# Utilities #
# #
error() {
[ $LOGLEVEL -ge 0 ] && printf "E: %s\n" "$*" >&2 || true
}
die() {
printf "E: %s\n" "$*" >&2
exit "${1:-1}"
}
warn() {
[ $LOGLEVEL -ge 1 ] && printf "W: %s\n" "$*" >&2 || true
}
msg() {
[ $LOGLEVEL -ge 1 ] && printf "M: %s\n" "$*" >&2 || true
}
info() {
[ $LOGLEVEL -ge 2 ] && printf "I: %s\n" "$*" >&2 || true
}
run() {
[ $LOGLEVEL -ge 2 ] && printf "R: %s\n" "$*" >&2
if [ $DRYRUN -eq 1 ]; then
true
else
"$@"
fi
}
trim() {
local IFS
IFS="$(printf ' \n\t')"
echo -n $*
}
get_mount() {
if type findmnt >/dev/null 2>&1 ; then
findmnt -cfmnoTARGET "$1"
else
cat /proc/self/mountinfo | \
awk -v dev="$1" \
'{ for(i=7;$i!="-";$i++); if($(i+2)==dev&&$4=="/"){print $5;r=1;exit}} END {exit !r}'
fi
}
substring(){
printf "%s\n" "$1" | awk -v start="$2" -v count="$3" \
'{print(substr($0,start+1,count));}'
}
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
# #
# Main functions #
# #
ct_main() {
local action
set_action() {
if [ -z $action ]; then
action="$@"
else
error "only one of -L, -M, or -U may be given"
ct_print_usage 1
fi
}
while getopts ":$SHORTOPTS" opt ; do
case $opt in
L) set_action list;;
M) set_action map;;
U) set_action unmap;;
c) CRYPTTAB="$OPTARG";;
f) FORCE=1;;
w) WAITTIME=$(printf '%s\n', "$OPTARG" | sed 's/[^0-9]//g');;
n) DRYRUN=1;;
q) LOGLEVEL=$(( LOGLEVEL - 1 ));;
v) LOGLEVEL=$(( LOGLEVEL + 1 ));;
h) ct_print_usage 0;;
o) OPTIONS="$OPTIONS,$OPTARG";;
O) FILTER="$FILTER,$OPTARG";;
:)
error "option requires an argument -- '$OPTARG'"
ct_print_usage 1
;;
?)
error "invalid option -- '$OPTARG'"
ct_print_usage 1
;;
esac
done
shift $(( OPTIND - 1 ))
# Warn if any of the dependencies are missing
for dep in $DEPS; do
type $dep >/dev/null 2>&1 || info "$dep not found, some functionality may fail"
done
# Check for UDEV
if pidof udevd >/dev/null 2>&1; then
UDEVRUNNING=1
info "Detected udevd"
else
info "udevd not running, or unable to detect it: waiting for devices disabled"
fi
# Pre-parse OPTIONS, a little ugly, but it works
preparse() {
local args tmpfs
if ! ct_parse_options $OPTIONS; then
error "Invalid options string: $OPTIONS"
exit 1
fi
OPTIONS="$args"
}
preparse
if [ -z "$action" -o "$action" = "list" ]; then
if [ $# -ne 0 ]; then
warn "With -L, volumes given on the command line have no effect"
fi
list_func() { printf "%s\n" "$1"; }
ct_read_crypttab list_func
elif [ "$action" = "unmap" ]; then
if [ $# -gt 0 ]; then
[ "$FILTER" != "!noauto" ] && \
info "Filters from -O are ignored in this mode"
unset FILTER
fi
ct_main_unmap "$@"
elif [ "$action" = "map" ]; then
if [ $# -ne 0 ]; then
[ "$FILTER" != "!noauto" ] && \
info "Filters from -O are ignored in this mode"
unset FILTER
fi
ct_main_map "$@"
else
error "Internal error: no action"
false
fi
}
ct_main_unmap() {
if [ $# -eq 0 ]; then
ct_read_crypttab ct_unmap
else
local vol ret=0
for vol in "$@"; do
ct_unmap "$vol" || ret=$(( ret+1 ))
done
return $ret
fi
}
ct_main_map() {
if [ $# -eq 0 ]; then
ct_read_crypttab ct_map
elif [ $# -eq 1 ]; then
local vol _tmp="$1"
find_func() {
if [ "$1" = "$_tmp" -o "$2" = "$_tmp" ]; then
printf "%s" "$*"
else
false
fi
}
if vol="$(ct_read_crypttab -1 find_func)"; then
ct_map $vol
else
error "Unable to find '$_tmp' in '$CRYPTTAB'"
false
fi
elif [ $# -le 3 ]; then
ct_map "$@"
else
error "Too many options given for -M"
ct_print_usage 1
fi
}
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
# #
# Functions for iterating over crypttab #
# #
ct_read_crypttab() {
if [ ! -f "$CRYPTTAB" -o ! -r "$CRYPTTAB" ]; then
error "cannot read $CRYPTTAB"
return 1
fi
local func="$@" line lineno=0 name dev key options ret=0 adhoc=0
if [ "$1" = "-1" ]; then
adhoc=1
shift
func="$@"
fi
while read -r name dev key options <&3; do
lineno=$(( lineno + 1 ))
[ -z "$name" ] || [ "$(substring "$name" 0 1)" = "#" ] && continue
# unescape devname and keyname
name=$(printf '%b' "$name")
dev=$(printf '%b' "$dev")
key=$(printf '%b' "$key")
if [ -z "$name" ]; then
warn "$CRYPTTAB:$lineno: the name (first column) cannot be blank"
continue
elif [ -z "$dev" ]; then
warn "$CRYPTTAB:$lineno: the device (second column) cannot be blank"
continue
fi
case $key in
-|none|"")
key=-
;;
/dev/random|/dev/urandom|/dev/hw_random)
options="$options,%random"
;;
ASK)
info "$CRYPTTAB:$lineno: ASK is a deprecated key, please use '-' or 'none'"
key=-
;;
SWAP)
info "$CRYPTTAB:$lineno: SWAP is a deprecated key, please use '/dev/urandom' and the 'swap' option"
key="/dev/urandom"
options="$options,swap,%random"
;;
/*|UUID=*|PARTUUID=*|LABEL=*)
:
;;
*)
warn "$CRYPTTAB:$lineno: plain text keys are not supported"
key=-
;;
esac
if ct_check_filter $options; then
if ! $func "$name" "$dev" "$key" $options; then
ret=$(( ret + 1 ))
elif [ $adhoc -eq 1 ]; then
ret=0
break
fi
fi
done 3< "$CRYPTTAB"
return $ret
}
ct_check_filter() {
local IFS fltr opt
IFS="$(printf ',')"
for fltr in $FILTER; do
fltr="$(trim $fltr)"
[ -z "$fltr" ] && continue
if [ x"$(substring "$fltr" 0 1)" != "x!" ]; then
for opt in $*; do
opt="$(trim $opt)"
[ -z "$opt" ] && continue
if [ "$fltr" = "$opt" -o "$fltr" = "${opt%%=*}" ]; then
continue 2
fi
done
return 1
else
for opt in $*; do
opt="$(trim $opt)"
[ -z "$opt" ] && continue
if [ "$fltr" = "!$opt" -o "$fltr" = "!${opt%%=*}" ]; then
return 1
fi
done
fi
done
return 0
}
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
# #
# Mapping, unmapping, finding stuff #
# #
ct_unmap() {
local name="$1"
if [ ! -e "/dev/mapper/$name" ]; then
warn "Volume was not mapped (no '/dev/mapper/$name')"
elif run cryptsetup remove "$name"; then
info "$name unmapped"
else
error "failed to unmap $name"
false
fi
}
ct_map() {
local name="$1" dev="$2" key="$3" args="" tmpfs
local key_dev="" key_fstype="" key_mntpnt="" key_dev_umount=0
shift 3
if [ -e "/dev/mapper/$name" ]; then
error "Volume is already mapped ('/dev/mapper/$name' exists')"
return 1
fi
# this function sets the args and tmpfs variables
if ! ct_parse_options "$@"; then
error "Unable to parse options"
return 1
fi
args="$args $OPTIONS"
# resolve the encrypted device, can't do much without this
if ! dev="$(ct_resolve_device "$dev")"; then
error "device '$dev' not found"
return 1
fi
# parse various key formats
case "$key" in
*:*:*)
key_dev="${key%%:*}"
key="${key#*:}"
key_fstype="${key%%:*}"
key="${key#*:}"
case "$key_fstype" in
*[!0-9]*)
:;;
*)
warn "<dev>:<offset>:<length> is a deprecated key format. Please use"
warn " the keyfile-offset and keyfile-size options instead. This"
warn " format will *soon* be removed from cryptmount/crypttab!"
opts="$opts --keyfile-offset=$key_fstype --keyfile-size=$key"
key="$key_dev"
unset key_fstype
unset key_dev
esac
;;
*:*)
key_dev="${key%%:*}"
key="${key#*:}"
;;
""|-)
unset key_dev
unset key
;;
*)
unset key_dev
;;
esac
# resolve any needed key device and mount if necessary
if [ "$key_dev" ]; then
if key_dev="$(ct_resolve_device "$key_dev")"; then
if key_mntpnt="$(get_mount "$key_dev")"; then
key="$key_mntpnt/$key"
elif key_mntpnt="$(mktemp -d)"; then
[ -n "$key_fstype" ] && key_fstype="-t $key_fstype"
if run mount -r $key_fstype "$key_dev" "$key_mntpnt"; then
key="$key_mntpnt/$key"
key_dev_umount=1
else
error "unable to mount key device '$key_dev',"
error " falling back on interactive password"
unset key
fi
else
error "unable to find or create mountpoint for key device,"
error " falling back on interactive password"
unset key
fi
else
error "key device '$key_dev' not found"
error " falling back on interactive password"
unset key
fi
elif [ -n "$key" -a "$key" != "-" ]; then
if ! key="$(ct_resolve_device "$key")"; then
error "key '$key' not found"
error " falling back on interactive password"
unset key
fi
fi
if [ "$key" ]; then
key=--key-file="$key"
fi
local ret=0
# the main event, run cryptsetup (and mkswap, mkfs if necessary)
if cryptsetup isLuks "$dev"; then
info "device '$dev' detected as LUKS"
if run cryptsetup luksOpen $key $args "$dev" "$name"; then
info "sucessfully mapped '$dev' to '/dev/mapper/$name'"
else
error "unable to map '$dev' to '/dev/mapper/$name'"
ret=1
fi
else
info "device '$dev' assumed to be plain"
# cryptsetup 'create' can be destructive, don't do it if blkid can
# identify the device type
if [ $FORCE -ne 1 ] && blkid -p "$dev" >/dev/null 2>&1; then
error "Refusing to call 'cryptsetup create' on device that might"
error " have data. If you are sure this is what you want, use"
error " the -f option"
ret=1
elif run cryptsetup create $key $args "$name" "$dev"; then
info "sucessfully mapped '$dev' to '/dev/mapper/$name'"
if [ "$tmpfs" = "swap" ]; then
if run mkswap -f -L "$name" "/dev/mapper/$name"; then
info "mkswap successful on '/dev/mapper/$name'"
else
error "mkswap failed for '/dev/mapper/$name'"
ret=1
fi
elif [ "$tmpfs" ]; then
if run mkfs -t "$tmpfs" "/dev/mapper/$name"; then
info "mkfs successful on '/dev/mapper/$name'"
else
error "mkfs failed for '/dev/mapper/$name'"
ret=1
fi
fi
else
error "unable to map '$dev' to '/dev/maper/name/$name'"
ret=1
fi
fi
# clean up after ourselves
if [ $key_dev_umount -eq 1 ]; then
if ! run umount "$key_dev"; then
warn "unable to mount key device '$key_dev'"
else
run rmdir "$key_mntpnt"
fi
fi
return $ret
}
ct_resolve_device() {
local tmp="" device="$1" seconds=$WAITTIME tag tagval
case "$device" in
UUID=*|PARTUUID=*|LABEL=*)
tmp="$(blkid -l -o device -t "$device")"
if [ -z "$tmp" ]; then
if [ $UDEVRUNNING -eq 1 ]; then
tag="$(awk -v t="${device%%=*}" 'BEGIN { print tolower(t) }')"
tagval="${device#*=}"
device="/dev/disk/by-$tag/$tagval"
fi
else
device="$tmp"
fi
esac
if [ ! -e "$device" -a "$(substring "$device" 0 5)" = "/dev/" -a "$UDEVRUNNING" -eq 1 ]; then
msg "Waiting $seconds seconds for '$device'..."
until [ -e "$device" -o $seconds -eq 0 ]; do
sleep 1
seconds=$(( seconds - 1 ))
done
fi
printf "%s" "$device"
if [ -e "$device" ]; then
info "resolve: found '$device'"
else
error "resolve: unable to find '$device'"
return 1
fi
}
ct_parse_options() {
local IFS=',' optlst="$*" opt key val depr=0
for opt in $optlst; do
# separate key and value
unset key val
case "$opt" in
"")
continue;;
-*)
if [ $depr -eq 0 ]; then
info "You are using a deprecated format for the options field. The entire"
info " field will be passed directly to cryptsetup. Please use the more"
info " standardized comma-deliminated options list instead. This format"
info " will be removed in a future version of cryptmount/crypttab!"
depr=1
fi
args="$args $opt"
continue
;;
*=*)
key="${opt%%=*}"
val="${opt#*=}"
[ "$key" = "$val" ] && unset val
;;
*)
key="$opt";;
esac
case "$key" in
swap)
# set external variable
tmpfs="swap"
;;
luks|plain)
warn "Ignoring option $key, LUKS volumes are automatically detected"
;;
noauto|%*)
:
;;
skip|precheck|check|checkargs|noearly|loud|keyscript)
warn "Ignoring Debian specific option '$key'"
;;
tmp)
# set an external variable
[ -z "$val" ] && msg "Defaulting tmp to ext4"
tmpfs="${val:-ext4}"
;;
size)
args="$args --key-size $val"
;;
device-size)
args="$args --size $val"
;;
none)
args=
;;
*)
if [ ${#key} -eq 1 ]; then
args="$args -$key $val"
else
args="$args --$key $val"
fi
;;
esac
done
return 0
}
# #
# ---------------------------------------------------------------------------- #
ct_main "$@"
# vim: set ft=sh noet ts=2 sw=2 :