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