|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -eu -o pipefail |
| 4 | + |
| 5 | +PATH=/sbin:/bin:/usr/sbin:/usr/bin |
| 6 | + |
| 7 | +[ "$(id -un)" = root ] \ |
| 8 | + || exec -a "$0" osascript -e " |
| 9 | + do shell script quoted form of \"$0\" & space & quoted form of \"$*\" with prompt \"BootUnlock Configurator requires administrative privileges to work with volumes and to update the System keychain.\" with administrator privileges" \ |
| 10 | + || exit 1 |
| 11 | + |
| 12 | +# Location of the log file |
| 13 | +LOG_FILE=/var/log/BootUnlock.log |
| 14 | + |
| 15 | +# Determine the cannonical location of this script |
| 16 | +WORK_DIR="${BASH_SOURCE%/*}" |
| 17 | +[ "$WORK_DIR" != "$BASH_SOURCE" ] || WORK_DIR=. |
| 18 | +pushd "$WORK_DIR" &>/dev/null |
| 19 | +WORK_DIR=$(pwd -P) |
| 20 | +popd &>/dev/null |
| 21 | +SELF="$WORK_DIR/${0##*/}" |
| 22 | + |
| 23 | +printf 'Redirecting standard output and errors to "%s" ...\n' "$LOG_FILE" |
| 24 | +exec >>"$LOG_FILE" 2>&1 |
| 25 | +printf '===[ update.sh: %s ]===\n' "$(date)" |
| 26 | + |
| 27 | +# A quick and dirty way of determining the root device :) |
| 28 | +ROOT_DEVICE=$(df -l / | grep -E '^/dev/' | cut -f1 -d' ' | head -1 | cut -f3- -d/) |
| 29 | + |
| 30 | +# Get the list of volumes with the encryption enabled |
| 31 | +IFS=$'\n' VOLUME=($(diskutil apfs list -plist \ |
| 32 | + | xsltproc --novalid "${0%/*}/diskutil.xsl" - \ |
| 33 | + | grep -E ':true:(true)?$' \ |
| 34 | + | cut -f1-3 -d':' \ |
| 35 | +)) |
| 36 | + |
| 37 | +# Generate a list of volumes for GUI |
| 38 | +VOLUME_LIST= |
| 39 | +for V in "${VOLUME[@]}" ; do |
| 40 | + NAME="${V%%:*}"; V="${V#*:}" |
| 41 | + UUID="${V%%:*}"; V="${V#*:}" |
| 42 | + DEVICE="${V%%:*}"; V="${V#*:}" |
| 43 | + |
| 44 | + # macOS can automatically mount the system volume, so there is no point |
| 45 | + # of presenting that choice to the user |
| 46 | + [ "$DEVICE" != "$ROOT_DEVICE" ] || continue |
| 47 | + |
| 48 | + VOLUME_LIST="$VOLUME_LIST${VOLUME_LIST:+, }\"$DEVICE > $NAME\"" |
| 49 | +done |
| 50 | + |
| 51 | +RESPONSE=$(osascript -e " |
| 52 | + set volumeList to { $VOLUME_LIST } |
| 53 | + set unlockList to choose from list volumeList with title \"BootUnlock\" with prompt \" |
| 54 | +Please select volume(s) you want to be automatically unlocked during the boot (you can select multiple volumes by holding the Option key)\" multiple selections allowed true empty selection allowed true |
| 55 | +") |
| 56 | + |
| 57 | +if [ -z "$RESPONSE" -o "$RESPONSE" = false ]; then |
| 58 | + osascript -e "display alert \"BootUnlock\" message \" |
| 59 | +You did not select any volumes, so BootUnlock is not going to do anything at the system boot up time. |
| 60 | +
|
| 61 | +If you reconsider and will want to enable unlocking of a particular volume you can re-run the '$SELF' script at a later time |
| 62 | +\" as critical" &>/dev/null |
| 63 | + exit 0 |
| 64 | +fi |
| 65 | + |
| 66 | +RESPONSE="$(printf '%s' "$RESPONSE" | tr ',' '\n' | sed 's,^[[:space:]]*,,;s,>.*$,,;s,[[:space:]]*$,,')" |
| 67 | + |
| 68 | +# Real work starts here :) |
| 69 | +for V in "${VOLUME[@]}" ; do |
| 70 | + NAME="${V%%:*}"; V="${V#*:}" |
| 71 | + UUID="${V%%:*}"; V="${V#*:}" |
| 72 | + DEVICE="${V%%:*}"; V="${V#*:}" |
| 73 | + |
| 74 | + # We are going to work only on the volumes selected by the user |
| 75 | + printf '%s' "$RESPONSE" | grep -E "^$DEVICE\$" &>/dev/null || continue |
| 76 | + |
| 77 | + while : ; do # This is a wrapper in case user provides a wrong password |
| 78 | + PASSPHRASE=$(osascript -e " |
| 79 | + display dialog \"Please provide the passphrase for volume '$NAME':\" with title \"BootUnlock\" buttons { \"Skip\", \"Unlock\" } default button \"Unlock\" with icon file ((path to \"apps\" as text) & \"Utilities:Disk Utility.app:Contents:Resources:AppIcon.icns\") default answer \"\" hidden answer true |
| 80 | + ") |
| 81 | + |
| 82 | + PASSPHRASE=$(printf '%s' "$PASSPHRASE" | cut -f3- -d:) |
| 83 | + |
| 84 | + if printf '%s' "$PASSPHRASE" | diskutil apfs unlock "$DEVICE" -stdinpassphrase -verify -user "$UUID"; then |
| 85 | + printf 'Adding password for volume "%s" with UUID %s to the System keychain...\n' "$NAME" "$UUID" |
| 86 | + if sudo /usr/bin/security add-generic-password \ |
| 87 | + -a "$UUID" -s "$UUID" -l "$NAME" \ |
| 88 | + -D 'Encrypted Volume Password' \ |
| 89 | + -T '' -T "$WORK_DIR/BootUnlock" \ |
| 90 | + -w "$PASSPHRASE" \ |
| 91 | + -U \ |
| 92 | + /Library/Keychains/System.keychain; then |
| 93 | + break |
| 94 | + else |
| 95 | + osascript -e "display alert \"BootUnlock\" message \" |
| 96 | +BootUnlock experienced an internal error while adding password to the System keychain. |
| 97 | +
|
| 98 | +Please check the /var/log/BootUnlock.log log file for more information. |
| 99 | +\" as critical" &>/dev/null |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | + else |
| 103 | + osascript -e "display alert \"BootUnlock\" message \" |
| 104 | +The specified password for volume '$NAME' does not seem to be valid. BootUnlock will prompt you for the password again. |
| 105 | +
|
| 106 | +If you believe that you are providing the correct password, yet it is not recognised, please check the /var/log/BootUnlock.log log file for more information. |
| 107 | +\" as critical" &>/dev/null |
| 108 | + fi |
| 109 | + done |
| 110 | +done |
| 111 | + |
0 commit comments