#!/bin/sh

# To view the formatted manual page of this file, type:
#	POSTFIXSOURCE/mantools/srctoman - post-install | nroff -man

#++
# NAME
#	post-install 1
# SUMMARY
#	Postfix post-installation script
# SYNOPSIS
#	postfix post-install [name=value] command ...
# DESCRIPTION
#	The post-install script performs the finishing touch of a Postfix
#	installation, after the executable programs and configuration
#	files are installed. Usage is one of the following:
# .IP o
#	While installing Postfix from source code on the local machine, the
#	script is run by the postfix-install script to update selected file
#	or directory permissions and to update Postfix configuration files.
# .IP o
#	While installing Postfix from a pre-built package, the script is run
#	by the package management procedure to set all file or directory
#	permissions and to update Postfix configuration files.
# .IP o
#	The script can be used to change installation parameter settings such
#	as mail_owner or setgid_group after Postfix is already installed.
# .IP o
#	The script can be used to create additional Postfix instances.
# .IP o
#	At Postfix start-up time, the script is run from "postfix check" to
#	create missing queue directories.
# .PP
#	The post-install script is controlled by installation parameters.
#	Specific parameters are described at the end of this document.
#	All installation parameters must be specified ahead of time via
#	one of the methods described below.
#
#	Arguments
# .IP create-missing
#	Create missing queue directories with ownerships and permissions
#	according to the contents of $global_config_directory/postfix-files,
#	using the mail_owner and setgid_group parameter settings from the
#	command line or from the installed main.cf file.
#
#	This is required at Postfix start-up time.
# .IP create-instance
#	Create a postfix instance. This creates the configuration directory
#	if necessary, and populates it with stock configuration files.
#	A new value of the queue_directory parameter must be specified on
#	the command line, or already exist in a main.cf file in the new
#	configuration directory.  Additional manual editing of the new
#	main.cf is almost always required before the new instance
#	is fully operational. The \fBdisable_start\fR parameter of the new
#	instance is set to \fByes\fR. This prevents the instance from being
#	started prematurely. Remember to change this to \fBno\fR (the default
#	value when the parameter is not set in \fBmain.cf\fR) after you have
#	finished configuring the instance.
#
#	Additional instances listed in the \fBalternate_config_directories\fR
#	parameter of the default \fBmain.cf\fR file (always true when instances
#	are added via this script) are automatically updated when an upgrade to
#	a new version of Postfix updates the default master.cf or main.cf file,
#	or changes queue file ownership and/or permissions.
# .IP set-permissions
#	Set all file/directory ownerships and permissions according to the
#	contents of $global_config_directory/postfix-files, using the
#	mail_owner and setgid_group parameter settings from the command line
#	or from the installed main.cf file. Implies create-missing.
#
#	This is required when installing Postfix from a pre-built package,
#	or when changing the mail_owner or setgid_group installation parameter
#	settings after Postfix is already installed. Use only with the default
#	instance to consistently update all instances.
# .IP upgrade-permissions
#	Update ownership and permission of existing files/directories as
#	specified in $global_config_directory/postfix-files, using the
#	mail_owner and setgid_group parameter settings from the command
#	line or from the installed main.cf file. Implies create-missing.
#
#	This is required when upgrading an existing Postfix instance.
#	Use only with the default instance to consistently update all
#	instances.
# .IP upgrade-configuration
#	Edit the installed main.cf and master.cf files, in order to account
#	for missing services and to fix deprecated parameter settings.
#
#	This is required when upgrading an existing Postfix instance. When
#	applied to the default instance, has the side-effect of updating all
#	secondary instances.
# .IP upgrade-source
#	Short-hand for: upgrade-permissions upgrade-configuration.
#
#	This is recommended when upgrading Postfix from source code.
#	Use only with the default instance to consistently update all
#	instances.
# .IP upgrade-package
#	Short-hand for: set-permissions upgrade-configuration.
#
#	This is recommended when upgrading Postfix from a pre-built package.
#	Use only with the default instance to consistently update all
#	instances.
# .IP first-install-reminder
#	Remind the user that they still need to configure main.cf and the
#	aliases file, and that newaliases still needs to be run.
#
#	This is recommended when Postfix is installed for the first time.
# MULTIPLE POSTFIX INSTANCES
# .ad
# .fi
#	Multiple Postfix instances on the same machine can share command and
#	daemon program files but must have separate configuration and queue
#	directories. Also since two instances of smtpd(8) cannot listen on the
#	same IP address and port, either the "inet_interfaces" parameter in
#	main.cf or the "smtpd" entry in the "master.cf" file will need to be
#	changed appropriately in the second instance. This last step (in
#	addition to other likely changes of the secondary instance main.cf)
#	lies outside the scope of this script at this time, and will need to
#	be performed by hand after the instance is created.
#
#	When upgrading a multi-instance configuration of a Postfix release
#	prior to 2.2, make sure to list \fBall\fR secondary instances in
#	\fBalternate_config_directories\fR parameter of the default main.cf
#	file before performing the upgrade. If upgrading via binary package
#	that does not execute "\fBpostfix post-install upgrade-package\fR",
#	run the command by hand after the upgrade.
#
#	To create a secondary Postfix installation on the same machine,
#	execute:
# .na
# .nf
# .RS
# .HP
# \fBpostfix post-install create-instance\fR \e
# \fBconfig_directory=\fIsecondary-config_directory\fR \e
# \fBqueue_directory=\fIsecondary-queue_directory\fR \e
# \fBsyslog_name=\fIsecondary-syslog_name\fR
# .RE
# .ad
# .fi
# .PP
#	This creates a secondary Postfix queue directory, sets access
#	permissions, and saves the specified installation parameters to the
#	secondary main.cf file.  The stock main.cf and master.cf files are
#	copied from the global configuration directory if not already present.
#	To ensure that upgrades update the configuration files of the new
#	instance, the new configuration directory is added to the
#	alternate_config_directories parameter in the default main.cf file.
#
# INSTALLATION PARAMETER INPUT METHODS
# .ad
# .fi
#	Parameter settings can be specified through a variety of
#	mechanisms.  In order of decreasing precedence these are:
# .IP "default instance"
#	For secondary Postfix instances, all installation parameters other
#	than queue_directory, syslog_name (and of course config_directory)
#	need to be the same as the corresponding parameters in the default
#	configuration. To change these system-wide parameters, first edit
#	the default main.cf file, and then update all remaining instances
#	by running "\fBpostfix post-install upgrade-package\fR".
# .sp
#	Note, most of the system-wide parameters should not be changed,
#	except as part of upgrading or re-installing the Postfix software.
#	Always run "\fBpostfix post-install upgrade-package\fR" after
#	upgrading Postfix from a binary package (well constructed binary
#	packages will do this automatically).
# .IP "command line"
#	Parameter settings can be given as name=value arguments on
#	the post-install command line. Settings that override the
#	installed main.cf file are saved.
# .IP "installed configuration files"
#	If a parameter is not specified via the command line, post-install
#	will attempt to extract its value from the already installed
#	Postfix main.cf configuration file. These settings have the lowest
#	precedence.
# INSTALLATION PARAMETER DESCRIPTION
# .ad
# .fi
#	The description of installation parameters is as follows:
# .IP config_directory
#	The directory for Postfix configuration files. When creating a new
#	postfix instance, specify its configuration directory as a command-line
#	variable assignment, rather than as a \fB-c\fR option to the 
#	\fIpostfix\fR command:
# .na
# .nf
# .RS
# .HP
# \fBpostfix post-install create-instance \e
# config_directory=/etc/postfix/2nd \e
# queue_directory=/var/spool/postfix/2nd \e
# syslog_name=postfix-2nd\fR
# .RE
# .ad
# .fi
# .IP queue_directory
#	The directory for Postfix queues. The queue directories of distinct
#	instances must not be the same. When creating a secondary Postfix
#	instance you must specify an explicit queue directory.
# .IP syslog_name
#	The instance prefix in Postfix log messages. It is best (but not
#	required) to configure each Postfix instance with its own
#	\fIsyslog_name\fR, so that one can distinguish the corresponding
#	syslog entries.
# .IP daemon_directory
#	The directory for Postfix daemon programs. This directory
#	should not be in the command search path of any users.
# .IP command_directory
#	The directory for Postfix administrative commands. This
#	directory should be in the command search path of adminstrative users.
# .IP sendmail_path
#	The full pathname for the Postfix sendmail command.
#	This is the Sendmail-compatible mail submission interface.
# .IP newaliases_path
#	The full pathname for the Postfix newaliases command.
#	This is the Sendmail-compatible command to build aliases(5) databases
#	for the Postfix local delivery agent.
# .IP mailq_path
#	The full pathname for the Postfix mailq command.
#	This is the Sendmail-compatible command to list the mail queue.
# .IP mail_owner
#	The owner of the Postfix queue. Its numerical user ID and group ID
#	must not be used by any other accounts on the system.
# .IP setgid_group
#	The group for mail submission and for queue management commands.
#	Its numerical group ID must not be used by any other accounts on the
#	system, not even by the mail_owner account.
# .IP html_directory
#	The directory for the Postfix HTML files.
# .IP manpage_directory
#	The directory for the Postfix on-line manual pages.
# .IP sample_directory
#	The directory for the Postfix sample configuration files.
# .IP readme_directory
#	The directory for the Postfix README files.
# .IP global_config_directory
#	The directory containing configuration files and scripts shared
#	by all Postfix instances.
# SEE ALSO
#	postfix(1) Postfix control program.
#	postfix-install(1) Install Postfix from source.
# FILES
#	$config_directory/main.cf, Postfix installation parameters.
#	$global_config_directory/install.cf, obsolete configuration file.
#	$global_config_directory/postfix-files, installation control file.
#	$global_config_directory/postfix-script, postfix control script.
#	$global_config_directory/post-install, this script.
#	$global_config_directory/initial-main.cf, stock main.cf file.
#	$global_config_directory/initial-master.cf, stock master.cf file.
# LICENSE
# .ad
# .fi
#	The Secure Mailer license must be distributed with this software.
# AUTHOR(S)
#	Wietse Venema
#	IBM T.J. Watson Research
#	P.O. Box 704
#	Yorktown Heights, NY 10598, USA
#--

umask 022

PATH=/bin:/usr/bin:/usr/sbin:/usr/etc:/sbin:/etc:/usr/contrib/bin:/usr/gnu/bin:/usr/ucb:/usr/bsd
SHELL=/bin/sh
IFS=" 	
"
BACKUP_IFS="$IFS"

INVOKE_AS="postfix post-install"
USAGE="Usage: $INVOKE_AS command ... [name=value ...]
  Multiple commands can be specified; the available commands are:
    create-missing          Create missing queue directories.
    create-instance         Create a new Postfix instance.
    set-permissions	    Set permissions for all Postfix files.
    upgrade-permissions	    Update permissions that changed between releases.
    upgrade-configuration   Make necessary changes to existing configuration files .
    upgrade-source          When installing or upgrading from source code.
    upgrade-package         When installing or upgrading from pre-built package.
    first-install-reminder  Remind of mandatory first-time configuration steps.
    name=value              Specify an installation parameter".

# Process command-line options and parameter settings. Work around
# brain damaged shells. "IFS=value command" should not make the
# IFS=value setting permanent. But some broken standard allows it.

register=; copy_cfs=
create=; set_perms=; upgrade_perms=; upgrade_conf=; first_install_reminder=
obsolete=; keep_list=;

# Don't propagate the queue_directory from the default instance to secondary
# instances. Only command line values below should override any existing
# value in main.cf.
queue_directory=

for arg
do
    case $arg in
                *=*) IFS= eval $arg; IFS="$BACKUP_IFS";;
     create-missing) create=1;;
    create-instance) create=1; register=1; copy_cfs=1;;
	  set-perm*) create=1; set_perms=1;;
      upgrade-perm*) create=1; upgrade_perms=1;;
      upgrade-conf*) upgrade_conf=1;;
     upgrade-source) create=1; upgrade_conf=1; upgrade_perms=1;;
    upgrade-package) create=1; upgrade_conf=1; set_perms=1;;
     first-install*) first_install_reminder=1;;
		  *) echo "$0: Error: $USAGE" 1>&2; exit 1;;
    esac
    shift
done

# Sanity checks.

test -n "$create$upgrade_conf$first_install_reminder" || {
    echo "$0: Error: $USAGE" 1>&2
    exit 1
}

POSTCONF="$command_directory/postconf"

$POSTCONF -d mail_version >/dev/null 2>/dev/null || {
    echo $0: Error: no $POSTCONF command found in $command_directory. 1>&2
    exit 1
}

default_config_directory=`$POSTCONF -d -h config_directory` || exit 1

default_var() {
    $POSTCONF -c "$default_config_directory" "$@"
}

instance_var() {
    $POSTCONF -c "$config_directory" "$@"
}

test -n "$config_directory" ||
    config_directory=${default_config_directory}

# Create or upgrade a non-default Postfix instance.
#
if [ -n "$register" -a "$config_directory" != "$default_config_directory" ]
then
    # alt_cf_dirs is also needed near the end of the script
    # under the same conditions to register the instance if all goes well.
    #
    alt_cf_dirs=`default_var -h alternate_config_directories | tr ',' ' '`

    # Everything except queue_directory is mastered in the default instance.
    #
    for name in daemon_directory command_directory mail_owner \
        setgid_group sendmail_path newaliases_path mailq_path \
	manpage_directory sample_directory readme_directory html_directory \
	global_config_directory
    do
	eval "old=\$$name"
        new=`default_var -h $name` || exit 1
	eval "$name=$new"
	test -z "$old" -o "$old" = "$new" || {
	    echo "$0: Error: $name must match the default configuration" 1>&2
	    echo "Upgrade the default configuration first." 1>&2
	    exit 1
	}
    done

    if [ -n "$copy_cfs" ]
    then
	# Create directory if missing
	#
	test -d "$config_directory" || mkdir -p "$config_directory" || {
	    echo "$0: Error: Could not create configuration directory" 1>&2
	    exit 1
	}

	# Copy main.cf and master.cf if not present.
	#
	for cf in main.cf master.cf
	do
	    test -f "$config_directory/$cf" ||
	    cp -p "$global_config_directory/initial-$cf" \
	    	"$config_directory/$cf" || {
		echo "$0: Error: Could not install initial $cf" 1>&2
		exit 1
	    }
	done
	# In case we are using dynamic maps symlink dynamicmaps.cf
	#
	if [ -f $global_config_directory/dynamicmaps.cf \
	    -a ! -f $config_directory/dynamicmaps.cf ]; then
	    ln -s $global_config_directory/dynamicmaps.cf \
	    $config_directory/dynamicmaps.cf || {
		echo "$0: Error: Could not symlink dynamicmaps.cf" 1>&2
		exit 1
	    }
	fi

	if [ -n "$queue_directory" ]
	then
	    # Update main.cf with new queue_directory parameter.
	    #
	    instance_var -e "queue_directory = $queue_directory" || {
		echo "$0: Error: Failed to set queue_directory parameter" 1>&2
		exit 1
	    }
	else
	    # Load queue_directory parameter from main.cf
	    #
	    queue_directory=`instance_var -h queue_directory` || exit 1
	fi

	# Enforce unique value of each instance queue_directory.
	#
	for cfdir in $default_config_directory $alt_cf_dirs
	do
	    test "$cfdir" = "$config_directory" && continue
	    cfqdir=`$POSTCONF -c "$cfdir" -h queue_directory`
	    test "$queue_directory" = "$cfqdir" && {
		echo "$0: Error: queue_directory same as for $cfdir" 1>&2
		exit 1
	    }
	done
    fi
fi

test -d "$config_directory" || {
    echo $0: Error: $config_directory is not a directory. 1>&2
    exit 1
}

# SunOS5 fmt(1) truncates lines > 1000 characters.

fake_fmt() {
    sed '
    :top
	/^\(  *\)\([^ ][^ ]*\)  */{
	    s//\1\2\
\1/
	    P
	    D
	    b top
	}
    ' | fmt
}

case `uname -s` in
HP-UX*) FMT=cat;;
SunOS*) FMT=fake_fmt;;
     *) FMT=fmt;;
esac

# If a parameter is not set via the command line try to use settings
# from installed configuration files.

# Extract parameter settings from the obsolete install.cf file, as
# a transitional aid.

grep setgid_group $config_directory/main.cf >/dev/null 2>&1 || {
    test -f $config_directory/install.cf  && {
        for name in sendmail_path newaliases_path mailq_path setgid manpages
        do
	eval junk=\$$name
        case "$junk" in
        "") eval unset $name;;
        esac
	   eval : \${$name="\`. $config_directory/install.cf; echo \$$name\`"} \
		|| exit 1
        done
        : ${setgid_group=$setgid}
        : ${manpage_directory=$manpages}
    }
}

# Extract parameter settings from the installed main.cf file.

test -f $config_directory/main.cf && {
    for name in daemon_directory command_directory queue_directory mail_owner \
        setgid_group sendmail_path newaliases_path mailq_path \
	html_directory manpage_directory sample_directory readme_directory \
	global_config_directory syslog_name
    do
	eval junk=\$$name
        case "$junk" in
        "") eval unset $name;;
        esac
        eval : \${$name=\`instance_var -h $name\`} || exit 1
    done
}

# Sanity checks

test -f $global_config_directory/postfix-files || {
    echo $0: Error: $global_config_directory/postfix-files is not a file. 1>&2
    exit 1
}

case $manpage_directory in
 no) echo $0: Error: manpage_directory no longer accepts \"no\" values. 1>&2
     echo Try again with \"$INVOKE_AS manpage_directory=/path/name ...\". 1>&2; exit 1;;
esac

case $setgid_group in
 no) echo $0: Error: setgid_group no longer accepts \"no\" values. 1>&2
     echo Try again with \"$INVOKE_AS setgid_group=groupname ...\" 1>&2; exit 1;;
esac

for path in "$daemon_directory" "$command_directory" "$queue_directory" \
    "$sendmail_path" "$newaliases_path" "$mailq_path" "$manpage_directory" \
    "$global_config_directory"
do
   case "$path" in
   /*) ;;
    *) echo $0: Error: \"$path\" should be an absolute path name. 1>&2; exit 1;;
   esac
done

for path in "$html_directory" "$readme_directory"
do
   case "$path" in
   /*) ;;
   no) ;;
    *) echo $0: Error: \"$path\" should be \"no\" or an absolute path name. 1>&2; exit 1;;
   esac
done

# Find out what parameters were not specified via command line,
# via environment, or via installed configuration files.

missing=
for name in daemon_directory command_directory queue_directory mail_owner \
    setgid_group sendmail_path newaliases_path mailq_path manpage_directory \
    readme_directory html_directory
do
    eval test -n \"\$$name\" || missing="$missing $name"
done

# All parameters must be specified at this point.

test -n "$non_interactive" -a -n "$missing" && {
    cat <<EOF | ${FMT} 1>&2
$0: Error: some required installation parameters are not defined.

- Either the parameters need to be given in the $config_directory/main.cf
file from a recent Postfix installation,

- Or the parameters need to be specified through the process
environment.

- Or the parameters need to be specified as name=value arguments
on the $0 command line,

The following parameters were missing:

    $missing

EOF
    exit 1
}

# Save settings, allowing command line override.

override=
for name in daemon_directory command_directory queue_directory mail_owner \
    setgid_group sendmail_path newaliases_path mailq_path manpage_directory \
    sample_directory readme_directory html_directory \
    global_config_directory syslog_name
do
    eval test \"\$$name\" = \"`instance_var -h $name`\" || {
	override=1
	break
    }
done

test -n "$override" && {
    instance_var -e \
	"daemon_directory = $daemon_directory" \
	"command_directory = $command_directory" \
	"queue_directory = $queue_directory" \
	"mail_owner = $mail_owner" \
	"setgid_group = $setgid_group" \
	"sendmail_path = $sendmail_path" \
	"mailq_path = $mailq_path" \
	"newaliases_path = $newaliases_path" \
	"html_directory = $html_directory" \
	"manpage_directory = $manpage_directory" \
	"sample_directory = $sample_directory" \
	"readme_directory = $readme_directory" \
	"global_config_directory = $global_config_directory" \
	"syslog_name = $syslog_name" \
    || exit 1
}

# Use file/directory status information in
# $global_config_directory/postfix-files.

test -n "$create" && {
    exec <$global_config_directory/postfix-files || exit 1
    while IFS=: read path type owner group mode flags junk
    do
	IFS="$BACKUP_IFS"
	set_permission=
	# Skip comments.
	case $path in
	[$]*) ;;
	   *) continue;;
	esac
	# Skip hard links and symbolic links.
	case $type in
	[hl]) continue;;
	[df]) ;;
	   *) echo unknown type $type for $path in $config_directory/postfix-files1>&2; exit 1;;
	esac
	# Expand $name, and canonicalize null fields.
	for name in path owner group flags
	do
	    eval junk=\${$name}
	    case $junk in
	    [$]*) eval $name=$junk;;
	       -) eval $name=;;
	       *) ;;
	    esac
	done
	# Skip uninstalled files.
	case $path in
	no|no/*) continue;;
	esac
	# Munge paths for alternatives.
	case $path in
		/usr/sbin/sendmail) path=$path.postfix ;;
	esac
	# Pick up the flags.
	case $flags in *u*) upgrade_flag=1;; *) upgrade_flag=;; esac
	case $flags in *c*) create_flag=1;; *) create_flag=;; esac
	case $flags in *r*) prune="";; *) prune="-prune -o -prune";; esac
	case $flags in *o*) obsolete_flag=1;; *) obsolete_flag=;; esac
	# Flag obsolete objects. XXX Solaris 2..9 does not have "test -e".
	if [ -n "$obsolete_flag" ]
	then
	    test -r $path -a "$type" != "d" && obsolete="$obsolete $path"
	    continue;
	else
	    keep_list="$keep_list $path"
	fi
	# Create missing directories with proper owner/group/mode settings.
	if [ -n "$create" -a "$type" = "d" -a \
	     -n "$create_flag" -a ! -d "$path" ]
	then
	    mkdir $path || exit 1
	    set_permission=1
	# Update all owner/group/mode settings.
	elif [ -n "$set_perms" ]
	then
	    set_permission=1
	# Update obsolete owner/group/mode settings.
	elif [ -n "$upgrade_perms" -a -n "$upgrade_flag" ]
	then
	    set_permission=1
	fi
	test -n "$set_permission" && {
	    if [ -n "$group" ]
	    then
	    	findcond="! -user $owner -o ! -perm $mode -o ! -group $group"
	    else
	    	findcond="! -user $owner -o ! -perm $mode"
	    fi

	    # Make sure find arguments are good, i.e. test validity
	    # of $owner and $group if specified.
	    #
	    find /dev/null \( $findcond \) -print >/dev/null || exit 1

	    # Update only files that don't match the desired permissions.
	    # Chmod first, so we don't give away suid to owner. Chmod
	    # is only recursive for directories. Chown and chgrp apply
	    # to all files.
	    # Don't "chmod -R"; queue file status is encoded in mode bits.
	    #
	    find "$path" -type $type \( $findcond \) \
	    	-exec chmod "$mode" "{}" ";" $prune
	    find "$path" \( $findcond \) -exec chown "$owner" "{}" ";" $prune
	    test -z "$group" ||
		find $path \( $findcond \) -exec chgrp "$group" "{}" ";" $prune
	}
    done
    IFS="$BACKUP_IFS"
}

# Upgrade existing Postfix configuration files if necessary.

test -n "$upgrade_conf" && {

    # Add missing relay service to master.cf.

    grep '^relay' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for relay service
	cat >>$config_directory/master.cf <<EOF || exit 1
relay	  unix	-	-	n	-	-	smtp
EOF
    }

    # Add missing flush service to master.cf.

    grep '^flush.*flush' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for flush service
	cat >>$config_directory/master.cf <<EOF || exit 1
flush     unix  -       -       n       1000?   0       flush
EOF
    }

    # Add missing trace service to master.cf.

    grep 'trace.*bounce' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for trace service
	cat >>$config_directory/master.cf <<EOF || exit 1
trace	  unix	-	-	n	-	0	bounce
EOF
    }

    # Add missing verify service to master.cf.

    grep '^verify.*verify' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for verify service
	cat >>$config_directory/master.cf <<EOF || exit 1
verify	  unix	-	-	n	-	1	verify
EOF
    }

    # Fix verify service process limit.

    grep '^verify.*[ 	]0[ 	]*verify' \
	$config_directory/master.cf >/dev/null && {
	    echo Editing $config_directory/master.cf, setting verify process limit to 1
	    ed $config_directory/master.cf <<EOF || exit 1
/^verify.*[ 	]0[ 	]*verify/
s/\([ 	]\)0\([ 	]\)/\11\2/
p
w
q
EOF
    }

    # Change privileged pickup service into unprivileged.

    grep "^pickup[ 	]*fifo[ 	]*n[ 	]*n" \
	$config_directory/master.cf >/dev/null && {
	    echo Editing $config_directory/master.cf, making the pickup service unprivileged
	    ed $config_directory/master.cf <<EOF || exit 1
/^pickup[ 	]*fifo[ 	]*n[ 	]*n/
s/\(n[ 	]*\)n/\1-/
p
w
q
EOF
    }

    # Change private cleanup and flush services into public.

    for name in cleanup flush
    do
	grep "^$name[ 	]*unix[ 	]*[-y]" \
	    $config_directory/master.cf >/dev/null && {
		echo Editing $config_directory/master.cf, making the $name service public
	    ed $config_directory/master.cf <<EOF || exit 1
/^$name[ 	]*unix[ 	]*[-y]/
s/[-y]/n/
p
w
q
EOF
	}
    done

    # File systems have improved since Postfix came out, and all we
    # require now is that defer and deferred are hashed because those
    # can contain lots of files.

    found=`instance_var -h hash_queue_names`
    missing=
    (echo "$found" | grep defer >/dev/null)  || missing="$missing defer"
    (echo "$found" | grep deferred>/dev/null)|| missing="$missing deferred"
    test -n "$missing" && {
	echo fixing main.cf hash_queue_names for missing $missing
	instance_var -e hash_queue_names="$found$missing" || exit 1
    }

    # Turn on safety nets for new features that could bounce mail that
    # would be accepted by a previous Postfix version.
    # This safety net is also documented in LOCAL_RECIPIENT_README.

    unknown_local=unknown_local_recipient_reject_code
    has_lrm=`instance_var -c $config_directory -n local_recipient_maps`
    has_lrjc=`instance_var -c $config_directory -n $unknown_local`

    if [ -z "$has_lrm" -a -z "$has_lrjc" ]
    then
	echo SAFETY: editing main.cf, setting $unknown_local=450.
	echo See the LOCAL_RECIPIENT_README file for details.
	instance_var -c $config_directory -e "$unknown_local = 450" || exit 1
    fi

    # Add missing proxymap service to master.cf.

    grep '^proxymap.*proxymap' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for proxymap service
	cat >>$config_directory/master.cf <<EOF || exit 1
proxymap  unix	-	-	n	-	-	proxymap
EOF
    }

    # Add missing anvil service to master.cf.

    grep '^anvil.*anvil' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for anvil service
	cat >>$config_directory/master.cf <<EOF || exit 1
anvil	  unix	-	-	n	-	1	anvil
EOF
    }

    # Add missing scache service to master.cf.

    grep '^scache.*scache' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for scache service
	cat >>$config_directory/master.cf <<EOF || exit 1
scache	  unix	-	-	n	-	1	scache
EOF
    }

    # Add missing discard service to master.cf.

    grep '^discard.*discard' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for discard service
	cat >>$config_directory/master.cf <<EOF || exit 1
discard	  unix	-	-	n	-	-	discard
EOF
    }

    # Update the tlsmgr fifo->unix service.

    grep "^tlsmgr[ 	]*fifo[ 	]" \
	$config_directory/master.cf >/dev/null && {
	    echo Editing $config_directory/master.cf, updating the tlsmgr from fifo to unix service
	    ed $config_directory/master.cf <<EOF || exit 1
/^tlsmgr[ 	]*fifo[ 	]/
s/fifo/unix/
s/[0-9][0-9]*/&?/
p
w
q
EOF
    }

    # Add missing tlsmgr service to master.cf.

    grep '^tlsmgr.*tlsmgr' $config_directory/master.cf >/dev/null || {
	echo Editing $config_directory/master.cf, adding missing entry for tlsmgr service
	cat >>$config_directory/master.cf <<EOF || exit 1
tlsmgr    unix  -       -       n       1000?   1       tlsmgr
EOF
    }

    # Report (but do not remove) obsolete files.

    test -n "$obsolete" && {
	cat <<EOF | ${FMT}

    Note: the following files or directories still exist but are
    no longer part of Postfix:

    $obsolete

EOF
    }

}

# A reminder if this is the first time Postfix is being installed.

test -n "$first_install_reminder" && {

    ALIASES=`instance_var -c $config_directory -h alias_database | sed 's/^[^:]*://'`
    NEWALIASES_PATH=`instance_var -c $config_directory -h newaliases_path`
    cat <<EOF | ${FMT}

    Warning: you still need to edit myorigin/mydestination/mynetworks
    parameter settings in $config_directory/main.cf.

    See also http://www.postfix.org/faq.html for information about
    dialup sites or about sites inside a firewalled network.

    BTW: Check your $ALIASES file and be sure to set up aliases
    that send mail for root and postmaster to a real person, then
    run $NEWALIASES_PATH.

EOF

}

# A reminder if this is a stock "disabled" main.cf file.

test -n "$first_install_reminder$copy_cfs" && {

    cat <<EOF | ${FMT}

    Warning: your main.cf file has the "disable_start" parameter set to "yes".
    This prevents Postfix from being started before it is fully configured.
    Once you have edited the file to suit your taste, run "postfix enable".

EOF

}

# Secondary Postfix instance management.
#
if [ "$config_directory" = "$default_config_directory" ]
then
    if [ -n "$upgrade_conf" -a -n "${upgrade_perms}${set_perms}" ]
    then
	# When upgrading the main instance,
	# also handle known *existing* secondaries.
	#
	alt_cf_dirs=`default_var -h alternate_config_directories | tr ',' ' '`
	for cfdir in $alt_cf_dirs
	do
	    test "$cfdir" = "$config_directory" && continue
	    test -f "$cfdir/main.cf" -a -f "$cfdir/master.cf" || continue
	    "$global_config_directory/post-install" upgrade-configuration \
	    	${set_perms:+set-permissions} \
		${upgrade_perms:+upgrade-permissions} \
		"config_directory=$cfdir" \
		"command_directory=$command_directory"
	done
    fi
elif [ -n "$register" ]
then
    # When done creating or upgrading a secondary instance,
    # add it to alternate_config_directories if missing.
    #
    listed=0
    for cfdir in $alt_cf_dirs
    do
	test "$cfdir" = "$config_directory" && listed=1
    done
    test "$listed" = "0" && {
	alt_cf_dirs=`echo $alt_cf_dirs "$config_directory" | sed 's/ /, /g'`
	default_var -e "alternate_config_directories = $alt_cf_dirs"
    }
fi

exit 0
