#!/bin/bash
# File: mksftpjail
# Author: JRKlotz (HP)
# Description: Utility script used to create an sftp jailed
#	environment on an RHEL-5 or RHEL-6 Linux system.
#  Note: This system must be an LDAP Client.
#-----------------------------------------------------------
# Note: The follwing ostype check is crafted to work accross
# several differnt OSs amd shells.
ostype=`uname`
if test "X$ostype" != "XLinux"
then
  echo "mksftpjail-e-, Invalid OS type ($ostype) for this script."
  exit 4
fi

mycmd=$(/bin/basename $0)
tfile=/tmp/Jail$$.tmp
tconf=/tmp/Jail$$conf.tmp
sDate=`date "+%Y%m%d%H"`

DJailDir="/sftp"
DPort=6022

ps=/bin/ps
cp=/bin/cp
mv=/bin/mv
awk=/bin/awk
cut=/bin/cut
sed=/bin/sed
grep=/bin/grep
diff=/usr/bin/diff
lsof=/usr/sbin/lsof
kill=/bin/kill
mount=/bin/mount
mkdir=/bin/mkdir
chown=/bin/chown
chmod=/bin/chmod
sleep=/bin/sleep
umount=/bin/umount
getent=/usr/bin/getent
service=/sbin/service
chkconfig=/sbin/chkconfig
ldapsearch=/usr/bin/ldapsearch

ldaphost="ldaps://ldap.tco.census.gov:636"
ldapgroup="ou=Groups, ou=Posix, o=U.S. Census Bureau, c=US"

echo " "
#
#-----------------------------------------------------------
# Definitions of locally used functions.
#-----------------------------------------------------------

# Help /Usage message.
function Usage () {
 
  if [ "$2" != "" ]; then
    echo " "
    echo "${mycmd}-f-, ${2}"
  fi 

  echo " "
  echo "Usage: ${mycmd} " 1>&2
  echo "  -j JailDir:    Root directory to be Jailled."  1>&2
  echo "  -g JailGroup:  Group that will be used to control jail access."  1>&2
  echo "  -n JailIndex:  Optional Index of the current Jail configuration (1-5)."  1>&2
  echo "  -l Logging:    Enable Logging of Jailed Activity for Jail."  1>&2
  echo "  -L NoLogging:  Disable Logging of Jailed Activity for Jail."  1>&2
  echo "  -r ReFresh:    Force the Refresh (rebuild) of an existing Jailed environment."  1>&2
  echo "  -b HomeDir:    Bind Mount /home in Jail (allows ssh key use)." 1>&2
  echo "  -B NoHomeDir:  Do Not Bind Mount /home in Jail." 1>&2
  echo "  -h Help:       Print this help message."  1>&2

  if [ -e $tfile ]; then rm ${tfile}; fi
  if [ -e $tconf ]; then rm ${tconf}; fi

  exit $1
} # end function Usage

# Parse out passed parameters
function Get_Params () {
  PreOpt=""
  while getopts ":j:g:n:rlLbBh" f
  do case $f in
    j)  JailDir=$(echo $OPTARG | ${awk} '{print tolower($1);}');;
    g)  JailGroup=$(echo $OPTARG | ${awk} '{print tolower($1);}');;
    n)  JailIdx=$(echo $OPTARG | ${awk} '{print tolower($1);}');;
    r)  Rebuild=0;;
    l)  Logging=0;;
    L)  NoLogging=0;;
    b)  HomeDir=0;;
    B)  NoHomeDir=0;;
    h)  Usage 0;;
   \?)  echo "${mycmd}-e-, unknown option \"-${OPTARG}\"." 1>&2
        Usage 1;;
   \:)  echo "${mycmd}-e-, Option \"-${OPTARG}\" requires a parameter." 1>&2
        Usage 1;;
    esac
  done
  if [[ $Logging -eq 0 && $NoLogging -eq 0 ]]; then
    Usage 1 "Logging and NoLogging are mutually exclusive."
  fi

  if [[ $HomeDir -eq 0 && NoHomeDir -eq 0 ]]; then
     Usage 1 "HomeDir and NoHomeDir are mutually exclusive."
  fi

  if [ "$JailDir" == " " ]; then JailDir=""; fi
  if [ "$JailGroup" == " " ]; then JailGroup=""; fi

  if [ "$JailIdx" == " " ]; then JailIdx=""; fi
  if [ "$JailIdx" != "" ]; then
    tmp="${JailIdx}0"
    let JailIdx=tmp/10
    if [[ $JailIdx -lt 1 || $JailIdx -gt 5 ]]; then
      Usage 1 "Invalid Jail Index Specified (none or 1-5).";
    fi
  fi

return 0;

} # end function Get_Params

# Validate passed parameters
function Check_Params () {

# Check the jail Group.

  if [[ "$JailGroup" == "" ]]; then
    Usage 1 "Invalid or No Jail Group Specified."; 
  fi
  ${ldapsrch} -b "${ldapgroup}" -s sub cn=${JailGroup} gidnumber | ${grep} -q -s -i gidnumber:
  if [ $? -ne 0 ]; then 
    Usage 1 "Specified Jail group (${JailGroup}) not defined in LDAP."; 
  fi

# Check the jail Directory.

  local tdir=$(basename ${JailDir})
  if [ "${JailDir}" != "/${tdir}" ]; then
    Usage 1 "Jail Directory must be directly off of root (/): i.e. ${DJailDir}${JailIdx} "
  else
    JailDir="/${tdir}"
  fi

  if [[ "${oJailDir}" != "" && "${oJailDir}" != "${JailDir}" ]]; then
    ${mount} | ${grep} bind | ${awk} '{print $3;}' | ${grep} -v -x "^${oJailDir}/home" | ${grep} "^${oJailDir}/"
    if [ $? -eq 0 ]; then
       Usage 2 "The listed bind mounts must be removed before changing a jailed home directory."
    fi
  fi

return 0;
} # end function Check_Params

# Function to determine if the system has a Working LDAP configuration.
function chkgrp () {
  grpchk=1
  ${grep} -q -s "^${1}:" /etc/group
  if [ $? -ne 0 ]; then
    tgrp=$(${getent} group boc)
    if [[ $? -eq 0 && "${tgrp}" != "" ]]; then
      grpchk=0
    fi
  else
    grpchk=2
  fi
  return $grpchk
} # end function chkgrp

function isldap () {
  tisldap=1
  gvalid=1
  grplst="boc,census,dir"
  idx=1
  grp=$(echo "${grplst}" | ${cut} -s -d "," -f${idx} )
  while [[ "${grp}" != "" && $tisldap -ne 0 ]]
  do
    chkgrp "${grp}"
    tisldap=$?
    if [ $tisldap -ne 2 ]; then gvalid=0; fi
    let idx=idx+1
    grp=$(echo "${grplst}" | ${cut} -s -d "," -f${idx} )
  done
  if [ $gvalid -ne 0 ]; then
    echo "${mycmd}-e-, Unable to determine if system is an LDAP client."
    exit 2
  fi
  return $tisldap
} # end isldap

# Check to see is the jailed area is already defined and
# see if a cross definiition of jailed areas is being requested.
function Check_Exists () {

    local all_exist=0
    local some_exist=1

    if [ -e /etc/ssh/${Sftpd}_config ]; then 

      cat /etc/ssh/${Sftpd}_config | ${awk} '{ cmd=tolower($1);
	if ( cmd == "port" ) 		printf "port %d\n", $2; 
        if ( cmd == "chrootdirectory" )	printf "cdir %s\n", $2;
        if ( cmd == "allowgroups" )	printf "allow %s\n", $2;
        if ( cmd == "subsystem" )	printf "Subsystem %s %s %s %s %s %s\n", $2, $3, $4, $5, $6, $7;
        }' > ${tfile}

      some_exist=0; 
      cPort=$(${grep} -e port ${tfile} | ${awk} '{print $2;}' )
      tport=$DPort
      if [ "${JailIdx}" != "" ]; then
        let tport=$tport+$JailIdx
      fi
      if [ "$cPort" != "$tport" ]; then
        all_exist=1
        echo "-w-, Currently defined Jail port (${cport}) does not match requested jail port (${tport})."
      fi

      cJailDir=$(${grep} -e cdir ${tfile} | ${awk} '{print $2;}' )
#     echo "cJailDir: ${cJailDir}, JailDir: ${JailDir}."
      if [ "${cJailDir}" != "" ]; then 
        if [ "${cJailDir}" != "$JailDir" ]; then
          if [ "${JailDir}" == "" ]; then
            JailDir="$cJailDir"
          else
            all_exist=1
            echo "${mycmd}-i-, Currently defined Jail root (${cJailDir}) does not match requested jail root (${JailDir})."
            oJailDir="${cJailDir}"
          fi
        fi
      fi

      cJailGroup=$(${grep} -e ^allow ${tfile} | ${awk} '{print $2;}' )
      if [ "${cJailGroup}" != "${JailGroup}" ]; then
        if [ "${JailGroup}" == "" ]; then
          JailGroup="${cJailGroup}"
        else
          all_exist=1
          echo "${mycmd}-i-, Currently defined Jail group (${cJailGroup}) does not match requested jail group (${JailGroup})."
          oJailGroup="${cJailGroup}"
        fi
      fi
    else 
      all_exist=1; 
    fi

    if [ "${JailDir}" == "" ]; then
      JailDir="${DJailDir}${JailIdx}"
    fi
    if [ "${cJailDir}" == "" ]; then
      cJailDir="${JailDir}"
    fi

    if [ -e /etc/pam.d/${Sftpd} ]; then some_exist=0; else all_exist=1; fi
    if [ -e /etc/sysconfig/${Sftpd} ]; then some_exist=0; else all_exist=1; fi

    ${grep} -v "#" /etc/ssh/sshd_config | ${grep} -i ^DenyGroups | ${grep} -q -s -i ${JailGroup}
    if [ $? -eq 0 ]; then some_exist=0; else all_exist=1; fi

    Subsys=$(${grep} -i -e "^Subsystem" ${tfile})
    if [ $? -eq 0 ]; then
      some_exist=0
      echo "${Subsys}" | ${grep} -i -e "\-l VERBOSE" | ${grep} -i -q -s "\-f LOCAL"
      if [[ $? -eq 0 && $NoLogging -ne 0 ]]; then Logging=0; fi
    else
      all_exist=1
    fi

    if [ $Logging -eq 0 ]; then
      ${grep} -v ^# /etc/rsyslog.conf | ${grep} -i '$AddUnixListenSocket' | ${grep} -q -s ${cJailDir}/dev/log
      rtn1=$?
      ${grep} -v ^# /etc/rsyslog.conf | ${grep} -q -s -i local6.* 
      rtn2=$?
      if [[ $rtn1 -eq 0 && $rtn2 -eq 0 ]]; then some_exist=0; else all_exist=1; fi
    fi

    if [ "${cJailDir}" != "" ]; then
      ${mount} | ${grep} bind | ${awk} '{print $3;}' | ${grep} -q -s -x "${cJailDir}/home"
      rtn1=$?
      ${grep} -v ^# /etc/fstab | ${grep} bind | ${awk} '{print $2;}' | ${grep} -q -s -x "${cJailDir}/home"
      rtn2=$?
#      echo "rtn: $rtn1 $rtn2 ${cJailDir}/home"
      if [[ ($rtn1 -eq 0 || $rtn2 -eq 0) && $NoHomeDir -ne 0 ]]; then
        HomeDir=0;
      fi
      if [ $HomeDir -eq 0 ]; then
        if [ -d ${cJailDir}/home ]; then some_exist=0; else all_exist=1; fi
        if [ $rtn1 -eq 0 ]; then some_exist=0; else all_exist=1; fi
        if [ $rtn2 -eq 0 ]; then some_exist=0; else all_exist=1; fi
      fi
    fi


  if [ $Rebuild -ne 0 ]; then
    if [ $some_exist -eq 0 ]; then

      if [ $all_exist -eq 0 ]; then
        echo "${mycmd}-i-, All required configuration for the ${Sftpd}, ${cJailDir} Jail exist."
        echo "               Use the -r qualifier to force a rebuild of the area."
        Usage 1
      fi

      if [ $some_exist -eq 0 ]; then
        echo "${mycmd}-w-, Some, but not all configuration for the ${Sftpd}, ${cJailDir} Jail exist."
        echo "               Use the -r qualifier to force a rebuild of the area."
        Usage 1
      fi


    fi # if some_exist
  fi # if Rebuild


  return 0
} # end function Check_Exists

function unmount_home () {
#echo "unmount $1"

  ${mount} | ${grep} bind | ${awk} '{print $3;}' | ${grep} -q -s -x "${1}"
  if [ $? -eq 0 ]; then
    pids=$(${lsof} ${1} | ${grep} " ${1}" | ${awk} '{printf "%s ",$2}')
    if [ "${pids}" != "" ]; then
      echo "-w-, killing users with open files on ${1}."
      ${kill} -9 ${pids}
      ${sleep} 2s
      ${umount} -f ${mpoint}
      if [ $? -ne 0 ]; then
        echo "-e-, Error forcing un-mount of ${1}."
      fi
    else
      ${umount} ${1}
      if [ $? -ne 0 ]; then
        echo "-e-, Error un-mounting ${1}."
      fi
    fi
  fi

} #end function unmount_home

function Save_File () {
  if [ -e $1 ]; then
    if [ ! -e $1.${sDate} ]; then
      ${cp} -p $1 $1.${sDate}
      return $?
    fi 
  fi
  return 0
} #end function Save_File

#end of function definition area
#-----------------------------------------------------------
# Main procedure section.
#-----------------------------------------------------------
#
# Defaults for passwd parameters.
#
JailDir=""
oJailDir=""
JailGroup=""
oJailGroup=""
JailIdx=""
Rebuild=1
Logging=1
NoLogging=1
HomeDir=1
NoHomeDir=1
Port=${DPort}


isldap
rtn=$?
if [ $rtn -ne 0 ]; then 
  echo "${mycmd}-e-, System must be an LDAP Client to use this procedure." 1>&2
  exit 1
fi

# make the ldapsearch command for local use.
if [ -x ${ldapsearch} ]; then
  boc=""
  boc=$(${ldapsearch} -x -LLL -b "${ldapgroup}" cn=boc gidnumber)
  if [[ $? -ne 0 || "${boc}" == "" ]]; then
    ldapsrch="${ldapsearch} -x -LLL -H \"${ldaphost}\" "
    boc=$(${ldapsrch} -b "${ldapgroup}" cn=boc gidnumber)
    if [[ $? -ne 0 || "${boc}" == "" ]]; then
      echo "${mycmd}-e-, OpenLDAP is not configured on this system."
      exit 5
    fi
  else
    ldapsrch="${ldapsearch} -x -LLL "
  fi
else
  echo "${mycmd}-e-, OpenLDAP commands are missing."
  exit 5
fi

# Get passwd parameters

Get_Params $*
shift $((OPTIND-1))
Get_Params $*

#
# Check for the proper syslog to be operational
#
${ps} aux | ${grep} -v grep | ${grep} -q -s -w /sbin/rsyslogd
if [ $? -ne 0 ]; then
  echo "${mycmd}-e-, The system must be configured to use rsyslogd for logging."
  exit 5
fi 

#
# Check for the proper configuration of the bindmnt startup script
#
bndmnt=1
if [ ! -x /etc/init.d/bindmnt ]; then
  echo "${mycmd}-e-, The bindmnt init.d startup script must be installed if you have gfs volumes."
else
  test -h /etc/rc3.d/S*bindmnt
  rtn=$?
  test -h /etc/rc4.d/S*bindmnt
  let rtn=$rtn+$?
  test -h /etc/rc5.d/S*bindmnt
  let rtn=$rtn+$?

  if [ $rtn -ne 0 ]; then
    echo "${mycmd}-e-, The bindmnt init.d startup script must be enabled if you have gfs volumes."
    exit 5
  fi
  bndmnt=0
fi

# Build some of the basics based on the passed parameters

Sftpd="sftp${JailIdx}d"
if [ "${JailIdx}" != "" ]; then
  let Port=$Port+$JailIdx
fi

#echo "JailDir: ${JailDir}, Sftpd: ${Sftpd}, JailGroup: ${JailGroup}, Rebuild: ${Rebuild}, Logging: ${Logging}, HomeDir: ${HomeDir}"

# See if the area already exists and fill in old params for the defaults.
Check_Exists

#echo "JailDir: ${JailDir}, Sftpd: ${Sftpd}, JailGroup: ${JailGroup}, Rebuild: ${Rebuild}, Logging: ${Logging}, HomeDir: ${HomeDir}"

# Finsih validating the parameters.

Check_Params

#echo "JailDir: ${JailDir}, Sftpd: ${Sftpd}, JailGroup: ${JailGroup}, Rebuild: ${Rebuild}, Logging: ${Logging}, HomeDir: ${HomeDir}"

#
#---------------------------------------------------------
# Lets make an sftp Jailed sub-system
#---------------------------------------------------------
# Build the configuration files.
echo "${mycmd}-i-, Creating configuration for Jailed environment ${Sftpd} on ${JailDir}."

# Validate/Create the directory structure

if [ -e ${JailDir} ]; then
  if [ ! -d ${JailDir} ]; then 
    Usage 1 "A file of the same name as the Jail directory exists (${JailDir})."
  else
    if [ $Logging -eq 0 ]; then
      ${mount} | ${awk} '{print $3;}' | ${grep} -q -s -x ${JailDir}
      if [ $? -eq 0 ]; then 
        Usage 1 "The Jail directory (${JailDir}) cannot be a mount point when logging."
      fi
    fi
  fi
else
  ${mkdir} ${JailDir}
fi
${chown} root:root ${JailDir}
${chmod} 2755 ${JailDir}


# Create the startup script from the sshd startup script.
# Also create related files and directories.
 
Save_File /etc/init.d/${Sftpd}
SFTPD=$(echo "$Sftpd" | ${awk} '{print toupper($1);}')
${sed} -e "s_SSHD_${SFTPD}_g" /etc/init.d/sshd | ${sed} -e "s_sshd_${Sftpd}_g" | ${sed} -e "s_/usr/sbin_/usr/local/bin_g" > /etc/init.d/${Sftpd}
${chmod} 755 /etc/init.d/${Sftpd}

Save_File /etc/sysconfig/${Sftpd}
echo "OPTIONS=\"-f /etc/ssh/${Sftpd}_config\"" > /etc/sysconfig/${Sftpd}

if [ ! -d /var/empty/${Sftpd}/etc ]; then ${mkdir} -p /var/empty/${Sftpd}/etc; fi
${chmod} 755 /var/empty/${Sftpd} /var/empty/${Sftpd}/etc

if [ -h /usr/local/bin/${Sftpd} ]; then rm /usr/local/bin/${Sftpd}; fi
ln -s /usr/sbin/sshd /usr/local/bin/${Sftpd}

# Create the sftpd configuration file.

Save_File /etc/ssh/${Sftpd}_config 
Logg=""
if [ $Logging -eq 0 ]; then Logg=" -l VERBOSE -f LOCAL6"; fi
${grep} -v -i -e PidFile -e Subsystem -e ForceCommand -e AllowGroups -e ChrootDirectory -e DenyGroups /etc/ssh/sshd_config > ${tconf}
${sed} -e "/^Port/ c Port ${Port}\ " ${tconf} > /etc/ssh/${Sftpd}_config
echo "PidFile /var/run/${Sftpd}.pid"		>> /etc/ssh/${Sftpd}_config
echo "Subsystem	sftp	internal-sftp ${Logg}"	>> /etc/ssh/${Sftpd}_config
echo "AllowGroups ${JailGroup}"			>> /etc/ssh/${Sftpd}_config
echo "ChrootDirectory ${JailDir}"		>> /etc/ssh/${Sftpd}_config
${chown} root:root /etc/ssh/${Sftpd}_config
${chmod} 600 /etc/ssh/${Sftpd}_config
${chkconfig} --del ${Sftpd}
${chkconfig} --add ${Sftpd}

# Create the sftpd pam.d file.

Save_File /etc/pam.d/${Sftpd}
echo "auth		include	sshd" 	>  /etc/pam.d/${Sftpd}
echo "account		include sshd"	>> /etc/pam.d/${Sftpd}
echo "password	include sshd"	>> /etc/pam.d/${Sftpd}
echo "session		include sshd"   >> /etc/pam.d/${Sftpd}
${chown} root:root /etc/pam.d/${Sftpd}
${chmod} 644 /etc/pam.d/${Sftpd}

# Update the sshd_config file for the jailed users.

Save_File /etc/ssh/sshd_config
Deny=$(cat /etc/ssh/sshd_config | ${awk} '{if ( tolower($1) == "denygroups") printf "%s %s %s %s %s %s %s", $2, $3, $4, $5, $6, $7, $8;}') 
if [ "${Deny}" != "" ]; then
  idx=1
  tDeny="DenyGroups"
  tmp=$(echo "${Deny}" | cut -d " " -f ${idx})
  while [ "${tmp}" != "" ]; do
    if [ "${oJailGroup}" != "" ]; then
      echo "${tmp}" | ${grep} -i -q -s -x "${oJailGroup}"
      if [ $? -ne 0 ]; then
        tDeny="${tDeny} ${tmp}"
      fi
    else
      tDeny="${tDeny} ${tmp}"
    fi
    let idx=idx+1
    tmp=$(echo "${Deny}" | cut -d " " -f $idx)
  done
  Deny="${tDeny}"
  echo "${Deny}" | ${grep} -i -q -s -w "${JailGroup}"
  if [ $? -ne 0 ]; then
    Deny="${Deny} ${JailGroup}"
  fi

  ${grep} -i -v "denygroups" /etc/ssh/sshd_config > ${tconf}
  echo "${Deny}" >> ${tconf}
  ${mv} ${tconf} /etc/ssh/sshd_config
  ${chmod} 600 /etc/ssh/sshd_config
else
  echo "DenyGroups ${JailGroup}" >> /etc/ssh/sshd_config
fi

# Configure (or un-configure) home directories as requested.

if [ "${oJailDir}" == "" ]; then oJailDir=${JailDir}; fi
if [[ $HomeDir -eq 0 || $NoHomeDir -eq 0 ]]; then
  Save_File /etc/fstab
#  echo "oJailDir: ${oJailDir}."
  ${grep} -v -e ${oJailDir}/home -e ${JailDir}/home /etc/fstab > ${tconf}
  if [[ $NoHomeDir -eq 0 || "${oJailDir}" != "${JailDir}" ]]; then
    unmount_home ${oJailDir}/home
  fi
  if [ $HomeDir -eq 0 ]; then
    ${grep} -q -s -e "Place bind mounts after this point." ${tconf}
    if [ $? -ne 0 ]; then
      echo "# " >> ${tconf}
      echo "# Place bind mounts after this point." >> ${tconf}
      echo "# " >> ${tconf}
    fi
    if [ $bndmnt -eq 0 ]; then
      echo "/home			${JailDir}/home		none	bind	0 0" >> ${tconf}
    else
      echo "/home			${JailDir}/home		none	bind,noauto	0 0" >> ${tconf}
    fi
    if [ ! -d ${JailDir}/home ]; then
      mkdir -m 755 ${JailDir}/home
    fi 
  fi
  ${diff} --brief -b -q /etc/fstab ${tconf} > /dev/null
  if [ $? -ne 0 ]; then
    ${cp} ${tconf} /etc/fstab
    ${chmod} 600 /etc/fstab
    ${chown} root:root /etc/fstab
  fi
fi

# Configure (or un-configure) logging as requested

if [[ $Logging -eq 0 || $NoLogging -eq 0 ]]; then 
  Save_File /etc/rsyslog.conf
  ${grep} -v ${oJailDir}/dev/log /etc/rsyslog.conf > ${tconf}
  if [ $Logging -eq 0 ]; then
    ${grep} -i -q -s "local6\.\*" ${tconf}
    if [ $? -ne 0 ]; then
      echo "local6.* /var/log/jail-sftp.log" >> ${tconf}
    fi
    ${grep} -v "^#" ${tconf} | ${grep} -i "\$AddUnixListenSocket" | ${grep} -i -q -s "${JailDir}/dev/log"
    if [ $? -ne 0 ]; then
      echo "\$AddUnixListenSocket ${JailDir}/dev/log" >> ${tconf}
    fi
    if [ ! -d ${JailDir}/dev ]; then
      ${mkdir} -m 755 ${JailDir}/dev
    fi
  fi
  ${diff} --brief -b -q /etc/rsyslog.conf ${tconf} > /dev/null
  if [ $? -ne 0 ]; then
    ${cp} ${tconf} /etc/rsyslog.conf
    ${chmod} 600 /etc/rsyslog.conf
    ${chown} root:root /etc/rsyslog.conf
  fi 
fi


#---------------------------------------------------------
# Startup the jailed subsystem
#---------------------------------------------------------
echo "${mycmd}-i-, Starting up Jailed environment ${Sftpd} on ${JailDir}."

if [[ $Logging -eq 0 || $NoLogging -eq 0 ]]; then
  ${service} rsyslog restart
fi

${service} sshd restart

${ps} aux | ${grep} -v grep | ${grep} -q -s -w /usr/local/bin/${Sftpd}
if [ $? -eq 0 ]; then
  ${service} ${Sftpd} restart
else
  ${service} ${Sftpd} start
fi

if [[ $HomeDir -eq 0 ]]; then
  ${mount} | ${awk} '{print $3 " " $6;}' | grep -e bind | grep -q -s -w -e "^${JailDir}/home "
  if [ $? -ne 0 ]; then
    ${mount} ${JailDir}/home
  fi
fi  

#---------------------------------------------------------
# Cleanup and end
#---------------------------------------------------------

if [ -e $tfile ]; then rm ${tfile}; fi
if [ -e $tconf ]; then rm ${tconf}; fi

exit 0
