: use /bin/sh
#
#  mkconfig - to help set up smail on a new linux box
#  RCS ID: $Id: mkconfig,v 1.5 1993/10/03 03:28:58 root Exp root $
#
#  Copyright (c) 1993 Ian Kluft
#  Part of the Linux binary distribution of smail
#
#  As with the rest of this distribution of smail, this is distributed under
#  the GNU Public license.  Run "smail -bC" to see a copy of the license.
#
#  The smail/conf/EDITME and smail/conf/os/linux files in
#  /usr/lib/smail/tools.linux are included in case you want to experiment
#  with the smail 3.1.28 sources yourself.
#

# initialize
umask 022
dashline="------------------------------------------------------------------------------"
configdir="/usr/lib/smail/tools.linux"
configfile="config.state"

# print starting banner
echo "mkconfig - the smail configuration assistant"
echo
echo "by Ian Kluft"
echo "Send problem reports for this script to ikluft@kluft.com"
echo
echo "Send bug reports for smail to its author (see the smail(1) man page)"
echo

# move to install directory if not already there
if [ ! -d $configdir ]
then
	echo "please extract the smail binary archive before running this script"
	exit 1
fi
cd $configdir

# extract defaults from previous runs of mkconfig
if [ -f $configfile ]
then
	. $configfile
fi

# determine host name
echo "Determine hostname"
echo "$dashline"
if [ ! -z "$(uname -n)" ] 2>/dev/null
then
	hostname=`uname -n`
else
	echo "Your system's hostname has not been set up yet." >&2
	echo "Edit /etc/rc.local to include a line like the following" >&2
	echo "  /bin/hostname sysname" >&2
	echo "(replace sysname with a 2-8 letter system 'host' name)" >&2
	exit 1
fi
echo "hostname: $hostname"
echo

# determine e-mail domain
echo "Determine domain name"
echo "$dashline"
resolvconf=""
for i in /etc/resolv.conf /etc/inet/resolv.conf /conf/net/resolv.conf /var/etc/resolv/conf
do
	if [ -r $i ]
	then
		resolvconf=$i
		break
	fi
done
if [ ! -z "$resolvconf" ]
then
	# a nameserver is set up so we'll use that domain
	# (if this fails, you may have the original Linux sed...
	# replace it with GNU sed)
	domain=`grep "^[ \010]*domain" $resolvconf | sed '2,$d;s/^[ \010]*domain[ \010]*\(.*\)/\1/'`
	[ -z "$domain" ] && unset resolvconf  # detect bad DNS configuration
fi
if [ -z "$resolvconf" ]
then
	echo "Since you do not have domain name services installed, you"
	echo "need to pick an e-mail domain name for your system."
	echo
	echo "If you are part of a registered Internet domain, use that"
	echo "name (without a hostname - this script has already found that)."
	echo "Typical Internet domain names end in .com, .edu, .gov, .net,"
	echo ".org, .us, .ca, .uk, .fi, .de, .dk, .fr, .au, .nz, .jp, etc..."
	echo "If you do not recognize this, you probably don't have a domain."
	echo
	echo "If you are not part of a registered Internet domain, you can"
	echo "just hit ENTER and smail will use the default domain 'uucp'"
	echo "for you."
	echo
	echo "enter your e-mail domain name:"
	read domain
fi
echo "domain: ${domain:-<use smail default value>}"
echo

# determine more_hostnames
more_hostnames="${more_hostnames:-$hostname.${domain:-uucp}}"
echo "Determine other hostnames"
echo "$dashline"
echo "If there are any other names you would like your system to recognize"
echo "as itself, you should enter them here.  This may include your domain"
echo "name if your machine is the primary e-mail hub for the domain."
echo "For example, if $hostname is the e-mail hub for ${domain:-your email domain}, you would"
echo "want to use *both* the names ${domain:-your.email.domain} and $hostname.${domain:-your.email.domain}."
echo
echo "This script will automatically start with the following hostname(s):"
echo "  `echo \"$more_hostnames\" | sed 's/:/ /g'`"
echo "So you need to add the names of all the domains you act as a gateway"
echo "for, including your own."
echo
echo "If your system has no other names than $hostname, you may safely hit"
echo "ENTER and leave this blank."
echo
echo "Enter any extra hostnames for your system on separate lines.  Type"
echo "ENTER for a blank line when done."
while read tmpname && [ ! -z "$tmpname" ]
do
	# check for top-level domains, list from Nutshell Guide "DNS & Bind" by Albitz & Liu
	for top_domain in uucp bitnet arpa mil gov edu com org net nato int ag al aq ar at au bb be bg bo br bs bz \
		ca ch cl co cr cs de dk dm do ec eg es fi fr gb gr hk hu ie il in is it jm jp km kn kr lc lk lu my \
		mx na ni nl no nz pe pg ph pl pr pt py se sg sr tw th tn tr tt uk su us uy vc ve yu za zw
	do
		if [ x"$tmpname" = x"$top_domain" ]
		then
			echo "You cannot be the mail hub for the top-level domain .$tmpname!"
			echo "If you do not take this seriously, you will cause problems"
			echo "for yourself and e-mail administrators at all your neighbor"
			echo "sites.  Please try again."
			continue 2
		fi
	done
	if [ -z "$more_hostnames" ]
	then
		more_hostnames=$tmpname
	else
		more_hostnames="$more_hostnames:$tmpname"
	fi
done
echo
echo "more_hostnames: ${more_hostnames:-<none>}"
echo

# determine visible_name
echo "Determine visible name"
echo "$dashline"
echo "You can change the name that mail originating on your system will"
echo "appear to come from.  You can enter nothing and smail will use a"
echo "combination of your hostname and domain name, which is probably"
echo "correct.  However, on a departmental network or as a domain's e-mail"
echo "hub, you may want it just to use the domain name without the host"
echo "name.  (i.e. mail would appear to be from 'here.org' instead of"
echo "'box.here.org')"
echo ""
echo "Do not use the name of a domain which you are not a member of or"
echo "no one will be able to successfully reply to your users' mail."
echo ""
echo "It is safe to leave this blank if you don't have a reason to change"
echo "your host's visible name.  (smail will use $hostname.$domain)"
echo ""
echo "enter your machine's visible name:"
if [ "$visible_name" ]
then
	echo "(default is $visible_name)"
fi
read tmp_visible_name
visible_name=${tmp_visible_name:-$visible_name}
echo
echo "visible_name: ${visible_name:-<smail will use $hostname.$domain>}"
echo

# determine smart path
echo "determine smart_path (smarthost)"
echo "$dashline"
echo "smail can use the concept of a smart host which knows how to do e-mail"
echo "routing better than your system does.  A department mail hub or a UUCP"
echo "neighbor who is on the Internet would be good choices for smarthosts."
echo ""
echo "This applies to mail which is destined for another system.  If smail"
echo "can find it on the network or among UUCP neighbors, it can get it"
echo "there itself.  If not, smail will assume that the smarthost can deliver"
echo "it.  (The smarthost might decide it can't be delivered and return it.)"
echo ""
echo "This does not apply to users which are not recognized.  Those can be"
echo "handled by the smart_user configuration entry, which will come next."
echo ""
echo "You may leave this blank if you do not know the answer.  Be sure to"
echo "go back and edit /var/lib/smail/config when you find out who to use"
echo "as a smarthost."
echo
echo "enter the name of your smarthost:"
if [ "$smart_path" ]
then
	echo "(default is $smart_path)"
fi
read tmp_smart_path
smart_path=${tmp_smart_path:-$smart_path}
echo
echo "smart_path: ${smart_path:-<no smarthost chosen at this time>}"
echo
if [ ! -z "$smart_path" ]
then
	if uuname | grep "$smart_path" >/dev/null 2>&1
	then
		echo "smarthost delivery method: assuming UUCP"
		smart_transport="uux"
	else
		echo "smarthost delivery method: assuming TCP/IP"
		smart_transport="smtp"
	fi
	echo "Edit the smart_transport line in /var/lib/smail/config if"
	echo "this assumption is wrong."
fi
echo

# determine smart user
echo "determine smart_user"
echo "$dashline"
echo "if smail does not recognize a local mail address (on this system) it"
echo "is possible to have it sent to another machine for delivery.  This is"
echo "useful in a departmental network where users may often send mail to"
echo "people in the same building or company.  It is not useful if this is"
echo "the e-mail hub for your domain."
echo
echo "You may leave this blank if mail to unrecognized users should be"
echo "'bounced' back to the sender as undeliverable."
echo ""
echo "enter the name of the system to send unrecognized users' mail to:"
if [ "$smart_user" ]
then
	echo "(default is $smart_user)"
fi
read tmp_smart_user
smart_user=${tmp_smart_user:-$smart_user}
echo
echo "smart_user: ${smart_user:-<none>}"
echo

# determine postmaster
echo "determine postmaster address"
echo "$dashline"
echo "Every site, including yours, should have a 'postmaster' where mail can"
echo "be sent to report problems.  This can deliver into a file or to a user."
echo "You should probably have it direct mail to you, assuming you will be"
echo "the person maintaining smail on this system."
echo
echo "enter the name of the user that should receive mail for postmaster:"
if [ "$postmaster" ]
then
	echo "(default is $postmaster)"
fi
read tmp_postmaster
postmaster=${tmp_postmaster:-$postmaster}
while [ -z "$postmaster" -a -z "$smartuser" ]
do
	echo "You cannot leave this entry blank because you did not fill"
	echo "in the smart_user entry.  Problems with the mail subsystem"
	echo "must be reported to someone."
	echo
	echo "enter the name of the user who will be postmaster:"
	read postmaster
done
echo
echo "postmaster: ${postmaster:-mail will be forwarded to postmaster@$smart_user}"
echo

# banner for configuration generation
echo "generate configuration"
echo "$dashline"

# other config items
osname=`uname -s` || osname="Linux"
smtp_accept_max=10	# Linux is not a mainframe OS so we'll limit
                        #    simulateous SMTPs, remote hosts will try again
smtp_banner='"$primary_name $osname Smail$version #$compile_num ready at $date"'
received_field='"Received: \
	${if def:sender_host \
		{from $sender_host by $primary_name \
		${if def:sender_proto: with $sender_proto}\
		\n\t($osname Smail$version #$compile_num) }\
	else{by $primary_name ${if def:sender_proto:with $sender_proto }\
		($osname Smail$version #$compile_num)\n\t}}\
	id $message_id; $spool_date"'


# generate the config file
[ -f /var/lib/smail/config ] && mv /var/lib/smail/config /var/lib/smail/Oconfig 
{
	echo "#"
	echo "#	smail configuration for $hostname.$domain"
	echo "# (see smail(5) man page for details and other options)"
	echo "#"
	echo "-smtp_debug"
	echo "hostname=$hostname.$domain"
	[ ! -z "$domain" ] && echo "visible_domain=$domain"
	[ ! -z "$visible_name" ] && echo "visible_name=$visible_name"
	[ ! -z "$more_hostnames" ] && echo "more_hostnames=$more_hostnames"
	[ ! -z "$smart_path" ] && echo "smart_path=$smart_path"
	[ ! -z "$smart_transport" ] && echo "smart_transport=$smart_transport"
	[ ! -z "$smart_user" ] && echo "smart_user="'${user}'"@$smart_user"
	[ ! -z "$postmaster" ] && echo "postmaster=$postmaster"
	echo "smtp_accept_max=$smtp_accept_max"
	echo "smtp_banner=$smtp_banner"
	echo "received_field=$received_field"
} > /var/lib/smail/config &&
	echo installed /var/lib/smail/config

# generate directors/routers/transports and forward files with the local domain name
for file in directors routers transports forward
do
	if [ -f /var/lib/smail/$file ]
	then
		echo "You already have a /var/lib/smail/$file... no changes"
	else
		sed "s/\%domain\%/$domain/g;s/\%hostname\%/$hostname/g" \
			/usr/lib/smail/tools.linux/$file.orig > /var/lib/smail/$file && \
			echo installed /var/lib/smail/routers
	fi
done
echo building forwarding database
(cd /var/lib/smail; /usr/lib/smail/mkline forward | /usr/lib/smail/mkdbm -o forward) 
echo

# check for TCP/IP
inetconf="/etc/inetd.conf"
oldiconf="/etc/Oinetd.conf"
if [ ! -f $inetconf ]
then
	echo "Your system does not have an $inetconf file."
	echo "This script is assuming you do not have TCP/IP running."
	echo "When you set up TCP/IP, you may run this script again"
	echo "to set up your smail for networking."
else
	# announce TCP/IP section
	echo "TCP/IP setup"
	echo

	# announce rc script check
	echo "checking /etc/rc.local for smail startup..."

	# add a startup line to rc.local
	if egrep 'smail|sendmail' /etc/rc.local /etc/rc.*net* >/dev/null 2>&1
	then
		echo "smail startup code appears to exist in your /etc/rc.local or /etc/rc.*net*"
		echo "If not, make sure there is a line with '/usr/lib/sendmail -q10m'"
	else
		{
			echo
			echo "# start smail"
			echo "/usr/lib/sendmail -q10m"
		} >> /etc/rc.local
		echo "smail startup has been added to your /etc/rc.local."
		echo "smail will start automatically next time you reboot."
		echo "To start it now, run '/usr/lib/sendmail -q10m' as root"
	fi
	echo

	# check for smtpd in inetd.conf
	echo "verifying for SMTP services"
	if smtpline=`grep '^[ \011].*smtp[ \011]' $inetconf` 2>/dev/null
	then
		echo "you already have an SMTP in your $inetconf.  Verifying..."
		# sample correct smtpd entries:
		#smtp	stream  tcp     nowait  root    /usr/bin/smtpd		smtpd
		#smtp	stream  tcp     nowait  root    /usr/etc/tcpd		/usr/bin/smtpd
		if expr $smtpline : ".*[ \011]/usr/bin/smtpd" >/dev/null
		then
			echo "Your system already has SMTP services enabled - no changes to etc/inetd.conf"
		else
			if tcpd=`expr $smtpline : ".*[^ \011]\([^ \011]*tcpd[^ \011]*\)[ \011]"`
			then
				newsmtp="smtp	stream  tcp     nowait  root    $tcpd		/usr/bin/smtpd"
			else
				newsmtp="smtp	stream  tcp     nowait  root    /usr/bin/smtpd		smtpd"
			fi
			if mv $inetconf $oldiconf
			then
				if sed 's#^[ \011]*smtp[ \011].*$#$newsmtp#' $oldiconf > $inetconf
				then
					echo "Your $inetconf file has been successfully updated"
					echo "(The original has been saved in $oldiconf"
				else
					echo "ERROR: mkconfig was unable to modify your $inetconf"
					if mv $oldiconf $inetconf
					then
						echo "No changes were made."
						echo "Please make sure your SMTP service runs /usr/bin/smtpd"
						echo "(Write that down!)"
					else
						echo "Please make sure your SMTP service runs /usr/bin/smtpd"
						echo "(Write that down!)"
						echo
						echo "The temporary copy could not be moved back from $oldiconf"
						echo "Try again when your system is stable"
						exit 1
					fi
				fi
			else
					echo "ERROR: mkconfig was unable to modify your $inetconf"
					echo "Please make sure your SMTP service runs /usr/bin/smtpd"
					echo "(Write that down!)"
			fi
		fi
	else
		{
			echo "# SMTP - simple mail transfer protcol (smail)"
			echo "smtp	stream  tcp     nowait  root    /usr/bin/smtpd		smtpd"
		} >> /etc/inetd.conf
	fi
	echo
fi

# check for a /bin/mail
# that program is usually not aware of mail delivery subsystems

# Actually, the /bin/mail in Slackware is mailx, which *is* aware of smail.
# This code is outta here.
#if [ -f /bin/mail ]
#then
#	if strings /bin/mail | grep "/usr/lib/sendmail" > /dev/null
#	then
#		echo "mkconfig found a /bin/mail program on your system which is"
#		echo "sendmail-aware.  Since smail is commad-line compatible with"
#		echo "sendmail, /bin/mail has been left alone.  HOWEVER, please"
#		echo "note that a much better mail user agent is 'elm'.  See also"
#		echo "many other possibilities in the email-sw-FAQ file included"
#		echo "with this script."
#	else
#		if mv -f /bin/mail /bin/binmail > /dev/null 2>&1
#		then
#			if ln -fs /usr/bin/elm /bin/mail
#			then
#				echo "mkconfig found a /bin/mail program on your system which is"
#				echo "not aware of other mail subsystems.  It has been moved to"
#				echo "/bin/binmail.  /bin/mail is now a symbolic link to /usr/bin/elm,"
#				echo "which is the preferred mail reader in Linux."
#			else
#				echo "mkconfig found a /bin/mail program on your system which is"
#				echo "not aware of other mail subsystems.  It has been moved to"
#				echo "/bin/binmail."
#				echo
#				echo "ERROR: mkconfig was unable to replace /bin/mail"
#				echo
#				echo "Please use /usr/bin/elm instead of /bin/mail for readline mail."
#				echo "DO NOT USE /bin/mail - it does not give mail to smail for proper delivery."
#			fi
#		else
#			echo "mkconfig found a /bin/mail program on your system which is"
#			echo "not aware of other mail subsystems."
#			echo
#			echo "ERROR: mkconfig was unable to replace /bin/mail"
#			echo
#			echo "Please use /usr/bin/elm instead of /bin/mail for readline mail"
#			echo "DO NOT USE /bin/mail - it does not give mail to smail for proper delivery."
#		fi
#	fi
#	echo
#fi

# save configuration for future runs
for var in more_hostnames visible_name smart_path smart_user postmaster
do
	eval "echo $var=\"\$$var\""
done > $configfile

# we're done
echo "smail configuration done - see /var/lib/smail/config"
