4 # Copyright (c)2008,2009 Barton IT-Consulting, Alexander Barton
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.
13 # See the GNU General Public License for more details.
21 && echo "$NAME: Error detected, aborting now!" \
26 echo "$NAME (useradd.macosx.sh) release $RELEASE"
27 echo "Copyright (c)2008,2009 Barton IT-Consulting, Alex Barton (alex@barton-it.de)"
29 echo "This is free software; see the source for copying conditions. There is NO"
30 echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
32 echo "Usage: $NAME [-X] [-A] [-v] [-c <realName>] [-d <homeDir>]"
33 echo " [-g <groupId>] [-s <shell>] [-u <userId>] <login>"
35 echo " -A User should become a local administrator."
36 echo " -c <realName> Real name of the new user (aka \"comment\" field)."
37 echo " -d <homeDir> Home directory of the user; default: '/User/<login>'."
38 echo " -g <groupId> Group ID of the new user; default: 20 (='staff')."
39 echo " -s <shell> Shell of the new user; default: '/bin/bash'."
40 echo " -u <userId> User ID of the new user; default: next free UID."
41 echo " -v Verbose mode, show more information during runtime."
42 echo " -X Debug mode, only show what would be done."
47 # Check operating system version
48 sw_vers -productName 2>/dev/null | grep "Mac OS X" >/dev/null 2>&1
49 [ $? -eq 0 ] || Abort "This script requires Mac OS X!"
50 epoch=`sw_vers -productVersion | cut -d'.' -f1`
51 major=`sw_vers -productVersion | cut -d'.' -f2`
52 if [ $epoch -ne 10 -o $major -lt 5 -o $major -gt 6 ]; then
53 Abort "This script requires Mac OS X 10.5.x or 10.6.x!"
60 declare -i group_id=20
61 declare -i become_admin=0
68 while [ $# -gt 0 ]; do
71 declare -i become_admin=1
86 declare -i group_id="$1"
96 declare -i user_id="$1"
108 [ -z "$user_name" ] || Usage
113 [ -n "$user_name" ] || Usage
114 [ -n "$real_name" ] || real_name="$user_name"
115 [ -n "$home_dir" ] || home_dir="/Users/$user_name"
116 [ -n "$shell" ] || shell="/bin/bash"
118 [ "$UID" -eq 0 ] || Abort "This script must be run as root!"
120 # Check that directory services are running
121 launchctl list 2>/dev/null | fgrep "com.apple.DirectoryServices" >/dev/null 2>&1
122 if [ $? -ne 0 ]; then
123 echo "Starting directory services ..."
124 launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServicesLocal.plist || Abort
126 launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist || Abort
129 [ "$verbose" -ne 0 ] && echo "Directory services already loaded, ok."
132 # No user ID given, generate it
133 if [ $user_id -lt 0 ]; then
134 # search next free user ID
135 user_id=`echo $[$(dscl . -list /Users uid | awk '{print $2}' \
136 | sort -n | tail -n1)+1]`
139 # Validate user name, user id and group id
140 id "$user_id" >/dev/null 2>&1
141 [ $? -ne 0 ] || Abort "User id $user_id already used!"
142 id "$user_name" >/dev/null 2>&1
143 [ $? -ne 0 ] || Abort "User name '$user_name' already used!"
144 [ -n "$user_id" -a "$user_id" -ge 1 ] || Abort "User ID must be >0!"
145 [ -n "$group_id" -a "$group_id" -ge 0 ] || Abort "Group ID must be >=0!"
147 if [ "$verbose" -ne 0 -o "$debug" -ne 0 ]; then
150 echo "user_name=$user_name"
151 echo "real_name=$real_name"
152 echo "home_dir=$home_dir"
154 echo "become_admin=$become_admin"
157 [ "$debug" -ne 0 ] && exit 8
159 if [ `id -u` -ne 0 ]; then
160 Abort "This script requires root privileges!"
163 # Create directory entries
164 [ "$verbose" -ne 0 ] && echo "Creating user ..."
165 dscl . -create /Users/"$user_name" || Abort
166 [ "$verbose" -ne 0 ] && echo "Populating user ..."
167 dscl . -create /Users/"$user_name" UniqueID "$user_id" || Abort
168 dscl . -create /Users/"$user_name" PrimaryGroupID "$group_id" || Abort
169 dscl . -create /Users/"$user_name" NFSHomeDirectory "$home_dir" || Abort
170 dscl . -create /Users/"$user_name" UserShell "$shell" || Abort
171 dscl . -create /Users/"$user_name" RealName "$real_name" || Abort
172 #dscl . -passwd /Users/"$user_name" "maccc" || Abort
173 if [ "$become_admin" -ne 0 ]; then
174 [ "$verbose" -ne 0 ] && echo "Adding user to 'admin' group ..."
175 dscl . -append /Groups/admin GroupMembership "$user_name" || Abort
178 # Create home directory
179 [ "$verbose" -ne 0 ] && echo "Creating user home directory ..."
180 mkdir -p "$home_dir" || Abort
181 chown "$user_id:$group_id" "$home_dir" || Abort
182 chmod 755 "$home_dir" || Abort
184 [ "$verbose" -ne 0 ] && echo
185 echo "User '$user_name' ($user_id) has been created."