| 1 |
#!/bin/bash -e |
| 2 |
# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: |
| 3 |
# |
| 4 |
# Copyright (c) 2016 Red Hat, Inc. |
| 5 |
# Author: Nathaniel McCallum <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
. clevis-luks-common-functions |
| 21 |
|
| 22 |
SUMMARY="Unlocks a LUKS volume" |
| 23 |
|
| 24 |
function usage() { |
| 25 |
exec >&2 |
| 26 |
echo |
| 27 |
echo "Usage: clevis luks unlock -d DEV [-n NAME] [-t SLT]" |
| 28 |
echo |
| 29 |
echo "$SUMMARY": |
| 30 |
echo |
| 31 |
echo " -d DEV The LUKS device on which to perform unlocking" |
| 32 |
echo |
| 33 |
echo " -n NAME The name of the unlocked device node" |
| 34 |
echo |
| 35 |
echo " -t SLT Test the passphrase for the given slot without unlocking" |
| 36 |
echo " the device" |
| 37 |
echo |
| 38 |
echo " -o OPTS Pass options to underlying 'cryptsetup open'; be sure" |
| 39 |
echo " to quote the OPTS you pass, if they contain a space," |
| 40 |
echo " etc." |
| 41 |
echo |
| 42 |
exit 2 |
| 43 |
} |
| 44 |
|
| 45 |
if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then |
| 46 |
echo "$SUMMARY" |
| 47 |
exit 0 |
| 48 |
fi |
| 49 |
|
| 50 |
while getopts ":d:n:t:o:" o; do |
| 51 |
case "$o" in |
| 52 |
d) DEV="$OPTARG";; |
| 53 |
n) NAME="$OPTARG";; |
| 54 |
t) SLT="$OPTARG";; |
| 55 |
o) OPENARGS="$OPTARG";; |
| 56 |
*) usage;; |
| 57 |
esac |
| 58 |
done |
| 59 |
|
| 60 |
if [ -z "$DEV" ]; then |
| 61 |
echo "Did not specify a device!" >&2 |
| 62 |
usage |
| 63 |
fi |
| 64 |
|
| 65 |
if ! cryptsetup isLuks "$DEV"; then |
| 66 |
echo "$DEV is not a LUKS device!" >&2 |
| 67 |
exit 1 |
| 68 |
fi |
| 69 |
|
| 70 |
NAME="${NAME:-luks-"$(cryptsetup luksUUID "$DEV")"}" |
| 71 |
|
| 72 |
if [ -n "$SLT" ]; then |
| 73 |
if ! clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}" >/dev/null; then |
| 74 |
echo "Test for token slot ${SLT} on device ${DEV} failed." >&2 |
| 75 |
exit 1 |
| 76 |
fi |
| 77 |
else |
| 78 |
if ! pt=$(clevis_luks_unlock_device "${DEV}"); then |
| 79 |
echo "${DEV} could not be opened." >&2 |
| 80 |
exit 1 |
| 81 |
fi |
| 82 |
|
| 83 |
echo -n "${pt}" | cryptsetup ${OPENARGS} open -d- "${DEV}" "${NAME}" |
| 84 |
fi |