#!/bin/sh

# The updated qmailrocks.org qmail Installation guide for Debian, 2012-03-11
# I've modified Dave Sill's script slightly to accomodate
# the variations between his Qmail installation and the
# Qmailrocks installation.
# http://http://qmailrocks.thibs.com/downloads/scripts/qmr_inst_check

# Dave Sill, 2003-11-10
# http://lifewithqmail.org/inst_check

CHECKSEND=y
QMHOME=/var/qmail
BIN=/usr/bin
LBIN=/usr/local/bin

OK=1

if [ $# -eq 1 ]; then
  if [ "$1" = "-v" ]; then
    VERB=y
  else
    VERB=n
  fi
fi

if [ ! -d $QMHOME ]; then
  echo "! Couldn't find qmail's home directory, $QMHOME!"
else
  if [ "$VERB" = y ]; then
    echo "$QMHOME exists"
  fi
  for i in alias bin boot control doc man queue supervise users; do
    if [ ! -d $QMHOME/$i ]; then
      echo "! Couldn't find $QMHOME/$i!"
      OK=0
    elif [ "$VERB" = y ]; then
      echo "$QMHOME/$i exists"
    fi
  done
  if [ ! -f $QMHOME/supervise/qmail-send/run ]; then
    echo "! $QMHOME/supervise/qmail-send/run is missing"
    echo "...try: cp /downloads/scripts/service-qmail-send-run $QMHOME/supervise/qmail-send/run"
    OK=0
  elif [ ! -f /downloads/scripts/service-qmail-send-run ]; then
    echo "! /downloads/scripts/service-qmail-send-run is missing"
    echo "...try: cd /downloads/scripts/; wget http://qmailrocks.thibs.com/downloads/scripts/service-qmail-send-run"
    OK=0
  elif [ ! -r $QMHOME/supervise/qmail-send/run ]; then
   echo "! $QMHOME/supervise/qmail-send/run is not readable"
   echo "! /downloads/scripts/service-qmail-send-run is not readable"
   echo "...try: chmod 755 $QMHOME/supervise/qmail-send/run"
   echo "...try: chmod 755 /downloads/scripts/service-qmail-send-run"
   OK=0
  elif [ ! -x $QMHOME/supervise/qmail-send/run ]; then
   echo "! $QMHOME/supervise/qmail-send/run is not executable"
   echo "! /downloads/scripts/service-qmail-send-run is not executable"
   echo "...try: chmod 755 $QMHOME/supervise/qmail-send/run"
   echo "...try: chmod 755 /downloads/scripts/service-qmail-send-run"
   OK=0
  elif [  `cmp -s $QMHOME/supervise/qmail-send/run /downloads/scripts/service-qmail-send-run`]; then
    echo "! $QMHOME/supervise/qmail-send/run is not correct"
    echo "...try: cp /downloads/scripts/service-qmail-send-run $QMHOME/supervise/qmail-send/run"
    OK=0
  elif [ `head -1 $QMHOME/supervise/qmail-send/run|cat -vet` != '#!/bin/sh$' ]; then
    echo "! $QMHOME/supervise/qmail-send/run has bad magic cookie"
    echo "...try: dos2unix $QMHOME/supervise/qmail-send/run"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$QMHOME/supervise/qmail-send/run is executable and has a valid magic cookie"
  fi
fi

for i in alias qmaild qmaill qmailp qmailq qmailr qmails vpopmail; do
  grep "^$i:" /etc/passwd >/dev/null
  if [ $? -ne 0 ]; then
    echo "! Couldn't find $i user in /etc/passwd"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$i user exists"
  fi
done

for i in qmail nofiles vchkpw; do
  grep "^$i:" /etc/group >/dev/null
  if [ $? -ne 0 ]; then
    echo "! Couldn't find $i group in /etc/group"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$i group exists"
  fi
done

for i in tcprules tcpserver; do
  if [ ! -x $BIN/$i ]; then
    echo "! Couldn't find $BIN/$i from ucspi-tcp"
    echo "Try aptitude install ucspi-tcp"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$i from ucspi-tcp is installed"
  fi
done

for i in sslclient sslserver; do
  if [ ! -x $LBIN/$i ]; then
    echo "! Couldn't find $LBIN/$i from UCSPI-SSL"
    echo "Try to re-install UCSPI-SSL"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$i from UCSPI-SSL is installed"
  fi
done

for i in multilog softlimit setuidgid supervise svok svscan tai64nlocal; do
  if [ ! -x $BIN/$i ]; then
    echo "! Couldn't find $BIN/$i from daemontools"
    echo "aptitude install daemontools daemontools-run"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$i from daemontools is installed"
fi
done

if [ ! -d /service ]; then
  echo "! /service directory is missing"
  OK=0
elif [ "$VERB" = y ]; then
  echo "/service directory exists"
fi

if [ -f /etc/inittab ]; then
  grep "^SV" /etc/inittab >/dev/null
  if [ $? -ne 0 ]; then
    echo "! Couldn't find SV entry in inittab"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "svscan /service is configured to run via /etc/inittab"
  fi
  PS="ps -ef"
else
  grep "svscanboot" /etc/rc.local >/dev/null
  if [ $? -ne 0 ]; then
    echo "! Couldn't find 'csh -cf '/command/svscanboot &' in /etc/rc.local"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "/command/svscanboot is configured to run via /etc/rc.local"
  fi
  PS="ps -waux"
fi

SVRUN=`$PS | grep "svscan /etc/service" | grep -v grep`
if [ -z "$SVRUN" ]; then
  echo "'! svscan /service' doesn't seem to be running"
  OK=0
elif [ "$VERB" = y ]; then
  echo "svscan /etc/service is running"
fi

if [ ! -f $QMHOME/control/defaultdelivery ]; then
  echo "! Couldn't find $QMHOME/control/defaultdelivery"
  OK=0
elif [ ! -s $QMHOME/control/defaultdelivery ]; then
  echo "! $QMHOME/control/defaultdelivery is empty"
  OK=0
elif [ "$VERB" = y ]; then
  echo "$QMHOME/control/defaultdelivery looks OK"
fi

#grep "^DatabaseOwner qscand" /etc/clamav/freshclam.conf >/dev/null
#if [ $? -ne 0 ]; then
#  echo "! DatabaseOwner is incorrect in /etc/clamav/freshclam.conf"
#  OK=0
#elif [ "$VERB" = y ]; then
#  echo "DatabaseOwner is correct in /etc/clamav/freshclam.conf"
#fi

#grep "^LocalSocketGroup qscand" /etc/clamav/clamd.conf >/dev/null
#if [ $? -ne 0 ]; then
#  echo "! LocalSocketGroup is incorrect in /etc/clamav/clamd.conf"
#  OK=0
#elif [ "$VERB" = y ]; then
#  echo "LocalSocketGroup is correct in /etc/clamav/clamd.conf"
#fi

#grep "^User qscand" /etc/clamav/clamd.conf >/dev/null
#if [ $? -ne 0 ]; then
#  echo "! User is incorrect in /etc/clamav/clamd.conf"
#  OK=0
#elif [ "$VERB" = y ]; then
#  echo "User is correct in /etc/clamav/clamd.conf"
#fi

MYSQLDBSDIR=`grep datadir /etc/mysql/my.cnf |awk '{print $3}'`
if [ ! -d $MYSQLDBSDIR/vpopmail ]; then
  echo "! vpopmail database do not exist!"
  OK=0
elif [ "$VERB" = y ]; then
  echo "$MYSQLDBSDIR/vpopmail exists"
fi

if [ ! -f $QMHOME/bin/qmailctl ]; then
  echo "! $QMHOME/bin/qmailctl is missing"
  OK=0
elif [ ! -s $QMHOME/bin/qmailctl ]; then
  echo "! $QMHOME/bin/qmailctl is empty"
  OK=0
elif [ `head -1 $QMHOME/bin/qmailctl|cat -vet` != '#!/bin/sh$' ]; then
    echo "! $QMHOME/bin/qmailctl has bad magic cookie"
    echo "...try: dos2unix $QMHOME/bin/qmailctl"
    OK=0
elif [ ! -x $QMHOME/bin/qmailctl ]; then
  echo "! $QMHOME/bin/qmailctl is not executable"
  echo "...try: chmod 755 $QMHOME/bin/qmailctl"
  OK=0
elif [ "$VERB" = y ]; then
  echo "$QMHOME/bin/qmailctl looks OK"
fi
if [ ! -L /usr/bin/qmailctl ]; then
  echo "! /usr/bin/qmailctl is missing"
  echo "...try: ln -s $QMHOME/bin/qmailctl /usr/bin"
  OK=0
elif [ "$VERB" = y ]; then
  echo "/usr/bin/qmailctl link exists"
fi

SVCDIRS="qmail-send qmail-send/log qmail-smtpd qmail-smtpd/log"
for i in $SVCDIRS; do
  if [ ! -f $QMHOME/supervise/$i/run ]; then
    echo "! $QMHOME/supervise/$i/run file is missing"
    OK=0
  elif [ `head -1 $QMHOME/supervise/$i/run|cat -vet` != '#!/bin/sh$' ]; then
    echo "! $QMHOME/supervise/$i/run has bad magic cookie"
    echo "...try: dos2unix $QMHOME/supervise/$i/run"
    OK=0
  elif [ ! -x $QMHOME/supervise/$i/run ]; then
    echo "! $QMHOME/supervise/$i/run file is not executable"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$QMHOME/supervise/$i/run looks OK"
  fi
done

if [ ! -f $QMHOME/control/concurrencyincoming ]; then
  echo "! $QMHOME/control/concurrencyincoming is missing"
  echo "...try: echo 30 >$QMHOME/control/concurrencyincoming"
  OK=0
elif [ "$VERB" = y ]; then
  echo "$QMHOME/control/concurrencyincoming looks OK"
fi

LOGDIRS="/var/log/qmail /var/log/qmail/qmail-smtpd"
if [ "$CHECKSEND" = "y" ]; then
  LOGDIRS="$LOGDIRS /var/log/qmail/qmail-send"
fi
for i in $LOGDIRS; do
  if [ ! -d $i ]; then
    echo "! $i is missing"
    echo "...try: mkdir -p $i"
    OK=0
  elif [ "`ls -ld $i|awk '{print $3}'`" != "qmaill" ]; then
    echo "! $i has wrong owner, should be qmaill"
    echo "...try: chown qmaill $i"
    OK=0
  elif [ "`ls -ld $i|awk '{print $1}'`" != "drwxr-x---" ]; then
    echo "! $i has wrong mode, should be 750"
    echo "...try: chmod 750 $i"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$i looks OK"
  fi
done

#CLAMAVDIRS="/var/log/clamav /var/lib/clamav /var/run/clamav"
#for i in $CLAMAVDIRS; do
#  if [ ! -d $i ]; then
#    echo "! $i is missing"
#    echo "...try: mkdir -p $i"
#    OK=0
#  elif [ "`ls -ld $i|awk '{print $3}'`" != "qscand" ]; then
#    echo "! $i has wrong owner, should be qscand"
#    echo "...try: chown -R qscand:clamav $i"
#    OK=0
#  elif [ "`ls -ld $i|awk '{print $4}'`" != "clamav" ]; then
#    echo "! $i has wrong group, should be clamav"
#    echo "...try: chown -R qscand:clamav $i"
#    OK=0
#  elif [ "`ls -ld $i|awk '{print $1}'`" != "drwxr-xr-x" ]; then
#    echo "! $i has wrong mode, should be 755"
#    echo "...try: chmod 755 $i"
#    OK=0
#  elif [ "$VERB" = y ]; then
#    echo "$i looks OK"
#  fi
#done

SVCLINKS="/service/qmail-send /service/qmail-smtpd"
for i in $SVCLINKS; do
  if [ ! -L $i ]; then
    echo "! $i is missing"
    echo "...try: ln -s $QMHOME$i $i"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$i exists"
  fi
done

if [ ! -f /etc/tcp.smtp ]; then
  echo "! /etc/tcp.smtp is missing"
  echo "...try: echo '127.:allow,RELAYCLIENT=\"\"' >>/etc/tcp.smtp"
  OK=0
elif [ "$VERB" = y ]; then
  echo "/etc/tcp.smtp exists"
fi

if [ ! -f /etc/tcp.smtp.cdb ]; then
  echo "! /etc/tcp.smtp.cdb is missing"
  echo "...try: $QMHOME/bin/qmailctl cdb"
  OK=0
elif [ "$VERB" = y ]; then
  echo "/etc/tcp.smtp.cdb exists"
fi

AHOME=`grep "^alias:" /etc/passwd | awk -F: '{print $6}'`
if [ -z "$AHOME" ]; then
  echo "! Couldn't find user alias's home directory"
  OK=0
else
  for i in root postmaster mailer-daemon; do
    if [ ! -f $AHOME/.qmail-$i ]; then
      echo "! Alias for $i is missing"
      echo "...try: echo me >$AHOME/.qmail-$i"
      OK=0
    elif [ "$VERB" = y ]; then
      echo "$i alias exists"
    fi
  done
fi

if netstat -a | grep smtp | grep -i listen >/dev/null; then
  if $PS | grep sendmail | grep -v grep >/dev/null; then
    echo "! Sendmail is still running"
    echo "...try: $RCDIR/init.d/sendmail stop"
    OK=0
  elif $BIN/svok /service/qmail-smtpd; then
    if [ "$VERB" = y ]; then
      echo "/service/qmail-smtpd is running"
    fi
  else
    echo "! Something is listening on port 25 (not tcpserver/qmail-smtpd)"
    echo "...try: disabling current MTA"
    OK=0
  fi
fi

for i in /usr/lib/sendmail /usr/sbin/sendmail; do
  if [ -f $i -a ! -L $i ]; then
    echo "! $i is a file, should be a link"
    echo "...try: uninstalling current MTA or: mv $i $i.old; ln -s $QMHOME/bin/sendmail $i"
    OK=0
  elif [ ! -f $i ];then
    echo "! $i is missing"
    echo "...try: ln -s $QMHOME/bin/sendmail $i"
    OK=0
  elif [ "$VERB" = y ]; then
    echo "$i exists"
  fi
done

if [ $OK -eq 1 ]; then
  echo "Congratulations, your updated Debian Qmailrocks installation looks good!"
elif [ "$VERB" = y ]; then
  echo "! Potential problems were found with your LWQ installation"
fi
