#!/bin/sh
#-----------------------------------------------------------------------------#
# QMAIL.SH: Streamlined Qmail Configuration and Installation Script           #
# AUTHOR  : Jay D. Dyson, Treachery Unlimited <jdyson@treachery.net>          #
# VERSION : 2.0 - Accompanies "Qmail for Newbies" publication.                #
# REVISED : 02/13/2001 - v1.0 created to speed up multiple installs.          #
#         : 04/17/2001 - Added support for Linux & FreeBSD installs.          #
#         : 04/20/2001 - Release version, commented for general use.          #
#-----------------------------------------------------------------------------#
# WARNING / DISCLAIMER:                                                       #
#=============================================================================#
#  By use of this script, you agree to hold myself and the site where this    #
#  document is hosted harmless from any consequence arising from use and/or   #
#  misuse of the instructions contained herein.  (I have to say this because  #
#  there are always people out there whose utterly incomprehensible lack of   #
#  common sense is ripe fodder for bottom-feeding lawyer types.)  When in     #
#  doubt, *always* consult Qmail's original documentation.                    #
#=============================================================================#
# 
# GLOBAL DEFAULT VARIABLES
#
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/ccs/bin:/usr/ucb:/usr/local/bin" ; export PATH
QMAIL="/var/qmail"              # Qmail base directory
ALIAS="${QMAIL}/alias"          # Qmail alias directory
RESOLV="/etc/resolv.conf"       # Location of resolv.conf
INETD="/etc/inetd.conf"         # Location of inetd.conf
YN=""                           # Condition Initialization
NY=""                           #
PI=""                           # 
FQDN=""                         # 
CC=`cc -v |grep "not "`         # Show-stoppers, if missing
AR=`which ar |grep -v "no "`    #
LD=`which ld |grep -v "no "`    #
NR=`which nroff |grep -v "no "` #
OSTYPE=`uname -s`               # Get OS type information
ETEST=`echo -e`                 # Test for echo (Solaris /bin/sh echo is funky)
if [ "$ETEST" = "-e" ]; then
  SECHO="echo"
else
  SECHO="echo -e"
fi
#
# PREINSTALLATION CHECKS
#
echo "Streamlined Qmail Installation Script v2.0 - by Jay D. Dyson"
echo ""
echo "------------------------------------------------------------"
echo " By running this script, you acknowledge that you have read"
echo " and  agreed to the  terms and conditions  of the  WARNING/"
echo " DISCLAIMER  at the head of this  shell script.   IF YOU DO"
echo " NOT AGREE, ABORT NOW.  Otherwise, hit ENTER to start."
echo "------------------------------------------------------------"
echo ""
$SECHO "[Hit ENTER to agree to the terms and begin installation]\c"
read temp
# Must be root to install
ROOT=`id |cut -f2 -d'=' |cut -f1 -d"("`
if [ $ROOT -gt 0 ]; then
  echo "ERROR: You MUST be root to install Qmail!" ; echo ""
  exit 0
fi
# Make sure we have the needed utilities to compile.  Show-stoppers if not present.
if [ "$CC" = "" ]; then echo "Found: `which cc` (`which gcc`)"
  if [ -n "$AR" ]; then echo "Found: $AR"
    if [ -n "$LD" ]; then echo "Found: $LD"
      if [ -n "$NR" ]; then echo "Found: $NR"
        echo "Looks like everything necessary to compile is here."
      else echo "ERROR: Nroff missing!  Cannot make man pages."; echo ""; exit 0; fi
    else echo "ERROR: Link-editor missing!  Cannot compile."; echo ""; exit 0; fi
  else echo "ERROR: Cannot create/update library files!"; echo ""; exit 0; fi
else echo "ERROR: Working compiler not found!"; echo ""; exit 0
fi
while [ "$YN" = "" ]; do
  if [ -n "$OSTYPE" ]; then
    echo "Your OS appears to be \"${OSTYPE}\"."
    $SECHO "Is this correct? (y/n) \c"
    read YN
    case "$YN" in
      n*|N*)
        echo "" ; echo "Your uname is broken.  Aborting..."
        echo "Hardcode the OSTYPE variable in this script and try again." ; echo ""
        exit 0
      ;;
      y*|Y*)
        echo "Proceeding with $OSTYPE configuration..." ; echo ""
      ;;
      *)
        YN="" ; echo "" ; echo "Answer 'y' for yes or 'n' for no." ; echo ""
      ;;
    esac
  else
    echo "Can't determine your OS type!"
    echo "Hardcode the OSTYPE variable in this script and try again." ; echo ""
    exit 0
  fi
done
echo "Creating Qmail home directory..." ; echo ""
if [ -d /var ]; then
  mkdir /var/qmail
else
  if [ -f /var ]; then
    echo "/var was found, but it was a file.  Renamed to /var.file" ; echo ""
    mv /var /var.file
  fi
  echo "/var does not exist.  Creating /var directory." ; echo ""
  mkdir /var
  mkdir /var/qmail
fi
echo "Creating users and groups for Qmail operations." ; echo ""
# Solaris and Linux behave the same here.
if [ "$OSTYPE" = "SunOS" -o "$OSTYPE" = "Linux" ]; then
  echo "Adding groups:"
  echo "  nofiles"
  groupadd nofiles
  echo "  qmail"
  groupadd qmail
  echo "Adding users:"
  echo "  alias"
  useradd -g nofiles -d $ALIAS alias
  echo "  qmaild"
  useradd -g nofiles -d $QMAIL qmaild
  echo "  qmaill"
  useradd -g nofiles -d $QMAIL qmaill
  echo "  qmailp"
  useradd -g nofiles -d $QMAIL qmailp
  echo "  qmailq"
  useradd -g qmail   -d $QMAIL qmailq
  echo "  qmailr"
  useradd -g qmail   -d $QMAIL qmailr
  echo "  qmails"
  useradd -g qmail   -d $QMAIL qmails
  echo ""
  $SECHO "[Hit ENTER to start Qmail compilation and installation]\c"
  read temp
else
  if [ "$OSTYPE" = "FreeBSD" ]; then
    echo "Adding groups:"
    echo "  nofiles"
    pw groupadd nofiles
    echo "  qmail"
    pw groupadd qmail
    echo "Adding users:"
    echo "  alias"
    pw useradd alias  -g nofiles -d $ALIAS -s /nonexistent
    echo "  qmaild"
    pw useradd qmaild -g nofiles -d $QMAIL -s /nonexistent
    echo "  qmaill"
    pw useradd qmaill -g nofiles -d $QMAIL -s /nonexistent
    echo "  qmailp"
    pw useradd qmailp -g nofiles -d $QMAIL -s /nonexistent
    echo "  qmailq"
    pw useradd qmailq -g qmail   -d $QMAIL -s /nonexistent
    echo "  qmailr"
    pw useradd qmailr -g qmail   -d $QMAIL -s /nonexistent
    echo "  qmails"
    pw useradd qmails -g qmail   -d $QMAIL -s /nonexistent
    echo ""
    $SECHO "[Hit ENTER to start Qmail compilation and installation]\c"
    read temp
  else
    echo "This script was made to automate Qmail installation for platforms I"
    echo "could personally verify and test.  Unfortunately, this was limited to"
    echo "Solaris, Linux, and FreeBSD.  If you'd like to contribute verified"
    echo "install routines for other operating systems, please contact me at"
    echo "jdyson\@treachery.net and I will gladly credit you on future releases"
    echo "of this script.  For now, I've taken you as far as I can.  You'll have"
    echo "to perform the rest of the installation manually." ; echo ""
    echo "Refer to Qmail's INSTALL.  Your next step is in INSTALL.ids." ; echo ""
    echo "Stopping..." ; echo "" ; exit 0
  fi
fi
#
# COMPILE AND INSTALL
#
echo "" ; echo "Compiling Qmail..." ; echo ""
make setup check
#
# CONFIGURATION
#
while [ "$PI" = "" ]; do
  echo "" ; echo "Please scroll up and review the Qmail compilation." ; echo ""
  $SECHO "Did Qmail appear to successfully compile? (y/n) \c"
  read PI
  case "$PI" in
    n*|N*)
      echo "" ; echo "Since all preinstall checklists passed, your system has a problem"
      echo "I didn't encounter.  Please e-mail me ALL the output that this script"
      echo "generated and I'll see if I can help you resolve it.  You can reach"
      echo "me at jdyson\@treachery.net." ; echo ""
      exit 0
    ;;
    y*|Y*)
      echo "" ; echo "Proceeding with postinstall configuration..." ; echo ""
    ;;
    *)
      PI="" ; echo "" ; echo "Answer 'y' for yes or 'n' for no." ; echo ""
    ;;
  esac
done
echo "Setting up Qmail auto-start scripts for ${OSTYPE}..." ; echo ""
if [ "$OSTYPE" = "SunOS" ]; then
  RCDIR="/etc/rc2.d"
  RCFILE="${RCDIR}/S88sendmail"
  if [ -f $RCFILE ]; then
    echo "Renaming $RCFILE to $RCDIR/K88oldsendmail"
    mv $RCFILE $RCDIR/K88oldsendmail
  fi
  if [ -f $RCDIR/K88sendmail ]; then
    echo "Removing old SMI Sendmail stop script..."
    rm -f $RCDIR/K88sendmail
  fi
  echo "Creating new Qmail start script (${RCFILE})"
  echo "#!/sbin/sh"                                              >  $RCFILE
  echo "# Qmail v1.03 - Replacement of default sendmail"         >> $RCFILE
  echo "case \"\$1\" in"                                         >> $RCFILE
  echo "'start')"                                                >> $RCFILE
  echo "  if [ -f /usr/lib/sendmail -a -f /var/qmail/rc ]; then" >> $RCFILE
  echo "    /usr/bin/echo \"Starting Qmail...\""                 >> $RCFILE
  echo "    /usr/bin/csh -cf '/var/qmail/rc &'"                  >> $RCFILE
  echo "  fi"                                                    >> $RCFILE
  echo ";;"                                                      >> $RCFILE
  echo ""                                                        >> $RCFILE
  echo "'stop')"                                                 >> $RCFILE
  VER=`uname -r |cut -f2 -d'.'` # Solaris < 2.7 doesn't have pkill(1); must use kill(1).
  if [ $VER -lt 7 ]; then
    echo "  PID=\`/usr/bin/ps -e |/usr/bin/grep \"qmail-ls\" \\" >> $RCFILE
    echo "      |/usr/bin/grep -v grep |/usr/bin/cut -b2-6\`"    >> $RCFILE
    echo "  /usr/bin/kill -9 \$PID"                              >> $RCFILE
  else
    echo "  /usr/bin/pkill -x -u 0 qmail-lspawn"                 >> $RCFILE
  fi
  echo ";;"                                                      >> $RCFILE
  echo ""                                                        >> $RCFILE
  echo "*)"                                                      >> $RCFILE
  echo "  /usr/bin/echo \"Usage: \$0 { start | stop }\""         >> $RCFILE
  echo "  exit 1"                                                >> $RCFILE
  echo ";;"                                                      >> $RCFILE
  echo "esac"                                                    >> $RCFILE
  echo "exit 0"                                                  >> $RCFILE
  chmod 740 $RCFILE
  echo ""
  echo "$RCFILE configuration complete."
  echo "Please review the contents of $RCFILE to be sure the entries there"
  echo "are appropriate for your system."
  echo ""
  $SECHO "[Hit ENTER to continue]\c"
  read temp
fi
if [ "$OSTYPE" = "Linux" ]; then
  RCDIR="/etc/rc.d"
  RCFILE="${RCDIR}/rc.M"
  OLD="\/usr\/sbin\/sendmail"
  NEW="\/var\/qmail\/bin\/sendmail"
  echo "Renaming $RCFILE to ${RCDIR}/original_rc_M"
  mv $RCFILE ${RCDIR}/original_rc_M
  echo "  chmod 400 ${RCDIR}/original_rc_M"
  chmod 400 ${RCDIR}/original_rc_M
  echo "Modifying $RCFILE to support Qmail auto-startup..."
  cat ${RCDIR}/original_rc_M |sed s/"if \[ -x $OLD \]\; then"/"if \[ -x $NEW \]\; then"/ \
  |sed s/"echo \"Starting sendmail daemon ($OLD -bd -q15m)...\""/"echo \"Starting Qmail...\""/ \
  |sed s/"$OLD -bd -q15m"/"\/bin\/csh -cf '\/var\/qmail\/rc \&'"/ > ${RCFILE}
  chmod 755 $RCFILE
  echo "" ; echo "The following changes were made:" ; echo ""
  diff ${RCDIR}/original_rc_M $RCFILE
  echo ""
  echo "If the changes do not look like this:"
  echo ""
  echo "if [ -x /var/qmail/bin/sendmail ]; then"
  echo "   echo \"Starting Qmail...\""
  echo "   /bin/csh -cf '/var/qmail/rc \&'"
  echo ""
  echo "Then your $RCFILE will need to be manually edited to reflect the"
  echo "appropriate changes.  Please make a note of this."
  echo ""
  $SECHO "[Hit ENTER to continue]\c"
  read temp
fi
if [ "$OSTYPE" = "FreeBSD" ]; then
  RCDIR="/etc"
  RCFILE="${RCDIR}/rc"
  OLD="\/usr\/sbin\/sendmail"
  NEW="\/var\/qmail\/bin\/sendmail"
  echo "Renaming $RCFILE to ${RCDIR}/original_rc"
  mv $RCFILE ${RCDIR}/original_rc
  echo "  chmod 400 ${RCDIR}/original_rc"
  chmod 400 ${RCDIR}/original_rc
  echo "Modifying $RCFILE to support Qmail auto-startup..."
  cat ${RCDIR}/original_rc \
  |sed s/"if \[ -r \/etc\/mail\/sendmail.cf \]\; then"/"if \[ -x $NEW \]\; then"/ \
  |sed s/"echo -n ' sendmail'"/"echo -n ' Qmail'"/ \
  |sed s/"$OLD \${sendmail_flags}"/"\/bin\/csh -cf '\/var\/qmail\/rc \&'"/ \
  |sed s/"echo -n ' sendmail'"/"echo -n ' Qmail'"/ \
  |sed s/"$OLD \${sendmail_outbound_flags}"/"\/bin\/csh -cf '\/var\/qmail\/rc \&'"/ \
  > $RCFILE
  chmod 755 $RCFILE
  echo "" ; echo "The following changes were made:" ; echo ""
  diff ${RCDIR}/original_rc $RCFILE
  echo ""
  echo "If the changes do not look like this:"
  echo ""
  echo "if [ -x /var/qmail/bin/sendmail ]; then"
  echo "              echo -n ' Qmail'"
  echo "              /bin/csh -cf '/var/qmail/rc \&'"
  echo "                      echo -n ' Qmail'"
  echo "                      /bin/csh -cf '/var/qmail/rc \&'"
  echo ""
  echo "Then your $RCFILE will need to be manually edited to reflect the"
  echo "appropriate changes.  Please make a note of this."
  echo ""
  $SECHO "[Hit ENTER to continue]\c"
  read temp
fi
#
SYSTEM=`uname -n`
DOMAIN=`grep -i domain $RESOLV |grep -v "\#" |awk '{print $2}' |head -1`
if [ -n "$SYSTEM" -a -n "$DOMAIN" ]; then
  echo "Your host calls itself \"$SYSTEM\"."
  echo "$RESOLV indicates that your domain is \"$DOMAIN\"."
  echo ""
  ./config-fast ${SYSTEM}.${DOMAIN}
  echo ""
  echo "These values have been stored in /var/qmail/control/rcpthosts."
  echo "The rcpthosts is valuable in preventing your system from being"
  echo "abused as an open relay by spammers.  With this configuration,"
  echo "your system will only relay mail destined for your own system."
  echo "If you wish to relay mail to more systems (as would be the case"
  echo "if this system has additional aliases, virtual hosts, or serves"
  echo "as an MX for other systems), you will need to manually enter the"
  echo "additional system names in the rcpthosts file.  Please refer to"
  echo "'man qmail-smtpd' for more information."
  echo ""
  $SECHO "[Hit ENTER to continue]\c"
  read temp
else
  echo ""
  echo "ERROR: Cannot determine your hostname and/or your domain!"
  echo ""
  echo "I will need your Fully Qualified Domain Name (FQDN) in order to"
  echo "proceed.  Your FQDN is your system's hostname and domain name."
  echo "Example: if the hostname is \"foo\" and the domain is \"bar.com\", the"
  echo "FQDN would be \"foo.bar.com\"."
  echo ""
  while [ "$NY" = "" ]; do
    echo "Enter your Fully Qualified Domain Name"
    $SECHO ": \c"
    read FQDN
    echo ""
    echo "You entered your FQDN as: $FQDN"
    $SECHO "Is this correct? (y/n) \c"
    read NY
    case "$NY" in
      n*|N*)
        NY="" ; echo "Okay, let's try again." ; echo ""
      ;;
      y*|Y*)
        echo "Proceeding with configuration..." ; echo ""
        ./config-fast $FQDN
      ;;
      *)
        NY="" ; FQDN="" ; echo "" ; echo "Answer 'y' for yes or 'n' for no." ; echo ""
      ;;
    esac
  done
fi
$SECHO "[Hit ENTER to continue]\c"
read temp
echo ""
echo "Under the default Qmail installation, mail to the following addresses"
echo "will go to the user 'alias': postmaster, mailer-daemon and root."
echo "In order to accomplish this arrangement, the following files are"
echo "created in ${ALIAS}:"
echo "  .qmail-postmaster"
echo "  .qmail-mailer-daemon"
echo "  .qmail-root"
echo ""
echo "The alias account, which is non-privileged, can now be used to handle"
echo "the administrivia e-mail that would normally go to root (which, under"
echo "the right [or wrong] conditions, could be a security hazard." ; echo ""
touch $ALIAS/.qmail-postmaster
touch $ALIAS/.qmail-mailer-daemon
touch $ALIAS/.qmail-root
chmod 644 $ALIAS/.qmail*
$SECHO "[Hit ENTER to continue]\c"
read temp
#
# POST-INSTALLATION / POST-CONFIGURATION
#
$SECHO "Stopping sendmail..."
if [ "$OSTYPE" = "Linux" -o "$OSTYPE" = "FreeBSD" ]; then
  FLAG="-eaux"
else
  FLAG="-ef"
fi
SMKILL=`ps $FLAG |grep sendmail |grep -v grep |awk '{print $2}'`
kill -9 $SMKILL
echo "done."
if [ "$OSTYPE" = "Linux" -o "$OSTYPE" = "FreeBSD" ]; then
  SMPATH="/usr/sbin"
else
  SMPATH="/usr/lib"
fi
echo "Renaming $SMPATH/sendmail to $SMPATH/sendmail.old"
ls -al $SMPATH/sendmail
mv $SMPATH/sendmail $SMPATH/sendmail.old
echo "Stripping suid and exec bits from sendmail.old"
chmod 0000 $SMPATH/sendmail.old
ls -al $SMPATH/sendmail.old
echo "Creating symlink: $SMPATH/sendmail -> /var/qmail/bin/sendmail"
ln -s /var/qmail/bin/sendmail $SMPATH/sendmail
echo "Copying $QMAIL/boot/home to $QMAIL/rc (Qmail runtime control script)"
cp -p $QMAIL/boot/home $QMAIL/rc
chmod 755 $QMAIL/rc
echo ""
echo "Updating $INETD"
echo ""
echo "It should be noted that inetd is not as graceful or robust as"
echo "tcpserver (http://cr.yp.to/ucspi-tcp/tcpserver.html), but in"
echo "the interest of keeping things uncomplicated at this time, we'll"
echo "use inetd(1M)."
echo ""
echo "Removing old sendmail from ${INETD}..."
cat $INETD |grep -vi smtp |grep -vi sendmail |grep -v "\#" > ${INETD}.new
echo "${INETD} renamed to ${INETD}.old"
mv $INETD ${INETD}.old
chmod 0400 ${INETD}.old
echo "Adding Qmail entry to ${INETD}..."
echo "smtp	stream	tcp	nowait	qmaild	/var/qmail/bin/tcp-env	tcp-env	/var/qmail/bin/qmail-smtpd" >> ${INETD}.new
echo "${INETD}.new renamed to ${INETD}"
mv ${INETD}.new $INETD
$SECHO "Restarting inetd..."
if [ "$OSTYPE" = "Linux" -o "$OSTYPE" = "FreeBSD" ]; then
  FLAG="-eaux"
else
  FLAG="-ef"
fi
IKILL=`ps $FLAG |grep inetd |grep -v grep |awk '{print $2}'`
kill -HUP $IKILL
echo "done."
echo ""
$SECHO "[Hit ENTER to continue]\c"
read temp
echo ""
echo "Starting Qmail..."
echo ""
echo "csh -cf '/var/qmail/rc &'"
csh -cf '/var/qmail/rc &'
echo ""
echo "That's just about it!  All you need to do now is follow the instructions"
echo "in TEST.deliver and TEST.receive.  If everything checks out, you're now"
echo "set with the default installation of Qmail."
echo ""
echo "You may optionally install the following OPTIONAL packages as well:"
echo "     dot-forward (.forward support)"
echo "     fastforward (/etc/aliases support)"
echo "     EZMLM       (Mailing list support)"
echo ""
echo ""
echo "-Jay D. Dyson, 04/19/2001"
echo ""
# End
