-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsetAutoLogin.jamf.sh
122 lines (97 loc) · 4.6 KB
/
setAutoLogin.jamf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
: <<-LICENSE_BLOCK
setAutoLogin.jamf (20220731) - Copyright (c) 2021 Joel Bruner (https://github.com/brunerd)
Licensed under the MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LICENSE_BLOCK
#############
# VARIABLES #
#############
#JAMF script parameters are shifted +3
#provide a username, if blank will disable autologin
USERNAME="${4}"
#this can be blank if that is the password, it will be verified
PW="${5}"
#############
# FUNCTIONS #
#############
function jamflog {
local logFile="${2:-/var/log/jamf.log}"
#if we cannot write to the log or it does not exist, unset and tee simply echoes
[ ! -w "${logFile}" ] && unset logFile
#this will tee to jamf.log in the jamf log format: <Day> <Month> DD HH:MM:SS <Computer Name> ProcessName[PID]: <Message>
echo "$(date +'%a %b %d %H:%M:%S') ${myComputerName:="$(scutil --get ComputerName)"} ${myName:="$(basename "${0}" | sed 's/\..*$//')"}[${myPID:=$$}]: ${1}" | tee -a "${logFile}" 2>/dev/null
}
#given a string creates data for /etc/kcpassword
function kcpasswordEncode () (
#ascii string
thisString="${1}"
#macOS cipher hex ascii representation array
cipherHex_array=( 7D 89 52 23 D2 BC DD EA A3 B9 1F )
#converted to hex representation with spaces
thisStringHex_array=( $(/bin/echo -n "${thisString}" | xxd -p -u | sed 's/../& /g') )
#get padding by subtraction if under 12
if [ "${#thisStringHex_array[@]}" -lt 12 ]; then
padding=$(( 12 - ${#thisStringHex_array[@]} ))
#get padding by subtracting remainder of modulo 12 if over 12
elif [ "$(( ${#thisStringHex_array[@]} % 12 ))" -ne 0 ]; then
padding=$(( (12 - ${#thisStringHex_array[@]} % 12) ))
#otherwise even multiples of 12 still need 12 padding
else
padding=12
fi
#cycle through each element of the array + padding
for ((i=0; i < $(( ${#thisStringHex_array[@]} + ${padding})); i++)); do
#use modulus to loop through the cipher array elements
charHex_cipher=${cipherHex_array[$(( $i % 11 ))]}
#get the current hex representation element
charHex=${thisStringHex_array[$i]}
#use $(( shell Aritmethic )) to ^ XOR the two 0x## values (extra padding is 0x00)
#take decimal value and printf convert to two char hex value
#use xxd to convert hex to actual value and send to stdout (to avoid NULL issue in bash strings)
printf "%02X" "$(( 0x${charHex_cipher} ^ 0x${charHex:-00} ))" | xxd -r -p > /dev/stdout
done
)
########
# MAIN #
########
#quit if not root
if [ "${UID}" != 0 ]; then
jamflog "Please run as root, exiting."
exit 1
fi
#special case for Guest account (case SENSITIVE)
if [ "${USERNAME}" = "Guest" ]; then
#turn on Guest account
sysadminctl -guestAccount on
#set auto-login
defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser Guest
echo "Auto login enabled for Guest"
#if we have any other USERNAME
elif [ -n "${USERNAME}" ]; then
#check user
if ! id "${USERNAME}" &> /dev/null; then
jamflog "User '${USERNAME}' not found, exiting."
exit 1
fi
if ! /usr/bin/dscl /Search -authonly "${USERNAME}" "${PW}" &> /dev/null; then
jamflog "Invalid password for '${USERNAME}', exiting."
exit 1
fi
#encode password and write directly to file
kcpasswordEncode "${PW}" > /etc/kcpassword
#ensure ownership and permissions (600)
chown root:wheel /etc/kcpassword
chmod u=rw,go= /etc/kcpassword
#turn on auto login
/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser -string "${USERNAME}"
jamflog "Auto login enabled for '${USERNAME}'"
#if USERNAME empty, turn OFF
else
[ -f /etc/kcpassword ] && rm -f /etc/kcpassword
/usr/bin/defaults delete /Library/Preferences/com.apple.loginwindow autoLoginUser &> /dev/null
jamflog "Auto login disabled"
fi
exit 0