This file is indexed.

/usr/share/initramfs-tools/hooks/cryptroot is in cryptsetup 2:1.7.3-4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
#!/bin/sh

PREREQ=""

prereqs()
{
	echo "$PREREQ"
}

case $1 in
prereqs)
	prereqs
	exit 0
	;;
esac

. /usr/share/initramfs-tools/hook-functions

# get_fs_devices() - determine source device(s) of mount point from /etc/fstab
#
# expected arguments:
# * fstab mount point (full path)
#
# This function searches for the first entry from /etc/fstab which has the
# given mountpoint set. It returns the canonical_device() of corresponding
# source device (first field in /etc/fstab entry).
# In case of btrfs, canoncial_device() of all btrfs source devices (possibly
# more than one) are returned.
#
get_fs_devices() {
	local device mount type options dump pass
	local wantmount="$1"

	if [ ! -r /etc/fstab ]; then
		return 1
	fi

	grep -s '^[^#]' /etc/fstab | \
	while read device mount type options dump pass; do
		if [ "$mount" = "$wantmount" ]; then
			local devices
			if [ "$type" = "btrfs" ]; then
				for dev in $(btrfs filesystem show $(canonical_device "$device" --no-simplify) 2>/dev/null | sed -r -e 's/.*devid .+ path (.+)/\1/;tx;d;:x') ; do
					devices="${devices:+$devices }$(canonical_device "$dev")"
				done
			else
				devices=$(canonical_device "$device") || return 0
			fi
			printf '%s' "$devices"
			return
		fi
	done
}

# get_resume_devices() - determine devices used for system suspend/hibernate
#
# expected arguments:
# * none
#
# This function searches well known places for devices that are used for system
# suspension and/or hibernation. It returns canonical_device() of any detected
# devices and prints a warning if more than one device is detected.
#
get_resume_devices() {
	local device opt count dupe candidates devices derived
	candidates=""

	# First, get a list of potential resume devices

	# uswsusp
	if [ -e /etc/uswsusp.conf ]; then
		device=$(sed -rn 's/^resume device[[:space:]]*[:=][[:space:]]*// p' /etc/uswsusp.conf)
		if [ -n "$device" ]; then
			candidates="${candidates:+$candidates }$device"
		fi
	fi

	# uswsusp - again...
	if [ -e /etc/suspend.conf ]; then
		device=$(sed -rn 's/^resume device[[:space:]]*[:=][[:space:]]*// p' /etc/suspend.conf)
		if [ -n "$device" ]; then
			candidates="${candidates:+$candidates }$device"
		fi
	fi

	# regular swsusp
	for opt in $(cat /proc/cmdline); do
		case $opt in
			resume=*)
				device="${opt#resume=}"
				candidates="${candidates:+$candidates }$device"
				;;
		esac
	done

	# initramfs-tools >=0.129
	device="${RESUME:-auto}"
	if [ "$device" != none ]; then
		if [ "$device" = auto ]; then
			# next line from /usr/share/initramfs-tools/hooks/resume
			device="$(grep ^/dev/ /proc/swaps | sort -rnk3 | head -n 1 | cut -d " " -f 1)"
			if [ -n "$device" ]; then
				device="UUID=$(blkid -s UUID -o value "$device" || true)"
			fi
		fi
		candidates="${candidates:+$candidates }$device"
	fi

	# Now check the sanity of all candidates
	devices=""
	count=0
	for device in $candidates; do
		# Remove quotes around device candidate
		device=$(printf '%s' "$device" | sed -r -e 's/^"(.*)"\s*$/\1/' -e "s/^'(.*)'\s*$/\1/")

		# Weed out clever defaults
		if [ "$device" = "<path_to_resume_device_file>" ]; then
			continue
		fi

		# Detect devices required by decrypt_derived
		derived=$(get_derived_device "$device")
		if [ -n "$derived" ]; then
			devices="${devices:+$devices }$derived"
		fi

		device=$(canonical_device "$device") || return 0

		# Weed out duplicates
		dupe=0
		for opt in $devices; do
			if [ "$device" = "$opt" ]; then
				dupe=1
			fi
		done
		if [ $dupe -eq 1 ]; then
			continue
		fi

		# This device seems ok
		devices="${devices:+$devices }$device"
		count=$(( $count + 1 ))
	done

	if [ $count -gt 1 ]; then
		echo "cryptsetup: WARNING: found more than one resume device candidate:" >&2
		for device in $devices; do
			echo "                     $device" >&2
		done
	fi

	if [ $count -gt 0 ]; then
		printf '%s' "$devices"
	fi

	return 0
}

# get_initramfs_devices() - determine devices with explicit 'initramfs' option
#
# expected arguments:
# * none
#
# This function processes entries from /etc/crypttab with the 'initramfs'
# option set. For each processed device, potential get_derived_device()
# devices are determined. The canonical_device() of each detected device
# is returned.
#
get_initramfs_devices() {
	local device opt count dupe target source key options candidates devices derived

	candidates="$(grep -s '^[^#]' /etc/crypttab | \
	while read target source key options; do
		if printf '%s' "$options" | grep -Eq "^(.*,)?initramfs(,.*)?$"; then
			echo " /dev/mapper/$target"
		fi
	done;)"

	devices=""
	count=0
	for device in $candidates; do
		# Detect devices required by decrypt_derived
		derived=$(get_derived_device "$device")
		if [ -n "$derived" ]; then
			devices="${devices:+$devices }$derived"
		fi

		device=$(canonical_device "$device") || return 0

		# Weed out duplicates
		dupe=0
		for opt in $devices; do
			if [ "$device" = "$opt" ]; then
				dupe=1
			fi
		done
		if [ $dupe -eq 1 ]; then
			continue
		fi

		# This device seems ok
		devices="${devices:+$devices }$device"
		count=$(( $count + 1 ))
	done

	if [ $count -gt 0 ]; then
		printf '%s' "$devices"
	fi

	return 0
}

# get_derived_device() - determine dependency devices for decrypt_derived
#
# expected arguments:
# * crypttab target device name (either <name> or /dev/mapper/<name>)
#
# This function takes a target device name and checks whether this device has
# the decrypt_derived keyscript set in /etc/crypttab. If true, the dependency
# device required for the decrypt_derived keyscript is detected and its
# canonical_device() returned if it's not listed in $rootdevs.
#
get_derived_device() {
	local device derived
	device="$1"

	derived="$( awk -vtarget="${device#/dev/mapper/}" \
		'$1 == target && $4 ~ /^(.*,)?keyscript=([^,]*\/)?decrypt_derived(,.*)?$/ {print $3; exit}' \
		/etc/crypttab )"
	if [ -n "$derived" ]; then
		if node_is_in_crypttab "$derived"; then
			derived=$(canonical_device "/dev/mapper/$derived") || return 0
			if ! printf '%s' "$rootdevs" | tr ' ' '\n' | grep -Fxq "$derived"; then
				printf '%s' "$derived"
			fi
		else
			echo "cryptsetup: WARNING: decrypt_derived device $derived not found in crypttab" >&2
		fi
	fi
}

# node_is_in_crypttab() - test whether a device is configured in /etc/crypttab
#
# expected arguments:
# * crypttab target device names (without /dev/mapper/ prefix)
#
# This function takes a target device name and fails if it is not
# configured in /etc/crypttab.
#
node_is_in_crypttab() {
	[ -f /etc/crypttab ] || return 1
	sed -n '/^[^#]/ s/\s.*//p' /etc/crypttab | grep -Fxq "$1"
}

# node_or_pv_is_in_crypttab() - test whether devices are configured in /etc/crypttab
#
# expected arguments:
# * crypttab target device names (without /dev/mapper/ prefix), or LVM
#   logical volume device-mapper name (format <VG>-<LV>)
#
# This function fails unless every argument is either a target device
# name configured in /etc/crypttab, or an LVM logical volume
# device-mapper name (format <VG>-<LV>) with only parents devices (PVs)
# configured in /etc/crypttab.
#
node_or_pv_is_in_crypttab() {
	local node lvmnodes lvmnode
	for node in "$@"; do
		if ! node_is_in_crypttab "$node"; then
			lvmnodes="$(get_lvm_deps "$node" --assert-crypt)" || return 1
			[ "$lvmnodes" ] || return 1
			for lvmnode in $lvmnodes; do
				node_is_in_crypttab "$lvmnode" || return 1
			done
		fi
	done
	return 0
}

# get_lvm_deps() - determine the parent devices (PVs) of a LVM logical volume
#
# expected arguments:
# * LVM logical volume device-mapper name (format <VG>-<LV>)
# * optional options to the function
#
# This function takes a LVM logical volume name and determines the corresponding
# crypted physical volumes (PVs). It returns the name of the underlying
# device-mapper crypt devices (without /dev/mapper).
# If option '--assert-crypt' is given as second argument, then the
# function fails unless all PVs are dm-crypt devices.
#
get_lvm_deps() {
	local node opt deps maj min depnode
	node="$1"
	opt="${2:-}"

	if [ -z "$node" ]; then
		echo "cryptsetup: WARNING: get_lvm_deps - invalid arguments" >&2
		return 1
	fi

	if ! deps=$(vgs --noheadings -o pv_name $(dmsetup --noheadings splitname $node | cut -d':' -f1) 2>/dev/null); then
		# $node is not a LVM node, stopping here
		[ "$opt" != '--assert-crypt' ] && return 0 || return 1
	fi

	# We should now have a list of physical volumes for the VG
	for dep in $deps; do
		depnode=$(dmsetup info -c --noheadings -o name "$dep" 2>/dev/null)
		if [ -z "$depnode" ]; then
			[ "$opt" != '--assert-crypt' ] && continue || return 1
		fi
		if [ "$(dmsetup table "$depnode" 2>/dev/null | cut -d' ' -f3)" != "crypt" ]; then
			get_lvm_deps "$depnode" $opt || return 1
			continue
		fi
		printf '%s\n' "$depnode"
	done

	return 0
}

# get_device_opts() - determine and set options for a crypttab target device
#
# expected arguments:
# * crypttab target device name (without /dev/mapper/ prefix)
# * optional extra options
#
# This function determines options for a crypttab target device and sets them
# accordingly. In order to detect the options, it parses the corresponding
# /etc/crypttab entry and takes optional extra options as second argument.
# Some sanity checks are done on the corresponding source device and configured
# options.
# After everything is processed, the options are saved in '$OPTIONS' for later
# access by parent functions.
#
get_device_opts() {
	local target source link extraopts rootopts opt key
	target="$1"
	extraopts="$2"
	KEYSCRIPT=""
	KEYFILE="" # key file to copy to the initramfs image
	CRYPTHEADER=""
	OPTIONS=""

	if [ -z "$target" ]; then
		echo "cryptsetup: WARNING: get_device_opts - invalid arguments" >&2
		return 1
	fi

	opt="$( awk -vtarget="$target" '$1 == target {gsub(/[ \t]+/," "); print; exit}' /etc/crypttab )"
	source=$( printf '%s' "$opt" | cut -d " " -f2 )
	key=$( printf '%s' "$opt" | cut -d " " -f3 )
	rootopts=$( printf '%s' "$opt" | cut -d " " -f4- )

	if [ -z "$opt" ] || [ -z "$source" ] || [ -z "$key" ] || [ -z "$rootopts" ]; then
		echo "cryptsetup: WARNING: invalid line in /etc/crypttab for $target - $opt" >&2
		return 1
	fi

	# Sanity checks for $source
	if [ -h "$source" ]; then
		link=$(readlink -nqe "$source")
		if [ -z "$link" ]; then
			echo "cryptsetup: WARNING: $source is a dangling symlink" >&2
			return 1
		fi

		if [ "$link" != "${link#/dev/mapper/}" ]; then
			echo "cryptsetup: NOTE: using $link instead of $source for $target" >&2
			source="$link"
		fi
	fi

	if [ "UUID=${source#UUID=}" = "$source" -a ! -b "/dev/disk/by-uuid/${source#UUID=}" ] || [ "UUID=${source#UUID=}" != "$source" -a ! -b "$source" ]; then
		echo "cryptsetup: WARNING: Invalid source device $source" >&2
	fi

	# Sanity checks for $key
	if [ "$key" = "/dev/random" ] || [ "$key" = "/dev/urandom" ]; then
		echo "cryptsetup: WARNING: target $target has a random key, skipped" >&2
		return 1
	fi

	if [ -n "$extraopts" ]; then
		rootopts="$extraopts,$rootopts"
	fi

	# We have all the basic options, let's go trough them
	OPTIONS="target=$target,source=$source"
	local IFS_BCK="$IFS"
	local IFS=", "
	unset HASH_FOUND
	unset LUKS_FOUND
	for opt in $rootopts; do
		case $opt in
			cipher=*)
				OPTIONS="$OPTIONS,$opt"
				;;
			size=*)
				OPTIONS="$OPTIONS,$opt"
				;;
			hash=*)
				OPTIONS="$OPTIONS,$opt"
				HASH_FOUND=1
				;;
			tries=*)
				OPTIONS="$OPTIONS,$opt"
				;;
			discard)
				OPTIONS="$OPTIONS,$opt"
				;;
			luks)
				LUKS_FOUND=1
				;;
			header=*)
				opt="${opt#header=}"
				if [ ! -e "$opt" ]; then
					echo "cryptsetup: WARNING: target $target has an invalid header, skipped" >&2
					return 1
				fi
				CRYPTHEADER="$opt"
				OPTIONS="$OPTIONS,header=$CRYPTHEADER"
				;;
			tcrypt)
				OPTIONS="$OPTIONS,$opt"
				;;
			keyscript=*)
				opt="${opt#keyscript=}"
				if [ ! -x "/lib/cryptsetup/scripts/$opt" ] && [ ! -x "$opt" ]; then
					echo "cryptsetup: WARNING: target $target has an invalid keyscript, skipped" >&2
					return 1
				fi
				KEYSCRIPT="$opt"
				OPTIONS="$OPTIONS,keyscript=/lib/cryptsetup/scripts/$(basename "$opt")"
				;;
			keyslot=*)
				OPTIONS="$OPTIONS,$opt"
				;;
			veracrypt)
				OPTIONS="$OPTIONS,$opt"
				;;
			lvm=*)
				OPTIONS="$OPTIONS,$opt"
				;;
			rootdev)
				OPTIONS="$OPTIONS,$opt"
				;;
			resumedev)
				OPTIONS="$OPTIONS,$opt"
				;;
			*)
				# Presumably a non-supported option
				;;
		esac
	done
	IFS="$IFS_BCK"

	# Warn for missing hash option, unless we have a LUKS partition
	if [ -z "$HASH_FOUND" ] && [ -z "$LUKS_FOUND" ]; then
		echo "WARNING: Option hash missing in crypttab for target $target, assuming ripemd160." >&2
		echo "         If this is wrong, this initramfs image will not boot." >&2
		echo "         Please read /usr/share/doc/cryptsetup/README.initramfs.gz and add" >&2
		echo "         the correct hash option to your /etc/crypttab."  >&2
	fi

	# Warn that header only applies to a LUKS partition currently
	if [ -n "$CRYPTHEADER" ] && [ -z "$LUKS_FOUND" ]; then
		echo "WARNING: Option LUKS missing in crypttab for target $target." >&2
		echo "         Headers are only supported for LUKS devices." >&2
	fi

	# If keyscript is set, the "key" is just an argument to the script
	if [ "$key" != "none" ] && [ -z "$KEYSCRIPT" ]; then
		case "$key" in
			$KEYFILE_PATTERN)
				KEYFILE="$key"
				key="/cryptroot-keyfiles/${target}.key"
				;;
			*)
				key=$(readlink -e "$key")
				# test whether $target is a root device (or parent of the root device)
				if printf '%s' "$OPTIONS" | grep -Eq '^(.*,)?rootdev(,.*)?$'; then
					echo "cryptsetup: WARNING: root target $target uses a key file, skipped" >&2
					return 1
				# test whether a) key file is not on root fs
				#           or b) root fs is not encrypted
				elif [ "$(stat -c %m -- "$key" 2>/dev/null)" != / ] || ! node_or_pv_is_in_crypttab $rootdevs; then
					echo "cryptsetup: WARNING: $target's key file $key is not on an encrypted root FS, skipped" >&2
					return 1
				fi
				if printf '%s' "$OPTIONS" | grep -Eq '^(.*,)?resumedev(,.*)?$'; then
					# we'll be able to decrypt the device, but won't be able to use it for resuming
					echo "cryptsetup: WARNING: resume device $source uses a key file" >&2
				fi
				# prepend "/root" (to be substituted by the real root FS
				# mountpoint "$rootmnt" in the boot script) to the
				# absolute filename
				key="/root$key"
				;;
		esac
		OPTIONS="$OPTIONS,keyscript=cat"
	fi
	OPTIONS="$OPTIONS,key=$key"
}

# get_device_modules() - determine required crypto kernel modules for device
#
# expected arguments:
# * crypttab target device name (without /dev/mapper/ prefix)
#
# This function determines the required crypto kernel modules for cipher,
# block cipher and optionally ivhash of the target device and returns them.
#
get_device_modules() {
	local node value cipher blockcipher ivhash
	node="$1"

	# Check the ciphers used by the active root mapping
	value=$(dmsetup table "$node" | cut -d " " -f4)
	cipher=$(echo "$value" | cut -d ":" -f1 | cut -d "-" -f1)
	blockcipher=$(echo "$value" | cut -d ":" -f1 | cut -d "-" -f2)
	ivhash=$(echo "$value" | cut -d ":" -s -f2)

	if [ -n "$cipher" ]; then
		echo "$cipher"
	else
		return 1
	fi

	if [ -n "$blockcipher" ] && [ "$blockcipher" != "plain" ]; then
		echo "$blockcipher"
	fi

	if [ -n "$ivhash" ] && [ "$ivhash" != "plain" ]; then
		echo "$ivhash"
	fi
	return 0
}

# canonical_device() - determine the 
#
# expected arguments:
# * device (either full path or LABEL=<x> or UUID=<y>)
# * optional options to the function
#
# This function takes a device as argument and determines the corresponding
# canonical device name.
# If option '--no-simplify' is given as second argument, then the origin device
# path after unraveling LABEL= and UUID= format and following symlinks is
# returned.
# If no option is given, the device is further unraveled and depending on the
# device path, either the corresponding device-mapper path (as found in
# /dev/mapper/) or the the corresponding disk symlink (as found in
# /dev/disk/by-*/) is returned.
#
canonical_device() {
	local dev altdev original
	dev="$1"
	opt="$2"

	if [ "${dev#LABEL=}" != "$dev" ]; then
		altdev="${dev#LABEL=}"
		dev="/dev/disk/by-label/$(printf '%s' "$altdev" | sed 's,/,\\x2f,g')"
	elif [ "${dev#UUID=}" != "$dev" ]; then
		altdev="${dev#UUID=}"
		dev="/dev/disk/by-uuid/$altdev"
	fi

	original="$dev"
	if [ -h "$dev" ]; then
		dev=$(readlink -e "$dev")
	fi

	if [ "$opt" = "--no-simplify" ]; then
		printf '%s' "$dev"
		return 0
	fi

	if [ "x${dev%/dev/dm-*}" = "x" ]; then
		# try to detect corresponding symlink in /dev/mapper/
		for dmdev in /dev/mapper/*; do
			if [ "$(readlink -e "$dmdev")" = "$dev" ]; then
				dev="$dmdev"
			fi
		done
	fi

	altdev="${dev#/dev/mapper/}"
	if [ "$altdev" != "$dev" ]; then
		printf '%s' "$altdev"
		return 0
	elif [ "x${original%/dev/disk/by-*/*}" = "x" ]; then
		# support crypttab UUID/LABEL entries
		# this is a /dev/disk/by-*/ path so return just the 'basename'
		echo "${original##/dev/disk/by-*/}"
		return 0
	fi

	echo "cryptsetup: WARNING: failed to detect canonical device of $original" >&2
	return 1
}

# add_device() - Process a given device and add to /conf/conf.d/cryptroot
#
# expected arguments:
# * device name (either crypttab target device name without /dev/mapper/ prefix
#                    or LVM device-mapper name in format '<VG>:<LV>')
#
# This function takes a device name, does all required processing and adds the
# result with all device options to /conf/conf.d/cryptroot in the initramfs.
# Additionally, it returns required kernel modules.
#
add_device() {
	local node nodes lvmnodes opts lastopts i count
	nodes="$1"
	opts=""     # Applied to all nodes
	lastopts="" # Applied to last node

	if [ -z "$nodes" ]; then
		return 0
	fi

	# Flag root and resume devices
	if printf '%s' "$rootdevs" | tr ' ' '\n' | grep -Fxq "$nodes"; then
		opts="${opts:+$opts,}rootdev"
	fi
	if printf '%s' "$resumedevs" | tr ' ' '\n' | grep -Fxq "$nodes"; then
		opts="${opts:+$opts,}resumedev"
	fi

	# Check that it is a node under /dev/mapper/
	# nodes=$(canonical_device "$nodes") || return 0

	# Can we find this node in crypttab
	if ! node_is_in_crypttab "$nodes"; then
		# dm node but not in crypttab, is it a lvm device backed by dm-crypt nodes?
		lvmnodes=$(get_lvm_deps "$nodes") || return 1

		# not backed by any dm-crypt nodes; stop here
		if [ -z "$lvmnodes" ]; then
		    return 0
		fi

		# It is a lvm device!
		opts="${opts:+$opts,}lvm=$nodes"
		nodes="$lvmnodes"
	fi

	# Prepare to setup each node
	count=$(printf '%s' "$nodes" | wc -w)
	i=1
	for node in $nodes; do
		# Prepare the additional options
		if [ $i -eq $count ]; then
			if [ -n "$lastopts" ]; then
				opts="${opts:+$opts,}$lastopts"
			fi
		fi

		# Get crypttab root options
		if ! get_device_opts "$node" "$opts"; then
			continue
		fi
		printf '%s\n' "$OPTIONS" >>"$DESTDIR/conf/conf.d/cryptroot"

		# If we have a keyscript, make sure it is included
		if [ -n "$KEYSCRIPT" ]; then
			if [ ! -d "$DESTDIR/lib/cryptsetup/scripts" ]; then
				mkdir -p "$DESTDIR/lib/cryptsetup/scripts"
			fi

			if [ -e "/lib/cryptsetup/scripts/$KEYSCRIPT" ]; then
				copy_exec "/lib/cryptsetup/scripts/$KEYSCRIPT" /lib/cryptsetup/scripts >&2
			elif [ -e "$KEYSCRIPT" ]; then
				copy_exec "$KEYSCRIPT" /lib/cryptsetup/scripts >&2
			elif KSTYPE="$(type "$KEYSCRIPT" 2>&1)"; then
				if [ -x "${KSTYPE#"$KEYSCRIPT" is }" ]; then
					copy_exec "${KSTYPE#"$KEYSCRIPT" is }" /lib/cryptsetup/scripts >&2
				fi
			else
				echo "cryptsetup: WARNING: failed to find keyscript $KEYSCRIPT" >&2
				continue
			fi
		elif [ -n "$KEYFILE" ]; then
			case "$KEYFILE" in
				$KEYFILE_PATTERN)
					mkdir -pm0700 "$DESTDIR/cryptroot-keyfiles"
					cp --preserve=all "$KEYFILE" "$DESTDIR/cryptroot-keyfiles/${node}.key"
					;;
			esac
		fi

		# If we have a LUKS header, make sure it is included
		# TODO: make it configurable to include the LUKS header into initramfs
		# disabled for now due to security reasons
		if [ -n "$CRYPTHEADER" ]; then
			if [ ! -d "$DESTDIR/conf/conf.d/cryptheader" ]; then
				mkdir -p "$DESTDIR/conf/conf.d/cryptheader"
			fi

			#if [ -e "$CONFDIR/conf.d/cryptheader/$CRYPTHEADER" ]; then
			#	copy_exec "$CONFDIR/conf.d/cryptheader/$CRYPTHEADER" /conf/conf.d/cryptheader >&2
			#elif [ -e "$CRYPTHEADER" ]; then
			#	copy_exec "$CRYPTHEADER" /conf/conf.d/cryptheader >&2
			#else
			#	echo "cryptsetup: WARNING: failed to find LUKS header $CRYPTHEADER" >&2
			#	continue
			#fi
		fi
		

		# Calculate needed modules
		modules=$(get_device_modules $node | sort | uniq)
		if [ -z "$modules" ]; then
			echo "cryptsetup: WARNING: failed to determine cipher modules to load for $node" >&2
			continue
		fi
		echo dm_mod
		echo dm_crypt
		echo "$modules"
		# Load hardware aes module
		if cpu_has_aesni; then
			echo aesni
		fi
		i=$(( $i + 1 ))
	done

	return 0
}

# cpu_has_aesni() - Detect whether the host CPU has AES-NI support
#
# expected arguments:
# * none
#
# This functions returns true when the host CPU has AES-NI support.
#
cpu_has_aesni() {
	return $(grep -q "^flags\s*:\s*.*aes" /proc/cpuinfo)
}

# add_crypto_modules() - determine kernel module path and add to initramfs
#
# expected arguments:
# * kernel module name
#
# This function takes a kernel module name, determines the corresponding path
# and runs manual_add_modules() from initramfs hook functions to add the module
# to the initramfs.
#
add_crypto_modules() {
	local mod file altmod found genericfound
	mod="$1"
	found=""
	genericfound=""

	if [ -z "$mod" ]; then
		return 1
	fi

	# We have several potential sources of modules (in order of preference):
	#
	#   a) /lib/modules/$VERSION/kernel/arch/$ARCH/crypto/$mod-$specific.ko
	#   b) /lib/modules/$VERSION/kernel/crypto/$mod_generic.ko
	#   c) /lib/modules/$VERSION/kernel/crypto/$mod.ko
	#
	# and (currently ignored):
	#
	#   d) /lib/modules/$VERSION/kernel/drivers/crypto/$specific-$mod.ko

	for file in $(find "$MODULESDIR/kernel/arch/" -name "$mod-*.ko" 2>/dev/null); do
		altmod="${file##*/}"
		altmod="${altmod%.ko}"
		manual_add_modules "$altmod"
		found="yes"
	done

	for file in $(find "$MODULESDIR/kernel/crypto/" -name "${mod}_generic.ko" 2>/dev/null); do
		altmod="${file##*/}"
		altmod="${altmod%.ko}"
		manual_add_modules "$altmod"
		found="yes"
		genericfound="yes"
	done

	if [ -z "$genericfound" ]; then
		for file in $(find "$MODULESDIR/kernel/crypto/" -name "${mod}.ko" 2>/dev/null); do
			altmod="${file##*/}"
			altmod="${altmod%.ko}"
			manual_add_modules "$altmod"
			found="yes"
		done
	fi

	if [ -z "$found" ]; then
		return 1
	fi

	return 0
}

#
# Begin real processing
#

setup="no"
rootdevs=""
usrdevs=""
resumedevs=""

# XXX Backward compatibility: remove once Stretch has been promoted stable
for v in CRYPTSETUP KEYFILE_PATTERN; do
	if eval [ "\${$v+x}" ]; then
		echo "WARNING: Setting $v in /etc/initramfs-tools/initramfs.conf" \
		     "is deprecated and will stop working in the future." \
		     "Use /etc/cryptsetup-initramfs/conf-hook instead." >&2
	fi
done

# Load the hook's config
if [ -f "/etc/cryptsetup-initramfs/conf-hook" ]; then
	. /etc/cryptsetup-initramfs/conf-hook
fi

# Include cryptsetup modules, regardless of _this_ machine configuration
if [ -n "$CRYPTSETUP" ] && [ "$CRYPTSETUP" != "n" ]; then
	setup="yes"
fi

if [ "$KEYFILE_PATTERN" ]; then
	setup="yes"
	case "${UMASK:-$(umask)}" in
		0[0-7]77) ;;
		*) echo "WARNING: permissive UMASK (${UMASK:-$(umask)})." \
		        "Private key material inside the initrd might be left unprotected." >&2
		;;
	esac
fi

# Find the root and resume device(s)
if [ -r /etc/crypttab ]; then
	rootdevs=$(get_fs_devices /)
	if [ -z "$rootdevs" ]; then
		echo "cryptsetup: WARNING: could not determine root device from /etc/fstab" >&2
	fi
	usrdevs=$(get_fs_devices /usr)
	resumedevs=$(get_resume_devices)
	initramfsdevs=$(get_initramfs_devices)
fi

# Load the config opts and modules for each device
for dev in $rootdevs $usrdevs $resumedevs $initramfsdevs; do
	if ! modules=$(add_device "$dev"); then
		echo "cryptsetup: FAILURE: could not determine configuration for $dev" >&2
		continue
	fi

	if [ -n "$modules" ]; then
		setup="yes"
	fi

	if [ "$setup" = "no" ]; then
		continue
	fi

	if [ "$MODULES" = "most" ]; then
		archcrypto="$(find "$MODULESDIR/kernel/arch" -type d -name "crypto" 2>/dev/null)"
		if [ -n "$archcrypto" ]; then
			copy_modules_dir "${archcrypto##*${MODULESDIR}/}"
		fi
		copy_modules_dir "kernel/crypto"
	else
		for mod in $modules; do
			add_crypto_modules $mod
		done
	fi
done

# With large initramfs, we always add a basic subset of modules
if [ "$MODULES" != "dep" ] && [ "$setup" = "yes" ]; then
	for mod in aes cbc chainiv cryptomgr krng sha256 xts; do
		add_crypto_modules $mod
	done
fi

# See if we need to add the basic components
if [ "$setup" = "yes" ]; then
	for mod in dm_mod dm_crypt; do
		manual_add_modules $mod
	done

	copy_exec /sbin/cryptsetup
	copy_exec /sbin/dmsetup
	copy_exec /lib/cryptsetup/askpass

	# We need sed. Either via busybox or as standalone binary.
	if [ "$BUSYBOX" = "n" ] || [ ! -e ${BUSYBOXDIR}/busybox ]; then
		copy_exec /bin/sed
	fi
fi

exit 0