#!/bin/bash # # @(#) Create a new user in OSX. V1.1 # # xaos@xm5design.com - July 14, 2010 # DSCLLIST="/tmp/dsclist.txt" DSCLLISTS="/tmp/dsclists.txt" DRURUN=0 NUSER="newuser" FIND_NEXT_UID=0 # # usage() { echo "Usage: $0 [ -l [user] | -h | -a | -d ] User_Name { UID | . } GID" echo -e "\nWhere:\n" echo -e "\t-a\t\tWill create the user as an administrator" echo -e "\t-d\t\tWill do a \"Dry Run\". It will check everything but not run any commands" echo -e "\t-h\t\tWill print this help message" echo -e "\t-l [user]\tWill list a user or all users and quit" echo -e "\nSpecifying a \".\" (dot) for a UID means find the next available one." echo -e "If the GID does not exist, the necessary commands to create it will be printed out." echo -e "A default password (made of random numbers) will be created along with the userid." } # # This only works for Darwin if [[ "$(uname -s)" != "Darwin" ]] then echo "This script will only work for OSX" exit 1 fi # # NUSER=$1 NUID=$2 NGID=$3 NADMIN=0 # # Get help ? if [[ "${NUSER}" = "-h"* || "${NUSER}" = "--h"* ]] then usage exit 1 fi # # Show a list of users? if [[ "${NUSER}" = "-l" ]] then shift MUSER=$1 if [[ "${MUSER}" == "" ]] then GENULIST=$(dscl "." list /users | sort -k 1) else GENULIST=${MUSER} fi # DSCGLIST=$(dscl . list /Groups) # for ii in ${GENULIST} do echo "${ii}" dscl . read /Users/${ii} | egrep "PrimaryGroupID|NFSHomeDirectory|RealName|UniqueID|UserShell" >${DSCLLIST} while read line do if [[ "${line}" = *PrimaryGroupID* ]] then set $line gtext=$1;gnumid=$2 foundid=0 # for kk in ${DSCGLIST} do GIDTEXT=$(dscl . read /Groups/$kk | grep PrimaryGroupID) set $GIDTEXT gtextg=$1; gnumidg=$2 # # now, compare the two if (( gnumid == gnumidg )) then foundid=1 foundname=$kk break fi done if (( foundid )) then echo -e "\tPrimaryGroupID:\n\t\tName : ${foundname}\n\t\tNumber : ${gnumid}\n\t\tExists : Yes" else echo -e "\tPrimaryGroupID:\n\t\tName : ${foundname}\n\t\tNumber : ${gnumid}\n\t\tExists : No" fi else set $line key=$1 shift keytext=$* echo -e "\t${key}\n\t\t\"${keytext}\"" fi done < ${DSCLLIST} done rm -f ${DSCLLIST} exit 0 fi # # Does he need to be admin or dry run? while : do if [[ "${NUSER}" = "-"* ]] then if [[ "${NUSER}" = "-a" ]] then NADMIN=1 elif [[ "${NUSER}" = "-d" ]] then DRYRUN=1 else echo "Error: Unknown option: \"${NUSER}\". Bye." exit fi shift NUSER=$1 NUID=$2 NGID=$3 else break fi done # # Check for no parms if [[ "${NGID}" = "" ]] then usage exit 1 fi # # Check if the user exists if dscl "." read /Users/${NUSER} >/dev/null 2>&1 then echo -e "User: \"${NUSER}\", already exists. Bye." exit 1 fi # # if [[ "${NUID}" = "." ]] then FIND_NEXT_UID=1 fi # # if (( FIND_NEXT_UID )) then # # Go through every id GENULIST=$(dscl "." list /Users | sort -k 1) rm -rf ${DSCLLIST} for ii in ${GENULIST} do dscl . read /Users/${ii} | egrep "UniqueID" >>${DSCLLIST} done cat ${DSCLLIST} | awk '{print $2}' | sort -n > ${DSCLLISTS} vim ${DSCLLISTS} NUID=$(tail -1 ${DSCLLISTS}) let NUID=$NUID+1 echo -e "New UID found: \"${NUID}\"" rm -f ${DSCLLIST} ${DSCLLISTS} fi # # Check if the UID exists GENULIST=$(dscl "." list /Users | sort -k 1) # foundid=0 for ii in ${GENULIST} do dscl . read /Users/${ii} | egrep "UniqueID" >${DSCLLIST} while read line do set $line text=$1;numid=$2 if (( numid == NUID )) then foundid=1 break fi done < ${DSCLLIST} done rm -f ${DSCLLIST} # # Check it if (( foundid )) then echo -e "UID: \"${NUID}\", already exists. Bye." exit 1 else echo -e "UID: \"${NUID}\", does not exist. Cool." fi # # Check if the GID exists GENULIST=$(dscl "." list /Groups | sort -k 1) # foundid=0 for ii in ${GENULIST} do dscl . read /Groups/${ii} | egrep "PrimaryGroupID" >${DSCLLIST} while read line do set $line text=$1;numid=$2 if (( numid == NGID )) then foundid=1 break fi done < ${DSCLLIST} done rm -f ${DSCLLIST} # # Check it if (( foundid )) then echo -e "GID: \"${NGID}\", exists. Cool." else echo -e "GID: \"${NGID}\", does not exist.\n\tYou must create with commands:" echo -e "\t\tdscl . create /Groups/${NUSER}" echo -e "\t\tdscl . create /Groups/${NUSER} name ${NUSER}" echo -e "\t\tdscl . create /Groups/${NUSER} passwd \"*\"" echo -e "\t\tdscl . create /Groups/${NUSER} gid ${NGID}" echo -e "\t\tdscl . create /Groups/${NUSER} users ${NUSER}" fi # # MYNAME="Dr. Maximvs ${NUSER}" MYPASS=$(printf "%d\n" "${RANDOM}${RANDOM}${RANDOM}") # if (( DRYRUN )) then echo -e "Preparing New Account\n\tUser: \"${NUSER}\"\n\tName: \"${MYNAME}\"\n\tUID: \"${NUID}\"\n\tGID: \"${NGID}\"\n\tPassword: \"${MYPASS}\"" exit fi # # Process request dscl "." -create /Users/${NUSER} || exit 1 # # Create and set the shell property to bash. dscl "." -create /Users/${NUSER} UserShell /bin/bash || { echo "Error Creating User Shell. Account was never created"; exit 1; } # # Create and set the user’s full name. dscl "." -create /Users/${NUSER} RealName "${MYNAME}" || { echo -e "Error Creating User Real Name. Account Will be deleted"; dscl . delete /Users/${NUSER}; exit 1; } # # Create and set the user’s ID. dscl "." -create /Users/${NUSER} UniqueID ${NUID} || { echo -e "Error Creating UID. Account Will be deleted"; dscl . delete /Users/${NUSER}; exit 1; } # # Create and set the user’s group ID property. dscl "." -create /Users/${NUSER} PrimaryGroupID ${NGID} || { echo -e "Error Creating GID. Account Will be deleted"; dscl . delete /Users/${NUSER}; exit 1; } # # Create and set the user home directory. dscl "." -create /Users/${NUSER} NFSHomeDirectory /Users/${NUSER} || { echo -e "Error Creating Home Directory. Account Will be deleted"; dscl . delete /Users/${NUSER}; exit 1; } # # Set the password. dscl "." passwd /Users/${NUSER} ${MYPASS} || { echo -e "\nError Creating User Password. Continuing"; dscl . delete /Users/${NUSER}; exit 1; } echo -e "New User created.\n\tUser: \"${NUSER}\"\n\tName: \"${MYNAME}\"\n\tDUID: \"${NUID}\"\n\tGID: \"${NGID}\"\n\tPassword: \"${MYPASS}\"" if (( NADMIN )) then # # If you would like Dr. Maximus to be able to perform administrative functions: dscl "." -append /Groups/admin GroupMembership ${NUSER} || exit 1 echo -e "\tAdministrator: \"Yes\"" else echo -e "\tAdministrator: \"No\"" fi # # Create the home dir createhomedir -c > /dev/null || exit 1 chmod 700 /Users/${NUSER} # # Now, check with Dscl echo "Now Checking with dscl:" dscl . read /Users/${NUSER} | sed 's/^/ /' # echo -e "\nTo Delete the user do:\n\tdscl \".\" delete /Users/${NUSER}" # exit 0