install.fairie/home/.chezmoiscripts/universal/run_before_01-macos-privileges.sh.tmpl

82 lines
4.4 KiB
Cheetah
Raw Normal View History

2023-11-29 23:18:43 -08:00
#!/usr/bin/env bash
# @file Ensure Terminal Permissions
# @brief Ensures the macOS terminal used for the provisioning process has full disk access permissions
# @description
# This script ensures the terminal running the provisioning process has full disk access permissions. It also
# prints information regarding the process of how to enable the permission as well as information related to
# the specific reasons that the terminal needs full disk access. More specifically, the scripts need full
# disk access to modify various system files and permissions.
#
# ## Sources
#
# * [Detecting Full Disk Access permission on macOS](https://www.dzombak.com/blog/2021/11/macOS-Scripting-How-to-tell-if-the-Terminal-app-has-Full-Disk-Access.html)
{{ includeTemplate "universal/profile-before" }}
{{ includeTemplate "universal/logg-before" }}
# @description Prints information describing why full disk access is required for the script to run on macOS.
printFullDiskAccessNotice() {
if [ -d /Applications ] && [ -d /System ]; then
logg md "${XDG_DATA_HOME:-$HOME/.local/share}/chezmoi/docs/terminal/full-disk-access.md"
fi
}
# @description Ensures the terminal running the provisioning process script has full disk access on macOS. It does this
# by attempting to read a file that requires full disk access. If it does not, the program opens the preferences
# pane where the user can grant access so that the script can continue.
ensureFullDiskAccess() {
if [ -d /Applications ] && [ -d /System ]; then
if ! plutil -lint /Library/Preferences/com.apple.TimeMachine.plist > /dev/null ; then
2023-12-06 22:29:23 -08:00
printFullDiskAccessNotice
2023-11-29 23:18:43 -08:00
logg star 'Opening Full Disk Access preference pane.. Grant full-disk access for the terminal you would like to run the provisioning process with.' && open "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles"
2023-12-05 23:33:54 -08:00
logg info 'You may have to force quit the terminal and have it reload'
if [ ! -f "$HOME/.zshrc" ] || ! cat "$HOME/.zshrc" | grep '# TEMPORARY FOR INSTALL DOCTOR MACOS' > /dev/null; then
echo 'bash <(curl -sSL https://install.doctor/start) # TEMPORARY FOR INSTALL DOCTOR MACOS' >> "$HOME/.zshrc"
fi
2023-12-05 10:19:08 -08:00
logg prompt 'Press ENTER to check for Full Disk Access again' && read -r
2023-11-29 23:18:43 -08:00
ensureFullDiskAccess
else
logg success 'Current terminal has full disk access'
2023-12-05 23:44:44 -08:00
if [ -f "$HOME/.zshrc" ]; then
if command -v gsed > /dev/null; then
sudo gsed -i '/# TEMPORARY FOR INSTALL DOCTOR MACOS/d' "$HOME/.zshrc" || logg warn "Failed to remove kickstart script from .zshrc"
else
sudo sed -i '/# TEMPORARY FOR INSTALL DOCTOR MACOS/d' "$HOME/.zshrc" || logg warn "Failed to remove kickstart script from .zshrc"
fi
2023-12-05 23:33:54 -08:00
fi
2023-11-29 23:18:43 -08:00
fi
fi
}
2023-12-05 18:50:29 -08:00
# @description Applies changes that require input from the user such as using Touch ID on macOS when
# importing certificates into the system keychain.
#
# * Ensures CloudFlare Teams certificate is imported into the system keychain
importCloudFlareCert() {
if [ -d /Applications ] && [ -d /System ] && [ -z "$HEADLESS_INSTALL" ]; then
2023-12-06 22:23:25 -08:00
### Acquire certificate
if [ ! -f "$HOME/.local/etc/ssl/cloudflare/Cloudflare_CA.crt" ]; then
logg info 'Downloading Cloudflare_CA.crt from https://developers.cloudflare.com/cloudflare-one/static/documentation/connections/Cloudflare_CA.crt to determine if it is already in the System.keychain'
CRT_TMP="$(mktemp)"
curl -sSL https://developers.cloudflare.com/cloudflare-one/static/documentation/connections/Cloudflare_CA.crt > "$CRT_TMP"
else
CRT_TMP="$HOME/.local/etc/ssl/cloudflare/Cloudflare_CA.crt"
fi
### Validate / import certificate
security verify-cert -c "$CRT_TMP" > /dev/null 2>&1
if [ $? != 0 ]; then
logg info '**macOS Manual Security Permission** Requesting security authorization for Cloudflare trusted certificate'
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "$CRT_TMP" && logg success 'Successfully imported Cloudflare_CA.crt into System.keychain'
fi
### Remove temporary file, if necessary
if [ ! -f "$HOME/.local/etc/ssl/cloudflare/Cloudflare_CA.crt" ]; then
rm -f "$CRT_TMP"
fi
2023-12-05 18:50:29 -08:00
fi
}
2023-11-29 23:18:43 -08:00
ensureFullDiskAccess
2023-12-05 18:50:29 -08:00
importCloudFlareCert