#! /bin/sh
#
# Copyright (c) 1997-1999 by Sun Microsystems, Inc.
# All rights reserved.
#
# This script will add a complete virtual host by hard linking a clone
# from the template dir on the same file system.  Each file
# system needs its own template setup.  Only the files needed
# and/or exist are linked.  The new anon setup will be in
# ${root_dir}/${hostname}.  The config file created for the new host
# will be put in /etc/inet/${hostname}/ftpaccess, and will be a copy
# of /etc/inet/ftpaccess.
# 
#
#ident  "@(#)ftpaddhost 1.18     99/02/24 SMI"

usage()
{
	/bin/echo `/usr/bin/gettext Usage:` `/usr/bin/basename $0` \
	    template_dir root_dir hostname '[anon_dir]' >&2
	exit 1
}


TEXTDOMAIN=ftpd
export TEXTDOMAIN

[ ${#} -lt 3 ] && usage

# List of directories to create if they exist in the template
dirlist='etc dev pub usr usr/bin usr/lib usr/lib/security usr/share
	usr/share/lib usr/share/lib/zoneinfo var var/nis
	opt opt/SUNWisp opt/SUNWisp/lib'

# List of files to link into new tree (if they exist in template)
# Note that wildcards are sh globbed within the template
filelist='./Welcome
	etc/group etc/netconfig etc/pam.conf etc/services etc/hosts
	etc/nsswitch.conf etc/passwd
	usr/bin/ls
	usr/lib/ld.so usr/lib/libdl.so.* usr/lib/libw.so.*
	usr/lib/nss_xfn.so.* usr/lib/ld.so.* usr/lib/libmp.so.*
	usr/lib/libxfn.so.* usr/lib/nss_dns.so.*
	usr/lib/libC.so.* usr/lib/libnsl.so.*
	usr/lib/nss_files.so.* usr/lib/straddr.so
	usr/lib/libc.so.* usr/lib/libsocket.so.*
	usr/lib/nss_nis.so.* usr/lib/straddr.so.*
	usr/lib/security/pam_unix.so usr/lib/security/pam_unix.so.*
	opt/SUNWisp/lib/pam_ldap.so opt/SUNWisp/lib/pam_ldap.so.*
	usr/share/lib/zoneinfo/*'

# Same but for devices.  This list is not globbed.
devlist='tcp ticlts ticots ticotsord udp zero'


# Called to create a directory
create_dir()
{
	/usr/bin/mkdir -p $1
	/usr/bin/chmod $2 $1
	/usr/bin/chown $3 $1
	/usr/bin/chgrp $4 $1
}


# Called to set up a system root.  Usage: setup_sysroot templ_dir root_dir
setup_sysroot()
{
	tmpldir=$1
	ftpdir=$2

	# Create directories
	create_dir $ftpdir 755 root bin

	for d in $dirlist ; do
	  if [ -d $tmpldir/$d ] ; then
		/usr/bin/rm -rf $ftpdir/$d >/dev/null 2>&1
		/usr/bin/mkdir $ftpdir/$d
		/usr/bin/chmod 755 $ftpdir/$d
		/usr/bin/chown root $ftpdir/$d
		/usr/bin/chgrp bin $ftpdir/$d
	  fi
	done

	# Symlink /usr/bin to /bin
	(cd $ftpdir; /usr/bin/ln -s usr/bin bin)

	# Duplicate device nodes

	for dev in $devlist ; do
	        if [ -c "$tmpldir/dev/$dev" -o -b "$tmpldir/dev/$dev" ] ; then
			dev_ls="`/usr/bin/ls -lL $tmpldir/dev/$dev | \
		            /usr/bin/sed -e 's/,//'`"
			dev_major="`/bin/echo $dev_ls | /usr/bin/awk \
			    '{print $5}'`"
			dev_minor="`/bin/echo $dev_ls | /usr/bin/awk \
			    '{print $6}'`"
			dev_type=`/bin/echo $dev_ls | /usr/bin/cut -c1`
			/usr/sbin/mknod $ftpdir/dev/$dev $dev_type $dev_major \
			    $dev_minor
			/usr/bin/chmod 666 $ftpdir/dev/$dev
		fi
	done

	# Copy files

	for fspec in $filelist ; do
		dir=`/bin/echo $fspec | /usr/bin/sed -e 's^/[a-zA-Z0-9*._]*$^^1'`
		if [ -d $tmpldir/$dir ] ; then
			f=`/usr/bin/basename $fspec`
			flist="`(cd $tmpldir/$dir; /bin/echo $f)`"
			for f in $flist ; do
				[ -r "$tmpldir/$dir/$f" ] && \
				    /usr/bin/cp -rp $tmpldir/$dir/$f \
					$ftpdir/$dir/$f
			done
		fi
	done
}

# Setup up main virtual server root
template=$1
vhroot=$2/$3
vhname=$3
anondir=$4

setup_sysroot $template $vhroot
create_dir $vhroot/var/ftp 755 root sys

# Setup anon dir within root

if [ -n "$anondir" -a ! "$anondir" = / ] ; then
  setup_sysroot $template $vhroot$anondir

  # Separate anon ftp dir on VH, so put transfer log within its space
  logpath=$vhroot/var/ftp/xferlog.data
else
  # No separate anon ftp dir on VH, so put transfer log elsewhere
  logpath=/var/ftp/xferlog.data.${vhname}
fi

# Add virtual entry to ftpservers, and copy ftpaccess file

/bin/echo $vhname /etc/inet/${vhname}/ftpaccess >>/etc/inet/ftpservers
/usr/bin/mkdir /etc/inet/${vhname} >/dev/null 2>&1
/usr/bin/cp /etc/inet/ftpaccess /etc/inet/${vhname}/ftpaccess

# Set default logfile

/bin/echo root $2/$3 >>/etc/inet/${vhname}/ftpaccess
/bin/echo logfile $logpath >>/etc/inet/${vhname}/ftpaccess
