Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.82 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ###
  4. #
  5. # Name: reissue_filevault_recovery_key.sh
  6. # Description: This script is intended to run on Macs which no longer have
  7. # a valid recovery key in the JSS. It prompts users to enter
  8. # their Mac password, and uses this password to generate a
  9. # new FileVault key and escrow with the JSS. The "redirect
  10. # FileVault keys to JSS" configuration profile must already
  11. # be deployed in order for this script to work correctly.
  12. # Author: Elliot Jordan <elliot@elliotjordan.com>
  13. # Created: 2015-01-05
  14. # Last Modified: 2017-11-16
  15. # Version: 1.9
  16. #
  17. ###
  18.  
  19.  
  20. ################################## VARIABLES ##################################
  21.  
  22. # Company logo. (Tested with PNG, JPG, GIF, PDF, and AI formats.)
  23. LOGO="/private/var/jamf/images/logo.png"
  24.  
  25. # The title of the message that will be displayed to the user.
  26. # Not too long, or it'll get clipped.
  27. PROMPT_TITLE="Encryption Key Escrow"
  28.  
  29. # The body of the message that will be displayed before prompting the user for
  30. # their password. All message strings below can be multiple lines.
  31. PROMPT_MESSAGE="Your Mac's FileVault encryption key needs to be escrowed by PretendCo IT.
  32.  
  33. Click the Next button below, then enter your Mac's password when prompted."
  34.  
  35. # The body of the message that will be displayed after 5 incorrect passwords.
  36. FORGOT_PW_MESSAGE="You made five incorrect password attempts.
  37.  
  38. Please contact the Help Desk at 555-1212 for help with your Mac password."
  39.  
  40. # The body of the message that will be displayed after successful completion.
  41. SUCCESS_MESSAGE="Thank you! Your FileVault key has been escrowed."
  42.  
  43. # The body of the message that will be displayed if a failure occurs.
  44. FAIL_MESSAGE="Sorry, an error occurred while escrowing your FileVault key. Please contact the Help Desk at 555-1212 for help."
  45.  
  46. # Optional but recommended: The profile identifiers of the FileVault Key
  47. # Redirection profiles (e.g. ABCDEF12-3456-7890-ABCD-EF1234567890).
  48. PROFILE_IDENTIFIER_10_12="" # 10.12 and earlier
  49. PROFILE_IDENTIFIER_10_13="" # 10.13 and later
  50.  
  51.  
  52. ###############################################################################
  53. ######################### DO NOT EDIT BELOW THIS LINE #########################
  54. ###############################################################################
  55.  
  56.  
  57. ######################## VALIDATION AND ERROR CHECKING ########################
  58.  
  59. # Suppress errors for the duration of this script. (This prevents JAMF Pro from
  60. # marking a policy as "failed" if the words "fail" or "error" inadvertently
  61. # appear in the script output.)
  62. exec 2>/dev/null
  63.  
  64. BAILOUT=false
  65.  
  66. # Make sure we have root privileges (for fdesetup).
  67. if [[ $EUID -ne 0 ]]; then
  68. REASON="This script must run as root."
  69. BAILOUT=true
  70. fi
  71.  
  72. # Check for remote users.
  73. REMOTE_USERS=$(/usr/bin/who | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | wc -l)
  74. if [[ $REMOTE_USERS -gt 0 ]]; then
  75. REASON="Remote users are logged in."
  76. BAILOUT=true
  77. fi
  78.  
  79. # Make sure the custom logo file is present.
  80. if [[ ! -f "$LOGO" ]]; then
  81. REASON="Custom logo not present: $LOGO"
  82. BAILOUT=true
  83. fi
  84.  
  85. # Convert POSIX path of logo icon to Mac path for AppleScript
  86. LOGO_POSIX="$(/usr/bin/osascript -e 'tell application "System Events" to return POSIX file "'"$LOGO"'" as text')"
  87.  
  88. # Bail out if jamfHelper doesn't exist.
  89. jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
  90. if [[ ! -x "$jamfHelper" ]]; then
  91. REASON="jamfHelper not found."
  92. BAILOUT=true
  93. fi
  94.  
  95. # Most of the code below is based on the JAMF reissueKey.sh script:
  96. # https://github.com/JAMFSupport/FileVault2_Scripts/blob/master/reissueKey.sh
  97.  
  98. # Check the OS version.
  99. OS_MAJOR=$(/usr/bin/sw_vers -productVersion | awk -F . '{print $1}')
  100. OS_MINOR=$(/usr/bin/sw_vers -productVersion | awk -F . '{print $2}')
  101. if [[ "$OS_MAJOR" -ne 10 || "$OS_MINOR" -lt 9 ]]; then
  102. REASON="This script requires macOS 10.9 or higher. This Mac has $(sw_vers -productVersion)."
  103. BAILOUT=true
  104. elif [[ "$OS_MAJOR" -eq 10 && "$OS_MINOR" -ge 13 ]]; then
  105. echo "[WARNING] This script is still in BETA in High Sierra, because the fdesetup binary has changed significantly. Please use with caution."
  106. fi
  107.  
  108. # Check to see if the encryption process is complete
  109. FV_STATUS="$(/usr/bin/fdesetup status)"
  110. if grep -q "Encryption in progress" <<< "$FV_STATUS"; then
  111. REASON="FileVault encryption is in progress. Please run the script again when it finishes."
  112. BAILOUT=true
  113. elif grep -q "FileVault is Off" <<< "$FV_STATUS"; then
  114. REASON="Encryption is not active."
  115. BAILOUT=true
  116. elif ! grep -q "FileVault is On" <<< "$FV_STATUS"; then
  117. REASON="Unable to determine encryption status."
  118. BAILOUT=true
  119. fi
  120.  
  121. # Get the logged in user's name
  122. CURRENT_USER=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
  123.  
  124. # Make sure there's an actual user logged in
  125. if [[ -z $CURRENT_USER || "$CURRENT_USER" == "root" ]]; then
  126. REASON="No user is currently logged in."
  127. BAILOUT=true
  128. else
  129. # Make sure logged in account is already authorized with FileVault 2
  130. FV_USERS="$(/usr/bin/fdesetup list)"
  131. if ! egrep -q "^${CURRENT_USER}," <<< "$FV_USERS"; then
  132. REASON="$CURRENT_USER is not on the list of FileVault enabled users: $FV_USERS"
  133. BAILOUT=true
  134. fi
  135. fi
  136.  
  137. # If specified, the FileVault key redirection profile needs to be installed.
  138. if [[ "$OS_MAJOR" -eq 10 && "$OS_MINOR" -le 12 ]]; then
  139. if [[ "$PROFILE_IDENTIFIER_10_12" != "" ]]; then
  140. if ! /usr/bin/profiles -Cv | grep -q "profileIdentifier: $PROFILE_IDENTIFIER_10_12"; then
  141. REASON="The FileVault Key Redirection profile is not yet installed."
  142. BAILOUT=true
  143. fi
  144. fi
  145. elif [[ "$OS_MAJOR" -eq 10 && "$OS_MINOR" -gt 12 ]]; then
  146. if [[ "$PROFILE_IDENTIFIER_10_13" != "" ]]; then
  147. if ! /usr/bin/profiles -Cv | grep -q "profileIdentifier: $PROFILE_IDENTIFIER_10_13"; then
  148. REASON="The FileVault Key Redirection profile is not yet installed."
  149. BAILOUT=true
  150. fi
  151. fi
  152. fi
  153.  
  154.  
  155. ################################ MAIN PROCESS #################################
  156.  
  157. # Get information necessary to display messages in the current user's context.
  158. USER_ID=$(/usr/bin/id -u "$CURRENT_USER")
  159. if [[ "$OS_MAJOR" -eq 10 && "$OS_MINOR" -le 9 ]]; then
  160. L_ID=$(/usr/bin/pgrep -x -u "$USER_ID" loginwindow)
  161. L_METHOD="bsexec"
  162. elif [[ "$OS_MAJOR" -eq 10 && "$OS_MINOR" -gt 9 ]]; then
  163. L_ID=$USER_ID
  164. L_METHOD="asuser"
  165. fi
  166.  
  167. # If any error occurred in the validation section, bail out.
  168. if [[ "$BAILOUT" == "true" ]]; then
  169. echo "[ERROR]: $REASON"
  170. launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "utility" -icon "$LOGO" -title "$PROMPT_TITLE" -description "$FAIL_MESSAGE: $REASON." -button1 'OK' -defaultButton 1 -startlaunchd &>/dev/null &
  171. exit 1
  172. fi
  173.  
  174. # Display a branded prompt explaining the password prompt.
  175. echo "Alerting user $CURRENT_USER about incoming password prompt..."
  176. /bin/launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "utility" -icon "$LOGO" -title "$PROMPT_TITLE" -description "$PROMPT_MESSAGE" -button1 "Next" -defaultButton 1 -startlaunchd &>/dev/null
  177.  
  178. # Get the logged in user's password via a prompt.
  179. echo "Prompting $CURRENT_USER for their Mac password..."
  180. USER_PASS="$(/bin/launchctl "$L_METHOD" "$L_ID" /usr/bin/osascript -e 'display dialog "Please enter the password you use to log in to your Mac:" default answer "" with title "'"${PROMPT_TITLE//\"/\\\"}"'" giving up after 86400 with text buttons {"OK"} default button 1 with hidden answer with icon file "'"${LOGO_POSIX//\"/\\\"}"'"' -e 'return text returned of result')"
  181.  
  182. # Thanks to James Barclay (@futureimperfect) for this password validation loop.
  183. TRY=1
  184. until /usr/bin/dscl /Search -authonly "$CURRENT_USER" "$USER_PASS" &>/dev/null; do
  185. (( TRY++ ))
  186. echo "Prompting $CURRENT_USER for their Mac password (attempt $TRY)..."
  187. USER_PASS="$(/bin/launchctl "$L_METHOD" "$L_ID" /usr/bin/osascript -e 'display dialog "Sorry, that password was incorrect. Please try again:" default answer "" with title "'"${PROMPT_TITLE//\"/\\\"}"'" giving up after 86400 with text buttons {"OK"} default button 1 with hidden answer with icon file "'"${LOGO_POSIX//\"/\\\"}"'"' -e 'return text returned of result')"
  188. if (( TRY >= 5 )); then
  189. echo "[ERROR] Password prompt unsuccessful after 5 attempts. Displaying \"forgot password\" message..."
  190. /bin/launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "utility" -icon "$LOGO" -title "$PROMPT_TITLE" -description "$FORGOT_PW_MESSAGE" -button1 'OK' -defaultButton 1 -startlaunchd &>/dev/null &
  191. exit 1
  192. fi
  193. done
  194. echo "Successfully prompted for Mac password."
  195.  
  196. # If needed, unload and kill FDERecoveryAgent.
  197. if /bin/launchctl list | grep -q "com.apple.security.FDERecoveryAgent"; then
  198. echo "Unloading FDERecoveryAgent LaunchDaemon..."
  199. /bin/launchctl unload /System/Library/LaunchDaemons/com.apple.security.FDERecoveryAgent.plist
  200. fi
  201. if pgrep -q "FDERecoveryAgent"; then
  202. echo "Stopping FDERecoveryAgent process..."
  203. killall "FDERecoveryAgent"
  204. fi
  205.  
  206. # Translate XML reserved characters to XML friendly representations.
  207. USER_PASS=${USER_PASS//&/&amp;}
  208. USER_PASS=${USER_PASS//</&lt;}
  209. USER_PASS=${USER_PASS//>/&gt;}
  210. USER_PASS=${USER_PASS//\"/&quot;}
  211. USER_PASS=${USER_PASS//\'/&apos;}
  212.  
  213. # For 10.13's escrow process, store the last modification time of /var/db/FileVaultPRK.dat
  214. if [[ "$OS_MINOR" -ge 13 ]]; then
  215. echo "Checking for /var/db/FileVaultPRK.dat on macOS 10.13+..."
  216. PRK_MOD=0
  217. if [ -e "/var/db/FileVaultPRK.dat" ]; then
  218. echo "Found existing personal recovery key."
  219. PRK_MOD=$(/usr/bin/stat -f "%Sm" -t "%s" "/var/db/FileVaultPRK.dat")
  220. fi
  221. fi
  222.  
  223. echo "Issuing new recovery key..."
  224. FDESETUP_OUTPUT="$(/usr/bin/fdesetup changerecovery -norecoverykey -verbose -personal -inputplist << EOF
  225. <?xml version="1.0" encoding="UTF-8"?>
  226. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  227. <plist version="1.0">
  228. <dict>
  229. <key>Username</key>
  230. <string>$CURRENT_USER</string>
  231. <key>Password</key>
  232. <string>$USER_PASS</string>
  233. </dict>
  234. </plist>
  235. EOF
  236. )"
  237.  
  238. # Clear password variable.
  239. unset USER_PASS
  240.  
  241. # Test success conditions.
  242. FDESETUP_RESULT=$?
  243. # Differentiate <=10.12 and >=10.13 success conditions
  244. if [[ "$OS_MINOR" -ge 13 ]]; then
  245. # Check new modification time of of FileVaultPRK.dat
  246. ESCROW_STATUS=1
  247. if [ -e "/var/db/FileVaultPRK.dat" ]; then
  248. NEW_PRK_MOD=$(/usr/bin/stat -f "%Sm" -t "%s" "/var/db/FileVaultPRK.dat")
  249. if [[ $NEW_PRK_MOD -gt $PRK_MOD ]]; then
  250. ESCROW_STATUS=0
  251. echo "Recovery key updated locally and available for collection via MDM."
  252. else
  253. echo "[WARNING] The recovery key does not appear to have been updated locally."
  254. fi
  255. fi
  256. else
  257. # Check output of fdesetup command for indication of an escrow attempt
  258. grep -q "Escrowing recovery key..." <<< "$FDESETUP_OUTPUT"
  259. ESCROW_STATUS=$?
  260. fi
  261.  
  262. if [[ $FDESETUP_RESULT -ne 0 ]]; then
  263. [[ -n "$FDESETUP_OUTPUT" ]] && echo "$FDESETUP_OUTPUT"
  264. echo "[WARNING] fdesetup exited with return code: $FDESETUP_RESULT."
  265. echo "See this page for a list of fdesetup exit codes and their meaning:"
  266. echo "https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man8/fdesetup.8.html"
  267. echo "Displaying \"failure\" message..."
  268. /bin/launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "utility" -icon "$LOGO" -title "$PROMPT_TITLE" -description "$FAIL_MESSAGE: fdesetup exited with code $FDESETUP_RESULT. Output: $FDESETUP_OUTPUT" -button1 'OK' -defaultButton 1 -startlaunchd &>/dev/null &
  269. elif [[ $ESCROW_STATUS -ne 0 ]]; then
  270. [[ -n "$FDESETUP_OUTPUT" ]] && echo "$FDESETUP_OUTPUT"
  271. echo "[WARNING] FileVault key was generated, but escrow cannot be confirmed. Please verify that the redirection profile is installed and the Mac is connected to the internet."
  272. echo "Displaying \"failure\" message..."
  273. /bin/launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "utility" -icon "$LOGO" -title "$PROMPT_TITLE" -description "$FAIL_MESSAGE: New key generated, but escrow did not occur." -button1 'OK' -defaultButton 1 -startlaunchd &>/dev/null &
  274. else
  275. [[ -n "$FDESETUP_OUTPUT" ]] && echo "$FDESETUP_OUTPUT"
  276. echo "Displaying \"success\" message..."
  277. /bin/launchctl "$L_METHOD" "$L_ID" "$jamfHelper" -windowType "utility" -icon "$LOGO" -title "$PROMPT_TITLE" -description "$SUCCESS_MESSAGE" -button1 'OK' -defaultButton 1 -startlaunchd &>/dev/null &
  278. fi
  279.  
  280. exit $FDESETUP_RESULT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement