#!/bin/bash -e # # Create home directories for specified users who have UIDs above a certain # value. The default value is 500, as set in "/etc/login.defs". Either # pass a list of user names in on the command line, or run with "--all" to # create home directories for all known users. # # if test $# -lt 1 ; then echo Usage: `basename $0` '[ -m 500 ] [ --minimum-uid=500 ] [ user ... | "-a" | "--all" ]' exit 1 fi minuid=`grep '^UID_MIN[ ]' /etc/login.defs | awk '{print $NF}'` minuid=${minuid:-500} users= all=false testmode=false while test -n "$1" ; do case "$1" in -a|--all) all=true;; -m[0-9]*) minuid=`echo $1 | cut -c3-`;; --minimum-uid=[0-9]*) minuid=`echo $1 | cut -f2 -d=`;; -m|--minimum-uid) shift; minuid=$1;; -t|--test) testmode=true;; *) users="$users $1";; esac shift done mkonehomedir() { if ! test -d "$homedir" ; then echo "$user" if $testmode ; then continue fi if mkdir -pm700 "$homedir" ; then if pushd /etc/skel > /dev/null ; then find . | sort -r | cpio --quiet -pmd "$homedir" popd > /dev/null fi chown -R "$uid":"$gid" "$homedir" chmod 700 "$homedir" find "$homedir" -print0 | xargs -0 -r -n1 restorecon groupwrite=false if test "$uid" -eq "$gid" ; then if test `id -un "$user"` = `id -gn "$user"` ; then groupwrite=true fi fi if $groupwrite ; then chmod 775 "$homedir" else chmod 755 "$homedir" fi # Hey, why not? if test -d /etc/mkhomedir.d ; then for script in /etc/mkhomedir.d/* ; do if test -x $script ; then $script "$user" "$homedir" fi done fi fi fi } mkhomedir() { oldIFS="$IFS" IFS=: while read user passwd uid gid gecos homedir shell ; do if test "$uid" -ge "$minuid" ; then if ! echo "$user" | egrep -q '(_|\$$)' ; then mkonehomedir fi fi done IFS="$oldIFS" } if $all ; then getent passwd | grep -v '^+' | sed -e 's,\\,\\\\,g' | mkhomedir else getent passwd $users | sed -e 's,\\,\\\\,g' | mkhomedir fi