06f80e
#!/bin/sh
06f80e
# Generate db and cf files if necessary. This used to be handled by
06f80e
# /etc/mail/Makefile.
06f80e
06f80e
teste() {
06f80e
  if ! test -e "$1"; then
06f80e
    echo "$1 doesn't exist"
06f80e
    exit 2
06f80e
  fi
06f80e
}
06f80e
06f80e
makedb() {
06f80e
  teste "${1%.db}"
06f80e
06f80e
  if [ -z "$SM_FORCE_DBREBUILD" ]; then
06f80e
    test "${1%.db}" -nt "$1" || return 0
06f80e
  fi
06f80e
06f80e
  if [ "$1" = userdb.db ]; then
06f80e
    makemap btree "$1" < "${1%.db}"
06f80e
  else
06f80e
    makemap hash "$1" < "${1%.db}"
06f80e
  fi
06f80e
}
06f80e
06f80e
makealiasesdb() {
06f80e
  uptodate=1
06f80e
06f80e
  if [ -z "$SM_FORCE_DBREBUILD" ]; then
06f80e
    files=$(grep '^O AliasFile=' sendmail.cf |
06f80e
      while read a; do echo ${a#*=}; done)
06f80e
06f80e
    for a in $files; do
06f80e
      if [ "$a" = /etc/aliases ]; then
06f80e
        # /etc/aliases.db may be used by other MTA, make sure nothing
06f80e
        # has touched it since our last newaliases call
06f80e
        test "$a" -nt "${a}.db" ||
06f80e
          test aliasesdb-stamp -nt "${a}.db" ||
06f80e
          test aliasesdb-stamp -ot "${a}.db" || continue
06f80e
      else
06f80e
        test "$a" -nt "${a}.db" || continue
06f80e
      fi
06f80e
06f80e
      uptodate=0
06f80e
      break
06f80e
    done
06f80e
  else
06f80e
    uptodate=0
06f80e
  fi
06f80e
06f80e
  [ $uptodate = 1 ] && return 0
06f80e
06f80e
  # check if alternatives is configured to sendmail
06f80e
  if [ "$(readlink -e /usr/bin/newaliases)" = /usr/sbin/sendmail.sendmail ]
06f80e
  then
06f80e
    /usr/bin/newaliases > /dev/null
06f80e
    touch -r /etc/aliases.db aliasesdb-stamp 2> /dev/null
06f80e
  else
06f80e
    rm -f aliasesdb-stamp
06f80e
  fi
06f80e
}
06f80e
06f80e
makecf() {
06f80e
  mc=${1%.cf}.mc
06f80e
06f80e
  teste "$mc"
06f80e
06f80e
  if [ -z "$SM_FORCE_CFREBUILD" ]; then
06f80e
    test "$mc" -nt "$1" || return 0
06f80e
  fi
06f80e
06f80e
  if test -f /usr/share/sendmail-cf/m4/cf.m4; then
06f80e
    umask 022
06f80e
    [ -e "$1" ] && mv -f "$1" "$1".bak
06f80e
    m4 "$mc" > "$1"
06f80e
  else
06f80e
    echo "WARNING: '$mc' is modified. Please install package sendmail-cf to update your configuration."
06f80e
    exit 15
06f80e
  fi
06f80e
}
06f80e
06f80e
makeall() {
06f80e
  # These could be used by sendmail, but are not part of the default install.
06f80e
  # To use them you will have to generate your own sendmail.cf with
06f80e
  # FEATURE('whatever')
06f80e
  test -f bitdomain && makedb bitdomain.db
06f80e
  test -f uudomain && makedb uudomain.db
06f80e
  test -f genericstable && makedb genericstable.db
06f80e
  test -f userdb && makedb userdb.db
06f80e
  test -f authinfo && makedb authinfo.db
06f80e
06f80e
  makedb virtusertable.db
06f80e
  makedb access.db
06f80e
  makedb domaintable.db
06f80e
  makedb mailertable.db
06f80e
06f80e
  makecf sendmail.cf
06f80e
  makecf submit.cf
06f80e
}
06f80e
06f80e
cd /etc/mail || exit 1
06f80e
06f80e
[ $# -eq 0 ] && makeall
06f80e
06f80e
for target; do
06f80e
  case "$target" in
06f80e
    *.db)
06f80e
      makedb "$target"
06f80e
      ;;
06f80e
    *.cf)
06f80e
      makecf "$target"
06f80e
      ;;
06f80e
    all)
06f80e
      makeall
06f80e
      ;;
06f80e
    aliases)
06f80e
      makealiasesdb
06f80e
      ;;
06f80e
    clean)
06f80e
      rm -f *.db *~ aliasesdb-stamp
06f80e
      ;;
06f80e
    start|stop|restart)
06f80e
      service sendmail "$target"
06f80e
      ;;
06f80e
    *)
06f80e
      echo "Don't know how to make $target"
06f80e
      exit 2
06f80e
  esac
06f80e
done