Package: postfix Version: 2.4.3-1 Severity: wishlist Tags: patch
Hello, The attached patch to /etc/init.d/postfix allows running and controlling more than one postfix instance. I am taking advantage of the postfix design, such that one configuration is selected as primary at compile time (usually /etc/postfix), and then any additional config directory trees which describe a separate postfix instance must be listed under the alternate_config_directories parameter in /etc/postfix/main.cf. I use this information to enumerate config directories belonging to each instance, and then loop over them for every requested action. More information about this config parameter can be found at: http://www.postfix.org/postconf.5.html#alternate_config_directories Please feel free to apply this patch at the next release if you find the idea useful. Cheers Peter P.S. I deliberately did not maintain correct indentation in order to obtain a smaller diff. Please fix for clarity.
--- /etc/init.d/postfix.orig 2007-06-02 01:44:00.000000000 -0400 +++ /etc/init.d/postfix 2007-07-18 10:59:24.000000000 -0400 @@ -7,6 +7,7 @@ PATH=/bin:/usr/bin:/sbin:/usr/sbin DAEMON=/usr/sbin/postfix +DEFAULT_CONFIG="/etc/postfix" NAME=Postfix TZ= unset TZ @@ -16,13 +17,18 @@ test -f /etc/default/postfix && . /etc/default/postfix -test -x $DAEMON && test -f /etc/postfix/main.cf || exit 0 +test -x $DAEMON && test -f "$DEFAULT_CONFIG/main.cf" || exit 0 . /lib/lsb/init-functions #DISTRO=$(lsb_release -is 2>/dev/null || echo Debian) running() { - queue=$(postconf -h queue_directory 2>/dev/null || echo /var/spool/postfix) + if [ -n $1 ]; then + CFG_DIR="$1" + else + CFG_DIR="$DEFAULT_CONFIG" + fi + queue=$(postconf -c $CFG_DIR -h queue_directory 2>/dev/null) if [ -f ${queue}/pid/master.pid ]; then pid=$(sed 's/ //g' ${queue}/pid/master.pid) exe=$(ls -l /proc/$pid/exe 2>/dev/null | sed 's/.* //; s/.*\///') @@ -31,24 +37,56 @@ fi fi } + +parse_alt_cfg() { + OIFS=$IFS + IFS= + while read line ; do + # skip comments/blanks + if echo $line | grep -E "^[[:space:]]*$|^#" &>/dev/null; then continue ;fi + + # look for alt config parameter + if echo $line | grep "^alternate_config_directories[[:space:]]*=" &>/dev/null; then + ALT_CONFIGS=$(echo $line | sed 's/alternate_config_directories\s*=//') + ALT_CFG="y" + # if a line starts with a space and we are stil within an alt config - append + elif [ "$ALT_CFG" = "y" ] && echo $line | grep "^[[:space:]]" &>/dev/null; then + ALT_CONFIGS="$ALT_CONFIGS $line" + # otherwise a different parameter is encountered - we are done + else + ALT_CFG="" + fi + done < "$DEFAULT_CONFIG/main.cf" + + IFS=$OIFS +} + +parse_alt_cfg +CONFIGS="$DEFAULT_CONFIG $ALT_CONFIGS" + + + case "$1" in start) - log_daemon_msg "Starting Postfix Mail Transport Agent" postfix - RUNNING=$(running) + + for CFG in $CONFIGS; do + + log_daemon_msg "Starting Postfix Mail Transport Agent" "$CFG" + RUNNING=$(running "$CFG") if [ -n "$RUNNING" ]; then log_end_msg 0 else # see if anything is running chrooted. - NEED_CHROOT=$(awk '/^[0-9a-z]/ && ($5 ~ "[-yY]") { print "y"; exit}' /etc/postfix/master.cf) + NEED_CHROOT=$(awk '/^[0-9a-z]/ && ($5 ~ "[-yY]") { print "y"; exit}' "$CFG/master.cf") if [ -n "$NEED_CHROOT" ] && [ -n "$SYNC_CHROOT" ]; then # Make sure that the chroot environment is set up correctly. oldumask=$(umask) umask 022 - cd $(postconf -h queue_directory) + cd $(postconf -c "$CFG" -h queue_directory) # if we're using unix:passwd.byname, then we need to add etc/passwd. - local_maps=$(postconf -h local_recipient_maps) + local_maps=$(postconf -c "$CFG" -h local_recipient_maps) if [ "X$local_maps" != "X${local_maps#*unix:passwd.byname}" ]; then if [ "X$local_maps" = "X${local_maps#*proxy:unix:passwd.byname}" ]; then sed 's/^\([^:]*\):[^:]*/\1:x/' /etc/passwd > etc/passwd @@ -71,19 +109,24 @@ umask $oldumask fi - if start-stop-daemon --start --exec ${DAEMON} -- quiet-quick-start; then + if start-stop-daemon --start --exec ${DAEMON} -- -c "$CFG" quiet-quick-start; then log_end_msg 0 else log_end_msg 1 fi fi + + done ;; stop) - RUNNING=$(running) - log_daemon_msg "Stopping Postfix Mail Transport Agent" postfix + + for CFG in $CONFIGS; do + + RUNNING=$(running "$CFG") + log_daemon_msg "Stopping Postfix Mail Transport Agent" "$CFG" if [ -n "$RUNNING" ]; then - if ${DAEMON} quiet-stop; then + if ${DAEMON} -c "$CFG" quiet-stop; then log_end_msg 0 else log_end_msg 1 @@ -91,6 +134,8 @@ else log_end_msg 0 fi + + done ;; restart) @@ -99,16 +144,26 @@ ;; force-reload|reload) - log_action_begin_msg "Reloading Postfix configuration" - if ${DAEMON} quiet-reload; then + + for CFG in $CONFIGS; do + + log_action_begin_msg "Reloading Postfix configuration ($CFG)" + if ${DAEMON} -c "$CFG" quiet-reload; then log_action_end_msg 0 else log_action_end_msg 1 fi + + done ;; flush|check|abort) - ${DAEMON} $1 + + for CFG in $CONFIGS; do + + ${DAEMON} -c "$CFG" $1 + + done ;; *)