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