|
| 1 | +#!/bin/bash |
| 2 | +: <<-LICENSE_BLOCK |
| 3 | +setAutoLogin (20220731) - Copyright (c) 2021 Joel Bruner (https://github.com/brunerd) |
| 4 | +Licensed under the MIT License |
| 5 | +
|
| 6 | +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: |
| 7 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 8 | +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. |
| 9 | +LICENSE_BLOCK |
| 10 | + |
| 11 | +############# |
| 12 | +# VARIABLES # |
| 13 | +############# |
| 14 | + |
| 15 | +USERNAME="${1}" |
| 16 | + |
| 17 | +#this can be blank if that is the password |
| 18 | +PW="${2}" |
| 19 | + |
| 20 | +############# |
| 21 | +# FUNCTIONS # |
| 22 | +############# |
| 23 | + |
| 24 | +#given a string creates data for /etc/kcpassword |
| 25 | +function kcpasswordEncode () ( |
| 26 | + |
| 27 | + #ascii string |
| 28 | + thisString="${1}" |
| 29 | + |
| 30 | + #macOS cipher hex ascii representation array |
| 31 | + cipherHex_array=( 7D 89 52 23 D2 BC DD EA A3 B9 1F ) |
| 32 | + |
| 33 | + #converted to hex representation with spaces |
| 34 | + thisStringHex_array=( $(/bin/echo -n "${thisString}" | xxd -p -u | sed 's/../& /g') ) |
| 35 | + |
| 36 | + #get padding by subtraction if under 12 |
| 37 | + if [ "${#thisStringHex_array[@]}" -lt 12 ]; then |
| 38 | + padding=$(( 12 - ${#thisStringHex_array[@]} )) |
| 39 | + #get padding by subtracting remainder of modulo 12 if over 12 |
| 40 | + elif [ "$(( ${#thisStringHex_array[@]} % 12 ))" -ne 0 ]; then |
| 41 | + padding=$(( (12 - ${#thisStringHex_array[@]} % 12) )) |
| 42 | + #otherwise even multiples of 12 still need 12 padding |
| 43 | + else |
| 44 | + padding=12 |
| 45 | + fi |
| 46 | + |
| 47 | + #cycle through each element of the array + padding |
| 48 | + for ((i=0; i < $(( ${#thisStringHex_array[@]} + ${padding})); i++)); do |
| 49 | + #use modulus to loop through the cipher array elements |
| 50 | + charHex_cipher=${cipherHex_array[$(( $i % 11 ))]} |
| 51 | + |
| 52 | + #get the current hex representation element |
| 53 | + charHex=${thisStringHex_array[$i]} |
| 54 | + |
| 55 | + #use $(( shell Aritmethic )) to ^ XOR the two 0x## values (extra padding is 0x00) |
| 56 | + #take decimal value and printf convert to two char hex value |
| 57 | + #use xxd to convert hex to actual value and send to stdout (to avoid NULL issue in bash strings) |
| 58 | + printf "%02X" "$(( 0x${charHex_cipher} ^ 0x${charHex:-00} ))" | xxd -r -p > /dev/stdout |
| 59 | + done |
| 60 | +) |
| 61 | + |
| 62 | +######## |
| 63 | +# MAIN # |
| 64 | +######## |
| 65 | + |
| 66 | +#quit if not root |
| 67 | +if [ "${UID}" != 0 ]; then |
| 68 | + echo "Please run as root, exiting." |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +#special case for Guest account (case SENSITIVE) |
| 73 | +if [ "${USERNAME}" = "Guest" ]; then |
| 74 | + #turn on Guest account |
| 75 | + sysadminctl -guestAccount on |
| 76 | + #set auto-login |
| 77 | + defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser Guest |
| 78 | + |
| 79 | + echo "Auto login enabled for Guest" |
| 80 | +#if we have any other USERNAME |
| 81 | +elif [ -n "${USERNAME}" ]; then |
| 82 | + |
| 83 | + #check if user exists |
| 84 | + if ! id "${USERNAME}" &> /dev/null; then |
| 85 | + echo "User '${USERNAME}' not found, exiting." |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | + |
| 89 | + #check that the supplied password is valid |
| 90 | + if ! /usr/bin/dscl /Search -authonly "${USERNAME}" "${PW}" &> /dev/null; then |
| 91 | + echo "Invalid password for '${USERNAME}', exiting." |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + #encode password and write file |
| 96 | + kcpasswordEncode "${PW}" > /etc/kcpassword |
| 97 | + |
| 98 | + #ensure ownership and permissions are correct (600) |
| 99 | + chown root:wheel /etc/kcpassword |
| 100 | + chmod u=rw,go= /etc/kcpassword |
| 101 | + |
| 102 | + #turn on auto login for the user |
| 103 | + /usr/bin/defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser -string "${USERNAME}" |
| 104 | + echo "Auto login enabled for '${USERNAME}'" |
| 105 | + |
| 106 | +#if no USERNAME, turn auto-login OFF |
| 107 | +else |
| 108 | + [ -f /etc/kcpassword ] && rm -f /etc/kcpassword |
| 109 | + /usr/bin/defaults delete /Library/Preferences/com.apple.loginwindow autoLoginUser &> /dev/null |
| 110 | + echo "Auto login disabled" |
| 111 | +fi |
| 112 | + |
| 113 | +exit 0 |
0 commit comments