| 1 | #!/bin/bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | export PATH=/usr/bin:/bin:/usr/sbin:/sbin |
| 5 | [[ $# == 2 && $EUID == 0 && $(uname -sm) == 'Darwin arm64' && $(sysctl -n hw.model) =~ ^VirtualMac[0-9]+,[0-9]+$ ]] |
| 6 | user=$1 |
| 7 | installation=$2 |
| 8 | [[ $user =~ ^[a-z][a-z0-9_]{0,30}$ && $user != root && $user != cageprovision && $user != cagesetup ]] |
| 9 | [[ -f /var/db/cage-guest-os && ! -L /var/db/cage-guest-os && $(stat -f %u /var/db/cage-guest-os) == 0 && $(cat /var/db/cage-guest-os) == macos ]] |
| 10 | receipt=/var/db/cage-initial-setup/account.json |
| 11 | [[ -f $receipt && ! -L $receipt && $(stat -f %u "$receipt") == 0 && $(plutil -extract installation raw "$receipt") == "$installation" ]] |
| 12 | bound_user=$(plutil -extract user raw "$receipt" 2>/dev/null || echo cage) |
| 13 | [[ $bound_user == "$user" ]] |
| 14 | phase=$(plutil -extract state raw "$receipt") |
| 15 | [[ $phase == ready ]] |
| 16 | [[ $(dscl . -read /Users/$user NFSHomeDirectory) == "NFSHomeDirectory: /Users/$user" && " $(id -Gn "$user") " != *' admin '* ]] |
| 17 | IFS= read -r password |
| 18 | [[ -n $password ]] |
| 19 | printf '%s\n' "$password" | dscl /Local/Default -authonly "$user" >/dev/null |
| 20 | |
| 21 | if fdesetup status | grep -q 'FileVault is On'; then |
| 22 | echo 'FileVault 已启用,请使用客机密码登录。' >&2 |
| 23 | exit 1 |
| 24 | fi |
| 25 | existing=$(defaults read /Library/Preferences/com.apple.loginwindow autoLoginUser 2>/dev/null || true) |
| 26 | [[ -z $existing || $existing == "$user" ]] |
| 27 | umask 077 |
| 28 | tmp=$(mktemp /etc/cage-autologin.XXXXXXXX) |
| 29 | trap 'rm -f "$tmp"' EXIT |
| 30 | |
| 31 | |
| 32 | printf '%s\n' "$password" | /usr/bin/perl -e ' |
| 33 | use strict; use warnings; |
| 34 | my $password = <STDIN>; chomp $password; |
| 35 | my @key = (0x7d,0x89,0x52,0x23,0xd2,0xbc,0xdd,0xea,0xa3,0xb9,0x1f); |
| 36 | my $plain = $password . "\0"; |
| 37 | $plain .= "\0" while length($plain) % 12; |
| 38 | print pack("C*", map { ord(substr($plain,$_,1)) ^ $key[$_ % @key] } 0..length($plain)-1); |
| 39 | ' > "$tmp" |
| 40 | unset password |
| 41 | [[ ! -L /etc/kcpassword ]] |
| 42 | chmod 600 "$tmp" |
| 43 | mv -f "$tmp" /etc/kcpassword |
| 44 | defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser -string "$user" |
| 45 | defaults write /Library/Preferences/com.apple.loginwindow autoLoginUserUID -int "$(id -u "$user")" |
| 46 | |
| 47 | |
| 48 | provision=cageprovision |
| 49 | source=/Users/$provision/Library/Preferences/com.apple.SetupAssistant.plist |
| 50 | if [[ -f $source && ! -L $source ]]; then |
| 51 | cat "$source" | sudo -Hu "$user" /bin/bash -ec ' |
| 52 | umask 077 |
| 53 | mkdir -p "$HOME/Library/Preferences" |
| 54 | target="$HOME/Library/Preferences/com.apple.SetupAssistant.plist" |
| 55 | [[ ! -L $target ]] |
| 56 | cat > "$target" |
| 57 | plutil -lint "$target" >/dev/null |
| 58 | ' |
| 59 | fi |
| 60 | |
| 61 | sudo -Hu "$user" /bin/bash -ec ' |
| 62 | domain=com.apple.SetupAssistant |
| 63 | version=$(sw_vers -productVersion) |
| 64 | build=$(sw_vers -buildVersion) |
| 65 | for key in DidSeeAccessibility DidSeeActivationLock DidSeeAppStore DidSeeAppearanceSetup DidSeeApplePaySetup DidSeeCloudSetup DidSeeLockdownMode DidSeePrivacy DidSeeScreenTime DidSeeSetupSequence DidSeeSiriSetup DidSeeSyncSetup DidSeeSyncSetup2 DidSeeTermsOfAddress DidSeeTouchIDSetup DidSeeiCloudLoginForStorageServices SkipExpressSettingsUpdating SkipFirstLoginOptimization; do |
| 66 | defaults write "$domain" "$key" -bool true |
| 67 | done |
| 68 | for key in LastPreLoginTasksPerformedBuild LastSeenBuddyBuildVersion; do defaults write "$domain" "$key" -string "$build"; done |
| 69 | for key in LastPreLoginTasksPerformedVersion LastSeenAgeRangeSelectionProductVersion LastSeenCloudProductVersion LastSeenDiagnosticsProductVersion; do defaults write "$domain" "$key" -string "$version"; done |
| 70 | defaults write "$domain" MiniBuddyLaunchReason -int 0 |
| 71 | defaults write "$domain" MiniBuddyShouldLaunchToResumeSetup -bool false |
| 72 | defaults -currentHost write com.apple.screensaver idleTime -int 0 |
| 73 | ' |
| 74 | |
| 75 | |
| 76 | pmset -a sleep 0 displaysleep 0 |
| 77 | echo '客机自动登录与首次设置已准备。' |