|
| 1 | +#!/usr/bin/env zsh |
| 2 | +# |
| 3 | +# This script performs the following tasks: |
| 4 | +# * prompt for current user password |
| 5 | +# * prompt user for Pre-Boot Authentication password |
| 6 | +# * grant current user privileges to unlock FileVault |
| 7 | +# |
| 8 | +# Copyright (c) 2025 Doug Campbell. All rights reserved. |
| 9 | + |
| 10 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${(%):-%x}" )" &> /dev/null && pwd ) |
| 11 | + |
| 12 | +. "${SCRIPT_DIR}/lib/config.sh" |
| 13 | +. "${SCRIPT_DIR}/lib/display.sh" |
| 14 | +. "${SCRIPT_DIR}/lib/filevault.sh" |
| 15 | +. "${SCRIPT_DIR}/lib/globals.sh" |
| 16 | +. "${SCRIPT_DIR}/lib/input.sh" |
| 17 | +. "${SCRIPT_DIR}/lib/logging.sh" |
| 18 | +. "${SCRIPT_DIR}/lib/preboot.sh" |
| 19 | +. "${SCRIPT_DIR}/lib/quoting.sh" |
| 20 | +. "${SCRIPT_DIR}/lib/system.sh" |
| 21 | +. "${SCRIPT_DIR}/lib/util.sh" |
| 22 | + |
| 23 | +SCRIPT_USER=$(logname) |
| 24 | + |
| 25 | +TRAPEXIT() { |
| 26 | + [[ $SUDO_INVALIDATE_ON_EXIT -eq 0 ]] && /usr/bin/sudo -k |
| 27 | +} |
| 28 | + |
| 29 | +main () { |
| 30 | + prepare_display_environment |
| 31 | + check_run_command_as_root |
| 32 | + check_run_command_as_admin |
| 33 | + |
| 34 | + # get script user password |
| 35 | + # note: we get this separately because we need to have sudo prior to attempting to verify |
| 36 | + # the remaining accounts just in case they are currently disabled. |
| 37 | + ohai 'Getting password for account currently running this script.' |
| 38 | + get_account_password_aux $SCRIPT_USER |
| 39 | + printf '\n' |
| 40 | + |
| 41 | + get_sudo "${PASSWORDS[$SCRIPT_USER]}" |
| 42 | + |
| 43 | + if is_account_exist "preboot"; then |
| 44 | + ohai 'Getting password for Pre-Boot Authentication account.' |
| 45 | + get_account_password_aux "preboot" |
| 46 | + printf "\n" |
| 47 | + |
| 48 | + # add current user to FileVault (temporarily) |
| 49 | + enable_account "preboot" |
| 50 | + grant_account_filevault_access "$SCRIPT_USER" "${PASSWORDS[$SCRIPT_USER]}" "preboot" "${PASSWORDS[preboot]}" |
| 51 | + enable_secure_token_for_account "$SCRIPT_USER" "${PASSWORDS[$SCRIPT_USER]}" "preboot" "${PASSWORDS[preboot]}" |
| 52 | + disable_account "preboot" |
| 53 | + fi |
| 54 | + |
| 55 | + generate_new_filevault_recovery_key "$SCRIPT_USER" "${PASSWORDS[$SCRIPT_USER]}" |
| 56 | + |
| 57 | + if is_account_exist "preboot"; then |
| 58 | + # remove current user from FileVault |
| 59 | + get_sudo "${PASSWORDS[$SCRIPT_USER]}" |
| 60 | + remove_filevault_unlock_for_other_users |
| 61 | + fi |
| 62 | + |
| 63 | + printf '\n' |
| 64 | + display_warning "A FileVault recovery key has been generatered and stored in the same folder as this script. The filename begins with this device's serial # "$(ioreg -l | awk -F'"' '/IOPlatformSerialNumber/{print $4}')"." |
| 65 | + printf '\n' |
| 66 | + display_message "Copy this file to a safe location (e.g. Google Drive) before proceeding." |
| 67 | + |
| 68 | + printf '\n' |
| 69 | + read -s -k '?Press any key to continue.' |
| 70 | + printf '\n' |
| 71 | + |
| 72 | + kill `ps -A | grep -w Terminal.app | grep -v grep | awk '{print $1}'` |
| 73 | +} |
| 74 | + |
| 75 | +main "$@" |
| 76 | + |
| 77 | +exit 0 |
0 commit comments