Initial import.
authorAlexander Barton <alex@barton.de>
Mon, 28 Jul 2008 15:40:10 +0000 (17:40 +0200)
committerAlexander Barton <alex@barton.de>
Mon, 28 Jul 2008 15:40:10 +0000 (17:40 +0200)
listusers.sh [new file with mode: 0755]
useradd.macosx.sh [new file with mode: 0755]

diff --git a/listusers.sh b/listusers.sh
new file mode 100755 (executable)
index 0000000..f9f0f2f
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/bash
+
+while [ -n "$1" ]; do
+       case "$1" in
+       "--all"|"-a")
+               ALL=1
+               ;;
+       "--local"|"-l")
+               LOCAL=1
+               ;;
+       *)
+               echo "Usage: $0 [--local|-l]"
+               exit 1
+       esac
+       shift
+done
+
+sw_vers -productName | grep -i "Server" >/dev/null 2>&1
+if [ $? -eq 0 -a -z "$LOCAL" ]; then
+       # Mac OS X Server
+       OD=1
+fi
+
+if [ -n "$OD" ]; then
+       HOST="localhost"
+       USER_BASE="/LDAPv3/127.0.0.1/Users"
+       UID_MIN=1000
+else
+       HOST="."
+       USER_BASE="/Users"
+       UID_MIN=500
+fi
+
+[ -n "$ALL" ] && UID_MIN=0
+
+dscl "$HOST" -list "$USER_BASE" | while read x; do
+       declare -i id
+       id=`dscl "$HOST" -read "$USER_BASE/$x" UniqueID | cut -d: -f2`
+       if [ "$id" -gt $UID_MIN ]; then
+               echo " $id: $x"
+       fi
+done | sort -n
+
+# -eof-
diff --git a/useradd.macosx.sh b/useradd.macosx.sh
new file mode 100755 (executable)
index 0000000..518aef3
--- /dev/null
@@ -0,0 +1,187 @@
+#!/bin/bash
+#
+# useradd.macosx
+# Copyright (c)2008 Barton IT-Consulting, Alexander Barton
+#
+# 2008-07-28, alex@barton-it.de - R2
+#  - start directory services when not running (e. g. single user mode)
+# 2008-07-28, alex@barton-it.de - R1
+#  - initial version.
+#
+
+NAME=`basename $0`
+RELEASE="2"
+
+Abort() {
+       [ $# -lt 1 ] \
+               && echo "$NAME: Error detected, aborting now!" \
+               || echo "$NAME: $*"
+       exit 9
+}
+Usage() {
+       echo "$NAME (useradd.macosx.sh) release $RELEASE"
+       echo "Copyright (c)2008 Barton IT-Consulting, Alexander Barton (alex@barton-it.de)"
+       echo
+       echo "This is free software; see the source for copying conditions. There is NO"
+       echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
+       echo
+       echo "Usage: $NAME [-X] [-A] [-v] [-c <realName>] [-d <homeDir>]"
+       echo "       [-g <groupId>] [-s <shell>] [-u <userId>] <login>"
+       echo
+       echo " -A               User should become a local administrator."
+       echo " -c <realName>    Real name of the new user (aka \"comment\" field)."
+       echo " -d <homeDir>     Home directory of the user; default: '/User/<login>'."
+       echo " -g <groupId>     Group ID of the new user; default: 20 (='staff')."
+       echo " -s <shell>       Shell of the new user; default: '/bin/bash'."
+       echo " -u <userId>      User ID of the new user; default: next free UID."
+       echo " -v               Verbose mode, show more information during runtime."
+       echo " -X               Debug mode, only show what would be done."
+       echo
+       exit 1
+}
+
+# Check operating system version
+sw_vers -productName 2>/dev/null | grep "Mac OS X" >/dev/null 2>&1
+[ $? -eq 0 ] || Abort "This script requires Mac OS X!"
+epoch=`sw_vers -productVersion | cut -d'.' -f1`
+major=`sw_vers -productVersion | cut -d'.' -f2`
+if [ $epoch -ne 10 -o $major -ne 5 ]; then
+       Abort "This script requires Mac OS X 10.5.x Leopard!"
+fi
+
+# Defaults
+declare -i debug=0
+declare -i verbose=0
+declare -i user_id=-1
+declare -i group_id=20
+declare -i become_admin=0
+user_name=""
+real_name=""
+home_dir=""
+shell="/bin/bash"
+
+# Parse command line
+while [ $# -gt 0 ]; do
+       case "$1" in
+               "-A")
+                       declare -i become_admin=1
+                       ;;
+               "-c")
+                       shift
+                       [ $# -gt 0 ] || Usage
+                       real_name="$1"
+                       ;;
+               "-d")
+                       shift
+                       [ $# -gt 0 ] || Usage
+                       home_dir="$1"
+                       ;;
+               "-g")
+                       shift
+                       [ $# -gt 0 ] || Usage
+                       declare -i group_id="$1"
+                       ;;
+               "-s")
+                       shift
+                       [ $# -gt 0 ] || Usage
+                       shell="$1"
+                       ;;
+               "-u")
+                       shift
+                       [ $# -gt 0 ] || Usage
+                       declare -i user_id="$1"
+                       ;;
+               "-v")
+                       declare -i verbose=1
+                       ;;
+               "-X")
+                       declare -i debug=1
+                       ;;
+               -*)
+                       Usage
+                       ;;
+               *)
+                       [ -z "$user_name" ] || Usage
+                       user_name="$1"
+       esac
+       shift
+done
+[ -n "$user_name" ] || Usage
+[ -n "$real_name" ] || real_name="$user_name"
+[ -n "$home_dir" ] || home_dir="/Users/$user_name"
+[ -n "$shell" ] || shell="/bin/bash"
+
+# Check that directory services are running
+launchctl list 2>/dev/null | fgrep "com.apple.DirectoryServices" >/dev/null 2>&1
+if [ $? -ne 0 ]; then
+       echo "Starting directory services ..."
+       launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServicesLocal.plist || Abort
+       sleep 1
+       launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist || Abort
+       sleep 1
+else
+       [ "$verbose" -ne 0 ] && echo "Directory services already loaded, ok."
+fi
+
+# No user ID given, generate it
+if [ $user_id -lt 0 ]; then
+       # search next free user ID
+       declare -i new_id=501
+       id "$new_id" >/dev/null 2>&1
+       while [ $? -eq 0 ]; do
+               new_id=$new_id+1;
+               id "$new_id" >/dev/null 2>&1
+       done
+       user_id=$new_id
+fi
+
+# Validate user name, user id and group id
+id "$user_id" >/dev/null 2>&1
+[ $? -ne 0 ] || Abort "User id $user_id already used!"
+id "$user_name" >/dev/null 2>&1
+[ $? -ne 0 ] || Abort "User name '$user_name' already used!"
+[ -n "$user_id" -a "$user_id" -ge 1 ] || Abort "User ID must be >0!"
+[ -n "$group_id" -a "$group_id" -ge 0 ] || Abort "Group ID must be >=0!"
+
+if [ "$verbose" -ne 0 -o "$debug" -ne 0 ]; then
+       echo "uid=$user_id"
+       echo "gid=$group_id"
+       echo "user_name=$user_name"
+       echo "real_name=$real_name"
+       echo "home_dir=$home_dir"
+       echo "shell=$shell"
+       echo "become_admin=$become_admin"
+       echo
+fi
+[ "$debug" -ne 0 ] && exit 8
+
+if [ `id -u` -ne 0 ]; then
+       Abort "This script requires root privileges!"
+fi
+
+# Create directory entries
+[ "$verbose" -ne 0 ] && echo "Creating user ..."
+dscl . -create /Users/"$user_name" || Abort
+[ "$verbose" -ne 0 ] && echo "Populating user ..."
+dscl . -create /Users/"$user_name" UniqueID "$user_id" || Abort
+dscl . -create /Users/"$user_name" PrimaryGroupID "$group_id" || Abort
+dscl . -create /Users/"$user_name" NFSHomeDirectory "$home_dir" || Abort
+dscl . -create /Users/"$user_name" UserShell "$shell" || Abort
+dscl . -create /Users/"$user_name" RealName "$real_name" || Abort
+dscl . -passwd /Users/"$user_name" "maccc" || Abort
+if [ "$become_admin" -ne 0 ]; then
+       [ "$verbose" -ne 0 ] && echo "Adding user to 'admin' group ..."
+       dscl . -append /Groups/admin GroupMembership "$user_name" || Abort
+fi
+
+# Create home directory
+[ "$verbose" -ne 0 ] && echo "Creating user home directory ..."
+mkdir -p "$home_dir" || Abort
+chown "$user_id:$group_id" "$home_dir" || Abort
+chmod 755 "$home_dir" || Abort
+
+[ "$verbose" -ne 0 ] && echo
+echo "User '$user_name' ($user_id) has been created."
+exit 0
+
+# -eof-
\ No newline at end of file