Blame SOURCES/rear-bz1832394.patch

52df35
diff --git a/doc/user-guide/06-layout-configuration.adoc b/doc/user-guide/06-layout-configuration.adoc
52df35
index f59384db..88ba0420 100644
52df35
--- a/doc/user-guide/06-layout-configuration.adoc
52df35
+++ b/doc/user-guide/06-layout-configuration.adoc
52df35
@@ -630,7 +630,7 @@ lvmvol <volume_group> <name> <size(bytes)> <layout> [key:value ...]
52df35
 
52df35
 === LUKS Devices ===
52df35
 ----------------------------------
52df35
-crypt /dev/mapper/<name> <device> [cipher=<cipher>] [key_size=<key size>] [hash=<hash function>] [uuid=<uuid>] [keyfile=<keyfile>] [password=<password>]
52df35
+crypt /dev/mapper/<name> <device> [type=<type>] [cipher=<cipher>] [key_size=<key size>] [hash=<hash function>] [uuid=<uuid>] [keyfile=<keyfile>] [password=<password>]
52df35
 ----------------------------------
52df35
 
52df35
 === DRBD ===
52df35
diff --git a/usr/share/rear/layout/prepare/GNU/Linux/160_include_luks_code.sh b/usr/share/rear/layout/prepare/GNU/Linux/160_include_luks_code.sh
52df35
index 05279bc8..0c662f67 100644
52df35
--- a/usr/share/rear/layout/prepare/GNU/Linux/160_include_luks_code.sh
52df35
+++ b/usr/share/rear/layout/prepare/GNU/Linux/160_include_luks_code.sh
52df35
@@ -1,35 +1,75 @@
52df35
+
52df35
 # Code to recreate and/or open LUKS volumes.
16375c
 
16375c
 create_crypt() {
16375c
+    # See the create_device() function in lib/layout-functions.sh what "device type" means:
16375c
+    local device_type="$1"
16375c
+    if ! grep -q "^crypt $device_type " "$LAYOUT_FILE" ; then
16375c
+        LogPrintError "Skip recreating LUKS volume $device_type (no 'crypt $device_type' entry in $LAYOUT_FILE)"
16375c
+        # FIXME: The return code is ignored in the create_device() function in lib/layout-functions.sh:
16375c
+        return 1
16375c
+    fi
16375c
+    
16375c
     local crypt target_device source_device options
16375c
-    read crypt target_device source_device options < <(grep "^crypt $1 " "$LAYOUT_FILE")
16375c
+    local mapping_name option key value
16375c
+    local cryptsetup_options="" keyfile="" password=""
16375c
 
16375c
-    local target_name=${target_device#/dev/mapper/}
16375c
+    read crypt target_device source_device options < <( grep "^crypt $device_type " "$LAYOUT_FILE" )
16375c
+
16375c
+    # Careful! One cannot 'test -b $source_device' here at the time when this code is run
16375c
+    # because the source device is usually a disk partition block device like /dev/sda2
16375c
+    # but disk partition block devices usually do not yet exist (in particular not on a new clean disk)
16375c
+    # because partitions are actually created later when the diskrestore.sh script is run
16375c
+    # but not here when this code is run which only generates the diskrestore.sh script:
16375c
+    if ! test $source_device ; then
16375c
+        LogPrintError "Skip recreating LUKS volume $device_type: No source device (see the 'crypt $device_type' entry in $LAYOUT_FILE)"
16375c
+        # FIXME: The return code is ignored in the create_device() function in lib/layout-functions.sh:
16375c
+        return 1
16375c
+    fi
16375c
+
16375c
+    mapping_name=${target_device#/dev/mapper/}
16375c
+    if ! test $mapping_name ; then
16375c
+        LogPrintError "Skip recreating LUKS volume $device_type on $source_device: No /dev/mapper/... mapping name (see the 'crypt $device_type' entry in $LAYOUT_FILE)"
16375c
+        # FIXME: The return code is ignored in the create_device() function in lib/layout-functions.sh:
16375c
+        return 1
16375c
+    fi
16375c
 
16375c
-    local cryptsetup_options="" keyfile="" password=""
16375c
-    local option key value
16375c
     for option in $options ; do
16375c
-        key=${option%=*}
16375c
+        # $option is of the form keyword=value and
16375c
+        # we assume keyword has no '=' character but value could be anything that may have a '=' character
16375c
+        # so we split keyword=value at the leftmost '=' character so that
16375c
+        # e.g. keyword=foo=bar gets split into key="keyword" and value="foo=bar":
16375c
+        key=${option%%=*}
16375c
         value=${option#*=}
16375c
-
16375c
+        # The "cryptseup luksFormat" command does not require any of the type, cipher, key-size, hash, uuid option values
16375c
+        # because if omitted a cryptseup default value is used so we treat those values as optional.
16375c
+        # Using plain test to ensure the value is a single non empty and non blank word
16375c
+        # without quoting because test " " would return zero exit code
16375c
+        # cf. "Beware of the emptiness" in https://github.com/rear/rear/wiki/Coding-Style
16375c
         case "$key" in
16375c
-            cipher)
16375c
-                cryptsetup_options+=" --cipher $value"
16375c
+            (type)
16375c
+                test $value && cryptsetup_options+=" --type $value"
16375c
+                ;;
16375c
+            (cipher)
16375c
+                test $value && cryptsetup_options+=" --cipher $value"
52df35
+                ;;
52df35
+            (key_size)
52df35
+                test $value && cryptsetup_options+=" --key-size $value"
16375c
                 ;;
16375c
-            key_size)
16375c
-                cryptsetup_options+=" --key-size $value"
52df35
+            (hash)
52df35
+                test $value && cryptsetup_options+=" --hash $value"
16375c
                 ;;
16375c
-            hash)
16375c
-                cryptsetup_options+=" --hash $value"
52df35
+            (uuid)
52df35
+                test $value && cryptsetup_options+=" --uuid $value"
16375c
                 ;;
16375c
-            uuid)
16375c
-                cryptsetup_options+=" --uuid $value"
52df35
+            (keyfile)
52df35
+                test $value && keyfile=$value
16375c
                 ;;
16375c
-            keyfile)
16375c
-                keyfile=$value
52df35
+            (password)
52df35
+                test $value && password=$value
16375c
                 ;;
16375c
-            password)
16375c
-                password=$value
16375c
+            (*)
16375c
+                LogPrintError "Skipping unsupported LUKS cryptsetup option '$key' in 'crypt $target_device $source_device' entry in $LAYOUT_FILE"
16375c
                 ;;
16375c
         esac
16375c
     done
52df35
@@ -37,26 +77,25 @@ create_crypt() {
16375c
     cryptsetup_options+=" $LUKS_CRYPTSETUP_OPTIONS"
16375c
 
16375c
     (
16375c
-    echo "Log \"Creating LUKS device $target_name on $source_device\""
16375c
+    echo "LogPrint \"Creating LUKS volume $mapping_name on $source_device\""
16375c
     if [ -n "$keyfile" ] ; then
16375c
         # Assign a temporary keyfile at this stage so that original keyfiles do not leak onto the rescue medium.
16375c
         # The original keyfile will be restored from the backup and then re-assigned to the LUKS device in the
16375c
         # 'finalize' stage.
16375c
         # The scheme for generating a temporary keyfile path must be the same here and in the 'finalize' stage.
16375c
-        keyfile="${TMPDIR:-/tmp}/LUKS-keyfile-$target_name"
16375c
+        keyfile="$TMP_DIR/LUKS-keyfile-$mapping_name"
16375c
         dd bs=512 count=4 if=/dev/urandom of="$keyfile"
16375c
         chmod u=rw,go=- "$keyfile"
16375c
-
16375c
         echo "cryptsetup luksFormat --batch-mode $cryptsetup_options $source_device $keyfile"
16375c
-        echo "cryptsetup luksOpen --key-file $keyfile $source_device $target_name"
16375c
+        echo "cryptsetup luksOpen --key-file $keyfile $source_device $mapping_name"
16375c
     elif [ -n "$password" ] ; then
16375c
         echo "echo \"$password\" | cryptsetup luksFormat --batch-mode $cryptsetup_options $source_device"
16375c
-        echo "echo \"$password\" | cryptsetup luksOpen $source_device $target_name"
16375c
+        echo "echo \"$password\" | cryptsetup luksOpen $source_device $mapping_name"
16375c
     else
16375c
-        echo "LogPrint \"Please enter the password for LUKS device $target_name ($source_device):\""
16375c
+        echo "LogUserOutput \"Set the password for LUKS volume $mapping_name (for 'cryptsetup luksFormat' on $source_device):\""
16375c
         echo "cryptsetup luksFormat --batch-mode $cryptsetup_options $source_device"
16375c
-        echo "LogPrint \"Please re-enter the password for LUKS device $target_name ($source_device):\""
16375c
-        echo "cryptsetup luksOpen $source_device $target_name"
16375c
+        echo "LogUserOutput \"Enter the password for LUKS volume $mapping_name (for 'cryptsetup luksOpen' on $source_device):\""
16375c
+        echo "cryptsetup luksOpen $source_device $mapping_name"
16375c
     fi
16375c
     echo ""
16375c
     ) >> "$LAYOUT_CODE"
52df35
@@ -64,38 +103,61 @@ create_crypt() {
52df35
 
52df35
 # Function open_crypt() is meant to be used by the 'mountonly' workflow
52df35
 open_crypt() {
52df35
+    # See the do_mount_device() function in lib/layout-functions.sh what "device type" means:
52df35
+    local device_type="$1"
52df35
+    if ! grep -q "^crypt $device_type " "$LAYOUT_FILE" ; then
52df35
+        LogPrintError "Skip opening LUKS volume $device_type (no 'crypt $device_type' entry in $LAYOUT_FILE)"
52df35
+        # FIXME: The return code is ignored in the do_mount_device() function in lib/layout-functions.sh:
52df35
+        return 1
52df35
+    fi
52df35
+
52df35
     local crypt target_device source_device options
52df35
-    read crypt target_device source_device options < <(grep "^crypt $1 " "$LAYOUT_FILE")
52df35
+    local mapping_name option key value
52df35
+    local cryptsetup_options="" keyfile="" password=""
52df35
 
52df35
-    local target_name=${target_device#/dev/mapper/}
52df35
+    read crypt target_device source_device options < <( grep "^crypt $device_type " "$LAYOUT_FILE" )
52df35
+
52df35
+    if ! test -b "$source_device" ; then
52df35
+        LogPrintError "Skip opening LUKS volume $device_type on device '$source_device' that is no block device (see the 'crypt $device_type' entry in $LAYOUT_FILE)"
52df35
+        # FIXME: The return code is ignored in the do_mount_device() function in lib/layout-functions.sh:
52df35
+        return 1
52df35
+    fi
52df35
+
52df35
+    mapping_name=${target_device#/dev/mapper/}
52df35
+    if ! test $mapping_name ; then
52df35
+        LogPrintError "Skip opening LUKS volume $device_type on $source_device: No /dev/mapper/... mapping name (see the 'crypt $device_type' entry in $LAYOUT_FILE)"
52df35
+        # FIXME: The return code is ignored in the do_mount_device() function in lib/layout-functions.sh:
52df35
+        return 1
52df35
+    fi
52df35
 
52df35
-    local cryptsetup_options="" keyfile="" password=""
52df35
-    local option key value
52df35
     for option in $options ; do
52df35
-        key=${option%=*}
52df35
+        # $option is of the form keyword=value and
52df35
+        # we assume keyword has no '=' character but value could be anything that may have a '=' character
52df35
+        # so we split keyword=value at the leftmost '=' character so that
52df35
+        # e.g. keyword=foo=bar gets split into key="keyword" and value="foo=bar":
52df35
+        key=${option%%=*}
52df35
         value=${option#*=}
52df35
-
52df35
         case "$key" in
52df35
-            keyfile)
52df35
-                keyfile=$value
52df35
+            (keyfile)
52df35
+                test $value && keyfile=$value
52df35
                 ;;
52df35
-            password)
52df35
-                password=$value
52df35
+            (password)
52df35
+                test $value && password=$value
52df35
                 ;;
52df35
         esac
52df35
     done
52df35
 
52df35
     (
52df35
-    echo "Log \"Opening LUKS device $target_name on $source_device\""
52df35
+    echo "LogPrint \"Opening LUKS volume $mapping_name on $source_device\""
52df35
     if [ -n "$keyfile" ] ; then
52df35
         # During a 'mountonly' workflow, the original keyfile is supposed to be
52df35
         # available at this point.
52df35
-        echo "cryptsetup luksOpen --key-file $keyfile $source_device $target_name"
52df35
+        echo "cryptsetup luksOpen --key-file $keyfile $source_device $mapping_name"
52df35
     elif [ -n "$password" ] ; then
52df35
-        echo "echo \"$password\" | cryptsetup luksOpen $source_device $target_name"
52df35
+        echo "echo \"$password\" | cryptsetup luksOpen $source_device $mapping_name"
52df35
     else
52df35
-        echo "LogPrint \"Please enter the password for LUKS device $target_name ($source_device):\""
52df35
-        echo "cryptsetup luksOpen $source_device $target_name"
52df35
+        echo "LogUserOutput \"Enter the password for LUKS volume $mapping_name (for 'cryptsetup luksOpen' on $source_device):\""
52df35
+        echo "cryptsetup luksOpen $source_device $mapping_name"
52df35
     fi
52df35
     echo ""
52df35
     ) >> "$LAYOUT_CODE"
52df35
diff --git a/usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh b/usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh
52df35
index c1e1cfd5..afeabf6a 100644
52df35
--- a/usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh
52df35
+++ b/usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh
16375c
@@ -9,6 +9,8 @@ Log "Saving Encrypted volumes."
52df35
 REQUIRED_PROGS+=( cryptsetup dmsetup )
52df35
 COPY_AS_IS+=( /usr/share/cracklib/\* /etc/security/pwquality.conf )
16375c
 
16375c
+local invalid_cryptsetup_option_value="no"
16375c
+
16375c
 while read target_name junk ; do
16375c
     # find the target device we're mapping
16375c
     if ! [ -e /dev/mapper/$target_name ] ; then
52df35
@@ -30,29 +32,96 @@ while read target_name junk ; do
16375c
         source_device="$(get_device_name ${slave##*/})"
16375c
     done
16375c
 
16375c
-    if ! cryptsetup isLuks $source_device >/dev/null 2>&1; then
16375c
+    if ! blkid -p -o export $source_device >$TMP_DIR/blkid.output ; then
16375c
+        LogPrintError "Error: Cannot get attributes for $target_name ('blkid -p -o export $source_device' failed)"
52df35
         continue
52df35
     fi
52df35
 
52df35
-    # gather crypt information
52df35
-    cipher=$(cryptsetup luksDump $source_device | grep "Cipher name" | sed -r 's/^.+:\s*(.+)$/\1/')
52df35
-    mode=$(cryptsetup luksDump $source_device | grep "Cipher mode" | cut -d: -f2- | awk '{printf("%s",$1)};')
52df35
-    key_size=$(cryptsetup luksDump $source_device | grep "MK bits" | sed -r 's/^.+:\s*(.+)$/\1/')
52df35
-    hash=$(cryptsetup luksDump $source_device | grep "Hash spec" | sed -r 's/^.+:\s*(.+)$/\1/')
52df35
-    uuid=$(cryptsetup luksDump $source_device | grep "UUID" | sed -r 's/^.+:\s*(.+)$/\1/')
52df35
-    keyfile_option=$([ -f /etc/crypttab ] && awk '$1 == "'"$target_name"'" && $3 != "none" && $3 != "-" && $3 != "" { print "keyfile=" $3; }' /etc/crypttab)
16375c
+    if ! grep -q "TYPE=crypto_LUKS" $TMP_DIR/blkid.output ; then
16375c
+        Log "Skipping $target_name (no 'TYPE=crypto_LUKS' in 'blkid -p -o export $source_device' output)"
16375c
+        continue
16375c
+    fi
52df35
 
52df35
-    # LUKS version 2 is not yet suppported, see https://github.com/rear/rear/issues/2204
52df35
-    # When LUKS version 2 is used the above code fails at least to determine the hash value
52df35
-    # so we use an empty hash value as a simple test if gathering crypt information was successful:
52df35
-    test "$hash" || Error "No hash value for LUKS device '$target_name' at '$source_device' (only LUKS version 1 is supported)"
16375c
+    # Detect LUKS version:
16375c
+    # Remove all non-digits in particular to avoid leading or trailing spaces in the version string
16375c
+    # cf. "Beware of the emptiness" in https://github.com/rear/rear/wiki/Coding-Style
16375c
+    # that could happen if the blkid output contains "VERSION = 2" so that 'cut -d= -f2' results " 2".
16375c
+    version=$( grep "VERSION" $TMP_DIR/blkid.output | cut -d= -f2 | tr -c -d '[:digit:]' )
16375c
+    if ! test "$version" = "1" -o "$version" = "2" ; then
16375c
+        LogPrintError "Error: Unsupported LUKS version for $target_name ('blkid -p -o export $source_device' shows 'VERSION=$version')"
16375c
+        continue
16375c
+    fi
16375c
+    luks_type=luks$version
52df35
 
52df35
-    echo "crypt /dev/mapper/$target_name $source_device cipher=$cipher-$mode key_size=$key_size hash=$hash uuid=$uuid $keyfile_option" >> $DISKLAYOUT_FILE
52df35
-done < <( dmsetup ls --target crypt )
16375c
+    # Gather crypt information:
16375c
+    if ! cryptsetup luksDump $source_device >$TMP_DIR/cryptsetup.luksDump ; then
16375c
+        LogPrintError "Error: Cannot get LUKS$version values for $target_name ('cryptsetup luksDump $source_device' failed)"
52df35
+        continue
52df35
+    fi
16375c
+    uuid=$( grep "UUID" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
16375c
+    keyfile_option=$( [ -f /etc/crypttab ] && awk '$1 == "'"$target_name"'" && $3 != "none" && $3 != "-" && $3 != "" { print "keyfile=" $3; }' /etc/crypttab )
16375c
+    if test $luks_type = "luks1" ; then
16375c
+        cipher_name=$( grep "Cipher name" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
16375c
+        cipher_mode=$( grep "Cipher mode" $TMP_DIR/cryptsetup.luksDump | cut -d: -f2- | awk '{printf("%s",$1)};' )
16375c
+        cipher=$cipher_name-$cipher_mode
16375c
+        key_size=$( grep "MK bits" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
16375c
+        hash=$( grep "Hash spec" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
16375c
+    elif test $luks_type = "luks2" ; then
16375c
+        cipher=$( grep "cipher:" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
16375c
+        # More than one keyslot may be defined - use key_size from the first slot.
16375c
+        # Depending on the version the "cryptsetup luksDump" command outputs the key_size value
16375c
+        # as a line like
16375c
+        #         Key:        512 bits
16375c
+        # and/or as a line like
16375c
+        #         Cipher key: 512 bits
16375c
+        # cf. https://github.com/rear/rear/pull/2504#issuecomment-718729198 and subsequent comments
16375c
+        # so we grep for both lines but use only the first match from the first slot:
16375c
+        key_size=$( egrep -m 1 "Key:|Cipher key:" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+) bits$/\1/' )
16375c
+        hash=$( grep "Hash" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
16375c
+    fi
16375c
 
52df35
-# cryptsetup is required in the recovery system if disklayout.conf contains at least one 'crypt' entry
52df35
-# see the create_crypt function in layout/prepare/GNU/Linux/160_include_luks_code.sh
52df35
-# what program calls are written to diskrestore.sh
52df35
-# cf. https://github.com/rear/rear/issues/1963
52df35
-grep -q '^crypt ' $DISKLAYOUT_FILE && REQUIRED_PROGS+=( cryptsetup ) || true
16375c
+    # Basic checks that the cipher key_size hash uuid values exist
16375c
+    # cf. https://github.com/rear/rear/pull/2504#issuecomment-718729198
16375c
+    # because some values are needed during "rear recover"
16375c
+    # to set cryptsetup options in layout/prepare/GNU/Linux/160_include_luks_code.sh
16375c
+    # and it seems cryptsetup fails when options with empty values are specified
16375c
+    # cf. https://github.com/rear/rear/pull/2504#issuecomment-719479724
16375c
+    # For example a LUKS1 crypt entry in disklayout.conf looks like
16375c
+    # crypt /dev/mapper/luks1test /dev/sda7 type=luks1 cipher=aes-xts-plain64 key_size=256 hash=sha256 uuid=1b4198c9-d9b0-4c57-b9a3-3433e391e706 
16375c
+    # and a LUKS1 crypt entry in disklayout.conf looks like
16375c
+    # crypt /dev/mapper/luks2test /dev/sda8 type=luks2 cipher=aes-xts-plain64 key_size=256 hash=sha256 uuid=3e874a28-7415-4f8c-9757-b3f28a96c4d2 
16375c
+    # Only the keyfile_option value is optional and the luks_type value is already tested above.
16375c
+    # Using plain test to ensure a value is a single non empty and non blank word
16375c
+    # without quoting because test " " would return zero exit code
16375c
+    # cf. "Beware of the emptiness" in https://github.com/rear/rear/wiki/Coding-Style
16375c
+    # Do not error out instantly here but only report errors here so the user can see all messages
16375c
+    # and actually error out at the end of this script if there was one actually invalid value:
16375c
+    if ! test $cipher ; then
16375c
+        LogPrint "No 'cipher' value for LUKS$version volume $target_name in $source_device"
16375c
+    fi
16375c
+    if test $key_size ; then
16375c
+        if ! is_positive_integer $key_size ; then
16375c
+            LogPrintError "Error: 'key_size=$key_size' is no positive integer for LUKS$version volume $target_name in $source_device"
16375c
+            invalid_cryptsetup_option_value="yes"
16375c
+        fi
16375c
+    else
16375c
+        LogPrint "No 'key_size' value for LUKS$version volume $target_name in $source_device"
16375c
+    fi
16375c
+    if ! test $hash ; then
16375c
+        LogPrint "No 'hash' value for LUKS$version volume $target_name in $source_device"
16375c
+    fi
16375c
+    if ! test $uuid ; then
16375c
+        # Report a missig uuid value as an error to have the user informed
16375c
+        # but do not error out here because things can be fixed manually during "rear recover"
16375c
+        # cf. https://github.com/rear/rear/pull/2506#issuecomment-721757810
16375c
+        # and https://github.com/rear/rear/pull/2506#issuecomment-722315498
16375c
+        # and https://github.com/rear/rear/issues/2509
16375c
+        LogPrintError "Error: No 'uuid' value for LUKS$version volume $target_name in $source_device (mounting it or booting the recreated system may fail)"
16375c
+    fi
16375c
+
16375c
+    echo "crypt /dev/mapper/$target_name $source_device type=$luks_type cipher=$cipher key_size=$key_size hash=$hash uuid=$uuid $keyfile_option" >> $DISKLAYOUT_FILE
16375c
+
52df35
+done < <( dmsetup ls --target crypt )
52df35
 
16375c
+# Let this script return successfully when invalid_cryptsetup_option_value is not true:
16375c
+is_true $invalid_cryptsetup_option_value && Error "Invalid or empty LUKS cryptsetup option value(s) in $DISKLAYOUT_FILE" || true