Debian Codegraph @

src/luks/clevis-luks-regen

raw
1 #!/bin/bash -e
2 # vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
3 #
4 # Copyright (c) 2020 Red Hat, Inc.
5 # Author: Radovan Sroka <[email protected]>
6 # Author: Sergio Correia <[email protected]>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #
21
22 . clevis-luks-common-functions
23
24 SUMMARY="Regenerate clevis binding"
25
26 if [ "${1}" = "--summary" ]; then
27 echo "${SUMMARY}"
28 exit 0
29 fi
30
31 usage_and_exit () {
32 exec >&2
33 echo "Usage: clevis luks regen [-q] -d DEV -s SLOT"
34 echo
35 echo "${SUMMARY}"
36 echo
37 echo " -d DEV The LUKS device on which to perform rebinding"
38 echo
39 echo " -s SLT The LUKS slot to use"
40 echo
41 echo " -q Do not prompt for confirmation"
42 echo
43 exit "${1}"
44 }
45
46 QOPT=
47 while getopts ":hqd:s:" o; do
48 case "${o}" in
49 d) DEV="${OPTARG}";;
50 h) usage_and_exit 0;;
51 s) SLT="${OPTARG}";;
52 q) QOPT="-q";;
53 *) usage_and_exit 1;;
54 esac
55 done
56
57 if [ -z "${DEV}" ]; then
58 echo "Did not specify a device!" >&2
59 exit 1
60 fi
61
62 if [ -z "${SLT}" ]; then
63 echo "Did not specify a slot!" >&2
64 exit 1
65 fi
66
67 # Get pin and configuration.
68 if ! pin_cfg="$(clevis luks list -d "${DEV}" -s "${SLT}")" \
69 || [ -z "${pin_cfg}" ]; then
70 exit 1
71 fi
72
73 pin="$(echo "${pin_cfg}" | cut -d' ' -f2)"
74 cfg="$(echo "${pin_cfg}" | cut -d' ' -f3 | sed -e "s/'//g")"
75 if [ -z "${pin}" ] || [ -z "${cfg}" ]; then
76 echo "Invalid pin or configuration" >&2
77 exit 1
78 fi
79
80 echo "Regenerating binding (device ${DEV}, slot ${SLT}):"
81 echo "Pin: ${pin}, Config: '${cfg}'"
82
83 if [ -z "${QOPT}" ]; then
84 read -r -p "Do you want to proceed? [ynYN] " ans
85 [ "${ans}" != "y" ] && [ "${ans}" != "Y" ] && exit 0
86 fi
87
88 if ! clevis_luks_do_bind "${DEV}" "${SLT}" "" "${pin}" "${cfg}" \
89 "-y" "overwrite"; then
90 echo "Unable to regenerate binding in ${DEV}:${SLT}" >&2
91 exit 1
92 fi
93 echo "Binding regenerated successfully" >&2