# Part of passwordless cryptofs setup in Debian Etch.
# See: https://wejn.org/how-to-make-passwordless-cryptsetup.html
# Author: Wejn <wejn at box dot cz>
# Updated by Rodolfo Garcia (kix) <kix at kix dot com>
# For multiple partitions
# http://www.kix.es/
# Updated by TJ <linux@tjworld.net> 7 July 2008
# For use with Ubuntu Hardy, usplash, automatic detection of USB devices,
# detection and examination of *all* partitions on the device (not just partition #1),
# automatic detection of partition type, refactored, commented, debugging code.
# Updated by Hendrik van Antwerpen <hendrik at van-antwerpen dot net> 3 Sept 2008
# For encrypted key device support, also added stty support for not
# showing your password in console mode.
# Updated by Jan-Pascal van Best janpascal/at/vanbest/org 2009-12-07
# to support latest debian updates (vol_id missing, blkid used instead)

# define counter-intuitive shell logic values (based on /bin/true & /bin/false)
# NB. use FALSE only to *set* something to false, but don't test for
# equality, because a program might return any non-zero on error

# set DEBUG=$TRUE to display debug messages, DEBUG=$FALSE to be quiet

# is usplash available? default false
# test for outfifo from Ubuntu Hardy cryptroot script, the second test
# alone proves not completely reliable.
if [ -p /dev/.initramfs/usplash_outfifo -a -x /sbin/usplash_write ]; then
    # use innocuous command to determine if usplash is running
    # usplash_write will return exit-code 1 if usplash isn't running
    # need to set a flag to tell usplash_write to report no usplash
    # enable verbose messages (required to display messages if kernel boot option "quiet" is enabled
    /sbin/usplash_write "VERBOSE on"
    if [ $? -eq $TRUE ]; then
        # usplash is running
        /sbin/usplash_write "CLEAR"

# is stty available? default false
# check for stty executable
if [ -x /bin/stty ]; then
elif [ `(busybox stty >/dev/null 2>&1; echo $?)` -eq $TRUE ]; then
    STTYCMD="busybox stty"

# print message to usplash or stderr
# usage: msg <command> "message" [switch]
# command: TEXT | STATUS | SUCCESS | FAILURE | CLEAR (see 'man usplash_write' for all commands)
# switch : switch used for echo to stderr (ignored for usplash)
# when using usplash the command will cause "message" to be
# printed according to the usplash <command> definition.
# using the switch -n will allow echo to write multiple messages
# to the same line
msg ()
    if [ $# -gt 0 ]; then
        # handle multi-line messages
        echo $2 | while read LINE; do
            if [ $USPLASH -eq $TRUE ]; then
                # use usplash
                /sbin/usplash_write "$1 $LINE"      
                # use stderr for all messages
                echo $3 "$2" >&2

dbg ()
    if [ $DEBUG -eq $TRUE ]; then
        msg "$@"

# read password from console or with usplash
# usage: readpass "prompt"
readpass ()
    if [ $# -gt 0 ]; then
        if [ $USPLASH -eq $TRUE ]; then
            usplash_write "INPUTQUIET $1: "
            PASS="$(cat /dev/.initramfs/usplash_outfifo)"
            [ $STTY -ne $TRUE ] && msg TEXT "WARNING stty not found, password will be visible"
            echo -n "$1" >&2
            $STTYCMD -echo
            read -r PASS </dev/console >/dev/null
            [ $STTY -eq $TRUE ] && echo >&2
            $STTYCMD echo
    echo -n "$PASS"

dbg STATUS "Executing crypto-usb-key.sh ..."

# flag tracking key-file availability

# temporary mount path for USB key

if [ "x$1" = "x" -o "x$1" = "xnone" ]; then
    # default key-file on the USB disk

# If the file already exists use it.
# This is useful where an encrypted volume contains keyfile(s) for later
# volumes and is now mounted and accessible
if [ -f $KEYFILE ]; then
    dbg TEXT "Found $KEYFILE"
    cat $KEYFILE
    DEV="existing mount"
    # Is the USB driver loaded?
    cat /proc/modules | busybox grep usb_storage >/dev/null 2>&1
    if [ $USBLOAD -gt 0 ]; then
        dbg TEXT "Loading driver 'usb_storage'"
        modprobe usb_storage >/dev/null 2>&1

    # give the system time to settle and open the USB devices
    sleep 7

    # Are there any SCSI block devices?
    ls -d /sys/block/sd* >/dev/null 2>&1

    if [ $SBD -eq $TRUE ]; then
        mkdir -p $MD
        dbg TEXT "Trying to get key-file '$KEYFILE' ..."
        #for SFS in /sys/block/sd*/sd??; do
        for SFS in /sys/block/sd*/sd??; do
            dbg TEXT "Examining $SFS" -n
            # is it a USB device?
            ls -l ${SFS}/../device | busybox grep 'usb' >/dev/null 2>&1
            dbg TEXT ", USB=$USB" -n
            # Is the device removable?
            REMOVABLE=0`cat ${SFS}/../removable`
            dbg TEXT ", REMOVABLE=$REMOVABLE" -n
            if [ $USB -eq $TRUE -a $REMOVABLE -eq 1 -a -f $SFS/dev ]; then
                dbg TEXT ", *possible key device*" -n
                DEV=`busybox basename $SFS`
                # Check if key device itself is encrypted
                /sbin/cryptsetup isLuks /dev/${DEV} >/dev/null 2>&1
                # Open crypted partition and prepare for mount
                if [ $ENCRYPTED -eq $TRUE ]; then
                    dbg TEXT ", encrypted device" -n
                    # Use blkid to determine label
                    LABEL=$(/sbin/blkid -s LABEL -o value /dev/${DEV})
                    dbg TEXT ", label $LABEL" -n
                    while [ $TRIES -gt 0 -a $DECRYPTED -ne $TRUE ]; do
                        PASS="`readpass \"Enter LUKS password for key device ${DEV} (${LABEL}) (or empty to skip): \"`"
                        if [ -z "$PASS" ]; then
                            dbg TEXT ", device skipped" -n
                        echo $PASS | /sbin/cryptsetup luksOpen /dev/${DEV} bootkey >/dev/null 2>&1
                    # If open failed, skip this device
                    if [ $DECRYPTED -ne $TRUE ]; then
                        dbg TEXT "decrypting device failed" -n
                    # Decrypted device to use
                dbg TEXT ", device $DEV" -n
                # Use blkid to determine label
                LABEL=$(/sbin/blkid -s LABEL -o value /dev/${DEV})
                dbg TEXT ", label $LABEL" -n
                # Use blkid to determine fstype
                FSTYPE=$(/sbin/blkid -s TYPE -o value /dev/${DEV})
                dbg TEXT ", fstype $FSTYPE" -n
                # Is the file-system driver loaded?
                cat /proc/modules | busybox grep $FSTYPE >/dev/null 2>&1
                if [ $FSLOAD -gt 0 ]; then
                    dbg TEXT ", loading driver for $FSTYPE" -n
                    # load the correct file-system driver
                    modprobe $FSTYPE >/dev/null 2>&1
                dbg TEXT ", mounting /dev/$DEV on $MD" -n
                mount /dev/${DEV} $MD -t $FSTYPE -o ro >/dev/null 2>&1
                dbg TEXT ", (`mount | busybox grep $DEV`)" -n
                dbg TEXT ", checking fot $MD/$KEYFILE" -n
                if [ -f $MD/$KEYFILE ]; then
                    dbg TEXT ", found $MD/$KEYFILE" -n
                    cat $MD/$KEYFILE
                dbg TEXT ", umount $MD" -n
                umount $MD >/dev/null 2>&1
                # Close encrypted key device
                if [ $ENCRYPTED -eq $TRUE -a $DECRYPTED -eq $TRUE ]; then
                    dbg TEXT ", closing encrypted device" -n
                    /sbin/cryptsetup luksClose bootkey >/dev/null 2>&1
                dbg TEXT ", done\n\n" -n
                if [ $OPENED -eq $TRUE ]; then
                dbg TEXT ", device `busybox basename $SFS` ignored" -n
            dbg CLEAR ""

# clear existing usplash text and status messages
[ $USPLASH -eq $TRUE ] && msg STATUS "                               " && msg CLEAR ""

if [ $OPENED -ne $TRUE ]; then
    msg TEXT "FAILED to find suitable USB key-file ..."
    readpass "Try to enter the LUKS password: "
    msg TEXT "Success loading key-file from $SFS ($LABEL)"

[ $USPLASH -eq $TRUE ] && /sbin/usplash_write "VERBOSE default"