Advertisement
Guest User

Untitled

a guest
May 12th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #!/bin/bash
  2. # Adds a user via command line, and sets the machine as "Setup Done"
  3.  
  4. # Ensure that it runs as root
  5. if [ "$(id -u)" != "0" ]; then
  6. echo "${0} must be run as root, or using sudo"
  7. exit 1
  8. fi
  9.  
  10. if [ -n "${1}" -a -n "${2}" -a -n "${3}" -a -n "${4}" ]; then
  11. echo "Using parameters from command line:"
  12. echo " USERNAME: ${1}"
  13. echo " FULLNAME: ${2}"
  14. echo " PASSWORD: ****"
  15. echo " EXTRA_GROUPS: ${4}"
  16. elif [ -n "${1}" -o -n "${2}" -o -n "${3}" -o -n "${4}" ]; then
  17. echo "Usage: ${0} [<USERNAME> <FULLNAME> <PASSWORD> <EXTRA_GROUPS>]"
  18. exit 1
  19. fi
  20.  
  21. # Lower-cases a string
  22. function to_lower { echo "${1}" | tr '[:upper:]' '[:lower:]'; }
  23.  
  24. # Required information
  25. echo "Creating a new user..."
  26. USERNAME="${1}"
  27. while [ -z "${USERNAME}" ]; do
  28. echo -n "Username: "
  29. read USERNAME
  30. USERNAME="$(to_lower "${USERNAME}")"
  31. if [ -n "${USERNAME}" -a -d "/Users/${USERNAME}" ]; then
  32. echo "Username '${USERNAME}' already exists"
  33. USERNAME=""
  34. fi
  35. done
  36.  
  37. FULLNAME="${2}"
  38. while [ -z "${FULLNAME}" ]; do
  39. echo -n "Full name: "
  40. read FULLNAME
  41. done
  42.  
  43. PASSWORD="${3}"
  44. while [ -z "${PASSWORD}" ]; do
  45. echo -n "Password: "
  46. read -s PASSWD1
  47. echo ""
  48. echo -n "Retype password: "
  49. read -s PASSWD2
  50. echo ""
  51. if [ "${PASSWD1}" == "${PASSWD2}" ]; then
  52. PASSWORD="${PASSWD1}"
  53. else
  54. echo "Passwords do not match"
  55. fi
  56. done
  57.  
  58. EXTRA_GROUPS="${4}"
  59. while [ -z "${EXTRA_GROUPS}" ]; do
  60. echo -n "Is this an administrative user? (y/n) "
  61. read ADMIN
  62. ADMIN="$(to_lower "${ADMIN}")"
  63.  
  64. if [ "${ADMIN}" == "y" ]; then
  65. EXTRA_GROUPS="admin"
  66. elif [ "${ADMIN}" == "n" ]; then
  67. EXTRA_GROUPS="staff"
  68. else
  69. echo "Please enter 'y' or 'n'"
  70. fi
  71. done
  72.  
  73. # Create a UID that is not currently in use
  74. echo "Creating an unused UID for new user..."
  75. MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
  76. if [ ${MAXID} -lt 500 ]; then MAXID=500; fi
  77. USERID=$((MAXID+1))
  78.  
  79. # Create the user account by running dscl
  80. echo "Creating necessary files..."
  81. dscl . -create /Users/${USERNAME}
  82. dscl . -create /Users/${USERNAME} UserShell /bin/bash
  83. dscl . -create /Users/${USERNAME} RealName "${FULLNAME}"
  84. dscl . -create /Users/${USERNAME} UniqueID "${USERID}"
  85. dscl . -create /Users/${USERNAME} PrimaryGroupID 20
  86. dscl . -create /Users/${USERNAME} NFSHomeDirectory /Users/${USERNAME}
  87. dscl . -passwd /Users/${USERNAME} ${PASSWORD}
  88.  
  89. # Add user to any specified groups
  90. echo "Adding user to specified groups..."
  91. for GROUP in ${EXTRA_GROUPS} ; do
  92. dseditgroup -o edit -t user -a ${USERNAME} ${GROUP}
  93. if [ "${GROUP}" == "admin" -a ! -f "/var/db/.AppleSetupDone" ]; then
  94. touch /var/db/.AppleSetupDone
  95. fi
  96. done
  97.  
  98. # Create the home directory
  99. echo "Creating home directory..."
  100. createhomedir -c 2>&1 | grep -v "shell-init"
  101.  
  102. # Patch to not prompt for iCloud
  103. PDIR="/Users/${USERNAME}/Library/Preferences"
  104. SUP="${PDIR}/com.apple.SetupAssistant"
  105. [ -d "${PDIR}" ] || { sudo -u ${USERNAME} -- mkdir -p "${PDIR}"; }
  106. defaults write "${SUP}" DidSeeCloudSetup -bool TRUE
  107. defaults write "${SUP}" GestureMovieSeen none
  108. defaults write "${SUP}" LastSeenCloudProductVersion "$(sw_vers -productVersion)"
  109. chown ${USERNAME}:staff "${SUP}.plist"
  110.  
  111. echo "Created user #${USERID}: ${USERNAME} (${FULLNAME})"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement