#!/bin/sh
# $Id: sm.check.sh.in,v 1.11 2007/02/14 03:45:14 ca Exp $
#
# Copyright (c) 2005, 2006 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 MeTA1
# ---------------------------------------------------------------------

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
MTAQD=${DESTDIR}${MTAQDIR:-/var/spool/meta1}
# configuration directory
MTAETC=${DESTDIR}${MTACONFDIR:-/usr/local/etc/meta1}
# log directory (unless syslog(3) is used)
LOGDIR=${MTALOGDIR:-.}
if test ${LOGDIR} != "." -a "${DESTDIR}"X != "X"
then
  LOGDIR=${DESTDIR}${LOGDIR}
fi

# users and groups
# SMTP Server
MTAS=${MTAS:-meta1s}
# SMTP Client
MTAC=${MTAC:-meta1c}
# QMGR
MTAQ=${MTAQ:-meta1q}
# address resolver (misc)
MTAM=${MTAM:-meta1m}
# generic (configuration etc)
MTA=${MTA:-meta1}
# possible groups for logfiles (rw)
LGS=${MTALG:-"operator sysadmin root cannotfindgroupforlogfile"}
# path to build directory
MTAOBJ=`pwd`
S=${MTA_OBJ:-${MTAOBJ}}
# base name of configuration file
MTACNFB=${MTACONF:-meta1.conf}

# ----
# these values will be replaced by configure
srcdir=.
top_srcdir=..
prefix=
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}/meta1
pkglibdir=${libdir}/meta1
pkgincludedir=${includedir}/meta1
top_builddir=.
# ----
# some utilities
CHOWN=${MTACHOWN:-chown}
CHMOD=${MTACHMOD:-chmod}
SED=${MTASED:-sed}
# ----
# END COMMON SETUP SECTION
# -------------------------------------------------------

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

RUNASPRG=${MTARUNAS:-/usr/local/bin/runas}
if ${RUNASPRG} ${MTAM} /usr/local/sbin/t-hostname >/dev/null 2>&1
then
   RUNAS="${RUNASPRG} ${MTAM}"
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
  MTACGID=`${RUNAS} /usr/local/sbin/t-getgroup ${MTAC} 2>/dev/null`
  if test "X${MTACGID}" = "X"
  then
    echo "$0: Error: cannot find gid for ${MTAC}"
    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 ${MTAS} ${MTAC} ${MTAQ} ${MTAM} ${MTA}
  do
    MTAUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${user} 2>/dev/null`
    if test "X${MTAUID}" = "X"
    then
      echo "$0: Error: cannot find uid for ${user}"
      ERRS=`expr ${ERRS} + 1 `
    fi
  done
fi

MTASUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${MTAS} 2>/dev/null`
MTACUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${MTAC} 2>/dev/null`
MTAQUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${MTAQ} 2>/dev/null`
MTAMUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${MTAM} 2>/dev/null`
MTAUID=`${RUNAS} /usr/local/sbin/t-getgroup -p ${MTA} 2>/dev/null`
MTASGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${MTAS} 2>/dev/null`
MTACGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${MTAC} 2>/dev/null`
MTAQGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${MTAQ} 2>/dev/null`
MTAMGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${MTAM} 2>/dev/null`
MTAGID=`${RUNAS} /usr/local/sbin/t-getgroup -g ${MTA} 2>/dev/null`

if test "X${MTASUID}" = "X"
then
  echo "$0: Error: cannot find uid for ${MTAS}"
  ERRS=`expr ${ERRS} + 1 `
fi
if test "X${MTACUID}" = "X"
then
  echo "$0: Error: cannot find uid for ${MTAC}"
  ERRS=`expr ${ERRS} + 1 `
fi
if test "X${MTAQUID}" = "X"
then
  echo "$0: Error: cannot find uid for ${MTAQ}"
  ERRS=`expr ${ERRS} + 1 `
fi
if test "X${MTAMUID}" = "X"
then
  echo "$0: Error: cannot find uid for ${MTAM}"
  ERRS=`expr ${ERRS} + 1 `
fi
if test "X${MTAUID}" = "X"
then
  echo "$0: Error: cannot find uid for ${MTA}"
  ERRS=`expr ${ERRS} + 1 `
fi

if test "X${MTASGID}" = "X"
then
  echo "$0: Error: cannot find gid for ${MTAS}"
  ERRS=`expr ${ERRS} + 1 `
fi
if test "X${MTACGID}" = "X"
then
  echo "$0: Error: cannot find gid for ${MTAC}"
  ERRS=`expr ${ERRS} + 1 `
fi
if test "X${MTAQGID}" = "X"
then
  echo "$0: Error: cannot find gid for ${MTAQ}"
  ERRS=`expr ${ERRS} + 1 `
fi
if test "X${MTAMGID}" = "X"
then
  echo "$0: Error: cannot find gid for ${MTAM}"
  ERRS=`expr ${ERRS} + 1 `
fi
if test "X${MTAGID}" = "X"
then
  echo "$0: Error: cannot find gid for ${MTA}"
  ERRS=`expr ${ERRS} + 1 `
fi

# -- 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 ${MTAQD}
then
  ${CHKFILE} -M 0755 ${MTAQD} || ERRS=`expr ${ERRS} + 1 `
  if cd ${MTAQD}
  then
    :
  else
    echo "$0: Error: cannot chdir to ${MTAQD}"
    ERRS=`expr ${ERRS} + 1 `
    exit 1
  fi
else
  echo "$0: Error: ${MTAQD}: 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 ${MTASUID} -G ${MTAQGID} -M 0771 ${i} || ERRS=`expr ${ERRS} + 1 `
  else
    echo "$0: Error: ${MTAQD}/${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 ${MTAQUID} -G ${MTAQGID} -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 ${MTAQUID} -G ${MTAMGID} qmsmar || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${MTAQUID} -G ${MTACGID} qmsmtpc || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${MTAQUID} -G ${MTASGID} qmsmtps || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${MTASUID} -G ${MTASGID} smtps || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${MTAQUID} -G ${MTAQGID} ibdb || ERRS=`expr ${ERRS} + 1 `
${CHKFILE} -U ${MTAQUID} -G ${MTAQGID} ibdb/ibdb || ERRS=`expr ${ERRS} + 1 `

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

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

MTACNF=${MTAETC}/${MTACNFB}
if test -s ${MTACNF}
then
  MTACONFPRG=/usr/local/bin/meta1conf
  if test -x ${MTACONFPRG}
  then
    ${MTACONFPRG} ${MTACNF} >/dev/null 2>&1
    R=$?
  else
    R=0
  fi
  if test ${R} != 0
  then
     echo "$0: ${MTACNF} is not syntactically correct for this version of meta1."
     exit 1
  fi
else
  echo "$0: Error ${MTACNF} missing"
  ERRS=`expr ${ERRS} + 1 `
  exit 1
fi

for map in ${MTAETC}/aliases ${MTAETC}/access ${MTAETC}/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


syntax highlighted by Code2HTML, v. 0.9.1