#!/bin/bash # # Purpose : Configuration profiles (.mobileconfig) for configure.d items. # Source this file; it uses wait_until from configure. # # A configuration profile is how macOS itself sets up Mail, Calendar and # Contacts accounts: each account is a payload in the profile, and installing # the profile adds the accounts, with their passwords in the keychain. # Removing the profile removes them again. # # Since macOS 11 only you can install a profile (profiles install no longer # works without device management), so profile_install opens it, which puts # it under Downloaded in System Settings > Profiles, and waits until you've # installed it there. A profile with the same PayloadIdentifier as one that's # installed replaces it, and payloads keep their PayloadUUID between versions, # so the accounts are changed rather than added again. PROFILES_PANE="x-apple.systempreferences:com.apple.Profiles-Settings.extension" xml_escape() { # sed, not ${s//&/\&}: bash 3.2 keeps that backslash printf '%s' "$1" | sed -e 's/&/\&/g' -e 's//\>/g' \ -e 's/"/\"/g' } # The same UUID each time for the same name, so a payload is the same account # in each version of the profile stable_uuid() { md5 -q -s "$1" | tr a-f A-F \ | sed -E 's/^(.{8})(.{4})(.{4})(.{4})(.{12})$/\1-\2-\3-\4-\5/' } # The installed configuration profiles, as XML. The form of the command # changed in macOS 10.13; the old one still works on some versions. profiles_xml() { profiles show -type configuration -output stdout-xml 2> /dev/null \ || profiles -P -o stdout-xml 2> /dev/null } # profile_installed [uuid]: with a UUID, only that version profile_installed() { local xml xml="$(profiles_xml)" || return 1 grep -qF "$1" <<< "$xml" || return 1 [ -z "$2" ] || grep -qF "$2" <<< "$xml" } # profile_install # Opens the profile and waits for you to install it in System Settings. The # file has passwords in it, so the caller deletes it afterwards either way. profile_install() { local file="$1" id="$2" uuid="$3" name="$4" open "$file" || return 1 sleep 1 open "$PROFILES_PANE" 2> /dev/null echo echo "System Settings shows \"$name\" under Downloaded (in Privacy & Security >" echo "Profiles, or General > Device Management). Double-click it, choose Install," echo "and enter your Mac password when asked." if wait_until 480 profile_installed "$id" "$uuid"; then echo "Installed \"$name\"." return 0 fi echo "\"$name\" wasn't installed. macOS forgets a downloaded profile after a few" echo "minutes; to try again, run this again." return 1 } # Removing a profile you installed yourself works from the command line on # most versions of macOS; if it doesn't, you remove it in System Settings. profile_remove() { local id="$1" name="$2" profile_installed "$id" || return 0 profiles remove -identifier "$id" > /dev/null 2>&1 \ || profiles -R -p "$id" > /dev/null 2>&1 profile_installed "$id" || return 0 open "$PROFILES_PANE" 2> /dev/null echo "In System Settings > Profiles, choose \"$name\", then the - button to remove it." wait_until 300 not_installed "$id" } not_installed() { ! profile_installed "$1" }