diff --git a/Utilities/LogoutHook/LogoutHook.command b/Utilities/LogoutHook/LogoutHook.command new file mode 100755 index 0000000000000000000000000000000000000000..187e6a5e6c1c949929d4e4e90b709832ce997a3d --- /dev/null +++ b/Utilities/LogoutHook/LogoutHook.command @@ -0,0 +1,67 @@ +#!/bin/sh + +# +# Copyright © 2019 Rodion Shingarev. All rights reserved. +# Slight optimizations by PMheart and vit9696. +# + +abort() { + echo "Fatal error: ${1}" + exit 1 +} + +if [ ! -x /usr/bin/dirname ] || [ ! -x /usr/sbin/nvram ] || [ ! -x /usr/bin/grep ] || [ ! -x /bin/chmod ] || [ ! -x /usr/bin/sed ] || [ ! -x /usr/bin/base64 ] || [ ! -x /bin/rm ] || [ ! -x /bin/mkdir ] || [ ! -x /bin/cat ] || [ ! -x /bin/dd ] || [ ! -x /usr/bin/stat ] || [ ! -x /usr/libexec/PlistBuddy ] || [ ! -x /usr/sbin/ioreg ] || [ ! -x /usr/bin/xxd ] || [ ! -x /usr/sbin/diskutil ] || [ ! -x /bin/cp ] || [ ! -x /usr/bin/wc ]; then + abort "Unix environment is broken!" +fi + +cd "$(/usr/bin/dirname "${0}")" || abort "Failed to enter working directory!" + +nvram=/usr/sbin/nvram +if [ -z "$("${nvram}" -x '8BE4DF61-93CA-11D2-AA0D-00E098032B8C:BootOrder' | /usr/bin/grep 'xml')" ]; then + nvram=./nvram.mojave + if [ ! -f "${nvram}" ]; then + abort "${nvram} does NOT exist!" + elif [ ! -x "${nvram}" ]; then + abort "${nvram} is not executable!" + fi +fi + +getKey() { + local key="$1" + "${nvram}" -x "${key}" | /usr/bin/sed '/\/,/\<\/data\>/!d;//d' | /usr/bin/base64 --decode +} + +/bin/rm -rf dumps +/bin/mkdir dumps || abort "Failed to create dumps directory!" +cd dumps || abort "Failed to enter dumps directory!" + +"${nvram}" -xp > ./nvram1.plist || abort "Failed to dump nvram!" + +getKey '8BE4DF61-93CA-11D2-AA0D-00E098032B8C:Boot0080' > ./Boot0080 +if [ ! -z "$(/bin/cat ./Boot0080)" ]; then + getKey 'efi-boot-device-data' > efi-boot-device-data || abort "Failed to retrieve efi-boot-device-data!" + /bin/dd seek=24 if=efi-boot-device-data of=Boot0080 bs=1 count=$(/usr/bin/stat -f%z efi-boot-device-data) || abort "Failed to fill Boot0080 with efi-boot-device-data!" + /usr/libexec/PlistBuddy -c "Import Add:8BE4DF61-93CA-11D2-AA0D-00E098032B8C:Boot0080 Boot0080" ./nvram.plist || abort "Failed to import Boot0080!" +fi + +for key in BootOrder BootCurrent BootNext Boot008{1..3}; do + getKey "8BE4DF61-93CA-11D2-AA0D-00E098032B8C:${key}" > "${key}" + if [ ! -z "$(/bin/cat "${key}")" ]; then + /usr/libexec/PlistBuddy -c "Import Add:8BE4DF61-93CA-11D2-AA0D-00E098032B8C:${key} ${key}" ./nvram.plist || abort "Failed to import ${key} from 8BE4DF61-93CA-11D2-AA0D-00E098032B8C!" + fi +done + +/usr/libexec/PlistBuddy -c "Add Version integer 1" ./nvram.plist || abort "Failed to add Version!" +/usr/libexec/PlistBuddy -c "Add Add:7C436110-AB2A-4BBB-A880-FE41995C9F82 dict" ./nvram.plist || abort "Failed to add dict 7C436110-AB2A-4BBB-A880-FE41995C9F82" +/usr/libexec/PlistBuddy -c "Merge nvram1.plist Add:7C436110-AB2A-4BBB-A880-FE41995C9F82" ./nvram.plist || abort "Failed to merge with nvram1.plist!" + +UUID="$("${nvram}" 4D1FDA02-38C7-4A6A-9CC6-4BCCA8B30102:boot-path | /usr/bin/sed 's/.*GPT,\([^,]*\),.*/\1/')" +if [ "$(printf "${UUID}" | /usr/bin/wc -c)" -eq 36 ] && [ -z "$(echo "${UUID}" | /usr/bin/sed 's/[-0-9A-F]//g')" ]; then + /usr/sbin/diskutil mount "${UUID}" || abort "Failed to mount ${UUID}!" + /bin/cp ./nvram.plist "$(/usr/sbin/diskutil info "${UUID}" | /usr/bin/sed -n 's/.*Mount Point: *//p')" || abort "Failed to copy nvram.plist!" + exit 0 +else + abort "Illegal UUID or unknown loader!" +fi + +/bin/rm -rf dumps diff --git a/Utilities/LogoutHook/README.md b/Utilities/LogoutHook/README.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a3391508b6d101698ffcf5d6d3192cad2c32c9 --- /dev/null +++ b/Utilities/LogoutHook/README.md @@ -0,0 +1,8 @@ +LogoutHook +=========== + +## Installation +```sudo defaults write com.apple.loginwindow LogoutHook /path/to/LogoutHook.command``` + +## Notes +`LogoutHook.command` highly depends on macOS `nvram` utility supporting `-x` option, which is unavailable on 10.12 and below. (Our `nvram.mojave` somehow fixes that issue by invoking it instead of system one) \ No newline at end of file diff --git a/Utilities/LogoutHook/nvram.mojave b/Utilities/LogoutHook/nvram.mojave new file mode 100755 index 0000000000000000000000000000000000000000..9425357bd31ba25d865d72da8b3de7dd64217f19 Binary files /dev/null and b/Utilities/LogoutHook/nvram.mojave differ