#!/bin/sh
# $Id: sm.check.sh.in,v 1.5 2005/08/09 22:12:58 ca Exp $
#
# Copyright (c) 2005 Sendmail, Inc. and its suppliers.
#	All rights reserved.
#
# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#

# ---------------------------------------------------------------------
# Simple script to perform pre/post-installation checks for sendmail X
# ---------------------------------------------------------------------

PRE=false
POST=false
ERRS=0
WARNS=0
while getopts pP FLAG
do
  case "${FLAG}" in
    p) PRE=true;;
    P) POST=true;;
    ?)
       echo "$0: unknown option ${FLAG}"
       echo "$0: options:"
       echo "-p     perform pre-installation checks"
       echo "-P     perform post-installation checks"
       exit 64
       ;;
  esac
done
shift `expr ${OPTIND} - 1`

# -------------------------------------------------------
# BEGIN COMMON SETUP SECTION
# queue directory
SMXQD=${DESTDIR}${SMXQDIR:-/var/spool/smx}
# configuration directory
SMXETC=${DESTDIR}${SMXCONFDIR:-/usr/local/etc/smx}
# log directory (unless syslog(3) is used)
LOGDIR=${SMXLOGDIR:-.}
if test ${LOGDIR} != "." -a "${DESTDIR}"X != "X"
then
  LOGDIR=${DESTDIR}${LOGDIR}
fi

# users and groups
# SMTP Server
SMXS=${SMXS:-smxs}
# SMTP Client
SMXC=${SMXC:-smxc}
# QMGR
SMXQ=${SMXQ:-smxq}
# address resolver (misc)
SMXM=${SMXM:-smxm}
# generic (configuration etc)
SMX=${SMX:-smx}
# possible groups for logfiles (rw)
LGS=${SMXLG:-"operator sysadmin root cannotfindgroupforlogfile"}
# path to build directory
SMXOBJ=`pwd`
S=${SMX_OBJ:-${SMXOBJ}}
# base name of configuration file
SMXCNFB=${SMXCONF:-smx.conf}

# ----
# these values will be replaced by configure
srcdir=.
top_srcdir=..
prefix=/usr/local
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
sbindir=${exec_prefix}/sbin
libexecdir=${exec_prefix}/libexec
datadir=${prefix}/share
sysconfdir=${prefix}/etc
sharedstatedir=${prefix}/com
localstatedir=${prefix}/var
libdir=${exec_prefix}/lib
infodir=${prefix}/info
mandir=${prefix}/man
includedir=${prefix}/include
oldincludedir=/usr/include
pkgdatadir=${datadir}/smX
pkglibdir=${libdir}/smX
pkgincludedir=${includedir}/smX
top_builddir=.
# ----
# some utilities
CHOWN=${SMXCHOWN:-chown}
CHMOD=${SMXCHMOD:-chmod}
SED=${SMXSED:-sed}
# ----
# END COMMON SETUP SECTION
# -------------------------------------------------------

TD=`dirname $0`
HERE=`pwd`
cd ${TD}/.. && SD=`pwd` && cd ${HERE}
if ${POST}
then
  :
else
  /usr/local/sbin/noroot || exit 64
fi
CHKFILE=/usr/local/sbin/statit

RUNASPRG=${SMXRUNAS:-/usr/local/bin/runas}
if ${RUNASPRG} ${SMXM} /usr/local/sbin/t-hostname >/dev/null 2>&1
then
   RUNAS="${RUNASPRG} ${SMXM}"
else
   RUNAS=""
fi

if ${PRE}
then
  # hostname
  HOSTNAME=`${RUNAS} /usr/local/sbin/t-hostname -l -V 2>/dev/null`
  if test "X${HOSTNAME}" = "X"
  then
    HOSTNAME="unconfigured.host.name"
    echo "$0: Warning: cannot determine host name"
    WARNS=`expr ${WARNS} + 1 `
  fi
  SMXCGID=`${RUNAS} /usr/local/sbin/t-getgroup ${SMXC} 2>/dev/null`
  if test "X${SMXCGID}" = "X"
  then
    echo "$0: Error: cannot find gid for ${SMXC}"
    ERRS=`expr ${ERRS} + 1 `
    exit 1
  fi
  # get a group for root to use for logfiles
  for LG in ${LGS}
  do
    if /usr/local/sbin/t-getgroup ${LG} >/dev/null 2>/dev/null
    then
      break
    fi
  done
  if test "${LG}" = "cannotfindgroupforlogfile"
  then
    echo "$0: Cannot find group for logfile"
    exit 1
  fi

  for user in ${SMXS} ${SMXC} ${SMXQ} ${SMXM} ${SMX}
  do
    SMXUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${user} 2>/dev/null`
    if test "X${SMXUID}" = "X"
    then
      echo "$0: Error: cannot find uid for ${user}"
      ERRS=`expr ${ERRS} + 1 `
    fi
  done
fi

SMXSUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${SMXS} 2>/dev/null`
SMXCUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${SMXC} 2>/dev/null`
SMXQUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${SMXQ} 2>/dev/null`
SMXMUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${SMXM} 2>/dev/null`
SMXUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${SMX} 2>/dev/null`
SMXSGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${SMXS} 2>/dev/null`
SMXCGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${SMXC} 2>/dev/null`
SMXQGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${SMXQ} 2>/dev/null`
SMXMGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${SMXM} 2>/dev/null`
SMXGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${SMX} 2>/dev/null`

# -- try to find name server
NS=""
RSLVC=/etc/resolv.conf
if test -s ${RSLVC}
then
NS=`grep '^nameserver' ${RSLVC} | head -1 | ${SED} -e 's/nameserver[	 ]*\([0-9][0-9\.]*\).*$/\1/'`
fi
if test -z "${NS}"
then
  echo "$0: no nameserver found"
  NS=127.0.0.1
fi

if ${PRE}
then
  echo "$0: pre-installation check successful"
  exit 0
fi

if ${POST}
then
  # continue down below
  :
else
  exit 0
fi

if test -d ${SMXQD}
then
  ${CHKFILE} -M 0755 ${SMXQD} || ERRS=`expr ${ERRS} + 1 `
  if cd ${SMXQD}
  then
    :
  else
    echo "$0: Error: cannot chdir to ${SMXQD}"
    ERRS=`expr ${ERRS} + 1 `
    exit 1
  fi
else
  echo "$0: Error: ${SMXQD}: no such directory"
  ERRS=`expr ${ERRS} + 1 `
  exit 1
fi

# check queue directories (for CDB)
for i in 0 1 2 3 4 5 6 7 8 9 A B C D E F
do
  if test -d ${i}
  then
    ${CHKFILE} -U ${SMXSUID} -G ${SMXQGID} -M 0771 ${i} || ERRS=`expr ${ERRS} + 1 `
  else
    echo "$0: Error: ${SMXQD}/${i}: no such directory"
    ERRS=`expr ${ERRS} + 1 `
  fi
done

# deferred envelope queue
DEFEDB=defedb
if test -d ${DEFEDB}
then
  # check owner and mode
  ${CHKFILE} -U ${SMXQUID} -G ${SMXQGID} -M 0700 ${DEFEDB} || ERRS=`expr ${ERRS} + 1 `
else
  echo "$0: Error: ${DEFEDB}: no such directory"
  ERRS=`expr ${ERRS} + 1 `
fi

# directories for sockets etc
for i in qmsmtps qmsmtpc qmsmar
do
  if test -d ${i}
  then
    # check owner and mode
    ${CHKFILE} -M 02770 ${i} || ERRS=`expr ${ERRS} + 1 `
  else
    echo "$0: Error: ${i}: no such directory"
    ERRS=`expr ${ERRS} + 1 `
  fi
done

# directories for sockets, IBDB
for i in smtps ibdb ibdb/ibdb
do
  if test -d ${i}
  then
    :
  else
    echo "$0: Error: ${i}: no such directory"
    ERRS=`expr ${ERRS} + 1 `
  fi
done

${CHKFILE} -M 0700 ibdb ibdb/ibdb || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -M 0750 smtps || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${SMXQUID} -G ${SMXMGID} qmsmar || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${SMXQUID} -G ${SMXCGID} qmsmtpc || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${SMXQUID} -G ${SMXSGID} qmsmtps || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${SMXSUID} -G ${SMXSGID} smtps || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${SMXQUID} -G ${SMXQGID} ibdb || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${SMXQUID} -G ${SMXQGID} ibdb/ibdb || ERRS=`expr ${ERRS} + 1 `

# --
# create configuration directory
if test -d ${SMXETC}
then
  # check owner and mode
  :
else
  echo "$0: Error: ${SMXETC}: no such directory"
  ERRS=`expr ${ERRS} + 1 `
fi

# create "mailertable"
MT=${SMXETC}/mt
if test -f ${MT}
then
  ${CHKFILE} -U ${SMXMUID} -G ${SMXMGID} ${MT}
else
  echo "$0: Error: ${MT}: no such file"
  ERRS=`expr ${ERRS} + 1 `
fi

SMXCNF=${SMXETC}/${SMXCNFB}
if test -s ${SMXCNF}
then
  SMXCONFPRG=/usr/local/bin/smxconf
  if test -x ${SMXCONFPRG}
  then
    ${SMXCONFPRG} ${SMXCNF} >/dev/null 2>&1
    R=$?
  else
    R=0
  fi
  if test ${R} != 0
  then
     echo "$0: ${SMXCNF} is not syntactically correct for this version of sendmail X."
     exit 1
  fi
else
  echo "$0: Error ${SMXCNF} missing"
  ERRS=`expr ${ERRS} + 1 `
  exit 1
fi

for map in ${SMXETC}/aliases ${SMXETC}/access ${SMXETC}/qmgr_conf
do
  if test -f ${map}
  then
    # check owner and mode
    :
    mapdb=${map}.db
    if test -s ${mapdb}
    then
      # check owner and mode
      :
    else
      echo "$0: Warning: ${mapdb} does not exist"
      WARNS=`expr ${WARNS} + 1 `
    fi
  else
    echo "$0: Warning: ${map} does not exist"
    WARNS=`expr ${WARNS} + 1 `
  fi
done

if test ${ERRS} = 0
then
  if test ${WARNS} = 0
  then
    echo "$0: post-installation check successful"
  else
    echo "$0: post-installation: ${WARNS} warnings"
  fi
else
  echo "$0: post-installation failed: ${ERRS} errors"
fi

exit 0
