#!/bin/bash -e
# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
#
# Copyright (c) 2017 Red Hat, Inc.
# Author: Javier Martinez Canillas <javierm@redhat.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

SUMMARY="Unbinds a pin bound to a LUKS volume"
UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e

# We require cryptsetup >= 2.0.4 to fully support LUKSv2.
# Support is determined at build time.
function luks2_supported() {
    return @OLD_CRYPTSETUP@
}

function usage() {
    exec >&2
    echo
    echo "Usage: clevis luks unbind -d DEV -s SLT"
    echo
    echo "$SUMMARY":
    echo
    echo "  -d DEV  The bound LUKS device"
    echo
    echo "  -s SLOT The LUKS slot number for the pin unbind"
    echo
    echo "  -f      Do not ask for confirmation and wipe slot in batch-mode"
    echo
    exit 2
}

if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
    echo "$SUMMARY"
    exit 0
fi

FRC=()
while getopts ":d:s:f" o; do
    case "$o" in
    f) FRC+=(-q);;
    d) DEV="$OPTARG";;
    s) SLT="$OPTARG";;
    *) usage;;
    esac
done

if [ -z "$DEV" ]; then
    echo "Did not specify a device!" >&2
    usage
fi

if [ -z "$SLT" ]; then
    echo "Did not specify a slot!" >&2
    usage
fi

if ! cryptsetup isLuks "$DEV"; then
    echo "$DEV is not a LUKS device!" >&2
    exit 1
fi

if luks2_supported; then
    if cryptsetup isLuks --type luks1 "$DEV"; then
        luks_type="luks1"
    elif cryptsetup isLuks --type luks2 "$DEV";then
        luks_type="luks2"
    else
        echo "$DEV is not a supported LUKS device!" >&2
        exit 1
    fi
else
    luks_type="luks1"
fi

if [ "$luks_type" == "luks1" ]; then
    if ! luksmeta test -d "$DEV" 2>/dev/null; then
        echo "The $DEV device does not contain a LUKSMeta header!" >&2
        exit 1
    fi

    read -r slot state uuid < <(luksmeta show -d "$DEV" | grep "^$SLT *")

    if [ "$uuid" == "empty" ]; then
        echo "The LUKSMeta slot $SLT on device $DEV is already empty." >&2
        exit 1
    fi

    [ "$state" == "active" ] && KILL=true

elif [ "$luks_type" == "luks2" ]; then
    dump="$(cryptsetup luksDump "$DEV")"
    grep -q "^\s*$SLT: luks2" <<< "$dump" && KILL=true
    TOK="$(grep -E -B1 "^\s+Keyslot:\s+$SLT$" <<< "$dump" \
        | sed -rn 's|^\s+([0-9]+): clevis|\1|p')"
fi

if [ -z "${FRC[*]}" ]; then
    echo "The unbind operation will wipe a slot. This operation is unrecoverable." >&2
    read -r -p "Do you wish to erase LUKS slot $SLT on $DEV? [ynYN] " ans < /dev/tty
    [[ "$ans" =~ ^[yY]$ ]] || exit 0
fi

if [ -n "$KILL" ]; then
    if ! cryptsetup luksKillSlot "$DEV" "$SLT" "${FRC[@]}"; then
        echo "LUKS slot $SLT for device $DEV couldn't be deleted"
        exit 1
    fi
fi

if [ "$luks_type" == "luks1" ]; then
    if ! luksmeta wipe -f -d "$DEV" -u "$UUID" -s "$SLT"; then
        echo "LUKSMeta slot $SLT for device $DEV couldn't be deleted"
        exit 1
    fi
elif [ "$luks_type" == "luks2" ] && [ -n "$TOK" ]; then
    if ! cryptsetup token remove --token-id "$TOK" "$DEV"; then
        echo "Error while removing token $TOK from LUKS device $DEV!" >&2
        exit 1
    fi
fi
