Home | History | Annotate | Line # | Download | only in conf
      1 #!/bin/sh
      2 #	$NetBSD: postfix-wrapper,v 1.2 2025/02/25 19:15:41 christos Exp $
      3 #
      4 
      5 #++
      6 # NAME
      7 #	postfix-wrapper 1
      8 # SUMMARY
      9 #	trivial but useful multi-instance manager
     10 # SYNOPSIS
     11 #	postfix command
     12 # DESCRIPTION
     13 #	Postfix versions 2.6 and later provide support for multiple
     14 #	Postfix instances. Instances share executable files and
     15 #	documentation, but have their own directories for configuration,
     16 #	queue and data files. In many cases different instances
     17 #	have different myhostname and inet_interfaces settings,
     18 #	though this is not always necessary.
     19 #
     20 #	This command implements a trivial Postfix multi-instance
     21 #	manager. It simply applies commands such as "postfix start"
     22 #	to all the applicable Postfix instances.
     23 # MANAGING MULTIPLE INSTANCES
     24 # .ad
     25 # .fi
     26 #	To hook the postfix-wrapper multi-instance manager into
     27 #	Postfix, see the POSTFIX-WRAPPER INITIALIZATION section
     28 #	below.  To create a new Postfix instance, see the CREATING
     29 #	A NEW POSTFIX INSTANCE section below.
     30 #
     31 #	To start, stop, get status, etc., with multiple Postfix
     32 #	instances, use:
     33 #
     34 # .nf
     35 #	    # postfix command
     36 # .fi
     37 #
     38 #	For example, to find out what Postfix instances are configured:
     39 #
     40 # .nf
     41 #	    # postfix status
     42 # .fi
     43 #
     44 #	The postfix(1) command invokes the postfix-wrapper command.
     45 #	This in turn applies the postfix(1) command to the default
     46 #	Postfix instance, and to each instance specified with the
     47 #	default main.cf file's multi_instance_directories parameter
     48 #	value.
     49 #
     50 #	The postfix-wrapper command will start, stop, reload, etc.,
     51 #	only Postfix instances that have "multi_instance_enable =
     52 #	yes" in their main.cf files.  When an instance is disabled,
     53 #	postfix-wrapper replaces "start" commands by "check" so
     54 #	that problems will still be reported.
     55 #
     56 #	The startup order is taken from the multi_instance_directories
     57 #	parameter; the default instance is prepended to the list.
     58 #	The startup order is used for all postfix(1) commands,
     59 #	except for commands that stop Postfix instances. In those
     60 #	cases the order is reversed.
     61 # MANAGING INDIVIDUAL INSTANCES
     62 # .ad
     63 # .fi
     64 #	To manage an individual Postfix instance, use:
     65 #
     66 # .nf
     67 #	    # postfix -c /path/to/config_directory command
     68 # .fi
     69 #
     70 #	This is also needed to manage the default Postfix instance,
     71 #	after you turn on multi-instance support.
     72 #
     73 #	To use the Postfix sendmail command with a non-default
     74 #	Postfix instance, use:
     75 #
     76 # .nf
     77 #	    # sendmail -C /path/to/config_directory ...
     78 # .fi
     79 #
     80 #	Note 1: that's capital C, not lower-case c.
     81 #
     82 #	Note 2: only the default Postfix instance will check or
     83 #	update the shared Postfix files, including the executable
     84 #	files and documentation.
     85 # POSTFIX-WRAPPER INITIALIZATION
     86 # .ad
     87 # .fi
     88 #	To hook this program into Postfix, execute the command
     89 #	shown below.
     90 #
     91 #	This command should be entered as one line.
     92 #
     93 #	In the example, replace /etc/postfix with the default Postfix
     94 #	configuration directory, and replace /usr/libexec/postfix
     95 #	with the daemon directory pathname of the default Postfix
     96 #	instance.
     97 #
     98 # .nf
     99 #	    # postconf -c /etc/postfix -e
    100 #		"multi_instance_enable=yes"
    101 #		"multi_instance_wrapper=/usr/libexec/postfix/postfix-wrapper"
    102 # .fi
    103 # CREATING A NEW POSTFIX INSTANCE
    104 # .ad
    105 # .fi
    106 #	To create a Postfix instance called "postfix-test", start
    107 #	with generic main.cf and master.cf files and customize the
    108 #	locations of the queue and data directories with the commands
    109 #	shown below.  The last command updates main.cf and creates
    110 #	any directories that Postfix will need.
    111 #
    112 #	Each command below should be entered as one line.
    113 #
    114 #	In the example, replace /etc/postfix with the default Postfix
    115 #	configuration directory, and replace /usr/libexec/postfix
    116 #	with the daemon directory pathname of the default Postfix
    117 #	instance.
    118 #
    119 # .nf
    120 #	    # mkdir /etc/postfix-test
    121 #	    # cp /usr/libexec/postfix/main.cf /etc/postfix-test
    122 #	    # cp /usr/libexec/postfix/master.cf /etc/postfix-test
    123 #	    # postconf -c /etc/postfix-test -e 
    124 #		"multi_instance_name=postfix-test"
    125 #	    # postfix -c /etc/postfix post-install
    126 #		"config_directory=/etc/postfix-test"
    127 #		"queue_directory=/var/spool/postfix-test"
    128 #		"data_directory=/var/lib/postfix-test"
    129 #		create-missing
    130 # .fi
    131 #
    132 #	Register this Postfix instance with the default instance.
    133 #	This command should be entered as one line.
    134 #
    135 # .nf
    136 #	    # postconf -e "multi_instance_directories=`postconf
    137 #		-h multi_instance_directories` /etc/postfix-test"
    138 # .fi
    139 #
    140 #	Edit the myhostname and inet_interfaces main.cf parameters,
    141 #	so that they will not conflict with the default Postfix
    142 #	instance, and change whatever else needs to be changed.
    143 #
    144 #	Test the instance with:
    145 #
    146 # .nf
    147 #	    # postfix -c /etc/postfix-test start
    148 #	    # postfix -c /etc/postfix-test status
    149 #	    [ other tests ... ]
    150 # .fi
    151 #
    152 #	When everything is working satisfactorily, enable start/stop/etc.
    153 #	by the multi-instance manager:
    154 #
    155 # .nf
    156 #	    # postconf -c /etc/postfix-test -e multi_instance_enable=yes
    157 # DIAGNOSTICS
    158 # .ad
    159 # .fi
    160 #	When an operation fails, the affected Postfix instance logs
    161 #	a message, and the multi-instance manager skips to the next
    162 #	instance.
    163 # BUGS
    164 #	Support for the multi_instance_group feature is not implemented.
    165 # SEE ALSO
    166 #	postfix(1) Postfix control program
    167 #	postfix-wrapper(5) multi-instance manager API
    168 #	postmulti(1) full-blown multi-instance manager
    169 # LICENSE
    170 # .ad
    171 # .fi
    172 #	The Secure Mailer license must be distributed with this software.
    173 # AUTHOR(S)
    174 #	Wietse Venema
    175 #	IBM T.J. Watson Research
    176 #	P.O. Box 704
    177 #	Yorktown Heights, NY 10598, USA
    178 #--
    179 
    180 # Sanity checks.
    181 
    182 : ${command_directory?"do not invoke this command directly"}
    183 : ${daemon_directory?"do not invoke this command directly"}
    184 
    185 # Readability.
    186 
    187 POSTCONF=$command_directory/postconf
    188 POSTFIX=$command_directory/postfix
    189 
    190 # Canonicalize the instance directory list. The list is specified
    191 # in startup order.
    192 
    193 instance_dirs=`$POSTCONF -h multi_instance_directories | sed 'y/,/ /'` ||
    194     exit 1
    195 
    196 case "$1" in
    197   stop|quick-stop|abort|drain)
    198 	all_dirs=
    199 	for dir in $config_directory $instance_dirs
    200 	do
    201 	    all_dirs="$dir $all_dirs"
    202 	done;;
    203      *) all_dirs="$config_directory $instance_dirs";;
    204 esac
    205 
    206 # Execute the command on all applicable instances. When a Postfix
    207 # instance is disabled, replace "postfix start" by "postfix check"
    208 # so that problems will still be reported.
    209 
    210 err=0
    211 for dir in $all_dirs
    212 do
    213     case "$1" in
    214     start)
    215 	test "`$POSTCONF -c $dir -h multi_instance_enable`" = yes || {
    216 	    $POSTFIX -c $dir check || err=$?
    217 	    continue
    218 	};;
    219     stop|abort|drain|flush|reload)
    220 	test "`$POSTCONF -c $dir -h multi_instance_enable`" = yes ||
    221 	    continue;;
    222     esac
    223     $POSTFIX -c $dir "$@" || err=$?
    224 done
    225 
    226 exit $err
    227