#!/bin/sh
# update-exim4.conf(8) - Generate /var/lib/exim4/config.autogenerated

set -e
set -C
set -f

UPEX4C_confdir="/etc/exim4"
UPEX4C_sections="main acl router transport retry rewrite auth"

# list of ue4cc options that need to support both colons and
# semicolons as separators. dc_other_hostnames and dc_smarthost
# has special handling.
UPEX4C_semicolon="dc_local_interfaces dc_relay_nets dc_relay_domains"
EXIM="/usr/sbin/exim4"

UPEX4C_verbose=no
UPEX4C_autoconfigfile=/var/lib/exim4/config.autogenerated
UPEX4C_outputfile="${UPEX4C_autoconfigfile}"
UPEX4C_version=""

usage() {
cat <<EOF
$0 - Generate exim4 configuration files
       Options:
       -v|--verbose - Enable verbose mode, tell about ignored files
       -h|--help - Show this message
       --keepcomments - Do not remove comment lines
       --removecomments - Remove comment lines
       -o|--output file - write output to file instead of ${UPEX4C_outputfile}
       -d|--confdir directory - read input from given directory instead of ${UPEX4C_confdir}
EOF
}

## Parse commandline
TEMP=$(getopt -n update-exim4.conf \
	-l keepcomments,removecomments,output:,confdir:,help,verbose -- \
	+o:d:vh "$@")

if test "$?" != 0; then
	echo "Terminating..." >&2
	exit 1
fi

eval set -- ${TEMP}
while test "$1" != "--"; do
	case $1 in
		-h|--help)
			usage
			exit 0
		;;
		-v|--verbose)
			UPEX4C_verbose=yes
		;;
		--keepcomments)
			UPEX4C_comments=yes
		;;
		--removecomments)
			UPEX4C_comments=no
		;;
		-o|--output)
			shift
			UPEX4C_outputfile="$1"
		;;
		-d|--confdir)
			shift
			UPEX4C_confdir="$1"
		;;
	esac
	shift
done
shift

# No non-option arguments allowed.
if [ "$#" -ne 0 ]; then
	echo "No non option arguments ($@) allowed" >&2
	usage >&2
	exit 1
fi

# exit immediately if /etc/exim4/exim4.conf exists and -o was not specified
if [ -e /etc/exim4/exim4.conf ] && \
  [ "${UPEX4C_outputfile}" = "${UPEX4C_autoconfigfile}" ] ; then
  exit 0
fi

UE4CC="$UPEX4C_confdir/update-exim4.conf.conf"
UPEX4C_confd="$UPEX4C_confdir/conf.d"

[ -d "$(dirname "$UPEX4C_outputfile")" ] || \
{ printf "$0: Error, missing $(dirname "$UPEX4C_outputfile"), exiting.\n" 1>&2 ; exit 1 ; }

if [ -f "$UE4CC" ]; then
  . "$UE4CC"
else
  echo >&2 "$0: Error, no $UE4CC, exiting."
  exit 1
fi

lowerpipe() {
  tr 'A-Z' 'a-z'
}

lowercase() {
  echo "$*" | lowerpipe
}

check_ascii_pipe() {
  IN="$(cat)"
  # Use "abcdef... instead of a a-z or [:alnum:] here since the alternatives
  # will also match non-ascii characters.
  OUT="$(echo $IN | sed 's/[^-0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\/\.!*@_~:;< \[\]]/_/g')"
  if [ "$OUT" != "$IN" ]; then
    echo >&2 "$0: non-ascii value $IN read from $UE4CC, sanitizing to $OUT"
  fi
  echo $OUT
}

[ "${CFILEMODE}" = "" ] && CFILEMODE=644
[ "${dc_use_split_config}" = "" ] && dc_use_split_config='false'
[ "${dc_localdelivery}" = "" ] && dc_localdelivery='mail_spool'
[ "${UPEX4C_comments:-}" = "" ] && UPEX4C_comments="${ue4c_keepcomments:-no}"

TEMPLATEFILE="${UPEX4C_confdir}/exim4.conf.template"

dc_use_split_config="$(lowercase $dc_use_split_config)"
UPEX4C_verbose="$(lowercase $UPEX4C_verbose)"

if [ "${dc_use_split_config}" = "true" ]; then
  [ "${UPEX4C_verbose}" = "yes" ] && \
	echo "using split configuration scheme from ${UPEX4C_confd}"
  if ! [ -d "${UPEX4C_confd}" ]; then
    printf >&2 "$0: Error, no ${UPEX4C_confd}, exiting.\n"
    exit 1
  fi
else
  [ "${UPEX4C_verbose}" = "yes" ] && \
	echo "using non-split configuration scheme from ${TEMPLATEFILE}"
fi

# take only the first word from /etc/mailname
mailname="$(< /etc/mailname sed -n 's/\([-[:alnum:]@\.]\+\).*/\1/;p;q' | lowerpipe | check_ascii_pipe)"

# barf if lookups are found. They have never been supported here.
if echo " ${dc_other_hostnames} ${dc_smarthost} ${dc_local_interfaces} ${dc_relay_nets} ${dc_relay_domains}"| grep -q '[[:space:]]\(partial-\)\?\(cdb\|dbm\|dbmnz\|\(d\|ipl\|\(n\?wild\)\?l\)search\|nis\)\([\*@]\)\?[[:space:]]*;'; then
  echo >&2 "WARNING: using 'lookup;' constructs in $UE4CC has never been supported! See /usr/share/doc/exim4-config/NEWS.Debian.gz for details."
fi

dc_other_hostnames="$(lowercase $dc_other_hostnames | check_ascii_pipe)"
# add localhost, get rid of spaces, trailing (semi)colons and make the list
# colon separated
local_domains="$(echo @:localhost:"${dc_other_hostnames}" | \
	sed -e 's/[;: ]*$//' -e 's/ *//' -e 's/;/:/g')"

UPEX4C_internal_tmp="$(tempfile -m600 -p ex4)"

trap "rm -f ${UPEX4C_internal_tmp}" EXIT INT TERM

# run-parts emulation, stolen from Branden's /etc/X11/Xsession
# Addition: Use file.rul instead if file if it exists.
run_parts () {
	# reset LC_COLLATE
	unset LANG LC_COLLATE LC_ALL

	if [ -z "$1" ]; then
		errormessage "$0: internal run_parts called without an argument"
	fi
	if [ ! -d "$1" ]; then
		errormessage "$0: internal run_parts called, but $1 does not exist or is not a directory."
	fi
	for F in $(ls $1); do
		if expr "$F" : '[[:alnum:]_-]\+$' > /dev/null 2>&1; then
			if [ -f "$1/$F" ] ; then
				if [ -f "$1/${F}.rul" ] ; then
					echo "$1/${F}.rul"
				else
					echo "$1/$F"
				fi
			fi
		else
			if [ "${UPEX4C_verbose}" = "yes" ] && \
				[ -f "$1/$F" ] && \
				! expr "$F" : '[[:alnum:]_-]\+\.rul'> /dev/null 2>&1 ; then
				echo \
				"internal run-parts: ignoring file: $1/$F" 1>&2
			fi
		fi
	done;
}
# also from Branden
errormessage () {
	# pretty-print messages of arbitrary length (no trailing newline)
	echo "$*" | fold -s -w ${COLUMNS:-80} >&2;
}

cat_parts() {
       if [ -z "$1" ]; then
               errormessage "$0: internal cat_parts called without an argument"
       fi
       if [ ! -d "$1" ]; then
               errormessage "$0: internal cat_parts called, but $1 does not exist or is not a directory."
       fi
       for file in $(run_parts $1); do
               echo "#####################################################"
               echo "### $file"
               echo "#####################################################"
               cat "$file"
	       echo
               echo "#####################################################"
               echo "### end $file"
               echo "#####################################################"
       done
}

gentmpconf() {
	rm -f "${UPEX4C_outputfile}.tmp"
	touch "${UPEX4C_outputfile}.tmp"
	# this can be removed by the end of 2007
	#chown --reference=${TEMPLATEFILE} \
	#	${UPEX4C_outputfile}.tmp ${UPEX4C_outputfile}
	#chmod --reference=${TEMPLATEFILE} \
	#		${UPEX4C_outputfile}.tmp ${UPEX4C_outputfile}
	if [ "$(id -u)" = "0" ]; then 
		chown root:Debian-exim "${UPEX4C_outputfile}.tmp"
		[ -e "${UPEX4C_outputfile}" ] && \
		chown root:Debian-exim "${UPEX4C_outputfile}"
	fi
	chmod 640 "${UPEX4C_outputfile}.tmp"
	if [ -e "${UPEX4C_outputfile}" ]; then
	  chmod 640 "${UPEX4C_outputfile}"
	fi
}

removecomments(){
	if [ "${UPEX4C_comments}" = "no" ] ; then
		grep -E -v '^[[:space:]]*#' | sed -e '/^$/N;/\n$/D' ;
	else
		cat
	fi
}

gentmpconf

cat << EOF >> "${UPEX4C_outputfile}.tmp"
#########
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# This file was generated dynamically from
EOF

if [ "${dc_use_split_config}" = "true" ] ; then
cat << EOF >> "${UPEX4C_outputfile}.tmp"
# split config files in the $UPEX4C_confd/ directory.
EOF
else
cat << EOF >> "${UPEX4C_outputfile}.tmp"
# non-split config ($UPEX4C_confdir/exim4.conf.localmacros
# and $UPEX4C_confdir/exim4.conf.template).
EOF
fi

cat << EOF >> "${UPEX4C_outputfile}.tmp"
# The config files are supplemented with package installation/configuration
# settings managed by debconf. This data is stored in
# $UPEX4C_confdir/update-exim4.conf.conf
# Any changes you make here will be lost.
# See /usr/share/doc/exim4-base/README.Debian.gz and update-exim4.conf(8)
# for instructions of customization.
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
# WARNING WARNING WARNING
#########
EOF

# handle ";" in input values as separator change

for field in $UPEX4C_semicolon; do
  if eval echo \$$field | grep -q ";"; then
    eval temp=\$$field
    if ! echo $temp | grep -q "^<"; then
      temp="<; $temp"
      eval "$field='$temp'"
    fi
  fi
done

# fix up smarthost line: change semicolons into single colons
dc_smarthost="$(lowercase $dc_smarthost | check_ascii_pipe | sed 's/;/:/g')"

dc_relay_nets="$(lowercase $dc_relay_nets | check_ascii_pipe)"

if echo "$dc_relay_nets" | grep -q '^<;'; then
  dc_relay_nets="$dc_relay_nets ; 127.0.0.1 ; ::1"
else
  dc_relay_nets="$dc_relay_nets : 127.0.0.1 : ::::1"
fi

dc_eximconfig_configtype="$(lowercase $dc_eximconfig_configtype | check_ascii_pipe)"
dc_hide_mailname="$(lowercase $dc_hide_mailname | check_ascii_pipe)"
dc_readhost="$(lowercase $dc_readhost | check_ascii_pipe)"
case "$dc_eximconfig_configtype" in
	satellite|smarthost)
        if [ "${dc_hide_mailname}" = "true" ] && [ -n "${dc_readhost}" ] ; then
		hide_mailname=1
	fi
	;;
	local)
	;;
	internet)
	;;
	none|*)
                if [ "${dc_use_split_config}" = "true" ] ; then
                        for i in ${UPEX4C_sections} ; do
                                cat_parts "${UPEX4C_confd}/$i"
                        done | \
                        removecomments \
			>> "${UPEX4C_outputfile}.tmp"
                else
			LOCALMACROS=""
			if [ -e "/etc/exim4/exim4.conf.localmacros" ]; then
			  LOCALMACROS="/etc/exim4/exim4.conf.localmacros"
			fi
                        cat "${LOCALMACROS:-/dev/null}" "${TEMPLATEFILE:-/dev/null}" | \
                        removecomments \
                                >> "${UPEX4C_outputfile}.tmp"
                fi
		mv -f "${UPEX4C_outputfile}.tmp" "${UPEX4C_outputfile}"
		chmod "${CFILEMODE}" "${UPEX4C_outputfile}"
		[ "${UPEX4C_verbose}" = "yes" ] && \
			echo "Not substituting variables since conftype is none (or other)"
		exit 0
	;;
esac

UPEX4C_macros="##############################################\n"
UPEX4C_macros="${UPEX4C_macros}# the following macro definitions were created\n"
UPEX4C_macros="${UPEX4C_macros}# dynamically by $0\n"

preprocess_macro() {
  macroname="${1:-}"
  shift
  contents="$(lowercase ${@:-empty} | check_ascii_pipe)"
  printf "%s" ".ifndef $macroname\n$macroname=$contents\n.endif\n"
}

seed_macro() {
  UPEX4C_macros="${UPEX4C_macros}$(preprocess_macro "$1" "$2")"
}

file2macros() {
  file="$1"
  < $1 \
    sed -n '/^[[:upper:]]/p;' | \
    grep -v '^CFILEMODE=' | \
    while read line; do
      errormessage "undocumented line $line found in $1, generating exim macro"
      left="$(echo $line | sed 's/\([^=]*\).*/\1/')"
      right="$(echo $line | sed 's/[^=]*=\(.*\)/\1/')"
      preprocess_macro "$left" "$right"
    done
}

if [ "${dc_local_interfaces}" != "" ] ; then
	seed_macro "MAIN_LOCAL_INTERFACES" "${dc_local_interfaces}"
fi

if [ "${dc_minimaldns}" = "true" ] ; then
	seed_macro "DC_minimaldns" "1"
	if guessed_name="$(hostname --fqdn | lowerpipe | check_ascii_pipe | grep '\.')" ; then
		seed_macro "MAIN_HARDCODE_PRIMARY_HOSTNAME" "$guessed_name"
	else
		errormessage "hostname --fqdn did not return a fully qualified name, dc_minimaldns will not work. Please fix your /etc/hosts setup."
	fi
fi

if [ -n "${hide_mailname:-}" ]; then
  seed_macro "HIDE_MAILNAME" "${hide_mailname:-}"
fi
seed_macro "MAIN_PACKAGE_VERSION" "$UPEX4C_version"
seed_macro "MAIN_LOCAL_DOMAINS" "${local_domains}"
seed_macro "MAIN_RELAY_TO_DOMAINS" "${dc_relay_domains}"
seed_macro "ETC_MAILNAME" "$mailname"
seed_macro "LOCAL_DELIVERY" "${dc_localdelivery}"
seed_macro "MAIN_RELAY_NETS" "${dc_relay_nets}"
seed_macro "DCreadhost" "${dc_readhost}"
seed_macro "DCsmarthost" "${dc_smarthost}"
seed_macro "DC_eximconfig_configtype" "${dc_eximconfig_configtype}"
seed_macro "DCconfig_${dc_eximconfig_configtype}" "1"

# dump everything starting with a capital into macros as well
# this is going to stay undocumented, but fixes PEBCAK where people write
# macros into ue4cc.

UPEX4C_macros="${UPEX4C_macros}$(file2macros $UE4CC)"

UPEX4C_macros="${UPEX4C_macros}##############################################\n"

case "${dc_use_split_config}" in
true)
	for i in ${UPEX4C_sections} ; do
		echo "# begin processing $i #####"
		cat_parts "${UPEX4C_confd}/$i"
		echo "# end of $i  #####"
	done \
	| removecomments \
	| sed "s|^\(UPEX4CmacrosUPEX4C.*\)$|\1\n$UPEX4C_macros|" \
	>> "${UPEX4C_outputfile}.tmp"
	RELEVANTTEMPLATE="$UPEX4C_confd"
;;
false)
	if [ ! -r "$TEMPLATEFILE" ] ; then
		echo "Error: Unsplit config selected and $TEMPLATEFILE missing ... exiting" 1>&2
		exit 1
	fi
	LOCALMACROS=""
	if [ -e "/etc/exim4/exim4.conf.localmacros" ]; then
		LOCALMACROS="${UPEX4C_confdir}/exim4.conf.localmacros"
	fi
	cat "${LOCALMACROS:-/dev/null}" "${TEMPLATEFILE:-/dev/null}" \
	| removecomments \
	| sed "s|^\(UPEX4CmacrosUPEX4C.*\)$|\1\n$UPEX4C_macros|" \
 	>> "${UPEX4C_outputfile}.tmp"
	RELEVANTTEMPLATE="$TEMPLATEFILE"
;;
*)
	errormessage "Invalid value for dc_use_split_config: \"${dc_use_split_config}\", exiting."
	rm -f "${UPEX4C_outputfile}.tmp"
	exit 1
;;
esac

# check for left-over DEBCONF strings that may cause installation trouble
# (fix PEBCAK for people who don't accept conffile changes and don't
# read docs)
if grep -qr '^[^#]*DEBCONF[[:lower:]_]\+DEBCONF' $RELEVANTTEMPLATE \
   && ! grep -qr '^[[:space:]]*DEBCONFstringOK_config_adapted[[:space:]]*=' $RELEVANTTEMPLATE; then
  errormessage "DEBCONFsomethingDEBCONF found in exim configuration. This is most probably caused by you upgrading to exim4 4.67-3 or later without accepting the suggested conffile changes. Please read /usr/share/doc/exim4-config/NEWS.Debian.gz for 4.67-2 and 4.67-4"
fi

# check for left-over UPEX4CmacrosUPEX4C comment string that may cause
# installation trouble (fix PEBCAK for people who don't accept conffile
# changes and don't read docs)
if grep -qr '# UPEX4CmacrosUPEX4C' $RELEVANTTEMPLATE \
   && ! grep -qr '^[[:space:]]*UPEX4CmacrosOK_config_adapted[[:space:]]*=' $RELEVANTTEMPLATE; then
  errormessage "UPEX4CmacrosUPEX4C found in an exim configuration comment. This is most probably caused by you upgrading to exim4 4.67-5 or later without accepting the suggested conffile changes. Please read /usr/share/doc/exim4-config/NEWS.Debian.gz for 4.67-5"
fi


# test validity if called without -o
if [ "${UPEX4C_outputfile}" = "${UPEX4C_autoconfigfile}" ] && \
	[ -x "${EXIM}" ] ; then
	if ! "${EXIM}" -C "${UPEX4C_outputfile}.tmp" -bV > /dev/null ; then
	        # we have an error in the configuration file. Do not install
		# and activate. However, errors in string expansions inside
		# the configuration file are not detected by this check!
		errormessage "Invalid new configfile ${UPEX4C_outputfile}.tmp, not installing ${UPEX4C_outputfile}.tmp to ${UPEX4C_outputfile}"
		exit 1
	fi
fi

mv -f "${UPEX4C_outputfile}.tmp" "${UPEX4C_outputfile}"
chmod "${CFILEMODE}" "${UPEX4C_outputfile}"

# end of file
