Automatic home directory creation

When accessing a Solaris system with an externally authenticated user (ie NIS, Active Directory or LDAP) you often need to create a users home directory prior to the first login or you end up with an error.

The Solaris automounter has a feature called executable maps. This means that whenever a directory on the moint point is accessed a script will be executed. The script returns the destination on stdout but as it is a shell script it allows us to customise it's usage so that we can create a home directory and copy a users profile into place.

  1. Add the following lines to /etc/auto_master:
    # Automount (and create if not exist) home dirs for external users
    /home /etc/auto_homedir.sh
    This essentially tells the automounter to execute script /etc/auto_homedir.sh whenever someone accesses directories on /home. (you can specify any location).
  2. Create the /etc/auto_homedir.sh script (downloadable from here):
    #!/bin/sh
    # @(#) automounter executable script.
    # @(#) $Id: auto_homedir.sh,v 1.3 2010/05/20 21:06:35 mchurchi Exp $
    # ----------------------------------------------------------------------
    # Create a users home directory and copy system profiles in place if
    # they do not exit.
    #
    # This script must be referenced in /etc/auto_master in order to have
    # any effect. For example:
    #
    # /home /etc/auto_homedir.sh
    #
    # Furthermore, it must have the sticky bit set:
    #
    # chmod +t+x /etc/auto_homedir.sh
    #
    # Prereq:
    # Solaris 10 requires patches 147774-01 (sparc) 147775-01 (x86)
    # 7085850 automounter fails to execute executable automounter maps
    #
    # Input:
    # This script receives an arg $1 which is the name of the object
    # (directory) that is being accessed under the moint point.
    #
    # Output:
    # Returns the path of the physical home dir on sdtout, ie:
    # localhost:johndoe
    # ----------------------------------------------------------------------

    # ----------------------------------------------------------------------
    # Set following vars to match your environment
    #
    MNTDIR=/home ; # Path of your mount point
    PHYSDIR=/export/home ; # Location of the physical user home directory
    USERGRP="staff" ; # The group name to give to the user's home dir
    #
    # NOTE: MNTDIR must match the first column in /etc/auto_master file.

    # ----------------------------------------------------------------------
    # Check if user who is logging in exists in passwd name service
    getent passwd $1 >/dev/null
    if [ $? -ne 0 ]
    then
    exit
    fi

    # ----------------------------------------------------------------------
    # Now we know that $1 is a valid user set home directory
    HDIR="${MNTDIR}/$1" ; # Mount point to home dir
    PDIR="${PHYSDIR}/$1" ; # Physical patch to home dir

    # ----------------------------------------------------------------------
    # Next see if the user's physical home dir exist. If not create it.
    if [ ! -d "$PDIR" ]
    then
    # Create the physical home directory
    mkdir -p "${PDIR}"

    # Copy system profiles
    cp -r /etc/skel "${PDIR}/"

    # Set owner/group
    chown -R "$1":"$USERGRP" "${PDIR}"
    fi

    # ----------------------------------------------------------------------
    # Return the path of the physical home dir to the automounter and exit.
    echo "localhost:$PDIR"
    exit
  3. Set sticky bit on the script
    # chmod +t+x /etc/auto_homedir
  4. Restart the automounter
    # svcadm restart autofs