#!/bin/sh -e
#-----------------------------------------------------------------------------
#
# $Sendmail: update_mc,v 8.12.11 2004-04-22 15:39:33 cowboy Exp $
#
# Update Sendmail databases for Debian
#
# Copyright (c) 2000-2003 Richard Nelson.  All Rights Reserved.
#
# Notes (to all):
#   * assumes makemap dbtype /etc/mail/database < /etc/mail/database
#
# Notes (to self):
#   * changes made herein *must* be reflected in
#       parse_mc,update_mk,update_db,debian.m4
#
#-----------------------------------------------------------------------------
set -e;

changed=0;

#-----------------------------------------------------------------------------
# Move sendmail.{mc,cf} from /etc/ to /etc/mail
if [ ! -f /etc/mail/sendmail.mc ]; then
	if [ -f /etc/sendmail.mc ]; then
		echo "Moving /etc/sendmail.* to /etc/mail/";
		mv /etc/sendmail.mc /etc/mail/sendmail.mc;
		chown root:smmsp /etc/mail/sendmail.mc;
		chmod 0644 /etc/mail/sendmail.mc;
		changed=1;
		fi;
	if [ -f /etc/sendmail.cf ]; then
		mv /etc/sendmail.cf /etc/mail/sendmail.cf;
		chown root:smmsp /etc/mail/sendmail.cf;
		chmod 0644 /etc/mail/sendmail.cf;
		changed=1;
		fi;
	fi;

#-----------------------------------------------------------------------------
# Install prototype submit.mc
if [ ! -f /etc/mail/submit.mc ]; then
	cp -p /usr/share/sendmail/cf/debian/submit.mc \
		/etc/mail/submit.mc;
	chown root:smmsp /etc/mail/submit.mc;
	chmod 0644 /etc/mail/submit.mc;
	fi;

#-----------------------------------------------------------------------------
# Make sure dialup support m4 files are extant
if [ ! -f /etc/mail/dialup.m4 ]; then
	touch /etc/mail/dialup.m4;
	chown root:smmsp /etc/mail/dialup.m4;
	chmod 0640 /etc/mail/dialup.m4;
	fi;
if [ ! -f /etc/mail/provider.m4 ]; then
	touch /etc/mail/provider.m4;
	chown root:smmsp /etc/mail/provider.m4;
	chmod 0640 /etc/mail/provider.m4;
	fi;

#-----------------------------------------------------------------------------
# Make sure not using text mailertable (it doesn't work)
if (grep -qEe "^[[:space:]]*FEATURE\(\`?mailertable'?, \`text /etc/mail/mailertable'\)" /etc/mail/sendmail.mc); then
	changed=1;
	echo "Correcting FEATURE(mailertable) in /etc/mail/sendmail.mc";
	sed -e "s=^\([[:space:]]*\)\(\`\?\)FEATURE(\`\?mailertable'\?, \`text /etc/mail/mailertable')\(dnl\)\?=\1\2FEATURE(\`mailertable')dnl=g" \
		 /etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
	fi;

#-----------------------------------------------------------------------------
# Make sure smrsh points to the correct location
if (grep -qEe "^[[:space:]]*\`?FEATURE\(\`?smrsh'?[[:space:]]*,.*\)" \
	/etc/mail/sendmail.mc); then
	changed=1;
	echo "Correcting FEATURE(smrsh) in /etc/mail/sendmail.mc";
	sed -e "s=\([[:space:]]*\)\(\`\?\)FEATURE(\`\?smrsh'\?[[:space:]]*,.*)\(dnl\)\?=\1\2FEATURE(\`smrsh')dnl=g" \
		/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
	fi;

#-----------------------------------------------------------------------------
# Convert FEATURE(nouucp) to FEATURE(nouucp,reject)
if (grep -qEe "^[[:space:]]*\`?FEATURE\(\`?nouucp'?\)" \
	/etc/mail/sendmail.mc); then
	changed=1;
	echo "Correcting FEATURE(nouucp) in /etc/mail/sendmail.mc";
	sed -e "s=^\([[:space:]]*\)\(\`\?\)FEATURE(\`\?nouucp'\?)\(dnl\)\?=\1\2FEATURE(\`nouucp', \`reject')dnl=g" \
		/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
	fi;

#-----------------------------------------------------------------------------
# Convert FEATURE(rbl) to FEATURE(dnsbl,rbl.maps.vix.com)
if (grep -qEe "^[[:space:]]*\`?FEATURE\(\`?rbl'?\)" \
	/etc/mail/sendmail.mc); then
	changed=1;
	echo "Correcting FEATURE(rbl) in /etc/mail/sendmail.mc";
	sed -e "s=^\([[:space:]]*\)\(\`\?\)FEATURE(\`\?rbl'\?)\(dnl\)\?=\1\2FEATURE(\`dnsbl', \`rbl.maps.vix.com')dnl=g" \
		/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
	fi;

#-----------------------------------------------------------------------------
# Convert FEATURE(dont_masquerade_local) to FEATURE(local_no_masquerade)
if [ -e /usr/share/sendmail/cf/feature/local_no_masquerade.m4 ]; then
	if (grep -qEe "^[[:space:]]*\`?FEATURE\(\`?dont_masquerade_local'?\)" \
		/etc/mail/sendmail.mc); then
		changed=1;
		echo "Correcting FEATURE(dont_masquerade_local) in /etc/mail/sendmail.mc";
		sed -e "s=^\([[:space:]]*\)\(\`\?\)FEATURE(\`\?dont_masquerade_local'\?)\(dnl\)\?=\1\2FEATURE(\`local_no_masquerade')dnl=g" \
			/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
		chown root:smmsp /etc/mail/sendmail.mc.new;
		chmod 0644 /etc/mail/sendmail.mc.new;
		mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
		fi;
	fi;

#-----------------------------------------------------------------------------
# Convert FEATURE(access_db,...) to FEATURE(access_db)
if (grep -qEe "^[[:space:]]*\`?FEATURE\(\`?access_db'?[[:space:]]*,.*\)" \
	/etc/mail/sendmail.mc); then
	changed=1;
	sed -e "s=^\([[:space:]]*\)\(\`\?\)FEATURE(\`\?access_db'\?[[:space:]]*,[[:space:]]*\`\?hash[[:space:]]*\(-o\)\?[[:space:]]*/etc/mail/access.db'\?[[:space:]]*)\(dnl\)\?=\1\2FEATURE(\`access_db')dnl=g" \
	/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
	fi;

#-----------------------------------------------------------------------------
# change include(/usr/share/sendmail/sendmail.cf/m4/cf.m4)
if (grep -qEe "^[[:space:]]*\`?include\(\`?/usr/share/sendmail/sendmail.cf/m4/cf.m4'?\)" \
	/etc/mail/sendmail.mc); then
	sed -e "s=sendmail/sendmail.cf=sendmail/cf=g" \
		/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
	sed -e "s=sendmail/sendmail.cf=sendmail/cf=g" \
		/etc/mail/submit.mc > /etc/mail/submit.mc.new;
	chown root:smmsp /etc/mail/submit.mc.new;
	chmod 0644 /etc/mail/submit.mc.new;
	mv /etc/mail/submit.mc.new /etc/mail/submit.mc;
	fi;

#-----------------------------------------------------------------------------
# Add include(/usr/share/sendmail/cf/m4/cf.m4)
if (grep -qEe "^[[:space:]]*\`?include\(\`?/usr/share/sendmail/cf/m4/cf.m4'?\)" \
	/etc/mail/sendmail.mc); then
	true;
else
	changed=1;
	echo "Adding include(.../cf.m4) to /etc/mail/sendmail.mc";
	sed -e "s=^\([[:space:]]*\)\(\`\?\)VERSIONID(=\1\2include(\`/usr/share/sendmail/cf/m4/cf.m4')dnl\\
\1\2VERSIONID(=" \
		/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
	fi;

#-----------------------------------------------------------------------------
# Add define(`_USE_ETC_MAIL_')dnl
if (grep -qEe "^[[:space:]]*\`?define\(\`?_USE_ETC_MAIL_" \
	/etc/mail/sendmail.mc); then
	true;
else
	changed=1;
	sed -e "s=^\([[:space:]]*\)\(\`\?\)include(\`\?/usr/share/sendmail/cf/m4/cf.m4'\?)\(dnl\)\?=\1\2define(\`_USE_ETC_MAIL_')dnl\\
include(\`/usr/share/sendmail/cf/m4/cf.m4')dnl=" \
		/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc;
	fi;

#-----------------------------------------------------------------------------
# Add DOMAIN(debian-mta)
if (grep -qEe "^[[:space:]]*\`?DOMAIN\(\`?debian-mta'?\)" \
	/etc/mail/sendmail.mc); then
	true;
else
	changed=1;
	echo "Adding DOMAIN(debian-mta) to /etc/mail/sendmail.mc";
	sed -e "s=^\([[:space:]]*\)\(\`\?\)OSTYPE(\`\?debian'\?)\(dnl\)\?=\1\2OSTYPE(\`debian')dnl\\
\1\2DOMAIN(\`debian-mta')dnl=" \
		/etc/mail/sendmail.mc > /etc/mail/sendmail.mc.new;
	chown root:smmsp /etc/mail/sendmail.mc.new;
	chmod 0644 /etc/mail/sendmail.mc.new;
	mv /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc
	fi;

#-----------------------------------------------------------------------------
# Cleanup submit.mc
sed -e "/^define(\`\?confRUN_AS_USER.*$/d" \
	-e "/^define(\`\?confTRUSTED_USER.*$/d" \
	/etc/mail/submit.mc > /etc/mail/submit.mc.new;
	chown root:smmsp /etc/mail/submit.mc.new;
	chmod 0644 /etc/mail/submit.mc.new;
	mv /etc/mail/submit.mc.new /etc/mail/submit.mc

exit $changed;
