#!/bin/sh
#
#    mkftp - make anonymous/restricted FTP directory hierarchy
#
#  8-aug-1995 jrv add echo stmts and case stmt for OS-specific stuff
#  5-jun-1995 jrv initial release (John_Vanderpool@gsfc.nasa.gov)

# this should work on any machine (maybe shared lib part needs a tweak)
# but was designed for SGIs - give it a whirl - it does NOT put the
# account in /etc/passwd - you have to do that yourself.
#

#
#    init
#
UNAME=`uname`
FTPUSERS=/etc/ftpusers
PASSWD=/etc/passwd

#
#    ftp (root)
#
DEFROOT=/net/ftp
FTPROOT="${1:-$DEFROOT}"

#
#    mkdirs
#
echo "\ncreating directories..."

for dir in $FTPROOT bin dev etc lib pub
do
  echo "  $dir"
  mkdir $dir
  chown root.sys $dir
  chmod 555 $dir
  if [ "$dir" = "$FTPROOT" ]; then
    cd $FTPROOT
  fi
done

#
#    bin
#
echo "\nprocessing bin..."

echo " ls"
cp -p /bin/ls bin/
chmod 111 bin/ls

#
#    dev
#
echo "\nprocessing dev directory..."

case $UNAME in

  IRIX)
    echo "  zero"
    mknod dev/zero c 37 0
    chmod 444 dev/zero
    ;;

esac

#
#    etc
#
echo "\nprocessing etc directory..."

echo "  passwd"
awk 'BEGIN { FS=":" } { print $1 ":*:" $3 ":" $4 "::/tmp:/bin/false" }' $PASSWD > etc/passwd
chmod 444 etc/passwd
echo "recommended to remove all non-essential entries from $FTPROOT/etc/passwd"

echo "  group"
cp /etc/group etc/group
chmod 444 etc/group

#
#    lib
#
echo "\nprocessing lib directory..."

for file in /lib/rld /lib/libc.so.1
do
  if [ -f $file ]; then
    echo "  $file"
    cp -p $file lib/
    chmod 555 $file
  fi
done

#
#    restrict
#
echo "\nprocessing $FTPUSERS..."

echo "`basename $FTPROOT` restrict" >> $FTPUSERS

#
#    outro
#
cd ..            # in case it was relative to $PWD
ls -laR $FTPROOT
