| 1 | # |
| 2 | # opaladmin-workflow.sh |
| 3 | # |
| 4 | |
| 5 | WORKFLOW_opaladmin_DESCRIPTION="administrate TCG Opal 2-compliant self-encrypting disks" |
| 6 | WORKFLOWS+=( opaladmin ) |
| 7 | |
| 8 | function opaladmin_usage_error() { |
| 9 | # prints usage information, then exits. |
| 10 | |
| 11 | Error "Use '$PROGRAM opaladmin help' for more information." |
| 12 | } |
| 13 | |
| 14 | function opaladmin_help() { |
| 15 | # prints a help message. |
| 16 | |
| 17 | LogPrintError "Usage: $PROGRAM opaladmin ACTION [-- OPTIONS] [DEVICE ...]" |
| 18 | LogPrintError " Administrate TCG Opal 2-compliant self-encrypting disks" |
| 19 | LogPrintError "" |
| 20 | LogPrintError "Options:" |
| 21 | LogPrintError " -I FILE, --image=FILE use FILE as the PBA image (default: auto-detect)" |
| 22 | LogPrintError "(Unfortunately, options must be prefixed by '--'.)" |
| 23 | LogPrintError "" |
| 24 | LogPrintError "Actions:" |
| 25 | LogPrintError " info print locking information" |
| 26 | LogPrintError " setupERASE enable locking and assign a disk password, ERASING ALL DATA ON THE DISK(S)" |
| 27 | LogPrintError " (requires one or more DEVICE arguments, or 'ALL' for all available disks)" |
| 28 | LogPrintError " changePW change the disk password" |
| 29 | LogPrintError " uploadPBA upload the PBA image to boot disk(s) (whose shadow MBR is enabled)" |
| 30 | LogPrintError " unlock unlock disk(s)" |
| 31 | LogPrintError " deactivate permanently deactivate the locking mechanism on disk(s)" |
| 32 | LogPrintError " reactivate permanently reactivate the locking mechanism on disk(s)" |
| 33 | LogPrintError " resetDEK assign a new data encryption key, ERASING ALL DATA ON THE DISK(S)" |
| 34 | LogPrintError " (requires one or more DEVICE arguments, or 'ALL' for all available disks)" |
| 35 | LogPrintError " factoryRESET reset disk(s) to factory defaults, ERASING ALL DATA ON THE DISK(S)" |
| 36 | LogPrintError " (requires one or more DEVICE arguments, or 'ALL' for all available disks)" |
| 37 | LogPrintError " help print this help message and exit" |
| 38 | LogPrintError "" |
| 39 | LogPrintError "If multiple Opal 2-compliant disks are available and no DEVICE argument is present," |
| 40 | LogPrintError "actions are performed on all available disks." |
| 41 | } |
| 42 | |
| 43 | function WORKFLOW_opaladmin() { |
| 44 | # ReaR command 'opaladmin' |
| 45 | |
| 46 | # Do nothing in simulation mode, cf. https://github.com/rear/rear/issues/1939 |
| 47 | if is_true "$SIMULATE" ; then |
| 48 | LogPrint "${BASH_SOURCE[0]} administrates TCG Opal 2-compliant self-encrypting disks" |
| 49 | return 0 |
| 50 | fi |
| 51 | |
| 52 | [[ -n "$DEBUGSCRIPTS" ]] && set -$DEBUGSCRIPTS_ARGUMENT |
| 53 | |
| 54 | Log "Command line options of the opaladmin workflow: $*" |
| 55 | |
| 56 | if (($# < 1)); then |
| 57 | PrintError "Missing required ACTION argument." |
| 58 | opaladmin_usage_error |
| 59 | fi |
| 60 | |
| 61 | # Parse action argument |
| 62 | local action |
| 63 | |
| 64 | case "$1" in |
| 65 | (info|changePW|uploadPBA|unlock|deactivate|reactivate) |
| 66 | action="$1" |
| 67 | shift |
| 68 | ;; |
| 69 | (setupERASE|resetDEK|factoryRESET) |
| 70 | action="$1" |
| 71 | shift |
| 72 | if (($# < 1)); then |
| 73 | PrintError "Missing required DEVICE argument for action '$action'." |
| 74 | opaladmin_usage_error |
| 75 | fi |
| 76 | ;; |
| 77 | (help) |
| 78 | opaladmin_help |
| 79 | return 0 |
| 80 | ;; |
| 81 | ("") |
| 82 | PrintError "Missing required ACTION argument." |
| 83 | opaladmin_usage_error |
| 84 | ;; |
| 85 | (*) |
| 86 | PrintError "Unknown action '$1'." |
| 87 | opaladmin_usage_error |
| 88 | ;; |
| 89 | esac |
| 90 | |
| 91 | # Parse options |
| 92 | local options |
| 93 | options="$(getopt -n "$PROGRAM opaladmin" -o "I:" -l "image:" -- "$@" 2>&8)" |
| 94 | [[ $? != 0 ]] && opaladmin_usage_error |
| 95 | |
| 96 | eval set -- "$options" |
| 97 | while true; do |
| 98 | case "$1" in |
| 99 | (-I|--image) |
| 100 | OPALADMIN_IMAGE_FILE="$2" |
| 101 | shift 2 |
| 102 | ;; |
| 103 | (--) |
| 104 | shift |
| 105 | break |
| 106 | ;; |
| 107 | (*) |
| 108 | Error "Internal error during option processing ('$1')." |
| 109 | ;; |
| 110 | esac |
| 111 | done |
| 112 | |
| 113 | # Find TCG Opal 2-compliant disks |
| 114 | OPALADMIN_DEVICES=( $(opal_devices) ) |
| 115 | (( ${#OPALADMIN_DEVICES[@]} == 0 )) && Error "Could not detect TCG Opal 2-compliant disks." |
| 116 | |
| 117 | # Device arguments on the command line override auto-detected devices |
| 118 | if (($# > 0)); then |
| 119 | local device |
| 120 | for device; do |
| 121 | if [[ "$device" == "ALL" ]]; then |
| 122 | # use all available devices, check no further arguments |
| 123 | set -- "${OPALADMIN_DEVICES[@]}" |
| 124 | break |
| 125 | elif ! IsInArray "$device" "${OPALADMIN_DEVICES[@]}"; then |
| 126 | Error "Device '$device' is not a TCG Opal 2-compliant self-encrypting disk." |
| 127 | fi |
| 128 | done |
| 129 | |
| 130 | OPALADMIN_DEVICES=( "$@" ) |
| 131 | fi |
| 132 | |
| 133 | : ${OPALADMIN_IMAGE_FILE:="$(opal_local_pba_image_file)"} |
| 134 | [[ -n "$OPALADMIN_IMAGE_FILE" ]] && opal_check_pba_image "$OPALADMIN_IMAGE_FILE" |
| 135 | |
| 136 | eval "opaladmin_${action}_action" # do not pass arguments here due to eval |
| 137 | |
| 138 | return 0 |
| 139 | } |
| 140 | |
| 141 | |
| 142 | # |
| 143 | # Action functions |
| 144 | # - take a list of devices as arguments, defaulting to "${OPALADMIN_DEVICES[@]}", |
| 145 | # - call opaladmin_get_disk_password() before using the password "$OPAL_DISK_PASSWORD", |
| 146 | # - use the PBA image "$OPALADMIN_IMAGE_FILE" (when empty: there is no PBA image). |
| 147 | # |
| 148 | |
| 149 | function opaladmin_info_action() { |
| 150 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 151 | # prints locking information. |
| 152 | |
| 153 | LogUserOutput "$(opal_device_information "${devices[@]}")" |
| 154 | } |
| 155 | |
| 156 | function opaladmin_setupERASE_action() { |
| 157 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 158 | # enables locking, assigns a disk password and resets the DEK, ERASING ALL DATA ON THE DISK. |
| 159 | |
| 160 | local device |
| 161 | local -i device_number=1 |
| 162 | |
| 163 | if [[ -z "$OPALADMIN_IMAGE_FILE" ]]; then |
| 164 | LogUserOutput "Could not find a PBA image file." |
| 165 | local prompt="Continue setup without boot disk support (y/n)? " |
| 166 | local confirmation="$(opal_choice_input "OPALADMIN_SETUP_NO_BOOT_SUPPORT" "$prompt" "y" "n")" |
| 167 | [[ "$confirmation" == "y" ]] || Error "Setup aborted." |
| 168 | fi |
| 169 | |
| 170 | for device in "${devices[@]}"; do |
| 171 | source "$(opal_device_attributes "$device" attributes)" |
| 172 | |
| 173 | LogUserOutput "" |
| 174 | |
| 175 | if [[ "${attributes[support]}" == "n" ]]; then |
| 176 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") does not support locking - skipping setup." |
| 177 | else |
| 178 | if [[ "${attributes[setup]}" == "y" ]]; then |
| 179 | LogUserOutput "Opal locking on device $(opal_device_identification "$device") has already been enabled." |
| 180 | |
| 181 | opaladmin_device_unlock_if_locked "$device" "'$device'" |
| 182 | else |
| 183 | LogUserOutput "Setting up Opal locking on device $(opal_device_identification "$device")..." |
| 184 | |
| 185 | local enable_boot_unlocking="n" |
| 186 | |
| 187 | if [[ -n "$OPALADMIN_IMAGE_FILE" ]]; then |
| 188 | local prompt="Shall device '$device' act as a boot device for disk unlocking (y/n)? " |
| 189 | enable_boot_unlocking="$(opal_choice_input "OPALADMIN_SETUP_BOOT_$device_number" "$prompt" "y" "n")" |
| 190 | fi |
| 191 | |
| 192 | if [[ -z "$OPAL_DISK_PASSWORD" ]]; then |
| 193 | # If this is the first time a password is being entered, check twice. |
| 194 | OPAL_DISK_PASSWORD="$(opal_checked_password_input "OPAL_DISK_PASSWORD" "disk password")" |
| 195 | fi |
| 196 | |
| 197 | opal_device_setup "$device" "$OPAL_DISK_PASSWORD" || Error "Could not set up device '$device'." |
| 198 | LogUserOutput "Initial setup successful." |
| 199 | |
| 200 | if [[ "$enable_boot_unlocking" == "y" ]]; then |
| 201 | opaladmin_use_image_file |
| 202 | LogUserOutput "Enabling shadow MBR and uploading the PBA to device '$device'..." |
| 203 | opal_device_enable_mbr "$device" "$OPAL_DISK_PASSWORD" || |
| 204 | Error "Could not enable the shadow MBR on device '$device'." |
| 205 | opal_device_load_pba_image "$device" "$OPAL_DISK_PASSWORD" "$OPALADMIN_IMAGE_FILE" || |
| 206 | Error "Could not upload the PBA image to device '$device'." |
| 207 | LogUserOutput "Shadow MBR enabled and PBA uploaded." |
| 208 | else |
| 209 | opal_device_disable_mbr "$device" "$OPAL_DISK_PASSWORD" |
| 210 | LogUserOutput "Shadow MBR disabled." |
| 211 | fi |
| 212 | fi |
| 213 | |
| 214 | opaladmin_resetDEK_action "$device" |
| 215 | fi |
| 216 | |
| 217 | device_number+=1 |
| 218 | done |
| 219 | } |
| 220 | |
| 221 | function opaladmin_changePW_action() { |
| 222 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 223 | # changes the disk password. |
| 224 | |
| 225 | local new_password device try_count |
| 226 | |
| 227 | new_password="$(opal_checked_password_input "OPALADMIN_NEW_PASSWORD" "new disk password")" |
| 228 | |
| 229 | for device in "${devices[@]}"; do |
| 230 | LogUserOutput "" |
| 231 | |
| 232 | if [[ "$(opal_device_attribute "$device" "setup")" == "y" ]]; then |
| 233 | LogUserOutput "Changing disk password of device $(opal_device_identification "$device")..." |
| 234 | |
| 235 | for try_count in $(seq 3); do |
| 236 | opaladmin_get_disk_password "old password" |
| 237 | if opal_device_change_password "$device" "$OPAL_DISK_PASSWORD" "$new_password"; then |
| 238 | LogUserOutput "Password changed on device $(opal_device_identification "$device")." |
| 239 | break |
| 240 | else |
| 241 | OPAL_DISK_PASSWORD="" # Assume that the password for this disk did not fit, retry with a new one |
| 242 | PrintError "Could not change password on device $(opal_device_identification "$device")." |
| 243 | fi |
| 244 | done |
| 245 | else |
| 246 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") has not been setup, cannot change password." |
| 247 | fi |
| 248 | done |
| 249 | |
| 250 | OPAL_DISK_PASSWORD="$new_password" |
| 251 | } |
| 252 | |
| 253 | function opaladmin_uploadPBA_action() { |
| 254 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 255 | # uploads the PBA image on disk(s) whose shadow MBR is enabled. |
| 256 | |
| 257 | local device |
| 258 | |
| 259 | for device in "${devices[@]}"; do |
| 260 | LogUserOutput "" |
| 261 | |
| 262 | if [[ "$(opal_device_attribute "$device" "setup")" == "y" ]]; then |
| 263 | if opal_device_mbr_is_enabled "$device"; then |
| 264 | opaladmin_use_image_file |
| 265 | LogUserOutput "Uploading the PBA to device $(opal_device_identification "$device")..." |
| 266 | opaladmin_get_disk_password |
| 267 | opal_device_load_pba_image "$device" "$OPAL_DISK_PASSWORD" "$OPALADMIN_IMAGE_FILE" || |
| 268 | Error "Could not upload the PBA image to device '$device'." |
| 269 | LogUserOutput "PBA uploaded." |
| 270 | else |
| 271 | LogUserOutput "Device $(opal_device_identification "$device") is not a boot device, skipping PBA upload." |
| 272 | fi |
| 273 | else |
| 274 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") has not been setup, cannot upload PBA." |
| 275 | fi |
| 276 | done |
| 277 | } |
| 278 | |
| 279 | function opaladmin_unlock_action() { |
| 280 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 281 | # unlocks disk(s). |
| 282 | |
| 283 | local device |
| 284 | |
| 285 | for device in "${devices[@]}"; do |
| 286 | LogUserOutput "" |
| 287 | |
| 288 | if [[ "$(opal_device_attribute "$device" "setup")" == "y" ]]; then |
| 289 | if [[ "$(opal_device_attribute "$device" "locked")" == "y" ]]; then |
| 290 | opaladmin_device_unlock_if_locked "$device" "$(opal_device_identification "$device")" |
| 291 | else |
| 292 | LogUserOutput "Device $(opal_device_identification "$device") is already unlocked." |
| 293 | fi |
| 294 | else |
| 295 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") has not been setup, cannot unlock." |
| 296 | fi |
| 297 | done |
| 298 | } |
| 299 | |
| 300 | function opaladmin_deactivate_action() { |
| 301 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 302 | # permanently deactivates the locking mechanism on disk(s) |
| 303 | |
| 304 | local device |
| 305 | |
| 306 | for device in "${devices[@]}"; do |
| 307 | LogUserOutput "" |
| 308 | |
| 309 | if [[ "$(opal_device_attribute "$device" "setup")" == "y" ]]; then |
| 310 | opaladmin_deactivate_locking "$device" "$(opal_device_identification "$device")" |
| 311 | else |
| 312 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") has not been setup, cannot deactivate locking." |
| 313 | fi |
| 314 | done |
| 315 | } |
| 316 | |
| 317 | function opaladmin_reactivate_action() { |
| 318 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 319 | # permanently reactivates the locking mechanism on disk(s) |
| 320 | |
| 321 | local device |
| 322 | |
| 323 | for device in "${devices[@]}"; do |
| 324 | LogUserOutput "" |
| 325 | |
| 326 | if [[ "$(opal_device_attribute "$device" "setup")" == "y" ]]; then |
| 327 | opaladmin_reactivate_locking "$device" "$(opal_device_identification "$device")" |
| 328 | else |
| 329 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") has not been setup, cannot reactivate locking." |
| 330 | fi |
| 331 | done |
| 332 | } |
| 333 | |
| 334 | function opaladmin_resetDEK_action() { |
| 335 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 336 | # assigns a new data encryption key, ERASING ALL DATA ON THE DISK. |
| 337 | |
| 338 | local device |
| 339 | |
| 340 | for device in "${devices[@]}"; do |
| 341 | LogUserOutput "" |
| 342 | |
| 343 | if [[ "$(opal_device_attribute "$device" "setup")" == "y" ]]; then |
| 344 | # Unlock before checking device contents |
| 345 | opaladmin_device_unlock_if_locked "$device" "$(opal_device_identification "$device")" |
| 346 | |
| 347 | local confirmation="$(opaladmin_erase_confirmation "$device" "Reset data encryption key (DEK) of device '$device'")" |
| 348 | |
| 349 | if [[ "$confirmation" == "YesERASE" ]]; then |
| 350 | LogUserOutput "About to reset the data encryption key (DEK) of device $(opal_device_identification "$device")..." |
| 351 | opaladmin_get_disk_password |
| 352 | opal_device_regenerate_dek_ERASING_ALL_DATA "$device" "$OPAL_DISK_PASSWORD" |
| 353 | if (( $? == 0 )); then |
| 354 | LogUserOutput "Data encryption key (DEK) reset, data erased." |
| 355 | else |
| 356 | LogUserOutput "WARNING: Could not reset data encryption key (DEK) of device '$device'." |
| 357 | fi |
| 358 | else |
| 359 | LogUserOutput "SKIPPING: Data encryption key (DEK) of device $(opal_device_identification "$device") left untouched." |
| 360 | fi |
| 361 | else |
| 362 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") has not been setup, data encryption key (DEK) left untouched." |
| 363 | fi |
| 364 | done |
| 365 | } |
| 366 | |
| 367 | function opaladmin_factoryRESET_action() { |
| 368 | local devices=( "${@:-${OPALADMIN_DEVICES[@]}}" ) |
| 369 | # resets disks to factory defaults, ERASING ALL DATA ON THE DISK. |
| 370 | |
| 371 | local device |
| 372 | |
| 373 | for device in "${devices[@]}"; do |
| 374 | LogUserOutput "" |
| 375 | |
| 376 | if [[ "$(opal_device_attribute "$device" "setup")" == "y" ]]; then |
| 377 | # Unlock before checking device contents |
| 378 | opaladmin_device_unlock_if_locked "$device" "$(opal_device_identification "$device")" |
| 379 | |
| 380 | local confirmation="$(opaladmin_erase_confirmation "$device" "Factory-reset device '$device'")" |
| 381 | |
| 382 | if [[ "$confirmation" == "YesERASE" ]]; then |
| 383 | LogUserOutput "About to reset device $(opal_device_identification "$device") to factory defaults..." |
| 384 | opaladmin_get_disk_password |
| 385 | opal_device_factory_reset_ERASING_ALL_DATA "$device" "$OPAL_DISK_PASSWORD" || |
| 386 | Error "Could not reset device '$device' to factory defaults." |
| 387 | LogUserOutput "Device reset to factory defaults, data erased." |
| 388 | else |
| 389 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") left untouched." |
| 390 | fi |
| 391 | else |
| 392 | LogUserOutput "SKIPPING: Device $(opal_device_identification "$device") has not been setup, left untouched." |
| 393 | fi |
| 394 | done |
| 395 | } |
| 396 | |
| 397 | function opaladmin_device_unlock_if_locked() { |
| 398 | local device="${1:-?}" |
| 399 | local identification="${2:-?}" |
| 400 | # unlocks the device if necessary. |
| 401 | |
| 402 | if [[ "$(opal_device_attribute "$device" "locked")" == "y" ]]; then |
| 403 | LogUserOutput "Unlocking device $identification..." |
| 404 | opaladmin_get_disk_password |
| 405 | opal_device_unlock "$device" "$OPAL_DISK_PASSWORD" || Error "Could not unlock device '$device'." |
| 406 | LogUserOutput "Device unlocked." |
| 407 | fi |
| 408 | } |
| 409 | |
| 410 | function opaladmin_deactivate_locking() { |
| 411 | local device="${1:-?}" |
| 412 | local identification="${2:-?}" |
| 413 | # permanently deactivates the locking mechanism on the device. |
| 414 | |
| 415 | LogUserOutput "Persistently deactivating the locking mechanism on device $identification..." |
| 416 | opaladmin_get_disk_password |
| 417 | opal_device_deactivate_locking "$device" "$OPAL_DISK_PASSWORD" || |
| 418 | Error "Could not deactivate locking on device '$device'." |
| 419 | LogUserOutput "Locking deactivated." |
| 420 | } |
| 421 | |
| 422 | function opaladmin_reactivate_locking() { |
| 423 | local device="${1:-?}" |
| 424 | local identification="${2:-?}" |
| 425 | # permanently reactivates the locking mechanism on the device. |
| 426 | |
| 427 | LogUserOutput "Reactivate the locking mechanism on device $identification..." |
| 428 | opaladmin_get_disk_password |
| 429 | |
| 430 | opal_device_reactivate_locking "$device" "$OPAL_DISK_PASSWORD" || |
| 431 | Error "Could not reactivate locking on device '$device'." |
| 432 | LogUserOutput "Locking reactivated." |
| 433 | } |
| 434 | |
| 435 | function opaladmin_erase_confirmation() { |
| 436 | local device="${1:?}" |
| 437 | local prompt="${2:?}, ERASING ALL DATA (YesERASE/No)? " |
| 438 | # prints "YesERASE" if $device may be erased, after checking partitions and, if necessary, asking for confirmation |
| 439 | |
| 440 | local confirmation="No" |
| 441 | |
| 442 | [[ "$(opal_device_attribute "$device" "locked")" == "n" ]] || BugError "Cannot safety-check contents of locked device '$device'" |
| 443 | |
| 444 | if opal_disk_has_partitions "$device"; then |
| 445 | if opal_disk_has_mounted_partitions "$device"; then |
| 446 | LogUserOutput "Device $(opal_device_identification "$device") contains mounted partitions:" |
| 447 | LogUserOutput "$(opal_disk_partition_information "$device")" |
| 448 | else |
| 449 | LogUserOutput "Device $(opal_device_identification "$device") contains partitions:" |
| 450 | LogUserOutput "$(opal_disk_partition_information "$device")" |
| 451 | confirmation="$(opal_choice_input "OPALADMIN_ERASE_CONFIRM" "$prompt" "YesERASE" "No")" |
| 452 | fi |
| 453 | else |
| 454 | confirmation="YesERASE" |
| 455 | fi |
| 456 | |
| 457 | echo "$confirmation" |
| 458 | } |
| 459 | |
| 460 | function opaladmin_get_disk_password() { |
| 461 | local which="${1:-disk password}" |
| 462 | # sets $OPAL_DISK_PASSWORD, asking the user if not already done. |
| 463 | # REQUIRES ReaR functions 'UserInput', 'PrintError' |
| 464 | |
| 465 | if [[ -z "$OPAL_DISK_PASSWORD" ]]; then |
| 466 | OPAL_DISK_PASSWORD="$(opal_password_input "OPAL_DISK_PASSWORD" "Enter $which: ")" |
| 467 | fi |
| 468 | } |
| 469 | |
| 470 | function opaladmin_use_image_file() { |
| 471 | # ensures that $OPALADMIN_IMAGE_FILE is non-empty or exits with an error. |
| 472 | |
| 473 | [[ -n "$OPALADMIN_IMAGE_FILE" ]] || Error "Could not find a PBA image file." |
| 474 | LogPrint "Using PBA image file '$OPALADMIN_IMAGE_FILE'." |
| 475 | } |