#!/bin/sh -e
#
# Interactively configure Sendmail for Debian
#
# Robert Leslie <rob@mars.org>

PATH="/usr/sbin:/usr/bin:/sbin:/bin"

reload="yes"
umask 022

###############################################################################

while [ $# -gt 0 ]
do
    case "$1" in
	--no-reload)
	    reload=""
	    shift
	;;

	*)
	    echo "Usage: $0 [--no-reload]" >&2
	    exit 1
	;;
    esac
done

###############################################################################

function descrip() {
    echo ""
    echo "$1"
    echo "$1" | sed -e 's/./-/g'
    cat -
    echo ""
}

function yesno() {
    local N="N" Y="Y"
    local q="$1" def=$(eval "echo -n \$$2")

    while :
    do
	echo -n "$q? [$def] "
	read REPLY
	REPLY=$(echo -n "$REPLY" | sed -e 's/^[ 	]*//' -e 's/[ 	]*$//')
	test -n "$REPLY" || REPLY="$def"

	case "$REPLY" in
	    [Yy]*)
		eval "$2=\"Y\""
		return 0
	    ;;

	    [Nn]*)
		eval "$2=\"N\""
		return 1
	    ;;
	esac
    done
}

function input() {
    local q="$1" def=$(eval "echo -n \$$2")

    echo -n "$q? [$def] "
    read REPLY
    REPLY=$(echo -n "$REPLY" | sed -e 's/^[ 	]*//' -e 's/[ 	]*$//')
    test -n "$REPLY" || REPLY="$def"

    if [ NONE = "$3" ]
    then
	test NONE != "$REPLY" || REPLY=""
    fi

    eval "$2=\"$REPLY\""
}

function testconfig() {
    CONFIG=$(grep "$1" /etc/mail/sendmail.mc 2>/dev/null)
}

function makecf() {
    echo "Generating /etc/mail/sendmail.cf from /etc/mail/sendmail.mc ..."
    m4 /usr/lib/sendmail.cf/m4/cf.m4 /etc/mail/sendmail.mc  \
	>/etc/mail/sendmail.cf.new
    mv -f /etc/mail/sendmail.cf.new /etc/mail/sendmail.cf

    echo "Updating alias database ..."
    newaliases

    if [ "$reload" ] &&  \
	yesno "Reload the running sendmail now with the new configuration" Y
    then
	echo "Reloading sendmail ..."
	/etc/init.d/sendmail reload >/dev/null
    fi
}

###############################################################################

if [ -f /etc/mail/sendmail.mc ]
then
    if yesno "Configure sendmail with the existing /etc/mail/sendmail.mc" Y
    then
	makecf
	exit 0
    fi
fi

###############################################################################

exec 3>/etc/mail/sendmail.mc.new
trap "rm -f /etc/mail/sendmail.mc.new" 0

###############################################################################

descrip "Sendmail Configuration" <<EOT
By answering the following questions, you can configure sendmail for your
system. Default values are determined either by your existing configuration
or from common usage.
EOT

echo -n "Press [ENTER] "
read

cat /usr/lib/sendmail.cf/cf/debproto.mc >&3

###############################################################################

descrip "Mail Name" <<EOT
Your \`mail name' is the hostname portion of the address to be shown on
outgoing news and mail messages (following the username and @ sign). This
name will be used by other programs besides sendmail; it should be the single,
canonical name from which mail will appear to originate.
EOT

mailname=$(cat /etc/mailname 2>/dev/null || hostname --fqdn)
input "Mail name" mailname

echo "$mailname" >/etc/mailname

###############################################################################

descrip "Null Client" <<EOT
A special configuration known as the "null client" can be created for this
host if all mail should be forwarded to a central hub via a local SMTP-based
network. This may be a suitable configuration if you want to forward all of
your mail to your local Internet service provider (ISP) for delivery.

To enable this option, give the name of the host to which all mail should be
forwarded. Otherwise leave the option empty or answer \`NONE' to disable it.
EOT

nullclient=""
! testconfig "FEATURE(nullclient" ||
    nullclient=$(echo -n "$CONFIG" | sed -e 's/[^,]*, *\([^)]*\).*/\1/')

input "Null client forward host" nullclient NONE

test -z "$nullclient" || echo "FEATURE(nullclient, $nullclient)dnl" >&3

###############################################################################

descrip "Address Canonification" <<EOT
Usually sendmail will canonify all addresses by consulting a name server and
resolving hosts to their fully qualified domain name (FQDN). Under special
circumstances you may want to disable this feature, for example if this
machine acts only as a mail gateway.
EOT

test -z "$nullclient" || cat <<EOT
Since this host will be configured as a null client, this option controls
whether addresses will be qualified with \`$mailname' when
sent to the central hub via SMTP. Disabling this feature may cause mail to
appear to come from (and be qualified by the domain for) the hub machine
rather than this host.

EOT

nocanonify="N"
! testconfig "FEATURE(nocanonify)" || nocanonify="Y"

! yesno "Disable address canonification" nocanonify ||
    echo "FEATURE(nocanonify)dnl" >&3

###############################################################################

if [ -z "$nullclient" ]
then

###############################################################################

descrip "SMTP Mailer" <<EOT
If you plan to exchange mail with other computers, you should enable the
SMTP mailer. Even if you don't plan to exchange mail with other computers,
it is still a good idea to enable this so local programs can use it.
EOT

smtp="Y"
if [ -f /etc/mail/sendmail.mc ]
then
    testconfig "MAILER(smtp)" || smtp="N"
fi

yesno "Enable the SMTP mailer" smtp || true

###############################################################################

descrip "Masquerade Envelope" <<EOT
If you want mail envelopes (as well as mail headers) to appear to come from
\`$mailname', you can enable this option.
EOT

masqenvelope="Y"
if [ -f /etc/mail/sendmail.mc ]
then
    testconfig "FEATURE(masquerade_envelope)" || masqenvelope="N"
fi

! yesno "Masquerade envelopes" masqenvelope ||
    echo "FEATURE(masquerade_envelope)dnl" >&3

###############################################################################

descrip "All Masquerade" <<EOT
If enabled, this feature will cause recipient addresses to also appear to come
from \`$mailname'. Normally they get the local hostname.
Although this may be right for ordinary users, it can break local aliases. For
example, if you send to "localalias", the originating sendmail will find that
alias and send to all members, but send the message with
"To: localalias@$mailname". Since that alias likely does
not exist, replies will fail. Use this feature ONLY if you can guarantee that
the ENTIRE namespace of \`$mailname' supersets all the
local entries. If in doubt, it is safe to leave this option disabled.
EOT

allmasquerade="N"
! testconfig "FEATURE(allmasquerade)" || allmasquerade="Y"

! yesno "All masquerade" allmasquerade ||
    echo "FEATURE(allmasquerade)dnl" >&3

###############################################################################

descrip "Always Add Domain" <<EOT
If enabled, the local host domain is included even on locally delivered mail.
Normally it is not added unless it is already present.
EOT

alwaysdomain="N"
! testconfig "FEATURE(always_add_domain)" || alwaysdomain="Y"

! yesno "Always add domain" alwaysdomain ||
    echo "FEATURE(always_add_domain)dnl" >&3

###############################################################################

descrip "Mail Acceptance" <<EOT
Sendmail is usually configured to accept mail for your mail name
($mailname). However, under special circumstances you
may not wish sendmail to do this, particularly if (and disabling this option
generally requires that) mail for \`$mailname' is MXed
to another host. If in doubt, it is safe to leave this option enabled.
EOT

acceptmailname="Y"
if [ -f /etc/mail/sendmail.mc ]
then
    testconfig "^Cw.*$mailname" || acceptmailname="N"
fi

yesno "Accept mail for \`$mailname'" acceptmailname || true

###############################################################################

descrip "Alternate Names" <<EOT
In addition to the canonical mail name \`$mailname', you can
add any number of additional alternate names to recognize for receiving mail.
If other hosts are MXed to you for local mail, this is where you should list
them. This list is saved into the file /etc/mail/sendmail.cw so it can be
changed later as needed.

To answer this question, separate each alternate name with a space, or answer
\`NONE' to eliminate all alternate names.
EOT

altnames=$(cat /etc/mail/sendmail.cw 2>/dev/null | tr -s '\n \t' ' ' |
           sed -e 's/^ *//' -e 's/ *$//')
input "Alternate names" altnames NONE

if [ -n "$altnames" ]
then
    echo "$altnames" | tr -s ' \t' '\n' >/etc/mail/sendmail.cw
else
    cat /dev/null >/etc/mail/sendmail.cw
fi

echo "FEATURE(use_cw_file)dnl" >&3

###############################################################################

descrip "Trusted Users" <<EOT
Sendmail allows a special group of users to set their envelope "From" address
using the -f option without generating a warning message. If you have
software such as Majordomo installed, you will want to include the usernames
from such software here. Note that "root", "daemon", and "uucp" are included
automatically and do not need to be specified. This list is saved into the
file /etc/mail/sendmail.ct so it can be changed later as needed.

To answer this question, separate each username with a space, or answer
\`NONE' to eliminate all usernames.
EOT

trusted=$(cat /etc/mail/sendmail.ct 2>/dev/null | tr -s '\n \t' ' ' |
          sed -e 's/^ *//' -e 's/ *$//')
input "Trusted users" trusted NONE

if [ -n "$trusted" ]
then
    echo "$trusted" | tr -s ' \t' '\n' >/etc/mail/sendmail.ct
else
    cat /dev/null >/etc/mail/sendmail.ct
fi

echo "FEATURE(use_ct_file)dnl" >&3

###############################################################################

descrip "Redirect Feature" <<EOT
If enabled, this feature will allow you to alias old names to
<new-address>.REDIRECT, causing sendmail to return mail to the sender with
an error but indicating the recipient's new address.
EOT

redirect="N"
! testconfig "FEATURE(redirect)" || redirect="Y"

! yesno "Enable redirect option" redirect || echo "FEATURE(redirect)dnl" >&3

###############################################################################

descrip "UUCP Addresses" <<EOT
Sendmail can be configured to be smart about UUCP addresses, or it can do
nothing special with UUCP addresses at all. If you care about UUCP, you will
need to do some additional configuration, perhaps outside of this script.
EOT

uucp="N"
if [ -f /etc/mail/sendmail.mc ]
then
    testconfig "FEATURE(nouucp)" || uucp="Y"
fi

yesno "Enable UUCP addressing" uucp || echo "FEATURE(nouucp)dnl" >&3

###############################################################################

descrip "Sticky Host" <<EOT
If enabled, mail sent to \`user@$mailname' is marked as
"sticky" -- that is, the local addresses aren't matched against UDB and don't
go through ruleset 5. This is used if you want a setup where \`user' is not
necessarily the same as \`user@$mailname', e.g., to make
a distinct domain-wide namespace. If in doubt, it is safe to leave this
option disabled.
EOT

sticky="N"
! testconfig "FEATURE(stickyhost)" || sticky="Y"

! yesno "Enable sticky host option" sticky || echo "FEATURE(stickyhost)dnl" >&3

###############################################################################

descrip "DNS" <<EOT
If you are directly connected to the Internet and have access to a domain
name server, you should enable this option.
EOT

dns="Y"
! testconfig "FEATURE(nodns)" || dns="N"

yesno "Enable DNS" dns || echo "FEATURE(nodns)dnl" >&3

###############################################################################

if [ Y = "$dns" ]
then

###############################################################################

descrip "Best MX is Local" <<EOT
If enabled, this option will cause sendmail to accept mail as though locally
addressed for any host that lists this machine as the best possible MX record.
This generates additional DNS traffic, but should be OK for low-to-medium
traffic hosts. N.B.: This feature is fundamentally incompatible with wildcard
MX records. If you have a wildcard MX record that matches your domain, you
cannot use this feature.
EOT

bestmxlocal="N"
! testconfig "FEATURE(bestmx_is_local)" || bestmxlocal="Y"

! yesno "Assume best MX is local" bestmxlocal ||
    echo "FEATURE(bestmx_is_local)dnl" >&3

###############################################################################

fi  # finished DNS configuration

###############################################################################

descrip "Sendmail Restricted Shell" <<EOT
If enabled, this option causes sendmail to use the sendmail restricted shell
program (smrsh) instead of /bin/sh for mailing to programs. This improves your
ability to control what gets run via email; only those programs which appear
in a special directory can be run. If you enable this option, please carefully
read the smrsh(8) man page for further information.
EOT

smrsh="N"
! testconfig "FEATURE(smrsh" || smrsh="Y"

! yesno "Use the Sendmail Restricted Shell (smrsh)" smrsh ||
    echo "FEATURE(smrsh, \`/usr/sbin/smrsh')dnl" >&3

###############################################################################

descrip "Mailer Name" <<EOT
You can change the name used for internally generated outgoing messages.
Usually this is \`MAILER-DAEMON' but it would not be unreasonable to change
it to something such as \`postmaster'.
EOT

daemon="MAILER-DAEMON"
! testconfig "confMAILER_NAME" ||
    daemon=$(echo -n "$CONFIG" | sed -e 's/[^,]*, *`\([^'"'"']*\).*/\1/')

input "Mailer name" daemon

test "MAILER-DAEMON" = "$daemon" ||
    echo "define(\`confMAILER_NAME', \`$daemon')dnl" >&3

###############################################################################

descrip "Me Too" <<EOT
Sendmail normally excludes the sender address from group expansion. Enabling
this option will cause the sender to be included.
EOT

metoo="N"
! testconfig "confME_TOO.*True" || metoo="Y"

! yesno "Enable me too option" metoo ||
    echo "define(\`confME_TOO', True)dnl" >&3

###############################################################################

descrip "Message Timeouts" <<EOT
Sendmail will issue a warning message to the sender if it can't deliver a
message within a reasonable amount of time. It will also send a failure
notification and give up trying to deliver the message if it can't deliver it
after an unreasonable amount of time.

You can configure the message timeouts after which warning and failure
notifications are sent. Sendmail's defaults are 4 hours and 5 days (4h/5d),
respectively, but many people feel warnings after only 4 hours are premature.
EOT

qw="4h"
! testconfig "confTO_QUEUEWARN" ||
    qw=$(echo -n "$CONFIG" | sed -e 's/[^,]*, *`\([^'"'"']*\).*/\1/')

qr="5d"
! testconfig "confTO_QUEUERETURN" ||
    qr=$(echo -n "$CONFIG" | sed -e 's/[^,]*, *`\([^'"'"']*\).*/\1/')

timeouts="$qw/$qr"

input "Message timeouts" timeouts

qw=$(expr "$timeouts" : '\([^/]*\)/')
qr=$(expr "$timeouts" : '[^/]*/\(.*\)')

test "4h" = "$qw" || echo "define(\`confTO_QUEUEWARN', \`$qw')dnl" >&3
test "5d" = "$qr" || echo "define(\`confTO_QUEUERETURN', \`$qr')dnl" >&3

###############################################################################

echo "MAILER(local)dnl" >&3
test "N" = "$smtp" || echo "MAILER(smtp)dnl" >&3
test "N" = "$acceptmailname" || echo "Cw$mailname" >&3

###############################################################################

fi  # finished non-"null client" configuration

echo "MASQUERADE_AS($mailname)dnl" >&3

###############################################################################

echo "" >&3

if [ -f /etc/mail/sendmail.mc ]
then
    sed -n -e '/^## Custom/,$p' /etc/mail/sendmail.mc >&3
else
    echo "## Custom configurations below (will be preserved)" >&3
fi

descrip "Configuration Complete" <<EOT
Advanced configuration, such as alternate mailers, the use of mailertables,
Bitnet domains, and UUCP domains can be accomplished by manually editing the
/etc/mail/sendmail.mc configuration file and rerunning
\`$0' to generate the appropriate /etc/mail/sendmail.cf
file. (Local changes made at the end of /etc/mail/sendmail.mc will be
preserved by \`$0'.)

The m4 library files for sendmail configuration are kept in the
/usr/lib/sendmail.cf directory. You may wish to review the documentation in
/usr/doc/sendmail to assist in further customization. 

You may wish to customize your alias database; see the aliases(5) man page
for information on the format and use of the /etc/aliases file.
EOT

###############################################################################

exec 3>&-

if [ -f /etc/mail/sendmail.mc ]
then
    echo "Saving old /etc/mail/sendmail.mc as /etc/mail/sendmail.mc.old ..."
    mv -f /etc/mail/sendmail.mc /etc/mail/sendmail.mc.old
fi

mv -f /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc
trap - 0

makecf
exit 0
